<!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>[31669] trunk: Improved 'orderby' meta syntax in `WP_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/31669">31669</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/31669","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>2015-03-07 18:37:05 +0000 (Sat, 07 Mar 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'>Improved 'orderby' meta syntax in `WP_User_Query`.
Recent commits have added the ability to order query results by specific
clauses of the 'meta_query' parameter (comments <a href="https://core.trac.wordpress.org/changeset/31467">[31467]</a>, posts <a href="https://core.trac.wordpress.org/changeset/31312">[31312]</a> and
<a href="https://core.trac.wordpress.org/changeset/31340">[31340]</a>). The current changeset ports the same functionality to `WP_User_Query`.
Also introduced is the ability to pass the value of `$meta_key` to 'orderby'.
The internals of `WP_User_Query::prepare_users()` had to be reordered
somewhat to support these changes, primarily to ensure that the `meta_query`
object generates its SQL clauses before the 'orderby' parameter is parsed.
See <a href="https://core.trac.wordpress.org/ticket/31265">#31265</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.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="trunksrcwpincludesuserphp"></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.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user.php 2015-03-07 17:33:48 UTC (rev 31668)
+++ trunk/src/wp-includes/user.php 2015-03-07 18:37:05 UTC (rev 31669)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -531,13 +531,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * column to search in based on search string. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type array $search_columns Array of column names to be searched. Accepts 'ID', 'login',
</span><span class="cx" style="display: block; padding: 0 10px"> * 'nicename', 'email', 'url'. Default empty array.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @type string|array $orderby Field to sort the retrieved users by. May be a single value,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type string|array $orderby Field(s) to sort the retrieved users by. May be a single value,
</ins><span class="cx" style="display: block; padding: 0 10px"> * an array of values, or a multi-dimensional array with fields as keys
</span><span class="cx" style="display: block; padding: 0 10px"> * and orders ('ASC' or 'DESC') as values. Accepted values are'ID',
</span><span class="cx" style="display: block; padding: 0 10px"> * 'display_name' (or 'name'), 'user_login' (or 'login'),
</span><span class="cx" style="display: block; padding: 0 10px"> * 'user_nicename' (or 'nicename'), 'user_email' (or 'email'),
</span><span class="cx" style="display: block; padding: 0 10px"> * 'user_url' (or 'url'), 'user_registered' (or 'registered'),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * 'post_count', 'meta_value', or 'meta_value_num'. To use 'meta_value'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 'post_count', 'meta_value', 'meta_value_num', the value of
+ * `$meta_key`, or an array key of `$meta_query`. To use 'meta_value'
</ins><span class="cx" style="display: block; padding: 0 10px"> * or 'meta_value_num', `$meta_key` must be also be defined.
</span><span class="cx" style="display: block; padding: 0 10px"> * Default 'user_login'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type string $order Designates ascending or descending order of users. Order values
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -625,6 +626,52 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $include = false;
</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">+ // Meta query.
+ $this->meta_query = new WP_Meta_Query();
+ $this->meta_query->parse_query_vars( $qv );
+
+ $blog_id = 0;
+ if ( isset( $qv['blog_id'] ) ) {
+ $blog_id = absint( $qv['blog_id'] );
+ }
+
+ $role = '';
+ if ( isset( $qv['role'] ) ) {
+ $role = trim( $qv['role'] );
+ }
+
+ if ( $blog_id && ( $role || is_multisite() ) ) {
+ $cap_meta_query = array();
+ $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
+
+ if ( $role ) {
+ $cap_meta_query['value'] = '"' . $role . '"';
+ $cap_meta_query['compare'] = 'like';
+ }
+
+ if ( empty( $this->meta_query->queries ) ) {
+ $this->meta_query->queries = array( $cap_meta_query );
+ } elseif ( ! in_array( $cap_meta_query, $this->meta_query->queries, true ) ) {
+ // Append the cap query to the original queries and reparse the query.
+ $this->meta_query->queries = array(
+ 'relation' => 'AND',
+ array( $this->meta_query->queries, $cap_meta_query ),
+ );
+ }
+
+ $this->meta_query->parse_query_vars( $this->meta_query->queries );
+ }
+
+ if ( ! empty( $this->meta_query->queries ) ) {
+ $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
+ $this->query_from .= $clauses['join'];
+ $this->query_where .= $clauses['where'];
+
+ if ( 'OR' == $this->meta_query->relation ) {
+ $this->query_fields = 'DISTINCT ' . $this->query_fields;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // sorting
</span><span class="cx" style="display: block; padding: 0 10px"> $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : '';
</span><span class="cx" style="display: block; padding: 0 10px"> $order = $this->parse_order( $qv['order'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -728,10 +775,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
</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">- $blog_id = 0;
- if ( isset( $qv['blog_id'] ) )
- $blog_id = absint( $qv['blog_id'] );
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
</span><span class="cx" style="display: block; padding: 0 10px"> $qv['meta_value'] = 0;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -739,44 +782,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
</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">- $this->meta_query = new WP_Meta_Query();
- $this->meta_query->parse_query_vars( $qv );
-
- $role = '';
- if ( isset( $qv['role'] ) )
- $role = trim( $qv['role'] );
-
- if ( $blog_id && ( $role || is_multisite() ) ) {
- $cap_meta_query = array();
- $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
-
- if ( $role ) {
- $cap_meta_query['value'] = '"' . $role . '"';
- $cap_meta_query['compare'] = 'like';
- }
-
- if ( empty( $this->meta_query->queries ) ) {
- $this->meta_query->queries = array( $cap_meta_query );
- } elseif ( ! in_array( $cap_meta_query, $this->meta_query->queries, true ) ) {
- // Append the cap query to the original queries and reparse the query.
- $this->meta_query->queries = array(
- 'relation' => 'AND',
- array( $this->meta_query->queries, $cap_meta_query ),
- );
- }
-
- $this->meta_query->parse_query_vars( $this->meta_query->queries );
- }
-
- if ( !empty( $this->meta_query->queries ) ) {
- $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
- $this->query_from .= $clauses['join'];
- $this->query_where .= $clauses['where'];
-
- if ( 'OR' == $this->meta_query->relation )
- $this->query_fields = 'DISTINCT ' . $this->query_fields;
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $include ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Sanitized earlier.
</span><span class="cx" style="display: block; padding: 0 10px"> $ids = implode( ',', $include );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -955,6 +960,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected function parse_orderby( $orderby ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_query_clauses = $this->meta_query->get_clauses();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $_orderby = '';
</span><span class="cx" style="display: block; padding: 0 10px"> if ( in_array( $orderby, array( 'login', 'nicename', 'email', 'url', 'registered' ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $_orderby = 'user_' . $orderby;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -975,7 +982,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $_orderby = 'post_count';
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( 'ID' == $orderby || 'id' == $orderby ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $_orderby = 'ID';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( 'meta_value' == $orderby ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $_orderby = "$wpdb->usermeta.meta_value";
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( 'meta_value_num' == $orderby ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $_orderby = "$wpdb->usermeta.meta_value+0";
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -983,6 +990,9 @@
</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( $wpdb->users.ID, $include_sql )";
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
+ $meta_clause = $meta_query_clauses[ $orderby ];
+ $_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
</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 $_orderby;
</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 2015-03-07 17:33:48 UTC (rev 31668)
+++ trunk/tests/phpunit/tests/user/query.php 2015-03-07 18:37:05 UTC (rev 31669)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -180,6 +180,123 @@
</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 31265
+ */
+ public function test_orderby_somekey_where_meta_key_is_somekey() {
+ $users = $this->factory->user->create_many( 3, array(
+ 'role' => 'author'
+ ) );
+
+ update_user_meta( $users[0], 'foo', 'zzz' );
+ update_user_meta( $users[1], 'foo', 'aaa' );
+ update_user_meta( $users[2], 'foo', 'jjj' );
+
+ $q = new WP_User_Query( array(
+ 'include' => $users,
+ 'meta_key' => 'foo',
+ 'orderby' => 'foo',
+ 'fields' => 'ids'
+ ) );
+
+ $expected = array( $users[1], $users[2], $users[0] );
+
+ $this->assertEquals( $expected, $q->get_results() );
+ }
+
+ /**
+ * @ticket 31265
+ */
+ public function test_orderby_clause_key() {
+ $users = $this->factory->user->create_many( 3 );
+ add_user_meta( $users[0], 'foo', 'aaa' );
+ add_user_meta( $users[1], 'foo', 'zzz' );
+ add_user_meta( $users[2], 'foo', 'jjj' );
+
+ $q = new WP_User_Query( array(
+ 'fields' => 'ids',
+ 'meta_query' => array(
+ 'foo_key' => array(
+ 'key' => 'foo',
+ 'compare' => 'EXISTS',
+ ),
+ ),
+ 'orderby' => 'foo_key',
+ 'order' => 'DESC',
+ ) );
+
+ $this->assertEquals( array( $users[1], $users[2], $users[0] ), $q->results );
+ }
+
+ /**
+ * @ticket 31265
+ */
+ public function test_orderby_clause_key_as_secondary_sort() {
+ $u1 = $this->factory->user->create( array(
+ 'user_registered' => '2015-01-28 03:00:00',
+ ) );
+ $u2 = $this->factory->user->create( array(
+ 'user_registered' => '2015-01-28 05:00:00',
+ ) );
+ $u3 = $this->factory->user->create( array(
+ 'user_registered' => '2015-01-28 03:00:00',
+ ) );
+
+ add_user_meta( $u1, 'foo', 'jjj' );
+ add_user_meta( $u2, 'foo', 'zzz' );
+ add_user_meta( $u3, 'foo', 'aaa' );
+
+ $q = new WP_User_Query( array(
+ 'fields' => 'ids',
+ 'meta_query' => array(
+ 'foo_key' => array(
+ 'key' => 'foo',
+ 'compare' => 'EXISTS',
+ ),
+ ),
+ 'orderby' => array(
+ 'comment_date' => 'asc',
+ 'foo_key' => 'asc',
+ ),
+ ) );
+
+ $this->assertEquals( array( $u3, $u1, $u2 ), $q->results );
+ }
+
+ /**
+ * @ticket 31265
+ */
+ public function test_orderby_more_than_one_clause_key() {
+ $users = $this->factory->user->create_many( 3 );
+
+ add_user_meta( $users[0], 'foo', 'jjj' );
+ add_user_meta( $users[1], 'foo', 'zzz' );
+ add_user_meta( $users[2], 'foo', 'jjj' );
+ add_user_meta( $users[0], 'bar', 'aaa' );
+ add_user_meta( $users[1], 'bar', 'ccc' );
+ add_user_meta( $users[2], 'bar', 'bbb' );
+
+ $q = new WP_User_Query( array(
+ 'fields' => 'ids',
+ 'meta_query' => array(
+ 'foo_key' => array(
+ 'key' => 'foo',
+ 'compare' => 'EXISTS',
+ ),
+ 'bar_key' => array(
+ 'key' => 'bar',
+ 'compare' => 'EXISTS',
+ ),
+ ),
+ 'orderby' => array(
+ 'foo_key' => 'asc',
+ 'bar_key' => 'desc',
+ ),
+ ) );
+
+ $this->assertEquals( array( $users[2], $users[0], $users[1] ), $q->results );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 30064
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_orderby_include_with_empty_include() {
</span></span></pre>
</div>
</div>
</body>
</html>