From b68aeac7f45c8435c78306561e3a8abdcb2ab1f4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 1 Nov 2023 17:35:30 +0100 Subject: [PATCH 1/5] Block Hooks: Extract insert_hooked_blocks() function --- src/wp-includes/blocks.php | 87 ++++++++++++++------------------------ 1 file changed, 31 insertions(+), 56 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 1dc1e66230a97..8e6b7cc17034d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -757,6 +757,33 @@ function get_hooked_blocks() { return $hooked_blocks; } +function insert_hooked_blocks( &$block, $relative_position, &$anchor_block, $hooked_blocks, $context ) { + $anchor_block_type = $anchor_block['blockName']; + $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) + ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] + : array(); + + /** + * Filters the list of hooked block types for a given anchor block type and relative position. + * + * @since 6.4.0 + * + * @param string[] $hooked_block_types The list of hooked block types. + * @param string $relative_position The relative position of the hooked blocks. + * Can be one of 'before', 'after', 'first_child', or 'last_child'. + * @param string $anchor_block_type The anchor block type. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + */ + $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); + + $markup = ''; + foreach ( $hooked_block_types as $hooked_block_type ) { + $markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' ); + } + + return $markup; +} + /** * Returns a function that injects the theme attribute into, and hooked blocks before, a given block. * @@ -794,40 +821,10 @@ function make_before_block_visitor( $hooked_blocks, $context ) { if ( $parent_block && ! $prev ) { // 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(); - - /** - * Filters the list of hooked block types for a given anchor block type and relative position. - * - * @since 6.4.0 - * - * @param string[] $hooked_block_types The list of hooked block types. - * @param string $relative_position The relative position of the hooked blocks. - * Can be one of 'before', 'after', 'first_child', or 'last_child'. - * @param string $anchor_block_type The anchor block type. - * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. - */ - $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_comment_delimited_block_content( $hooked_block_type, array(), '' ); - } + $markup .= insert_hooked_blocks( $block, 'first_child', $parent_block, $hooked_blocks, $context ); } - $relative_position = 'before'; - $anchor_block_type = $block['blockName']; - $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) - ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] - : array(); - - /** 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_comment_delimited_block_content( $hooked_block_type, array(), '' ); - } + $markup .= insert_hooked_blocks( $block, 'before', $block, $hooked_blocks, $context ); return $markup; }; @@ -863,33 +860,11 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it. */ return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) { - $markup = ''; - - $relative_position = 'after'; - $anchor_block_type = $block['blockName']; - $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) - ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] - : array(); - - /** 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_comment_delimited_block_content( $hooked_block_type, array(), '' ); - } + $markup = insert_hooked_blocks( $block, 'after', $block, $hooked_blocks, $context ); if ( $parent_block && ! $next ) { // Candidate for last-child insertion. - $relative_position = 'last_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(); - - /** 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_comment_delimited_block_content( $hooked_block_type, array(), '' ); - } + $markup .= insert_hooked_blocks( $block, 'last_child', $parent_block, $hooked_blocks, $context ); } return $markup; From a258a01b53fafccfb6309ce138f2e984ab590723 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 1 Nov 2023 17:39:51 +0100 Subject: [PATCH 2/5] Remove obsolete block argument --- src/wp-includes/blocks.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 8e6b7cc17034d..060360fc08c6b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -757,7 +757,7 @@ function get_hooked_blocks() { return $hooked_blocks; } -function insert_hooked_blocks( &$block, $relative_position, &$anchor_block, $hooked_blocks, $context ) { +function insert_hooked_blocks( $relative_position, &$anchor_block, $hooked_blocks, $context ) { $anchor_block_type = $anchor_block['blockName']; $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] @@ -821,10 +821,10 @@ function make_before_block_visitor( $hooked_blocks, $context ) { if ( $parent_block && ! $prev ) { // Candidate for first-child insertion. - $markup .= insert_hooked_blocks( $block, 'first_child', $parent_block, $hooked_blocks, $context ); + $markup .= insert_hooked_blocks( 'first_child', $parent_block, $hooked_blocks, $context ); } - $markup .= insert_hooked_blocks( $block, 'before', $block, $hooked_blocks, $context ); + $markup .= insert_hooked_blocks( 'before', $block, $hooked_blocks, $context ); return $markup; }; @@ -860,11 +860,11 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it. */ return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) { - $markup = insert_hooked_blocks( $block, 'after', $block, $hooked_blocks, $context ); + $markup = insert_hooked_blocks( 'after', $block, $hooked_blocks, $context ); if ( $parent_block && ! $next ) { // Candidate for last-child insertion. - $markup .= insert_hooked_blocks( $block, 'last_child', $parent_block, $hooked_blocks, $context ); + $markup .= insert_hooked_blocks( 'last_child', $parent_block, $hooked_blocks, $context ); } return $markup; From 09bff63c3251cd3eb2dba1f91421d35ddb5244c0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 14 Nov 2023 17:50:10 +0500 Subject: [PATCH 3/5] Change argument order --- src/wp-includes/blocks.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 060360fc08c6b..abae7fba15b8e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -757,7 +757,7 @@ function get_hooked_blocks() { return $hooked_blocks; } -function insert_hooked_blocks( $relative_position, &$anchor_block, $hooked_blocks, $context ) { +function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_blocks, $context ) { $anchor_block_type = $anchor_block['blockName']; $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] @@ -821,10 +821,10 @@ function make_before_block_visitor( $hooked_blocks, $context ) { if ( $parent_block && ! $prev ) { // Candidate for first-child insertion. - $markup .= insert_hooked_blocks( 'first_child', $parent_block, $hooked_blocks, $context ); + $markup .= insert_hooked_blocks( $parent_block, 'first_child', $hooked_blocks, $context ); } - $markup .= insert_hooked_blocks( 'before', $block, $hooked_blocks, $context ); + $markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context ); return $markup; }; @@ -860,11 +860,11 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it. */ return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) { - $markup = insert_hooked_blocks( 'after', $block, $hooked_blocks, $context ); + $markup = insert_hooked_blocks( $block, 'after', $hooked_blocks, $context ); if ( $parent_block && ! $next ) { // Candidate for last-child insertion. - $markup .= insert_hooked_blocks( 'last_child', $parent_block, $hooked_blocks, $context ); + $markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context ); } return $markup; From 19ff5d51e0f4719d5affd41f21ac7888af64744b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Nov 2023 16:10:39 +0100 Subject: [PATCH 4/5] Add PHPDoc --- src/wp-includes/blocks.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index abae7fba15b8e..88cfe0c0717b7 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -757,6 +757,18 @@ function get_hooked_blocks() { return $hooked_blocks; } +/** + * Returns the markup for blocks hooked to the given anchor block in a specific relative position. + * + * @since 6.5.0 + * + * @param array $anchor_block The anchor block. + * @param string $relative_position The relative position of the hooked blocks. + * Can be one of 'before', 'after', 'first_child', or 'last_child'. + * @param array $hooked_blocks An array of blocks hooked to the given anchor block. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + * @return string + */ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_blocks, $context ) { $anchor_block_type = $anchor_block['blockName']; $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) @@ -768,11 +780,11 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block * * @since 6.4.0 * - * @param string[] $hooked_block_types The list of hooked block types. - * @param string $relative_position The relative position of the hooked blocks. - * Can be one of 'before', 'after', 'first_child', or 'last_child'. - * @param string $anchor_block_type The anchor block type. - * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + * @param string[] $hooked_block_types The list of hooked block types. + * @param string $relative_position The relative position of the hooked blocks. + * Can be one of 'before', 'after', 'first_child', or 'last_child'. + * @param string $anchor_block_type The anchor block type. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. */ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); From 0b0f7cb9970d44a876cff350bba6dd110a030977 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Nov 2023 16:50:57 +0100 Subject: [PATCH 5/5] Add basic test coverage for insert_hooked_blocks --- .../tests/blocks/insertHookedBlocks.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/phpunit/tests/blocks/insertHookedBlocks.php diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php new file mode 100644 index 0000000000000..6af6061de1f07 --- /dev/null +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -0,0 +1,34 @@ + $anchor_block_name, + ); + + // Maybe move to class level and include other relative positions? + // And/or data provider? + $hooked_blocks = array( + $anchor_block_name => array( + 'after' => array( 'tests/hooked-before' ), + ) + ); + + $actual = insert_hooked_blocks( $anchor_block, 'after', $hooked_blocks, array() ); + $this->assertSame( '', $actual ); + } +}