Make WordPress Core

Changeset 56578


Ignore:
Timestamp:
09/14/2023 08:50:43 AM (20 months ago)
Author:
Bernhard Reiter
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r56414 r56578  
    515515
    516516/**
     517 * Injects the active theme's stylesheet as a `theme` attribute
     518 * into a given template part block.
     519 *
     520 * @since 6.4.0
     521 * @access private
     522 *
     523 * @param array $block a parsed block.
     524 * @return array Updated block.
     525 */
     526function _inject_theme_attribute_in_template_part_block( $block ) {
     527    if (
     528        'core/template-part' === $block['blockName'] &&
     529        ! isset( $block['attrs']['theme'] )
     530    ) {
     531        $block['attrs']['theme'] = get_stylesheet();
     532    }
     533    return $block;
     534}
     535
     536/**
    517537 * Parses a block template and removes the theme attribute from each template part.
    518538 *
     
    566586    $template->id             = $theme . '//' . $template_file['slug'];
    567587    $template->theme          = $theme;
    568     $template->content        = _inject_theme_attribute_in_block_template_content( $template_content );
    569588    $template->slug           = $template_file['slug'];
    570589    $template->source         = 'theme';
     
    589608        $template->area = $template_file['area'];
    590609    }
     610
     611    $blocks            = parse_blocks( $template_content );
     612    $template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );
    591613
    592614    return $template;
  • trunk/tests/phpunit/data/templates/template-with-template-part-with-existing-theme-attribute.html

    r56562 r56578  
    1 <!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->
     1<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full","tagName":"header","className":"site-header"} /-->
  • trunk/tests/phpunit/tests/block-template-utils.php

    r56562 r56578  
    209209            'a template with a template part block with an existing theme attribute' => array(
    210210                'filename' => 'template-with-template-part-with-existing-theme-attribute.html',
    211                 'expected' => '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->',
     211                'expected' => '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full","tagName":"header","className":"site-header"} /-->',
    212212            ),
    213213            'a template with no template part block' => array(
     
    217217<!-- /wp:paragraph -->',
    218218            ),
     219        );
     220    }
     221
     222    /**
     223     * @ticket 59338
     224     *
     225     * @covers ::test_inject_theme_attribute_in_template_part_block
     226     */
     227    public function test_inject_theme_attribute_in_template_part_block() {
     228        $template_part_block_without_theme_attribute = array(
     229            'blockName'    => 'core/template-part',
     230            'attrs'        => array(
     231                'slug'      => 'header',
     232                'align'     => 'full',
     233                'tagName'   => 'header',
     234                'className' => 'site-header',
     235            ),
     236            'innerHTML'    => '',
     237            'innerContent' => array(),
     238            'innerBlocks'  => array(),
     239        );
     240
     241        $actual   = _inject_theme_attribute_in_template_part_block( $template_part_block_without_theme_attribute );
     242        $expected = array(
     243            'blockName'    => 'core/template-part',
     244            'attrs'        => array(
     245                'slug'      => 'header',
     246                'align'     => 'full',
     247                'tagName'   => 'header',
     248                'className' => 'site-header',
     249                'theme'     => get_stylesheet(),
     250            ),
     251            'innerHTML'    => '',
     252            'innerContent' => array(),
     253            'innerBlocks'  => array(),
     254        );
     255        $this->assertSame(
     256            $expected,
     257            $actual,
     258            '`theme` attribute was not correctly injected in template part block.'
     259        );
     260
     261        // Does not inject theme when there is an existing theme attribute.
     262        $template_part_block_with_existing_theme_attribute = array(
     263            'blockName'    => 'core/template-part',
     264            'attrs'        => array(
     265                'slug'      => 'header',
     266                'align'     => 'full',
     267                'tagName'   => 'header',
     268                'className' => 'site-header',
     269                'theme'     => 'fake-theme',
     270            ),
     271            'innerHTML'    => '',
     272            'innerContent' => array(),
     273            'innerBlocks'  => array(),
     274        );
     275
     276        $actual = _inject_theme_attribute_in_template_part_block( $template_part_block_with_existing_theme_attribute );
     277        $this->assertSame(
     278            $template_part_block_with_existing_theme_attribute,
     279            $actual,
     280            'Existing `theme` attribute in template part block was not respected by attribute injection.'
     281        );
     282
     283        // Does not inject theme when there is no template part.
     284        $non_template_part_block = array(
     285            'blockName'    => 'core/post-content',
     286            'attrs'        => array(),
     287            'innerHTML'    => '',
     288            'innerContent' => array(),
     289            'innerBlocks'  => array(),
     290        );
     291
     292        $actual = _inject_theme_attribute_in_template_part_block( $non_template_part_block );
     293        $this->assertSame(
     294            $non_template_part_block,
     295            $actual,
     296            '`theme` attribute injection modified non-template-part block.'
    219297        );
    220298    }
Note: See TracChangeset for help on using the changeset viewer.