Make WordPress Core

Changeset 56818


Ignore:
Timestamp:
10/10/2023 01:19:42 PM (6 months ago)
Author:
Bernhard Reiter
Message:

Patterns: Inject theme attribute into Template Part blocks.

[56805] introduced a regression: The theme attribute was no longer injected into Template Part blocks inside of patterns. This caused errors on the frontend, where instead of a given template part, an error message such as Template part has been deleted or is unavailable: header was seen.

This changeset rectifies that problem, and adds unit test coverage to guard against future regressions.

Follow-up to [56805].
Props scruffian, gziolo.
Fixes #59583.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-patterns-registry.php

    r56805 r56818  
    165165    private function prepare_content( $pattern, $hooked_blocks ) {
    166166        $content = $pattern['content'];
     167
     168        $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
     169        $after_block_visitor  = null;
    167170        if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
    168             $blocks               = parse_blocks( $content );
    169171            $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern );
    170172            $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $pattern );
    171             $content              = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
    172         }
     173        }
     174        $blocks  = parse_blocks( $content );
     175        $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
    173176
    174177        return $content;
  • trunk/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php

    r56733 r56818  
    318318
    319319    /**
     320     * Should insert a theme attribute into Template Part blocks in registered patterns.
     321     *
     322     * @ticket 59583
     323     *
     324     * @covers WP_Block_Patterns_Registry::register
     325     * @covers WP_Block_Patterns_Registry::get_all_registered
     326     */
     327    public function test_get_all_registered_includes_theme_attribute() {
     328        $test_pattern = array(
     329            'title'   => 'Test Pattern',
     330            'content' => '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->',
     331        );
     332        $this->registry->register( 'test/pattern', $test_pattern );
     333
     334        $expected = sprintf(
     335            '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
     336            get_stylesheet()
     337        );
     338        $patterns = $this->registry->get_all_registered();
     339        $this->assertSame( $expected, $patterns[0]['content'] );
     340    }
     341
     342    /**
    320343     * Should insert hooked blocks into registered patterns.
    321344     *
     
    370393
    371394    /**
     395     * Should insert a theme attribute into Template Part blocks in registered patterns.
     396     *
     397     * @ticket 59583
     398     *
     399     * @covers WP_Block_Patterns_Registry::register
     400     * @covers WP_Block_Patterns_Registry::get_registered
     401     */
     402    public function test_get_registered_includes_theme_attribute() {
     403        $test_pattern = array(
     404            'title'   => 'Test Pattern',
     405            'content' => '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->',
     406        );
     407        $this->registry->register( 'test/pattern', $test_pattern );
     408
     409        $expected = sprintf(
     410            '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
     411            get_stylesheet()
     412        );
     413        $pattern  = $this->registry->get_registered( 'test/pattern' );
     414        $this->assertSame( $expected, $pattern['content'] );
     415    }
     416
     417    /**
    372418     * Should insert hooked blocks into registered patterns.
    373419     *
Note: See TracChangeset for help on using the changeset viewer.