Themes: Inject theme attribute during serialization.

Rather than using `_inject_theme_attribute_in_block_template_content` to inject the `theme` attribute into all Template Part blocks found in a given file-based Block Template, introduce a new function called `_inject_theme_attribute_in_template_part_block`, and use that as second argument to `serialize_blocks()` (introduced in [56557]) in order to inject said attribute during tree traversal for serialization.

This allows for a more modular approach that will eventually be extended to implement automatic insertion of hooked blocks.

Note that we're guarding `_build_block_template_result_from_file()` (i.e. the callsite of `_inject_theme_attribute_in_template_part_block` and previously of `_inject_theme_attribute_in_block_template_content`) against regressions through additional unit test coverage added in [56562].

Props @gziolo.
Fixes #59338. See #59313.

git-svn-id: https://develop.svn.wordpress.org/trunk@56578 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Bernie Reiter
2023-09-14 08:50:43 +00:00
parent 11c53d559b
commit bbb85de12d
3 changed files with 103 additions and 3 deletions

View File

@@ -1 +1 @@
<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->
<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full","tagName":"header","className":"site-header"} /-->

View File

@@ -208,7 +208,7 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
),
'a template with a template part block with an existing theme attribute' => array(
'filename' => 'template-with-template-part-with-existing-theme-attribute.html',
'expected' => '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->',
'expected' => '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full","tagName":"header","className":"site-header"} /-->',
),
'a template with no template part block' => array(
'filename' => 'template.html',
@@ -219,6 +219,84 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
);
}
/**
* @ticket 59338
*
* @covers ::test_inject_theme_attribute_in_template_part_block
*/
public function test_inject_theme_attribute_in_template_part_block() {
$template_part_block_without_theme_attribute = array(
'blockName' => 'core/template-part',
'attrs' => array(
'slug' => 'header',
'align' => 'full',
'tagName' => 'header',
'className' => 'site-header',
),
'innerHTML' => '',
'innerContent' => array(),
'innerBlocks' => array(),
);
$actual = _inject_theme_attribute_in_template_part_block( $template_part_block_without_theme_attribute );
$expected = array(
'blockName' => 'core/template-part',
'attrs' => array(
'slug' => 'header',
'align' => 'full',
'tagName' => 'header',
'className' => 'site-header',
'theme' => get_stylesheet(),
),
'innerHTML' => '',
'innerContent' => array(),
'innerBlocks' => array(),
);
$this->assertSame(
$expected,
$actual,
'`theme` attribute was not correctly injected in template part block.'
);
// Does not inject theme when there is an existing theme attribute.
$template_part_block_with_existing_theme_attribute = array(
'blockName' => 'core/template-part',
'attrs' => array(
'slug' => 'header',
'align' => 'full',
'tagName' => 'header',
'className' => 'site-header',
'theme' => 'fake-theme',
),
'innerHTML' => '',
'innerContent' => array(),
'innerBlocks' => array(),
);
$actual = _inject_theme_attribute_in_template_part_block( $template_part_block_with_existing_theme_attribute );
$this->assertSame(
$template_part_block_with_existing_theme_attribute,
$actual,
'Existing `theme` attribute in template part block was not respected by attribute injection.'
);
// Does not inject theme when there is no template part.
$non_template_part_block = array(
'blockName' => 'core/post-content',
'attrs' => array(),
'innerHTML' => '',
'innerContent' => array(),
'innerBlocks' => array(),
);
$actual = _inject_theme_attribute_in_template_part_block( $non_template_part_block );
$this->assertSame(
$non_template_part_block,
$actual,
'`theme` attribute injection modified non-template-part block.'
);
}
public function test_inject_theme_attribute_in_block_template_content() {
$theme = get_stylesheet();
$content_without_theme_attribute = '<!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /-->';