Make WordPress Core


Ignore:
Timestamp:
11/01/2023 12:44:02 PM (11 months ago)
Author:
Bernhard Reiter
Message:

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.

Reviewed by hellofromTonya.
Merges [56970] to the 6.4 branch.

See #59646.
Props gziolo.
Fixes #59669.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/6.4/src/wp-includes/blocks.php

    r57040 r57043  
    10641064            }
    10651065
    1066             $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
    1067 
    10681066            if ( is_callable( $post_callback ) ) {
    10691067                $next = count( $block['innerBlocks'] ) - 1 === $block_index
     
    10711069                    : $block['innerBlocks'][ $block_index + 1 ];
    10721070
    1073                 $block_content .= call_user_func_array(
     1071                $post_markup = call_user_func_array(
    10741072                    $post_callback,
    10751073                    array( &$inner_block, &$block, $next )
    10761074                );
    10771075            }
     1076
     1077            $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
     1078            $block_content .= isset( $post_markup ) ? $post_markup : '';
     1079
    10781080            ++$block_index;
    10791081        }
     
    11391141        }
    11401142
    1141         $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
    1142 
    11431143        if ( is_callable( $post_callback ) ) {
    11441144            $next = count( $blocks ) - 1 === $index
     
    11461146                : $blocks[ $index + 1 ];
    11471147
    1148             $result .= call_user_func_array(
     1148            $post_markup = call_user_func_array(
    11491149                $post_callback,
    11501150                array( &$block, &$parent_block, $next )
    11511151            );
    11521152        }
     1153
     1154        $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
     1155        $result .= isset( $post_markup ) ? $post_markup : '';
    11531156    }
    11541157
Note: See TracChangeset for help on using the changeset viewer.