Skip to content

Commit

Permalink
Comment Template Block: Retain inner blocks inserted via render_block…
Browse files Browse the repository at this point in the history
…_data filter (#50883)

Add a unit test to verify that an inner block added to a given Comment Template block via the `render_block_data` filter is retained at `render_block` time.  Then, change the block's code to actually retain those modifications from `render_block_data`.
  • Loading branch information
ockham authored May 30, 2023
1 parent 2bb724a commit 660b34c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/block-library/src/comment-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,20 @@ function block_core_comment_template_render_comments( $comments, $block ) {
$context['commentId'] = $comment_id;
return $context;
};

/*
* We set commentId context through the `render_block_context` filter so
* that dynamically inserted blocks (at `render_block` filter stage)
* will also receive that context.
*/
add_filter( 'render_block_context', $filter_block_context );
$block_content = $block->render( array( 'dynamic' => false ) );

/*
* We construct a new WP_Block instance from the parsed block so that
* it'll receive any changes made by the `render_block_data` filter.
*/
$block_content = ( new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) );

remove_filter( 'render_block_context', $filter_block_context );

$children = $comment->get_children();
Expand Down
51 changes: 51 additions & 0 deletions phpunit/blocks/render-comment-template-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,55 @@ public function test_rendering_comment_template_sets_comment_id_context() {
"Rendered markup doesn't contain Comment Author Name block."
);
}

public function test_inner_block_inserted_by_render_block_data_is_retained() {
$render_block_callback = new MockAction();
add_filter( 'render_block', array( $render_block_callback, 'filter' ), 10, 3 );

$render_block_data_callback = static function( $parsed_block ) {
// Add a Social Links block to a Comment Template block's inner blocks.
if ( 'core/comment-template' === $parsed_block['blockName'] ) {
$inserted_block_markup = <<<END
<!-- wp:social-links -->
<ul class="wp-block-social-links"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /--></ul>
<!-- /wp:social-links -->'
END;

$inserted_blocks = parse_blocks( $inserted_block_markup );

$parsed_block['innerBlocks'][] = $inserted_blocks[0];
}
return $parsed_block;
};

add_filter( 'render_block_data', $render_block_data_callback, 10, 1 );
$parsed_blocks = parse_blocks(
'<!-- wp:comments --><!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template --><!-- /wp:comments -->'
);
$block = new WP_Block(
$parsed_blocks[0],
array(
'postId' => self::$custom_post->ID,
)
);
$block->render();
remove_filter( 'render_block_data', $render_block_data_callback );

$this->assertSame( 5, $render_block_callback->get_call_count() );

$args = $render_block_callback->get_args();
$this->assertSame( 'core/comment-content', $args[0][2]->name );
$this->assertSame( 'core/comment-template', $args[1][2]->name );
$this->assertCount( 2, $args[1][2]->inner_blocks, "Inner block inserted by render_block_data filter wasn't retained." );
$this->assertInstanceOf(
'WP_Block',
$args[1][2]->inner_blocks[1],
"Inner block inserted by render_block_data isn't a WP_Block class instance."
);
$this->assertSame(
'core/social-links',
$args[1][2]->inner_blocks[1]->name,
"Inner block inserted by render_block_data isn't named as expected."
);
}
}

0 comments on commit 660b34c

Please sign in to comment.