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.
Built from https://develop.svn.wordpress.org/trunk@34729


git-svn-id: http://core.svn.wordpress.org/trunk@34693 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-10-01 03:11:30 +00:00
parent 594a208753
commit dd9709d179
2 changed files with 16 additions and 60 deletions

View File

@ -1240,74 +1240,30 @@ function comments_template( $file = '/comments.php', $separate_comments = false
$per_page = (int) get_option( 'comments_per_page' ); $per_page = (int) get_option( 'comments_per_page' );
} }
/* $comment_args['order'] = 'ASC';
* 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['number'] = $per_page; $comment_args['number'] = $per_page;
$page = (int) get_query_var( 'cpage' ); $page = (int) get_query_var( 'cpage' );
if ( 'newest' === get_option( 'default_comments_page' ) ) {
if ( $page ) {
$comment_args['order'] = 'ASC';
/* if ( $page ) {
* We don't have enough data (namely, the total number of comments) to calculate an $comment_args['offset'] = ( $page - 1 ) * $per_page;
* exact offset. We'll fetch too many comments, and trim them as needed } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
* after the query. $comment_args['offset'] = 0;
*/
$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;
}
} else { } else {
$comment_args['order'] = 'ASC'; // If fetching the first page of 'newest', we need a top-level comment count.
if ( $page ) { $top_level_query = new WP_Comment_Query();
$comment_args['offset'] = ( $page - 1 ) * $per_page; $top_level_count = $top_level_query->query( array(
} else { 'count' => true,
$comment_args['offset'] = 0; '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 ); $comment_query = new WP_Comment_Query( $comment_args );
$_comments = $comment_query->comments; $_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. // Trees must be flattened before they're passed to the walker.
$comments_flat = array(); $comments_flat = array();
foreach ( $_comments as $_comment ) { foreach ( $_comments as $_comment ) {

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-alpha-34728'; $wp_version = '4.4-alpha-34729';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.