From b5aaa445f9d43f22600d34db78fd76cd2fd6385e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 14:23:58 +0100 Subject: [PATCH 01/29] Don't set ignoredHookedBlocks metadata upon read --- src/wp-includes/blocks.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 7f88372f0f7c1..dc71c175c197f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -873,18 +873,13 @@ function get_hooked_blocks() { * @return string The markup for the given hooked block, or an empty string if the block is ignored. */ function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_block ) { - if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); - } - - if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) ) { + if ( + isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) && + in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) + ) { return ''; } - // The following is only needed for the REST API endpoint. - // However, its presence does not affect the frontend. - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type; - return serialize_block( $hooked_block ); } From 2e28810d83c088a7f3acfedf28df6604a84c2ede Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 14:27:45 +0100 Subject: [PATCH 02/29] Don't pass anchor_block by reference --- 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 dc71c175c197f..2dba1d0b9a639 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -869,10 +869,9 @@ function get_hooked_blocks() { * @param string $hooked_block_type The type of the hooked block. This could be different from * $hooked_block['blockName'], as a filter might've modified the latter. * @param array $anchor_block The anchor block, represented as a parsed block array. - * Passed by reference. * @return string The markup for the given hooked block, or an empty string if the block is ignored. */ -function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_block ) { +function get_hooked_block_markup( $hooked_block, $hooked_block_type, $anchor_block ) { if ( isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) && in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) From 40fa3fb427ff610688a8f7e4b6789059f4be9bd2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 14:28:30 +0100 Subject: [PATCH 03/29] Update PHPDoc --- 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 2dba1d0b9a639..476a27676655f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -853,7 +853,7 @@ function get_hooked_blocks() { /** * Conditionally returns the markup for a given hooked block. * - * Accepts three arguments: A hooked block, its type, and a reference to an anchor block. + * Accepts three arguments: A hooked block, its type, and an anchor block. * If the anchor block has already been processed, and the given hooked block type is in the list * of ignored hooked blocks, an empty string is returned. * From 1f0d0878199667226f0040f72a80afebcbc92a37 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 14:35:40 +0100 Subject: [PATCH 04/29] Add new set_ignored_hooked_blocks_metadata() function --- src/wp-includes/blocks.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 476a27676655f..22a0d044c97e6 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -850,6 +850,36 @@ function get_hooked_blocks() { return $hooked_blocks; } + +/** + * Adds a given block type to the list of an anchor block's ignored hooked block types. + * + * Given an anchor block and a hooked block type, add the hooked block type to the list + * of ignored hooked block types stored in the anchor block's `metadata` attribute. + * + * This function is meant for internal use only. + * + * @since 6.5.0 + * @access private + * + * @param array $anchor_block The anchor block, represented as a parsed block array. + * @param string $hooked_block_type The type of the hooked block. + * @return array The anchor block, with the hooked block type added to its `metadata.ignoredHookedBlocks` + * attribute. + */ +function set_ignored_hooked_blocks_metadata( $anchor_block, $hooked_block_type ) { + if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); + } + + if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) ) { + return $anchor_block; + } + + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type; + return $anchor_block; +} + /** * Conditionally returns the markup for a given hooked block. * From c434d751bb8e2b68e29811e07ee0b75c2a502edb Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 14:58:39 +0100 Subject: [PATCH 05/29] 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 22a0d044c97e6..845382fc1a998 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -867,7 +867,7 @@ function get_hooked_blocks() { * @return array The anchor block, with the hooked block type added to its `metadata.ignoredHookedBlocks` * attribute. */ -function set_ignored_hooked_blocks_metadata( $anchor_block, $hooked_block_type ) { +function set_ignored_hooked_blocks_metadata( $anchor_block, $hooked_block_type ) { if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); } From b63d367a6dee7845a0c36fd6e158b0be48a53678 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 15:27:48 +0100 Subject: [PATCH 06/29] Fix first batch of unit tests --- .../tests/blocks/getHookedBlockMarkup.php | 24 ++++--------- .../tests/blocks/insertHookedBlocks.php | 36 +++++-------------- 2 files changed, 14 insertions(+), 46 deletions(-) diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php index 6433e65c35628..4941aab104b9e 100644 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php @@ -22,20 +22,16 @@ class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase { * @ticket 59572 * @ticket 60008 * @ticket 60126 + * @ticket 60506 * * @covers ::get_hooked_block_markup */ - public function test_get_hooked_block_markup_adds_metadata() { + public function test_get_hooked_block_markup_returns_correct_markup() { $anchor_block = array( 'blockName' => 'tests/anchor-block', ); $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "Hooked block type wasn't added to ignoredHookedBlocks metadata." - ); $this->assertSame( '', $actual, @@ -47,10 +43,11 @@ public function test_get_hooked_block_markup_adds_metadata() { * @ticket 59572 * @ticket 60008 * @ticket 60126 + * @ticket 60506 * * @covers ::get_hooked_block_markup */ - public function test_get_hooked_block_markup_if_block_is_already_hooked() { + public function test_get_hooked_block_markup_if_block_is_ignored() { $anchor_block = array( 'blockName' => 'tests/anchor-block', 'attrs' => array( @@ -61,11 +58,6 @@ public function test_get_hooked_block_markup_if_block_is_already_hooked() { ); $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "ignoredHookedBlocks metadata shouldn't have been modified." - ); $this->assertSame( '', $actual, @@ -77,10 +69,11 @@ public function test_get_hooked_block_markup_if_block_is_already_hooked() { * @ticket 59572 * @ticket 60008 * @ticket 60126 + * @ticket 60506 * * @covers ::get_hooked_block_markup */ - public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() { + public function test_get_hooked_block_markup_if_other_block_is_ignored() { $other_hooked_block_type = 'tests/other-hooked-block'; $other_hooked_block = array( 'blockName' => $other_hooked_block_type, @@ -98,11 +91,6 @@ public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() { ); $actual = get_hooked_block_markup( $other_hooked_block, $other_hooked_block_type, $anchor_block ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE, $other_hooked_block_type ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "Newly hooked block should've been added to ignoredHookedBlocks metadata while retaining previously ignored one." - ); $this->assertSame( '', $actual, diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index c4762a661c225..945466e61de8d 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -25,20 +25,16 @@ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase { /** * @ticket 59572 * @ticket 60126 + * @ticket 60506 * * @covers ::insert_hooked_blocks */ - public function test_insert_hooked_blocks_adds_metadata() { + public function test_insert_hooked_blocks_returns_correct_markup() { $anchor_block = array( 'blockName' => self::ANCHOR_BLOCK_TYPE, ); $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "Hooked block type wasn't added to ignoredHookedBlocks metadata." - ); $this->assertSame( '', $actual, @@ -49,10 +45,11 @@ public function test_insert_hooked_blocks_adds_metadata() { /** * @ticket 59572 * @ticket 60126 + * @ticket 60506 * * @covers ::insert_hooked_blocks */ - public function test_insert_hooked_blocks_if_block_is_already_hooked() { + public function test_insert_hooked_blocks_if_block_is_ignored() { $anchor_block = array( 'blockName' => 'tests/anchor-block', 'attrs' => array( @@ -63,11 +60,6 @@ public function test_insert_hooked_blocks_if_block_is_already_hooked() { ); $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "ignoredHookedBlocks metadata shouldn't have been modified." - ); $this->assertSame( '', $actual, @@ -78,10 +70,11 @@ public function test_insert_hooked_blocks_if_block_is_already_hooked() { /** * @ticket 59572 * @ticket 60126 + * @ticket 60506 * * @covers ::insert_hooked_blocks */ - public function test_insert_hooked_blocks_adds_to_ignored_hooked_blocks() { + public function test_insert_hooked_blocks_if_other_block_is_ignored() { $anchor_block = array( 'blockName' => 'tests/anchor-block', 'attrs' => array( @@ -92,11 +85,6 @@ public function test_insert_hooked_blocks_adds_to_ignored_hooked_blocks() { ); $actual = insert_hooked_blocks( $anchor_block, 'before', self::HOOKED_BLOCKS, array() ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE, self::OTHER_HOOKED_BLOCK_TYPE ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "Newly hooked block should've been added to ignoredHookedBlocks metadata while retaining previously ignored one." - ); $this->assertSame( '', $actual, @@ -107,6 +95,7 @@ public function test_insert_hooked_blocks_adds_to_ignored_hooked_blocks() { /** * @ticket 59572 * @ticket 60126 + * @ticket 60506 * * @covers ::insert_hooked_blocks */ @@ -139,11 +128,6 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() { $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "Hooked block type wasn't added to ignoredHookedBlocks metadata." - ); $this->assertSame( '', $actual, @@ -154,6 +138,7 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() { /** * @ticket 59572 * @ticket 60126 + * @ticket 60506 * * @covers ::insert_hooked_blocks */ @@ -189,11 +174,6 @@ public function test_insert_hooked_blocks_filter_can_wrap_block() { $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 ); - $this->assertSame( - array( self::HOOKED_BLOCK_TYPE ), - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], - "Hooked block type wasn't added to ignoredHookedBlocks metadata." - ); $this->assertSame( '
', $actual, From 36c0fc3cab73f010cc7629714cd5f409bfc44364 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 15:34:11 +0100 Subject: [PATCH 07/29] Fix remaining unit tests --- tests/phpunit/tests/blocks/getHookedBlocks.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/getHookedBlocks.php b/tests/phpunit/tests/blocks/getHookedBlocks.php index 526b2e494f1a2..30fba1f2b5de1 100644 --- a/tests/phpunit/tests/blocks/getHookedBlocks.php +++ b/tests/phpunit/tests/blocks/getHookedBlocks.php @@ -136,6 +136,7 @@ public function test_get_hooked_blocks_matches_found() { /** * @ticket 59313 * @ticket 60008 + * @ticket 60506 * * @covers ::get_hooked_blocks * @covers ::get_block_file_template @@ -150,7 +151,7 @@ public function test_loading_template_with_hooked_blocks() { $template->content ); $this->assertStringContainsString( - '' + '' . '', $template->content ); @@ -167,6 +168,7 @@ public function test_loading_template_with_hooked_blocks() { /** * @ticket 59313 * @ticket 60008 + * @ticket 60506 * * @covers ::get_hooked_blocks * @covers ::get_block_file_template @@ -178,7 +180,7 @@ public function test_loading_template_part_with_hooked_blocks() { $this->assertStringContainsString( '' - . '', + . '', $template->content ); $this->assertStringNotContainsString( @@ -198,6 +200,7 @@ public function test_loading_template_part_with_hooked_blocks() { /** * @ticket 59313 * @ticket 60008 + * @ticket 60506 * * @covers ::get_hooked_blocks * @covers WP_Block_Patterns_Registry::get_registered @@ -218,7 +221,7 @@ public function test_loading_pattern_with_hooked_blocks() { $pattern['content'] ); $this->assertStringContainsString( - '' + '' . '
' . '', str_replace( array( "\n", "\t" ), '', $pattern['content'] ) From 2358241e95d80065a50d888fb3f434ac1f2c5a19 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 16:39:29 +0100 Subject: [PATCH 08/29] There can never be enough callbacks --- src/wp-includes/blocks.php | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 845382fc1a998..5106b3a46b6f3 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -987,15 +987,19 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * This function is meant for internal use only. * * @since 6.4.0 + * @since 6.5.0 Added $callback argument. * @access private * * @param array $hooked_blocks An array of blocks hooked to another given block. * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, * or pattern that the blocks belong to. + * @param callable $callback A function that will be called for each block to generate + * the markup for a given list of blocks that are hooked to it. + * Default: 'insert_hooked_blocks'. * @return callable A function that returns the serialized markup for the given block, * including the markup for any hooked blocks before it. */ -function make_before_block_visitor( $hooked_blocks, $context ) { +function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) { /** * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup. * @@ -1008,17 +1012,23 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * @param array $prev The previous sibling block of the given block. Default null. * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it. */ - return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) { + return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) { _inject_theme_attribute_in_template_part_block( $block ); $markup = ''; if ( $parent_block && ! $prev ) { // Candidate for first-child insertion. - $markup .= insert_hooked_blocks( $parent_block, 'first_child', $hooked_blocks, $context ); + $markup .= call_user_func_array( + $callback, + array( &$parent_block, 'first_child', $hooked_blocks, $context ) + ); } - $markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context ); + $markup .= call_user_func_array( + $callback, + array( &$block, 'before', $hooked_blocks, $context ) + ); return $markup; }; @@ -1034,15 +1044,19 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * This function is meant for internal use only. * * @since 6.4.0 + * @since 6.5.0 Added $callback argument. * @access private * * @param array $hooked_blocks An array of blocks hooked to another block. * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, * or pattern that the blocks belong to. + * @param callable $callback A function that will be called for each block to generate + * the markup for a given list of blocks that are hooked to it. + * Default: 'insert_hooked_blocks'. * @return callable A function that returns the serialized markup for the given block, * including the markup for any hooked blocks after it. */ -function make_after_block_visitor( $hooked_blocks, $context ) { +function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) { /** * Injects hooked blocks after the given block, and returns the serialized markup. * @@ -1054,12 +1068,18 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * @param array $next The next sibling block of the given block. Default null. * @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', $hooked_blocks, $context ); + return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) { + $markup = call_user_func_array( + $callback, + array( &$block, 'after', $hooked_blocks, $context ) + ); if ( $parent_block && ! $next ) { // Candidate for last-child insertion. - $markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context ); + $markup .= call_user_func_array( + $callback, + array( &$parent_block, 'last_child', $hooked_blocks, $context ) + ); } return $markup; From 03304739e526c6de3dd3e2239df1594fee4f158b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 17:11:27 +0100 Subject: [PATCH 09/29] Tweak set_ignored_hooked_blocks_metadata to match callback signature --- src/wp-includes/blocks.php | 46 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 5106b3a46b6f3..1a68052a8cdb3 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -850,34 +850,46 @@ function get_hooked_blocks() { return $hooked_blocks; } - /** - * Adds a given block type to the list of an anchor block's ignored hooked block types. - * - * Given an anchor block and a hooked block type, add the hooked block type to the list - * of ignored hooked block types stored in the anchor block's `metadata` attribute. + * Adds a list of hooked block types to an anchor block's ignored hooked block types. * * This function is meant for internal use only. * * @since 6.5.0 * @access private * - * @param array $anchor_block The anchor block, represented as a parsed block array. - * @param string $hooked_block_type The type of the hooked block. - * @return array The anchor block, with the hooked block type added to its `metadata.ignoredHookedBlocks` - * attribute. + * @param array $parsed_anchor_block The anchor block, in parsed block array format. + * @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 hooked block types, grouped by anchor block and relative position. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + * @return string An empty string. */ -function set_ignored_hooked_blocks_metadata( $anchor_block, $hooked_block_type ) { - if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); - } +function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) { + $anchor_block_type = $parsed_anchor_block['blockName']; + $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) + ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] + : array(); - if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) ) { - return $anchor_block; + /** 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 ); + if ( empty ( $hooked_block_types ) ) { + return ''; } - $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type; - return $anchor_block; + $previously_ignored_hooked_blocks = isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) + ? $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] + : array(); + + $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique( + array_merge( + $previously_ignored_hooked_blocks, + $hooked_block_types + ) + ); + + // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`). + return ''; } /** From e6eea6e8498415eec29135884c908496a5e9f0c5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 17:28:25 +0100 Subject: [PATCH 10/29] Wire it all up --- src/wp-includes/default-filters.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 2d9f5368d46c8..f55430032c8f6 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -752,4 +752,28 @@ add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 ); add_action( 'init', '_wp_register_default_font_collections' ); +// TODO: This should probably go somewhere else. +function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { + $before_block_visitor = null; + $after_block_visitor = null; + $hooked_blocks = get_hooked_blocks(); + if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { + // At this point, the post has already been created. + // We need to build the corresponding `WP_Block_Template` object. + // To that end, we need to prevent hooked blocks insertion from running again. + + // Suppress all hooked blocks getting inserted into the context. + add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); + $template = _build_block_template_result_from_post( $post ); + remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); + + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); + } + $blocks = parse_blocks( $template->content ); + $post->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); +} +add_action( 'rest_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 1 ); +// Do we need to add it separately for wp_template_part? + unset( $filter, $action ); From eb1daa1ecd1ec6c4a13bb358e901a7c6e578ce09 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 09:32:26 +0100 Subject: [PATCH 11/29] 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 1a68052a8cdb3..4715e254a976d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -873,7 +873,7 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po /** 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 ); - if ( empty ( $hooked_block_types ) ) { + if ( empty( $hooked_block_types ) ) { return ''; } From d057ece8ce7f05a41d32802b862cd1ef4e6b6df7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 10:30:02 +0100 Subject: [PATCH 12/29] Bail early --- src/wp-includes/default-filters.php | 31 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index f55430032c8f6..e88cff9a8bfd4 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -754,22 +754,23 @@ // TODO: This should probably go somewhere else. function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { - $before_block_visitor = null; - $after_block_visitor = null; - $hooked_blocks = get_hooked_blocks(); - if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { - // At this point, the post has already been created. - // We need to build the corresponding `WP_Block_Template` object. - // To that end, we need to prevent hooked blocks insertion from running again. - - // Suppress all hooked blocks getting inserted into the context. - add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); - $template = _build_block_template_result_from_post( $post ); - remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); - - $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); - $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); + $hooked_blocks = get_hooked_blocks(); + if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { + return; } + + // At this point, the post has already been created. + // We need to build the corresponding `WP_Block_Template` object. + // To that end, we need to prevent hooked blocks insertion from running again. + + // Suppress all hooked blocks getting inserted into the context. + add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); + $template = _build_block_template_result_from_post( $post ); + remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); + + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); + $blocks = parse_blocks( $template->content ); $post->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); } From ab6a8c983ae4e42b9aa00dea21f12119bd7a502e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 11:22:52 +0100 Subject: [PATCH 13/29] Actually update post --- src/wp-includes/default-filters.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index e88cff9a8bfd4..0b48da1db16b4 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -771,8 +771,16 @@ function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); - $blocks = parse_blocks( $template->content ); - $post->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + $blocks = parse_blocks( $template->content ); + $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + + wp_update_post( + array( + 'ID' => $post->ID, + 'post_content' => $content, + // Do we need to update post_filtered_content? + ) + ); } add_action( 'rest_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 1 ); // Do we need to add it separately for wp_template_part? From 998147fd6e3652eb6bb49d309716fb07a1f78845 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 11:23:03 +0100 Subject: [PATCH 14/29] Use correct action --- src/wp-includes/default-filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 0b48da1db16b4..e13f2db74995e 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -782,7 +782,7 @@ function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { ) ); } -add_action( 'rest_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 1 ); +add_action( 'rest_after_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); // Do we need to add it separately for wp_template_part? unset( $filter, $action ); From 12963440f57d4709d4e24181d89fd05f7e47f45c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 11:23:39 +0100 Subject: [PATCH 15/29] Add action for template parts --- src/wp-includes/default-filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index e13f2db74995e..fe94fe4f3f173 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -783,6 +783,6 @@ function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { ); } add_action( 'rest_after_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); -// Do we need to add it separately for wp_template_part? +add_action( 'rest_after_insert_wp_template_part', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); unset( $filter, $action ); From 27724fc6750c47b22b2a047d486a218424c2e5a4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 11:25:03 +0100 Subject: [PATCH 16/29] Add note on action vs filter --- src/wp-includes/default-filters.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index fe94fe4f3f173..3a7be49548307 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -782,6 +782,8 @@ function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { ) ); } +// It might be nice to use a filter instead of an action, but the `WP_REST_Templates_Controller` doesn't +// provide one (unlike e.g. `WP_REST_Posts_Controller`, which has `rest_pre_insert_{$this->post_type}`). add_action( 'rest_after_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); add_action( 'rest_after_insert_wp_template_part', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); From 012004bbf0808f8a70a9a9ef41fd3566a8225496 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 11:31:51 +0100 Subject: [PATCH 17/29] Clarify comment --- src/wp-includes/default-filters.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 3a7be49548307..a5f7359045585 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -760,10 +760,8 @@ function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { } // At this point, the post has already been created. - // We need to build the corresponding `WP_Block_Template` object. - // To that end, we need to prevent hooked blocks insertion from running again. - - // Suppress all hooked blocks getting inserted into the context. + // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor. + // To that end, we need to suppress hooked blocks from getting inserted into the template. add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); $template = _build_block_template_result_from_post( $post ); remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); From 27a2ec862b4ca1d61c160ebdc21e9b3103832964 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 11:41:49 +0100 Subject: [PATCH 18/29] Fix more unit tests --- .../block-templates/buildBlockTemplateResultFromPost.php | 4 ++-- tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php index 0d469abe88106..e7a81e9989b55 100644 --- a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php +++ b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php @@ -67,6 +67,7 @@ public function test_should_build_template_part() { /** * @ticket 59646 + * @ticket 60506 */ public function test_should_inject_hooked_block_into_template() { register_block_type( @@ -83,11 +84,11 @@ public function test_should_inject_hooked_block_into_template() { 'wp_template' ); $this->assertStringStartsWith( '', $template->content ); - $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template->content ); } /** * @ticket 59646 + * @ticket 60506 */ public function test_should_inject_hooked_block_into_template_part() { register_block_type( @@ -104,6 +105,5 @@ public function test_should_inject_hooked_block_into_template_part() { 'wp_template_part' ); $this->assertStringEndsWith( '', $template_part->content ); - $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template_part->content ); } } diff --git a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php index 500f9f93d0049..64991a849dfe5 100644 --- a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php +++ b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php @@ -344,6 +344,7 @@ public function test_get_all_registered_includes_theme_attribute() { * * @ticket 59476 * @ticket 60008 + * @ticket 60506 * * @covers WP_Block_Patterns_Registry::register * @covers WP_Block_Patterns_Registry::get_all_registered @@ -385,9 +386,7 @@ public function test_get_all_registered_includes_hooked_blocks() { $registered = $this->registry->get_all_registered(); $this->assertCount( 3, $registered ); $this->assertStringEndsWith( '', $registered[1]['content'] ); - $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $registered[1]['content'] ); $this->assertStringEndsWith( '', $registered[2]['content'] ); - $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $registered[2]['content'] ); } /** @@ -418,6 +417,7 @@ public function test_get_registered_includes_theme_attribute() { * * @ticket 59476 * @ticket 60008 + * @ticket 60506 * * @covers WP_Block_Patterns_Registry::register * @covers WP_Block_Patterns_Registry::get_registered @@ -446,7 +446,6 @@ public function test_get_registered_includes_hooked_blocks() { $pattern = $this->registry->get_registered( 'test/one' ); $this->assertStringStartsWith( '', $pattern['content'] ); - $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $pattern['content'] ); } /** From 12ccd0d455719cf9be5529ba4c94fafd3663648b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 11:54:45 +0100 Subject: [PATCH 19/29] Start adding test coverage for set_ignored_hooked_blocks_metadata --- .../blocks/setIgnoredHookedBlocksMetadata.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php diff --git a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php new file mode 100644 index 0000000000000..86860b91d1c53 --- /dev/null +++ b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php @@ -0,0 +1,33 @@ + 'tests/anchor-block', + ); + + $hooked_blocks = array( + 'tests/anchor-block' => array( + 'after' => array( 'tests/hooked-block' ), + ) + ); + + set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null ); + $this->assertSame( $anchor_block[ 'attrs' ][ 'metadata' ][ 'ignoredHookedBlocks' ], array( 'tests/hooked-block' ) ); + } +} From 1128b5f7d70fa4806f83e0248759cefa0681c295 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 12:16:53 +0100 Subject: [PATCH 20/29] Add more test coverage and a small fix --- src/wp-includes/blocks.php | 4 +-- .../blocks/setIgnoredHookedBlocksMetadata.php | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 4715e254a976d..71c5dfb8b70cd 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -877,8 +877,8 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po return ''; } - $previously_ignored_hooked_blocks = isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) - ? $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] + $previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) + ? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] : array(); $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique( diff --git a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php index 86860b91d1c53..88afca19697f9 100644 --- a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php +++ b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php @@ -30,4 +30,32 @@ public function test_set_ignored_hooked_blocks_metadata() { set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null ); $this->assertSame( $anchor_block[ 'attrs' ][ 'metadata' ][ 'ignoredHookedBlocks' ], array( 'tests/hooked-block' ) ); } + + /** + * @ticket 60506 + * + * @covers ::set_ignored_hooked_blocks_metadata + */ + public function test_set_ignored_hooked_blocks_metadata_retains_existing_items() { + $anchor_block = array( + 'blockName' => 'tests/anchor-block', + 'attrs' => array( + 'metadata' => array( + 'ignoredHookedBlocks' => array( 'tests/other-ignored-block' ), + ), + ), + ); + + $hooked_blocks = array( + 'tests/anchor-block' => array( + 'after' => array( 'tests/hooked-block' ), + ) + ); + + set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null ); + $this->assertSame( + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + array( 'tests/other-ignored-block', 'tests/hooked-block' ) + ); + } } From 5817b49bd48ba13b149d6c83baa1d6b789d10af7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 12:23:06 +0100 Subject: [PATCH 21/29] Add test coverage for hooked block added by filter --- .../blocks/setIgnoredHookedBlocksMetadata.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php index 88afca19697f9..0685324569991 100644 --- a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php +++ b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php @@ -58,4 +58,35 @@ public function test_set_ignored_hooked_blocks_metadata_retains_existing_items() array( 'tests/other-ignored-block', 'tests/hooked-block' ) ); } + + /** + * @ticket 60506 + * + * @covers ::set_ignored_hooked_blocks_metadata + */ + public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_filter() { + $anchor_block = array( + 'blockName' => 'tests/anchor-block', + 'attrs' => array(), + ); + + $hooked_blocks = array(); + + $filter = function( $hooked_block_types, $relative_position, $anchor_block_type ) { + if ( 'tests/anchor-block' === $anchor_block_type && 'after' === $relative_position ) { + $hooked_block_types[] = 'tests/hooked-block-added-by-filter'; + } + + return $hooked_block_types; + }; + + add_filter( 'hooked_block_types', $filter, 10, 3 ); + set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null ); + remove_filter( 'hooked_block_types', $filter, 10 ); + + $this->assertSame( + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + array( 'tests/hooked-block-added-by-filter' ) + ); + } } From 60b2732e041e81ed1f72b1c45bcb6b6b90c79dbf Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 13:20:55 +0100 Subject: [PATCH 22/29] Add test to cover context-aware filter --- .../blocks/setIgnoredHookedBlocksMetadata.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php index 0685324569991..8fcbf3f5283da 100644 --- a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php +++ b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php @@ -11,6 +11,22 @@ * @group block-hooks */ class Tests_Blocks_SetIgnoredHookedBlocksMetadata extends WP_UnitTestCase { + /** + * @ticket 60506 + */ + private static function create_block_template_object() { + $template = new WP_Block_Template(); + $template->type = 'wp_template'; + $template->theme = 'test-theme'; + $template->slug = 'single'; + $template->id = $template->theme . '//' . $template->slug; + $template->title = 'Single'; + $template->content = ''; + $template->description = 'Description of my template'; + + return $template; + } + /** * @ticket 60506 * @@ -89,4 +105,43 @@ public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_filte array( 'tests/hooked-block-added-by-filter' ) ); } + + /** + * @ticket 60506 + * + * @covers ::set_ignored_hooked_blocks_metadata + */ + public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_context_aware_filter() { + $anchor_block = array( + 'blockName' => 'tests/anchor-block', + 'attrs' => array(), + ); + + $filter = function( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { + if ( + ! $context instanceof WP_Block_Template || + ! property_exists( $context, 'slug' ) || + 'single' !== $context->slug + ) { + return $hooked_block_types; + } + + if ( 'tests/anchor-block' === $anchor_block_type && 'after' === $relative_position) { + $hooked_block_types[] = 'tests/hooked-block-added-by-filter'; + } + + return $hooked_block_types; + }; + + $template = self::create_block_template_object(); + + add_filter( 'hooked_block_types', $filter, 10, 4 ); + set_ignored_hooked_blocks_metadata( $anchor_block, 'after', array(), $template ); + remove_filter( 'hooked_block_types', $filter, 10 ); + + $this->assertSame( + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + array( 'tests/hooked-block-added-by-filter' ) + ); + } } From 61847cbb3d19213b4425980fa5064d6b223faf2f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 13:24:39 +0100 Subject: [PATCH 23/29] Remove obsolete comment about post_filtered_content --- src/wp-includes/default-filters.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index a5f7359045585..737b2d829a3fe 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -776,7 +776,6 @@ function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { array( 'ID' => $post->ID, 'post_content' => $content, - // Do we need to update post_filtered_content? ) ); } From 712260fb71cf9d451da00bc65e672af7b9acc3dc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 13:26:29 +0100 Subject: [PATCH 24/29] Move set_ignored_hooked_blocks_metadata function definition below insert_hooked_blocks --- src/wp-includes/blocks.php | 84 +++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 71c5dfb8b70cd..640ff69fb8c92 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -850,48 +850,6 @@ function get_hooked_blocks() { return $hooked_blocks; } -/** - * Adds a list of hooked block types to an anchor block's ignored hooked block types. - * - * This function is meant for internal use only. - * - * @since 6.5.0 - * @access private - * - * @param array $parsed_anchor_block The anchor block, in parsed block array format. - * @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 hooked block types, grouped by anchor block and relative position. - * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. - * @return string An empty string. - */ -function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) { - $anchor_block_type = $parsed_anchor_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 ); - if ( empty( $hooked_block_types ) ) { - return ''; - } - - $previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) - ? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] - : array(); - - $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique( - array_merge( - $previously_ignored_hooked_blocks, - $hooked_block_types - ) - ); - - // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`). - return ''; -} - /** * Conditionally returns the markup for a given hooked block. * @@ -989,6 +947,48 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke return $markup; } +/** + * Adds a list of hooked block types to an anchor block's ignored hooked block types. + * + * This function is meant for internal use only. + * + * @since 6.5.0 + * @access private + * + * @param array $parsed_anchor_block The anchor block, in parsed block array format. + * @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 hooked block types, grouped by anchor block and relative position. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + * @return string An empty string. + */ +function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) { + $anchor_block_type = $parsed_anchor_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 ); + if ( empty( $hooked_block_types ) ) { + return ''; + } + + $previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) + ? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] + : array(); + + $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique( + array_merge( + $previously_ignored_hooked_blocks, + $hooked_block_types + ) + ); + + // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`). + return ''; +} + /** * Returns a function that injects the theme attribute into, and hooked blocks before, a given block. * From 7c7f01e99a342fc1bfd004877541aa7f6d797b02 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 14:35:12 +0100 Subject: [PATCH 25/29] Inline get_hooked_block_markup --- src/wp-includes/blocks.php | 41 ++----- .../tests/blocks/getHookedBlockMarkup.php | 100 ------------------ 2 files changed, 7 insertions(+), 134 deletions(-) delete mode 100644 tests/phpunit/tests/blocks/getHookedBlockMarkup.php diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 640ff69fb8c92..7b981d2e69316 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -850,38 +850,6 @@ function get_hooked_blocks() { return $hooked_blocks; } -/** - * Conditionally returns the markup for a given hooked block. - * - * Accepts three arguments: A hooked block, its type, and an anchor block. - * If the anchor block has already been processed, and the given hooked block type is in the list - * of ignored hooked blocks, an empty string is returned. - * - * The hooked block type is specified separately as it's possible that a filter might've modified - * the hooked block such that `$hooked_block['blockName']` does no longer reflect the original type. - * - * This function is meant for internal use only. - * - * @since 6.5.0 - * @access private - * - * @param array $hooked_block The hooked block, represented as a parsed block array. - * @param string $hooked_block_type The type of the hooked block. This could be different from - * $hooked_block['blockName'], as a filter might've modified the latter. - * @param array $anchor_block The anchor block, represented as a parsed block array. - * @return string The markup for the given hooked block, or an empty string if the block is ignored. - */ -function get_hooked_block_markup( $hooked_block, $hooked_block_type, $anchor_block ) { - if ( - isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) && - in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) - ) { - return ''; - } - - return serialize_block( $hooked_block ); -} - /** * Returns the markup for blocks hooked to the given anchor block in a specific relative position. * @@ -940,8 +908,13 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $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 need to pass the original $hooked_block_type as well. - $markup .= get_hooked_block_markup( $parsed_hooked_block, $hooked_block_type, $parsed_anchor_block ); + // 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 ) + ) { + $markup .= serialize_block( $parsed_hooked_block ); + } } return $markup; diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php deleted file mode 100644 index 4941aab104b9e..0000000000000 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ /dev/null @@ -1,100 +0,0 @@ - 'tests/different-hooked-block', - 'attrs' => array(), - 'innerContent' => array(), - ); - - /** - * @ticket 59572 - * @ticket 60008 - * @ticket 60126 - * @ticket 60506 - * - * @covers ::get_hooked_block_markup - */ - public function test_get_hooked_block_markup_returns_correct_markup() { - $anchor_block = array( - 'blockName' => 'tests/anchor-block', - ); - - $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block ); - $this->assertSame( - '', - $actual, - "Markup for hooked block wasn't generated correctly." - ); - } - - /** - * @ticket 59572 - * @ticket 60008 - * @ticket 60126 - * @ticket 60506 - * - * @covers ::get_hooked_block_markup - */ - public function test_get_hooked_block_markup_if_block_is_ignored() { - $anchor_block = array( - 'blockName' => 'tests/anchor-block', - 'attrs' => array( - 'metadata' => array( - 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ), - ), - ), - ); - - $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block ); - $this->assertSame( - '', - $actual, - "No markup should've been generated for ignored hooked block." - ); - } - - /** - * @ticket 59572 - * @ticket 60008 - * @ticket 60126 - * @ticket 60506 - * - * @covers ::get_hooked_block_markup - */ - public function test_get_hooked_block_markup_if_other_block_is_ignored() { - $other_hooked_block_type = 'tests/other-hooked-block'; - $other_hooked_block = array( - 'blockName' => $other_hooked_block_type, - 'attrs' => array(), - 'innerContent' => array(), - ); - - $anchor_block = array( - 'blockName' => 'tests/anchor-block', - 'attrs' => array( - 'metadata' => array( - 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ), - ), - ), - ); - - $actual = get_hooked_block_markup( $other_hooked_block, $other_hooked_block_type, $anchor_block ); - $this->assertSame( - '', - $actual, - "Markup for newly hooked block should've been generated." - ); - } -} From f4f856b1d0aaa6872bff71f9bceaf9ecce9f7d5e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 14:47:05 +0100 Subject: [PATCH 26/29] Coding Standards in test --- .../tests/blocks/setIgnoredHookedBlocksMetadata.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php index 8fcbf3f5283da..16cdfa18e204d 100644 --- a/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php +++ b/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php @@ -40,11 +40,11 @@ public function test_set_ignored_hooked_blocks_metadata() { $hooked_blocks = array( 'tests/anchor-block' => array( 'after' => array( 'tests/hooked-block' ), - ) + ), ); set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null ); - $this->assertSame( $anchor_block[ 'attrs' ][ 'metadata' ][ 'ignoredHookedBlocks' ], array( 'tests/hooked-block' ) ); + $this->assertSame( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], array( 'tests/hooked-block' ) ); } /** @@ -65,7 +65,7 @@ public function test_set_ignored_hooked_blocks_metadata_retains_existing_items() $hooked_blocks = array( 'tests/anchor-block' => array( 'after' => array( 'tests/hooked-block' ), - ) + ), ); set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null ); @@ -88,7 +88,7 @@ public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_filte $hooked_blocks = array(); - $filter = function( $hooked_block_types, $relative_position, $anchor_block_type ) { + $filter = function ( $hooked_block_types, $relative_position, $anchor_block_type ) { if ( 'tests/anchor-block' === $anchor_block_type && 'after' === $relative_position ) { $hooked_block_types[] = 'tests/hooked-block-added-by-filter'; } @@ -117,7 +117,7 @@ public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_conte 'attrs' => array(), ); - $filter = function( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { + $filter = function ( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { if ( ! $context instanceof WP_Block_Template || ! property_exists( $context, 'slug' ) || @@ -126,7 +126,7 @@ public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_conte return $hooked_block_types; } - if ( 'tests/anchor-block' === $anchor_block_type && 'after' === $relative_position) { + if ( 'tests/anchor-block' === $anchor_block_type && 'after' === $relative_position ) { $hooked_block_types[] = 'tests/hooked-block-added-by-filter'; } From 7d43f213cac60d6e5ad37cc7276446885a170f63 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 15:32:04 +0100 Subject: [PATCH 27/29] Add test coverage to verify hooked blocks aren't added if ignored --- tests/phpunit/tests/block-templates/base.php | 4 +- .../buildBlockTemplateResultFromPost.php | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-templates/base.php b/tests/phpunit/tests/block-templates/base.php index 4add267507505..daa58c041cbd6 100644 --- a/tests/phpunit/tests/block-templates/base.php +++ b/tests/phpunit/tests/block-templates/base.php @@ -39,7 +39,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', - 'post_content' => '

Template

', + 'post_content' => '

Template

', 'post_excerpt' => 'Description of my template', 'tax_input' => array( 'wp_theme' => array( @@ -57,7 +57,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template_part', 'post_name' => 'my_template_part', 'post_title' => 'My Template Part', - 'post_content' => '

Template Part

', + 'post_content' => '

Template Part

', 'post_excerpt' => 'Description of my template part', 'tax_input' => array( 'wp_theme' => array( diff --git a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php index e7a81e9989b55..586e9beded17b 100644 --- a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php +++ b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php @@ -20,6 +20,10 @@ public function tear_down() { $registry->unregister( 'tests/my-block' ); } + if ( $registry->is_registered( 'tests/ignored' ) ) { + $registry->unregister( 'tests/ignored' ); + } + parent::tear_down(); } @@ -106,4 +110,46 @@ public function test_should_inject_hooked_block_into_template_part() { ); $this->assertStringEndsWith( '', $template_part->content ); } + + /** + * @ticket 59646 + * @ticket 60506 + */ + public function test_should_not_inject_ignored_hooked_block_into_template() { + register_block_type( + 'tests/ignored', + array( + 'block_hooks' => array( + 'core/heading' => 'after', + ), + ) + ); + + $template = _build_block_template_result_from_post( + self::$template_post, + 'wp_template' + ); + $this->assertStringNotContainsString( '', $template->content ); + } + + /** + * @ticket 59646 + * @ticket 60506 + */ + public function test_should_not_inject_ignored_hooked_block_into_template_part() { + register_block_type( + 'tests/ignored', + array( + 'block_hooks' => array( + 'core/heading' => 'after', + ), + ) + ); + + $template_part = _build_block_template_result_from_post( + self::$template_part_post, + 'wp_template_part' + ); + $this->assertStringNotContainsString( '', $template_part->content ); + } } From 3c9aa4ba6c312cf9c7b432b8a39c388be68ce61b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 15:37:02 +0100 Subject: [PATCH 28/29] Move set_ignored_hooked_blocks_metadata_upon_rest_insert to block-template-utils.php --- src/wp-includes/block-template-utils.php | 36 ++++++++++++++++++++++++ src/wp-includes/default-filters.php | 27 ------------------ 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 9237d1339d02d..9e2569bf1280d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1432,3 +1432,39 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' $template_hierarchy[] = 'index'; return $template_hierarchy; } +/** + * Inject ignoredHookedBlocks metadata attributes into a template or template part. + * + * Given a `wp_template` or `wp_template_part` post object, locate all blocks that have + * hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor + * blocks to reflect the latter. + * + * @param WP_Post $post A post object with post type set to `wp_template` or `wp_template_part`. + * @return WP_Post The updated post object. + */ +function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { + $hooked_blocks = get_hooked_blocks(); + if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { + return; + } + + // At this point, the post has already been created. + // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor. + // To that end, we need to suppress hooked blocks from getting inserted into the template. + add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); + $template = _build_block_template_result_from_post( $post ); + remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); + + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); + + $blocks = parse_blocks( $template->content ); + $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + + wp_update_post( + array( + 'ID' => $post->ID, + 'post_content' => $content, + ) + ); +} diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 737b2d829a3fe..d6c6d029ce59a 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -752,33 +752,6 @@ add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 ); add_action( 'init', '_wp_register_default_font_collections' ); -// TODO: This should probably go somewhere else. -function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { - $hooked_blocks = get_hooked_blocks(); - if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { - return; - } - - // At this point, the post has already been created. - // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor. - // To that end, we need to suppress hooked blocks from getting inserted into the template. - add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); - $template = _build_block_template_result_from_post( $post ); - remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); - - $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); - $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); - - $blocks = parse_blocks( $template->content ); - $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - - wp_update_post( - array( - 'ID' => $post->ID, - 'post_content' => $content, - ) - ); -} // It might be nice to use a filter instead of an action, but the `WP_REST_Templates_Controller` doesn't // provide one (unlike e.g. `WP_REST_Posts_Controller`, which has `rest_pre_insert_{$this->post_type}`). add_action( 'rest_after_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); From 51832fe58de9202cec8c4f73d2bf38074f111492 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Feb 2024 15:37:40 +0100 Subject: [PATCH 29/29] Rename to inject_ignored_hooked_blocks_metadata_attributes --- src/wp-includes/block-template-utils.php | 2 +- src/wp-includes/default-filters.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 9e2569bf1280d..89a2f04fb2dd3 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1442,7 +1442,7 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' * @param WP_Post $post A post object with post type set to `wp_template` or `wp_template_part`. * @return WP_Post The updated post object. */ -function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) { +function inject_ignored_hooked_blocks_metadata_attributes( $post ) { $hooked_blocks = get_hooked_blocks(); if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { return; diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index d6c6d029ce59a..3f7e43f8615bc 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -754,7 +754,7 @@ // It might be nice to use a filter instead of an action, but the `WP_REST_Templates_Controller` doesn't // provide one (unlike e.g. `WP_REST_Posts_Controller`, which has `rest_pre_insert_{$this->post_type}`). -add_action( 'rest_after_insert_wp_template', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); -add_action( 'rest_after_insert_wp_template_part', 'set_ignored_hooked_blocks_metadata_upon_rest_insert', 10, 3 ); +add_action( 'rest_after_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 ); +add_action( 'rest_after_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 ); unset( $filter, $action );