diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 523422e7df..0bee2474e0 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1323,10 +1323,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 ); + } }