<!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>[39082] trunk: Roles/Capabilities: Add a new `wp_roles_init` filter.</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/39082">39082</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/39082","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>pento</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-02 00:30:29 +0000 (Wed, 02 Nov 2016)</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'>Roles/Capabilities: Add a new `wp_roles_init` filter.
Historically, it's been difficult to extend user roles, but reasonable to work around by waiting until after `init` has fired, to add custom roles and capabilities. With the addition of Locale Switching, Core now potentially loads roles before `init` has fired, leaving a window where custom roles and capabilities are not handled.
The new filter allows plugins to add their own custom roles whenever they're initialised (on page load, or when switching sites, for example), so that they can always be obeyed.
`WP_Roles` has also been tidied up a little bit, to remove duplicate code.
Props johnjamesjacoby, pento.
Fixes <a href="https://core.trac.wordpress.org/ticket/23016">#23016</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="#trunktestsphpunittestsusercapabilitiesphp">trunk/tests/phpunit/tests/user/capabilities.php</a></li>
<li><a href="#trunktestsphpunittestsusermultisitephp">trunk/tests/phpunit/tests/user/multisite.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 2016-11-01 21:23:42 UTC (rev 39081)
+++ trunk/src/wp-includes/class-wp-roles.php 2016-11-02 00:30:29 UTC (rev 39082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -127,6 +127,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->role_names[$role] = $this->roles[$role]['name'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * After the roles have been initialized, allow plugins to add their own roles.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_Roles A reference to the WP_Roles object.
+ */
+ do_action( 'wp_roles_init', $this );
</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">@@ -136,28 +145,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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @deprecated 4.7.0 Use new WP_Roles()
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access public
</span><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">- global $wpdb;
-
- // There is no need to reinit if using the wp_user_roles global.
- if ( ! $this->use_db ) {
- return;
- }
-
- // Duplicated from _init() to avoid an extra function call.
- $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
- $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'];
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ __deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' );
+ $this->_init();
</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></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 2016-11-01 21:23:42 UTC (rev 39081)
+++ trunk/src/wp-includes/ms-blogs.php 2016-11-02 00:30:29 UTC (rev 39082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -766,7 +766,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;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wpdb, $wp_roles;
</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">@@ -822,7 +822,7 @@
</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"> if ( did_action( 'init' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_roles()->reinit();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_roles = new WP_Roles();
</ins><span class="cx" style="display: block; padding: 0 10px"> $current_user = wp_get_current_user();
</span><span class="cx" style="display: block; padding: 0 10px"> $current_user->for_blog( $new_blog );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -850,7 +850,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;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wpdb, $wp_roles;
</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">@@ -896,7 +896,7 @@
</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"> if ( did_action( 'init' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_roles()->reinit();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_roles = new WP_Roles();
</ins><span class="cx" style="display: block; padding: 0 10px"> $current_user = wp_get_current_user();
</span><span class="cx" style="display: block; padding: 0 10px"> $current_user->for_blog( $blog );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</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 2016-11-01 21:23:42 UTC (rev 39081)
+++ trunk/tests/phpunit/tests/user/capabilities.php 2016-11-02 00:30:29 UTC (rev 39082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -41,8 +41,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // this will flush everything and reload it from the db
</span><span class="cx" style="display: block; padding: 0 10px"> unset($GLOBALS['wp_user_roles']);
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_roles;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( is_object( $wp_roles ) )
- $wp_roles->_init();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_roles = new WP_Roles();
</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"> function _meta_yes_you_can( $can, $key, $post_id, $user_id, $cap, $caps ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1626,4 +1625,33 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( current_user_can( 'do_not_allow' ), "Non-logged-in user should not have the do_not_allow capability" );
</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">+ protected $_role_test_wp_roles_role;
+ /**
+ * @ticket 23016
+ */
+ public function test_wp_roles_init_action() {
+ $this->_role_test_wp_roles_init = array(
+ 'role' => 'test_wp_roles_init',
+ 'info' => array(
+ 'name' => 'Test WP Roles Init',
+ 'capabilities' => array( 'testing_magic' => true ),
+ ),
+ );
+ add_action( 'wp_roles_init', array( $this, '_hook_wp_roles_init' ), 10, 1 );
+
+ $wp_roles = new WP_Roles();
+
+ remove_action( 'wp_roles_init', array( $this, '_hook_wp_roles_init' ) );
+
+ $expected = new WP_Role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['capabilities'] );
+
+ $role = $wp_roles->get_role( $this->_role_test_wp_roles_init['role'] );
+
+ $this->assertEquals( $expected, $role );
+ $this->assertContains( $this->_role_test_wp_roles_init['info']['name'], $wp_roles->role_names );
+ }
+
+ public function _hook_wp_roles_init( $wp_roles ) {
+ $wp_roles->add_role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['name'], $this->_role_test_wp_roles_init['info']['capabilities'] );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsusermultisitephp"></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/multisite.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/multisite.php 2016-11-01 21:23:42 UTC (rev 39081)
+++ trunk/tests/phpunit/tests/user/multisite.php 2016-11-02 00:30:29 UTC (rev 39082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -396,6 +396,34 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertWPError( $result );
</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 23016
+ */
+ public function test_wp_roles_global_is_reset() {
+ global $wp_roles;
+ $role = 'test_global_is_reset';
+ $role_name = 'Test Global Is Reset';
+ $blog_id = self::factory()->blog->create();
+
+ $wp_roles->add_role( $role, $role_name, array() );
+
+ $this->assertNotEmpty( $wp_roles->get_role( $role ) );
+
+ switch_to_blog( $blog_id );
+
+ $this->assertEmpty( $wp_roles->get_role( $role ) );
+
+ $wp_roles->add_role( $role, $role_name, array() );
+
+ $this->assertNotEmpty( $wp_roles->get_role( $role ) );
+
+ restore_current_blog();
+
+ $this->assertNotEmpty( $wp_roles->get_role( $role ) );
+
+ $wp_roles->remove_role( $role );
+ }
</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"> endif ;
</span></span></pre>
</div>
</div>
</body>
</html>