From a428ffc53d15ebd98c4db9ca302f37d546a43c7e Mon Sep 17 00:00:00 2001
From: Boone Gorges <boonebgorges@git.wordpress.org>
Date: Mon, 2 Jan 2017 20:06:59 +0000
Subject: [PATCH] Ignore the 'comment_order' setting when determining comment
 pagination.

[38740] incorrectly introduced logic that changed a comment's page when
'comment_order' was set to 'desc'. This is in violation of the design
of the comment pagination system: a comment's page is designed not to
change when 'comment_order' or 'default_comment_page' are changed.
See #31101.

Merges [39663] to the 4.7 branch.

Props rachelbaker.
Fixes #39280.


git-svn-id: https://develop.svn.wordpress.org/branches/4.7@39664 602fd350-edb4-49c9-b593-d223f7449a82
---
 src/wp-includes/comment.php                   |  8 +-----
 .../tests/comment/getPageOfComment.php        | 26 +++++++++++++++++--
 2 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php
index ae759c04f5..ba60070fd7 100644
--- a/src/wp-includes/comment.php
+++ b/src/wp-includes/comment.php
@@ -1003,12 +1003,6 @@ 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,
@@ -1019,7 +1013,7 @@ function get_page_of_comment( $comment_ID, $args = array() ) {
 			'date_query' => array(
 				array(
 					'column' => "$wpdb->comments.comment_date_gmt",
-					$compare => $comment->comment_date_gmt,
+					'before' => $comment->comment_date_gmt,
 				)
 			),
 		);
diff --git a/tests/phpunit/tests/comment/getPageOfComment.php b/tests/phpunit/tests/comment/getPageOfComment.php
index e8c0d4ea5c..580d129228 100644
--- a/tests/phpunit/tests/comment/getPageOfComment.php
+++ b/tests/phpunit/tests/comment/getPageOfComment.php
@@ -241,18 +241,40 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
 
 	/**
 	 * @ticket 31101
+	 * @ticket 39280
 	 */
-	public function test_should_respect_comment_order_newest() {
+	public function test_should_ignore_comment_order() {
 		$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 ) ) );
+		$c4 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ) ) );
 
 		update_option( 'comment_order', 'desc' );
 		update_option( 'page_comments', 1 );
-		update_option( 'comments_per_page', 2 );
+		update_option( 'comments_per_page', 1 );
+
+		$this->assertEquals( 2, get_page_of_comment( $c3 ) );
+	}
+
+	/**
+	 * @ticket 31101
+	 * @ticket 39280
+	 */
+	public function test_should_ignore_default_comment_page() {
+		$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 ) ) );
+		$c4 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ) ) );
+
+		update_option( 'default_comment_page', 'newest' );
+		update_option( 'page_comments', 1 );
+		update_option( 'comments_per_page', 1 );
 
 		$this->assertEquals( 2, get_page_of_comment( $c3 ) );
 	}