[wp-trac] [WordPress Trac] #50391: Unnecessary switch_to_blog() in get_blog_details()

WordPress Trac noreply at wordpress.org
Mon Jun 15 10:53:27 UTC 2020


#50391: Unnecessary switch_to_blog() in get_blog_details()
--------------------------------+--------------------
 Reporter:  SergeyBiryukov      |      Owner:  (none)
     Type:  defect (bug)        |     Status:  new
 Priority:  normal              |  Milestone:  5.5
Component:  Networks and Sites  |    Version:
 Severity:  normal              |   Keywords:
  Focuses:  multisite           |
--------------------------------+--------------------
 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50391>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list