Make WordPress Core


Ignore:
Timestamp:
09/26/2023 04:59:11 PM (3 years ago)
Author:
Bernhard Reiter
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r56722 r56724  
    491491
    492492/**
    493  * Parses a block template and removes the theme attribute from each template part.
    494  *
    495  * @since 5.9.0
     493 * Removes the `theme` attribute from a given template part block.
     494 *
     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.
    500  */
    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 );
    520         }
    521 
    522         return $new_content;
     498 * @param array $block a parsed block.
     499 * @return void
     500 */
     501function _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'] );
     507        }
    523508}
    524509
    … …  
    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(
Note: See TracChangeset for help on using the changeset viewer.