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

Inject theme attribute during serialization #5192

Closed
Show file tree
Hide file tree
Changes from all 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
24 changes: 23 additions & 1 deletion src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,26 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
return $template_content;
}

/**
* Injects the active theme's stylesheet as a `theme` attribute
* into a given template part block.
*
* @since 6.4.0
* @access private
*
* @param array $block a parsed block.
* @return array Updated block.
*/
function _inject_theme_attribute_in_template_part_block( $block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = get_stylesheet();
}
return $block;
}

/**
* Parses a block template and removes the theme attribute from each template part.
*
Expand Down Expand Up @@ -565,7 +585,6 @@ function _build_block_template_result_from_file( $template_file, $template_type
$template = new WP_Block_Template();
$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->content = _inject_theme_attribute_in_block_template_content( $template_content );
$template->slug = $template_file['slug'];
$template->source = 'theme';
$template->type = $template_type;
Expand All @@ -589,6 +608,9 @@ function _build_block_template_result_from_file( $template_file, $template_type
$template->area = $template_file['area'];
}

$blocks = parse_blocks( $template_content );
$template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );

return $template;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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"} /-->
80 changes: 79 additions & 1 deletion tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function data_build_block_template_result_from_file_injects_theme_attribu
),
'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',
Expand All @@ -219,6 +219,84 @@ public function data_build_block_template_result_from_file_injects_theme_attribu
);
}

/**
* @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"} /-->';
Expand Down