From 8f4d3126116be8b59ab61b113efcc804fe1d8518 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 18 Aug 2022 15:20:41 +0200 Subject: [PATCH 1/4] Comments block: Remove empty block wrapper --- packages/block-library/src/comments/index.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/comments/index.php b/packages/block-library/src/comments/index.php index 5df2ed550a21ec..c7e25835b4a23a 100644 --- a/packages/block-library/src/comments/index.php +++ b/packages/block-library/src/comments/index.php @@ -24,11 +24,6 @@ function render_block_core_comments( $attributes, $content, $block ) { global $post; - $is_legacy = 'core/post-comments' === $block->name || ! empty( $attributes['legacy'] ); - if ( ! $is_legacy ) { - return $block->render( array( 'dynamic' => false ) ); - } - $post_id = $block->context['postId']; if ( ! isset( $post_id ) ) { return ''; @@ -44,6 +39,11 @@ function render_block_core_comments( $attributes, $content, $block ) { return ''; } + $is_legacy = 'core/post-comments' === $block->name || ! empty( $attributes['legacy'] ); + if ( ! $is_legacy ) { + return $block->render( array( 'dynamic' => false ) ); + } + $post_before = $post; $post = get_post( $post_id ); setup_postdata( $post ); From dad7549a72f3537b9d4a517bff921445bfad68a1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 18 Aug 2022 15:33:28 +0200 Subject: [PATCH 2/4] Add explanatory comment --- packages/block-library/src/comments/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/comments/index.php b/packages/block-library/src/comments/index.php index c7e25835b4a23a..14fe6355a11a70 100644 --- a/packages/block-library/src/comments/index.php +++ b/packages/block-library/src/comments/index.php @@ -39,6 +39,7 @@ function render_block_core_comments( $attributes, $content, $block ) { return ''; } + // If this isn't the legacy block, we need to render the static version of this block. $is_legacy = 'core/post-comments' === $block->name || ! empty( $attributes['legacy'] ); if ( ! $is_legacy ) { return $block->render( array( 'dynamic' => false ) ); From 70e899472661d2f37b2d00af5bd26db089d590b3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 19 Aug 2022 11:52:41 +0200 Subject: [PATCH 3/4] Add unit test --- phpunit/blocks/render-comments-test.php | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 phpunit/blocks/render-comments-test.php diff --git a/phpunit/blocks/render-comments-test.php b/phpunit/blocks/render-comments-test.php new file mode 100644 index 00000000000000..933ce66a212ce5 --- /dev/null +++ b/phpunit/blocks/render-comments-test.php @@ -0,0 +1,46 @@ + 'closed', + ); + self::$post_with_comments_disabled = self::factory()->post->create_and_get( $args ); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post_with_comments_disabled->ID, true ); + } + + /** + * @covers ::render_block_core_comments + */ + public function test_render_block_core_comments_empty_output_if_comments_disabled() { + $attributes = array(); + $parsed_blocks = parse_blocks( + '
' + ); + $parsed_block = $parsed_blocks[0]; + $context = array( 'postId' => self::$post_with_comments_disabled->ID ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + + $rendered = gutenberg_render_block_core_comments( $attributes, '', $block ); + $this->assertEmpty( $rendered ); + } +}; From 93af85e7750b48293b053b2e90e1a08aae683299 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 19 Aug 2022 14:47:58 +0200 Subject: [PATCH 4/4] Remove unnecessary function arg --- phpunit/blocks/render-comments-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/blocks/render-comments-test.php b/phpunit/blocks/render-comments-test.php index 933ce66a212ce5..112e161ba24b0f 100644 --- a/phpunit/blocks/render-comments-test.php +++ b/phpunit/blocks/render-comments-test.php @@ -17,7 +17,7 @@ class Tests_Blocks_RenderComments extends WP_UnitTestCase { */ protected static $post_with_comments_disabled; - public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + public static function wpSetUpBeforeClass() { $args = array( 'comment_status' => 'closed', );