<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[12796] trunk: Introduce WP_User::for_blog() and current_user_can_for_blog(
 ) to avoid calls to WP_User::_init_caps().</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12796">12796</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-01-22 17:27:28 +0000 (Fri, 22 Jan 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduce WP_User::for_blog() and current_user_can_for_blog() to avoid calls to WP_User::_init_caps(). fixes <a href="http://trac.wordpress.org/ticket/11781">#11781</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminmseditphp">trunk/wp-admin/ms-edit.php</a></li>
<li><a href="#trunkwpincludescapabilitiesphp">trunk/wp-includes/capabilities.php</a></li>
<li><a href="#trunkwpincludesmsfunctionsphp">trunk/wp-includes/ms-functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminmseditphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/ms-edit.php (12795 => 12796)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/ms-edit.php        2010-01-22 16:06:31 UTC (rev 12795)
+++ trunk/wp-admin/ms-edit.php        2010-01-22 17:27:28 UTC (rev 12796)
</span><span class="lines">@@ -222,6 +222,7 @@
</span><span class="cx"> 
</span><span class="cx">                 // get blog prefix
</span><span class="cx">                 $blog_prefix = $wpdb-&gt;get_blog_prefix( $id );
</span><ins>+
</ins><span class="cx">                 // user roles
</span><span class="cx">                 if ( is_array( $_POST[ 'role' ] ) == true ) {
</span><span class="cx">                         $newroles = $_POST[ 'role' ];
</span><span class="lines">@@ -230,8 +231,7 @@
</span><span class="cx">                                 $user = new WP_User($userid);
</span><span class="cx">                                 if ( ! $user )
</span><span class="cx">                                         continue;
</span><del>-                                // Hack. Init user caps for given blog.
-                                $user-&gt;_init_caps($blog_prefix . 'capabilities');
</del><ins>+                                $user-&gt;for_blog($id);
</ins><span class="cx">                                 $user-&gt;set_role($role);
</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 (12795 => 12796)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/capabilities.php        2010-01-22 16:06:31 UTC (rev 12795)
+++ trunk/wp-includes/capabilities.php        2010-01-22 17:27:28 UTC (rev 12796)
</span><span class="lines">@@ -755,6 +755,23 @@
</span><span class="cx">                 return 'level_' . $level;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        /**
+         * Set the blog to operate on. Defaults to the current blog.
+         *
+         * @since 3.0
+         *
+         * @param int $blog_id Optional Blog ID, defaults to current blog.
+         */
+        function for_blog( $blog_id = '' ) {
+                global $wpdb;
+                if ( !empty($blog_id) ) {
+                        $cap_key = $wpdb-&gt;get_blog_prefix( $blog_id );
+                        $cap_key. 'capabilities';
+                } else {
+                        $cap_key = '';
+                }
+                $this-&gt;_init_caps($cap_key);
+        }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -1018,6 +1035,36 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Whether current user has a capability or role for a given blog.
+ *
+ * @since 2.0.0
+ *
+ * @param int $blog_id Blog ID
+ * @param string $capability Capability or role name.
+ * @return bool
+ */
+function current_user_can_for_blog( $blog_id, $capability ) {
+        $current_user = wp_get_current_user();
+
+    if ( is_multisite() &amp;&amp; is_super_admin() )
+                return true;
+
+        if ( empty( $current_user ) )
+                return false;
+
+        // 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 );
+
+        $args = array_slice( func_get_args(), 2 );
+        $args = array_merge( array( $capability ), $args );
+
+        return call_user_func_array( array( &amp;$user, 'has_cap' ), $args );
+}
+
+/**
</ins><span class="cx">  * Whether author of supplied post has capability or role.
</span><span class="cx">  *
</span><span class="cx">  * @since 2.9.0
</span></span></pre></div>
<a id="trunkwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-functions.php (12795 => 12796)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-functions.php        2010-01-22 16:06:31 UTC (rev 12795)
+++ trunk/wp-includes/ms-functions.php        2010-01-22 17:27:28 UTC (rev 12796)
</span><span class="lines">@@ -306,7 +306,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( is_object( $current_user ) )
</span><del>-                $current_user-&gt;_init_caps();
</del><ins>+                $current_user-&gt;for_blog( $blog_id );
</ins><span class="cx"> 
</span><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="lines">@@ -359,7 +359,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( is_object( $current_user ) )
</span><del>-                $current_user-&gt;_init_caps();
</del><ins>+                $current_user-&gt;for_blog( $blog_id );
</ins><span class="cx"> 
</span><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></pre>
</div>
</div>

</body>
</html>