Make WordPress Core

Changeset 59101


Ignore:
Timestamp:
09/27/2024 09:18:46 AM (23 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.

Location:
trunk
Files:
1 added
3 edited

Legend:

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

    r59073 r59101  
    616616        }
    617617
    618         $hooked_blocks        = get_hooked_blocks();
    619         $has_hooked_blocks    = ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' );
    620         $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
    621         $after_block_visitor  = null;
    622 
    623         if ( $has_hooked_blocks ) {
    624                 $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    625                 $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    626         }
    627 
    628         if ( 'wp_template_part' === $template->type && $has_hooked_blocks ) {
     618        if ( 'wp_template_part' === $template->type ) {
    629619                /*
    630620                 * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
     
    636626                        $template->content
    637627                );
    638                 $content           = traverse_and_serialize_blocks( parse_blocks( $content ), $before_block_visitor, $after_block_visitor );
     628                $content           = apply_block_hooks_to_content(
     629                        $content,
     630                        $template,
     631                        'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
     632                );
    639633                $template->content = remove_serialized_parent_block( $content );
    640634        } else {
    641                 $template->content = traverse_and_serialize_blocks(
    642                         parse_blocks( $template->content ),
    643                         $before_block_visitor,
    644                         $after_block_visitor
     635                $template->content = apply_block_hooks_to_content(
     636                        $template->content,
     637                        $template,
     638                        'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
    645639                );
    646640        }
     
    10371031        }
    10381032
    1039         $hooked_blocks = get_hooked_blocks();
    1040         if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
    1041                 $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    1042                 $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
    1043                 if ( 'wp_template_part' === $template->type ) {
    1044                         $existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
    1045                         $attributes                     = ! empty( $existing_ignored_hooked_blocks ) ? array( 'metadata' => array( 'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ) ) ) : array();
    1046 
    1047                         /*
    1048                          * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
    1049                          * we need to wrap its content a mock template part block and traverse it.
    1050                          */
    1051                         $content           = get_comment_delimited_block_content(
    1052                                 'core/template-part',
    1053                                 $attributes,
    1054                                 $template->content
    1055                         );
    1056                         $content           = traverse_and_serialize_blocks( parse_blocks( $content ), $before_block_visitor, $after_block_visitor );
    1057                         $template->content = remove_serialized_parent_block( $content );
    1058                 } else {
    1059                         $template->content = traverse_and_serialize_blocks(
    1060                                 parse_blocks( $template->content ),
    1061                                 $before_block_visitor,
    1062                                 $after_block_visitor
    1063                         );
    1064                 }
     1033        if ( 'wp_template_part' === $template->type ) {
     1034                $existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
     1035                $attributes                     = ! empty( $existing_ignored_hooked_blocks ) ? array( 'metadata' => array( 'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ) ) ) : array();
     1036
     1037                /*
     1038                 * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
     1039                 * we need to wrap its content a mock template part block and traverse it.
     1040                 */
     1041                $content           = get_comment_delimited_block_content(
     1042                        'core/template-part',
     1043                        $attributes,
     1044                        $template->content
     1045                );
     1046                $content           = apply_block_hooks_to_content(
     1047                        $content,
     1048                        $template,
     1049                        'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
     1050                );
     1051                $template->content = remove_serialized_parent_block( $content );
     1052        } else {
     1053                $template->content = apply_block_hooks_to_content(
     1054                        $template->content,
     1055                        $template,
     1056                        'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
     1057                );
    10651058        }
    10661059
  • trunk/src/wp-includes/blocks.php

    r59081 r59101  
    10361036 *
    10371037 * @since 6.6.0
     1038 * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered.
    10381039 * @access private
    10391040 *
     
    10481049function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) {
    10491050        $hooked_blocks = get_hooked_blocks();
    1050         if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
    1051                 return $content;
     1051
     1052        $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
     1053        $after_block_visitor  = null;
     1054        if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
     1055                $before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
     1056                $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $context, $callback );
    10521057        }
    10531058
    10541059        $blocks = parse_blocks( $content );
    1055 
    1056         $before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
    1057         $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $context, $callback );
    10581060
    10591061        return traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
  • 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.