Install.php
114 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin installer helper |
| 4 | * |
| 5 | * @author Parsa Kafi |
| 6 | * @package WP-Parsidate |
| 7 | * @subpackage Core/Install |
| 8 | */ |
| 9 | |
| 10 | namespace WPParsidate\Plugin; |
| 11 | |
| 12 | use WPParsidate\Helper\WordPress; |
| 13 | |
| 14 | class Install { |
| 15 | public static function run(): void { |
| 16 | $pluginData = get_plugin_data( WP_PARSI_ROOT ); |
| 17 | $currentVersion = $pluginData['Version']; |
| 18 | $oldVersion = get_option( WP_PARSI_KEY . '_plugin_version', '5.1.8' ); |
| 19 | $oldSettings = get_option( 'wpp_settings', [] ); |
| 20 | $settings = get_option( WP_PARSI_KEY, [] ); |
| 21 | |
| 22 | if ( ! empty( $oldSettings ) && empty( $settings ) && version_compare( $oldVersion, '6.0', '<' ) ) { |
| 23 | $pluginSettings = array( |
| 24 | // Core |
| 25 | 'admin_lang' => self::isEnable( $oldSettings, 'admin_lang' ), |
| 26 | 'user_lang' => self::isEnable( $oldSettings, 'user_lang' ), |
| 27 | 'persian_date' => self::isEnable( $oldSettings, 'persian_date' ), |
| 28 | 'months_name_type' => $oldSettings['months_name_type'] ?? 'persian', |
| 29 | 'disable_widget_block' => self::isEnable( $oldSettings, 'disable_widget_block' ), |
| 30 | 'enable_fonts' => self::isEnable( $oldSettings, 'enable_fonts' ), |
| 31 | 'debug_mode' => self::isEnable( $oldSettings, 'dev_mode' ), |
| 32 | 'multilingual_support' => self::isEnable( $oldSettings, 'wpp_multilingual_support' ), |
| 33 | |
| 34 | // Convert |
| 35 | 'conv_page_title' => self::isEnable( $oldSettings, 'conv_page_title' ), |
| 36 | 'conv_title' => self::isEnable( $oldSettings, 'conv_title' ), |
| 37 | 'conv_contents' => self::isEnable( $oldSettings, 'conv_contents' ), |
| 38 | 'conv_excerpt' => self::isEnable( $oldSettings, 'conv_excerpt' ), |
| 39 | 'conv_comments' => self::isEnable( $oldSettings, 'conv_comments' ), |
| 40 | 'conv_comment_count' => self::isEnable( $oldSettings, 'conv_comment_count' ), |
| 41 | 'conv_dates' => self::isEnable( $oldSettings, 'conv_dates' ), |
| 42 | 'conv_cats' => self::isEnable( $oldSettings, 'conv_cats' ), |
| 43 | 'conv_arabic' => self::isEnable( $oldSettings, 'conv_arabic' ), |
| 44 | 'conv_permalinks' => self::isEnable( $oldSettings, 'conv_permalinks' ), |
| 45 | |
| 46 | // Tools |
| 47 | 'date_in_admin_bar' => self::isEnable( $oldSettings, 'date_in_admin_bar' ), |
| 48 | |
| 49 | // Integration |
| 50 | 'hook_deactivator_list' => $oldSettings['dis_input'] ?? '', |
| 51 | ); |
| 52 | update_option( WP_PARSI_KEY, $pluginSettings, false ); |
| 53 | |
| 54 | // WooCommerce Settings |
| 55 | if ( WordPress::isPluginActivated( 'woocommerce/woocommerce.php' ) ) { |
| 56 | $wooSettings = array( |
| 57 | 'fix_prices' => self::isEnable( $oldSettings, 'woo_per_price' ), |
| 58 | 'fix_persian_postcode' => self::isEnable( $oldSettings, 'woo_accept_per_postcode' ), |
| 59 | 'fix_persian_phone' => self::isEnable( $oldSettings, 'woo_accept_per_phone' ), |
| 60 | 'dropdown_cities' => self::isEnable( $oldSettings, 'woo_dropdown_cities' ), |
| 61 | 'validate_postcode' => self::isEnable( $oldSettings, 'woo_validate_postcode' ), |
| 62 | 'validate_phone' => self::isEnable( $oldSettings, 'woo_validate_phone' ), |
| 63 | ); |
| 64 | |
| 65 | if ( ! empty( $oldSettings['woo_gateways'] ) && is_array( $oldSettings['woo_gateways'] ) ) { |
| 66 | $activeGateWays = array_keys( $oldSettings['woo_gateways'] ); |
| 67 | $gateWays = array( 'parsian', 'pasargad', 'mellat', 'melli' ); |
| 68 | foreach ( $gateWays as $gateWay ) { |
| 69 | if ( in_array( $gateWay, $activeGateWays, true ) ) { |
| 70 | $wooSettings[ $gateWay . '_gateway_enable' ] = true; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | update_option( WP_PARSI_KEY . '_woocommerce', $wooSettings, false ); |
| 76 | } |
| 77 | |
| 78 | // ACF Settings |
| 79 | if ( WordPress::isPluginActivated( 'advanced-custom-fields/acf.php' ) ) { |
| 80 | $acfSettings = array( |
| 81 | 'fix_date' => self::isEnable( $oldSettings, 'acf_fix_date' ), |
| 82 | 'save_persian_date' => self::isEnable( $oldSettings, 'acf_persian_date' ), |
| 83 | ); |
| 84 | |
| 85 | update_option( WP_PARSI_KEY . '_acf', $acfSettings, false ); |
| 86 | } |
| 87 | |
| 88 | // EDD Settings |
| 89 | if ( WordPress::isPluginActivated( 'easy-digital-downloads/easy-digital-downloads.php' ) ) { |
| 90 | $eddSettings = array( |
| 91 | 'fix_prices' => self::isEnable( $oldSettings, 'edd_prices' ), |
| 92 | 'fix_currency' => self::isEnable( $oldSettings, 'edd_rial_fix' ), |
| 93 | ); |
| 94 | |
| 95 | update_option( WP_PARSI_KEY . '_edd', $eddSettings, false ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Delete old setting option in DB |
| 100 | if ( ! empty( $oldSettings ) ) { |
| 101 | delete_option( 'wpp_settings' ); |
| 102 | } |
| 103 | |
| 104 | update_option( WP_PARSI_KEY . '_plugin_version', $currentVersion, false ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Check old setting is enable |
| 109 | */ |
| 110 | private static function isEnable( $array, $key ): bool { |
| 111 | return isset( $array[ $key ] ) && $array[ $key ] === 'enable'; |
| 112 | } |
| 113 | } |
| 114 |