Skip to content

Commit

Permalink
[parallel-activation] Improved the support for plugins with free and …
Browse files Browse the repository at this point in the history
…premium versions that can be activated in parallel.
  • Loading branch information
fajardoleo authored and swashata committed Nov 26, 2024
1 parent fb0ca9d commit 2ec8717
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
61 changes: 58 additions & 3 deletions includes/class-freemius.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class Freemius extends Freemius_Abstract {
*/
private $_enable_anonymous = true;

/**
* @since 2.9.1
* @var string|null Hints the SDK whether the plugin supports parallel activation mode, preventing the auto-deactivation of the free version when the premium version is activated, and vice versa.
*/
private $_premium_plugin_basename_from_parallel_activation;

/**
* @since 1.1.7.5
* @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
Expand Down Expand Up @@ -1651,6 +1657,31 @@ private function register_after_settings_parse_hooks() {
);
}
}

if (
$this->is_user_in_admin() &&
$this->is_parallel_activation() &&
$this->_premium_plugin_basename !== $this->premium_plugin_basename_from_parallel_activation
) {
$this->_premium_plugin_basename = $this->premium_plugin_basename_from_parallel_activation;

register_activation_hook(
dirname( $this->_plugin_dir_path ) . '/' . $this->_premium_plugin_basename,
array( &$this, '_activate_plugin_event_hook' )
);
}
}

/**
* Determines if a plugin is running in parallel activation mode.
*
* @author Leo Fajardo (@leorw)
* @since 2.9.1
*
* @return bool
*/
private function is_parallel_activation() {
return ! empty( $this->premium_plugin_basename_from_parallel_activation );
}

/**
Expand Down Expand Up @@ -5155,19 +5186,43 @@ private function parse_settings( &$plugin_info ) {
$this->_plugin :
new FS_Plugin();

$is_premium = $this->get_bool_option( $plugin_info, 'is_premium', true );
$premium_suffix = $this->get_option( $plugin_info, 'premium_suffix', '(Premium)' );

$module_type = $this->get_option( $plugin_info, 'type', $this->_module_type );

$parallel_activation = $this->get_option( $plugin_info, 'parallel_activation' );

if (
! $is_premium &&
is_array( $parallel_activation ) &&
( WP_FS__MODULE_TYPE_PLUGIN === $module_type ) &&
$this->get_bool_option( $parallel_activation, 'enabled' )
) {
$premium_basename = $this->get_option( $parallel_activation, 'premium_version_basename' );

if ( empty( $premium_basename ) ) {
throw new Exception('You need to specify the premium version basename to enable parallel version activation.');
}

$this->premium_plugin_basename_from_parallel_activation = $premium_basename;

if ( is_plugin_active( $premium_basename ) ) {
$is_premium = true;
}
}

$plugin->update( array(
'id' => $id,
'type' => $this->get_option( $plugin_info, 'type', $this->_module_type ),
'type' => $module_type,
'public_key' => $public_key,
'slug' => $this->_slug,
'premium_slug' => $this->get_option( $plugin_info, 'premium_slug', "{$this->_slug}-premium" ),
'parent_plugin_id' => $parent_id,
'version' => $this->get_plugin_version(),
'title' => $this->get_plugin_name( $premium_suffix ),
'file' => $this->_plugin_basename,
'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
'is_premium' => $is_premium,
'premium_suffix' => $premium_suffix,
'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
Expand Down Expand Up @@ -7429,7 +7484,7 @@ function _activate_plugin_event_hook() {
*/
if (
is_plugin_active( $other_version_basename ) &&
$this->apply_filters( 'deactivate_on_activation', true )
$this->apply_filters( 'deactivate_on_activation', ! $this->is_parallel_activation() )
) {
deactivate_plugins( $other_version_basename );
}
Expand Down
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.9.0.3';
$this_sdk_version = '2.9.0.4';

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

Expand Down

0 comments on commit 2ec8717

Please sign in to comment.