Install.php
| 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\Cache; |
| 13 | use WPParsidate\Helper\WordPress; |
| 14 | |
| 15 | class Install { |
| 16 | public function __construct() { |
| 17 | add_action( 'plugins_loaded', [ $this, 'init' ] ); |
| 18 | } |
| 19 | |
| 20 | public function init() { |
| 21 | if ( self::checkVersion() ) { |
| 22 | self::update(); |
| 23 | |
| 24 | self::updateVersion(); |
| 25 | } |
| 26 | |
| 27 | $settings = get_option( WP_PARSI_KEY, [] ); |
| 28 | if ( empty( $settings ) ) { |
| 29 | self::install(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | private static function install() { |
| 34 | $settings = get_option( WP_PARSI_KEY, [] ); |
| 35 | |
| 36 | if ( empty( $settings ) ) { |
| 37 | $pluginSettings = array( |
| 38 | 'persian_date' => true, |
| 39 | 'enable_fonts' => true, |
| 40 | ); |
| 41 | update_option( WP_PARSI_KEY, $pluginSettings, false ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public static function update(): void { |
| 46 | $oldVersion = get_option( WP_PARSI_KEY . '_plugin_version', '5.1.8' ); |
| 47 | $oldSettings = get_option( 'wpp_settings', [] ); |
| 48 | $settings = get_option( WP_PARSI_KEY, [] ); |
| 49 | |
| 50 | if ( ! empty( $oldSettings ) && empty( $settings ) && version_compare( $oldVersion, '6.0', '<' ) ) { |
| 51 | $pluginSettings = array( |
| 52 | // Core |
| 53 | 'persian_date' => self::isEnable( $oldSettings, 'persian_date' ), |
| 54 | 'months_name_type' => $oldSettings['months_name_type'] ?? 'persian', |
| 55 | 'disable_widget_block' => self::isEnable( $oldSettings, 'disable_widget_block' ), |
| 56 | 'enable_fonts' => self::isEnable( $oldSettings, 'enable_fonts' ), |
| 57 | 'debug_mode' => self::isEnable( $oldSettings, 'dev_mode' ), |
| 58 | 'multilingual_support' => self::isEnable( $oldSettings, 'wpp_multilingual_support' ), |
| 59 | 'conv_permalinks' => self::isEnable( $oldSettings, 'conv_permalinks' ), |
| 60 | |
| 61 | // Convert |
| 62 | 'conv_page_title' => self::isEnable( $oldSettings, 'conv_page_title' ), |
| 63 | 'conv_title' => self::isEnable( $oldSettings, 'conv_title' ), |
| 64 | 'conv_contents' => self::isEnable( $oldSettings, 'conv_contents' ), |
| 65 | 'conv_excerpt' => self::isEnable( $oldSettings, 'conv_excerpt' ), |
| 66 | 'conv_comments' => self::isEnable( $oldSettings, 'conv_comments' ), |
| 67 | 'conv_comment_count' => self::isEnable( $oldSettings, 'conv_comment_count' ), |
| 68 | 'conv_dates' => self::isEnable( $oldSettings, 'conv_dates' ), |
| 69 | 'conv_cats' => self::isEnable( $oldSettings, 'conv_cats' ), |
| 70 | 'conv_arabic' => self::isEnable( $oldSettings, 'conv_arabic' ), |
| 71 | |
| 72 | // Tools |
| 73 | 'date_in_admin_bar' => self::isEnable( $oldSettings, 'date_in_admin_bar' ), |
| 74 | 'hook_deactivator_list' => $oldSettings['dis_input'] ?? '', |
| 75 | ); |
| 76 | update_option( WP_PARSI_KEY, $pluginSettings, false ); |
| 77 | |
| 78 | // WooCommerce Settings |
| 79 | if ( WordPress::isPluginActivated( 'woocommerce/woocommerce.php' ) ) { |
| 80 | $wooSettings = array( |
| 81 | 'fix_prices' => self::isEnable( $oldSettings, 'woo_per_price' ), |
| 82 | 'fix_persian_postcode' => self::isEnable( $oldSettings, 'woo_accept_per_postcode' ), |
| 83 | 'fix_persian_phone' => self::isEnable( $oldSettings, 'woo_accept_per_phone' ), |
| 84 | 'dropdown_cities' => self::isEnable( $oldSettings, 'woo_dropdown_cities' ), |
| 85 | 'validate_postcode' => self::isEnable( $oldSettings, 'woo_validate_postcode' ), |
| 86 | 'validate_phone' => self::isEnable( $oldSettings, 'woo_validate_phone' ), |
| 87 | ); |
| 88 | |
| 89 | if ( ! empty( $oldSettings['woo_gateways'] ) && is_array( $oldSettings['woo_gateways'] ) ) { |
| 90 | $activeGateWays = array_keys( $oldSettings['woo_gateways'] ); |
| 91 | $gateWays = array( 'parsian', 'pasargad', 'mellat', 'melli' ); |
| 92 | foreach ( $gateWays as $gateWay ) { |
| 93 | if ( in_array( $gateWay, $activeGateWays, true ) ) { |
| 94 | $wooSettings[ $gateWay . '_gateway_enable' ] = true; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | update_option( WP_PARSI_KEY . '_woocommerce', $wooSettings, false ); |
| 100 | } |
| 101 | |
| 102 | // ACF Settings |
| 103 | if ( WordPress::isPluginActivated( 'advanced-custom-fields/acf.php' ) ) { |
| 104 | $acfSettings = array( |
| 105 | 'fix_date' => self::isEnable( $oldSettings, 'acf_fix_date' ), |
| 106 | 'save_persian_date' => self::isEnable( $oldSettings, 'acf_persian_date' ), |
| 107 | ); |
| 108 | |
| 109 | update_option( WP_PARSI_KEY . '_acf', $acfSettings, false ); |
| 110 | } |
| 111 | |
| 112 | // EDD Settings |
| 113 | if ( WordPress::isPluginActivated( 'easy-digital-downloads/easy-digital-downloads.php' ) ) { |
| 114 | $eddSettings = array( |
| 115 | 'fix_prices' => self::isEnable( $oldSettings, 'edd_prices' ), |
| 116 | 'fix_currency' => self::isEnable( $oldSettings, 'edd_rial_fix' ), |
| 117 | ); |
| 118 | |
| 119 | update_option( WP_PARSI_KEY . '_edd', $eddSettings, false ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Delete old setting option in DB |
| 124 | if ( ! empty( $oldSettings ) ) { |
| 125 | delete_option( 'wpp_settings' ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private static function checkVersion( $oldVersion = '5.1.8' ) { |
| 130 | $currentVersion = self::getCurrentPluginVersion(); |
| 131 | $oldVersion = get_option( WP_PARSI_KEY . '_plugin_version', $oldVersion ); |
| 132 | |
| 133 | return version_compare( $oldVersion, $currentVersion, '<' ); |
| 134 | } |
| 135 | |
| 136 | private static function updateVersion() { |
| 137 | update_option( WP_PARSI_KEY . '_plugin_version', self::getCurrentPluginVersion(), true ); |
| 138 | } |
| 139 | |
| 140 | private static function getCurrentPluginVersion() { |
| 141 | $version = Cache::get( 'plugin_version', false ); |
| 142 | |
| 143 | if ( $version ) { |
| 144 | return $version; |
| 145 | } |
| 146 | |
| 147 | $pluginData = get_plugin_data( WP_PARSI_ROOT ); |
| 148 | $currentVersion = $pluginData['Version']; |
| 149 | Cache::set( 'plugin_version', $currentVersion ); |
| 150 | |
| 151 | return $currentVersion; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Check old setting is enable |
| 156 | */ |
| 157 | private static function isEnable( $array, $key ): bool { |
| 158 | return isset( $array[ $key ] ) && $array[ $key ] === 'enable'; |
| 159 | } |
| 160 | } |
| 161 |