From 6dab53e93bb948a59e48b5856208946d9ee83e74 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 3 May 2022 14:31:12 +0000 Subject: [PATCH] Editor: Sets 'paged' query arg only when there are comments: `build_comment_query_vars_from_block()`. A SQL syntax error happened when a post has no comments and "Break comments into pages" is checked in Settings > Discussion. The fix sets the `'paged'` query arg only when there are comments. When there are no comments, `WP_Comment_Query` sets the default `'paged'` value to `1`. Props bernhard-reiter, luisherranz, czapla, cbravobernal, davidbaumwald, hellofromTonya. Follow-up to [53142], [53138]. Fixes #55658. git-svn-id: https://develop.svn.wordpress.org/trunk@53336 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 5 +- .../tests/blocks/renderCommentTemplate.php | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index adaf1ae255..9df70abaf4 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1325,7 +1325,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/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 8f70fff000..4199537f0c 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -137,6 +137,62 @@ class Tests_Blocks_RenderReusableCommentTemplate extends WP_UnitTestCase { ); } + /** + * 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. + * + * @ticket 55658 + * @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->assertSameSetsWithIndex( + 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 that both "Older Comments" and "Newer Comments" are displayed in the correct order * inside the Comment Query Loop when we enable pagination on Discussion Settings.