<!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>[41625] trunk: Multisite: Improve initializing available roles when switch sites.</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41625">41625</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41625","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-09-27 21:43:59 +0000 (Wed, 27 Sep 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Multisite: Improve initializing available roles when switch sites.

Switching the available roles and the current user's capabilities no longer happens in `switch_to_blog()` and `restore_current_blog()`, instead it has been moved to a new function `wp_switch_roles_and_user()` which is hooked into the site switching process. This allows to improve performance by temporarily unhooking the function when roles and capabilities do not need to be switched.

This change ensures that switching available roles now works closer to switching user capabilities, particularly the changes in <a href="https://core.trac.wordpress.org/changeset/41624">[41624]</a>. A new `WP_Roles::for_site( $site_id )` method has been introduced, and the `WP_Roles::_init()` method has been deprecated. It is furthermore possible to retrieve the site ID for which the available roles are currently initialized through a new `WP_Roles::get_site_id()`.

Props johnjamesjacoby, flixos90.
Fixes <a href="https://core.trac.wordpress.org/ticket/38645">#38645</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswprolesphp">trunk/src/wp-includes/class-wp-roles.php</a></li>
<li><a href="#trunksrcwpincludesmsblogsphp">trunk/src/wp-includes/ms-blogs.php</a></li>
<li><a href="#trunksrcwpincludesmsdefaultfiltersphp">trunk/src/wp-includes/ms-default-filters.php</a></li>
<li><a href="#trunktestsphpunittestsusercapabilitiesphp">trunk/tests/phpunit/tests/user/capabilities.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswprolesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-roles.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-roles.php  2017-09-27 21:09:11 UTC (rev 41624)
+++ trunk/src/wp-includes/class-wp-roles.php    2017-09-27 21:43:59 UTC (rev 41625)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -65,12 +65,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $use_db = true;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * The site ID the roles are initialized for.
+        *
+        * @since 4.9.0
+        * @var int
+        */
+       protected $site_id = 0;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Constructor
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 4.9.0 The $site_id argument was added.
+        *
+        * @global array $wp_user_roles Used to set the 'roles' property value.
+        *
+        * @param int $site_id Site ID to initialize roles for. Default is the current site.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function __construct() {
-               $this->_init();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function __construct( $site_id = null ) {
+               global $wp_user_roles;
+
+               $this->use_db = empty( $wp_user_roles );
+
+               $this->for_site( $site_id );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -97,38 +114,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * be used and the role option will not be updated or used.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.1.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @global array $wp_user_roles Used to set the 'roles' property value.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @deprecated 4.9.0 Use WP_Roles::for_site()
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function _init() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                global $wp_user_roles, $wpdb;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         _deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_site()' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
-               if ( ! empty( $wp_user_roles ) ) {
-                       $this->roles = $wp_user_roles;
-                       $this->use_db = false;
-               } else {
-                       $this->roles = get_option( $this->role_key );
-               }
-
-               if ( empty( $this->roles ) )
-                       return;
-
-               $this->role_objects = array();
-               $this->role_names =  array();
-               foreach ( array_keys( $this->roles ) as $role ) {
-                       $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
-                       $this->role_names[$role] = $this->roles[$role]['name'];
-               }
-
-               /**
-                * After the roles have been initialized, allow plugins to add their own roles.
-                *
-                * @since 4.7.0
-                *
-                * @param WP_Roles $this A reference to the WP_Roles object.
-                */
-               do_action( 'wp_roles_init', $this );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->for_site();
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,11 +129,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * after switching wpdb to a new site ID.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.5.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @deprecated 4.7.0 Use new WP_Roles()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @deprecated 4.7.0 Use WP_Roles::for_site()
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function reinit() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                _deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' );
-               $this->_init();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         _deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_site()' );
+
+               $this->for_site();
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -270,4 +262,100 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function is_role( $role ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return isset( $this->role_names[$role] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Initializes all of the available roles.
+        *
+        * @since 4.9.0
+        */
+       public function init_roles() {
+               if ( empty( $this->roles ) ) {
+                       return;
+               }
+
+               $this->role_objects = array();
+               $this->role_names =  array();
+               foreach ( array_keys( $this->roles ) as $role ) {
+                       $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
+                       $this->role_names[ $role ] = $this->roles[ $role ]['name'];
+               }
+
+               /**
+                * After the roles have been initialized, allow plugins to add their own roles.
+                *
+                * @since 4.7.0
+                *
+                * @param WP_Roles $this A reference to the WP_Roles object.
+                */
+               do_action( 'wp_roles_init', $this );
+       }
+
+       /**
+        * Sets the site to operate on. Defaults to the current site.
+        *
+        * @since 4.9.0
+        *
+        * @global wpdb $wpdb WordPress database abstraction object.
+        *
+        * @param int $site_id Site ID to initialize roles for. Default is the current site.
+        */
+       public function for_site( $site_id = null ) {
+               global $wpdb;
+
+               if ( ! empty( $site_id ) ) {
+                       $this->site_id = absint( $site_id );
+               } else {
+                       $this->site_id = get_current_blog_id();
+               }
+
+               $this->role_key = $wpdb->get_blog_prefix( $this->site_id ) . 'user_roles';
+
+               if ( ! empty( $this->roles ) && ! $this->use_db ) {
+                       return;
+               }
+
+               $this->roles = $this->get_roles_data();
+
+               $this->init_roles();
+       }
+
+       /**
+        * Gets the ID of the site for which roles are currently initialized.
+        *
+        * @since 4.9.0
+        *
+        * @return int Site ID.
+        */
+       public function get_site_id() {
+               return $this->site_id;
+       }
+
+       /**
+        * Gets the available roles data.
+        *
+        * @since 4.9.0
+        *
+        * @global array $wp_user_roles Used to set the 'roles' property value.
+        *
+        * @return array Roles array.
+        */
+       protected function get_roles_data() {
+               global $wp_user_roles;
+
+               if ( ! empty( $wp_user_roles ) ) {
+                       return $wp_user_roles;
+               }
+
+               if ( is_multisite() && $this->site_id != get_current_blog_id() ) {
+                       remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
+
+                       $roles = get_blog_option( $this->site_id, $this->role_key, array() );
+
+                       add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
+
+                       return $roles;
+               }
+
+               return get_option( $this->role_key, array() );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesmsblogsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-blogs.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-blogs.php        2017-09-27 21:09:11 UTC (rev 41624)
+++ trunk/src/wp-includes/ms-blogs.php  2017-09-27 21:43:59 UTC (rev 41625)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -792,7 +792,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return true Always returns True.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function switch_to_blog( $new_blog, $deprecated = null ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        global $wpdb, $wp_roles;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wpdb;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $blog_id = get_current_blog_id();
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $new_blog ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -847,12 +847,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( did_action( 'init' ) ) {
-               $wp_roles = new WP_Roles();
-               $current_user = wp_get_current_user();
-               $current_user->for_site( $new_blog );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /** This filter is documented in wp-includes/ms-blogs.php */
</span><span class="cx" style="display: block; padding: 0 10px">        do_action( 'switch_blog', $new_blog, $prev_blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">        $GLOBALS['switched'] = true;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -876,7 +870,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return bool True on success, false if we're already on the current blog
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function restore_current_blog() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        global $wpdb, $wp_roles;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wpdb;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -921,12 +915,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( did_action( 'init' ) ) {
-               $wp_roles = new WP_Roles();
-               $current_user = wp_get_current_user();
-               $current_user->for_site( $blog );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /** This filter is documented in wp-includes/ms-blogs.php */
</span><span class="cx" style="display: block; padding: 0 10px">        do_action( 'switch_blog', $blog, $prev_blog_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -937,6 +925,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Switches the initialized roles and current user capabilities to another site.
+ *
+ * @since 4.9.0
+ *
+ * @param int $new_site_id New site ID.
+ * @param int $old_site_id Old site ID.
+ */
+function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
+       if ( $new_site_id == $old_site_id ) {
+               return;
+       }
+
+       if ( ! did_action( 'init' ) ) {
+               return;
+       }
+
+       wp_roles()->for_site( $new_site_id );
+       wp_get_current_user()->for_site( $new_site_id );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Determines if switch_to_blog() is in effect
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.5.0
</span></span></pre></div>
<a id="trunksrcwpincludesmsdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-default-filters.php      2017-09-27 21:09:11 UTC (rev 41624)
+++ trunk/src/wp-includes/ms-default-filters.php        2017-09-27 21:43:59 UTC (rev 41625)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -32,6 +32,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'network_user_new_created_user',   'wp_send_new_user_notifications' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'sanitize_user', 'strtolower' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Roles
+add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Blogs
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'wpmu_validate_blog_signup', 'signup_nonce_check' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wpmu_new_blog', 'wpmu_log_new_registrations', 10, 2 );
</span></span></pre></div>
<a id="trunktestsphpunittestsusercapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/user/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/capabilities.php   2017-09-27 21:09:11 UTC (rev 41624)
+++ trunk/tests/phpunit/tests/user/capabilities.php     2017-09-27 21:43:59 UTC (rev 41625)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1939,4 +1939,75 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 333, $user->get_site_id() );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 38645
+        * @group ms-required
+        */
+       function test_init_roles_for_different_site() {
+               global $wpdb;
+
+               $site_id = self::factory()->blog->create();
+
+               switch_to_blog( $site_id );
+
+               $role_name = 'uploader';
+               add_role( $role_name, 'Uploader', array(
+                       'read'         => true,
+                       'upload_files' => true,
+               ) );
+
+               restore_current_blog();
+
+               $wp_roles = wp_roles();
+               $wp_roles->for_site( $site_id );
+
+               $this->assertTrue( isset( $wp_roles->role_objects[ $role_name ] ) );
+       }
+
+       /**
+        * @ticket 38645
+        */
+       function test_get_roles_data() {
+               global $wpdb;
+
+               $custom_roles = array(
+                       'test_role' => array(
+                               'name'         => 'Test Role',
+                               'capabilities' => array(
+                                       'do_foo' => true,
+                                       'do_bar' => false,
+                               ),
+                       ),
+               );
+
+               // Test `WP_Roles::get_roles_data()` by manually setting the roles option.
+               update_option( $wpdb->get_blog_prefix( get_current_blog_id() ) . 'user_roles', $custom_roles );
+
+               $roles = new WP_Roles();
+               $this->assertSame( $custom_roles, $roles->roles );
+       }
+
+       /**
+        * @ticket 38645
+        */
+       function test_roles_get_site_id_default() {
+               $roles = new WP_Roles();
+               $this->assertSame( get_current_blog_id(), $roles->get_site_id() );
+       }
+
+       /**
+        * @ticket 38645
+        */
+       function test_roles_get_site_id() {
+               global $wpdb;
+
+               // Suppressing errors here allows to get around creating an actual site,
+               // which is unnecessary for this test.
+               $suppress = $wpdb->suppress_errors();
+               $roles = new WP_Roles( 333 );
+               $wpdb->suppress_errors( $suppress );
+
+               $this->assertSame( 333, $roles->get_site_id() );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>