@@ -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
0 commit comments