Skip to content

Commit 2241a1e

Browse files
kaeizenbfintal
andauthored
fix: auto-deactivate other active Stackable plugin (#3574)
* add no-op function * prevent multiple active stackable plugins * Update plugin.php * add notice --------- Co-authored-by: Benjamin Intal <[email protected]>
1 parent 45272e1 commit 2241a1e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

plugin.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@
1717
exit;
1818
}
1919

20+
if ( ! function_exists( 'stackable_multiple_plugins_check' ) ) {
21+
// Prevent multiple Stackable plugin versions from being active simultaneously.
22+
function stackable_multiple_plugins_check() {
23+
if ( is_plugin_active( $GLOBALS['OTHER_STACKABLE_FILE'] ) ) {
24+
deactivate_plugins( $GLOBALS['OTHER_STACKABLE_FILE'] );
25+
}
26+
}
27+
}
28+
29+
if ( defined('STACKABLE_FILE') && STACKABLE_FILE !== __FILE__ && ! isset( $GLOBALS['OTHER_STACKABLE_FILE'] ) ) {
30+
// Get relative file path of the other Stackable version.
31+
$GLOBALS['OTHER_STACKABLE_FILE'] = plugin_basename( STACKABLE_FILE );
32+
33+
// Use a temporary option to store the other Stackable Plugin info needed for the admin notice. This will be deleted later.
34+
// Note: We cannot use add_action in the register_activation_hook callback so we use this temporary option.
35+
// See https://developer.wordpress.org/reference/functions/register_activation_hook/#process-flow for more info.
36+
add_option( 'stackable_other_stackable_plugin_info', [ 'BUILD' => STACKABLE_BUILD, 'VERSION' => STACKABLE_VERSION ] );
37+
38+
register_activation_hook( __FILE__, 'stackable_multiple_plugins_check' );
39+
}
40+
2041
// Freemius SDK: Auto deactivate the free version when activating the paid one.
2142
if ( function_exists( 'sugb_fs' ) ) {
2243
sugb_fs()->set_basename( true, __FILE__ );
@@ -158,6 +179,28 @@ function stackable_notice_gutenberg_plugin_ignore() {
158179
add_action( 'wp_ajax_stackable_notice_gutenberg_plugin_ignore', 'stackable_notice_gutenberg_plugin_ignore' );
159180
}
160181

182+
/**
183+
* Show notice if another Stackable plugin has been deactivated.
184+
*
185+
* @since 3.18.1
186+
*/
187+
if ( ! function_exists( 'stackable_notice_other_stackable_plugin_deactivated' ) ) {
188+
function stackable_notice_other_stackable_plugin_deactivated() {
189+
$OTHER_STACKABLE_INFO = get_option( 'stackable_other_stackable_plugin_info', false );
190+
if ( $OTHER_STACKABLE_INFO ) {
191+
printf(
192+
'<div class="notice notice-info is-dismissible stackable_notice_gutenberg_plugin"><p>%s</p></div>',
193+
sprintf( esc_html__( '%sStackable Notice%s: The Stackable plugin (%s version %s) has been deactivated. Only one active Stackable plugin is needed.', STACKABLE_I18N ), '<strong>', '</strong>', $OTHER_STACKABLE_INFO['BUILD'], $OTHER_STACKABLE_INFO['VERSION'] )
194+
);
195+
delete_option( 'stackable_other_stackable_plugin_info' );
196+
}
197+
}
198+
199+
if ( get_option( 'stackable_other_stackable_plugin_info', false ) ) {
200+
add_action( 'admin_notices', 'stackable_notice_other_stackable_plugin_deactivated' );
201+
}
202+
}
203+
161204
/********************************************************************************************
162205
* END Activation & PHP version checks.
163206
********************************************************************************************/

0 commit comments

Comments
 (0)