WooCommerce Code Reference

BlockTemplateUtilsDuplicated
in package

BlockTemplateUtils class used for serving block templates from Woo Blocks.

IMPORTANT: These methods have been duplicated from Gutenberg/lib/full-site-editing/block-templates.php as those functions are not for public usage.

For internal use only by the Automattic\WooCommerce\Internal\Brands package.

Tags
version
9.4.0

Table of Contents

DIRECTORY_NAMES  = array('DEPRECATED_TEMPLATES' => 'block-templates', 'DEPRECATED_TEMPLATE_PARTS' => 'block-template-parts', 'TEMPLATES' => 'templates', 'TEMPLATE_PARTS' => 'parts')
Directory names for block templates
PLUGIN_SLUG  = 'woocommerce/woocommerce'
WooCommerce plugin slug
convert_slug_to_title()  : string
Converts template slugs into readable titles.
create_new_block_template_object()  : object
Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any.
get_theme_template_path()  : string|null
Gets the first matching template part within themes directories
gutenberg_build_template_result_from_file()  : WP_Block_Template
Build a unified template object based on a theme file.
gutenberg_build_template_result_from_post()  : WP_Block_Template|WP_Error
Build a unified template object based a post Object.
gutenberg_flatten_blocks()  : array<string|int, mixed>
Returns an array containing the references of the passed blocks and their inner blocks.
gutenberg_inject_theme_attribute_in_content()  : string
Parses wp_template content and injects the current theme's stylesheet as a theme attribute into each wp_template_part
should_use_blockified_product_grid_templates()  : bool
Returns whether the blockified templates should be used or not.
supports_block_templates()  : bool
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.
theme_has_template()  : bool
Check if the theme has a template. So we know if to load our own in or not.
theme_has_template_part()  : bool
Check if the theme has a template. So we know if to load our own in or not.

Constants

DIRECTORY_NAMES

Directory names for block templates

protected array<string|int, mixed> DIRECTORY_NAMES = array('DEPRECATED_TEMPLATES' => 'block-templates', 'DEPRECATED_TEMPLATE_PARTS' => 'block-template-parts', 'TEMPLATES' => 'templates', 'TEMPLATE_PARTS' => 'parts')

Directory names conventions for block templates have changed with Gutenberg 12.1.0, however, for backwards-compatibility, we also keep the older conventions, prefixed with DEPRECATED_.

Methods

convert_slug_to_title()

Converts template slugs into readable titles.

public static convert_slug_to_title(string $template_slug) : string
Parameters
$template_slug : string

The templates slug (e.g. single-product).

Return values
stringHuman friendly title converted from the slug.

create_new_block_template_object()

Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any.

public static create_new_block_template_object(string $template_file, string $template_type, string $template_slug[, bool $template_is_from_theme = false ]) : object
Parameters
$template_file : string

Block template file path.

$template_type : string

wp_template or wp_template_part.

$template_slug : string

Block template slug e.g. single-product.

$template_is_from_theme : bool = false

If the block template file is being loaded from the current theme instead of Woo Blocks.

Return values
objectBlock template object.

get_theme_template_path()

Gets the first matching template part within themes directories

public static get_theme_template_path(string $template_slug[, string $template_type = 'wp_template' ]) : string|null

Since Gutenberg 12.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.

Parameters
$template_slug : string

The slug of the template (i.e. without the file extension).

$template_type : string = 'wp_template'

Either wp_template or wp_template_part.

Return values
string|nullThe matched path or `null` if no match was found.

gutenberg_build_template_result_from_file()

Build a unified template object based on a theme file.

public static gutenberg_build_template_result_from_file(array<string|int, mixed>|object $template_file, string $template_type) : WP_Block_Template
Parameters
$template_file : array<string|int, mixed>|object

Theme file.

$template_type : string

wp_template or wp_template_part.

Return values
WP_Block_TemplateTemplate.

gutenberg_build_template_result_from_post()

Build a unified template object based a post Object.

public static gutenberg_build_template_result_from_post(WP_Post $post) : WP_Block_Template|WP_Error
Parameters
$post : WP_Post

Template post.

Return values
WP_Block_Template|WP_ErrorTemplate.

gutenberg_flatten_blocks()

Returns an array containing the references of the passed blocks and their inner blocks.

public static gutenberg_flatten_blocks(array<string|int, mixed> &$blocks) : array<string|int, mixed>
Parameters
$blocks : array<string|int, mixed>

array of blocks.

Return values
array<string|int, mixed>block references to the passed blocks and their inner blocks.

gutenberg_inject_theme_attribute_in_content()

Parses wp_template content and injects the current theme's stylesheet as a theme attribute into each wp_template_part

public static gutenberg_inject_theme_attribute_in_content(string $template_content) : string
Parameters
$template_content : string

serialized wp_template content.

Return values
stringUpdated wp_template content.

should_use_blockified_product_grid_templates()

Returns whether the blockified templates should be used or not.

public static should_use_blockified_product_grid_templates() : bool

First, we need to make sure WordPress version is higher than 6.1 (lowest that supports Products block). Then, if the option is not stored on the db, we need to check if the current theme is a block one or not.

Return values
bool

theme_has_template()

Check if the theme has a template. So we know if to load our own in or not.

public static theme_has_template(string $template_name) : bool
Parameters
$template_name : string

name of the template file without .html extension e.g. 'single-product'.

Return values
bool

theme_has_template_part()

Check if the theme has a template. So we know if to load our own in or not.

public static theme_has_template_part(string $template_name) : bool
Parameters
$template_name : string

name of the template file without .html extension e.g. 'single-product'.

Return values
bool