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

Templates: Introduce _remove_theme_attribute_from_template_part_block function #5316

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
40 changes: 14 additions & 26 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,36 +490,21 @@ function _inject_theme_attribute_in_template_part_block( &$block ) {
}

/**
* Parses a block template and removes the theme attribute from each template part.
* Removes the `theme` attribute from a given template part block.
*
* @since 5.9.0
* @since 6.4.0
* @access private
*
* @param string $template_content Serialized block template content.
* @return string Updated block template content.
* @param array $block a parsed block.
* @return void
*/
function _remove_theme_attribute_in_block_template_content( $template_content ) {
$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _flatten_blocks( $template_blocks );
foreach ( $blocks as $key => $block ) {
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
unset( $blocks[ $key ]['attrs']['theme'] );
$has_updated_content = true;
}
}

if ( ! $has_updated_content ) {
return $template_content;
}

foreach ( $template_blocks as $block ) {
$new_content .= serialize_block( $block );
function _remove_theme_attribute_from_template_part_block( &$block ) {
if (
'core/template-part' === $block['blockName'] &&
isset( $block['attrs']['theme'] )
) {
unset( $block['attrs']['theme'] );
}

return $new_content;
}

/**
Expand Down Expand Up @@ -1278,7 +1263,10 @@ function wp_generate_block_templates_export_file() {
// Load templates into the zip file.
$templates = get_block_templates();
foreach ( $templates as $template ) {
$template->content = _remove_theme_attribute_in_block_template_content( $template->content );
$template->content = traverse_and_serialize_blocks(
parse_blocks( $template->content ),
'_remove_theme_attribute_from_template_part_block'
);

$zip->addFromString(
'templates/' . $template->slug . '.html',
Expand Down
40 changes: 40 additions & 0 deletions src/wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6084,3 +6084,43 @@ function _inject_theme_attribute_in_block_template_content( $template_content )

return $template_content;
}

/**
* Parses a block template and removes the theme attribute from each template part.
*
* @since 5.9.0
* @deprecated 6.4.0 Use traverse_and_serialize_blocks( parse_blocks( $template_content ), '_remove_theme_attribute_from_template_part_block' ) instead.
* @access private
*
* @param string $template_content Serialized block template content.
* @return string Updated block template content.
*/
function _remove_theme_attribute_in_block_template_content( $template_content ) {
_deprecated_function(
__FUNCTION__,
'6.4.0',
'traverse_and_serialize_blocks( parse_blocks( $template_content ), "_remove_theme_attribute_from_template_part_block" )'
);

$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _flatten_blocks( $template_blocks );
foreach ( $blocks as $key => $block ) {
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
unset( $blocks[ $key ]['attrs']['theme'] );
$has_updated_content = true;
}
}

if ( ! $has_updated_content ) {
return $template_content;
}

foreach ( $template_blocks as $block ) {
$new_content .= serialize_block( $block );
}

return $new_content;
}
26 changes: 26 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,39 @@ public function test_inject_theme_attribute_in_block_template_content() {

/**
* @ticket 54448
* @ticket 59460
*
* @dataProvider data_remove_theme_attribute_in_block_template_content
*
* @expectedDeprecated _remove_theme_attribute_in_block_template_content
*/
public function test_remove_theme_attribute_in_block_template_content( $template_content, $expected ) {
$this->assertSame( $expected, _remove_theme_attribute_in_block_template_content( $template_content ) );
}

/**
* @ticket 59460
*
* @covers ::_remove_theme_attribute_from_template_part_block
* @covers ::traverse_and_serialize_blocks
*
* @dataProvider data_remove_theme_attribute_in_block_template_content
*
* @param string $template_content The template markup.
* @param string $expected The expected markup after removing the theme attribute from Template Part blocks.
*/
public function test_remove_theme_attribute_from_template_part_block( $template_content, $expected ) {
$template_content_parsed_blocks = parse_blocks( $template_content );

$this->assertSame(
$expected,
traverse_and_serialize_blocks(
$template_content_parsed_blocks,
'_remove_theme_attribute_from_template_part_block'
)
);
}

public function data_remove_theme_attribute_in_block_template_content() {
return array(
array(
Expand Down
Loading