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
This commit is contained in:
Tonya Mork 2022-05-03 14:31:12 +00:00
parent 4558de543c
commit 6dab53e93b
2 changed files with 60 additions and 1 deletions

View File

@ -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.

View File

@ -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(
'<!-- 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->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.