From 9dd9880600c749e1f5d5290fe7af4b2f591b0090 Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Thu, 6 Oct 2016 17:41:51 +0000 Subject: [PATCH] Comments: Account for the `comment_order` option in `get_page_of_comment()`. Use the value of the `comment_order` setting to determine the date_query key to pass to `WP_Comment_Query`. Fixes a bug where sites that had comments ordered "newest" first would have the incorrect page number returned. Props tyxla, boonebgorges. Fixes #31101. git-svn-id: https://develop.svn.wordpress.org/trunk@38740 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 8 +++++++- .../phpunit/tests/comment/getPageOfComment.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 4a3966824e..220c45b14b 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -918,6 +918,12 @@ function get_page_of_comment( $comment_ID, $args = array() ) { if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) return get_page_of_comment( $comment->comment_parent, $args ); + if ( 'desc' === get_option( 'comment_order' ) ) { + $compare = 'after'; + } else { + $compare = 'before'; + } + $comment_args = array( 'type' => $args['type'], 'post_id' => $comment->comment_post_ID, @@ -928,7 +934,7 @@ function get_page_of_comment( $comment_ID, $args = array() ) { 'date_query' => array( array( 'column' => "$wpdb->comments.comment_date_gmt", - 'before' => $comment->comment_date_gmt, + $compare => $comment->comment_date_gmt, ) ), ); diff --git a/tests/phpunit/tests/comment/getPageOfComment.php b/tests/phpunit/tests/comment/getPageOfComment.php index b41f64998c..e8c0d4ea5c 100644 --- a/tests/phpunit/tests/comment/getPageOfComment.php +++ b/tests/phpunit/tests/comment/getPageOfComment.php @@ -238,4 +238,22 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase { $this->assertEquals( 2, get_page_of_comment( $c1 ) ); } + + /** + * @ticket 31101 + */ + public function test_should_respect_comment_order_newest() { + $now = time(); + + $p = self::factory()->post->create(); + $c1 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) ); + $c2 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) ); + $c3 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) ); + + update_option( 'comment_order', 'desc' ); + update_option( 'page_comments', 1 ); + update_option( 'comments_per_page', 2 ); + + $this->assertEquals( 2, get_page_of_comment( $c3 ) ); + } }