From 6da854fb7288071d5a888458aea0f6078501db1e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 31 Oct 2023 16:51:46 +0100 Subject: [PATCH 1/7] Block Hooks: Pass parent block by reference --- 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 1edc0762d3c00..f8a8c328b3c4b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -785,7 +785,7 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * @param array $prev The previous sibling block of the given block. * @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 ) { _inject_theme_attribute_in_template_part_block( $block ); $markup = ''; @@ -858,7 +858,7 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * @param array $next The next sibling block of the given block. * @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 ) { + return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) { $markup = ''; $relative_position = 'after'; @@ -1065,7 +1065,7 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb $block_content .= call_user_func_array( $pre_callback, - array( &$inner_block, $block, $prev ) + array( &$inner_block, &$block, $prev ) ); } @@ -1076,7 +1076,7 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb $post_markup = call_user_func_array( $post_callback, - array( &$inner_block, $block, $next ) + array( &$inner_block, &$block, $next ) ); } From 6f4d0439a2a4da0a784ee34df93dad9c811ff8c6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 31 Oct 2023 16:44:49 +0100 Subject: [PATCH 2/7] Pass reference to callback --- src/wp-includes/blocks.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index f8a8c328b3c4b..40716fcf310d4 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1132,6 +1132,7 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb */ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) { $result = ''; + $parent = null; // At the top level, there is no parent block to pass to the callback; yet the callback expects a reference. foreach ( $blocks as $index => $block ) { if ( is_callable( $pre_callback ) ) { $prev = 0 === $index @@ -1140,7 +1141,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $result .= call_user_func_array( $pre_callback, - array( &$block, null, $prev ) // At the top level, there is no parent block to pass to the callback. + array( &$block, $parent, $prev ) ); } @@ -1151,7 +1152,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $post_markup = call_user_func_array( $post_callback, - array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback. + array( &$block, $parent, $next ) ); } From 340c72bf3cf0727e75b26e91bd5113a951544ac8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 31 Oct 2023 16:54:15 +0100 Subject: [PATCH 3/7] Plural --- 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 40716fcf310d4..33971552dc1fd 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1132,7 +1132,7 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb */ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) { $result = ''; - $parent = null; // At the top level, there is no parent block to pass to the callback; yet the callback expects a reference. + $parent = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference. foreach ( $blocks as $index => $block ) { if ( is_callable( $pre_callback ) ) { $prev = 0 === $index From dd0f25228309de6d735c509d538c1b25bc7296ef Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 31 Oct 2023 17:35:19 +0100 Subject: [PATCH 4/7] Pass as reference at callsite --- src/wp-includes/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 33971552dc1fd..e07a242fe8dc0 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1141,7 +1141,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $result .= call_user_func_array( $pre_callback, - array( &$block, $parent, $prev ) + array( &$block, &$parent, $prev ) ); } @@ -1152,7 +1152,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $post_markup = call_user_func_array( $post_callback, - array( &$block, $parent, $next ) + array( &$block, &$parent, $next ) ); } From 50a340ab06543379ed062ca526d3f0b4e5d99a6d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 31 Oct 2023 19:55:06 +0100 Subject: [PATCH 5/7] Document that block and parent_block are passed by reference --- 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 e07a242fe8dc0..2297003784935 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -780,8 +780,8 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's * `first_child`, respectively, to the serialized markup for the given block. * - * @param array $block The block to inject the theme attribute into, and hooked blocks before. - * @param array $parent_block The parent block of the given block. + * @param array $block The block to inject the theme attribute into, and hooked blocks before. Passed by reference. + * @param array $parent_block The parent block of the given block. Passed by reference. * @param array $prev The previous sibling block of the given block. * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it. */ @@ -853,8 +853,8 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * Append the markup for any blocks hooked `after` the given block and as its parent's * `last_child`, respectively, to the serialized markup for the given block. * - * @param array $block The block to inject the hooked blocks after. - * @param array $parent_block The parent block of the given block. + * @param array $block The block to inject the hooked blocks after. Passed by reference. + * @param array $parent_block The parent block of the given block. Passed by reference. * @param array $next The next sibling block of the given block. * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it. */ From 39c59938ddb50c8e77be7ec14436a1fc7e5eb578 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 31 Oct 2023 19:55:56 +0100 Subject: [PATCH 6/7] Document that parent_block, prev, and next default to null --- 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 2297003784935..f2f8ac8554b30 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -781,8 +781,8 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * `first_child`, respectively, to the serialized markup for the given block. * * @param array $block The block to inject the theme attribute into, and hooked blocks before. Passed by reference. - * @param array $parent_block The parent block of the given block. Passed by reference. - * @param array $prev The previous sibling block of the given block. + * @param array $parent_block The parent block of the given block. Passed by reference. Default null. + * @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 ) { @@ -854,8 +854,8 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * `last_child`, respectively, to the serialized markup for the given block. * * @param array $block The block to inject the hooked blocks after. Passed by reference. - * @param array $parent_block The parent block of the given block. Passed by reference. - * @param array $next The next sibling block of the given block. + * @param array $parent_block The parent block of the given block. Passed by reference. Default null. + * @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 ) { From 7aa6c9cd0c99f3281995458fe54effd3d7ccf651 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 31 Oct 2023 19:57:19 +0100 Subject: [PATCH 7/7] Rename var from parent to parent_block --- src/wp-includes/blocks.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index f2f8ac8554b30..52b20b3e55b26 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1131,8 +1131,9 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb * @return string Serialized block markup. */ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) { - $result = ''; - $parent = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference. + $result = ''; + $parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference. + foreach ( $blocks as $index => $block ) { if ( is_callable( $pre_callback ) ) { $prev = 0 === $index @@ -1141,7 +1142,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $result .= call_user_func_array( $pre_callback, - array( &$block, &$parent, $prev ) + array( &$block, &$parent_block, $prev ) ); } @@ -1152,7 +1153,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $post_markup = call_user_func_array( $post_callback, - array( &$block, &$parent, $next ) + array( &$block, &$parent_block, $next ) ); }