diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 065cfcc037..98c151048a 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -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; - } +function _remove_theme_attribute_from_template_part_block( &$block ) { + if ( + 'core/template-part' === $block['blockName'] && + isset( $block['attrs']['theme'] ) + ) { + unset( $block['attrs']['theme'] ); } - - if ( ! $has_updated_content ) { - return $template_content; - } - - foreach ( $template_blocks as $block ) { - $new_content .= serialize_block( $block ); - } - - return $new_content; } /** @@ -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', diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 0375d69b4f..9f8981d663 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -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; +} diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index e5d4a7cdc8..b06e931529 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -361,13 +361,39 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase { /** * @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(