Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments block: Remove empty block wrapper #43383

Merged
merged 4 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/block-library/src/comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand All @@ -44,6 +39,12 @@ 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 ) );
}

$post_before = $post;
$post = get_post( $post_id );
setup_postdata( $post );
Expand Down
46 changes: 46 additions & 0 deletions phpunit/blocks/render-comments-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Comments block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Comments block.
*
* @group blocks
*/
class Tests_Blocks_RenderComments extends WP_UnitTestCase {
/**
* @var WP_Post
*/
protected static $post_with_comments_disabled;

public static function wpSetUpBeforeClass() {
$args = array(
'comment_status' => '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(
'<!-- wp:comments --><div class="wp-block-comments"><!-- wp:comments-title /--></div><!-- /wp:comments -->'
);
$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 );
}
};