Make WordPress Core

Changeset 57119

Timestamp:
11/17/2023 07:15:35 AM (9 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Expose serialized template content to filter.

The recently introduced Block Hooks API exposes a filter (hooked_block_types) which is given a $context argument, among others. If the filter is called on a block that's part of a template or template part, $context is set to the corresponding WP_Block_Template object.

However, that object's $content property is currently not exposed to the filter. This changeset amends that shortcoming.

This is useful for callbacks that might want to detect the presence of a serialized block instance (or potentially in the future utilize the HTML API) to restrict where the block is injected (before the template is rendered).

Addressing this also achieves parity with the structure of $context when it represents a pattern (where pattern serialized content is present).

Merges [57118] to the 6.4 branch.

Props nerrad.
Fixes #59882.

File:
1 edited

Legend:

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

    r56960 r57119  
    519519function _build_block_template_result_from_file( $template_file, $template_type ) {
    520520    $default_template_types = get_default_block_template_types();
    521     $template_content       = file_get_contents( $template_file['path'] );
    522521    $theme                  = get_stylesheet();
    523522
     
    525524    $template->id             = $theme . '//' . $template_file['slug'];
    526525    $template->theme          = $theme;
     526
    527527    $template->slug           = $template_file['slug'];
    528528    $template->source         = 'theme';
     
    555555        $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template );
    556556    }
    557     $blocks            = parse_blocks( $template_content );
     557    $blocks            = parse_blocks( $templatecontent );
    558558    $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
    559559
Note: See TracChangeset for help on using the changeset viewer.