diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 93ff0f7aaa..49b26cd5b6 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1287,12 +1287,22 @@ add_filter( 'block_type_metadata', '_wp_multiple_block_styles' ); function build_comment_query_vars_from_block( $block ) { $comment_args = array( - 'orderby' => 'comment_date_gmt', - 'order' => 'ASC', - 'status' => 'approve', - 'no_found_rows' => false, + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'no_found_rows' => false, ); + if ( is_user_logged_in() ) { + $comment_args['include_unapproved'] = array( get_current_user_id() ); + } else { + $unapproved_email = wp_get_unapproved_comment_author_email(); + + if ( $unapproved_email ) { + $comment_args['include_unapproved'] = array( $unapproved_email ); + } + } + if ( ! empty( $block->context['postId'] ) ) { $comment_args['post_id'] = (int) $block->context['postId']; } diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 6267838904..456e5ec145 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -250,4 +250,46 @@ class Tests_Blocks_RenderReusableCommentTemplate extends WP_UnitTestCase { $block->render() ); } + + /** + * Test that unapproved comments are included if it is a preview. + * + * @ticket 55634 + * @covers ::build_comment_query_vars_from_block + */ + function test_build_comment_query_vars_from_block_with_comment_preview() { + $parsed_blocks = parse_blocks( + '' + ); + + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$custom_post->ID, + ) + ); + + $commenter_filter = function () { + return array( + 'comment_author_email' => 'unapproved@example.org', + ); + }; + + add_filter( 'wp_get_current_commenter', $commenter_filter ); + + $this->assertEquals( + build_comment_query_vars_from_block( $block ), + array( + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'no_found_rows' => false, + 'include_unapproved' => array( 'unapproved@example.org' ), + 'post_id' => self::$custom_post->ID, + 'hierarchical' => 'threaded', + 'number' => 5, + 'paged' => 1, + ) + ); + } }