Skip to content

Commit 2ec8717

Browse files
fajardoleoswashata
authored andcommitted
[parallel-activation] Improved the support for plugins with free and premium versions that can be activated in parallel.
1 parent fb0ca9d commit 2ec8717

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

includes/class-freemius.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class Freemius extends Freemius_Abstract {
109109
*/
110110
private $_enable_anonymous = true;
111111

112+
/**
113+
* @since 2.9.1
114+
* @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.
115+
*/
116+
private $_premium_plugin_basename_from_parallel_activation;
117+
112118
/**
113119
* @since 1.1.7.5
114120
* @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
@@ -1651,6 +1657,31 @@ private function register_after_settings_parse_hooks() {
16511657
);
16521658
}
16531659
}
1660+
1661+
if (
1662+
$this->is_user_in_admin() &&
1663+
$this->is_parallel_activation() &&
1664+
$this->_premium_plugin_basename !== $this->premium_plugin_basename_from_parallel_activation
1665+
) {
1666+
$this->_premium_plugin_basename = $this->premium_plugin_basename_from_parallel_activation;
1667+
1668+
register_activation_hook(
1669+
dirname( $this->_plugin_dir_path ) . '/' . $this->_premium_plugin_basename,
1670+
array( &$this, '_activate_plugin_event_hook' )
1671+
);
1672+
}
1673+
}
1674+
1675+
/**
1676+
* Determines if a plugin is running in parallel activation mode.
1677+
*
1678+
* @author Leo Fajardo (@leorw)
1679+
* @since 2.9.1
1680+
*
1681+
* @return bool
1682+
*/
1683+
private function is_parallel_activation() {
1684+
return ! empty( $this->premium_plugin_basename_from_parallel_activation );
16541685
}
16551686

16561687
/**
@@ -5155,19 +5186,43 @@ private function parse_settings( &$plugin_info ) {
51555186
$this->_plugin :
51565187
new FS_Plugin();
51575188

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

5192+
$module_type = $this->get_option( $plugin_info, 'type', $this->_module_type );
5193+
5194+
$parallel_activation = $this->get_option( $plugin_info, 'parallel_activation' );
5195+
5196+
if (
5197+
! $is_premium &&
5198+
is_array( $parallel_activation ) &&
5199+
( WP_FS__MODULE_TYPE_PLUGIN === $module_type ) &&
5200+
$this->get_bool_option( $parallel_activation, 'enabled' )
5201+
) {
5202+
$premium_basename = $this->get_option( $parallel_activation, 'premium_version_basename' );
5203+
5204+
if ( empty( $premium_basename ) ) {
5205+
throw new Exception('You need to specify the premium version basename to enable parallel version activation.');
5206+
}
5207+
5208+
$this->premium_plugin_basename_from_parallel_activation = $premium_basename;
5209+
5210+
if ( is_plugin_active( $premium_basename ) ) {
5211+
$is_premium = true;
5212+
}
5213+
}
5214+
51605215
$plugin->update( array(
51615216
'id' => $id,
5162-
'type' => $this->get_option( $plugin_info, 'type', $this->_module_type ),
5217+
'type' => $module_type,
51635218
'public_key' => $public_key,
51645219
'slug' => $this->_slug,
51655220
'premium_slug' => $this->get_option( $plugin_info, 'premium_slug', "{$this->_slug}-premium" ),
51665221
'parent_plugin_id' => $parent_id,
51675222
'version' => $this->get_plugin_version(),
51685223
'title' => $this->get_plugin_name( $premium_suffix ),
51695224
'file' => $this->_plugin_basename,
5170-
'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
5225+
'is_premium' => $is_premium,
51715226
'premium_suffix' => $premium_suffix,
51725227
'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
51735228
'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
@@ -7429,7 +7484,7 @@ function _activate_plugin_event_hook() {
74297484
*/
74307485
if (
74317486
is_plugin_active( $other_version_basename ) &&
7432-
$this->apply_filters( 'deactivate_on_activation', true )
7487+
$this->apply_filters( 'deactivate_on_activation', ! $this->is_parallel_activation() )
74337488
) {
74347489
deactivate_plugins( $other_version_basename );
74357490
}

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.9.0.3';
18+
$this_sdk_version = '2.9.0.4';
1919

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

0 commit comments

Comments
 (0)