From 382871ad9dc4e708a44746af0c95cb2eb41d1626 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 24 Oct 2014 02:50:24 +0000 Subject: [PATCH] Allow ORDER BY in `WP_Comment_Query::query()` to be disabled. Disable ORDER BY by passing boolean false, an empty array, or the string 'none' to the 'orderby parameter. This mirrors the behavior of `WP_Query`. Props psycleuk. Fixes #29902. git-svn-id: https://develop.svn.wordpress.org/trunk@30004 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 11 ++++++-- tests/phpunit/tests/comment/query.php | 36 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 20b208ed9e..c5776512c0 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -387,7 +387,10 @@ class WP_Comment_Query { $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC'; - if ( ! empty( $this->query_vars['orderby'] ) ) { + // Disable ORDER BY with 'none', an empty array, or boolean false. + if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { + $orderby = ''; + } else if ( ! empty( $this->query_vars['orderby'] ) ) { $ordersby = is_array( $this->query_vars['orderby'] ) ? $this->query_vars['orderby'] : preg_split( '/[,\s]/', $this->query_vars['orderby'] ); @@ -588,7 +591,11 @@ class WP_Comment_Query { $groupby = 'GROUP BY ' . $groupby; } - $this->request = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby $orderby $order $limits"; + if ( $orderby ) { + $orderby = "ORDER BY $orderby $order"; + } + + $this->request = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby $orderby $limits"; if ( $this->query_vars['count'] ) { return $wpdb->get_var( $this->request ); diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 0d8bb02c54..db171759a0 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -577,4 +577,40 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertContains( 'ORDER BY comment_date_gmt', $q->request ); } + + /** + * @ticket 29902 + */ + public function test_orderby_none() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => 'none', + ) ); + + $this->assertNotContains( 'ORDER BY', $q->request ); + } + + /** + * @ticket 29902 + */ + public function test_orderby_empty_array() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => array(), + ) ); + + $this->assertNotContains( 'ORDER BY', $q->request ); + } + + /** + * @ticket 29902 + */ + public function test_orderby_false() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => false, + ) ); + + $this->assertNotContains( 'ORDER BY', $q->request ); + } }