Skip to content

Commit

Permalink
Block Hooks API: Add the block hooks algorithm to Woo templates (#45737)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
tjcafferkey and github-actions committed Mar 21, 2024
1 parent e4017f0 commit f2d828f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: major
Type: add

Adds block hooks algorithm to WooCommerce templates.
42 changes: 35 additions & 7 deletions plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ public static function build_template_result_from_post( $post ) {
$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;
}

Expand Down Expand Up @@ -229,6 +242,21 @@ public static function build_template_result_from_file( $template_file, $templat
$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;
}

Expand Down Expand Up @@ -391,7 +419,7 @@ public static function get_theme_template_path( $template_slug, $template_type =
// 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;
Expand Down Expand Up @@ -570,13 +598,13 @@ public static function remove_theme_templates_with_custom_alternative( $template

// 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;
}
Expand All @@ -591,7 +619,7 @@ function( $template ) {
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 ) );
}
Expand All @@ -611,7 +639,7 @@ function( $template ) use ( $customised_template_slugs ) {
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;
Expand All @@ -620,7 +648,7 @@ function( $template ) use ( $templates, $theme_slug ) {
// 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;
}
);
Expand Down Expand Up @@ -700,7 +728,7 @@ public static function get_block_templates_from_db( $slugs = array(), $template_
$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
Expand Down

0 comments on commit f2d828f

Please sign in to comment.