Automattic\WooCommerce\Blocks
BlockTemplatesController::get_block_template_fallback()
This function is used on the pre_get_block_template to return the fallback template from the db in case the template is eligible for it.
Currently, the Products by Category, Products by Tag and Products by Attribute templates fall back to the Product Catalog template. That means that if there are customizations in the Product Catalog template, they are also reflected in the other templates as long as they haven't been customized as well.
Method of the class: BlockTemplatesController{}
No Hooks.
Return
Object|null
.
Usage
$BlockTemplatesController = new BlockTemplatesController(); $BlockTemplatesController->get_block_template_fallback( $template, $id, $template_type );
- $template(\WP_Block_Template|null) (required)
- 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_template_fallback() BlockTemplatesController::get block template fallback code WC 9.3.1
public function get_block_template_fallback( $template, $id, $template_type ) { // Add protection against invalid ids. if ( ! is_string( $id ) || ! strstr( $id, '//' ) ) { return null; } // Add protection against invalid template types. if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { return null; } $template_name_parts = explode( '//', $id ); $theme = $template_name_parts[0] ?? ''; $slug = $template_name_parts[1] ?? ''; $registered_template = BlockTemplateUtils::get_template( $slug ); if ( empty( $theme ) || empty( $slug ) || ! $registered_template || ! isset( $registered_template->fallback_template ) ) { return null; } $wp_query_args = array( 'post_name__in' => array( $registered_template->fallback_template, $slug ), 'post_type' => $template_type, 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), 'no_found_rows' => true, 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query array( 'taxonomy' => 'wp_theme', 'field' => 'name', 'terms' => $theme, ), ), ); $template_query = new \WP_Query( $wp_query_args ); $posts = $template_query->posts; // If we have more than one result from the query, it means that the current template is present in the db (has // been customized by the user) and we should not return the fallback template. if ( count( $posts ) > 1 ) { return null; } if ( count( $posts ) > 0 && $registered_template->fallback_template === $posts[0]->post_name ) { $template = _build_block_template_result_from_post( $posts[0] ); if ( ! is_wp_error( $template ) ) { $template->id = $theme . '//' . $slug; $template->slug = $slug; $template->title = BlockTemplateUtils::get_block_template_title( $slug ); $template->description = BlockTemplateUtils::get_block_template_description( $slug ); unset( $template->source ); return $template; } } return $template; }