<!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>[38715] trunk: Query: Add nicename and login params to user query.</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/38715">38715</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/38715","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-10-04 02:26:26 +0000 (Tue, 04 Oct 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'>Query: Add nicename and login params to user query.
New parameters are: `login`, `login__in`, `login__not_in`, `nicename`,
`nicename__in`, `nicename__not_in`.
`login__in` and `nicename__in` are also now valid values for the
'orderby' parameter.
Props ryanplas.
Fixes <a href="https://core.trac.wordpress.org/ticket/36624">#36624</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpuserqueryphp">trunk/src/wp-includes/class-wp-user-query.php</a></li>
<li><a href="#trunktestsphpunittestsuserqueryphp">trunk/tests/phpunit/tests/user/query.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpuserqueryphp"></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-user-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-user-query.php 2016-10-04 00:50:09 UTC (rev 38714)
+++ trunk/src/wp-includes/class-wp-user-query.php 2016-10-04 02:26:26 UTC (rev 38715)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -124,6 +124,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'fields' => 'all',
</span><span class="cx" style="display: block; padding: 0 10px"> 'who' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'has_published_posts' => null,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'nicename' => '',
+ 'nicename__in' => array(),
+ 'nicename__not_in' => array(),
+ 'login' => '',
+ 'login__in' => array(),
+ 'login__not_in' => array()
</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"> return wp_parse_args( $args, $defaults );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,6 +146,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0 Added 'paged', 'role__in', and 'role__not_in' parameters. The 'role' parameter was updated to
</span><span class="cx" style="display: block; padding: 0 10px"> * permit an array or comma-separated list of values. The 'number' parameter was updated to support
</span><span class="cx" style="display: block; padding: 0 10px"> * querying for all users with using -1.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0 Added 'nicename', 'nicename__in', 'nicename__not_in', 'login', 'login__in',
+ * and 'login__not_in' parameters.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @access public
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -173,12 +181,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * an array of values, or a multi-dimensional array with fields as
</span><span class="cx" style="display: block; padding: 0 10px"> * keys and orders ('ASC' or 'DESC') as values. Accepted values are
</span><span class="cx" style="display: block; padding: 0 10px"> * 'ID', 'display_name' (or 'name'), 'include', 'user_login'
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * (or 'login'), 'user_nicename' (or 'nicename'), 'user_email'
- * (or 'email'), 'user_url' (or 'url'), 'user_registered'
- * or 'registered'), 'post_count', 'meta_value', 'meta_value_num',
- * the value of `$meta_key`, or an array key of `$meta_query`. To use
- * 'meta_value' or 'meta_value_num', `$meta_key` must be also be
- * defined. Default 'user_login'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * (or 'login'), 'login__in', 'user_nicename' (or 'nicename'),
+ * 'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'),
+ * 'user_registered' (or 'registered'), 'post_count', 'meta_value',
+ * 'meta_value_num', the value of `$meta_key`, or an array key of
+ * `$meta_query`. To use 'meta_value' or 'meta_value_num', `$meta_key`
+ * must be also be defined. Default 'user_login'.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @type string $order Designates ascending or descending order of users. Order values
</span><span class="cx" style="display: block; padding: 0 10px"> * passed as part of an `$orderby` array take precedence over this
</span><span class="cx" style="display: block; padding: 0 10px"> * parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -203,6 +211,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @type bool|array $has_published_posts Pass an array of post types to filter results to users who have
</span><span class="cx" style="display: block; padding: 0 10px"> * published posts in those post types. `true` is an alias for all
</span><span class="cx" style="display: block; padding: 0 10px"> * public post types.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type string $nicename The user nicename. Default empty.
+ * @type array $nicename__in An array of nicenames to include. Users matching one of these
+ * nicenames will be included in results. Default empty array.
+ * @type array $nicename__not_in An array of nicenames to exclude. Users matching one of these
+ * nicenames will not be included in results. Default empty array.
+ * @type string $login The user login. Default empty.
+ * @type array $login__in An array of logins to include. Users matching one of these
+ * logins will be included in results. Default empty array.
+ * @type array $login__not_in An array of logins to exclude. Users matching one of these
+ * logins will not be included in results. Default empty array.
</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"> public function prepare_query( $query = array() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -276,6 +294,40 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->query_where .= " AND {$this->db->users}.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )";
</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">+ // nicename
+ if ( '' !== $qv['nicename']) {
+ $this->query_where .= $this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] );
+ }
+
+ if ( ! empty( $qv['nicename__in'] ) ) {
+ $sanitized_nicename__in = array_map( 'esc_sql', $qv['nicename__in'] );
+ $nicename__in = implode( "','", $sanitized_nicename__in );
+ $this->query_where .= " AND user_nicename IN ( '$nicename__in' )";
+ }
+
+ if ( ! empty( $qv['nicename__not_in'] ) ) {
+ $sanitized_nicename__not_in = array_map( 'esc_sql', $qv['nicename__not_in'] );
+ $nicename__not_in = implode( "','", $sanitized_nicename__not_in );
+ $this->query_where .= " AND user_nicename NOT IN ( '$nicename__not_in' )";
+ }
+
+ // login
+ if ( '' !== $qv['login']) {
+ $this->query_where .= $this->db->prepare( ' AND user_login = %s', $qv['login'] );
+ }
+
+ if ( ! empty( $qv['login__in'] ) ) {
+ $sanitized_login__in = array_map( 'esc_sql', $qv['login__in'] );
+ $login__in = implode( "','", $sanitized_login__in );
+ $this->query_where .= " AND user_login IN ( '$login__in' )";
+ }
+
+ if ( ! empty( $qv['login__not_in'] ) ) {
+ $sanitized_login__not_in = array_map( 'esc_sql', $qv['login__not_in'] );
+ $login__not_in = implode( "','", $sanitized_login__not_in );
+ $this->query_where .= " AND user_login NOT IN ( '$login__not_in' )";
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Meta query.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->meta_query = new WP_Meta_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->meta_query->parse_query_vars( $qv );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -434,7 +486,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</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">- $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'nicename__in' === $_orderby || 'login__in' === $_orderby ) {
+ $orderby_array[] = $parsed;
+ } else {
+ $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
+ }
</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"> // If no valid clauses were found, order by user_login.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -700,6 +756,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $include = wp_parse_id_list( $this->query_vars['include'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $include_sql = implode( ',', $include );
</span><span class="cx" style="display: block; padding: 0 10px"> $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( 'nicename__in' === $orderby ) {
+ $sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
+ $nicename__in = implode( "','", $sanitized_nicename__in );
+ $_orderby = "FIELD( user_nicename, '$nicename__in' )";
+ } elseif ( 'login__in' === $orderby ) {
+ $sanitized_login__in = array_map( 'esc_sql', $this->query_vars['login__in'] );
+ $login__in = implode( "','", $sanitized_login__in );
+ $_orderby = "FIELD( user_login, '$login__in' )";
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_clause = $meta_query_clauses[ $orderby ];
</span><span class="cx" style="display: block; padding: 0 10px"> $_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
</span></span></pre></div>
<a id="trunktestsphpunittestsuserqueryphp"></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/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/query.php 2016-10-04 00:50:09 UTC (rev 38714)
+++ trunk/tests/phpunit/tests/user/query.php 2016-10-04 02:26:26 UTC (rev 38715)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -878,6 +878,193 @@
</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">+ * @ticket 36624
+ */
+ public function test_nicename_returns_user_with_nicename() {
+ wp_update_user( array(
+ 'ID' => self::$author_ids[0],
+ 'user_nicename' => 'peter'
+ ) );
+
+ $q = new WP_User_Query( array (
+ 'nicename' => 'peter'
+ ) );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+ $expected = array( self::$author_ids[0] );
+
+ $this->assertContains( "AND user_nicename = 'peter'", $q->query_where);
+ $this->assertEqualSets( $expected, $found);
+ }
+
+ /**
+ * @ticket 36624
+ */
+ public function test_nicename__in_returns_users_with_included_nicenames() {
+ wp_update_user( array(
+ 'ID' => self::$author_ids[0],
+ 'user_nicename' => 'peter'
+ ) );
+
+ wp_update_user( array(
+ 'ID' => self::$author_ids[1],
+ 'user_nicename' => 'paul'
+ ) );
+
+ wp_update_user( array(
+ 'ID' => self::$author_ids[2],
+ 'user_nicename' => 'mary'
+ ) );
+
+ $q = new WP_User_Query( array (
+ 'nicename__in' => array( 'peter', 'paul', 'mary' )
+ ) );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+ $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
+
+ $this->assertContains( "AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where);
+ $this->assertEqualSets( $expected, $found );
+ }
+
+ /**
+ * @ticket 36624
+ */
+ public function test_nicename__not_in_returns_users_without_included_nicenames() {
+ wp_update_user( array(
+ 'ID' => self::$author_ids[0],
+ 'user_nicename' => 'peter'
+ ) );
+
+ wp_update_user( array(
+ 'ID' => self::$author_ids[1],
+ 'user_nicename' => 'paul'
+ ) );
+
+ wp_update_user( array(
+ 'ID' => self::$author_ids[2],
+ 'user_nicename' => 'mary'
+ ) );
+
+ $q = new WP_User_Query( array (
+ 'nicename__not_in' => array( 'peter', 'paul', 'mary' )
+ ) );
+
+ $foundCount = count($q->get_results());
+ $expectedCount = 10; // 13 total users minus 3 from query
+
+ $this->assertContains( "AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where);
+ $this->assertEquals( $expectedCount, $foundCount );
+ }
+
+ /**
+ * @ticket 36624
+ */
+ public function test_orderby_nicename__in() {
+ wp_update_user( array(
+ 'ID' => self::$author_ids[0],
+ 'user_nicename' => 'peter'
+ ) );
+
+ wp_update_user( array(
+ 'ID' => self::$author_ids[1],
+ 'user_nicename' => 'paul'
+ ) );
+
+ wp_update_user( array(
+ 'ID' => self::$author_ids[2],
+ 'user_nicename' => 'mary'
+ ) );
+
+ $q = new WP_User_Query( array (
+ 'nicename__in' => array( 'mary', 'peter', 'paul' ),
+ 'orderby' => 'nicename__in'
+ ) );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+ $expected = array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] );
+
+ $this->assertContains( "FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby);
+ $this->assertSame( $expected, $found );
+ }
+
+ /**
+ * @ticket 36624
+ */
+ public function test_login_returns_user_with_login() {
+
+ $user_login = get_userdata( self::$author_ids[0] )->user_login;
+
+ $q = new WP_User_Query( array (
+ 'login' => $user_login
+ ) );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+ $expected = array( self::$author_ids[0] );
+
+ $this->assertContains( "AND user_login = '$user_login'", $q->query_where);
+ $this->assertEqualSets( $expected, $found);
+ }
+
+ /**
+ * @ticket 36624
+ */
+ public function test_login__in_returns_users_with_included_logins() {
+ $user_login1 = get_userdata( self::$author_ids[0] )->user_login;
+ $user_login2 = get_userdata( self::$author_ids[1] )->user_login;
+ $user_login3 = get_userdata( self::$author_ids[2] )->user_login;
+
+ $q = new WP_User_Query( array (
+ 'login__in' => array( $user_login1, $user_login2, $user_login3 )
+ ) );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+ $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
+
+ $this->assertContains( "AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
+ $this->assertEqualSets( $expected, $found );
+ }
+
+ /**
+ * @ticket 36624
+ */
+ public function test_login__not_in_returns_users_without_included_logins() {
+ $user_login1 = get_userdata( self::$author_ids[0] )->user_login;
+ $user_login2 = get_userdata( self::$author_ids[1] )->user_login;
+ $user_login3 = get_userdata( self::$author_ids[2] )->user_login;
+
+ $q = new WP_User_Query( array (
+ 'login__not_in' => array( $user_login1, $user_login2, $user_login3 )
+ ) );
+
+ $foundCount = count($q->get_results());
+ $expectedCount = 10; // 13 total users minus 3 from query
+
+ $this->assertContains( "AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
+ $this->assertEquals( $expectedCount, $foundCount );
+ }
+
+ /**
+ * @ticket 36624
+ */
+ public function test_orderby_login__in() {
+ $user_login1 = get_userdata( self::$author_ids[0] )->user_login;
+ $user_login2 = get_userdata( self::$author_ids[1] )->user_login;
+ $user_login3 = get_userdata( self::$author_ids[2] )->user_login;
+
+ $q = new WP_User_Query( array (
+ 'login__in' => array( $user_login2, $user_login3, $user_login1 ),
+ 'orderby' => 'login__in'
+ ) );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+ $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
+
+ $this->assertContains( "FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby);
+ $this->assertSame( $expected, $found );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 25145
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_paged() {
</span></span></pre>
</div>
</div>
</body>
</html>