namespace Automattic\WooCommerce\Blocks\Utils;
use WP_Block_Patterns_Registry;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Blocks\Options;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry;
use Automattic\WooCommerce\Blocks\Templates\ProductCatalogTemplate;
* Utility methods used for serving block templates from WooCommerce Blocks.
* {@internal This class and its methods should only be used within the BlockTemplateController.php and is not intended for public use.}
class BlockTemplateUtils {
* Directory names for block templates
* Directory names conventions for block templates have changed with Gutenberg 12.1.0,
* however, for backwards-compatibility, we also keep the older conventions, prefixed
* @var string DEPRECATED_TEMPLATES Old directory name of the block templates directory.
* @var string DEPRECATED_TEMPLATE_PARTS Old directory name of the block template parts directory.
* @var string TEMPLATES_DIR_NAME Directory name of the block templates directory.
* @var string TEMPLATE_PARTS_DIR_NAME Directory name of the block template parts directory.
const DIRECTORY_NAMES = array(
'DEPRECATED_TEMPLATES' => 'block-templates',
'DEPRECATED_TEMPLATE_PARTS' => 'block-template-parts',
'TEMPLATES' => 'templates',
'TEMPLATE_PARTS' => 'parts',
const TEMPLATES_ROOT_DIR = 'templates';
* WooCommerce plugin slug
* This is used to save templates to the DB which are stored against this value in the wp_terms table.
const PLUGIN_SLUG = 'woocommerce/woocommerce';
* Deprecated WooCommerce plugin slug
* For supporting users who have customized templates under the incorrect plugin slug during the first release.
* More context found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423.
const DEPRECATED_PLUGIN_SLUG = 'woocommerce';
* Returns the template matching the slug
* @param string $template_slug Slug of the template to retrieve.
* @return AbstractTemplate|AbstractTemplatePart|null
public static function get_template( $template_slug ) {
$block_templates_registry = Package::container()->get( BlockTemplatesRegistry::class );
return $block_templates_registry->get_template( $template_slug );
* Returns an array containing the references of
* the passed blocks and their inner blocks.
* @param array $blocks array of blocks.
* @return array block references to the passed blocks and their inner blocks.
public static function flatten_blocks( &$blocks ) {
foreach ( $blocks as &$block ) {
$queue_count = count( $queue );
while ( $queue_count > 0 ) {
if ( ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as &$inner_block ) {
$queue[] = &$inner_block;
$queue_count = count( $queue );
* Parses wp_template content and injects the current theme's
* stylesheet as a theme attribute into each wp_template_part
* @param string $template_content serialized wp_template content.
* @return string Updated wp_template content.
public static function inject_theme_attribute_in_content( $template_content ) {
$has_updated_content = false;
$template_blocks = parse_blocks( $template_content );
$blocks = self::flatten_blocks( $template_blocks );
foreach ( $blocks as &$block ) {
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
$has_updated_content = true;
if ( $has_updated_content ) {
foreach ( $template_blocks as &$block ) {
$new_content .= serialize_block( $block );
return $template_content;
* Build a unified template object based a post Object.
* Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes.
* @param \WP_Post $post Template post.
* @return \WP_Block_Template|\WP_Error Template.
public static function build_template_result_from_post( $post ) {
$terms = get_the_terms( $post, 'wp_theme' );
if ( is_wp_error( $terms ) ) {
return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'woocommerce' ) );
$theme = $terms[0]->name;
$template = new \WP_Block_Template();
$template->wp_id = $post->ID;
$template->id = $theme . '//' . $post->post_name;
$template->theme = $theme;
$template->content = $post->post_content;
$template->slug = $post->post_name;
$template->source = 'custom';
$template->type = $post->post_type;
$template->description = $post->post_excerpt;
$template->title = $post->post_title;
$template->status = $post->post_status;
$template->has_theme_file = $has_theme_file;
$template->is_custom = false;
$template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
if ( 'wp_template_part' === $post->post_type ) {
$type_terms = get_the_terms( $post, 'wp_template_part_area' );
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
$template->area = $type_terms[0]->name;
// We are checking 'woocommerce' to maintain classic templates which are saved to the DB,
// prior to updating to use the correct slug.
// More information found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423.
if ( self::PLUGIN_SLUG === $theme || self::DEPRECATED_PLUGIN_SLUG === strtolower( $theme ) ) {
$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 );
* Build a unified template object based on a theme file.
* @internal Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes.
* @param array|object $template_file Theme file.
* @param string $template_type wp_template or wp_template_part.
* @return \WP_Block_Template Template.
public static function build_template_result_from_file( $template_file, $template_type ) {
$template_file = (object) $template_file;
// If the theme has an archive-products.html template but does not have product taxonomy templates
// then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend.
$template_is_from_theme = 'theme' === $template_file->source;
$theme_name = wp_get_theme()->get( 'TextDomain' );
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$template_content = file_get_contents( $template_file->path );
$template = new \WP_Block_Template();
$template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug;
$template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG;
$template->content = self::inject_theme_attribute_in_content( $template_content );
// Remove the term description block from the archive-product template
// as the Product Catalog/Shop page doesn't have a description.
if ( ProductCatalogTemplate::SLUG === $template_file->slug ) {
$template->content = str_replace( '<!-- wp:term-description {"align":"wide"} /-->', '', $template->content );
// Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
$template->source = $template_file->source ? $template_file->source : 'plugin';
$template->slug = $template_file->slug;
$template->type = $template_type;
$template->title = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug );
$template->description = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug );
$template->status = 'publish';
$template->has_theme_file = true;
$template->origin = $template_file->source;
$template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
$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 );
* Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any.
* @param string $template_file Block template file path.
* @param string $template_type wp_template or wp_template_part.
* @param string $template_slug Block template slug e.g. single-product.
* @param bool $template_is_from_theme If the block template file is being loaded from the current theme instead of Woo Blocks.
* @return object Block template object.
public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) {
$theme_name = wp_get_theme()->get( 'TextDomain' );
$new_template_item = array(
'slug' => $template_slug,
'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : self::PLUGIN_SLUG . '//' . $template_slug,
'path' => $template_file,
'type' => $template_type,
'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG,
// Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
'source' => $template_is_from_theme ? 'theme' : 'plugin',
'title' => self::get_block_template_title( $template_slug ),
'description' => self::get_block_template_description( $template_slug ),
'post_types' => array(), // Don't appear in any Edit Post template selector dropdown.
return (object) $new_template_item;
* Finds all nested template part file paths in a theme's directory.
* @param string $template_type wp_template or wp_template_part.
* @return array $path_list A list of paths to all template part files.
public static function get_template_paths( $template_type ) {
$wp_template_filenames = array(
'order-confirmation.html',
'product-search-results.html',
'taxonomy-product_attribute.html',
'taxonomy-product_brand.html',
'taxonomy-product_cat.html',
'taxonomy-product_tag.html',
if ( Features::is_enabled( 'launch-your-store' ) ) {
$wp_template_filenames[] = 'coming-soon.html';
$wp_template_part_filenames = array(
'coming-soon-social-links.html',
'simple-product-add-to-cart-with-options.html',
'external-product-add-to-cart-with-options.html',
'variable-product-add-to-cart-with-options.html',
'grouped-product-add-to-cart-with-options.html',
* This may return the blockified directory for wp_templates.
* At the moment every template file has a corresponding blockified file.
* If we decide to add a new template file that doesn't, we will need to update this logic.
$directory = self::get_templates_directory( $template_type );
function ( $filename ) use ( $directory ) {
return $directory . DIRECTORY_SEPARATOR . $filename;
'wp_template' === $template_type ? $wp_template_filenames : $wp_template_part_filenames
* Gets the directory where templates of a specific template type can be found.
* @param string $template_type wp_template or wp_template_part.
public static function get_templates_directory( $template_type = 'wp_template' ) {
$root_path = dirname( __DIR__, 3 ) . '/' . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR;
$templates_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATES'];
$template_parts_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATE_PARTS'];
if ( 'wp_template_part' === $template_type ) {
return $template_parts_directory;
if ( self::should_use_blockified_product_grid_templates() ) {
return $templates_directory . '/blockified';
return $templates_directory;
* Returns template title.
* @param string $template_slug The template slug (e.g. single-product).
* @return string Human friendly title.
public static function get_block_template_title( $template_slug ) {
$registered_template = self::get_template( $template_slug );
if ( isset( $registered_template ) ) {
return $registered_template->get_template_title();
// Human friendly title converted from the slug.
return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) );
* Returns template description.
* @param string $template_slug The template slug (e.g. single-product).
* @return string Template description.
public static function get_block_template_description( $template_slug ) {
$registered_template = self::get_template( $template_slug );
if ( isset( $registered_template ) ) {
return $registered_template->get_template_description();
* Returns area for template parts.
* @param string $template_slug The template part slug (e.g. mini-cart).
* @param string $template_type Either `wp_template` or `wp_template_part`.
* @return string Template part area.
public static function get_block_template_area( $template_slug, $template_type ) {
if ( 'wp_template_part' === $template_type ) {
$registered_template = self::get_template( $template_slug );
if ( $registered_template && property_exists( $registered_template, 'template_area' ) ) {
return $registered_template->template_area;
* Converts template paths into a slug
* @param string $path The template's path.
public static function generate_template_slug_from_path( $path ) {
$template_extension = '.html';
return basename( $path, $template_extension );
* Gets the first matching template part within themes directories
* Since [Gutenberg 12.1.0](https://github.com/WordPress/gutenberg/releases/tag/v12.1.0), the conventions for
* block templates and parts directory has changed from `block-templates` and `block-templates-parts`
* to `templates` and `parts` respectively.
* This function traverses all possible combinations of directory paths where a template or part
* could be located and returns the first one which is readable, prioritizing the new convention
* over the deprecated one, but maintaining that one for backwards compatibility.
* @param string $template_slug The slug of the template (i.e. without the file extension).
* @param string $template_type Either `wp_template` or `wp_template_part`.
* @return string|null The matched path or `null` if no match was found.
public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) {
$template_filename = $template_slug . '.html';
$possible_templates_dir = 'wp_template' === $template_type ? array(
self::DIRECTORY_NAMES['TEMPLATES'],
self::DIRECTORY_NAMES['DEPRECATED_TEMPLATES'],
self::DIRECTORY_NAMES['TEMPLATE_PARTS'],
self::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'],
// Combine the possible root directory names with either the template directory
// or the stylesheet directory for child themes.
$possible_paths = array_reduce(
function ( $carry, $item ) use ( $template_filename ) {
$filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename;
$carry[] = get_stylesheet_directory() . $filepath;
$carry[] = get_template_directory() . $filepath;
// Return the first matching.
foreach ( $possible_paths as $path ) {
if ( is_readable( $path ) ) {
* Check if the theme has a template. So we know if to load our own in or not.
* @param string $template_name name of the template file without .html extension e.g. 'single-product'.
public static function theme_has_template( $template_name ) {
return (bool) self::get_theme_template_path( $template_name, 'wp_template' );
* Check if the theme has a template. So we know if to load our own in or not.
* @param string $template_name name of the template file without .html extension e.g. 'single-product'.
public static function theme_has_template_part( $template_name ) {
return (bool) self::get_theme_template_path( $template_name, 'wp_template_part' );
* Checks to see if they are using a compatible version of WP, or if not they have a compatible version of the Gutenberg plugin installed.