Block Hooks API: Add the block hooks algorithm to Woo templates (#45737)

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Tom Cafferkey 2024-03-21 08:50:40 +00:00 committed by GitHub
parent e4017f0800
commit f2d828f49a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 7 deletions

View File

@ -0,0 +1,4 @@
Significance: major
Type: add
Adds block hooks algorithm to WooCommerce templates.

View File

@ -184,6 +184,19 @@ class BlockTemplateUtils {
$template->origin = 'plugin'; $template->origin = 'plugin';
} }
/*
* Run the block hooks algorithm introduced in WP 6.4 on the template content.
*/
if ( function_exists( 'inject_ignored_hooked_blocks_metadata_attributes' ) ) {
$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$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 );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
}
}
return $template; return $template;
} }
@ -229,6 +242,21 @@ class BlockTemplateUtils {
$template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
$template->area = self::get_block_template_area( $template->slug, $template_type ); $template->area = self::get_block_template_area( $template->slug, $template_type );
/*
* Run the block hooks algorithm introduced in WP 6.4 on the template content.
*/
if ( function_exists( 'inject_ignored_hooked_blocks_metadata_attributes' ) ) {
$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
$after_block_visitor = null;
$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$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 );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
}
return $template; return $template;
} }