Skip to content

Commit

Permalink
Get rid of the template_is_eligible_for_fallback() util
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Apr 8, 2024
1 parent d8685e8 commit 02adbc3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
8 changes: 4 additions & 4 deletions plugins/woocommerce/src/Blocks/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,10 @@ function ( $template ) use ( $template_slug ) {

// At this point the template only exists in the Blocks filesystem, if is a taxonomy-product_cat/tag/attribute.html template
// let's use the archive-product.html template from Blocks.
if ( BlockTemplateUtils::template_is_eligible_for_fallback( $template_slug ) ) {
$registered_template = BlockTemplateUtils::get_template( $template_slug );
$template_file = $this->get_template_path_from_woocommerce( $registered_template->fallback_template );
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, false );
$registered_template = BlockTemplateUtils::get_template( $template_slug );
if ( $registered_template && isset( $registered_template->fallback_template ) ) {
$template_file = $this->get_template_path_from_woocommerce( $registered_template->fallback_template );
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, false );
continue;
}

Expand Down
51 changes: 14 additions & 37 deletions plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,23 +500,6 @@ public static function supports_block_templates( $template_type = 'wp_template'
return false;
}

/**
* Checks if we can fall back to the `archive-product` 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 isset( $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 All @@ -525,19 +508,16 @@ public static function template_is_eligible_for_fallback( $template_slug ) {
* @return boolean
*/
public static function template_is_eligible_for_fallback_from_db( $template_slug, $db_templates ) {
$eligible_for_fallback = self::template_is_eligible_for_fallback( $template_slug );
if ( ! $eligible_for_fallback ) {
return false;
}

$registered_template = self::get_template( $template_slug );

$array_filter = array_filter(
$db_templates,
function ( $template ) use ( $registered_template ) {
return isset( $registered_template->fallback_template ) && $registered_template->fallback_template === $template->slug;
}
);
if ( $registered_template && isset( $registered_template->fallback_template ) ) {
$array_filter = array_filter(
$db_templates,
function ( $template ) use ( $registered_template ) {
return isset( $registered_template->fallback_template ) && $registered_template->fallback_template === $template->slug;
}
);
}

return count( $array_filter ) > 0;
}
Expand All @@ -550,16 +530,13 @@ function ( $template ) use ( $registered_template ) {
* @return boolean|object
*/
public static function get_fallback_template_from_db( $template_slug, $db_templates ) {
$eligible_for_fallback = self::template_is_eligible_for_fallback( $template_slug );
if ( ! $eligible_for_fallback ) {
return false;
}

$registered_template = self::get_template( $template_slug );

foreach ( $db_templates as $template ) {
if ( $registered_template->fallback_template === $template->slug ) {
return $template;
if ( $registered_template && isset( $registered_template->fallback_template ) ) {
foreach ( $db_templates as $template ) {
if ( $registered_template->fallback_template === $template->slug ) {
return $template;
}
}
}

Expand All @@ -578,7 +555,7 @@ public static function get_fallback_template_from_db( $template_slug, $db_templa
public static function template_is_eligible_for_fallback_from_theme( $template_slug ) {
$registered_template = self::get_template( $template_slug );

return self::template_is_eligible_for_fallback( $template_slug )
return $registered_template && isset( $registered_template->fallback_template )
&& ! self::theme_has_template( $template_slug )
&& self::theme_has_template( $registered_template->fallback_template );
}
Expand Down

0 comments on commit 02adbc3

Please sign in to comment.