Skip to content

Commit

Permalink
Add back 'template_is_eligible_for_fallback' method
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Apr 12, 2024
1 parent 64cfd3c commit 6beeec1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
7 changes: 4 additions & 3 deletions plugins/woocommerce/src/Blocks/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
$template_name_parts = explode( '//', $id );
$theme = $template_name_parts[0] ?? '';
$slug = $template_name_parts[1] ?? '';
$registered_template = BlockTemplateUtils::get_template( $template_slug );

if ( empty( $theme ) || empty( $slug ) || ! BlockTemplateUtils::template_is_eligible_for_fallback( $slug ) ) {
if ( empty( $theme ) || empty( $slug ) || ! $registered_template || ! isset( $registered_template->fallback_template ) ) {
return null;
}

$wp_query_args = array(
'post_name__in' => array( ProductCatalogTemplate::SLUG, $slug ),
'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,
Expand All @@ -171,7 +172,7 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
return null;
}

if ( count( $posts ) > 0 && ProductCatalogTemplate::SLUG === $posts[0]->post_name ) {
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 ) ) {
Expand Down
17 changes: 17 additions & 0 deletions plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,23 @@ public static function supports_block_templates( $template_type = 'wp_template'
return false;
}

/**
* Checks if we can fall back to a different template for a given slug.
*
* `taxonomy-product_cat`, `taxonomy-product_tag`, `taxonomy-product_attribute` templates can
* generally use the `archive-product` as a fallback if there are no specific overrides.
*
* @param string $template_slug Slug to check for fallbacks.
* @return boolean
*/
public static function template_is_eligible_for_fallback( $template_slug ) {
$registered_template = self::get_template( $template_slug );
if ( $registered_template && isset( $registered_template->fallback_template ) ) {
return ProductCatalogTemplate::SLUG === $registered_template->fallback_template;
}
return false;
}

/**
* Checks if we can fall back to an `archive-product` template stored on the db for a given slug.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public function provideFallbackData() {
);
}

/**
* Test template_is_eligible_for_fallback.
*
* @param string $input The template slug.
* @param bool $expected The expected result.
*
* @dataProvider provideFallbackData
*/
public function test_template_is_eligible_for_fallback( $input, $expected ) {
$this->assertEquals( $expected, BlockTemplateUtils::template_is_eligible_for_fallback( $input ) );
}

/**
* Test template_is_eligible_for_fallback_from_db when the template is not eligible.
*/
Expand Down

0 comments on commit 6beeec1

Please sign in to comment.