Make WordPress Core


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