Skip to content

Commit

Permalink
Block Hooks: Expose serialized template content to filter.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/branches/6.4@57119 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ockham committed Nov 17, 2023
1 parent 4ac4256 commit 1029dde
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,12 @@ function _remove_theme_attribute_from_template_part_block( &$block ) {
*/
function _build_block_template_result_from_file( $template_file, $template_type ) {
$default_template_types = get_default_block_template_types();
$template_content = file_get_contents( $template_file['path'] );
$theme = get_stylesheet();

$template = new WP_Block_Template();
$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->content = file_get_contents( $template_file['path'] );
$template->slug = $template_file['slug'];
$template->source = 'theme';
$template->type = $template_type;
Expand Down Expand Up @@ -554,7 +554,7 @@ function _build_block_template_result_from_file( $template_file, $template_type
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template );
}
$blocks = parse_blocks( $template_content );
$blocks = parse_blocks( $template->content );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );

return $template;
Expand Down

0 comments on commit 1029dde

Please sign in to comment.