From b0da2934b925077ab95cd36cad4217ed214665b1 Mon Sep 17 00:00:00 2001 From: Bernhard Reiter Date: Wed, 18 Oct 2023 19:32:23 +0000 Subject: [PATCH] Blocks: During traversal, allow post callback to modify block. Both the `$pre_callback` and `$post_callback` functions that are given as arguments to `traverse_and_serialize_block(s)` receive a reference to the current block as their first argument. However, while any changes that the "pre" callback makes to the block are reflected by the serialized markup, the same wasn't true for the "post" callback: Any changes that it made were only applied ''after'' the block had already been serialized. This commit changes the behavior such that `$post_callback`'s changes to the current block are also reflected in the serialized markup. See #59646. Props gziolo. Fixes #59669. Built from https://develop.svn.wordpress.org/trunk@56970 git-svn-id: http://core.svn.wordpress.org/trunk@56481 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 15 +++++++++------ wp-includes/version.php | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index ecb4fa5b3d..01cc2070ac 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -1062,18 +1062,20 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb ); } - $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback ); - if ( is_callable( $post_callback ) ) { $next = count( $block['innerBlocks'] ) - 1 === $block_index ? null : $block['innerBlocks'][ $block_index + 1 ]; - $block_content .= call_user_func_array( + $post_markup = call_user_func_array( $post_callback, array( &$inner_block, $block, $next ) ); } + + $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback ); + $block_content .= isset( $post_markup ) ? $post_markup : ''; + ++$block_index; } } @@ -1135,18 +1137,19 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal ); } - $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback ); - if ( is_callable( $post_callback ) ) { $next = count( $blocks ) - 1 === $index ? null : $blocks[ $index + 1 ]; - $result .= call_user_func_array( + $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. ); } + + $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback ); + $result .= isset( $post_markup ) ? $post_markup : ''; } return $result; diff --git a/wp-includes/version.php b/wp-includes/version.php index df5bf7b85d..24f2f5650c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-56969'; +$wp_version = '6.5-alpha-56970'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.