Always respect $comments array passed to wp_list_comments().

[36157] fixed a bug whereby `wp_list_comments()` would not properly recognize
custom pagination arguments. See #35175. However, it inadvertently introduced
a bug that caused any `$comments` array explicitly passed to the function to be
ignored, when that array was accompanied by pagination arguments that differ
from those in `$wp_query`. We address this bug by moving the logic introduced
in [36157] inside a block that only fires when no `$comments` array has been
provided to the function.

Props ivankristianto.
Fixes #35356.

git-svn-id: https://develop.svn.wordpress.org/trunk@36276 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-01-13 03:26:31 +00:00
parent f7d238dfe3
commit 1880b466c7
2 changed files with 98 additions and 47 deletions

View File

@ -1910,6 +1910,20 @@ function wp_list_comments( $args = array(), $comments = null ) {
*/
$r = apply_filters( 'wp_list_comments_args', $r );
// Figure out what comments we'll be looping through ($_comments)
if ( null !== $comments ) {
$comments = (array) $comments;
if ( empty($comments) )
return;
if ( 'all' != $r['type'] ) {
$comments_by_type = separate_comments($comments);
if ( empty($comments_by_type[$r['type']]) )
return;
$_comments = $comments_by_type[$r['type']];
} else {
$_comments = $comments;
}
} else {
/*
* If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
* perform a separate comment query and allow Walker_Comment to paginate.
@ -1928,22 +1942,20 @@ function wp_list_comments( $args = array(), $comments = null ) {
'order' => 'ASC',
'status' => 'all',
) );
}
if ( 'all' != $r['type'] ) {
$comments_by_type = separate_comments( $comments );
if ( empty( $comments_by_type[ $r['type'] ] ) ) {
return;
}
// Figure out what comments we'll be looping through ($_comments)
if ( null !== $comments ) {
$comments = (array) $comments;
if ( empty($comments) )
return;
if ( 'all' != $r['type'] ) {
$comments_by_type = separate_comments($comments);
if ( empty($comments_by_type[$r['type']]) )
return;
$_comments = $comments_by_type[$r['type']];
$_comments = $comments_by_type[ $r['type'] ];
} else {
$_comments = $comments;
}
}
// Otherwise, fall back on the comments from `$wp_query->comments`.
} else {
if ( empty($wp_query->comments) )
return;
@ -1957,14 +1969,16 @@ function wp_list_comments( $args = array(), $comments = null ) {
$_comments = $wp_query->comments;
}
// Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
if ( $wp_query->max_num_comment_pages ) {
$default_comments_page = get_option( 'default_comments_page' );
$cpage = get_query_var( 'cpage' );
if ( 'newest' === $default_comments_page ) {
$r['cpage'] = $cpage;
// When first page shows oldest comments, post permalink is the same as the comment permalink.
/*
* When first page shows oldest comments, post permalink is the same as
* the comment permalink.
*/
} elseif ( $cpage == 1 ) {
$r['cpage'] = '';
} else {
@ -1975,6 +1989,7 @@ function wp_list_comments( $args = array(), $comments = null ) {
$r['per_page'] = 0;
}
}
}
if ( '' === $r['per_page'] && get_option( 'page_comments' ) ) {
$r['per_page'] = get_query_var('comments_per_page');

View File

@ -110,4 +110,40 @@ class Tests_Comment_WpListComments extends WP_UnitTestCase {
preg_match_all( '|id="comment\-([0-9]+)"|', $found2, $matches );
$this->assertSame( array( $comments[1], $comments[0] ), array_map( 'intval', $matches[1] ) );
}
/**
* @ticket 35356
* @ticket 35175
*/
public function test_comments_param_should_be_respected_when_custom_pagination_params_are_passed() {
$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,
) );
}
update_option( 'page_comments', true );
update_option( 'comments_per_page', 2 );
$_comments = array( get_comment( $comments[1] ), get_comment( $comments[3] ) );
// Populate `$wp_query->comments` in order to show that it doesn't override `$_comments`.
$this->go_to( get_permalink( $p ) );
get_echo( 'comments_template' );
$found = wp_list_comments( array(
'echo' => false,
'per_page' => 1,
'page' => 2,
), $_comments );
preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
$this->assertSame( array( $comments[3] ), array_map( 'intval', $matches[1] ) );
}
}