Skip to content

Commit

Permalink
Comment Template: With pagination, make sure to request page 1 if the…
Browse files Browse the repository at this point in the history
…re are no comments (#40759)

Co-authored-by: Tonya Mork <[email protected]>
  • Loading branch information
ockham and Tonya Mork authored May 3, 2022
1 parent 690326d commit 2e7faf2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/compat/wordpress-6.0/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ function build_comment_query_vars_from_block( $block ) {
} elseif ( 'oldest' === $default_page ) {
$comment_args['paged'] = 1;
} elseif ( 'newest' === $default_page ) {
$comment_args['paged'] = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
$max_num_pages = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
if ( 0 !== $max_num_pages ) {
$comment_args['paged'] = $max_num_pages;
}
}
// Set the `cpage` query var to ensure the previous and next pagination links are correct
// when inheriting the Discussion Settings.
Expand Down
54 changes: 54 additions & 0 deletions phpunit/class-block-library-comment-template-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,60 @@ function test_build_comment_query_vars_from_block_no_context() {
);
}

/**
* Test that if pagination is set to display the last page by default (i.e. newest comments),
* the query is set to look for page 1 (rather than page 0, which would cause an error).
*
* Regression: https://github.com/WordPress/gutenberg/issues/40758.
*
* @covers ::build_comment_query_vars_from_block
*/
function test_build_comment_query_vars_from_block_pagination_with_no_comments() {
$comments_per_page = get_option( 'comments_per_page' );
$default_comments_page = get_option( 'default_comments_page' );

update_option( 'comments_per_page', 50 );
update_option( 'previous_default_page', 'newest' );

$post_without_comments = self::factory()->post->create_and_get(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_name' => 'fluffycat',
'post_title' => 'Fluffy Cat',
'post_content' => 'Fluffy Cat content',
'post_excerpt' => 'Fluffy Cat',
)
);

$parsed_blocks = parse_blocks(
'<!-- wp:comment-template --><!-- wp:comment-author-name /--><!-- wp:comment-content /--><!-- /wp:comment-template -->'
);

$block = new WP_Block(
$parsed_blocks[0],
array(
'postId' => $post_without_comments->ID,
)
);

$this->assertEquals(
array(
'orderby' => 'comment_date_gmt',
'order' => 'ASC',
'status' => 'approve',
'no_found_rows' => false,
'post_id' => $post_without_comments->ID,
'hierarchical' => 'threaded',
'number' => 50,
),
build_comment_query_vars_from_block( $block )
);

update_option( 'comments_per_page', $comments_per_page );
update_option( 'default_comments_page', $default_comments_page );
}

/**
* Test rendering a single comment
*/
Expand Down

0 comments on commit 2e7faf2

Please sign in to comment.