Skip to content

Commit ed9d106

Browse files
committed
Improved the support for plugins with free and premium versions that can be activated in parallel.
1 parent de76663 commit ed9d106

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

includes/class-freemius.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7402,11 +7402,12 @@ function _activate_plugin_event_hook() {
74027402
* @author Leo Fajardo (@leorw)
74037403
* @since 1.2.2
74047404
*/
7405-
if (
7406-
is_plugin_active( $other_version_basename ) &&
7407-
$this->apply_filters( 'deactivate_on_activation', true )
7408-
) {
7409-
deactivate_plugins( $other_version_basename );
7405+
if ( is_plugin_active( $other_version_basename ) ) {
7406+
if ( $this->apply_filters( 'deactivate_on_activation', true ) ) {
7407+
deactivate_plugins( $other_version_basename );
7408+
} else {
7409+
add_action( 'activated_plugin', array( &$this, '_activated_plugin' ), 10, 2 );
7410+
}
74107411
}
74117412
}
74127413

@@ -7544,6 +7545,44 @@ function _activate_plugin_event_hook() {
75447545
$this->_storage->was_plugin_loaded = true;
75457546
}
75467547

7548+
/**
7549+
* @author Leo Fajardo (@leorw)
7550+
* @since 2.8.0
7551+
*
7552+
* @param string $plugin
7553+
* @param bool $network_wide
7554+
*/
7555+
function _activated_plugin( $plugin, $network_wide ) {
7556+
$this->move_free_plugin_to_end_of_active_plugins( $this->_free_plugin_basename );
7557+
7558+
if ( $plugin !== $this->_free_plugin_basename ) {
7559+
/**
7560+
* To avoid placing the free version at the start of the active plugins option when the SDK is loaded from it, remove the SDK reference linked to the free version if it is the newest.
7561+
*/
7562+
fs_remove_sdk_reference_by_basename( $this->_free_plugin_basename );
7563+
}
7564+
}
7565+
7566+
/**
7567+
* @author Leo Fajardo (@leorw)
7568+
* @since 2.8.0
7569+
*
7570+
* @param string $plugin
7571+
*/
7572+
private function move_free_plugin_to_end_of_active_plugins( $plugin ) {
7573+
$active_plugins = get_option( 'active_plugins' );
7574+
7575+
// Move the free version to the last position in the active plugins option.
7576+
if ( false !== ( $key = array_search( $plugin, $active_plugins ) ) ) {
7577+
unset( $active_plugins[ $key ]);
7578+
7579+
$active_plugins[] = $plugin;
7580+
}
7581+
7582+
// Update the active plugins option with the reordered plugins.
7583+
update_option('active_plugins', $active_plugins);
7584+
}
7585+
75477586
/**
75487587
* @author Leo Fajardo (@leorw)
75497588
* @since 2.3.0
@@ -8202,16 +8241,7 @@ function _deactivate_plugin_hook() {
82028241
* @since 1.1.6
82038242
*/
82048243
private function remove_sdk_reference() {
8205-
global $fs_active_plugins;
8206-
8207-
foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) {
8208-
if ( $this->_plugin_basename == $data->plugin_path ) {
8209-
unset( $fs_active_plugins->plugins[ $sdk_path ] );
8210-
break;
8211-
}
8212-
}
8213-
8214-
fs_fallback_to_newest_active_sdk();
8244+
fs_remove_sdk_reference_by_basename( $this->_plugin_basename );
82158245
}
82168246

82178247
/**

includes/fs-essential-functions.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,25 @@ function fs_fallback_to_newest_active_sdk() {
415415
fs_update_sdk_newest_version( $newest_sdk_path, $newest_sdk_data->plugin_path );
416416
}
417417
}
418+
}
419+
420+
if ( ! function_exists( 'fs_remove_sdk_reference_by_basename' ) ) {
421+
/**
422+
* @author Leo Fajardo (@leorw)
423+
* @since 2.8.0
424+
*
425+
* @param string $plugin_basename
426+
*/
427+
function fs_remove_sdk_reference_by_basename( $plugin_basename ) {
428+
global $fs_active_plugins;
429+
430+
foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) {
431+
if ( $plugin_basename == $data->plugin_path ) {
432+
unset( $fs_active_plugins->plugins[ $sdk_path ] );
433+
break;
434+
}
435+
}
436+
437+
fs_fallback_to_newest_active_sdk();
438+
}
418439
}

start.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @var string
1717
*/
18-
$this_sdk_version = '2.7.3.5';
18+
$this_sdk_version = '2.7.3.6';
1919

2020
#region SDK Selection Logic --------------------------------------------------------------------
2121

0 commit comments

Comments
 (0)