From f2d828f49aa4ef6b7433f93681a4c8d47f7e9572 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 21 Mar 2024 08:50:40 +0000 Subject: [PATCH] Block Hooks API: Add the block hooks algorithm to Woo templates (#45737) Co-authored-by: github-actions --- ...37-add-block-hooks-api-woo-block-templates | 4 ++ .../src/Blocks/Utils/BlockTemplateUtils.php | 42 +++++++++++++++---- 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 plugins/woocommerce/changelog/45737-add-block-hooks-api-woo-block-templates diff --git a/plugins/woocommerce/changelog/45737-add-block-hooks-api-woo-block-templates b/plugins/woocommerce/changelog/45737-add-block-hooks-api-woo-block-templates new file mode 100644 index 00000000000..9f1b2033ebc --- /dev/null +++ b/plugins/woocommerce/changelog/45737-add-block-hooks-api-woo-block-templates @@ -0,0 +1,4 @@ +Significance: major +Type: add + +Adds block hooks algorithm to WooCommerce templates. \ No newline at end of file diff --git a/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php b/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php index 50beb6560e4..a6cdbe5c829 100644 --- a/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php +++ b/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php @@ -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