From 942fc4e0fccf5731b6c7a1696d5c24fbae67a986 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 15 Jun 2020 12:31:11 +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. Built from https://develop.svn.wordpress.org/trunk@48044 git-svn-id: http://core.svn.wordpress.org/trunk@47811 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/ms-blogs.php | 21 +++++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index 4adb674b8c..3da331ef12 100644 --- a/wp-includes/ms-blogs.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index 52f1d9afc3..53d221b49f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-48043'; +$wp_version = '5.5-alpha-48044'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.