From 01e1d03d6ff803a42bae331ea42500c5e6057f70 Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Sun, 18 May 2025 22:11:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[multisite]=20Improve=20retrieval=20of=20in?= =?UTF-8?q?dividual=E2=80=91site=20info=20in=20a=20multisite=20network.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-freemius.php | 63 ++++++++++++++++++-------- templates/account.php | 8 +++- templates/forms/license-activation.php | 4 ++ 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index fd10929a..9d7573d0 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -7814,9 +7814,9 @@ private function maybe_activate_bundle_license( $license = null, $sites = array( * * @param FS_Plugin_License $license * @param array $sites - * @param int $current_blog_id + * @param int $blog_id */ - private function activate_bundle_license( $license, $sites = array(), $current_blog_id = 0 ) { + private function activate_bundle_license( $license, $sites = array(), $blog_id = 0 ) { $is_network_admin = fs_is_network_admin(); $installs_by_blog_map = array(); @@ -7849,16 +7849,16 @@ private function activate_bundle_license( $license, $sites = array(), $current_b continue; } - if ( $current_blog_id > 0 ) { - $fs->switch_to_blog( $current_blog_id ); + if ( $blog_id > 0 ) { + $fs->switch_to_blog( $blog_id ); } if ( $fs->has_active_valid_license() ) { continue; } - if ( ! $is_network_admin || $current_blog_id > 0 ) { - if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $current_blog_id ) ) { + if ( ! $is_network_admin || $blog_id > 0 ) { + if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $blog_id ) ) { // Do not try to activate the license in the site level if the product is network active and the connection was not delegated. continue; } @@ -7895,18 +7895,20 @@ private function activate_bundle_license( $license, $sites = array(), $current_b } } + $current_blog_id = get_current_blog_id(); + foreach ( $sites as $site ) { if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) { continue; } - $blog_id = $site['blog_id']; + $site_blog_id = $site['blog_id']; - if ( ! isset( $installs_by_blog_map[ $blog_id ] ) ) { - $installs_by_blog_map[ $blog_id ] = self::get_all_sites( $fs->get_module_type(), $blog_id ); + if ( ! isset( $installs_by_blog_map[ $site_blog_id ] ) ) { + $installs_by_blog_map[ $site_blog_id ] = self::get_all_sites( $fs->get_module_type(), $site_blog_id ); } - $installs = $installs_by_blog_map[ $blog_id ]; + $installs = $installs_by_blog_map[ $site_blog_id ]; $install = null; if ( isset( $installs[ $fs->get_slug() ] ) ) { @@ -7932,18 +7934,20 @@ private function activate_bundle_license( $license, $sites = array(), $current_b break; } - if ( $fs->is_site_delegated_connection( $blog_id ) ) { + if ( $fs->is_site_delegated_connection( $site_blog_id ) ) { // Site activation delegated, don't activate bundle license on the site in the network admin. continue; } - if ( ! isset( $site_info_by_blog_map[ $blog_id ] ) ) { - $site_info_by_blog_map[ $blog_id ] = $fs->get_site_info( $site ); + if ( ! isset( $site_info_by_blog_map[ $site_blog_id ] ) ) { + $site_info_by_blog_map[ $site_blog_id ] = $fs->get_site_info( $site ); } - $filtered_sites[] = $site_info_by_blog_map[ $blog_id ]; + $filtered_sites[] = $site_info_by_blog_map[ $site_blog_id ]; } + $fs->switch_to_blog( $current_blog_id ); + if ( $has_install_with_license || empty( $filtered_sites ) ) { // 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. continue; @@ -7957,7 +7961,7 @@ private function activate_bundle_license( $license, $sites = array(), $current_b null, null, $sites, - ( $current_blog_id > 0 ? $current_blog_id : null ), + ( $blog_id > 0 ? $blog_id : null ), $license->user_id ); } @@ -8041,6 +8045,8 @@ function get_sites_for_network_level_optin() { $sites = array(); $all_sites = self::get_sites(); + $current_blog_id = get_current_blog_id(); + foreach ( $all_sites as $site ) { $blog_id = self::get_site_blog_id( $site ); @@ -8051,6 +8057,8 @@ function get_sites_for_network_level_optin() { } } + $this->switch_to_blog( $current_blog_id ); + return $sites; } @@ -9177,13 +9185,16 @@ private function get_installs_data_for_api( $install_url_by_install_id = array(); $subsite_registration_date_by_install_id = array(); + $current_blog_id = get_current_blog_id(); + $user = $this->_user; + foreach ( $sites as $site ) { $blog_id = self::get_site_blog_id( $site ); $install = $this->get_install_by_blog_id( $blog_id ); if ( is_object( $install ) ) { - if ( $install->user_id != $this->_user->id ) { + if ( $install->user_id != $user->id ) { // Install belongs to a different owner. continue; } @@ -9281,7 +9292,7 @@ private function get_installs_data_for_api( } } - restore_current_blog(); + $this->switch_to_blog( $current_blog_id ); $installs_data = array_merge( $installs_data, @@ -12268,11 +12279,15 @@ private function activate_license_on_many_sites( $license_key, array $site_ids = array() ) { + $current_blog_id = get_current_blog_id(); + $sites = array(); foreach ( $site_ids as $site_id ) { $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) ); } + $this->switch_to_blog( $current_blog_id ); + // Install the plugin. $result = $this->create_installs_with_user( $user, @@ -12588,6 +12603,8 @@ function is_whitelabeled( $ignore_data_debug_mode = false, $blog_id = null ) { ) { $sites = self::get_sites(); + $current_blog_id = get_current_blog_id(); + /** * If in network admin area and the add-on was not network-activated or network-activated * 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 ) { } } + $this->switch_to_blog( $current_blog_id ); + if ( $is_whitelabeled ) { break; } @@ -15774,7 +15793,7 @@ static function get_sites_blog_ids( $sites ) { * * @return array */ - function get_site_info( $site = null, $load_registration = false ) { + function get_site_info( $site = null, $load_registration = false, $restore_current_blog = false ) { $this->_logger->entrance(); $switched = false; @@ -15832,7 +15851,7 @@ function get_site_info( $site = null, $load_registration = false ) { $info[ 'registration_date' ] = $registration_date; } - if ( $switched ) { + if ( $switched && $restore_current_blog ) { restore_current_blog(); } @@ -17019,7 +17038,7 @@ function get_opt_in_params( $override_with = array(), $network_level_or_blog_id array( 'blog_id' => $network_level_or_blog_id ) : null; - $site = $this->get_site_info( $site ); + $site = $this->get_site_info( $site, false, true ); $diagnostic_info = array(); if ( FS_Permission_Manager::instance( $this )->is_diagnostic_tracking_allowed() ) { @@ -17761,11 +17780,15 @@ private function install_many_pending_with_user( FS_Permission_Manager::PERMISSION_EXTENSIONS => $is_extensions_tracking_allowed, ) ); + $current_blog_id = get_current_blog_id(); + $sites = array(); foreach ( $site_ids as $site_id ) { $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) ); } + $this->switch_to_blog( $current_blog_id ); + $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites ); } diff --git a/templates/account.php b/templates/account.php index 89120022..77d29047 100755 --- a/templates/account.php +++ b/templates/account.php @@ -144,12 +144,16 @@ $sites = Freemius::get_sites(); $all_installs_plan_id = null; $all_installs_license_id = ( $show_license_row ? $license->id : null ); + + $current_blog_id = get_current_blog_id(); + $user = $fs->get_user(); + foreach ( $sites as $s ) { $site_info = $fs->get_site_info( $s ); $install = $fs->get_install_by_blog_id( $site_info['blog_id'] ); $view_params = array( 'freemius' => $fs, - 'user' => $fs->get_user(), + 'user' => $user, 'license' => $license, 'site' => $site_info, 'install' => $install, @@ -173,6 +177,8 @@ $show_license_row = false; } } + + $fs->switch_to_blog( $current_blog_id ); } $has_bundle_license = false; diff --git a/templates/forms/license-activation.php b/templates/forms/license-activation.php index 3f009fff..201d7d7f 100644 --- a/templates/forms/license-activation.php +++ b/templates/forms/license-activation.php @@ -56,6 +56,8 @@ $subsite_url_by_install_id = array(); $install_url_by_install_id = array(); + $current_blog_id = get_current_blog_id(); + foreach ( $all_sites as $site ) { $site_details = $fs->get_site_info( $site ); @@ -96,6 +98,8 @@ $all_site_details[] = $site_details; } + $fs->switch_to_blog( $current_blog_id ); + if ( $is_network_activation ) { $vars = array( 'id' => $fs->get_id(), From a8e7e4ba18f0016b5bcf80defb30cab6274bf730 Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Thu, 10 Jul 2025 22:51:24 +0800 Subject: [PATCH 2/2] [multisite] Adapt a simplified version of the approach from the `feature/swas/restore_blog_fix_alt_exploration` branch. --- includes/class-freemius.php | 70 ++++++++++---------------- includes/class-fs-hook-snapshot.php | 45 +++++++++++++++++ require.php | 1 + start.php | 2 +- templates/account.php | 8 +-- templates/forms/license-activation.php | 4 -- 6 files changed, 75 insertions(+), 55 deletions(-) create mode 100644 includes/class-fs-hook-snapshot.php diff --git a/includes/class-freemius.php b/includes/class-freemius.php index 9d7573d0..fa28f512 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -7814,9 +7814,9 @@ private function maybe_activate_bundle_license( $license = null, $sites = array( * * @param FS_Plugin_License $license * @param array $sites - * @param int $blog_id + * @param int $current_blog_id */ - private function activate_bundle_license( $license, $sites = array(), $blog_id = 0 ) { + private function activate_bundle_license( $license, $sites = array(), $current_blog_id = 0 ) { $is_network_admin = fs_is_network_admin(); $installs_by_blog_map = array(); @@ -7849,16 +7849,16 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id = continue; } - if ( $blog_id > 0 ) { - $fs->switch_to_blog( $blog_id ); + if ( $current_blog_id > 0 ) { + $fs->switch_to_blog( $current_blog_id ); } if ( $fs->has_active_valid_license() ) { continue; } - if ( ! $is_network_admin || $blog_id > 0 ) { - if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $blog_id ) ) { + if ( ! $is_network_admin || $current_blog_id > 0 ) { + if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $current_blog_id ) ) { // Do not try to activate the license in the site level if the product is network active and the connection was not delegated. continue; } @@ -7895,20 +7895,18 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id = } } - $current_blog_id = get_current_blog_id(); - foreach ( $sites as $site ) { if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) { continue; } - $site_blog_id = $site['blog_id']; + $blog_id = $site['blog_id']; - if ( ! isset( $installs_by_blog_map[ $site_blog_id ] ) ) { - $installs_by_blog_map[ $site_blog_id ] = self::get_all_sites( $fs->get_module_type(), $site_blog_id ); + if ( ! isset( $installs_by_blog_map[ $blog_id ] ) ) { + $installs_by_blog_map[ $blog_id ] = self::get_all_sites( $fs->get_module_type(), $blog_id ); } - $installs = $installs_by_blog_map[ $site_blog_id ]; + $installs = $installs_by_blog_map[ $blog_id ]; $install = null; if ( isset( $installs[ $fs->get_slug() ] ) ) { @@ -7934,20 +7932,18 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id = break; } - if ( $fs->is_site_delegated_connection( $site_blog_id ) ) { + if ( $fs->is_site_delegated_connection( $blog_id ) ) { // Site activation delegated, don't activate bundle license on the site in the network admin. continue; } - if ( ! isset( $site_info_by_blog_map[ $site_blog_id ] ) ) { - $site_info_by_blog_map[ $site_blog_id ] = $fs->get_site_info( $site ); + if ( ! isset( $site_info_by_blog_map[ $blog_id ] ) ) { + $site_info_by_blog_map[ $blog_id ] = $fs->get_site_info( $site ); } - $filtered_sites[] = $site_info_by_blog_map[ $site_blog_id ]; + $filtered_sites[] = $site_info_by_blog_map[ $blog_id ]; } - $fs->switch_to_blog( $current_blog_id ); - if ( $has_install_with_license || empty( $filtered_sites ) ) { // 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. continue; @@ -7961,7 +7957,7 @@ private function activate_bundle_license( $license, $sites = array(), $blog_id = null, null, $sites, - ( $blog_id > 0 ? $blog_id : null ), + ( $current_blog_id > 0 ? $current_blog_id : null ), $license->user_id ); } @@ -8045,8 +8041,6 @@ function get_sites_for_network_level_optin() { $sites = array(); $all_sites = self::get_sites(); - $current_blog_id = get_current_blog_id(); - foreach ( $all_sites as $site ) { $blog_id = self::get_site_blog_id( $site ); @@ -8057,8 +8051,6 @@ function get_sites_for_network_level_optin() { } } - $this->switch_to_blog( $current_blog_id ); - return $sites; } @@ -9185,16 +9177,13 @@ private function get_installs_data_for_api( $install_url_by_install_id = array(); $subsite_registration_date_by_install_id = array(); - $current_blog_id = get_current_blog_id(); - $user = $this->_user; - foreach ( $sites as $site ) { $blog_id = self::get_site_blog_id( $site ); $install = $this->get_install_by_blog_id( $blog_id ); if ( is_object( $install ) ) { - if ( $install->user_id != $user->id ) { + if ( $install->user_id != $this->_user->id ) { // Install belongs to a different owner. continue; } @@ -9292,7 +9281,7 @@ private function get_installs_data_for_api( } } - $this->switch_to_blog( $current_blog_id ); + restore_current_blog(); $installs_data = array_merge( $installs_data, @@ -12279,15 +12268,11 @@ private function activate_license_on_many_sites( $license_key, array $site_ids = array() ) { - $current_blog_id = get_current_blog_id(); - $sites = array(); foreach ( $site_ids as $site_id ) { $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) ); } - $this->switch_to_blog( $current_blog_id ); - // Install the plugin. $result = $this->create_installs_with_user( $user, @@ -12603,8 +12588,6 @@ function is_whitelabeled( $ignore_data_debug_mode = false, $blog_id = null ) { ) { $sites = self::get_sites(); - $current_blog_id = get_current_blog_id(); - /** * If in network admin area and the add-on was not network-activated or network-activated * 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 ) { } } - $this->switch_to_blog( $current_blog_id ); - if ( $is_whitelabeled ) { break; } @@ -15793,9 +15774,13 @@ static function get_sites_blog_ids( $sites ) { * * @return array */ - function get_site_info( $site = null, $load_registration = false, $restore_current_blog = false ) { + function get_site_info( $site = null, $load_registration = false ) { $this->_logger->entrance(); + $fs_hook_snapshot = new FS_Hook_Snapshot(); + // Remove all filters from `switch_blog`. + $fs_hook_snapshot->remove( 'switch_blog' ); + $switched = false; $registration_date = null; @@ -15851,10 +15836,13 @@ function get_site_info( $site = null, $load_registration = false, $restore_curre $info[ 'registration_date' ] = $registration_date; } - if ( $switched && $restore_current_blog ) { + if ( $switched ) { restore_current_blog(); } + // Add the filters back to `switch_blog`. + $fs_hook_snapshot->restore( 'switch_blog' ); + return $info; } @@ -17038,7 +17026,7 @@ function get_opt_in_params( $override_with = array(), $network_level_or_blog_id array( 'blog_id' => $network_level_or_blog_id ) : null; - $site = $this->get_site_info( $site, false, true ); + $site = $this->get_site_info( $site ); $diagnostic_info = array(); if ( FS_Permission_Manager::instance( $this )->is_diagnostic_tracking_allowed() ) { @@ -17780,15 +17768,11 @@ private function install_many_pending_with_user( FS_Permission_Manager::PERMISSION_EXTENSIONS => $is_extensions_tracking_allowed, ) ); - $current_blog_id = get_current_blog_id(); - $sites = array(); foreach ( $site_ids as $site_id ) { $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) ); } - $this->switch_to_blog( $current_blog_id ); - $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites ); } diff --git a/includes/class-fs-hook-snapshot.php b/includes/class-fs-hook-snapshot.php new file mode 100644 index 00000000..3fb26412 --- /dev/null +++ b/includes/class-fs-hook-snapshot.php @@ -0,0 +1,45 @@ +removed_actions[ $hook ] = $wp_filter[ $hook ]; + unset( $wp_filter[ $hook ] ); + } + } + + /** + * Restore previously removed actions for a given hook. + */ + public function restore( $hook ) { + global $wp_filter; + + if ( ! empty( $wp_filter ) && isset( $this->removed_actions[ $hook ] ) ) { + $wp_filter[ $hook ] = $this->removed_actions[ $hook ]; + unset( $this->removed_actions[ $hook ] ); + } + } + } \ No newline at end of file diff --git a/require.php b/require.php index 7fa226a1..5289f11c 100644 --- a/require.php +++ b/require.php @@ -58,4 +58,5 @@ require_once WP_FS__DIR_INCLUDES . '/class-fs-admin-notices.php'; require_once WP_FS__DIR_INCLUDES . '/class-freemius-abstract.php'; require_once WP_FS__DIR_INCLUDES . '/sdk/Exceptions/Exception.php'; + require_once WP_FS__DIR_INCLUDES . '/class-fs-hook-snapshot.php'; require_once WP_FS__DIR_INCLUDES . '/class-freemius.php'; diff --git a/start.php b/start.php index 9b02a1f2..a86d7ae8 100644 --- a/start.php +++ b/start.php @@ -15,7 +15,7 @@ * * @var string */ - $this_sdk_version = '2.12.1'; + $this_sdk_version = '2.12.1.1'; #region SDK Selection Logic -------------------------------------------------------------------- diff --git a/templates/account.php b/templates/account.php index 77d29047..89120022 100755 --- a/templates/account.php +++ b/templates/account.php @@ -144,16 +144,12 @@ $sites = Freemius::get_sites(); $all_installs_plan_id = null; $all_installs_license_id = ( $show_license_row ? $license->id : null ); - - $current_blog_id = get_current_blog_id(); - $user = $fs->get_user(); - foreach ( $sites as $s ) { $site_info = $fs->get_site_info( $s ); $install = $fs->get_install_by_blog_id( $site_info['blog_id'] ); $view_params = array( 'freemius' => $fs, - 'user' => $user, + 'user' => $fs->get_user(), 'license' => $license, 'site' => $site_info, 'install' => $install, @@ -177,8 +173,6 @@ $show_license_row = false; } } - - $fs->switch_to_blog( $current_blog_id ); } $has_bundle_license = false; diff --git a/templates/forms/license-activation.php b/templates/forms/license-activation.php index 201d7d7f..3f009fff 100644 --- a/templates/forms/license-activation.php +++ b/templates/forms/license-activation.php @@ -56,8 +56,6 @@ $subsite_url_by_install_id = array(); $install_url_by_install_id = array(); - $current_blog_id = get_current_blog_id(); - foreach ( $all_sites as $site ) { $site_details = $fs->get_site_info( $site ); @@ -98,8 +96,6 @@ $all_site_details[] = $site_details; } - $fs->switch_to_blog( $current_blog_id ); - if ( $is_network_activation ) { $vars = array( 'id' => $fs->get_id(),