From a36ac67f2d370aec648afb801d37d184e9ee1baa Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Jan 2024 16:13:13 +0100 Subject: [PATCH 01/32] Block Hooks: Extract `insert_hooked_blocks()` function --- src/wp-includes/blocks.php | 99 ++++++++----------- .../tests/blocks/insertHookedBlocks.php | 34 +++++++ 2 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 tests/phpunit/tests/blocks/insertHookedBlocks.php diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 431e2b015332c..91626c58e9361 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -789,6 +789,45 @@ function get_hooked_block_markup( &$anchor_block, $hooked_block_type ) { return get_comment_delimited_block_content( $hooked_block_type, array(), '' ); } +/** + * 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 ] ) + ? $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_hooked_block_markup( $anchor_block, $hooked_block_type ); + } + + return $markup; +} + /** * Returns a function that injects the theme attribute into, and hooked blocks before, a given block. * @@ -826,40 +865,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_hooked_block_markup( $parent_block, $hooked_block_type ); - } + $markup .= insert_hooked_blocks( $parent_block, 'first_child', $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_hooked_block_markup( $block, $hooked_block_type ); - } + $markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context ); return $markup; }; @@ -895,33 +904,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_hooked_block_markup( $block, $hooked_block_type ); - } + $markup = insert_hooked_blocks( $block, 'after', $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_hooked_block_markup( $parent_block, $hooked_block_type ); - } + $markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context ); } return $markup; diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php new file mode 100644 index 0000000000000..fcf92fa642ebc --- /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 ); + } +} From 5350ae22275b51e91f06567155882e44dd84eda0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Jan 2024 16:44:12 +0100 Subject: [PATCH 02/32] Introduce hooked_block filter --- src/wp-includes/blocks.php | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 91626c58e9361..895cd17c1f179 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -758,9 +758,9 @@ function get_hooked_blocks() { } /** - * Conditionally returns the markup for a given hooked block type. + * Conditionally returns the markup for a given hooked block. * - * Accepts two arguments: A reference to an anchor block, and the name of a hooked block type. + * Accepts two arguments: A reference to an anchor block, and hooked 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. * @@ -769,15 +769,16 @@ function get_hooked_blocks() { * @since 6.5.0 * @access private * - * @param array $anchor_block The anchor block. Passed by reference. - * @param string $hooked_block_type The name of the hooked block type. - * @return string The markup for the given hooked block type, or an empty string if the block is ignored. + * @param array $anchor_block The anchor block. Passed by reference. + * @param array $hooked_block The hooked 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( &$anchor_block, $hooked_block_type ) { +function get_hooked_block_markup( &$anchor_block, $hooked_block ) { if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); } + $hooked_block_type = $hooked_block['blockName']; if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { return ''; } @@ -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, $hooked_block['attrs'], '' ); } /** @@ -822,7 +823,24 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block $markup = ''; foreach ( $hooked_block_types as $hooked_block_type ) { - $markup .= get_hooked_block_markup( $anchor_block, $hooked_block_type ); + $hooked_block = array( + 'blockName' => $hooked_block_type, + ); + + /** + * Filters the parsed block array for a given hooked block. + * + * @since 6.5.0 + * + * @param array $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 $anchor_block The anchor block. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + */ + $hooked_block = apply_filters( 'hooked_block', $hooked_block, $hooked_block_type, $relative_position, $anchor_block, $context ); + + $markup .= get_hooked_block_markup( $anchor_block, $hooked_block ); } return $markup; From e63213c2709103f719bfb2183c7edaec81f4e5f1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Jan 2024 16:56:16 +0100 Subject: [PATCH 03/32] get_hooked_block_markup: Change argument order --- src/wp-includes/blocks.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 895cd17c1f179..dfd613a123ab0 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -760,7 +760,7 @@ function get_hooked_blocks() { /** * Conditionally returns the markup for a given hooked block. * - * Accepts two arguments: A reference to an anchor block, and hooked block. + * Accepts two arguments: A hooked block, and a reference to 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. * @@ -769,11 +769,11 @@ function get_hooked_blocks() { * @since 6.5.0 * @access private * - * @param array $anchor_block The anchor block. Passed by reference. * @param array $hooked_block The hooked block, represented as a parsed block array. + * @param array $anchor_block The anchor block. 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( &$anchor_block, $hooked_block ) { +function get_hooked_block_markup( $hooked_block, &$anchor_block ) { if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); } @@ -840,7 +840,7 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block */ $hooked_block = apply_filters( 'hooked_block', $hooked_block, $hooked_block_type, $relative_position, $anchor_block, $context ); - $markup .= get_hooked_block_markup( $anchor_block, $hooked_block ); + $markup .= get_hooked_block_markup( $hooked_block, $anchor_block ); } return $markup; From a891d3c91aa6028be3b8c070ecfa4d9cf977a9a1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Jan 2024 16:58:40 +0100 Subject: [PATCH 04/32] Fix get_hooked_block_markup tests --- .../tests/blocks/getHookedBlockMarkup.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php index 2ea151f213f05..f4cebfc1f8f1b 100644 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php @@ -17,11 +17,15 @@ class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase { * @covers ::get_hooked_block_markup */ public function test_get_hooked_block_markup_adds_metadata() { + $hooked_block = array( + 'blockName' => 'tests/hooked-block', + ); + $anchor_block = array( 'blockName' => 'tests/anchor-block', ); - $actual = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' ); + $actual = get_hooked_block_markup( $hooked_block, $anchor_block ); $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); $this->assertSame( '', $actual ); } @@ -32,6 +36,10 @@ public function test_get_hooked_block_markup_adds_metadata() { * @covers ::get_hooked_block_markup */ public function test_get_hooked_block_markup_if_block_is_already_hooked() { + $hooked_block = array( + 'blockName' => 'tests/hooked-block', + ); + $anchor_block = array( 'blockName' => 'tests/anchor-block', 'attrs' => array( @@ -41,7 +49,7 @@ public function test_get_hooked_block_markup_if_block_is_already_hooked() { ), ); - $actual = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' ); + $actual = get_hooked_block_markup( $hooked_block, $anchor_block ); $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); $this->assertSame( '', $actual ); } @@ -52,6 +60,10 @@ public function test_get_hooked_block_markup_if_block_is_already_hooked() { * @covers ::get_hooked_block_markup */ public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() { + $other_hooked_block = array( + 'blockName' => 'tests/other-hooked-block', + ); + $anchor_block = array( 'blockName' => 'tests/anchor-block', 'attrs' => array( @@ -61,7 +73,7 @@ public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() { ), ); - $actual = get_hooked_block_markup( $anchor_block, 'tests/other-hooked-block' ); + $actual = get_hooked_block_markup( $other_hooked_block, $anchor_block ); $this->assertSame( array( 'tests/hooked-block', 'tests/other-hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); $this->assertSame( '', $actual ); } From fabd9c22aed7224ed168a982965098738b0d89f4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Jan 2024 17:04:37 +0100 Subject: [PATCH 05/32] Make block type name part of filter name --- 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 dfd613a123ab0..81b2e92a9135c 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -833,12 +833,11 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block * @since 6.5.0 * * @param array $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 $anchor_block The anchor block. * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. */ - $hooked_block = apply_filters( 'hooked_block', $hooked_block, $hooked_block_type, $relative_position, $anchor_block, $context ); + $hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $hooked_block, $relative_position, $anchor_block, $context ); $markup .= get_hooked_block_markup( $hooked_block, $anchor_block ); } From afd95d03ca71bb6484abe3061f2e68f66da6eacb Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Jan 2024 17:38:53 +0100 Subject: [PATCH 06/32] Set attrs to empty array --- src/wp-includes/blocks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 81b2e92a9135c..6d503837e6da2 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -825,6 +825,7 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block foreach ( $hooked_block_types as $hooked_block_type ) { $hooked_block = array( 'blockName' => $hooked_block_type, + 'attrs' => array(), ); /** From f797f8a9ac78cf14d32403e1492a99555d3bfa78 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 10 Jan 2024 12:14:56 +0100 Subject: [PATCH 07/32] Allow passing inner blocks --- src/wp-includes/blocks.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 6d503837e6da2..551de65d8173d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -787,7 +787,7 @@ function get_hooked_block_markup( $hooked_block, &$anchor_block ) { // 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, $hooked_block['attrs'], '' ); + return serialize_block( $hooked_block ); } /** @@ -824,8 +824,10 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block $markup = ''; foreach ( $hooked_block_types as $hooked_block_type ) { $hooked_block = array( - 'blockName' => $hooked_block_type, - 'attrs' => array(), + 'blockName' => $hooked_block_type, + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerContent' => array(), ); /** From 01f30ab06776c7e837a99ebba4821485c926dda3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 10 Jan 2024 12:22:14 +0100 Subject: [PATCH 08/32] Use correct block type in ignoredHookedBlocks --- src/wp-includes/blocks.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 551de65d8173d..a8173f32df0bd 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -769,16 +769,17 @@ function get_hooked_blocks() { * @since 6.5.0 * @access private * - * @param array $hooked_block The hooked block, represented as a parsed block array. - * @param array $anchor_block The anchor block. Passed by reference. + * @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. 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, &$anchor_block ) { +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(); } - $hooked_block_type = $hooked_block['blockName']; if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { return ''; } @@ -842,7 +843,9 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block */ $hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $hooked_block, $relative_position, $anchor_block, $context ); - $markup .= get_hooked_block_markup( $hooked_block, $anchor_block ); + // It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type, + // so we pass the original $hooked_block_type as well. + $markup .= get_hooked_block_markup( $hooked_block, $hooked_block_type, $anchor_block ); } return $markup; From 962eaf55cd09113ccfe8ccf80b144241871749a2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 11:26:38 +0100 Subject: [PATCH 09/32] Update comment --- src/wp-includes/blocks.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index a8173f32df0bd..73329a25c69cc 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -760,10 +760,13 @@ function get_hooked_blocks() { /** * Conditionally returns the markup for a given hooked block. * - * Accepts two arguments: A hooked block, and a reference to an anchor block. + * Accepts three arguments: A hooked block, its type, and a reference to 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 From 1a214a7f7cb9c8cfc461cf20ac358de3e184263d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 11:32:59 +0100 Subject: [PATCH 10/32] Mark insert_hooked_blocks as private --- src/wp-includes/blocks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 73329a25c69cc..2c37f84314347 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -798,6 +798,7 @@ function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_bl * Returns the markup for blocks hooked to the given anchor block in a specific relative position. * * @since 6.5.0 + * @access private * * @param array $anchor_block The anchor block. * @param string $relative_position The relative position of the hooked blocks. From 8b086190c317742a30bdef56a6de2bdb9082e112 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 11:36:09 +0100 Subject: [PATCH 11/32] Slight rewording of a comment --- 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 2c37f84314347..914e4353924dd 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -848,7 +848,7 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block $hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $hooked_block, $relative_position, $anchor_block, $context ); // It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type, - // so we pass the original $hooked_block_type as well. + // so we need to pass the original $hooked_block_type as well. $markup .= get_hooked_block_markup( $hooked_block, $hooked_block_type, $anchor_block ); } From 0658500fa03e97ac055e40094dd6d5f292ffd89f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 11:42:54 +0100 Subject: [PATCH 12/32] Clarify 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 914e4353924dd..937fdd9f9976c 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -803,7 +803,7 @@ function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_bl * @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 array $hooked_blocks An array of hooked blocks, 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 */ From 45ff3da6fd6d34050c23739bda74b287dcd1ef56 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 12:06:54 +0100 Subject: [PATCH 13/32] Fix tests --- .../tests/blocks/getHookedBlockMarkup.php | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php index f4cebfc1f8f1b..fbb7b5d696104 100644 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php @@ -11,23 +11,26 @@ * @group block-hooks */ class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase { + const HOOKED_BLOCK_TYPE = 'tests/hooked-block'; + const HOOKED_BLOCK = array( + 'blockName' => 'tests/hooked-block', + 'attrs' => array(), + 'innerContent' => array(), + ); + /** * @ticket 60008 * * @covers ::get_hooked_block_markup */ public function test_get_hooked_block_markup_adds_metadata() { - $hooked_block = array( - 'blockName' => 'tests/hooked-block', - ); - $anchor_block = array( 'blockName' => 'tests/anchor-block', ); - $actual = get_hooked_block_markup( $hooked_block, $anchor_block ); - $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); - $this->assertSame( '', $actual ); + $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'] ); + $this->assertSame( '', $actual ); } /** @@ -36,21 +39,17 @@ public function test_get_hooked_block_markup_adds_metadata() { * @covers ::get_hooked_block_markup */ public function test_get_hooked_block_markup_if_block_is_already_hooked() { - $hooked_block = array( - 'blockName' => 'tests/hooked-block', - ); - $anchor_block = array( 'blockName' => 'tests/anchor-block', 'attrs' => array( 'metadata' => array( - 'ignoredHookedBlocks' => array( 'tests/hooked-block' ), + 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ), ), ), ); - $actual = get_hooked_block_markup( $hooked_block, $anchor_block ); - $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); + $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'] ); $this->assertSame( '', $actual ); } @@ -60,21 +59,24 @@ public function test_get_hooked_block_markup_if_block_is_already_hooked() { * @covers ::get_hooked_block_markup */ public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() { - $other_hooked_block = array( - 'blockName' => 'tests/other-hooked-block', + $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( 'tests/hooked-block' ), + 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ), ), ), ); - $actual = get_hooked_block_markup( $other_hooked_block, $anchor_block ); - $this->assertSame( array( 'tests/hooked-block', 'tests/other-hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); - $this->assertSame( '', $actual ); + $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'] ); + $this->assertSame( '', $actual ); } } From 520f67ec60735ab6bd68c357a43807952041d105 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 12:18:33 +0100 Subject: [PATCH 14/32] Cover new hooked_block_type arg in tests --- tests/phpunit/tests/blocks/getHookedBlockMarkup.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php index fbb7b5d696104..5ceef6cf2be56 100644 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php @@ -13,7 +13,7 @@ class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase { const HOOKED_BLOCK_TYPE = 'tests/hooked-block'; const HOOKED_BLOCK = array( - 'blockName' => 'tests/hooked-block', + 'blockName' => 'tests/different-hooked-block', 'attrs' => array(), 'innerContent' => array(), ); @@ -30,7 +30,7 @@ public function test_get_hooked_block_markup_adds_metadata() { $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'] ); - $this->assertSame( '', $actual ); + $this->assertSame( '', $actual ); } /** @@ -77,6 +77,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'] ); - $this->assertSame( '', $actual ); + $this->assertSame( '', $actual ); } } From df3dc99a0bcdffa153af541ac003efccecac74ae Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 12:21:09 +0100 Subject: [PATCH 15/32] Add ticket references to tests --- tests/phpunit/tests/blocks/getHookedBlockMarkup.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php index 5ceef6cf2be56..2b7369349e0fe 100644 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php @@ -20,6 +20,7 @@ class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase { /** * @ticket 60008 + * @ticket 60126 * * @covers ::get_hooked_block_markup */ @@ -35,6 +36,7 @@ public function test_get_hooked_block_markup_adds_metadata() { /** * @ticket 60008 + * @ticket 60126 * * @covers ::get_hooked_block_markup */ @@ -55,6 +57,7 @@ public function test_get_hooked_block_markup_if_block_is_already_hooked() { /** * @ticket 60008 + * @ticket 60126 * * @covers ::get_hooked_block_markup */ From 7b9d6f2263f0e701a817ba94d194bfde6a36ce4a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 12:28:05 +0100 Subject: [PATCH 16/32] Another ticket reference --- tests/phpunit/tests/blocks/insertHookedBlocks.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index fcf92fa642ebc..e5ff280647525 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -12,6 +12,8 @@ */ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase { /** + * @ticket 60126 + * * @covers ::insert_hooked_blocks */ public function test_insert_hooked_blocks() { From c962fc28ea9cfc5d4702d379a0764826227ac2ae Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 11 Jan 2024 15:25:09 +0100 Subject: [PATCH 17/32] Add assertion to insert_hooked_blocks test --- tests/phpunit/tests/blocks/insertHookedBlocks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index e5ff280647525..dc06db88cc98b 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -31,6 +31,7 @@ public function test_insert_hooked_blocks() { ); $actual = insert_hooked_blocks( $anchor_block, 'after', $hooked_blocks, array() ); + $this->assertSame( array( 'tests/hooked-before' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); $this->assertSame( '', $actual ); } } From cf1297dc1fab333bc3a41d8982951caa65c75012 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 16 Jan 2024 14:05:55 +0100 Subject: [PATCH 18/32] insert_hooked_blocks Test: Turn vars into class consts --- .../tests/blocks/insertHookedBlocks.php | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index dc06db88cc98b..79d404b1454db 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -11,27 +11,33 @@ * @group block-hooks */ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase { + const ANCHOR_BLOCK_TYPE = 'tests/anchor-block'; + + const HOOKED_BLOCK_TYPE = 'tests/hooked-block'; + const HOOKED_BLOCK = array( + 'blockName' => 'tests/different-hooked-block', + 'attrs' => array(), + 'innerContent' => array(), + ); + + const HOOKED_BLOCKS = array( + self::ANCHOR_BLOCK_TYPE => array( + 'after' => array( self::HOOKED_BLOCK_TYPE ), + ), + ); + /** * @ticket 60126 * * @covers ::insert_hooked_blocks */ - public function test_insert_hooked_blocks() { - $anchor_block_name = 'tests/anchor-block'; - $anchor_block = array( - 'blockName' => $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' ), - ), + public function test_insert_hooked_blocks_adds_metadata() { + $anchor_block = array( + 'blockName' => self::ANCHOR_BLOCK_TYPE, ); - $actual = insert_hooked_blocks( $anchor_block, 'after', $hooked_blocks, array() ); - $this->assertSame( array( 'tests/hooked-before' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); - $this->assertSame( '', $actual ); + $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); + $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); + $this->assertSame( '', $actual ); } } From 311c2e6064bc3e5aa030e5c8f8486ad4ec834609 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 16 Jan 2024 14:08:27 +0100 Subject: [PATCH 19/32] Add more test coverage --- .../tests/blocks/insertHookedBlocks.php | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index 79d404b1454db..69100d6cc0eb2 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -20,9 +20,17 @@ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase { 'innerContent' => array(), ); + const OTHER_HOOKED_BLOCK_TYPE = 'tests/other-hooked-block'; + const OTHER_HOOKED_BLOCK = array( + 'blockName' => self::OTHER_HOOKED_BLOCK_TYPE, + 'attrs' => array(), + 'innerContent' => array(), + ); + const HOOKED_BLOCKS = array( self::ANCHOR_BLOCK_TYPE => array( - 'after' => array( self::HOOKED_BLOCK_TYPE ), + 'after' => array( self::HOOKED_BLOCK_TYPE ), + 'before' => array( self::OTHER_HOOKED_BLOCK_TYPE ), ), ); @@ -40,4 +48,44 @@ public function test_insert_hooked_blocks_adds_metadata() { $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); $this->assertSame( '', $actual ); } + + /** + * @ticket 60126 + * + * @covers ::insert_hooked_blocks + */ + public function test_insert_hooked_blocks_if_block_is_already_hooked() { + $anchor_block = array( + 'blockName' => 'tests/anchor-block', + 'attrs' => array( + 'metadata' => array( + 'ignoredHookedBlocks' => array( self::HOOKED_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'] ); + $this->assertSame( '', $actual ); + } + + /** + * @ticket 60126 + * + * @covers ::insert_hooked_blocks + */ + public function test_insert_hooked_blocks_adds_to_ignored_hooked_blocks() { + $anchor_block = array( + 'blockName' => 'tests/anchor-block', + 'attrs' => array( + 'metadata' => array( + 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ), + ), + ), + ); + + $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'] ); + $this->assertSame( '', $actual ); + } } From ace54d434bf40de2e74320564c239cf80ac7132e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 16 Jan 2024 14:50:49 +0100 Subject: [PATCH 20/32] Add ticket references to 59572 --- tests/phpunit/tests/blocks/getHookedBlockMarkup.php | 3 +++ tests/phpunit/tests/blocks/insertHookedBlocks.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php index 2b7369349e0fe..314fa987065d6 100644 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php @@ -19,6 +19,7 @@ class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase { ); /** + * @ticket 59572 * @ticket 60008 * @ticket 60126 * @@ -35,6 +36,7 @@ public function test_get_hooked_block_markup_adds_metadata() { } /** + * @ticket 59572 * @ticket 60008 * @ticket 60126 * @@ -56,6 +58,7 @@ public function test_get_hooked_block_markup_if_block_is_already_hooked() { } /** + * @ticket 59572 * @ticket 60008 * @ticket 60126 * diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index 69100d6cc0eb2..58d05a4d9efab 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -35,6 +35,7 @@ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase { ); /** + * @ticket 59572 * @ticket 60126 * * @covers ::insert_hooked_blocks @@ -50,6 +51,7 @@ public function test_insert_hooked_blocks_adds_metadata() { } /** + * @ticket 59572 * @ticket 60126 * * @covers ::insert_hooked_blocks @@ -70,6 +72,7 @@ public function test_insert_hooked_blocks_if_block_is_already_hooked() { } /** + * @ticket 59572 * @ticket 60126 * * @covers ::insert_hooked_blocks From e3df47685fa82548bdb2e0681762b342c9f8c218 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 10:07:06 +0100 Subject: [PATCH 21/32] Amend 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 937fdd9f9976c..1054aeb8258c2 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -842,7 +842,7 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block * * @param array $hooked_block The parsed block array for the given hooked block type. * @param string $relative_position The relative position of the hooked block. - * @param array $anchor_block The anchor block. + * @param array $anchor_block The anchor block, in parsed block array format. * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. */ $hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $hooked_block, $relative_position, $anchor_block, $context ); From 275a96a69234c16ed17d545720c9f1a38be26697 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 10:10:06 +0100 Subject: [PATCH 22/32] Change variable names to emphasize parsed block array format --- src/wp-includes/blocks.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 1054aeb8258c2..98057381fbde0 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -800,15 +800,15 @@ function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_bl * @since 6.5.0 * @access private * - * @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 hooked blocks, 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. + * @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 blocks, 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 */ -function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_blocks, $context ) { - $anchor_block_type = $anchor_block['blockName']; +function insert_hooked_blocks( &$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(); @@ -828,7 +828,7 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block $markup = ''; foreach ( $hooked_block_types as $hooked_block_type ) { - $hooked_block = array( + $parsed_hooked_block = array( 'blockName' => $hooked_block_type, 'attrs' => array(), 'innerBlocks' => array(), @@ -840,16 +840,16 @@ function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_block * * @since 6.5.0 * - * @param array $hooked_block The parsed block array for the given hooked block type. - * @param string $relative_position The relative position of the hooked block. - * @param array $anchor_block The anchor block, in parsed block array format. - * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + * @param array $parsed_hooked_block The parsed block array for the given hooked block type. + * @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|array $context The block template, template part, or pattern that the anchor block belongs to. */ - $hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $hooked_block, $relative_position, $anchor_block, $context ); + $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( $hooked_block, $hooked_block_type, $anchor_block ); + $markup .= get_hooked_block_markup( $parsed_hooked_block, $hooked_block_type, $parsed_anchor_block ); } return $markup; From e21c8e9ee64a525b635249abb27433ca767fe94a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 10:44:12 +0100 Subject: [PATCH 23/32] Remove unnecessary test mocks --- .../phpunit/tests/blocks/insertHookedBlocks.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index 58d05a4d9efab..6f9712786b1df 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -11,21 +11,9 @@ * @group block-hooks */ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase { - const ANCHOR_BLOCK_TYPE = 'tests/anchor-block'; - - const HOOKED_BLOCK_TYPE = 'tests/hooked-block'; - const HOOKED_BLOCK = array( - 'blockName' => 'tests/different-hooked-block', - 'attrs' => array(), - 'innerContent' => array(), - ); - + const ANCHOR_BLOCK_TYPE = 'tests/anchor-block'; + const HOOKED_BLOCK_TYPE = 'tests/hooked-block'; const OTHER_HOOKED_BLOCK_TYPE = 'tests/other-hooked-block'; - const OTHER_HOOKED_BLOCK = array( - 'blockName' => self::OTHER_HOOKED_BLOCK_TYPE, - 'attrs' => array(), - 'innerContent' => array(), - ); const HOOKED_BLOCKS = array( self::ANCHOR_BLOCK_TYPE => array( From c2912f8fd26b188b0044ad4b957bf4306335d791 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 11:13:50 +0100 Subject: [PATCH 24/32] Ooops --- 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 98057381fbde0..694f60a9ef248 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -845,7 +845,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param array $parsed_anchor_block The anchor block, in parsed block array format. * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. */ - $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, $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. From 13f1c5dd9cc68f8cfe1559d69839c076837271fa Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 11:14:07 +0100 Subject: [PATCH 25/32] Add test coverage for filter --- .../tests/blocks/insertHookedBlocks.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index 6f9712786b1df..d5b1b964ef712 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -79,4 +79,43 @@ public function test_insert_hooked_blocks_adds_to_ignored_hooked_blocks() { $this->assertSame( array( self::HOOKED_BLOCK_TYPE, self::OTHER_HOOKED_BLOCK_TYPE ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); $this->assertSame( '', $actual ); } + + /** + * @ticket 59572 + * @ticket 60126 + * + * @covers ::insert_hooked_blocks + */ + public function test_insert_hooked_blocks_filter_can_set_attributes() { + $anchor_block = array( + 'blockName' => self::ANCHOR_BLOCK_TYPE, + 'attrs' => array( + 'layout' => array( + 'type' => 'constrained', + ) + ), + 'innerContent' => array(), + ); + + $filter = function( $parsed_hooked_block, $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; + } + + // Does the anchor block have a layout attribute? + if ( isset( $parsed_anchor_block['attrs']['layout'] ) ) { + // Copy the anchor block's layout attribute to the hooked block. + $parsed_hooked_block['attrs']['layout'] = $parsed_anchor_block['attrs']['layout']; + } + + return $parsed_hooked_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 ); + + $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); + $this->assertSame( '', $actual ); + } } From b9f71add3d605bdfb6278bc636ff5d98f21a00ac Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 11:40:10 +0100 Subject: [PATCH 26/32] Add test to cover wrapping block --- .../tests/blocks/insertHookedBlocks.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index d5b1b964ef712..e1d39d19c1bd8 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -118,4 +118,49 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() { $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); $this->assertSame( '', $actual ); } + + /** + * @ticket 59572 + * @ticket 60126 + * + * @covers ::insert_hooked_blocks + */ + public function test_insert_hooked_blocks_filter_can_wrap_block() { + $anchor_block = array( + 'blockName' => self::ANCHOR_BLOCK_TYPE, + 'attrs' => array( + 'layout' => array( + 'type' => 'constrained', + ) + ), + 'innerContent' => array(), + ); + + $filter = function( $parsed_hooked_block ) { + if ( SELF::HOOKED_BLOCK_TYPE !== $parsed_hooked_block['blockName'] ) { + return $parsed_hooked_block; + } + + // Wrap the block in a Group block. + return array( + 'blockName' => 'core/group', + 'attrs' => array(), + 'innerBlocks' => array( $parsed_hooked_block ), + 'innerContent' => array( + '
', + null, + '
' + ), + ); + }; + 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 ); + + $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); + $this->assertSame( + '
', + $actual + ); + } } From 30c2e9708fc52b5226545460e6c9b59008375dbf Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 11:58:30 +0100 Subject: [PATCH 27/32] Add assertion messages --- .../tests/blocks/insertHookedBlocks.php | 57 +++++++++++++++---- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index e1d39d19c1bd8..aad40c362f533 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -34,8 +34,16 @@ public function test_insert_hooked_blocks_adds_metadata() { ); $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() ); - $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); - $this->assertSame( '', $actual ); + $this->assertSame( + array( self::HOOKED_BLOCK_TYPE ), + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + "Hooked block type wasn't added to ignoredHookedBlocks metadata." + ); + $this->assertSame( + '', + $actual, + "Markup for hooked block wasn't generated correctly." + ); } /** @@ -55,8 +63,16 @@ 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'] ); - $this->assertSame( '', $actual ); + $this->assertSame( + array( self::HOOKED_BLOCK_TYPE ), + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + "ignoredHookedBlocks metadata shouldn't have been modified." + ); + $this->assertSame( + '', + $actual, + "No markup should've been generated for ignored hooked block." + ); } /** @@ -76,8 +92,16 @@ 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'] ); - $this->assertSame( '', $actual ); + $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, + "Markup for newly hooked block should've been generated." + ); } /** @@ -115,8 +139,16 @@ 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'] ); - $this->assertSame( '', $actual ); + $this->assertSame( + array( self::HOOKED_BLOCK_TYPE ), + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + "Hooked block type wasn't added to ignoredHookedBlocks metadata." + ); + $this->assertSame( + '', + $actual, + "Markup wasn't generated correctly for hooked block with attribute set by filter." + ); } /** @@ -157,10 +189,15 @@ 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'] ); + $this->assertSame( + array( self::HOOKED_BLOCK_TYPE ), + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + "Hooked block type wasn't added to ignoredHookedBlocks metadata." + ); $this->assertSame( '
', - $actual + $actual, + "Markup wasn't generated correctly for hooked block wrapped in Group block by filter." ); } } From 501561777abf53ff79665543f4992f537db84b15 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 12:02:05 +0100 Subject: [PATCH 28/32] More assertion messages --- .../tests/blocks/getHookedBlockMarkup.php | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php index 314fa987065d6..6433e65c35628 100644 --- a/tests/phpunit/tests/blocks/getHookedBlockMarkup.php +++ b/tests/phpunit/tests/blocks/getHookedBlockMarkup.php @@ -31,8 +31,16 @@ public function test_get_hooked_block_markup_adds_metadata() { ); $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'] ); - $this->assertSame( '', $actual ); + $this->assertSame( + array( self::HOOKED_BLOCK_TYPE ), + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + "Hooked block type wasn't added to ignoredHookedBlocks metadata." + ); + $this->assertSame( + '', + $actual, + "Markup for hooked block wasn't generated correctly." + ); } /** @@ -53,8 +61,16 @@ 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'] ); - $this->assertSame( '', $actual ); + $this->assertSame( + array( self::HOOKED_BLOCK_TYPE ), + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], + "ignoredHookedBlocks metadata shouldn't have been modified." + ); + $this->assertSame( + '', + $actual, + "No markup should've been generated for ignored hooked block." + ); } /** @@ -82,7 +98,15 @@ 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'] ); - $this->assertSame( '', $actual ); + $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, + "Markup for newly hooked block should've been generated." + ); } } From 3092b85281476841ff620fa6a923fb947212e044 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 12:06:20 +0100 Subject: [PATCH 29/32] Coding standards --- .../tests/blocks/insertHookedBlocks.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/blocks/insertHookedBlocks.php b/tests/phpunit/tests/blocks/insertHookedBlocks.php index aad40c362f533..116aad1aaa14c 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -116,12 +116,12 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() { 'attrs' => array( 'layout' => array( 'type' => 'constrained', - ) + ), ), 'innerContent' => array(), ); - $filter = function( $parsed_hooked_block, $relative_position, $parsed_anchor_block ) { + $filter = function ( $parsed_hooked_block, $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; @@ -135,9 +135,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, 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, 3 ); $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), @@ -163,13 +163,13 @@ public function test_insert_hooked_blocks_filter_can_wrap_block() { 'attrs' => array( 'layout' => array( 'type' => 'constrained', - ) + ), ), 'innerContent' => array(), ); - $filter = function( $parsed_hooked_block ) { - if ( SELF::HOOKED_BLOCK_TYPE !== $parsed_hooked_block['blockName'] ) { + $filter = function ( $parsed_hooked_block ) { + if ( self::HOOKED_BLOCK_TYPE !== $parsed_hooked_block['blockName'] ) { return $parsed_hooked_block; } @@ -181,13 +181,13 @@ public function test_insert_hooked_blocks_filter_can_wrap_block() { 'innerContent' => array( '
', null, - '
' + '', ), ); }; - add_filter( 'hooked_block_' . SELF::HOOKED_BLOCK_TYPE, $filter, 10, 3 ); + 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, 3 ); $this->assertSame( array( self::HOOKED_BLOCK_TYPE ), From 47e80e16d9c8c9bdac8b1e1bced13595e02711c5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 12:13:12 +0100 Subject: [PATCH 30/32] More coding standards --- 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 116aad1aaa14c..c4762a661c225 100644 --- a/tests/phpunit/tests/blocks/insertHookedBlocks.php +++ b/tests/phpunit/tests/blocks/insertHookedBlocks.php @@ -175,9 +175,9 @@ public function test_insert_hooked_blocks_filter_can_wrap_block() { // Wrap the block in a Group block. return array( - 'blockName' => 'core/group', - 'attrs' => array(), - 'innerBlocks' => array( $parsed_hooked_block ), + 'blockName' => 'core/group', + 'attrs' => array(), + 'innerBlocks' => array( $parsed_hooked_block ), 'innerContent' => array( '
', null, From 9cac78c2829e72bad669ad322f3dc1d7ad168372 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 14:07:40 +0100 Subject: [PATCH 31/32] Tweak PHPDoc --- src/wp-includes/blocks.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 694f60a9ef248..9ad285aea6334 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -775,7 +775,8 @@ function get_hooked_blocks() { * @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. Passed by reference. + * @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 ) { From 7fdf956864595de3e1f96c787160941a80573b14 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 Jan 2024 14:09:48 +0100 Subject: [PATCH 32/32] More PHPDoc tweaking --- 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 9ad285aea6334..5c072e324ba6d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -804,7 +804,7 @@ function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_bl * @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 blocks, grouped by anchor block and relative position. + * @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 */