From 3d4ede082b1cb6e8850de464f6fec211560a30e9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 19 Feb 2024 15:48:47 +0100 Subject: [PATCH 1/4] Block Hooks: Add new hooked_block filter --- src/wp-includes/blocks.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 7931061086065..fd0da25d94e97 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -892,6 +892,21 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke 'innerContent' => array(), ); + /** + * Filters the parsed block array for a given hooked block. + * + * @since 6.5.0 + * + * @param array $parsed_hooked_block The parsed block array for the given hooked block type. + * @param string $hooked_block_type The hooked block type name. + * @param string $relative_position The relative position of the hooked block. + * @param array $parsed_anchor_block The anchor block, in parsed block array format. + * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, + * or pattern that the anchor block belongs to. + */ + $parsed_hooked_block = apply_filters( "hooked_block", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); + + /** * Filters the parsed block array for a given hooked block. * @@ -900,15 +915,16 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @since 6.5.0 * * @param array $parsed_hooked_block The parsed block array for the given hooked block type. + * @param string $hooked_block_type The hooked block type name. * @param string $relative_position The relative position of the hooked block. * @param array $parsed_anchor_block The anchor block, in parsed block array format. * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, * or pattern that the anchor block belongs to. */ - $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context ); + $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); - // It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type, - // so we explicitly look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata. + // It's possible that the filter returned a block of a different type, so we explicitly + // look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata. if ( ! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) || ! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) From b4e491804663175d291c26e41e36850176bd17da Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 19 Feb 2024 17:10:29 +0100 Subject: [PATCH 2/4] Coding Standards --- src/wp-includes/blocks.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index fd0da25d94e97..81207da795707 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -904,8 +904,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, * or pattern that the anchor block belongs to. */ - $parsed_hooked_block = apply_filters( "hooked_block", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); - + $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); /** * Filters the parsed block array for a given hooked block. From 7230597ebfa2556a6afca932941247ba8bb0f57d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 19 Feb 2024 17:13:21 +0100 Subject: [PATCH 3/4] Fix test --- tests/phpunit/tests/blocks/insertHookedBlocks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index 945466e61de8d..a0fd1f8b85c9e 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -110,7 +110,7 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() { 'innerContent' => array(), ); - $filter = function ( $parsed_hooked_block, $relative_position, $parsed_anchor_block ) { + $filter = function ( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block ) { // Is the hooked block adjacent to the anchor block? if ( 'before' !== $relative_position && 'after' !== $relative_position ) { return $parsed_hooked_block; @@ -124,9 +124,9 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() { return $parsed_hooked_block; }; - add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 ); + add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 4 ); $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); - remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 ); + remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 4 ); $this->assertSame( '', From 13b9e8a9081024c621b359d678803b15c8c43e4f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 19 Feb 2024 17:13:52 +0100 Subject: [PATCH 4/4] Remove unneded argument --- tests/phpunit/tests/blocks/insertHookedBlocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index a0fd1f8b85c9e..6c2dec0f07734 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -126,7 +126,7 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() { }; add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 4 ); $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); - remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 4 ); + remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10 ); $this->assertSame( '', @@ -172,7 +172,7 @@ public function test_insert_hooked_blocks_filter_can_wrap_block() { }; add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 ); $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); - remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 ); + remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10 ); $this->assertSame( '
',