Respect approval status when determining comment page count in `comments_template()`.

Since 4.4, when fetching the first page of comments and the 'newest' comments
are set to display first, `comments_template()` must perform arithmetic to
determine which comments to show. See #8071. This arithmetic requires the
total comment count for the current post, which is calculated with a separate
`WP_Comment_Query`. This secondary comment query did not properly account for
non-approved comment statuses; all unapproved comments should be part of the
comment count for admins, and individual users should have their own
unapproved comments included in the count. As a result, `comments_template()`
was, in some cases, being fooled into thinking that a post had fewer comments
available for pagination than it actually had, which resulted in empty pages
of comments.

We correct this problem by mirroring 'status' and 'include_unapproved' params
of the main comment query within the secondary query used to calculate
pagination.

Fixes #35068.

git-svn-id: https://develop.svn.wordpress.org/trunk@36040 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-12-21 03:06:41 +00:00
parent 4024cb673a
commit 817013978d
2 changed files with 125 additions and 2 deletions

View File

@ -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;
}

View File

@ -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;
}
}