From f14dc03ad6ec9578cf51eb4587291794d6e23e0e Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 1 Oct 2015 03:10:13 +0000 Subject: [PATCH] Simplify pagination logic in `comments_template()`. [34561] "fixed" the problem of newest-first comments showing fewer than 'per_page' comments on the post permalink when the total number of comments was not divisible by 'per_page'. See #29462. But this fix caused numerous other problems. First, comment pages reported by `get_page_of_comment()` (which expects comment pages to be filled oldest-first) were no longer correct. Second, and more seriously, the new logic caused comments to be shifted between pages, making their permalinks non-permanent. The current changeset reverts the changed behavior. In order to preserve the performance improvements introduced in [34561], an additional query must be performed when 'default_comments_page=newest' and 'cpage=0' (ie, you're viewing the post permalink). A nice side effect of this revert is that we no longer need the hacks required to determine proper comment pagination, introduced in [34561]. See #8071. See #34073. git-svn-id: https://develop.svn.wordpress.org/trunk@34729 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 74 ++++------------ .../tests/comment/commentsTemplate.php | 84 +++++++++++++++++++ 2 files changed, 99 insertions(+), 59 deletions(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index bad4a14ea4..a0cf16dfcf 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1240,74 +1240,30 @@ function comments_template( $file = '/comments.php', $separate_comments = false $per_page = (int) get_option( 'comments_per_page' ); } - /* - * For legacy reasons, higher page numbers always mean more recent comments, regardless of sort order. - * Since we don't have full pagination info until after the query, we use some tricks to get the - * right comments for the current page. - * - * Abandon all hope, ye who enter here! - */ - $flip_comment_order = $trim_comments_on_page= false; + $comment_args['order'] = 'ASC'; $comment_args['number'] = $per_page; $page = (int) get_query_var( 'cpage' ); - if ( 'newest' === get_option( 'default_comments_page' ) ) { - if ( $page ) { - $comment_args['order'] = 'ASC'; - /* - * We don't have enough data (namely, the total number of comments) to calculate an - * exact offset. We'll fetch too many comments, and trim them as needed - * after the query. - */ - $offset = ( $page - 2 ) * $per_page; - if ( 0 > $offset ) { - // `WP_Comment_Query` doesn't support negative offsets. - $comment_args['offset'] = 0; - } else { - $comment_args['offset'] = $offset; - } - - // Fetch double the number of comments we need. - $comment_args['number'] += $per_page; - $trim_comments_on_page = true; - } else { - $comment_args['order'] = 'DESC'; - $comment_args['offset'] = 0; - $flip_comment_order = true; - } + if ( $page ) { + $comment_args['offset'] = ( $page - 1 ) * $per_page; + } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) { + $comment_args['offset'] = 0; } else { - $comment_args['order'] = 'ASC'; - if ( $page ) { - $comment_args['offset'] = ( $page - 1 ) * $per_page; - } else { - $comment_args['offset'] = 0; - } + // 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( + 'count' => true, + 'orderby' => false, + 'post_id' => $post->ID, + 'parent' => 0, + ) ); + + $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page; } $comment_query = new WP_Comment_Query( $comment_args ); $_comments = $comment_query->comments; - // Delightful pagination quirk #1: `wp_list_comments()` expects the order to be wrong, so we make it wrong. - if ( $flip_comment_order ) { - $_comments = array_reverse( $_comments ); - } - - // Delightful pagination quirk #2: reverse chronological order requires page shifting. - if ( $trim_comments_on_page ) { - // Correct the value of max_num_pages, which is wrong because we manipulated the per_page 'number'. - $comment_query->max_num_pages = ceil( $comment_query->found_comments / $per_page ); - - // Identify the number of comments that should appear on page 1. - $page_1_count = $comment_query->found_comments - ( ( $comment_query->max_num_pages - 1 ) * $per_page ); - - // Use that value to shift the matched comments. - if ( 1 === $page ) { - $_comments = array_slice( $_comments, 0, $page_1_count ); - } else { - $_comments = array_slice( $_comments, $page_1_count, $per_page ); - } - } - // Trees must be flattened before they're passed to the walker. $comments_flat = array(); foreach ( $_comments as $_comment ) { diff --git a/tests/phpunit/tests/comment/commentsTemplate.php b/tests/phpunit/tests/comment/commentsTemplate.php index f86f3658ae..b47b99b184 100644 --- a/tests/phpunit/tests/comment/commentsTemplate.php +++ b/tests/phpunit/tests/comment/commentsTemplate.php @@ -325,4 +325,88 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase { $found_cids = array_map( 'intval', $matches[1] ); $this->assertSame( array( $comment_1, $comment_2 ), $found_cids ); } + + /** + * @ticket 8071 + * @ticket 34073 + * @ticket 29462 + */ + public function test_last_page_of_comments_should_be_full_when_default_comment_page_is_newest() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), + ) ); + $comment_2 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '2', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + $comment_3 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '3', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + + update_option( 'default_comments_page', 'newest' ); + update_option( 'comment_order', 'desc' ); + + $link = add_query_arg( array( + 'cpage' => 1, + 'comments_per_page' => 2, + ), get_permalink( $p ) ); + + $this->go_to( $link ); + $found = get_echo( 'comments_template' ); + + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + + $this->assertSame( array( $comment_2, $comment_3 ), $found_cids ); + } + + /** + * @ticket 8071 + * @ticket 34073 + * @ticket 29462 + */ + public function test_first_page_of_comments_should_have_remainder_when_default_comments_page_is_newest() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), + ) ); + $comment_2 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '2', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + $comment_3 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '3', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + + update_option( 'default_comments_page', 'newest' ); + update_option( 'comment_order', 'desc' ); + + $link = add_query_arg( array( + 'cpage' => 2, + 'comments_per_page' => 2, + ), get_permalink( $p ) ); + + $this->go_to( $link ); + $found = get_echo( 'comments_template' ); + + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + + $this->assertSame( array( $comment_1 ), $found_cids ); + } }