From 1880b466c795c26ec05824b93f6621518e779f9e Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 13 Jan 2016 03:26:31 +0000 Subject: [PATCH] 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 --- src/wp-includes/comment-template.php | 109 ++++++++++-------- .../phpunit/tests/comment/wpListComments.php | 36 ++++++ 2 files changed, 98 insertions(+), 47 deletions(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 0bee2474e0..49dc18870e 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1910,27 +1910,6 @@ function wp_list_comments( $args = array(), $comments = null ) { */ $r = apply_filters( 'wp_list_comments_args', $r ); - /* - * 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. - */ - if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) { - $current_cpage = get_query_var( 'cpage' ); - if ( ! $current_cpage ) { - $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages; - } - - $current_per_page = get_query_var( 'comments_per_page' ); - if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) { - $comments = get_comments( array( - 'post_id' => get_queried_object_id(), - 'orderby' => 'comment_date_gmt', - 'order' => 'ASC', - 'status' => 'all', - ) ); - } - } - // Figure out what comments we'll be looping through ($_comments) if ( null !== $comments ) { $comments = (array) $comments; @@ -1945,34 +1924,70 @@ function wp_list_comments( $args = array(), $comments = null ) { $_comments = $comments; } } else { - if ( empty($wp_query->comments) ) - return; - if ( 'all' != $r['type'] ) { - if ( empty($wp_query->comments_by_type) ) - $wp_query->comments_by_type = separate_comments($wp_query->comments); - if ( empty($wp_query->comments_by_type[$r['type']]) ) - return; - $_comments = $wp_query->comments_by_type[$r['type']]; - } else { - $_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. - } elseif ( $cpage == 1 ) { - $r['cpage'] = ''; - } else { - $r['cpage'] = $cpage; + /* + * 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. + */ + if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) { + $current_cpage = get_query_var( 'cpage' ); + if ( ! $current_cpage ) { + $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages; } - $r['page'] = 0; - $r['per_page'] = 0; + $current_per_page = get_query_var( 'comments_per_page' ); + if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) { + $comments = get_comments( array( + 'post_id' => get_queried_object_id(), + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'all', + ) ); + + 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; + } + } + + // Otherwise, fall back on the comments from `$wp_query->comments`. + } else { + if ( empty($wp_query->comments) ) + return; + if ( 'all' != $r['type'] ) { + if ( empty($wp_query->comments_by_type) ) + $wp_query->comments_by_type = separate_comments($wp_query->comments); + if ( empty($wp_query->comments_by_type[$r['type']]) ) + return; + $_comments = $wp_query->comments_by_type[$r['type']]; + } else { + $_comments = $wp_query->comments; + } + + 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. + */ + } elseif ( $cpage == 1 ) { + $r['cpage'] = ''; + } else { + $r['cpage'] = $cpage; + } + + $r['page'] = 0; + $r['per_page'] = 0; + } } } diff --git a/tests/phpunit/tests/comment/wpListComments.php b/tests/phpunit/tests/comment/wpListComments.php index 84f6e4c517..d8214f4f1c 100644 --- a/tests/phpunit/tests/comment/wpListComments.php +++ b/tests/phpunit/tests/comment/wpListComments.php @@ -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] ) ); + } }