diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 84a60dbc4a..f19d1a99da 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1313,12 +1313,19 @@ function comments_template( $file = '/comments.php', $separate_comments = false } else { // If fetching the first page of 'newest', we need a top-level comment count. $top_level_query = new WP_Comment_Query(); - $top_level_count = $top_level_query->query( array( + $top_level_args = array( 'count' => true, 'orderby' => false, 'post_id' => $post->ID, 'parent' => 0, - ) ); + 'status' => 'approve', + ); + + if ( isset( $comment_args['include_unapproved'] ) ) { + $top_level_args['include_unapproved'] = $comment_args['include_unapproved']; + } + + $top_level_count = $top_level_query->query( $top_level_args ); $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page; } diff --git a/tests/phpunit/tests/comment/commentsTemplate.php b/tests/phpunit/tests/comment/commentsTemplate.php index c112924884..308df77c24 100644 --- a/tests/phpunit/tests/comment/commentsTemplate.php +++ b/tests/phpunit/tests/comment/commentsTemplate.php @@ -573,4 +573,120 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase { $this->assertContains( 'cpage=1', $m ); } } + + /** + * @ticket 35068 + */ + public function test_query_offset_should_not_include_unapproved_comments() { + $now = time(); + $p = self::factory()->post->create(); + $comment_1 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_approved' => '0', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), + ) ); + $comment_2 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '2', + 'comment_approved' => '0', + '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' => '0', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + $comment_4 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '4', + 'comment_approved' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), + ) ); + + update_option( 'comment_order', 'asc' ); + update_option( 'default_comments_page', 'newest' ); + update_option( 'page_comments', 1 ); + update_option( 'comments_per_page', 2 ); + + $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_4 ), $found_cids ); + } + + /** + * @ticket 35068 + */ + public function test_query_offset_should_include_unapproved_comments() { + $comment_author_email = 'foo@example.com'; + + $now = time(); + $p = self::factory()->post->create(); + $comment_1 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_approved' => '0', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), + ) ); + $comment_2 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '2', + 'comment_approved' => '0', + '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' => '0', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), + 'comment_author_email' => $comment_author_email, + ) ); + $comment_4 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '4', + 'comment_approved' => '0', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + 'comment_author_email' => $comment_author_email, + ) ); + $comment_5 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '5', + 'comment_approved' => '0', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + 'comment_author_email' => $comment_author_email, + ) ); + $comment_6 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '6', + 'comment_approved' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), + ) ); + + update_option( 'comment_order', 'asc' ); + update_option( 'default_comments_page', 'newest' ); + update_option( 'page_comments', 1 ); + update_option( 'comments_per_page', 2 ); + + add_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) ); + $this->go_to( get_permalink( $p ) ); + $found = get_echo( 'comments_template' ); + remove_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) ); + + // 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_4, $comment_3 ), $found_cids ); + } + + public function fake_current_commenter( $commenter ) { + $commenter['comment_author_email'] = 'foo@example.com'; + return $commenter; + } }