From ed9d10687dd49f1ca39b628de35d13e77431160f Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Tue, 23 Jul 2024 22:41:02 +0800 Subject: [PATCH] Improved the support for plugins with free and premium versions that can be activated in parallel. --- includes/class-freemius.php | 60 +++++++++++++++++++++-------- includes/fs-essential-functions.php | 21 ++++++++++ start.php | 2 +- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index c22650ee..b0ca8ae6 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -7402,11 +7402,12 @@ function _activate_plugin_event_hook() { * @author Leo Fajardo (@leorw) * @since 1.2.2 */ - if ( - is_plugin_active( $other_version_basename ) && - $this->apply_filters( 'deactivate_on_activation', true ) - ) { - deactivate_plugins( $other_version_basename ); + if ( is_plugin_active( $other_version_basename ) ) { + if ( $this->apply_filters( 'deactivate_on_activation', true ) ) { + deactivate_plugins( $other_version_basename ); + } else { + add_action( 'activated_plugin', array( &$this, '_activated_plugin' ), 10, 2 ); + } } } @@ -7544,6 +7545,44 @@ function _activate_plugin_event_hook() { $this->_storage->was_plugin_loaded = true; } + /** + * @author Leo Fajardo (@leorw) + * @since 2.8.0 + * + * @param string $plugin + * @param bool $network_wide + */ + function _activated_plugin( $plugin, $network_wide ) { + $this->move_free_plugin_to_end_of_active_plugins( $this->_free_plugin_basename ); + + if ( $plugin !== $this->_free_plugin_basename ) { + /** + * 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. + */ + fs_remove_sdk_reference_by_basename( $this->_free_plugin_basename ); + } + } + + /** + * @author Leo Fajardo (@leorw) + * @since 2.8.0 + * + * @param string $plugin + */ + private function move_free_plugin_to_end_of_active_plugins( $plugin ) { + $active_plugins = get_option( 'active_plugins' ); + + // Move the free version to the last position in the active plugins option. + if ( false !== ( $key = array_search( $plugin, $active_plugins ) ) ) { + unset( $active_plugins[ $key ]); + + $active_plugins[] = $plugin; + } + + // Update the active plugins option with the reordered plugins. + update_option('active_plugins', $active_plugins); + } + /** * @author Leo Fajardo (@leorw) * @since 2.3.0 @@ -8202,16 +8241,7 @@ function _deactivate_plugin_hook() { * @since 1.1.6 */ private function remove_sdk_reference() { - global $fs_active_plugins; - - foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) { - if ( $this->_plugin_basename == $data->plugin_path ) { - unset( $fs_active_plugins->plugins[ $sdk_path ] ); - break; - } - } - - fs_fallback_to_newest_active_sdk(); + fs_remove_sdk_reference_by_basename( $this->_plugin_basename ); } /** diff --git a/includes/fs-essential-functions.php b/includes/fs-essential-functions.php index 84ffcbe0..64c9101b 100644 --- a/includes/fs-essential-functions.php +++ b/includes/fs-essential-functions.php @@ -415,4 +415,25 @@ function fs_fallback_to_newest_active_sdk() { fs_update_sdk_newest_version( $newest_sdk_path, $newest_sdk_data->plugin_path ); } } + } + + if ( ! function_exists( 'fs_remove_sdk_reference_by_basename' ) ) { + /** + * @author Leo Fajardo (@leorw) + * @since 2.8.0 + * + * @param string $plugin_basename + */ + function fs_remove_sdk_reference_by_basename( $plugin_basename ) { + global $fs_active_plugins; + + foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) { + if ( $plugin_basename == $data->plugin_path ) { + unset( $fs_active_plugins->plugins[ $sdk_path ] ); + break; + } + } + + fs_fallback_to_newest_active_sdk(); + } } \ No newline at end of file diff --git a/start.php b/start.php index dfb41de2..1ffb197a 100644 --- a/start.php +++ b/start.php @@ -15,7 +15,7 @@ * * @var string */ - $this_sdk_version = '2.7.3.5'; + $this_sdk_version = '2.7.3.6'; #region SDK Selection Logic --------------------------------------------------------------------