| 1 |
<?php |
| 2 |
/** |
| 3 |
* Checks if another version of Ghost Kit/Ghost Kit Pro is active and deactivates it. |
| 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 |
* GhostKit_Deactivate_Duplicate_Plugin constructor. |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'activated_plugin', array( $this, 'deactivate_other_instances' ) ); |
| 21 |
add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 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. |
| 27 |
* |
| 28 |
* @param string $plugin The plugin being activated. |
| 29 |
*/ |
| 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 ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$plugin_to_deactivate = 'ghostkit/class-ghost-kit.php'; |
| 36 |
$deactivated_notice_id = 1; |
| 37 |
|
| 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; |
| 42 |
} |
| 43 |
|
| 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() ); |
| 49 |
} |
| 50 |
|
| 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 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Displays a notice when either Ghost Kit or Ghost Kit Pro is automatically deactivated. |
| 62 |
*/ |
| 63 |
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 ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 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 |
?> |
| 75 |
<div class="notice notice-warning"> |
| 76 |
<p><?php echo esc_html( $message ); ?></p> |
| 77 |
</div> |
| 78 |
<?php |
| 79 |
|
| 80 |
delete_transient( 'gkt_deactivated_notice_id' ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
new GhostKit_Deactivate_Duplicate_Plugin(); |
| 85 |
|