Make WordPress Core


Ignore:
Timestamp:
09/27/2024 09:18:46 AM (8 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: apply_block_hooks_to_content in Patterns, Templates.

In the Patterns registry, use apply_block_hooks_to_content (introduced in [58291]) instead of the WP_Block_Patterns_Registry class's private get_content method. (The latter is removed as part of this changeset.)

In a similar vein, use apply_block_hooks_to_content in the _build_block_template_result_from_file and _build_block_template_result_from_post functions, respectively.

For that to work, apply_block_hooks_to_content is amended to inject the theme attribute into Template Part blocks, even if no hooked blocks are present.

This kind of centralization is required as a preparation for #61902.

Props bernhard-reiter, jonsurrell.
See #61902.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-patterns-registry.php

    r58186 r59101  
    160160
    161161    /**
    162      * Prepares the content of a block pattern. If hooked blocks are registered, they get injected into the pattern,
    163      * when they met the defined criteria.
    164      *
    165      * @since 6.4.0
    166      *
    167      * @param array $pattern       Registered pattern properties.
    168      * @param array $hooked_blocks The list of hooked blocks.
    169      * @return string The content of the block pattern.
    170      */
    171     private function prepare_content( $pattern, $hooked_blocks ) {
    172         $content = $pattern['content'];
    173 
    174         $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
    175         $after_block_visitor  = null;
    176         if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
    177             $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    178             $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    179         }
    180         $blocks  = parse_blocks( $content );
    181         $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
    182 
    183         return $content;
    184     }
    185 
    186     /**
    187162     * Retrieves the content of a registered block pattern.
    188163     *
     
    222197
    223198        $pattern            = $this->registered_patterns[ $pattern_name ];
    224         $pattern['content'] = $this->get_content( $pattern_name );
    225         $pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() );
     199        $content            = $this->get_content( $pattern_name );
     200        $pattern['content'] = apply_block_hooks_to_content(
     201            $content,
     202            $pattern,
     203            'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
     204        );
    226205
    227206        return $pattern;
     
    244223
    245224        foreach ( $patterns as $index => $pattern ) {
    246             $pattern['content']            = $this->get_content( $pattern['name'], $outside_init_only );
    247             $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks );
     225            $content                       = $this->get_content( $pattern['name'], $outside_init_only );
     226            $patterns[ $index ]['content'] = apply_block_hooks_to_content(
     227                $content,
     228                $pattern,
     229                'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
     230            );
    248231        }
    249232
Note: See TracChangeset for help on using the changeset viewer.