From 92f2281f6f9b79af6f84faa0798f19449a053461 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Wed, 20 Jan 2016 08:32:00 +0000 Subject: [PATCH] Comments: Ignore hierarchy in pagination calculation when comment threading is disabled. In order to calculate comment pagination when newest comments are displayed first, `comments_template()` must perform a separate query to determine the total number of paginating comments available on a post. See [34729], #8071, pagination calculation - can be defined as a top-level comment, or a comment with `parent=0`. However, when comment threading is disabled, yet comments exist in the database that have parents, all comments - even those with a parent - are "paginating". (This typically happens when comments threading was once enabled, but has since been turned off.) As such, the total-paginating- comments query should only be limited to top-level comments when 'thread_comments' is disabled. Merges [36275] to the 4.4 branch. Props jmdodd. Fixes #35419. git-svn-id: https://develop.svn.wordpress.org/branches/4.4@36362 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 5 ++- .../tests/comment/commentsTemplate.php | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index a363979f87..ad5b9caf5e 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1322,10 +1322,13 @@ function comments_template( $file = '/comments.php', $separate_comments = false 'count' => true, 'orderby' => false, 'post_id' => $post->ID, - 'parent' => 0, 'status' => 'approve', ); + if ( $comment_args['hierarchical'] ) { + $top_level_args['parent'] = 0; + } + if ( isset( $comment_args['include_unapproved'] ) ) { $top_level_args['include_unapproved'] = $comment_args['include_unapproved']; } diff --git a/tests/phpunit/tests/comment/commentsTemplate.php b/tests/phpunit/tests/comment/commentsTemplate.php index 3c4ddd368b..c26a88b0b2 100644 --- a/tests/phpunit/tests/comment/commentsTemplate.php +++ b/tests/phpunit/tests/comment/commentsTemplate.php @@ -728,4 +728,47 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase { $found_cids = array_map( 'intval', $matches[1] ); $this->assertSame( array( $comment_2, $comment_3, $comment_1 ), $found_cids ); } + + /** + * @ticket 35419 + */ + public function test_pagination_calculation_should_ignore_comment_hierarchy_when_threading_is_disabled() { + $now = time(); + $p = self::factory()->post->create(); + $comment_1 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_approved' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + $comment_2 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '2', + 'comment_approved' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + $comment_3 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '3', + 'comment_approved' => '1', + 'comment_parent' => $comment_1, + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), + ) ); + + update_option( 'thread_comments', 0 ); + + update_option( 'comment_order', 'asc' ); + update_option( 'comments_per_page', 2 ); + update_option( 'page_comments', 1 ); + update_option( 'default_comments_page', 'newest' ); + + $this->go_to( get_permalink( $p ) ); + $found = get_echo( 'comments_template' ); + + // Find the found comments in the markup. + preg_match_all( '|id="comment-([0-9]+)|', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_3 ), $found_cids ); + } }