Skip to content

Commit 99d55b6

Browse files
committed
[multisite] Adapt a simplified version of the approach from the feature/swas/restore_blog_fix_alt_exploration branch.
1 parent 01e1d03 commit 99d55b6

File tree

6 files changed

+72
-59
lines changed

6 files changed

+72
-59
lines changed

includes/class-freemius.php

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7814,9 +7814,9 @@ private function maybe_activate_bundle_license( $license = null, $sites = array(
78147814
*
78157815
* @param FS_Plugin_License $license
78167816
* @param array $sites
7817-
* @param int $blog_id
7817+
* @param int $current_blog_id
78187818
*/
7819-
private function activate_bundle_license( $license, $sites = array(), $blog_id = 0 ) {
7819+
private function activate_bundle_license( $license, $sites = array(), $current_blog_id = 0 ) {
78207820
$is_network_admin = fs_is_network_admin();
78217821

78227822
$installs_by_blog_map = array();
@@ -7849,16 +7849,16 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id =
78497849
continue;
78507850
}
78517851

7852-
if ( $blog_id > 0 ) {
7853-
$fs->switch_to_blog( $blog_id );
7852+
if ( $current_blog_id > 0 ) {
7853+
$fs->switch_to_blog( $current_blog_id );
78547854
}
78557855

78567856
if ( $fs->has_active_valid_license() ) {
78577857
continue;
78587858
}
78597859

7860-
if ( ! $is_network_admin || $blog_id > 0 ) {
7861-
if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $blog_id ) ) {
7860+
if ( ! $is_network_admin || $current_blog_id > 0 ) {
7861+
if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $current_blog_id ) ) {
78627862
// Do not try to activate the license in the site level if the product is network active and the connection was not delegated.
78637863
continue;
78647864
}
@@ -7895,20 +7895,18 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id =
78957895
}
78967896
}
78977897

7898-
$current_blog_id = get_current_blog_id();
7899-
79007898
foreach ( $sites as $site ) {
79017899
if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
79027900
continue;
79037901
}
79047902

7905-
$site_blog_id = $site['blog_id'];
7903+
$blog_id = $site['blog_id'];
79067904

7907-
if ( ! isset( $installs_by_blog_map[ $site_blog_id ] ) ) {
7908-
$installs_by_blog_map[ $site_blog_id ] = self::get_all_sites( $fs->get_module_type(), $site_blog_id );
7905+
if ( ! isset( $installs_by_blog_map[ $blog_id ] ) ) {
7906+
$installs_by_blog_map[ $blog_id ] = self::get_all_sites( $fs->get_module_type(), $blog_id );
79097907
}
79107908

7911-
$installs = $installs_by_blog_map[ $site_blog_id ];
7909+
$installs = $installs_by_blog_map[ $blog_id ];
79127910
$install = null;
79137911

79147912
if ( isset( $installs[ $fs->get_slug() ] ) ) {
@@ -7934,20 +7932,18 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id =
79347932
break;
79357933
}
79367934

7937-
if ( $fs->is_site_delegated_connection( $site_blog_id ) ) {
7935+
if ( $fs->is_site_delegated_connection( $blog_id ) ) {
79387936
// Site activation delegated, don't activate bundle license on the site in the network admin.
79397937
continue;
79407938
}
79417939

7942-
if ( ! isset( $site_info_by_blog_map[ $site_blog_id ] ) ) {
7943-
$site_info_by_blog_map[ $site_blog_id ] = $fs->get_site_info( $site );
7940+
if ( ! isset( $site_info_by_blog_map[ $blog_id ] ) ) {
7941+
$site_info_by_blog_map[ $blog_id ] = $fs->get_site_info( $site );
79447942
}
79457943

7946-
$filtered_sites[] = $site_info_by_blog_map[ $site_blog_id ];
7944+
$filtered_sites[] = $site_info_by_blog_map[ $blog_id ];
79477945
}
79487946

7949-
$fs->switch_to_blog( $current_blog_id );
7950-
79517947
if ( $has_install_with_license || empty( $filtered_sites ) ) {
79527948
// Do not try to activate the license at the network level if there's any install with a license or there's no site to activate the license on.
79537949
continue;
@@ -7961,7 +7957,7 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id =
79617957
null,
79627958
null,
79637959
$sites,
7964-
( $blog_id > 0 ? $blog_id : null ),
7960+
( $current_blog_id > 0 ? $current_blog_id : null ),
79657961
$license->user_id
79667962
);
79677963
}
@@ -8045,8 +8041,6 @@ function get_sites_for_network_level_optin() {
80458041
$sites = array();
80468042
$all_sites = self::get_sites();
80478043

8048-
$current_blog_id = get_current_blog_id();
8049-
80508044
foreach ( $all_sites as $site ) {
80518045
$blog_id = self::get_site_blog_id( $site );
80528046

@@ -8057,8 +8051,6 @@ function get_sites_for_network_level_optin() {
80578051
}
80588052
}
80598053

8060-
$this->switch_to_blog( $current_blog_id );
8061-
80628054
return $sites;
80638055
}
80648056

@@ -9185,16 +9177,13 @@ private function get_installs_data_for_api(
91859177
$install_url_by_install_id = array();
91869178
$subsite_registration_date_by_install_id = array();
91879179

9188-
$current_blog_id = get_current_blog_id();
9189-
$user = $this->_user;
9190-
91919180
foreach ( $sites as $site ) {
91929181
$blog_id = self::get_site_blog_id( $site );
91939182

91949183
$install = $this->get_install_by_blog_id( $blog_id );
91959184

91969185
if ( is_object( $install ) ) {
9197-
if ( $install->user_id != $user->id ) {
9186+
if ( $install->user_id != $this->_user->id ) {
91989187
// Install belongs to a different owner.
91999188
continue;
92009189
}
@@ -9292,7 +9281,7 @@ private function get_installs_data_for_api(
92929281
}
92939282
}
92949283

9295-
$this->switch_to_blog( $current_blog_id );
9284+
restore_current_blog();
92969285

92979286
$installs_data = array_merge(
92989287
$installs_data,
@@ -12279,15 +12268,11 @@ private function activate_license_on_many_sites(
1227912268
$license_key,
1228012269
array $site_ids = array()
1228112270
) {
12282-
$current_blog_id = get_current_blog_id();
12283-
1228412271
$sites = array();
1228512272
foreach ( $site_ids as $site_id ) {
1228612273
$sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
1228712274
}
1228812275

12289-
$this->switch_to_blog( $current_blog_id );
12290-
1229112276
// Install the plugin.
1229212277
$result = $this->create_installs_with_user(
1229312278
$user,
@@ -12603,8 +12588,6 @@ function is_whitelabeled( $ignore_data_debug_mode = false, $blog_id = null ) {
1260312588
) {
1260412589
$sites = self::get_sites();
1260512590

12606-
$current_blog_id = get_current_blog_id();
12607-
1260812591
/**
1260912592
* If in network admin area and the add-on was not network-activated or network-activated
1261012593
* and network-delegated, find any add-on whose is_whitelabeled flag is true.
@@ -12618,8 +12601,6 @@ function is_whitelabeled( $ignore_data_debug_mode = false, $blog_id = null ) {
1261812601
}
1261912602
}
1262012603

12621-
$this->switch_to_blog( $current_blog_id );
12622-
1262312604
if ( $is_whitelabeled ) {
1262412605
break;
1262512606
}
@@ -15793,10 +15774,12 @@ static function get_sites_blog_ids( $sites ) {
1579315774
*
1579415775
* @return array
1579515776
*/
15796-
function get_site_info( $site = null, $load_registration = false, $restore_current_blog = false ) {
15777+
function get_site_info( $site = null, $load_registration = false ) {
1579715778
$this->_logger->entrance();
1579815779

15799-
$switched = false;
15780+
$fs_hook_snapshot = new FS_Hook_Snapshot();
15781+
// Remove all filters from `switch_blog`.
15782+
$fs_hook_snapshot->remove( 'switch_blog' );
1580015783

1580115784
$registration_date = null;
1580215785

@@ -15809,7 +15792,6 @@ function get_site_info( $site = null, $load_registration = false, $restore_curre
1580915792

1581015793
if ( get_current_blog_id() != $blog_id ) {
1581115794
switch_to_blog( $blog_id );
15812-
$switched = true;
1581315795
}
1581415796

1581515797
if ( $site instanceof WP_Site ) {
@@ -15851,9 +15833,8 @@ function get_site_info( $site = null, $load_registration = false, $restore_curre
1585115833
$info[ 'registration_date' ] = $registration_date;
1585215834
}
1585315835

15854-
if ( $switched && $restore_current_blog ) {
15855-
restore_current_blog();
15856-
}
15836+
// Add the filters back to `switch_blog`.
15837+
$fs_hook_snapshot->restore( 'switch_blog' );
1585715838

1585815839
return $info;
1585915840
}
@@ -17038,7 +17019,7 @@ function get_opt_in_params( $override_with = array(), $network_level_or_blog_id
1703817019
array( 'blog_id' => $network_level_or_blog_id ) :
1703917020
null;
1704017021

17041-
$site = $this->get_site_info( $site, false, true );
17022+
$site = $this->get_site_info( $site );
1704217023

1704317024
$diagnostic_info = array();
1704417025
if ( FS_Permission_Manager::instance( $this )->is_diagnostic_tracking_allowed() ) {
@@ -17780,15 +17761,11 @@ private function install_many_pending_with_user(
1778017761
FS_Permission_Manager::PERMISSION_EXTENSIONS => $is_extensions_tracking_allowed,
1778117762
) );
1778217763

17783-
$current_blog_id = get_current_blog_id();
17784-
1778517764
$sites = array();
1778617765
foreach ( $site_ids as $site_id ) {
1778717766
$sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
1778817767
}
1778917768

17790-
$this->switch_to_blog( $current_blog_id );
17791-
1779217769
$this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
1779317770
}
1779417771

includes/class-fs-hook-snapshot.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @package Freemius
4+
* @copyright Copyright (c) 2025, Freemius, Inc.
5+
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6+
* @since 2.12.2
7+
*/
8+
9+
if ( ! defined( 'ABSPATH' ) ) {
10+
exit;
11+
}
12+
13+
/**
14+
* Class FS_Hook_Snapshot
15+
*
16+
* This class allows you to take a snapshot of the current actions attached to a WordPress hook, remove them, and restore them later.
17+
*/
18+
class FS_Hook_Snapshot {
19+
20+
private $removed_actions = array();
21+
22+
/**
23+
* Remove all actions from a given hook and store them for later restoration.
24+
*/
25+
public function remove( $hook ) {
26+
global $wp_filter;
27+
28+
if ( ! empty( $wp_filter ) && isset( $wp_filter[ $hook ] ) ) {
29+
$this->removed_actions[ $hook ] = $wp_filter[ $hook ];
30+
unset( $wp_filter[ $hook ] );
31+
}
32+
}
33+
34+
/**
35+
* Restore previously removed actions for a given hook.
36+
*/
37+
public function restore( $hook ) {
38+
global $wp_filter;
39+
40+
if ( ! empty( $wp_filter ) && isset( $this->removed_actions[ $hook ] ) ) {
41+
$wp_filter[ $hook ] = $this->removed_actions[ $hook ];
42+
unset( $this->removed_actions[ $hook ] );
43+
}
44+
}
45+
}

require.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@
5858
require_once WP_FS__DIR_INCLUDES . '/class-fs-admin-notices.php';
5959
require_once WP_FS__DIR_INCLUDES . '/class-freemius-abstract.php';
6060
require_once WP_FS__DIR_INCLUDES . '/sdk/Exceptions/Exception.php';
61+
require_once WP_FS__DIR_INCLUDES . '/class-fs-hook-snapshot.php';
6162
require_once WP_FS__DIR_INCLUDES . '/class-freemius.php';

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

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

templates/account.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,12 @@
144144
$sites = Freemius::get_sites();
145145
$all_installs_plan_id = null;
146146
$all_installs_license_id = ( $show_license_row ? $license->id : null );
147-
148-
$current_blog_id = get_current_blog_id();
149-
$user = $fs->get_user();
150-
151147
foreach ( $sites as $s ) {
152148
$site_info = $fs->get_site_info( $s );
153149
$install = $fs->get_install_by_blog_id( $site_info['blog_id'] );
154150
$view_params = array(
155151
'freemius' => $fs,
156-
'user' => $user,
152+
'user' => $fs->get_user(),
157153
'license' => $license,
158154
'site' => $site_info,
159155
'install' => $install,
@@ -177,8 +173,6 @@
177173
$show_license_row = false;
178174
}
179175
}
180-
181-
$fs->switch_to_blog( $current_blog_id );
182176
}
183177

184178
$has_bundle_license = false;

templates/forms/license-activation.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
$subsite_url_by_install_id = array();
5757
$install_url_by_install_id = array();
5858

59-
$current_blog_id = get_current_blog_id();
60-
6159
foreach ( $all_sites as $site ) {
6260
$site_details = $fs->get_site_info( $site );
6361

@@ -98,8 +96,6 @@
9896
$all_site_details[] = $site_details;
9997
}
10098

101-
$fs->switch_to_blog( $current_blog_id );
102-
10399
if ( $is_network_activation ) {
104100
$vars = array(
105101
'id' => $fs->get_id(),

0 commit comments

Comments
 (0)