<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[21485] trunk: switch_to_blog() and restore_current_blog() housekeeping.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/21485">21485</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2012-08-09 16:28:15 +0000 (Thu, 09 Aug 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 =&gt; $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.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesadminbarphp">trunk/wp-includes/admin-bar.php</a></li>
<li><a href="#trunkwpincludescapabilitiesphp">trunk/wp-includes/capabilities.php</a></li>
<li><a href="#trunkwpincludesclasswpxmlrpcserverphp">trunk/wp-includes/class-wp-xmlrpc-server.php</a></li>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
<li><a href="#trunkwpincludesmsblogsphp">trunk/wp-includes/ms-blogs.php</a></li>
<li><a href="#trunkwpincludesmssettingsphp">trunk/wp-includes/ms-settings.php</a></li>
<li><a href="#trunkwpsettingsphp">trunk/wp-settings.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesadminbarphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/admin-bar.php (21484 => 21485)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/admin-bar.php        2012-08-09 13:17:14 UTC (rev 21484)
+++ trunk/wp-includes/admin-bar.php        2012-08-09 16:28:15 UTC (rev 21485)
</span><span class="lines">@@ -344,8 +344,8 @@
</span><span class="cx">         $blue_wp_logo_url = includes_url('images/wpmini-blue.png');
</span><span class="cx"> 
</span><span class="cx">         foreach ( (array) $wp_admin_bar-&gt;user-&gt;blogs as $blog ) {
</span><del>-                // @todo Replace with some favicon lookup.
-                //$blavatar = '&lt;img src=&quot;' . esc_url( blavatar_url( blavatar_domain( $blog-&gt;siteurl ), 'img', 16, $blue_wp_logo_url ) ) . '&quot; alt=&quot;Blavatar&quot; width=&quot;16&quot; height=&quot;16&quot; /&gt;';
</del><ins>+                switch_to_blog( $blog-&gt;userblog_id );
+
</ins><span class="cx">                 $blavatar = '&lt;img src=&quot;' . esc_url($blue_wp_logo_url) . '&quot; alt=&quot;' . esc_attr__( 'Blavatar' ) . '&quot; width=&quot;16&quot; height=&quot;16&quot; class=&quot;blavatar&quot;/&gt;';
</span><span class="cx"> 
</span><span class="cx">                 $blogname = empty( $blog-&gt;blogname ) ? $blog-&gt;domain : $blog-&gt;blogname;
</span><span class="lines">@@ -355,28 +355,28 @@
</span><span class="cx">                         'parent'    =&gt; 'my-sites-list',
</span><span class="cx">                         'id'        =&gt; $menu_id,
</span><span class="cx">                         'title'     =&gt; $blavatar . $blogname,
</span><del>-                        'href'      =&gt; get_admin_url( $blog-&gt;userblog_id ),
</del><ins>+                        'href'      =&gt; admin_url(),
</ins><span class="cx">                 ) );
</span><span class="cx"> 
</span><span class="cx">                 $wp_admin_bar-&gt;add_menu( array(
</span><span class="cx">                         'parent' =&gt; $menu_id,
</span><span class="cx">                         'id'     =&gt; $menu_id . '-d',
</span><span class="cx">                         'title'  =&gt; __( 'Dashboard' ),
</span><del>-                        'href'   =&gt; get_admin_url( $blog-&gt;userblog_id ),
</del><ins>+                        'href'   =&gt; admin_url(),
</ins><span class="cx">                 ) );
</span><span class="cx"> 
</span><del>-                if ( current_user_can_for_blog( $blog-&gt;userblog_id, 'edit_posts' ) ) {
</del><ins>+                if ( current_user_can( 'edit_posts' ) ) {
</ins><span class="cx">                         $wp_admin_bar-&gt;add_menu( array(
</span><span class="cx">                                 'parent' =&gt; $menu_id,
</span><span class="cx">                                 'id'     =&gt; $menu_id . '-n',
</span><span class="cx">                                 'title'  =&gt; __( 'New Post' ),
</span><del>-                                'href'   =&gt; get_admin_url( $blog-&gt;userblog_id, 'post-new.php' ),
</del><ins>+                                'href'   =&gt; admin_url( 'post-new.php' ),
</ins><span class="cx">                         ) );
</span><span class="cx">                         $wp_admin_bar-&gt;add_menu( array(
</span><span class="cx">                                 'parent' =&gt; $menu_id,
</span><span class="cx">                                 'id'     =&gt; $menu_id . '-c',
</span><span class="cx">                                 'title'  =&gt; __( 'Manage Comments' ),
</span><del>-                                'href'   =&gt; get_admin_url( $blog-&gt;userblog_id, 'edit-comments.php' ),
</del><ins>+                                'href'   =&gt; admin_url( 'edit-comments.php' ),
</ins><span class="cx">                         ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="lines">@@ -384,8 +384,10 @@
</span><span class="cx">                         'parent' =&gt; $menu_id,
</span><span class="cx">                         'id'     =&gt; $menu_id . '-v',
</span><span class="cx">                         'title'  =&gt; __( 'Visit Site' ),
</span><del>-                        'href'   =&gt; get_home_url( $blog-&gt;userblog_id, '/' ),
</del><ins>+                        'href'   =&gt; home_url( '/' ),
</ins><span class="cx">                 ) );
</span><ins>+
+                restore_current_blog();
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludescapabilitiesphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/capabilities.php (21484 => 21485)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/capabilities.php        2012-08-09 13:17:14 UTC (rev 21484)
+++ trunk/wp-includes/capabilities.php        2012-08-09 16:28:15 UTC (rev 21485)
</span><span class="lines">@@ -108,13 +108,43 @@
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;role_objects = array();
</span><span class="cx">                 $this-&gt;role_names =  array();
</span><del>-                foreach ( (array) $this-&gt;roles as $role =&gt; $data ) {
</del><ins>+                foreach ( array_keys( $this-&gt;roles ) as $role ) {
</ins><span class="cx">                         $this-&gt;role_objects[$role] = new WP_Role( $role, $this-&gt;roles[$role]['capabilities'] );
</span><span class="cx">                         $this-&gt;role_names[$role] = $this-&gt;roles[$role]['name'];
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Reinitialize the object
+         *
+         * Recreates the role objects. This is typically called only by switch_to_blog()
+         * after switching wpdb to a new blog ID.
+         *
+         * @since 3.5.0
+         * @access public
+         */
+        function reinit() {
+                // There is no need to reinit if using the wp_user_roles global.
+                if ( ! $this-&gt;use_db )
+                        return;
+
+                global $wpdb, $wp_user_roles;
+
+                // Duplicated from _init() to avoid an extra function call.
+                $this-&gt;role_key = $wpdb-&gt;prefix . 'user_roles';
+                $this-&gt;roles = get_option( $this-&gt;role_key );
+                if ( empty( $this-&gt;roles ) )
+                        return;
+
+                $this-&gt;role_objects = array();
+                $this-&gt;role_names =  array();
+                foreach ( array_keys( $this-&gt;roles ) as $role ) {
+                        $this-&gt;role_objects[$role] = new WP_Role( $role, $this-&gt;roles[$role]['capabilities'] );
+                        $this-&gt;role_names[$role] = $this-&gt;roles[$role]['name'];
+                }
+        }
+
+        /**
</ins><span class="cx">          * Add role name with capabilities to list.
</span><span class="cx">          *
</span><span class="cx">          * Updates the list of roles, if the role doesn't already exist.
</span><span class="lines">@@ -233,8 +263,7 @@
</span><span class="cx">          * @param string $role Role name to look up.
</span><span class="cx">          * @return bool
</span><span class="cx">          */
</span><del>-        function is_role( $role )
-        {
</del><ins>+        function is_role( $role ) {
</ins><span class="cx">                 return isset( $this-&gt;role_names[$role] );
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="lines">@@ -1231,21 +1260,21 @@
</span><span class="cx">  * @return bool
</span><span class="cx">  */
</span><span class="cx"> function current_user_can_for_blog( $blog_id, $capability ) {
</span><ins>+        switch_to_blog( $blog_id );
+
</ins><span class="cx">         $current_user = wp_get_current_user();
</span><span class="cx"> 
</span><span class="cx">         if ( empty( $current_user ) )
</span><span class="cx">                 return false;
</span><span class="cx"> 
</span><del>-        // Create new object to avoid stomping the global current_user.
-        $user = new WP_User( $current_user-&gt;ID );
-
-        // Set the blog id. @todo add blog id arg to WP_User constructor?
-        $user-&gt;for_blog( $blog_id );
-
</del><span class="cx">         $args = array_slice( func_get_args(), 2 );
</span><span class="cx">         $args = array_merge( array( $capability ), $args );
</span><span class="cx"> 
</span><del>-        return call_user_func_array( array( &amp;$user, 'has_cap' ), $args );
</del><ins>+        $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
+
+        restore_current_blog();
+
+        return $can;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpincludesclasswpxmlrpcserverphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-xmlrpc-server.php (21484 => 21485)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-xmlrpc-server.php        2012-08-09 13:17:14 UTC (rev 21484)
+++ trunk/wp-includes/class-wp-xmlrpc-server.php        2012-08-09 16:28:15 UTC (rev 21485)
</span><span class="lines">@@ -473,9 +473,11 @@
</span><span class="cx">                                 continue;
</span><span class="cx"> 
</span><span class="cx">                         $blog_id = $blog-&gt;userblog_id;
</span><del>-                        $is_admin = current_user_can_for_blog( $blog_id, 'manage_options' );
</del><span class="cx"> 
</span><span class="cx">                         switch_to_blog( $blog_id );
</span><ins>+
+                        $is_admin = current_user_can( 'manage_options' );
+
</ins><span class="cx">                         $struct[] = array(
</span><span class="cx">                                 'isAdmin'                =&gt; $is_admin,
</span><span class="cx">                                 'url'                        =&gt; home_url( '/' ),
</span><span class="lines">@@ -483,6 +485,7 @@
</span><span class="cx">                                 'blogName'                =&gt; get_option( 'blogname' ),
</span><span class="cx">                                 'xmlrpc'                =&gt; site_url( 'xmlrpc.php' )
</span><span class="cx">                         );
</span><ins>+
</ins><span class="cx">                         restore_current_blog();
</span><span class="cx">                 }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (21484 => 21485)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2012-08-09 13:17:14 UTC (rev 21484)
+++ trunk/wp-includes/functions.php        2012-08-09 16:28:15 UTC (rev 21485)
</span><span class="lines">@@ -1432,7 +1432,7 @@
</span><span class="cx">  * @return array See above for description.
</span><span class="cx">  */
</span><span class="cx"> function wp_upload_dir( $time = null ) {
</span><del>-        global $switched;
</del><ins>+        global $_wp_switched;
</ins><span class="cx">         $siteurl = get_option( 'siteurl' );
</span><span class="cx">         $upload_path = get_option( 'upload_path' );
</span><span class="cx">         $upload_path = trim($upload_path);
</span><span class="lines">@@ -1456,12 +1456,12 @@
</span><span class="cx">                         $url = trailingslashit( $siteurl ) . $upload_path;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( defined('UPLOADS') &amp;&amp; !$main_override &amp;&amp; ( !isset( $switched ) || $switched === false ) ) {
</del><ins>+        if ( defined('UPLOADS') &amp;&amp; ! $main_override &amp;&amp; ! $_wp_switched ) {
</ins><span class="cx">                 $dir = ABSPATH . UPLOADS;
</span><span class="cx">                 $url = trailingslashit( $siteurl ) . UPLOADS;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( is_multisite() &amp;&amp; !$main_override &amp;&amp; ( !isset( $switched ) || $switched === false ) ) {
</del><ins>+        if ( is_multisite() &amp;&amp; ! $main_override &amp;&amp; ! $_wp_switched  ) {
</ins><span class="cx">                 if ( defined( 'BLOGUPLOADDIR' ) )
</span><span class="cx">                         $dir = untrailingslashit(BLOGUPLOADDIR);
</span><span class="cx">                 $url = str_replace( UPLOADS, 'files', $url );
</span></span></pre></div>
<a id="trunkwpincludesmsblogsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-blogs.php (21484 => 21485)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-blogs.php        2012-08-09 13:17:14 UTC (rev 21484)
+++ trunk/wp-includes/ms-blogs.php        2012-08-09 16:28:15 UTC (rev 21485)
</span><span class="lines">@@ -446,72 +446,61 @@
</span><span class="cx">  * @since MU
</span><span class="cx">  *
</span><span class="cx">  * @param int $new_blog The id of the blog you want to switch to. Default: current blog
</span><del>- * @param bool $validate Whether to check if $new_blog exists before proceeding
</del><ins>+ * @param bool $deprecated Depecreated argument
</ins><span class="cx">  * @return bool        True on success, False if the validation failed
</span><span class="cx">  */
</span><del>-function switch_to_blog( $new_blog, $validate = false ) {
-        global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
</del><ins>+function switch_to_blog( $new_blog, $deprecated = null ) {
+        global $wpdb, $wp_roles;
</ins><span class="cx"> 
</span><del>-        if ( empty($new_blog) )
-                $new_blog = $blog_id;
</del><ins>+        if ( empty( $new_blog ) )
+                $new_blog = $GLOBALS['blog_id'];
</ins><span class="cx"> 
</span><del>-        if ( $validate &amp;&amp; ! get_blog_details( $new_blog ) )
-                return false;
</del><ins>+        $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
</ins><span class="cx"> 
</span><del>-        if ( empty($switched_stack) )
-                $switched_stack = array();
-
-        $switched_stack[] = $blog_id;
-
</del><span class="cx">         /* If we're switching to the same blog id that we're on,
</span><span class="cx">         * set the right vars, do the associated actions, but skip
</span><span class="cx">         * the extra unnecessary work */
</span><del>-        if ( $blog_id == $new_blog ) {
-                do_action( 'switch_blog', $blog_id, $blog_id );
-                $switched = true;
</del><ins>+        if ( $new_blog == $GLOBALS['blog_id'] ) {
+                do_action( 'switch_blog', $new_blog, $new_blog );
+                $GLOBALS['_wp_switched'] = true;
</ins><span class="cx">                 return true;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        $wpdb-&gt;set_blog_id($new_blog);
-        $table_prefix = $wpdb-&gt;prefix;
-        $prev_blog_id = $blog_id;
-        $blog_id = $new_blog;
</del><ins>+        $wpdb-&gt;set_blog_id( $new_blog );
+        $GLOBALS['table_prefix'] = $wpdb-&gt;prefix;
+        $prev_blog_id = $GLOBALS['blog_id'];
+        $GLOBALS['blog_id'] = $new_blog;
</ins><span class="cx"> 
</span><del>-        if ( is_object( $wp_roles ) ) {
-                $wpdb-&gt;suppress_errors();
-                if ( method_exists( $wp_roles ,'_init' ) )
-                        $wp_roles-&gt;_init();
-                elseif ( method_exists( $wp_roles, '__construct' ) )
-                        $wp_roles-&gt;__construct();
-                $wpdb-&gt;suppress_errors( false );
-        }
-
-        if ( did_action('init') ) {
</del><ins>+        if ( did_action( 'init' ) ) {
+                $wp_roles-&gt;reinit();
</ins><span class="cx">                 $current_user = wp_get_current_user();
</span><del>-                if ( is_object( $current_user ) )
-                        $current_user-&gt;for_blog( $blog_id );
</del><ins>+                $current_user-&gt;for_blog( $new_blog );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
</span><del>-                wp_cache_switch_to_blog( $blog_id );
</del><ins>+                wp_cache_switch_to_blog( $new_blog );
</ins><span class="cx">         } else {
</span><ins>+                global $wp_object_cache;
+
</ins><span class="cx">                 if ( is_object( $wp_object_cache ) &amp;&amp; isset( $wp_object_cache-&gt;global_groups ) )
</span><span class="cx">                         $global_groups = $wp_object_cache-&gt;global_groups;
</span><span class="cx">                 else
</span><span class="cx">                         $global_groups = false;
</span><span class="cx">         
</span><span class="cx">                 wp_cache_init();
</span><del>-                if ( function_exists('wp_cache_add_global_groups') ) {
</del><ins>+
+                if ( function_exists( 'wp_cache_add_global_groups' ) ) {
</ins><span class="cx">                         if ( is_array( $global_groups ) )
</span><span class="cx">                                 wp_cache_add_global_groups( $global_groups );
</span><span class="cx">                         else
</span><span class="cx">                                 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
</span><del>-                        wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
</del><ins>+                        wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        do_action('switch_blog', $blog_id, $prev_blog_id);
-        $switched = true;
</del><ins>+        do_action( 'switch_blog', $new_blog, $prev_blog_id );
+        $GLOBALS['_wp_switched'] = true;
+
</ins><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -524,64 +513,57 @@
</span><span class="cx">  * @return bool True on success, False if we're already on the current blog
</span><span class="cx">  */
</span><span class="cx"> function restore_current_blog() {
</span><del>-        global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
</del><ins>+        global $wpdb, $wp_roles;
</ins><span class="cx"> 
</span><del>-        if ( !$switched )
</del><ins>+        if ( ! $GLOBALS['_wp_switched'] )
</ins><span class="cx">                 return false;
</span><span class="cx"> 
</span><del>-        if ( !is_array( $switched_stack ) )
-                return false;
</del><ins>+        $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
</ins><span class="cx"> 
</span><del>-        $blog = array_pop( $switched_stack );
-        if ( $blog_id == $blog ) {
</del><ins>+        if ( $GLOBALS['blog_id'] == $blog ) {
</ins><span class="cx">                 do_action( 'switch_blog', $blog, $blog );
</span><del>-                /* If we still have items in the switched stack, consider ourselves still 'switched' */
-                $switched = ( is_array( $switched_stack ) &amp;&amp; count( $switched_stack ) &gt; 0 );
</del><ins>+                // If we still have items in the switched stack, consider ourselves still 'switched'
+                $GLOBALS['_wp_switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
</ins><span class="cx">                 return true;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        $wpdb-&gt;set_blog_id($blog);
-        $prev_blog_id = $blog_id;
-        $blog_id = $blog;
-        $table_prefix = $wpdb-&gt;prefix;
</del><ins>+        $wpdb-&gt;set_blog_id( $blog );
+        $prev_blog_id = $GLOBALS['blog_id'];
+        $GLOBALS['blog_id'] = $blog;
+        $GLOBALS['table_prefix'] = $wpdb-&gt;prefix;
</ins><span class="cx"> 
</span><del>-        if ( is_object( $wp_roles ) ) {
-                $wpdb-&gt;suppress_errors();
-                if ( method_exists( $wp_roles ,'_init' ) )
-                        $wp_roles-&gt;_init();
-                elseif ( method_exists( $wp_roles, '__construct' ) )
-                        $wp_roles-&gt;__construct();
-                $wpdb-&gt;suppress_errors( false );
-        }
-
-        if ( did_action('init') ) {
</del><ins>+        if ( did_action( 'init' ) ) {
+                $wp_roles-&gt;reinit();
</ins><span class="cx">                 $current_user = wp_get_current_user();
</span><del>-                if ( is_object( $current_user ) )
-                        $current_user-&gt;for_blog( $blog_id );
</del><ins>+                $current_user-&gt;for_blog( $blog );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
</span><del>-                wp_cache_switch_to_blog( $blog_id );
</del><ins>+                wp_cache_switch_to_blog( $blog );
</ins><span class="cx">         } else {
</span><ins>+                global $wp_object_cache;
+
</ins><span class="cx">                 if ( is_object( $wp_object_cache ) &amp;&amp; isset( $wp_object_cache-&gt;global_groups ) )
</span><span class="cx">                         $global_groups = $wp_object_cache-&gt;global_groups;
</span><span class="cx">                 else
</span><span class="cx">                         $global_groups = false;
</span><span class="cx">         
</span><span class="cx">                 wp_cache_init();
</span><del>-                if ( function_exists('wp_cache_add_global_groups') ) {
</del><ins>+
+                if ( function_exists( 'wp_cache_add_global_groups' ) ) {
</ins><span class="cx">                         if ( is_array( $global_groups ) )
</span><span class="cx">                                 wp_cache_add_global_groups( $global_groups );
</span><span class="cx">                         else
</span><span class="cx">                                 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
</span><del>-                        wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
</del><ins>+                        wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        do_action('switch_blog', $blog_id, $prev_blog_id);
</del><ins>+        do_action( 'switch_blog', $blog, $prev_blog_id );
</ins><span class="cx"> 
</span><del>-        /* If we still have items in the switched stack, consider ourselves still 'switched' */
-        $switched = ( is_array( $switched_stack ) &amp;&amp; count( $switched_stack ) &gt; 0 );
</del><ins>+        // If we still have items in the switched stack, consider ourselves still 'switched'
+        $GLOBALS['_wp_switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
+
</ins><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesmssettingsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-settings.php (21484 => 21485)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-settings.php        2012-08-09 13:17:14 UTC (rev 21484)
+++ trunk/wp-includes/ms-settings.php        2012-08-09 16:28:15 UTC (rev 21485)
</span><span class="lines">@@ -126,6 +126,8 @@
</span><span class="cx"> $wpdb-&gt;set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
</span><span class="cx"> $wpdb-&gt;set_blog_id( $current_blog-&gt;blog_id, $current_blog-&gt;site_id );
</span><span class="cx"> $table_prefix = $wpdb-&gt;get_blog_prefix();
</span><ins>+$_wp_switched_stack = array();
+$_wp_switched = false;
</ins><span class="cx"> 
</span><span class="cx"> // need to init cache again after blog_id is set
</span><span class="cx"> wp_start_object_cache();
</span></span></pre></div>
<a id="trunkwpsettingsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-settings.php (21484 => 21485)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-settings.php        2012-08-09 13:17:14 UTC (rev 21484)
+++ trunk/wp-settings.php        2012-08-09 16:28:15 UTC (rev 21485)
</span><span class="lines">@@ -252,6 +252,13 @@
</span><span class="cx">  */
</span><span class="cx"> $GLOBALS['wp_widget_factory'] = new WP_Widget_Factory();
</span><span class="cx"> 
</span><ins>+/**
+ * WordPress User Roles
+ * @global object $wp_roles
+ * @since 2.0.0
+ */
+$GLOBALS['wp_roles'] = new WP_Roles();
+
</ins><span class="cx"> do_action( 'setup_theme' );
</span><span class="cx"> 
</span><span class="cx"> // Define the template related constants.
</span></span></pre>
</div>
</div>

</body>
</html>