Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the support for plugins with free and premium versions that … #727

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 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,45 @@ 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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the sake of properly handling bulk activation, can we also do this?

if ( $plugin !== $this->premium_plugin_basename() || $plugin !== $this->_free_plugin_basename ) {
  return;
}

$this->move_free_plugin_to_end_of_active_plugins();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposing to rename this function to _handle_free_and_premium_parallel_activation or something like that, just for readability.

// We cannot rely on fs_remove_sdk_reference_by_basename() for reordering, as the latest SDK reference might be associated with a different product, leading to the reordering of that product instead of the intended one.
$this->move_free_plugin_to_end_of_active_plugins( $this->_free_plugin_basename );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest not to pass the $this->_free_plugin_basename as the function can already get it from the instance. (Just for code readability)


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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it hurt us even if the SDK reference from the free version is at top? I don't mind doing this "just in case", but just asking for my understanding.

}
}

/**
* @author Leo Fajardo (@leorw)
* @since 2.8.0
*
* @param string $plugin
*/
private function move_free_plugin_to_end_of_active_plugins( $plugin ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the $plugin parameter and rely solely on $this->_free_plugin_basename. If that's not set, just bail early.

if ( empty( $this->_free_plugin_basename ) ) {
  return;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to handle network wide activation too?

CleanShot 2024-08-20 at 11 35 31@2x

Maybe it will be difficult to support network wide. Since I see the wp_get_active_network_plugins function does a sort when being called. If that's the case then please add some comment here.

$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 ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's either use array_splice or right after unset, let's reindex the array with $active_plugins = array_values( $active_plugins ).


$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 +8242,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.8.0';

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

Expand Down