#50391 closed defect (bug) (fixed)
Unnecessary switch_to_blog() in get_blog_details()
Reported by: | SergeyBiryukov | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.5 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Networks and Sites | Keywords: | good-first-bug has-patch |
Focuses: | multisite | Cc: |
Description
See this code in get_blog_details()
:
switch_to_blog( $blog_id ); $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();
This happens regardless of whether the blog ID in $wpdb
is the same as the blog being checked.
Though switch_to_blog()
itself has a contingency built in for this situation to prevent too much unnecessary overhead:
/* * If we're switching to the same blog id that we're on, * set the right vars, do the associated actions, but skip * the extra unnecessary work */ if ( $new_blog_id == $prev_blog_id ) { /** * Fires when the blog is switched. * * @since MU (3.0.0) * @since 5.4.0 The `$context` parameter was added. * * @param int $new_blog_id New blog ID. * @param int $prev_blog_id Previous blog ID. * @param string $context Additional context. Accepts 'switch' when called from switch_to_blog() * or 'restore' when called from restore_current_blog(). */ do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' ); $GLOBALS['switched'] = true; return true; }
This still triggers the switch_blog
action and still sets a switched state, but it skips completely switching to another instance. This is reverted once the $details
are filled in get_blog_details()
.
Ideally, the unnecessary switching should be prevented altogether.
Props to @djennez for the report.
Change History (7)
#2
in reply to:
↑ 1
@
4 years ago
- Keywords needs-patch good-first-bug added
Replying to arpitgshah:
Do we need to remove this action or need to get all the details with new blog?
Thanks for your interest!
We need to only perform the switch if $blog_id
is different from the current blog ID. Something like this:
$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' ); if ( $switched_blog ) { restore_current_blog(); }
#3
@
4 years ago
Props to @acsnaterse for finding the issue initially and reporting it on the Yoast SEO GitHub repo.
This ticket was mentioned in PR #336 on WordPress/wordpress-develop by helloarpitgshah.
4 years ago
#4
- Keywords has-patch added; needs-patch removed
Trac ticket:
#5
@
4 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 48044:
4 years ago
#6
Hi @helloarpitgshah, thanks for this. It was merged into core in https://core.trac.wordpress.org/changeset/48044.
Hey,
Do we need to remove this action or need to get all the details with new blog?
Can you please explain me more?
I am new in this but have 10 years of experience with WordPress Code.
Thanks,
Arpit G Shah