Comments: In `wp_list_comments()`, queries with custom pagination params should obey default `comment_status` logic.

When custom pagination parameters are passed to `wp_list_comments()`, a
secondary query must be performed to fetch the proper comments. See [36157].
This query should show comments of the same `comment_status` as the default
query initialized in `comments_template()`: show only comments that are
approved, or those that are unapproved but belong to the current user.

Props smerriman.
Fixes #37048.

git-svn-id: https://develop.svn.wordpress.org/trunk@37655 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-06-08 04:00:18 +00:00
parent 5c4a15d0fc
commit ca05a4b43d
2 changed files with 98 additions and 4 deletions

View File

@ -1960,13 +1960,23 @@ function wp_list_comments( $args = array(), $comments = null ) {
$current_per_page = get_query_var( 'comments_per_page' );
if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
$comments = get_comments( array(
$comment_args = array(
'post_id' => get_the_ID(),
'orderby' => 'comment_date_gmt',
'order' => 'ASC',
'status' => 'all',
) );
'status' => 'approve',
);
if ( is_user_logged_in() ) {
$comment_args['include_unapproved'] = get_current_user_id();
} else {
$commenter = wp_get_current_commenter();
if ( $commenter['comment_author_email'] ) {
$comment_args['include_unapproved'] = $commenter['comment_author_email'];
}
}
$comments = get_comments( $comment_args );
if ( 'all' != $r['type'] ) {
$comments_by_type = separate_comments( $comments );

View File

@ -146,4 +146,88 @@ class Tests_Comment_WpListComments extends WP_UnitTestCase {
preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
$this->assertSame( array( $comments[3] ), array_map( 'intval', $matches[1] ) );
}
/**
* @ticket 37048
*/
public function test_custom_pagination_should_not_result_in_unapproved_comments_being_shown() {
$p = self::factory()->post->create();
$comments = array();
$now = time();
for ( $i = 0; $i <= 5; $i++ ) {
$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
'comment_author' => 'Commenter ' . $i,
) );
}
// Only 2 and 5 are approved.
wp_set_comment_status( $comments[0], '0' );
wp_set_comment_status( $comments[1], '0' );
wp_set_comment_status( $comments[3], '0' );
wp_set_comment_status( $comments[4], '0' );
update_option( 'page_comments', true );
update_option( 'comments_per_page', 2 );
$this->go_to( get_permalink( $p ) );
// comments_template() populates $wp_query->comments
get_echo( 'comments_template' );
$found = wp_list_comments( array(
'echo' => false,
'per_page' => 1,
'page' => 2,
) );
preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
$this->assertSame( array( $comments[2] ), array_map( 'intval', $matches[1] ) );
}
/**
* @ticket 37048
*/
public function test_custom_pagination_should_allow_ones_own_unapproved_comments() {
$p = self::factory()->post->create();
$u = self::factory()->user->create();
$comments = array();
$now = time();
for ( $i = 0; $i <= 5; $i++ ) {
$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
'comment_author' => 'Commenter ' . $i,
'user_id' => $u,
) );
}
// Only 2 and 5 are approved.
wp_set_comment_status( $comments[0], '0' );
wp_set_comment_status( $comments[1], '0' );
wp_set_comment_status( $comments[3], '0' );
wp_set_comment_status( $comments[4], '0' );
update_option( 'page_comments', true );
update_option( 'comments_per_page', 2 );
wp_set_current_user( $u );
$this->go_to( get_permalink( $p ) );
// comments_template() populates $wp_query->comments
get_echo( 'comments_template' );
$found = wp_list_comments( array(
'echo' => false,
'per_page' => 1,
'page' => 2,
) );
preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
$this->assertSame( array( $comments[4] ), array_map( 'intval', $matches[1] ) );
}
}