<!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>[31467] trunk: Improve 'orderby' syntax for `WP_Comment_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/31467">31467</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/31467","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-02-16 14:09:40 +0000 (Mon, 16 Feb 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'>Improve 'orderby' syntax for `WP_Comment_Query`.
Since <a href="https://core.trac.wordpress.org/changeset/29027">[29027]</a>, `WP_Query` has supported an array of values for the `$orderby`
parameter, with field names as array keys and ASC/DESC as the array values.
This changeset introduces the same syntax to `WP_Comment_Query`.
We leverage the new support for multiple ORDER BY clauses to fix a bug that
causes comments to be queried in an indeterminate order when sorting by the
default `comment_date_gmt` and comments share the same value for
`comment_date_gmt`. By always including a `comment_ID` subclause at the end of
the ORDER BY statement, we ensure that comments always have a unique fallback
for sorting.
This changeset also includes improvements paralleling those introduced to
`WP_Query` in <a href="https://core.trac.wordpress.org/changeset/31312">[31312]</a> and <a href="https://core.trac.wordpress.org/changeset/31340">[31340]</a>, which allow `$orderby` to accept array keys
from specific `$meta_query` clauses. This change lets devs sort by multiple
clauses of an associated meta query. See <a href="https://core.trac.wordpress.org/ticket/31045">#31045</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/30478">#30478</a>. See <a href="https://core.trac.wordpress.org/ticket/31265">#31265</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescommentphp">trunk/src/wp-includes/comment.php</a></li>
<li><a href="#trunktestsphpunittestscommentqueryphp">trunk/tests/phpunit/tests/comment/query.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescommentphp"></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/comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment.php 2015-02-15 18:18:06 UTC (rev 31466)
+++ trunk/src/wp-includes/comment.php 2015-02-16 14:09:40 UTC (rev 31467)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -326,14 +326,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @type int $number Maximum number of comments to retrieve. Default null (no limit).
</span><span class="cx" style="display: block; padding: 0 10px"> * @type int $offset Number of comments to offset the query. Used to build LIMIT clause.
</span><span class="cx" style="display: block; padding: 0 10px"> * Default 0.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @type string|array $orderby Comment status or array of statuses. Accepts 'comment_agent',
- * 'comment_approved', 'comment_author', 'comment_author_email',
- * 'comment_author_IP', 'comment_author_url', 'comment_content',
- * 'comment_date', 'comment_date_gmt', 'comment_ID', 'comment_karma',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type string|array $orderby Comment status or array of statuses. To use 'meta_value' or
+ * 'meta_value_num', `$meta_key` must also be defined. To sort by
+ * a specific `$meta_query` clause, use that clause's array key.
+ * Accepts 'comment_agent', 'comment_approved', 'comment_author',
+ * 'comment_author_email', 'comment_author_IP',
+ * 'comment_author_url', 'comment_content', 'comment_date',
+ * 'comment_date_gmt', 'comment_ID', 'comment_karma',
</ins><span class="cx" style="display: block; padding: 0 10px"> * 'comment_parent', 'comment_post_ID', 'comment_type', 'user_id',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * 'meta_value', 'meta_value_num', or value of $meta_key.
- * Also accepts false, empty array, or 'none' to disable `ORDER BY`
- * clause. Default: 'comment_date_gmt'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 'meta_value', 'meta_value_num', the value of $meta_key, and the
+ * array keys of `$meta_query`. Also accepts false, an empty array,
+ * or 'none' to disable `ORDER BY` clause.
+ * Default: 'comment_date_gmt'.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'.
</span><span class="cx" style="display: block; padding: 0 10px"> * Default: 'DESC'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type int $parent Parent ID of comment to retrieve children of. Default empty.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -411,6 +415,10 @@
</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( $this->query_vars );
</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( $this->meta_query->queries ) ) {
+ $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires before comments are retrieved.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -514,39 +522,75 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->query_vars['orderby'] :
</span><span class="cx" style="display: block; padding: 0 10px"> preg_split( '/[,\s]/', $this->query_vars['orderby'] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $allowed_keys = array(
- 'comment_agent',
- 'comment_approved',
- 'comment_author',
- 'comment_author_email',
- 'comment_author_IP',
- 'comment_author_url',
- 'comment_content',
- 'comment_date',
- 'comment_date_gmt',
- 'comment_ID',
- 'comment_karma',
- 'comment_parent',
- 'comment_post_ID',
- 'comment_type',
- 'user_id',
- );
- if ( ! empty( $this->query_vars['meta_key'] ) ) {
- $allowed_keys[] = $this->query_vars['meta_key'];
- $allowed_keys[] = 'meta_value';
- $allowed_keys[] = 'meta_value_num';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $orderby_array = array();
+ $found_orderby_comment_ID = false;
+ foreach ( $ordersby as $_key => $_value ) {
+ if ( ! $_value ) {
+ continue;
+ }
+
+ if ( is_int( $_key ) ) {
+ $_orderby = $_value;
+ $_order = $order;
+ } else {
+ $_orderby = $_key;
+ $_order = $_value;
+ }
+
+ if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
+ $found_orderby_comment_ID = true;
+ }
+
+ $parsed = $this->parse_orderby( $_orderby );
+
+ if ( ! $parsed ) {
+ continue;
+ }
+
+ $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $ordersby = array_intersect( $ordersby, $allowed_keys );
- foreach ( $ordersby as $key => $value ) {
- if ( $value == $this->query_vars['meta_key'] || $value == 'meta_value' ) {
- $ordersby[ $key ] = "$wpdb->commentmeta.meta_value";
- } elseif ( $value == 'meta_value_num' ) {
- $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // If no valid clauses were found, order by comment_date_gmt.
+ if ( empty( $orderby_array ) ) {
+ $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
+ }
+
+ // To ensure determinate sorting, always include a comment_ID clause.
+ if ( ! $found_orderby_comment_ID ) {
+ $comment_ID_order = '';
+
+ // Inherit order from comment_date or comment_date_gmt, if available.
+ foreach ( $orderby_array as $orderby_clause ) {
+ if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) {
+ $comment_ID_order = $match[1];
+ break;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // If no date-related order is available, use the date from the first available clause.
+ if ( ! $comment_ID_order ) {
+ foreach ( $orderby_array as $orderby_clause ) {
+ if ( false !== strpos( 'ASC', $orderby_clause ) ) {
+ $comment_ID_order = 'ASC';
+ } else {
+ $comment_ID_order = 'DESC';
+ }
+
+ break;
+ }
+ }
+
+ // Default to DESC.
+ if ( ! $comment_ID_order ) {
+ $comment_ID_order = 'DESC';
+ }
+
+ $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $orderby = implode( ', ', $orderby_array );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $orderby = 'comment_date_gmt';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $orderby = "$wpdb->comments.comment_date_gmt $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"> $number = absint( $this->query_vars['number'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -709,12 +753,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
</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 ( ! empty( $this->meta_query->queries ) ) {
- $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
- $join .= $clauses['join'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $meta_query_clauses ) ) {
+ $join .= $meta_query_clauses['join'];
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Strip leading 'AND'.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $where[] = preg_replace( '/^\s*AND\s*/', '', $clauses['where'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $this->query_vars['count'] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $groupby = "{$wpdb->comments}.comment_ID";
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -729,7 +772,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $where = implode( ' AND ', $where );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filter the comment query clauses.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -744,7 +787,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
</span><span class="cx" style="display: block; padding: 0 10px"> $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
</span><span class="cx" style="display: block; padding: 0 10px"> $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
</del><span class="cx" style="display: block; padding: 0 10px"> $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
</span><span class="cx" style="display: block; padding: 0 10px"> $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -757,7 +799,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 ( $orderby ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $orderby = "ORDER BY $orderby $order";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $orderby = "ORDER BY $orderby";
</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"> $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -809,6 +851,88 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return ' AND (' . implode(' OR ', $searches) . ')';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Parse and sanitize 'orderby' keys passed to the comment query.
+ *
+ * @since 4.2.0
+ * @access protected
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param string $orderby Alias for the field to order by.
+ * @return string|bool Value to used in the ORDER clause. False otherwise.
+ */
+ protected function parse_orderby( $orderby ) {
+ global $wpdb;
+
+ $allowed_keys = array(
+ 'comment_agent',
+ 'comment_approved',
+ 'comment_author',
+ 'comment_author_email',
+ 'comment_author_IP',
+ 'comment_author_url',
+ 'comment_content',
+ 'comment_date',
+ 'comment_date_gmt',
+ 'comment_ID',
+ 'comment_karma',
+ 'comment_parent',
+ 'comment_post_ID',
+ 'comment_type',
+ 'user_id',
+ );
+
+ if ( ! empty( $this->query_vars['meta_key'] ) ) {
+ $allowed_keys[] = $this->query_vars['meta_key'];
+ $allowed_keys[] = 'meta_value';
+ $allowed_keys[] = 'meta_value_num';
+ }
+
+ $meta_query_clauses = $this->meta_query->get_clauses();
+ if ( $meta_query_clauses ) {
+ $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );
+ }
+
+ $parsed = false;
+ if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
+ $parsed = "$wpdb->commentmeta.meta_value";
+ } else if ( $orderby == 'meta_value_num' ) {
+ $parsed = "$wpdb->commentmeta.meta_value+0";
+ } else if ( in_array( $orderby, $allowed_keys ) ) {
+
+ if ( isset( $meta_query_clauses[ $orderby ] ) ) {
+ $meta_clause = $meta_query_clauses[ $orderby ];
+ $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
+ } else {
+ $parsed = "$wpdb->comments.$orderby";
+ }
+ }
+
+ return $parsed;
+ }
+
+ /**
+ * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
+ *
+ * @since 4.2.0
+ * @access protected
+ *
+ * @param string $order The 'order' query variable.
+ * @return string The sanitized 'order' query variable.
+ */
+ protected function parse_order( $order ) {
+ if ( ! is_string( $order ) || empty( $order ) ) {
+ return 'DESC';
+ }
+
+ if ( 'ASC' === strtoupper( $order ) ) {
+ return 'ASC';
+ } else {
+ return 'DESC';
+ }
+ }
</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="trunktestsphpunittestscommentqueryphp"></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/comment/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment/query.php 2015-02-15 18:18:06 UTC (rev 31466)
+++ trunk/tests/phpunit/tests/comment/query.php 2015-02-16 14:09:40 UTC (rev 31467)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -612,14 +612,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $comment_id2, $comments[1]->comment_ID );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'key' ) ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, count( $comments ) );
- $this->assertEquals( $comment_id, $comments[0]->comment_ID );
- $this->assertEquals( $comment_id3, $comments[1]->comment_ID );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'meta_value' ) ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, count( $comments ) );
- $this->assertEquals( $comment_id, $comments[0]->comment_ID );
- $this->assertEquals( $comment_id3, $comments[1]->comment_ID );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // value1 is present on two different keys for $comment_id yet we should get only one instance
</span><span class="cx" style="display: block; padding: 0 10px"> // of that comment in the results
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -631,6 +627,103 @@
</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 30478
+ */
+ public function test_orderby_clause_key() {
+ $comments = $this->factory->comment->create_many( 3 );
+ add_comment_meta( $comments[0], 'foo', 'aaa' );
+ add_comment_meta( $comments[1], 'foo', 'zzz' );
+ add_comment_meta( $comments[2], 'foo', 'jjj' );
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'fields' => 'ids',
+ 'meta_query' => array(
+ 'foo_key' => array(
+ 'key' => 'foo',
+ 'compare' => 'EXISTS',
+ ),
+ ),
+ 'orderby' => 'foo_key',
+ 'order' => 'DESC',
+ ) );
+
+ $this->assertEquals( array( $comments[1], $comments[2], $comments[0] ), $found );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_clause_key_as_secondary_sort() {
+ $c1 = $this->factory->comment->create( array(
+ 'comment_date' => '2015-01-28 03:00:00',
+ ) );
+ $c2 = $this->factory->comment->create( array(
+ 'comment_date' => '2015-01-28 05:00:00',
+ ) );
+ $c3 = $this->factory->comment->create( array(
+ 'comment_date' => '2015-01-28 03:00:00',
+ ) );
+
+ add_comment_meta( $c1, 'foo', 'jjj' );
+ add_comment_meta( $c2, 'foo', 'zzz' );
+ add_comment_meta( $c3, 'foo', 'aaa' );
+
+ $q = new WP_Comment_Query();
+ $found = $q->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( $c3, $c1, $c2 ), $found );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_more_than_one_clause_key() {
+ $comments = $this->factory->comment->create_many( 3 );
+
+ add_comment_meta( $comments[0], 'foo', 'jjj' );
+ add_comment_meta( $comments[1], 'foo', 'zzz' );
+ add_comment_meta( $comments[2], 'foo', 'jjj' );
+ add_comment_meta( $comments[0], 'bar', 'aaa' );
+ add_comment_meta( $comments[1], 'bar', 'ccc' );
+ add_comment_meta( $comments[2], 'bar', 'bbb' );
+
+ $q = new WP_Comment_Query();
+ $found = $q->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( $comments[2], $comments[0], $comments[1] ), $found );
+ }
+
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 27064
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_get_comments_by_user() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -985,64 +1078,78 @@
</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"> public function test_orderby_default() {
</span><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"> $q = new WP_Comment_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $q->query( array() );
</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->assertContains( 'ORDER BY comment_date_gmt', $q->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
</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 test_orderby_single() {
</span><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"> $q = new WP_Comment_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $q->query( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'comment_agent',
</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->assertContains( 'ORDER BY comment_agent', $q->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent", $q->request );
</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 test_orderby_single_invalid() {
</span><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"> $q = new WP_Comment_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $q->query( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'foo',
</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->assertContains( 'ORDER BY comment_date_gmt', $q->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
</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 test_orderby_comma_separated() {
</span><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"> $q = new WP_Comment_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $q->query( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'comment_agent, comment_approved',
</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->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
</ins><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">- public function test_orderby_array() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_orderby_flat_array() {
+ global $wpdb;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $q = new WP_Comment_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $q->query( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => array( 'comment_agent', 'comment_approved' ),
</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->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
</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 test_orderby_array_contains_invalid_item() {
</span><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"> $q = new WP_Comment_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $q->query( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => array( 'comment_agent', 'foo', 'comment_approved' ),
</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->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
</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 test_orderby_array_contains_all_invalid_items() {
</span><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"> $q = new WP_Comment_Query();
</span><span class="cx" style="display: block; padding: 0 10px"> $q->query( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => array( 'foo', 'bar', 'baz' ),
</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->assertContains( 'ORDER BY comment_date_gmt', $q->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
</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">@@ -1081,6 +1188,158 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertNotContains( 'ORDER BY', $q->request );
</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 30478
+ */
+ public function test_orderby_array() {
+ global $wpdb;
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'fields' => 'ids',
+ 'orderby' => array(
+ 'comment_agent' => 'DESC',
+ 'comment_date_gmt' => 'ASC',
+ 'comment_ID' => 'DESC',
+ ),
+ ) );
+
+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID DESC", $q->request );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_array_should_discard_invalid_columns() {
+ global $wpdb;
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'fields' => 'ids',
+ 'orderby' => array(
+ 'comment_agent' => 'DESC',
+ 'foo' => 'ASC',
+ 'comment_ID' => 'DESC',
+ ),
+ ) );
+
+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_ID DESC", $q->request );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_array_should_convert_invalid_order_to_DESC() {
+ global $wpdb;
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'fields' => 'ids',
+ 'orderby' => array(
+ 'comment_agent' => 'DESC',
+ 'comment_date_gmt' => 'foo',
+ 'comment_ID' => 'DESC',
+ ),
+ ) );
+
+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt DESC, $wpdb->comments.comment_ID DESC", $q->request );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date_gmt() {
+ global $wpdb;
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'fields' => 'ids',
+ 'orderby' => array(
+ 'comment_agent' => 'DESC',
+ 'comment_date_gmt' => 'ASC',
+ ),
+ ) );
+
+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID ASC", $q->request );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date() {
+ global $wpdb;
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'fields' => 'ids',
+ 'orderby' => array(
+ 'comment_agent' => 'DESC',
+ 'comment_date' => 'ASC',
+ ),
+ ) );
+
+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date ASC, $wpdb->comments.comment_ID ASC", $q->request );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_array_should_sort_by_comment_ID_DESC_as_fallback_when_not_sorted_by_date() {
+ global $wpdb;
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'fields' => 'ids',
+ 'orderby' => array(
+ 'comment_agent' => 'ASC',
+ ),
+ ) );
+
+ $this->assertContains( "ORDER BY $wpdb->comments.comment_agent ASC, $wpdb->comments.comment_ID DESC", $q->request );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_ASC() {
+ $now = current_time( 'mysql', 1 );
+ $comments = $this->factory->comment->create_many( 5, array(
+ 'comment_post_ID' => $this->post_id,
+ 'comment_date_gmt' => $now,
+ ) );
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'orderby' => 'comment_date_gmt',
+ 'order' => 'ASC',
+ ) );
+
+ // $comments is ASC by default.
+ $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) );
+ }
+
+ /**
+ * @ticket 30478
+ */
+ public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_DESC() {
+ $now = current_time( 'mysql', 1 );
+ $comments = $this->factory->comment->create_many( 5, array(
+ 'comment_post_ID' => $this->post_id,
+ 'comment_date_gmt' => $now,
+ ) );
+
+ $q = new WP_Comment_Query();
+ $found = $q->query( array(
+ 'orderby' => 'comment_date_gmt',
+ 'order' => 'DESC',
+ ) );
+
+ // $comments is ASC by default.
+ rsort( $comments );
+
+ $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_count() {
</span><span class="cx" style="display: block; padding: 0 10px"> $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7 ) );
</span></span></pre>
</div>
</div>
</body>
</html>