Automattic\WooCommerce\Blocks
BlockTemplatesController::get_block_template_fallback()
This function is used on the pre_get_block_template hook to return the fallback template from the db in case the template is eligible for it.
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 7.7.0
public function get_block_template_fallback( $template, $id, $template_type ) { $template_name_parts = explode( '//', $id ); list( $theme, $slug ) = $template_name_parts; if ( ! BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $slug ) ) { return null; } $wp_query_args = array( 'post_name__in' => array( 'archive-product', $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 `archive-product` template. if ( count( $posts ) > 1 ) { return null; } if ( count( $posts ) > 0 && 'archive-product' === $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; }