diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 5146c9f908..7b6e44c094 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1233,57 +1233,54 @@ function comments_template( $file = '/comments.php', $separate_comments = false $per_page = (int) get_option( 'comments_per_page' ); } - $flip_comment_order = $trim_comments_on_page = false; - if ( $post->comment_count > $per_page ) { - $comment_args['number'] = $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! - */ - $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; - } - } else { + /* + * 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; + $page = (int) get_query_var( 'cpage' ); + if ( 'newest' === get_option( 'default_comments_page' ) ) { + if ( $page ) { $comment_args['order'] = 'ASC'; - if ( $page ) { - $comment_args['offset'] = ( $page - 1 ) * $per_page; - } else { + + /* + * 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; + } + } else { + $comment_args['order'] = 'ASC'; + if ( $page ) { + $comment_args['offset'] = ( $page - 1 ) * $per_page; + } else { + $comment_args['offset'] = 0; } } $comment_query = new WP_Comment_Query( $comment_args ); $_comments = $comment_query->comments; - // Delightful pagination quirk #1: first page of results sometimes needs reordering. + // 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 ); } diff --git a/tests/phpunit/tests/comment/commentsTemplate.php b/tests/phpunit/tests/comment/commentsTemplate.php new file mode 100644 index 0000000000..81166f0ebc --- /dev/null +++ b/tests/phpunit/tests/comment/commentsTemplate.php @@ -0,0 +1,288 @@ +factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + + update_option( 'comment_order', 'asc' ); + update_option( 'default_comments_page', 'newest' ); + + $this->go_to( get_permalink( $p ) ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_1, $comment_2 ), $found_cids ); + } + + /** + * @ticket 8071 + */ + public function test_should_respect_comment_order_desc_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_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + + update_option( 'comment_order', 'desc' ); + update_option( 'default_comments_page', 'newest' ); + + $this->go_to( get_permalink( $p ) ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_2, $comment_1 ), $found_cids ); + } + + /** + * @ticket 8071 + */ + public function test_should_respect_comment_order_asc_when_default_comments_page_is_oldest() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + + update_option( 'comment_order', 'asc' ); + update_option( 'default_comments_page', 'oldest' ); + + $this->go_to( get_permalink( $p ) ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_1, $comment_2 ), $found_cids ); + } + + /** + * @ticket 8071 + */ + public function test_should_respect_comment_order_desc_when_default_comments_page_is_oldest() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + + update_option( 'comment_order', 'desc' ); + update_option( 'default_comments_page', 'oldest' ); + + $this->go_to( get_permalink( $p ) ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_2, $comment_1 ), $found_cids ); + } + + /** + * @ticket 8071 + */ + public function test_should_respect_comment_order_asc_when_default_comments_page_is_newest_on_subsequent_pages() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_post_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_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + $comment_4 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '4', + 'comment_post_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), + ) ); + + update_option( 'comment_order', 'asc' ); + update_option( 'default_comments_page', 'newest' ); + + $this->go_to( get_permalink( $p ) . '?cpage=2&comments_per_page=2' ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_3, $comment_4 ), $found_cids ); + } + + /** + * @ticket 8071 + */ + public function test_should_respect_comment_order_desc_when_default_comments_page_is_newest_on_subsequent_pages() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_post_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_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + $comment_4 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '4', + 'comment_post_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), + ) ); + + update_option( 'comment_order', 'desc' ); + update_option( 'default_comments_page', 'newest' ); + + $this->go_to( get_permalink( $p ) . '?cpage=2&comments_per_page=2' ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_4, $comment_3 ), $found_cids ); + } + + /** + * @ticket 8071 + */ + public function test_should_respect_comment_order_asc_when_default_comments_page_is_oldest_on_subsequent_pages() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_post_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_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + $comment_4 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '4', + 'comment_post_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), + ) ); + + update_option( 'comment_order', 'asc' ); + update_option( 'default_comments_page', 'oldest' ); + + $this->go_to( get_permalink( $p ) . '?cpage=2&comments_per_page=2' ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_1, $comment_2 ), $found_cids ); + } + + /** + * @ticket 8071 + */ + public function test_should_respect_comment_order_desc_when_default_comments_page_is_oldest_on_subsequent_pages() { + $now = time(); + $p = $this->factory->post->create(); + $comment_1 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_post_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_post_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_post_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + $comment_4 = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '4', + 'comment_post_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), + ) ); + + update_option( 'comment_order', 'desc' ); + update_option( 'default_comments_page', 'oldest' ); + + $this->go_to( get_permalink( $p ) . '?cpage=2&comments_per_page=2' ); + $found = get_echo( 'comments_template' ); + + // Life in the fast lane. + $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_2, $comment_1 ), $found_cids ); + } +}