Automattic\WooCommerce\Blocks
BlockTemplatesController::get_block_file_template
This function checks if there's a block template file in woocommerce/templates/templates/ to return to pre_get_posts short-circuiting the query in Gutenberg.
Method of the class: BlockTemplatesController{}
No Hooks.
Returns
Mixed|\WP_Block_Template|\WP_Error.
Usage
$BlockTemplatesController = new BlockTemplatesController(); $BlockTemplatesController->get_block_file_template( $template, $id, $template_type );
- $template(WP_Block_Template|null) (required)
- Return a block template object to short-circuit the default query, or null to allow WP to run its normal queries.
- $id(string) (required)
- Template unique identifier (example: theme_slug//template_slug).
- $template_type(string) (required)
- wp_template or wp_template_part.
BlockTemplatesController::get_block_file_template() BlockTemplatesController::get block file template code WC 10.6.2
public function get_block_file_template( $template, $id, $template_type ) {
$template_name_parts = explode( '//', $id );
if ( count( $template_name_parts ) < 2 ) {
return $template;
}
list( $template_id, $template_slug ) = $template_name_parts;
// This is a real edge-case, we are supporting users who have saved templates under the deprecated slug. See its definition for more information.
// You can likely ignore this code unless you're supporting/debugging early customised templates.
if ( BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG === strtolower( $template_id ) ) {
// Because we are using get_block_templates we have to unhook this method to prevent a recursive loop where this filter is applied.
remove_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
$template_with_deprecated_id = get_block_template( $id, $template_type );
// Let's hook this method back now that we have used the function.
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
if ( null !== $template_with_deprecated_id ) {
return $template_with_deprecated_id;
}
}
// If we are not dealing with a WooCommerce template let's return early and let it continue through the process.
if ( BlockTemplateUtils::PLUGIN_SLUG !== $template_id ) {
return $template;
}
// If we don't have a template let Gutenberg do its thing.
if ( ! $this->block_template_is_available( $template_slug, $template_type ) ) {
return $template;
}
$directory = BlockTemplateUtils::get_templates_directory( $template_type );
$template_file_path = $directory . '/' . $template_slug . '.html';
$template_object = BlockTemplateUtils::create_new_block_template_object( $template_file_path, $template_type, $template_slug );
$template_built = BlockTemplateUtils::build_template_result_from_file( $template_object, $template_type );
if ( null !== $template_built ) {
return $template_built;
}
// Hand back over to Gutenberg if we can't find a template.
return $template;
}