Make WordPress Core

Changeset 57118


Ignore:
Timestamp:
11/17/2023 07:08:47 AM (5 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).

Props nerrad.
Fixes #59882.

File:
1 edited

Legend:

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

    r56960 r57118  
    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    $template->content        = file_get_contents( $template_file['path'] );
    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( $template->content );
    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.