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';
}
/*
* 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;
}
@ -229,6 +242,21 @@ class BlockTemplateUtils {
$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 );
/*
* 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;
}
@ -391,7 +419,7 @@ class BlockTemplateUtils {
// or the stylesheet directory for child themes.
$possible_paths = array_reduce(
$possible_templates_dir,
function( $carry, $item ) use ( $template_filename ) {
function ( $carry, $item ) use ( $template_filename ) {
$filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename;
$carry[] = get_stylesheet_directory() . $filepath;
@ -570,13 +598,13 @@ class BlockTemplateUtils {
// Get the slugs of all templates that have been customised and saved in the database.
$customised_template_slugs = array_map(
function( $template ) {
function ( $template ) {
return $template->slug;
},
array_values(
array_filter(
$templates,
function( $template ) {
function ( $template ) {
// This template has been customised and saved as a post.
return 'custom' === $template->source;
}
@ -591,7 +619,7 @@ class BlockTemplateUtils {
return array_values(
array_filter(
$templates,
function( $template ) use ( $customised_template_slugs ) {
function ( $template ) use ( $customised_template_slugs ) {
// This template has been customised and saved as a post, so return it.
return ! ( 'theme' === $template->source && in_array( $template->slug, $customised_template_slugs, true ) );
}
@ -611,7 +639,7 @@ class BlockTemplateUtils {
public static function remove_duplicate_customized_templates( $templates, $theme_slug ) {
$filtered_templates = array_filter(
$templates,
function( $template ) use ( $templates, $theme_slug ) {
function ( $template ) use ( $templates, $theme_slug ) {
if ( $template->theme === $theme_slug ) {
// This is a customized template based on the theme template, so it should be returned.
return true;
@ -620,7 +648,7 @@ class BlockTemplateUtils {
// Only return it if there isn't a customized version of the theme template.
$is_there_a_customized_theme_template = array_filter(
$templates,
function( $theme_template ) use ( $template, $theme_slug ) {
function ( $theme_template ) use ( $template, $theme_slug ) {
return $theme_template->slug === $template->slug && $theme_template->theme === $theme_slug;
}
);
@ -700,7 +728,7 @@ class BlockTemplateUtils {
$saved_woo_templates = $check_query->posts;
return array_map(
function( $saved_woo_template ) {
function ( $saved_woo_template ) {
return self::build_template_result_from_post( $saved_woo_template );
},
$saved_woo_templates