Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#50391 closed defect (bug) (fixed)

Unnecessary switch_to_blog() in get_blog_details()

Reported by: sergeybiryukov's profile SergeyBiryukov Owned by: sergeybiryukov's profile 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)

#1 follow-up: @arpitgshah
4 years ago

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

#2 in reply to: ↑ 1 @SergeyBiryukov
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 @joostdevalk
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

#5 @SergeyBiryukov
4 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 48044:

Networks and Sites: Don't unnecessarily switch to the current blog in get_blog_details().

Props arpitgshah, djennez, acsnaterse, joostdevalk, SergeyBiryukov.
Fixes #50391.

desrosj commented on PR #336:


4 years ago
#6

Hi @helloarpitgshah, thanks for this. It was merged into core in https://core.trac.wordpress.org/changeset/48044.

This ticket was mentioned in Slack in #community-events by xhtmlpoint. View the logs.


4 years ago

Note: See TracTickets for help on using tickets.