Skip to content

Commit 01e1d03

Browse files
committed
[multisite] Improve retrieval of individual‑site info in a multisite network.
1 parent 2861dfa commit 01e1d03

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

includes/class-freemius.php

Lines changed: 43 additions & 20 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 $current_blog_id
7817+
* @param int $blog_id
78187818
*/
7819-
private function activate_bundle_license( $license, $sites = array(), $current_blog_id = 0 ) {
7819+
private function activate_bundle_license( $license, $sites = array(), $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(), $current_b
78497849
continue;
78507850
}
78517851

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

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

7860-
if ( ! $is_network_admin || $current_blog_id > 0 ) {
7861-
if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $current_blog_id ) ) {
7860+
if ( ! $is_network_admin || $blog_id > 0 ) {
7861+
if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $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,18 +7895,20 @@ private function activate_bundle_license( $license, $sites = array(), $current_b
78957895
}
78967896
}
78977897

7898+
$current_blog_id = get_current_blog_id();
7899+
78987900
foreach ( $sites as $site ) {
78997901
if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
79007902
continue;
79017903
}
79027904

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

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 );
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 );
79077909
}
79087910

7909-
$installs = $installs_by_blog_map[ $blog_id ];
7911+
$installs = $installs_by_blog_map[ $site_blog_id ];
79107912
$install = null;
79117913

79127914
if ( isset( $installs[ $fs->get_slug() ] ) ) {
@@ -7932,18 +7934,20 @@ private function activate_bundle_license( $license, $sites = array(), $current_b
79327934
break;
79337935
}
79347936

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

7940-
if ( ! isset( $site_info_by_blog_map[ $blog_id ] ) ) {
7941-
$site_info_by_blog_map[ $blog_id ] = $fs->get_site_info( $site );
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 );
79427944
}
79437945

7944-
$filtered_sites[] = $site_info_by_blog_map[ $blog_id ];
7946+
$filtered_sites[] = $site_info_by_blog_map[ $site_blog_id ];
79457947
}
79467948

7949+
$fs->switch_to_blog( $current_blog_id );
7950+
79477951
if ( $has_install_with_license || empty( $filtered_sites ) ) {
79487952
// 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.
79497953
continue;
@@ -7957,7 +7961,7 @@ private function activate_bundle_license( $license, $sites = array(), $current_b
79577961
null,
79587962
null,
79597963
$sites,
7960-
( $current_blog_id > 0 ? $current_blog_id : null ),
7964+
( $blog_id > 0 ? $blog_id : null ),
79617965
$license->user_id
79627966
);
79637967
}
@@ -8041,6 +8045,8 @@ function get_sites_for_network_level_optin() {
80418045
$sites = array();
80428046
$all_sites = self::get_sites();
80438047

8048+
$current_blog_id = get_current_blog_id();
8049+
80448050
foreach ( $all_sites as $site ) {
80458051
$blog_id = self::get_site_blog_id( $site );
80468052

@@ -8051,6 +8057,8 @@ function get_sites_for_network_level_optin() {
80518057
}
80528058
}
80538059

8060+
$this->switch_to_blog( $current_blog_id );
8061+
80548062
return $sites;
80558063
}
80568064

@@ -9177,13 +9185,16 @@ private function get_installs_data_for_api(
91779185
$install_url_by_install_id = array();
91789186
$subsite_registration_date_by_install_id = array();
91799187

9188+
$current_blog_id = get_current_blog_id();
9189+
$user = $this->_user;
9190+
91809191
foreach ( $sites as $site ) {
91819192
$blog_id = self::get_site_blog_id( $site );
91829193

91839194
$install = $this->get_install_by_blog_id( $blog_id );
91849195

91859196
if ( is_object( $install ) ) {
9186-
if ( $install->user_id != $this->_user->id ) {
9197+
if ( $install->user_id != $user->id ) {
91879198
// Install belongs to a different owner.
91889199
continue;
91899200
}
@@ -9281,7 +9292,7 @@ private function get_installs_data_for_api(
92819292
}
92829293
}
92839294

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

92869297
$installs_data = array_merge(
92879298
$installs_data,
@@ -12268,11 +12279,15 @@ private function activate_license_on_many_sites(
1226812279
$license_key,
1226912280
array $site_ids = array()
1227012281
) {
12282+
$current_blog_id = get_current_blog_id();
12283+
1227112284
$sites = array();
1227212285
foreach ( $site_ids as $site_id ) {
1227312286
$sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
1227412287
}
1227512288

12289+
$this->switch_to_blog( $current_blog_id );
12290+
1227612291
// Install the plugin.
1227712292
$result = $this->create_installs_with_user(
1227812293
$user,
@@ -12588,6 +12603,8 @@ function is_whitelabeled( $ignore_data_debug_mode = false, $blog_id = null ) {
1258812603
) {
1258912604
$sites = self::get_sites();
1259012605

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

12621+
$this->switch_to_blog( $current_blog_id );
12622+
1260412623
if ( $is_whitelabeled ) {
1260512624
break;
1260612625
}
@@ -15774,7 +15793,7 @@ static function get_sites_blog_ids( $sites ) {
1577415793
*
1577515794
* @return array
1577615795
*/
15777-
function get_site_info( $site = null, $load_registration = false ) {
15796+
function get_site_info( $site = null, $load_registration = false, $restore_current_blog = false ) {
1577815797
$this->_logger->entrance();
1577915798

1578015799
$switched = false;
@@ -15832,7 +15851,7 @@ function get_site_info( $site = null, $load_registration = false ) {
1583215851
$info[ 'registration_date' ] = $registration_date;
1583315852
}
1583415853

15835-
if ( $switched ) {
15854+
if ( $switched && $restore_current_blog ) {
1583615855
restore_current_blog();
1583715856
}
1583815857

@@ -17019,7 +17038,7 @@ function get_opt_in_params( $override_with = array(), $network_level_or_blog_id
1701917038
array( 'blog_id' => $network_level_or_blog_id ) :
1702017039
null;
1702117040

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

1702417043
$diagnostic_info = array();
1702517044
if ( FS_Permission_Manager::instance( $this )->is_diagnostic_tracking_allowed() ) {
@@ -17761,11 +17780,15 @@ private function install_many_pending_with_user(
1776117780
FS_Permission_Manager::PERMISSION_EXTENSIONS => $is_extensions_tracking_allowed,
1776217781
) );
1776317782

17783+
$current_blog_id = get_current_blog_id();
17784+
1776417785
$sites = array();
1776517786
foreach ( $site_ids as $site_id ) {
1776617787
$sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
1776717788
}
1776817789

17790+
$this->switch_to_blog( $current_blog_id );
17791+
1776917792
$this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
1777017793
}
1777117794

templates/account.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,16 @@
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+
147151
foreach ( $sites as $s ) {
148152
$site_info = $fs->get_site_info( $s );
149153
$install = $fs->get_install_by_blog_id( $site_info['blog_id'] );
150154
$view_params = array(
151155
'freemius' => $fs,
152-
'user' => $fs->get_user(),
156+
'user' => $user,
153157
'license' => $license,
154158
'site' => $site_info,
155159
'install' => $install,
@@ -173,6 +177,8 @@
173177
$show_license_row = false;
174178
}
175179
}
180+
181+
$fs->switch_to_blog( $current_blog_id );
176182
}
177183

178184
$has_bundle_license = false;

templates/forms/license-activation.php

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

59+
$current_blog_id = get_current_blog_id();
60+
5961
foreach ( $all_sites as $site ) {
6062
$site_details = $fs->get_site_info( $site );
6163

@@ -96,6 +98,8 @@
9698
$all_site_details[] = $site_details;
9799
}
98100

101+
$fs->switch_to_blog( $current_blog_id );
102+
99103
if ( $is_network_activation ) {
100104
$vars = array(
101105
'id' => $fs->get_id(),

0 commit comments

Comments
 (0)