<!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>[34965] trunk: Introduce the ability to filter the Users admin listing screen by users with no role, if such users exist.</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/34965">34965</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/34965","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-10-08 22:06:46 +0000 (Thu, 08 Oct 2015)</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'>Introduce the ability to filter the Users admin listing screen by users with no role, if such users exist.

Fixes <a href="https://core.trac.wordpress.org/ticket/22993">#22993</a>
Props spmlucas, johnbillion</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpuserslisttablephp">trunk/src/wp-admin/includes/class-wp-users-list-table.php</a></li>
<li><a href="#trunksrcwpincludesuserfunctionsphp">trunk/src/wp-includes/user-functions.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsusercountUsersphp">trunk/tests/phpunit/tests/user/countUsers.php</a></li>
<li><a href="#trunktestsphpunittestsuserwpGetUsersWithNoRolephp">trunk/tests/phpunit/tests/user/wpGetUsersWithNoRole.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpuserslisttablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-users-list-table.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-users-list-table.php 2015-10-08 22:06:42 UTC (rev 34964)
+++ trunk/src/wp-admin/includes/class-wp-users-list-table.php   2015-10-08 22:06:46 UTC (rev 34965)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -87,13 +87,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $paged = $this->get_pagenum();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args = array(
-                       'number' => $users_per_page,
-                       'offset' => ( $paged-1 ) * $users_per_page,
-                       'role' => $role,
-                       'search' => $usersearch,
-                       'fields' => 'all_with_meta'
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'none' === $role ) {
+                       $args = array(
+                               'number' => $users_per_page,
+                               'offset' => ( $paged-1 ) * $users_per_page,
+                               'include' => wp_get_users_with_no_role(),
+                               'search' => $usersearch,
+                               'fields' => 'all_with_meta'
+                       );
+               } else {
+                       $args = array(
+                               'number' => $users_per_page,
+                               'offset' => ( $paged-1 ) * $users_per_page,
+                               'role' => $role,
+                               'search' => $usersearch,
+                               'fields' => 'all_with_meta'
+                       );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( '' !== $args['search'] )
</span><span class="cx" style="display: block; padding: 0 10px">                        $args['search'] = '*' . $args['search'] . '*';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -166,6 +176,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $url = 'users.php';
</span><span class="cx" style="display: block; padding: 0 10px">                        $users_of_blog = count_users();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $total_users = $users_of_blog['total_users'];
</span><span class="cx" style="display: block; padding: 0 10px">                $avail_roles =& $users_of_blog['avail_roles'];
</span><span class="cx" style="display: block; padding: 0 10px">                unset($users_of_blog);
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -189,6 +200,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
</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">+                if ( ! empty( $avail_roles['none' ] ) ) {
+
+                       $class = '';
+
+                       if ( 'none' === $role ) {
+                               $class = ' class="current"';
+                       }
+
+                       $name = __( 'No role' );
+                       /* translators: User role name with count */
+                       $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
+                       $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$class>$name</a>";
+
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $role_links;
</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="trunksrcwpincludesuserfunctionsphp"></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/user-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user-functions.php  2015-10-08 22:06:42 UTC (rev 34964)
+++ trunk/src/wp-includes/user-functions.php    2015-10-08 22:06:46 UTC (rev 34965)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -735,6 +735,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Using $strategy = 'memory' this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.4.0 The number of users with no role is now included in the `none` element.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global wpdb $wpdb
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -775,10 +776,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Get the meta_value index from the end of the result set.
</span><span class="cx" style="display: block; padding: 0 10px">                $total_users = (int) $row[$col];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $role_counts['none'] = ( $total_users - array_sum( $role_counts ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result['total_users'] = $total_users;
</span><span class="cx" style="display: block; padding: 0 10px">                $result['avail_roles'] =& $role_counts;
</span><span class="cx" style="display: block; padding: 0 10px">        } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $avail_roles = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $avail_roles = array(
+                       'none' => 0,
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -786,6 +791,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $b_roles = maybe_unserialize($caps_meta);
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! is_array( $b_roles ) )
</span><span class="cx" style="display: block; padding: 0 10px">                                continue;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( empty( $b_roles ) ) {
+                               $avail_roles['none']++;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         foreach ( $b_roles as $b_role => $val ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( isset($avail_roles[$b_role]) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $avail_roles[$b_role]++;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -799,6 +807,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $result['avail_roles'] =& $avail_roles;
</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">+        if ( is_multisite() ) {
+               $result['avail_roles']['none'] = 0;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         return $result;
</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">@@ -2231,3 +2243,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
</span><span class="cx" style="display: block; padding: 0 10px">        $manager->destroy_all();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Get the user IDs of all users with no role on this site.
+ *
+ * This function returns an empty array when used on Multisite.
+ *
+ * @since 4.4.0
+ *
+ * @return array Array of user IDs.
+ */
+function wp_get_users_with_no_role() {
+       global $wpdb;
+
+       if ( is_multisite() ) {
+               return array();
+       }
+
+       $prefix = $wpdb->get_blog_prefix();
+       $regex  = implode( '|', wp_roles()->get_names() );
+       $regex  = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
+       $users  = $wpdb->get_col( $wpdb->prepare( "
+               SELECT user_id
+               FROM $wpdb->usermeta
+               WHERE meta_key = '{$prefix}capabilities'
+               AND meta_value NOT REGEXP %s
+       ", $regex ) );
+
+       return $users;
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsusercountUsersphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/user/countUsers.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/countUsers.php                             (rev 0)
+++ trunk/tests/phpunit/tests/user/countUsers.php       2015-10-08 22:06:46 UTC (rev 34965)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,154 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group user
+ */
+class Tests_User_CountUsers extends WP_UnitTestCase {
+
+       /**
+        * @ticket 22993
+        *
+        * @dataProvider data_count_users_strategies
+        */
+       public function test_count_users_is_accurate( $strategy ) {
+
+               if ( is_multisite() ) {
+                       $this->markTestSkipped( 'Test does not run on multisite' );
+               }
+
+               // Setup users
+               $admin = $this->factory->user->create( array(
+                       'role' => 'administrator',
+               ) );
+               $editor = $this->factory->user->create( array(
+                       'role' => 'editor',
+               ) );
+               $author = $this->factory->user->create( array(
+                       'role' => 'author',
+               ) );
+               $contributor = $this->factory->user->create( array(
+                       'role' => 'contributor',
+               ) );
+               $subscriber = $this->factory->user->create( array(
+                       'role' => 'subscriber',
+               ) );
+               $none = $this->factory->user->create( array(
+                       'role' => '',
+               ) );
+               $nobody = $this->factory->user->create( array(
+                       'role' => '',
+               ) );
+
+               // Test user counts
+               $count = count_users( $strategy );
+
+               $this->assertEquals( 8, $count['total_users'] );
+               $this->assertEquals( array(
+                       'administrator' => 2,
+                       'editor'        => 1,
+                       'author'        => 1,
+                       'contributor'   => 1,
+                       'subscriber'    => 1,
+                       'none'          => 2,
+               ), $count['avail_roles'] );
+
+       }
+
+       /**
+        * @ticket 22993
+        * @group multisite
+        *
+        * @dataProvider data_count_users_strategies
+        */
+       public function test_count_users_multisite_is_accurate( $strategy ) {
+
+               if ( ! is_multisite() ) {
+                       $this->markTestSkipped( 'Test requires multisite' );
+               }
+
+               // Setup users
+               $admin = $this->factory->user->create( array(
+                       'role' => 'administrator',
+               ) );
+               $editor = $this->factory->user->create( array(
+                       'role' => 'editor',
+               ) );
+               $author = $this->factory->user->create( array(
+                       'role' => 'author',
+               ) );
+               $contributor = $this->factory->user->create( array(
+                       'role' => 'contributor',
+               ) );
+               $subscriber = $this->factory->user->create( array(
+                       'role' => 'subscriber',
+               ) );
+               $none = $this->factory->user->create( array(
+                       'role' => '',
+               ) );
+               $nobody = $this->factory->user->create( array(
+                       'role' => '',
+               ) );
+
+               // Setup blogs
+               $blog_1 = (int) $this->factory->blog->create( array(
+                       'user_id' => $editor,
+               ) );
+               $blog_2 = (int) $this->factory->blog->create( array(
+                       'user_id' => $author,
+               ) );
+
+               // Add users to blogs
+               add_user_to_blog( $blog_1, $subscriber, 'editor' );
+               add_user_to_blog( $blog_2, $none, 'contributor' );
+
+               // Test users counts on root site
+               $count = count_users( $strategy );
+
+               $this->assertEquals( 8, $count['total_users'] );
+               $this->assertEquals( array(
+                       'administrator' => 2,
+                       'editor'        => 1,
+                       'author'        => 1,
+                       'contributor'   => 1,
+                       'subscriber'    => 1,
+                       'none'          => 0,
+               ), $count['avail_roles'] );
+
+               // Test users counts on blog 1
+               switch_to_blog( $blog_1 );
+               $count = count_users( $strategy );
+               restore_current_blog();
+
+               $this->assertEquals( 2, $count['total_users'] );
+               $this->assertEquals( array(
+                       'administrator' => 1,
+                       'editor'        => 1,
+                       'none'          => 0,
+               ), $count['avail_roles'] );
+
+               // Test users counts on blog 2
+               switch_to_blog( $blog_2 );
+               $count = count_users( $strategy );
+               restore_current_blog();
+
+               $this->assertEquals( 2, $count['total_users'] );
+               $this->assertEquals( array(
+                       'administrator' => 1,
+                       'contributor'   => 1,
+                       'none'          => 0,
+               ), $count['avail_roles'] );
+
+       }
+
+       function data_count_users_strategies() {
+               return array(
+                       array(
+                               'time',
+                       ),
+                       array(
+                               'memory',
+                       ),
+               );
+       }
+
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsuserwpGetUsersWithNoRolephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/user/wpGetUsersWithNoRole.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/wpGetUsersWithNoRole.php                           (rev 0)
+++ trunk/tests/phpunit/tests/user/wpGetUsersWithNoRole.php     2015-10-08 22:06:46 UTC (rev 34965)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,84 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group user
+ */
+class Tests_User_GetUsersWithNoRole extends WP_UnitTestCase {
+
+       /**
+        * @ticket 22993
+        */
+       public function test_get_users_with_no_role_is_accurate() {
+
+               if ( is_multisite() ) {
+                       $this->markTestSkipped( 'Test does not run on multisite' );
+               }
+
+               // Setup users
+               $admin = $this->factory->user->create( array(
+                       'role' => 'administrator',
+               ) );
+               $editor = $this->factory->user->create( array(
+                       'role' => 'editor',
+               ) );
+               $nobody = $this->factory->user->create( array(
+                       'role' => '',
+               ) );
+               $nobody_else = $this->factory->user->create( array(
+                       'role' => '',
+               ) );
+
+               // Test users
+               $users = wp_get_users_with_no_role();
+
+               $this->assertEquals( array(
+                       $nobody,
+                       $nobody_else,
+               ), $users );
+
+       }
+
+       /**
+        * @ticket 22993
+        * @group multisite
+        */
+       public function test_get_users_with_no_role_multisite_is_accurate() {
+
+               if ( ! is_multisite() ) {
+                       $this->markTestSkipped( 'Test requires multisite' );
+               }
+
+               // Setup users
+               $admin = $this->factory->user->create( array(
+                       'role' => 'administrator',
+               ) );
+               $editor = $this->factory->user->create( array(
+                       'role' => 'editor',
+               ) );
+               $nobody = $this->factory->user->create( array(
+                       'role' => '',
+               ) );
+
+               // Setup blogs
+               $blog_1 = (int) $this->factory->blog->create( array(
+                       'user_id' => $editor,
+               ) );
+
+               // Add users to blogs
+               add_user_to_blog( $blog_1, $editor, 'editor' );
+
+               // Test users on root site
+               $users = wp_get_users_with_no_role();
+               $this->assertSame( array(), $users );
+
+               // Test users counts on blog 1
+               switch_to_blog( $blog_1 );
+               $users = wp_get_users_with_no_role();
+               restore_current_blog();
+
+               // Test users on root site
+               $this->assertSame( array(), $users );
+
+       }
+
+}
</ins></span></pre>
</div>
</div>

</body>
</html>