Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Parts: Fix theme slug injection for patterns #5450

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 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,16 @@ 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;
$hooked_blocks = get_hooked_blocks();
ockham marked this conversation as resolved.
Show resolved Hide resolved
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
23 changes: 23 additions & 0 deletions tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,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 XXXXX
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBD

*
* @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
Loading