| 1 |
<?php |
| 2 |
/** |
| 3 |
* Deactivates a copy of Ghost Kit that cannot run next to Ghost Kit Pro. |
| 4 |
* |
| 5 |
* @package ghostkit/deactivate-duplicate-plugin |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Deactivate_Duplicate_Plugin |
| 14 |
*/ |
| 15 |
class GhostKit_Deactivate_Duplicate_Plugin { |
| 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 |
/** |
| 37 |
* GhostKit_Deactivate_Duplicate_Plugin constructor. |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
add_action( 'activated_plugin', array( $this, 'deactivate_outdated_free_plugin' ) ); |
| 41 |
add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 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. |
| 48 |
* |
| 49 |
* @param string $plugin The plugin being activated. |
| 50 |
*/ |
| 51 |
public function deactivate_outdated_free_plugin( $plugin ) { |
| 52 |
if ( ! in_array( $plugin, array( self::FREE_PLUGIN, self::PRO_PLUGIN ), true ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$active_plugins = (array) get_option( 'active_plugins', array() ); |
| 57 |
|
| 58 |
if ( is_multisite() ) { |
| 59 |
$active_plugins = array_merge( |
| 60 |
$active_plugins, |
| 61 |
array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
if ( |
| 66 |
! in_array( self::FREE_PLUGIN, $active_plugins, true ) || |
| 67 |
! in_array( self::PRO_PLUGIN, $active_plugins, true ) |
| 68 |
) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 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; |
| 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 ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Displays a notice when Ghost Kit is automatically deactivated. |
| 100 |
*/ |
| 101 |
public function plugin_deactivated_notice() { |
| 102 |
if ( 1 !== (int) get_transient( 'gkt_deactivated_notice_id' ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
?> |
| 107 |
<div class="notice notice-warning"> |
| 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> |
| 116 |
</div> |
| 117 |
<?php |
| 118 |
|
| 119 |
delete_transient( 'gkt_deactivated_notice_id' ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
new GhostKit_Deactivate_Duplicate_Plugin(); |
| 124 |
|