Skip to content

Commit

Permalink
Patterns: Inject theme attribute into Template Part blocks.
Browse files Browse the repository at this point in the history
[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.

git-svn-id: https://develop.svn.wordpress.org/trunk@56818 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ockham committed Oct 10, 2023
1 parent 4984081 commit a9bb470
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/wp-includes/class-wp-block-patterns-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,15 @@ public function unregister( $pattern_name ) {
*/
private function prepare_content( $pattern, $hooked_blocks ) {
$content = $pattern['content'];

$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
$after_block_visitor = null;
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$blocks = parse_blocks( $content );
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern );
$content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
}
$blocks = parse_blocks( $content );
$content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );

return $content;
}
Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,29 @@ public function test_get_registered() {
$this->assertSame( $pattern_two, $pattern );
}

/**
* Should insert a theme attribute into Template Part blocks in registered patterns.
*
* @ticket 59583
*
* @covers WP_Block_Patterns_Registry::register
* @covers WP_Block_Patterns_Registry::get_all_registered
*/
public function test_get_all_registered_includes_theme_attribute() {
$test_pattern = array(
'title' => 'Test Pattern',
'content' => '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->',
);
$this->registry->register( 'test/pattern', $test_pattern );

$expected = sprintf(
'<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
get_stylesheet()
);
$patterns = $this->registry->get_all_registered();
$this->assertSame( $expected, $patterns[0]['content'] );
}

/**
* Should insert hooked blocks into registered patterns.
*
Expand Down Expand Up @@ -368,6 +391,29 @@ public function test_get_all_registered_includes_hooked_blocks() {
$this->assertSame( $expected, $registered );
}

/**
* Should insert a theme attribute into Template Part blocks in registered patterns.
*
* @ticket 59583
*
* @covers WP_Block_Patterns_Registry::register
* @covers WP_Block_Patterns_Registry::get_registered
*/
public function test_get_registered_includes_theme_attribute() {
$test_pattern = array(
'title' => 'Test Pattern',
'content' => '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->',
);
$this->registry->register( 'test/pattern', $test_pattern );

$expected = sprintf(
'<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
get_stylesheet()
);
$pattern = $this->registry->get_registered( 'test/pattern' );
$this->assertSame( $expected, $pattern['content'] );
}

/**
* Should insert hooked blocks into registered patterns.
*
Expand Down

0 comments on commit a9bb470

Please sign in to comment.