From fe935b7e40eb9483654bea79b3ecae7f6185b20a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 21 Dec 2023 16:24:43 +0100 Subject: [PATCH 1/2] Block Hooks: Set hooked block's layout attribute based on anchor block's --- src/wp-includes/blocks.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 431e2b015332c..8a2bce8630492 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -771,9 +771,10 @@ function get_hooked_blocks() { * * @param array $anchor_block The anchor block. Passed by reference. * @param string $hooked_block_type The name of the hooked block type. + * @param array $attributes Optional. Attributes to pass to the hooked block. Default empty array. * @return string The markup for the given hooked block type, or an empty string if the block is ignored. */ -function get_hooked_block_markup( &$anchor_block, $hooked_block_type ) { +function get_hooked_block_markup( &$anchor_block, $hooked_block_type, $attributes = array() ) { if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); } @@ -786,7 +787,7 @@ function get_hooked_block_markup( &$anchor_block, $hooked_block_type ) { // However, its presence does not affect the frontend. $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type; - return get_comment_delimited_block_content( $hooked_block_type, array(), '' ); + return get_comment_delimited_block_content( $hooked_block_type, $attributes, '' ); } /** @@ -903,10 +904,28 @@ function make_after_block_visitor( $hooked_blocks, $context ) { ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] : array(); + if ( isset( $block['attrs']['layout'] ) ) { + $layout = $block['attrs']['layout']; + } + /** This filter is documented in wp-includes/blocks.php */ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); foreach ( $hooked_block_types as $hooked_block_type ) { - $markup .= get_hooked_block_markup( $block, $hooked_block_type ); + $attributes = array(); + + // Does the anchor block have a layout attribute? + if ( isset( $layout ) ) { + // Has the hooked block type opted into layout block support? + $hooked_block_type_obj = WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type ); + if ( $hooked_block_type_obj && $hooked_block_type_obj instanceof WP_Block_Type ) { + if( $hooked_block_type_obj->attributes && isset( $hooked_block_type_obj->attributes['layout'] ) ) { + // Copy the anchor block's layout attribute to the hooked block. + $attributes['layout'] = $layout; + } + } + } + + $markup .= get_hooked_block_markup( $block, $hooked_block_type, $attributes ); } if ( $parent_block && ! $next ) { From 4a337098dde0c4eb72cd9b74087a5ef1f13a2a7f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 21 Dec 2023 17:02:49 +0100 Subject: [PATCH 2/2] Whitespace --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 8a2bce8630492..aa4777054274a 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -918,7 +918,7 @@ function make_after_block_visitor( $hooked_blocks, $context ) { // Has the hooked block type opted into layout block support? $hooked_block_type_obj = WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type ); if ( $hooked_block_type_obj && $hooked_block_type_obj instanceof WP_Block_Type ) { - if( $hooked_block_type_obj->attributes && isset( $hooked_block_type_obj->attributes['layout'] ) ) { + if ( $hooked_block_type_obj->attributes && isset( $hooked_block_type_obj->attributes['layout'] ) ) { // Copy the anchor block's layout attribute to the hooked block. $attributes['layout'] = $layout; }