From c64798e14518bfbbf34143eaef47ac84d6b1519d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 8 Dec 2023 16:02:23 +0100 Subject: [PATCH 1/2] Block Hooks: Issue warning about child insertion into semi-incompatible blocks --- src/wp-includes/blocks.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 431e2b015332c..3b5c8c42f1073 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -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(); @@ -847,6 +848,20 @@ 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 ); } + + $problematic_anchor_blocks_for_child_insertion = array( + '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'; From ee00e65d0a4d1c3547a58852be395fd0507e15c2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 8 Dec 2023 16:39:40 +0100 Subject: [PATCH 2/2] Add Pattern block to problematic notes, and a comment --- src/wp-includes/blocks.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 3b5c8c42f1073..392ba8d497234 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -849,7 +849,11 @@ function make_before_block_visitor( $hooked_blocks, $context ) { $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', );