Blocks: Change traverse_and_serialize_block(s)'s callback signature.

During work on #59399, it was discovered that ''sibling'' block insertion wasn't likely going to work the way it was planned, which required devising an alternative solution. This new solution requires some changes to `traverse_and_serialize_block(s)`:

- Change the signature of the existing callback such that:
  - the return value is a string that will be prepended to the result of the inner block traversal and serialization;
  - the function arguments are: a ''reference'' to the current block (so it can be modified inline, which is important e.g. for `theme` attribute insertion), the parent block, and the previous block (instead of the block index and chunk index).
- Add a second callback argument to `traverse_and_serialize_block(s)`, which is called ''after'' the block is traversed and serialized.
  - Its function arguments are a reference to the current block, the parent block, and the next block.

Props gziolo.
Fixes #59412. See #59313.

git-svn-id: https://develop.svn.wordpress.org/trunk@56644 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Bernie Reiter
2023-09-21 08:32:52 +00:00
parent 8fec1ec36e
commit aa033cba5c
4 changed files with 110 additions and 51 deletions

View File

@@ -225,7 +225,7 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
* @covers ::_inject_theme_attribute_in_template_part_block
*/
public function test_inject_theme_attribute_in_template_part_block() {
$template_part_block_without_theme_attribute = array(
$template_part_block = array(
'blockName' => 'core/template-part',
'attrs' => array(
'slug' => 'header',
@@ -238,7 +238,7 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
'innerBlocks' => array(),
);
$actual = _inject_theme_attribute_in_template_part_block( $template_part_block_without_theme_attribute );
_inject_theme_attribute_in_template_part_block( $template_part_block );
$expected = array(
'blockName' => 'core/template-part',
'attrs' => array(
@@ -254,7 +254,7 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
);
$this->assertSame(
$expected,
$actual,
$template_part_block,
'`theme` attribute was not correctly injected in template part block.'
);
}
@@ -265,7 +265,7 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
* @covers ::_inject_theme_attribute_in_template_part_block
*/
public function test_not_inject_theme_attribute_in_template_part_block_theme_attribute_exists() {
$template_part_block_with_existing_theme_attribute = array(
$template_part_block = array(
'blockName' => 'core/template-part',
'attrs' => array(
'slug' => 'header',
@@ -279,10 +279,11 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
'innerBlocks' => array(),
);
$actual = _inject_theme_attribute_in_template_part_block( $template_part_block_with_existing_theme_attribute );
$expected = $template_part_block;
_inject_theme_attribute_in_template_part_block( $template_part_block );
$this->assertSame(
$template_part_block_with_existing_theme_attribute,
$actual,
$expected,
$template_part_block,
'Existing `theme` attribute in template part block was not respected by attribute injection.'
);
}
@@ -301,10 +302,11 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
'innerBlocks' => array(),
);
$actual = _inject_theme_attribute_in_template_part_block( $non_template_part_block );
$expected = $non_template_part_block;
_inject_theme_attribute_in_template_part_block( $non_template_part_block );
$this->assertSame(
$expected,
$non_template_part_block,
$actual,
'`theme` attribute injection modified non-template-part block.'
);
}