From d9c53241dc45a9a801e4217661e9916041490886 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 15 Jun 2020 12:29:13 +0000 Subject: [PATCH] Networks and Sites: Don't unnecessarily switch to the current blog in `get_blog_details()`. Props arpitgshah, djennez, acsnaterse, joostdevalk, SergeyBiryukov. Fixes #50391. git-svn-id: https://develop.svn.wordpress.org/trunk@48044 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-blogs.php | 21 +++++++++++++++++-- .../tests/multisite/getBlogDetails.php | 10 +++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 4adb674b8c..3da331ef12 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -238,12 +238,21 @@ function get_blog_details( $fields = null, $get_all = true ) { return $details; } - switch_to_blog( $blog_id ); + $switched_blog = false; + + if ( get_current_blog_id() !== $blog_id ) { + switch_to_blog( $blog_id ); + $switched_blog = true; + } + $details->blogname = get_option( 'blogname' ); $details->siteurl = get_option( 'siteurl' ); $details->post_count = get_option( 'post_count' ); $details->home = get_option( 'home' ); - restore_current_blog(); + + if ( $switched_blog ) { + restore_current_blog(); + } /** * Filters a blog's details. @@ -516,7 +525,9 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { * or 'restore' when called from restore_current_blog(). */ do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' ); + $GLOBALS['switched'] = true; + return true; } @@ -534,6 +545,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { } else { $global_groups = false; } + wp_cache_init(); if ( function_exists( 'wp_cache_add_global_groups' ) ) { @@ -542,12 +554,14 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { } else { wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); } + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); } } /** This filter is documented in wp-includes/ms-blogs.php */ do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' ); + $GLOBALS['switched'] = true; return true; @@ -581,8 +595,10 @@ function restore_current_blog() { if ( $new_blog_id == $prev_blog_id ) { /** This filter is documented in wp-includes/ms-blogs.php */ do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' ); + // If we still have items in the switched stack, consider ourselves still 'switched'. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); + return true; } @@ -609,6 +625,7 @@ function restore_current_blog() { } else { wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); } + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); } } diff --git a/tests/phpunit/tests/multisite/getBlogDetails.php b/tests/phpunit/tests/multisite/getBlogDetails.php index 1ed6b89a83..51d980465c 100644 --- a/tests/phpunit/tests/multisite/getBlogDetails.php +++ b/tests/phpunit/tests/multisite/getBlogDetails.php @@ -142,6 +142,16 @@ if ( is_multisite() ) : $this->assertFalse( $site ); } + /** + * @ticket 50391 + */ + public function test_get_blog_details_does_not_switch_to_current_blog() { + $count = did_action( 'switch_blog' ); + + get_blog_details(); + $this->assertSame( $count, did_action( 'switch_blog' ) ); + } + /** * @dataProvider data_get_all *