Skip to content

Commit

Permalink
Comment Template Block: Set commentId context via filter (#50279)
Browse files Browse the repository at this point in the history
In the Comment Template block's render callback, instead of creating a new `WP_Block` instance with `commentId` context set, use the `render_block_context` filter to set that context and render the existing `WP_Block` instance.

This approach arguably follows our established patterns with regard to handling block context better. Notably, with the previous approach, we were only setting block context for the Comment Template block itself. In this PR, we extend it to apply to all child blocks, including ones that are dynamically inserted, e.g. via the `render_block` filter. This is relevant for Auto-inserting blocks (see #50103).
  • Loading branch information
ockham authored May 4, 2023
1 parent b65c5e9 commit 4409ba4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/block-library/src/comment-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ function block_core_comment_template_render_comments( $comments, $block ) {

$content = '';
foreach ( $comments as $comment ) {

$block_content = ( new WP_Block(
$block->parsed_block,
array(
'commentId' => $comment->comment_ID,
)
) )->render( array( 'dynamic' => false ) );
$comment_id = $comment->comment_ID;
$filter_block_context = static function( $context ) use ( $comment_id ) {
$context['commentId'] = $comment_id;
return $context;
};
add_filter( 'render_block_context', $filter_block_context );
$block_content = $block->render( array( 'dynamic' => false ) );
remove_filter( 'render_block_context', $filter_block_context );

$children = $comment->get_children();

Expand Down

1 comment on commit 4409ba4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 4409ba4.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4880863714
📝 Reported issues:

Please sign in to comment.