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.
Built from https://develop.svn.wordpress.org/trunk@56724


git-svn-id: http://core.svn.wordpress.org/trunk@56236 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Bernhard Reiter 2023-09-26 17:01:24 +00:00
parent 2c11f49301
commit dfd85ad690
3 changed files with 55 additions and 27 deletions

View File

@ -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',

View File

@ -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;
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56723';
$wp_version = '6.4-alpha-56724';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.