| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Checks if another version of Ghost Kit/Ghost Kit Pro is active and deactivates it. | |
| 3 | + * Deactivates a copy of Ghost Kit that cannot run next to Ghost Kit Pro. | |
| 4 | 4 | * |
| 5 | 5 | * @package ghostkit/deactivate-duplicate-plugin |
| 6 | 6 | */ |
| 7 | 7 | |
| @@ -13,68 +13,107 @@ | ||
| 13 | 13 | * Class GhostKit_Deactivate_Duplicate_Plugin |
| 14 | 14 | */ |
| 15 | 15 | class GhostKit_Deactivate_Duplicate_Plugin { |
| 16 | 16 | /** |
| 17 | + * Basename of the free plugin. | |
| 18 | + */ | |
| 19 | + const FREE_PLUGIN = 'ghostkit/class-ghost-kit.php'; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Basename of the Pro plugin. | |
| 23 | + */ | |
| 24 | + const PRO_PLUGIN = 'ghostkit-pro/class-ghost-kit-pro.php'; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * First version of the free plugin that steps aside on its own when the Pro plugin | |
| 28 | + * is active. Older ones declare the core class a second time instead, so they cannot | |
| 29 | + * stay active next to it. | |
| 30 | + * | |
| 31 | + * `version_compare()` ranks a pre-release below the release it precedes, so the | |
| 32 | + * `-alpha` suffix is what lets a beta or an RC of that version through. | |
| 33 | + */ | |
| 34 | + const FREE_PLUGIN_MIN_VERSION = '3.7.1-alpha'; | |
| 35 | + | |
| 36 | + /** | |
| 17 | 37 | * GhostKit_Deactivate_Duplicate_Plugin constructor. |
| 18 | 38 | */ |
| 19 | 39 | public function __construct() { |
| 20 | - add_action( 'activated_plugin', array( $this, 'deactivate_other_instances' ) ); | |
| 40 | + add_action( 'activated_plugin', array( $this, 'deactivate_outdated_free_plugin' ) ); | |
| 21 | 41 | add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) ); |
| 22 | 42 | } |
| 23 | 43 | |
| 24 | 44 | /** |
| 25 | - * Checks if another version of Ghost Kit/Ghost Kit Pro is active and deactivates it. | |
| 26 | - * Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated. | |
| 45 | + * Deactivates the free plugin when the Pro plugin is active and the free copy is too | |
| 46 | + * old to step aside by itself. Hooked on `activated_plugin`, so it runs whichever of | |
| 47 | + * the two was just switched on. | |
| 27 | 48 | * |
| 28 | 49 | * @param string $plugin The plugin being activated. |
| 29 | 50 | */ |
| 30 | - public function deactivate_other_instances( $plugin ) { | |
| 31 | - if ( ! in_array( $plugin, array( 'ghostkit/class-ghost-kit.php', 'ghostkit-pro/class-ghost-kit-pro.php' ), true ) ) { | |
| 51 | + public function deactivate_outdated_free_plugin( $plugin ) { | |
| 52 | + if ( ! in_array( $plugin, array( self::FREE_PLUGIN, self::PRO_PLUGIN ), true ) ) { | |
| 32 | 53 | return; |
| 33 | 54 | } |
| 34 | 55 | |
| 35 | - $plugin_to_deactivate = 'ghostkit/class-ghost-kit.php'; | |
| 36 | - $deactivated_notice_id = 1; | |
| 56 | + $active_plugins = (array) get_option( 'active_plugins', array() ); | |
| 37 | 57 | |
| 38 | - // If we just activated the free version, deactivate the Pro version. | |
| 39 | - if ( $plugin === $plugin_to_deactivate ) { | |
| 40 | - $plugin_to_deactivate = 'ghostkit-pro/class-ghost-kit-pro.php'; | |
| 41 | - $deactivated_notice_id = 2; | |
| 58 | + if ( is_multisite() ) { | |
| 59 | + $active_plugins = array_merge( | |
| 60 | + $active_plugins, | |
| 61 | + array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) | |
| 62 | + ); | |
| 42 | 63 | } |
| 43 | 64 | |
| 44 | - if ( is_multisite() && is_network_admin() ) { | |
| 45 | - $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() ); | |
| 46 | - $active_plugins = array_keys( $active_plugins ); | |
| 47 | - } else { | |
| 48 | - $active_plugins = (array) get_option( 'active_plugins', array() ); | |
| 65 | + if ( | |
| 66 | + ! in_array( self::FREE_PLUGIN, $active_plugins, true ) || | |
| 67 | + ! in_array( self::PRO_PLUGIN, $active_plugins, true ) | |
| 68 | + ) { | |
| 69 | + return; | |
| 49 | 70 | } |
| 50 | 71 | |
| 51 | - foreach ( $active_plugins as $plugin_basename ) { | |
| 52 | - if ( $plugin_to_deactivate === $plugin_basename ) { | |
| 53 | - set_transient( 'gkt_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS ); | |
| 54 | - deactivate_plugins( $plugin_basename ); | |
| 55 | - return; | |
| 56 | - } | |
| 72 | + $free_plugin_file = WP_PLUGIN_DIR . '/' . self::FREE_PLUGIN; | |
| 73 | + | |
| 74 | + // `active_plugins` keeps naming a plugin whose directory was removed by hand, | |
| 75 | + // and `get_plugin_data()` reads the file without checking that it is there. | |
| 76 | + if ( ! file_exists( $free_plugin_file ) ) { | |
| 77 | + return; | |
| 57 | 78 | } |
| 79 | + | |
| 80 | + if ( ! function_exists( 'get_plugin_data' ) ) { | |
| 81 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 82 | + } | |
| 83 | + | |
| 84 | + $free_plugin_data = get_plugin_data( $free_plugin_file, false, false ); | |
| 85 | + | |
| 86 | + if ( | |
| 87 | + empty( $free_plugin_data['Version'] ) || | |
| 88 | + version_compare( $free_plugin_data['Version'], self::FREE_PLUGIN_MIN_VERSION, '>=' ) | |
| 89 | + ) { | |
| 90 | + return; | |
| 91 | + } | |
| 92 | + | |
| 93 | + set_transient( 'gkt_deactivated_notice_id', 1, 1 * HOUR_IN_SECONDS ); | |
| 94 | + | |
| 95 | + deactivate_plugins( self::FREE_PLUGIN ); | |
| 58 | 96 | } |
| 59 | 97 | |
| 60 | 98 | /** |
| 61 | - * Displays a notice when either Ghost Kit or Ghost Kit Pro is automatically deactivated. | |
| 99 | + * Displays a notice when Ghost Kit is automatically deactivated. | |
| 62 | 100 | */ |
| 63 | 101 | public function plugin_deactivated_notice() { |
| 64 | - $deactivated_notice_id = (int) get_transient( 'gkt_deactivated_notice_id' ); | |
| 65 | - if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) { | |
| 102 | + if ( 1 !== (int) get_transient( 'gkt_deactivated_notice_id' ) ) { | |
| 66 | 103 | return; |
| 67 | 104 | } |
| 68 | 105 | |
| 69 | - $message = __( "Ghost Kit and Ghost Kit Pro should not be active at the same time. We've automatically deactivated Ghost Kit.", 'ghostkit' ); | |
| 70 | - if ( 2 === $deactivated_notice_id ) { | |
| 71 | - $message = __( "Ghost Kit and Ghost Kit Pro should not be active at the same time. We've automatically deactivated Ghost Kit Pro.", 'ghostkit' ); | |
| 72 | - } | |
| 73 | - | |
| 74 | 106 | ?> |
| 75 | 107 | <div class="notice notice-warning"> |
| 76 | - <p><?php echo esc_html( $message ); ?></p> | |
| 108 | + <p> | |
| 109 | + <?php | |
| 110 | + esc_html_e( | |
| 111 | + "This version of Ghost Kit is too old to run next to Ghost Kit Pro. We've automatically deactivated Ghost Kit.", | |
| 112 | + 'ghostkit' | |
| 113 | + ); | |
| 114 | + ?> | |
| 115 | + </p> | |
| 77 | 116 | </div> |
| 78 | 117 | <?php |
| 79 | 118 | |
| 80 | 119 | delete_transient( 'gkt_deactivated_notice_id' ); |