From 2e7faf2ae3641ccce58c148a0a2db0945bc01654 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 3 May 2022 17:55:27 +0200 Subject: [PATCH] Comment Template: With pagination, make sure to request page 1 if there are no comments (#40759) Co-authored-by: Tonya Mork --- lib/compat/wordpress-6.0/blocks.php | 5 +- ...ss-block-library-comment-template-test.php | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/blocks.php b/lib/compat/wordpress-6.0/blocks.php index 934a2fc8b87555..7a3f6b728eebc0 100644 --- a/lib/compat/wordpress-6.0/blocks.php +++ b/lib/compat/wordpress-6.0/blocks.php @@ -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. diff --git a/phpunit/class-block-library-comment-template-test.php b/phpunit/class-block-library-comment-template-test.php index c808ff5dc5c542..6e8a7ef0df311e 100644 --- a/phpunit/class-block-library-comment-template-test.php +++ b/phpunit/class-block-library-comment-template-test.php @@ -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( + '' + ); + + $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 */