Make WordPress Core


Ignore:
Timestamp:
08/09/2012 04:28:15 PM (12 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.
File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.