Make WordPress Core


Ignore:
Timestamp:
09/27/2024 09:18:46 AM (20 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/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
Note: See TracChangeset for help on using the changeset viewer.