Skip to content

Commit 76c6b57

Browse files
committed
Templates: Introduce _remove_theme_attribute_from_template_part_block.
Introduce a `_remove_theme_attribute_from_template_part_block()` function that can be used as a callback argument for `traverse_and_serialize_block(s)` on a parsed block tree in order to remove the `theme` attribute from all Template Part blocks found therein, and deprecate `_remove_theme_attribute_in_block_template_content()`. Counterpart to `_inject_theme_attribute_in_template_part_block` from #59338 (which superseded `_inject_theme_attribute_in_block_template_content`, deprecated in #59452). Props mukesh27. Fixes #59460. git-svn-id: https://develop.svn.wordpress.org/trunk@56724 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0ed8dcd commit 76c6b57

File tree

3 files changed

+80
-26
lines changed

3 files changed

+80
-26
lines changed

src/wp-includes/block-template-utils.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -490,36 +490,21 @@ function _inject_theme_attribute_in_template_part_block( &$block ) {
490490
}
491491

492492
/**
493-
* Parses a block template and removes the theme attribute from each template part.
493+
* Removes the `theme` attribute from a given template part block.
494494
*
495-
* @since 5.9.0
495+
* @since 6.4.0
496496
* @access private
497497
*
498-
* @param string $template_content Serialized block template content.
499-
* @return string Updated block template content.
498+
* @param array $block a parsed block.
499+
* @return void
500500
*/
501-
function _remove_theme_attribute_in_block_template_content( $template_content ) {
502-
$has_updated_content = false;
503-
$new_content = '';
504-
$template_blocks = parse_blocks( $template_content );
505-
506-
$blocks = _flatten_blocks( $template_blocks );
507-
foreach ( $blocks as $key => $block ) {
508-
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
509-
unset( $blocks[ $key ]['attrs']['theme'] );
510-
$has_updated_content = true;
511-
}
512-
}
513-
514-
if ( ! $has_updated_content ) {
515-
return $template_content;
516-
}
517-
518-
foreach ( $template_blocks as $block ) {
519-
$new_content .= serialize_block( $block );
501+
function _remove_theme_attribute_from_template_part_block( &$block ) {
502+
if (
503+
'core/template-part' === $block['blockName'] &&
504+
isset( $block['attrs']['theme'] )
505+
) {
506+
unset( $block['attrs']['theme'] );
520507
}
521-
522-
return $new_content;
523508
}
524509

525510
/**
@@ -1278,7 +1263,10 @@ function wp_generate_block_templates_export_file() {
12781263
// Load templates into the zip file.
12791264
$templates = get_block_templates();
12801265
foreach ( $templates as $template ) {
1281-
$template->content = _remove_theme_attribute_in_block_template_content( $template->content );
1266+
$template->content = traverse_and_serialize_blocks(
1267+
parse_blocks( $template->content ),
1268+
'_remove_theme_attribute_from_template_part_block'
1269+
);
12821270

12831271
$zip->addFromString(
12841272
'templates/' . $template->slug . '.html',

src/wp-includes/deprecated.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6084,3 +6084,43 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
60846084

60856085
return $template_content;
60866086
}
6087+
6088+
/**
6089+
* Parses a block template and removes the theme attribute from each template part.
6090+
*
6091+
* @since 5.9.0
6092+
* @deprecated 6.4.0 Use traverse_and_serialize_blocks( parse_blocks( $template_content ), '_remove_theme_attribute_from_template_part_block' ) instead.
6093+
* @access private
6094+
*
6095+
* @param string $template_content Serialized block template content.
6096+
* @return string Updated block template content.
6097+
*/
6098+
function _remove_theme_attribute_in_block_template_content( $template_content ) {
6099+
_deprecated_function(
6100+
__FUNCTION__,
6101+
'6.4.0',
6102+
'traverse_and_serialize_blocks( parse_blocks( $template_content ), "_remove_theme_attribute_from_template_part_block" )'
6103+
);
6104+
6105+
$has_updated_content = false;
6106+
$new_content = '';
6107+
$template_blocks = parse_blocks( $template_content );
6108+
6109+
$blocks = _flatten_blocks( $template_blocks );
6110+
foreach ( $blocks as $key => $block ) {
6111+
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
6112+
unset( $blocks[ $key ]['attrs']['theme'] );
6113+
$has_updated_content = true;
6114+
}
6115+
}
6116+
6117+
if ( ! $has_updated_content ) {
6118+
return $template_content;
6119+
}
6120+
6121+
foreach ( $template_blocks as $block ) {
6122+
$new_content .= serialize_block( $block );
6123+
}
6124+
6125+
return $new_content;
6126+
}

tests/phpunit/tests/block-template-utils.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,39 @@ public function test_inject_theme_attribute_in_block_template_content() {
361361

362362
/**
363363
* @ticket 54448
364+
* @ticket 59460
364365
*
365366
* @dataProvider data_remove_theme_attribute_in_block_template_content
367+
*
368+
* @expectedDeprecated _remove_theme_attribute_in_block_template_content
366369
*/
367370
public function test_remove_theme_attribute_in_block_template_content( $template_content, $expected ) {
368371
$this->assertSame( $expected, _remove_theme_attribute_in_block_template_content( $template_content ) );
369372
}
370373

374+
/**
375+
* @ticket 59460
376+
*
377+
* @covers ::_remove_theme_attribute_from_template_part_block
378+
* @covers ::traverse_and_serialize_blocks
379+
*
380+
* @dataProvider data_remove_theme_attribute_in_block_template_content
381+
*
382+
* @param string $template_content The template markup.
383+
* @param string $expected The expected markup after removing the theme attribute from Template Part blocks.
384+
*/
385+
public function test_remove_theme_attribute_from_template_part_block( $template_content, $expected ) {
386+
$template_content_parsed_blocks = parse_blocks( $template_content );
387+
388+
$this->assertSame(
389+
$expected,
390+
traverse_and_serialize_blocks(
391+
$template_content_parsed_blocks,
392+
'_remove_theme_attribute_from_template_part_block'
393+
)
394+
);
395+
}
396+
371397
public function data_remove_theme_attribute_in_block_template_content() {
372398
return array(
373399
array(

0 commit comments

Comments
 (0)