WordPress.org

Make WordPress Core

Changeset 21485


Ignore:
Timestamp:
08/09/2012 04:28:15 PM (6 years ago)
Author:
ryan
Message:

switch_to_blog() and restore_current_blog() housekeeping.

wp-includes/admin-bar.php:

  • Replace get_admin_url() and get_home_url() with admin_url() and home_url() and place them inside a switch/restore. Likewise replace current_user_can_for_blog() with current_user_can(). This avoids doing multiple switch restores.

wp-includes/ms-blogs.php:

  • Deprecate the $validate argument to switch_to_blog(). This avoids a not very necessary call to get_blog_details(), possibly saving a few queries.
  • Use $_wp_switched and $_wp_switched_stack instead of $switched and $switched_stack to make it less likely these globals will be stomped.
  • Use GLOBALS to access blog_id and other globals. I've preferred this style lately since it makes it obvious a global is being used and avoids global blog_id being stomped by a local variable.
  • Lose some is_object() checks. wp_get_current_user() always returns an object, for example.
  • Call the new WP_Roles::reinit() method.

wp-includes/class-wp-xmlrpc-server.php:

  • Replace current_user_can_for_blog() with current_user_can() and move it inside the switch/restore pair. This eliminates a switch/restore.

wp-includes/capabilities.php:

  • Use array_keys() instead of $role => $data since $data is unused. I *think* this is a bit faster.
  • Introduce WP_Roles::reinit(). This reinitializes WP_Roles and is used after switch_to_blog() has already update the blog ID in the wpdb object. If a global roles array is being used instead of the db, reinit is skipped.
  • current_user_can_for_blog() now does a switch/restore. It didn't before meaning it could be reinitializing the user with the wrong role information for the current blog.

wp-includes/ms-settings.php:

  • Define $_wp_switched_stack and $_wp_switched. This way switch_to_blog() and restore_current_blog() can rely on it being set.

wp-settings.php:

  • Instantiate the WP_Roles global. This was it is always defined during init. To remove the WP_Roles checks from WP_Role and WP_User this would probably have to move before plugins are loaded, which might not be a good thing.

wp-includes/functions.php:

  • Update wp_upload_dir() to reference _wp_switched.
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/admin-bar.php

    r21425 r21485  
    345345
    346346    foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
    347         // @todo Replace with some favicon lookup.
    348         //$blavatar = '<img src="' . esc_url( blavatar_url( blavatar_domain( $blog->siteurl ), 'img', 16, $blue_wp_logo_url ) ) . '" alt="Blavatar" width="16" height="16" />';
     347        switch_to_blog( $blog->userblog_id );
     348
    349349        $blavatar = '<img src="' . esc_url($blue_wp_logo_url) . '" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
    350350
     
    356356            'id'        => $menu_id,
    357357            'title'     => $blavatar . $blogname,
    358             'href'      => get_admin_url( $blog->userblog_id ),
     358            'href'      => admin_url(),
    359359        ) );
    360360
     
    363363            'id'     => $menu_id . '-d',
    364364            'title'  => __( 'Dashboard' ),
    365             'href'   => get_admin_url( $blog->userblog_id ),
    366         ) );
    367 
    368         if ( current_user_can_for_blog( $blog->userblog_id, 'edit_posts' ) ) {
     365            'href'   => admin_url(),
     366        ) );
     367
     368        if ( current_user_can( 'edit_posts' ) ) {
    369369            $wp_admin_bar->add_menu( array(
    370370                'parent' => $menu_id,
    371371                'id'     => $menu_id . '-n',
    372372                'title'  => __( 'New Post' ),
    373                 'href'   => get_admin_url( $blog->userblog_id, 'post-new.php' ),
     373                'href'   => admin_url( 'post-new.php' ),
    374374            ) );
    375375            $wp_admin_bar->add_menu( array(
     
    377377                'id'     => $menu_id . '-c',
    378378                'title'  => __( 'Manage Comments' ),
    379                 'href'   => get_admin_url( $blog->userblog_id, 'edit-comments.php' ),
     379                'href'   => admin_url( 'edit-comments.php' ),
    380380            ) );
    381381        }
     
    385385            'id'     => $menu_id . '-v',
    386386            'title'  => __( 'Visit Site' ),
    387             'href'   => get_home_url( $blog->userblog_id, '/' ),
    388         ) );
     387            'href'   => home_url( '/' ),
     388        ) );
     389
     390        restore_current_blog();
    389391    }
    390392}
  • trunk/wp-includes/capabilities.php

    r21413 r21485  
    109109        $this->role_objects = array();
    110110        $this->role_names =  array();
    111         foreach ( (array) $this->roles as $role => $data ) {
     111        foreach ( array_keys( $this->roles ) as $role ) {
     112            $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
     113            $this->role_names[$role] = $this->roles[$role]['name'];
     114        }
     115    }
     116
     117    /**
     118     * Reinitialize the object
     119     *
     120     * Recreates the role objects. This is typically called only by switch_to_blog()
     121     * after switching wpdb to a new blog ID.
     122     *
     123     * @since 3.5.0
     124     * @access public
     125     */
     126    function reinit() {
     127        // There is no need to reinit if using the wp_user_roles global.
     128        if ( ! $this->use_db )
     129            return;
     130
     131        global $wpdb, $wp_user_roles;
     132
     133        // Duplicated from _init() to avoid an extra function call.
     134        $this->role_key = $wpdb->prefix . 'user_roles';
     135        $this->roles = get_option( $this->role_key );
     136        if ( empty( $this->roles ) )
     137            return;
     138
     139        $this->role_objects = array();
     140        $this->role_names =  array();
     141        foreach ( array_keys( $this->roles ) as $role ) {
    112142            $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
    113143            $this->role_names[$role] = $this->roles[$role]['name'];
     
    234264     * @return bool
    235265     */
    236     function is_role( $role )
    237     {
     266    function is_role( $role ) {
    238267        return isset( $this->role_names[$role] );
    239268    }
     
    12321261 */
    12331262function current_user_can_for_blog( $blog_id, $capability ) {
     1263    switch_to_blog( $blog_id );
     1264
    12341265    $current_user = wp_get_current_user();
    12351266
     
    12371268        return false;
    12381269
    1239     // Create new object to avoid stomping the global current_user.
    1240     $user = new WP_User( $current_user->ID );
    1241 
    1242     // Set the blog id. @todo add blog id arg to WP_User constructor?
    1243     $user->for_blog( $blog_id );
    1244 
    12451270    $args = array_slice( func_get_args(), 2 );
    12461271    $args = array_merge( array( $capability ), $args );
    12471272
    1248     return call_user_func_array( array( &$user, 'has_cap' ), $args );
     1273    $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
     1274
     1275    restore_current_blog();
     1276
     1277    return $can;
    12491278}
    12501279
  • trunk/wp-includes/class-wp-xmlrpc-server.php

    r21414 r21485  
    474474
    475475            $blog_id = $blog->userblog_id;
    476             $is_admin = current_user_can_for_blog( $blog_id, 'manage_options' );
    477476
    478477            switch_to_blog( $blog_id );
     478
     479            $is_admin = current_user_can( 'manage_options' );
     480
    479481            $struct[] = array(
    480482                'isAdmin'       => $is_admin,
     
    484486                'xmlrpc'        => site_url( 'xmlrpc.php' )
    485487            );
     488
    486489            restore_current_blog();
    487490        }
  • trunk/wp-includes/functions.php

    r21373 r21485  
    14331433 */
    14341434function wp_upload_dir( $time = null ) {
    1435     global $switched;
     1435    global $_wp_switched;
    14361436    $siteurl = get_option( 'siteurl' );
    14371437    $upload_path = get_option( 'upload_path' );
     
    14571457    }
    14581458
    1459     if ( defined('UPLOADS') && !$main_override && ( !isset( $switched ) || $switched === false ) ) {
     1459    if ( defined('UPLOADS') && ! $main_override && ! $_wp_switched ) {
    14601460        $dir = ABSPATH . UPLOADS;
    14611461        $url = trailingslashit( $siteurl ) . UPLOADS;
    14621462    }
    14631463
    1464     if ( is_multisite() && !$main_override && ( !isset( $switched ) || $switched === false ) ) {
     1464    if ( is_multisite() && ! $main_override && ! $_wp_switched ) {
    14651465        if ( defined( 'BLOGUPLOADDIR' ) )
    14661466            $dir = untrailingslashit(BLOGUPLOADDIR);
  • trunk/wp-includes/ms-blogs.php

    r21480 r21485  
    447447 *
    448448 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
    449  * @param bool $validate Whether to check if $new_blog exists before proceeding
     449 * @param bool $deprecated Depecreated argument
    450450 * @return bool True on success, False if the validation failed
    451451 */
    452 function switch_to_blog( $new_blog, $validate = false ) {
    453     global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
    454 
    455     if ( empty($new_blog) )
    456         $new_blog = $blog_id;
    457 
    458     if ( $validate && ! get_blog_details( $new_blog ) )
    459         return false;
    460 
    461     if ( empty($switched_stack) )
    462         $switched_stack = array();
    463 
    464     $switched_stack[] = $blog_id;
     452function switch_to_blog( $new_blog, $deprecated = null ) {
     453    global $wpdb, $wp_roles;
     454
     455    if ( empty( $new_blog ) )
     456        $new_blog = $GLOBALS['blog_id'];
     457
     458    $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
    465459
    466460    /* If we're switching to the same blog id that we're on,
    467461    * set the right vars, do the associated actions, but skip
    468462    * the extra unnecessary work */
    469     if ( $blog_id == $new_blog ) {
    470         do_action( 'switch_blog', $blog_id, $blog_id );
    471         $switched = true;
     463    if ( $new_blog == $GLOBALS['blog_id'] ) {
     464        do_action( 'switch_blog', $new_blog, $new_blog );
     465        $GLOBALS['_wp_switched'] = true;
    472466        return true;
    473467    }
    474468
    475     $wpdb->set_blog_id($new_blog);
    476     $table_prefix = $wpdb->prefix;
    477     $prev_blog_id = $blog_id;
    478     $blog_id = $new_blog;
    479 
    480     if ( is_object( $wp_roles ) ) {
    481         $wpdb->suppress_errors();
    482         if ( method_exists( $wp_roles ,'_init' ) )
    483             $wp_roles->_init();
    484         elseif ( method_exists( $wp_roles, '__construct' ) )
    485             $wp_roles->__construct();
    486         $wpdb->suppress_errors( false );
    487     }
    488 
    489     if ( did_action('init') ) {
     469    $wpdb->set_blog_id( $new_blog );
     470    $GLOBALS['table_prefix'] = $wpdb->prefix;
     471    $prev_blog_id = $GLOBALS['blog_id'];
     472    $GLOBALS['blog_id'] = $new_blog;
     473
     474    if ( did_action( 'init' ) ) {
     475        $wp_roles->reinit();
    490476        $current_user = wp_get_current_user();
    491         if ( is_object( $current_user ) )
    492             $current_user->for_blog( $blog_id );
     477        $current_user->for_blog( $new_blog );
    493478    }
    494479
    495480    if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
    496         wp_cache_switch_to_blog( $blog_id );
     481        wp_cache_switch_to_blog( $new_blog );
    497482    } else {
     483        global $wp_object_cache;
     484
    498485        if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
    499486            $global_groups = $wp_object_cache->global_groups;
     
    502489   
    503490        wp_cache_init();
    504         if ( function_exists('wp_cache_add_global_groups') ) {
     491
     492        if ( function_exists( 'wp_cache_add_global_groups' ) ) {
    505493            if ( is_array( $global_groups ) )
    506494                wp_cache_add_global_groups( $global_groups );
    507495            else
    508496                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
    509             wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
     497            wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
    510498        }
    511499    }
    512500
    513     do_action('switch_blog', $blog_id, $prev_blog_id);
    514     $switched = true;
     501    do_action( 'switch_blog', $new_blog, $prev_blog_id );
     502    $GLOBALS['_wp_switched'] = true;
     503
    515504    return true;
    516505}
     
    525514 */
    526515function restore_current_blog() {
    527     global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
    528 
    529     if ( !$switched )
     516    global $wpdb, $wp_roles;
     517
     518    if ( ! $GLOBALS['_wp_switched'] )
    530519        return false;
    531520
    532     if ( !is_array( $switched_stack ) )
    533         return false;
    534 
    535     $blog = array_pop( $switched_stack );
    536     if ( $blog_id == $blog ) {
     521    $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
     522
     523    if ( $GLOBALS['blog_id'] == $blog ) {
    537524        do_action( 'switch_blog', $blog, $blog );
    538         /* If we still have items in the switched stack, consider ourselves still 'switched' */
    539         $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
     525        // If we still have items in the switched stack, consider ourselves still 'switched'
     526        $GLOBALS['_wp_switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
    540527        return true;
    541528    }
    542529
    543     $wpdb->set_blog_id($blog);
    544     $prev_blog_id = $blog_id;
    545     $blog_id = $blog;
    546     $table_prefix = $wpdb->prefix;
    547 
    548     if ( is_object( $wp_roles ) ) {
    549         $wpdb->suppress_errors();
    550         if ( method_exists( $wp_roles ,'_init' ) )
    551             $wp_roles->_init();
    552         elseif ( method_exists( $wp_roles, '__construct' ) )
    553             $wp_roles->__construct();
    554         $wpdb->suppress_errors( false );
    555     }
    556 
    557     if ( did_action('init') ) {
     530    $wpdb->set_blog_id( $blog );
     531    $prev_blog_id = $GLOBALS['blog_id'];
     532    $GLOBALS['blog_id'] = $blog;
     533    $GLOBALS['table_prefix'] = $wpdb->prefix;
     534
     535    if ( did_action( 'init' ) ) {
     536        $wp_roles->reinit();
    558537        $current_user = wp_get_current_user();
    559         if ( is_object( $current_user ) )
    560             $current_user->for_blog( $blog_id );
     538        $current_user->for_blog( $blog );
    561539    }
    562540
    563541    if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
    564         wp_cache_switch_to_blog( $blog_id );
     542        wp_cache_switch_to_blog( $blog );
    565543    } else {
     544        global $wp_object_cache;
     545
    566546        if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
    567547            $global_groups = $wp_object_cache->global_groups;
     
    570550   
    571551        wp_cache_init();
    572         if ( function_exists('wp_cache_add_global_groups') ) {
     552
     553        if ( function_exists( 'wp_cache_add_global_groups' ) ) {
    573554            if ( is_array( $global_groups ) )
    574555                wp_cache_add_global_groups( $global_groups );
    575556            else
    576557                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
    577             wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
     558            wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
    578559        }
    579560    }
    580561
    581     do_action('switch_blog', $blog_id, $prev_blog_id);
    582 
    583     /* If we still have items in the switched stack, consider ourselves still 'switched' */
    584     $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
     562    do_action( 'switch_blog', $blog, $prev_blog_id );
     563
     564    // If we still have items in the switched stack, consider ourselves still 'switched'
     565    $GLOBALS['_wp_switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
     566
    585567    return true;
    586568}
  • trunk/wp-includes/ms-settings.php

    r19760 r21485  
    127127$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
    128128$table_prefix = $wpdb->get_blog_prefix();
     129$_wp_switched_stack = array();
     130$_wp_switched = false;
    129131
    130132// need to init cache again after blog_id is set
  • trunk/wp-settings.php

    r21186 r21485  
    253253$GLOBALS['wp_widget_factory'] = new WP_Widget_Factory();
    254254
     255/**
     256 * WordPress User Roles
     257 * @global object $wp_roles
     258 * @since 2.0.0
     259 */
     260$GLOBALS['wp_roles'] = new WP_Roles();
     261
    255262do_action( 'setup_theme' );
    256263
Note: See TracChangeset for help on using the changeset viewer.