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

Block Hooks: Issue warning about child insertion into semi-incompatible blocks #5810

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ function make_before_block_visitor( $hooked_blocks, $context ) {
// Candidate for first-child insertion.
$relative_position = 'first_child';
$anchor_block_type = $parent_block['blockName'];

$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
: array();
Expand All @@ -847,6 +848,24 @@ function make_before_block_visitor( $hooked_blocks, $context ) {
foreach ( $hooked_block_types as $hooked_block_type ) {
$markup .= get_hooked_block_markup( $parent_block, $hooked_block_type );
}

// See https://make.wordpress.org/core/2023/10/15/introducing-block-hooks-for-dynamic-blocks/
// > Blocks cannot be hooked into post content or patterns crafted by the user, such as
// > synced patterns or theme templates and template parts that the user has modified.
$problematic_anchor_blocks_for_child_insertion = array(
'core/pattern',
'core/post-content',
'core/template-part',
);
if ( in_array( $anchor_block_type, $problematic_anchor_blocks_for_child_insertion, true ) ) {
// Display warning:
// The following block(s) are being inserted into the `first_child` position of $anchor_block_type:
// $hooked_block_types
// Note that due to the nature of the $anchor_block_type block, the hooked block(s) cannot be displayed in the
// Site Editor. Consider using `before` insertion instead.

// Inserting $hooked_block_types into `first_child` position of $anchor_block_type.
}
}

$relative_position = 'before';
Expand Down
Loading