Make WordPress Core

Changeset 56724


Ignore:
Timestamp:
09/26/2023 04:59:11 PM (12 months 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.

Location:
trunk
Files:
3 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(
  • trunk/src/wp-includes/deprecated.php

    r56722 r56724  
    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 */
     6098function _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}
  • trunk/tests/phpunit/tests/block-template-utils.php

    r56719 r56724  
    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 ) );
     372    }
     373
     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        );
    369395    }
    370396
Note: See TracChangeset for help on using the changeset viewer.