Skip to content

Commit

Permalink
Improved the support for plugins with free and premium versions that …
Browse files Browse the repository at this point in the history
…can be activated in parallel.
  • Loading branch information
fajardoleo committed Aug 19, 2024
1 parent de76663 commit ed9d106
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
60 changes: 45 additions & 15 deletions includes/class-freemius.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
21 changes: 21 additions & 0 deletions includes/fs-essential-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion start.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @var string
*/
$this_sdk_version = '2.7.3.5';
$this_sdk_version = '2.7.3.6';

#region SDK Selection Logic --------------------------------------------------------------------

Expand Down

0 comments on commit ed9d106

Please sign in to comment.