Make WordPress Core


Ignore:
Timestamp:
08/14/2019 02:28:52 PM (7 years ago)
Author:
SergeyBiryukov
Message:

Networks and Sites: Improve documentation and variable naming in switch_to_blog() and restore_current_blog().

In switch_to_blog():

  • Rename $blog_id to $prev_blog_id for clarity.
  • Rename $new_blog to $new_blog_id for consistency.
  • Pass $prev_blog_id as a second parameter to switch_blog action, instead of the duplicated $new_blog_id. This only clarifies documentation and does not affect functionality, since the values are equal in the context where the DocBlock is located.

In restore_current_blog():

  • Rename $blog to $new_blog_id for clarity.
  • Rename $blog_id to $prev_blog_id for clarity.

Props ChriCo, jeremyfelt, SergeyBiryukov.
Fixes #45594.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-blogs.php

    r45780 r45794  
    485485 * @global WP_Object_Cache $wp_object_cache
    486486 *
    487  * @param int  $new_blog   The id of the blog you want to switch to. Default: current blog
    488  * @param bool $deprecated Deprecated argument
    489  * @return true Always returns True.
    490  */
    491 function switch_to_blog( $new_blog, $deprecated = null ) {
     487 * @param int  $new_blog_id The ID of the blog to switch to. Default: current blog.
     488 * @param bool $deprecated  Not used.
     489 * @return true Always returns true.
     490 */
     491function switch_to_blog( $new_blog_id, $deprecated = null ) {
    492492        global $wpdb;
    493493
    494         $blog_id = get_current_blog_id();
    495         if ( empty( $new_blog ) ) {
    496                 $new_blog = $blog_id;
    497         }
    498 
    499         $GLOBALS['_wp_switched_stack'][] = $blog_id;
     494        $prev_blog_id = get_current_blog_id();
     495        if ( empty( $new_blog_id ) ) {
     496                $new_blog_id = $prev_blog_id;
     497        }
     498
     499        $GLOBALS['_wp_switched_stack'][] = $prev_blog_id;
    500500
    501501        /*
    … …  
    504504         * the extra unnecessary work
    505505         */
    506         if ( $new_blog == $blog_id ) {
     506        if ( $new_blog_id == $prev_blog_id ) {
    507507                /**
    508508                 * Fires when the blog is switched.
    … …  
    510510                 * @since MU (3.0.0)
    511511                 *
    512                  * @param int $new_blog New blog ID.
    513                  * @param int $new_blog Blog ID.
     512                 * @param int $new_blog_id New blog ID.
     513                 * @param int $prev_blog_id Previous blog ID.
    514514                 */
    515                 do_action( 'switch_blog', $new_blog, $new_blog );
     515                do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
    516516                $GLOBALS['switched'] = true;
    517517                return true;
    518518        }
    519519
    520         $wpdb->set_blog_id( $new_blog );
     520        $wpdb->set_blog_id( $new_blog_id );
    521521        $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
    522         $prev_blog_id            = $blog_id;
    523         $GLOBALS['blog_id']      = $new_blog;
     522        $GLOBALS['blog_id']      = $new_blog_id;
    524523
    525524        if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
    526                 wp_cache_switch_to_blog( $new_blog );
     525                wp_cache_switch_to_blog( $new_blog_id );
    527526        } else {
    528527                global $wp_object_cache;
    … …  
    546545
    547546        /** This filter is documented in wp-includes/ms-blogs.php */
    548         do_action( 'switch_blog', $new_blog, $prev_blog_id );
     547        do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
    549548        $GLOBALS['switched'] = true;
    550549
    … …  
    553552
    554553/**
    555  * Restore the current blog, after calling switch_to_blog()
     554 * Restore the current blog, after calling switch_to_blog().
    556555 *
    557556 * @see switch_to_blog()
    … …  
    565564 * @global WP_Object_Cache $wp_object_cache
    566565 *
    567  * @return bool True on success, false if we're already on the current blog
     566 * @return bool True on success, false if we're already on the current blog.
    568567 */
    569568function restore_current_blog() {
    … …  
    574573        }
    575574
    576         $blog    = array_pop( $GLOBALS['_wp_switched_stack'] );
    577         $blog_id = get_current_blog_id();
    578 
    579         if ( $blog_id == $blog ) {
     575        $new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
     576        $prev_blog_id = get_current_blog_id();
     577
     578        if ( $new_blog_id == $prev_blog_id ) {
    580579                /** This filter is documented in wp-includes/ms-blogs.php */
    581                 do_action( 'switch_blog', $blog, $blog );
     580                do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
    582581                // If we still have items in the switched stack, consider ourselves still 'switched'
    583582                $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
    … …  
    585584        }
    586585
    587         $wpdb->set_blog_id( $blog );
    588         $prev_blog_id            = $blog_id;
    589         $GLOBALS['blog_id']      = $blog;
     586        $wpdb->set_blog_id( $new_blog_id );
     587        $GLOBALS['blog_id']      = $new_blog_id;
    590588        $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
    591589
    592590        if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
    593                 wp_cache_switch_to_blog( $blog );
     591                wp_cache_switch_to_blog( $new_blog_id );
    594592        } else {
    595593                global $wp_object_cache;
    … …  
    614612
    615613        /** This filter is documented in wp-includes/ms-blogs.php */
    616         do_action( 'switch_blog', $blog, $prev_blog_id );
     614        do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
    617615
    618616        // If we still have items in the switched stack, consider ourselves still 'switched'
Note: See TracChangeset for help on using the changeset viewer.