Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Add fallbacks to the db and blocks versions
Browse files Browse the repository at this point in the history
  • Loading branch information
albarin committed Nov 18, 2022
1 parent 4503bd4 commit 875ff18
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
22 changes: 18 additions & 4 deletions src/BlockTemplatesController.php
Expand Up @@ -199,6 +199,7 @@ public function add_block_templates( $query_result, $query, $template_type ) {

// @todo: Add apply_filters to _gutenberg_get_template_files() in Gutenberg to prevent duplication of logic.
foreach ( $template_files as $template_file ) {

// If we have a template which is eligible for a fallback, we need to explicitly tell Gutenberg that
// it has a theme file (because it is using the fallback template file). And then `continue` to avoid
// adding duplicates.
Expand Down Expand Up @@ -253,7 +254,7 @@ public function add_block_templates( $query_result, $query, $template_type ) {
*/
$query_result = array_map(
function( $template ) {
if ( 'theme' === $template->origin && BlockTemplateUtils::template_has_title( $template ) ) {
if ( 'theme' === $template->origin ) {
return $template;
}
if ( $template->title === $template->slug ) {
Expand Down Expand Up @@ -342,6 +343,7 @@ public function get_block_templates_from_woocommerce( $slugs, $already_found_tem
// If the theme already has a template, or the template is already in the list (i.e. it came from the
// database) then we should not overwrite it with the one from the filesystem.
if (
BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_db( $template_slug, $already_found_templates ) ||
BlockTemplateUtils::theme_has_template( $template_slug ) ||
count(
array_filter(
Expand All @@ -362,6 +364,14 @@ function ( $template ) use ( $template_slug ) {
continue;
}

// 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_product_archive_fallback( $template_slug ) ) {
$template_file = $this->get_template_path_from_woocommerce( 'archive-product' );
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, false );
continue;
}

// At this point the template only exists in the Blocks filesystem and has not been saved in the DB,
// or superseded by the theme.
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug );
Expand Down Expand Up @@ -406,6 +416,10 @@ protected function get_templates_directory( $template_type = 'wp_template' ) {
return $this->templates_directory;
}

public function get_template_path_from_woocommerce( $template_slug, $template_type = 'wp_template' ) {
return $this->get_templates_directory( $template_type ) . '/' . $template_slug . '.html';
}

/**
* Checks whether a block template with that name exists in Woo Blocks
*
Expand Down Expand Up @@ -442,13 +456,13 @@ public function render_block_template() {
} elseif (
( is_product_taxonomy() && is_tax( 'product_cat' ) ) &&
! BlockTemplateUtils::theme_has_template( 'taxonomy-product_cat' ) &&
$this->block_template_is_available( 'archive-product' )
$this->block_template_is_available( 'taxonomy-product_cat' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_product_taxonomy() && is_tax( 'product_tag' ) ) &&
! BlockTemplateUtils::theme_has_template( 'taxonomy-product_tag' ) &&
$this->block_template_is_available( 'archive-product' )
$this->block_template_is_available( 'taxonomy-product_tag' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
Expand All @@ -465,7 +479,7 @@ public function render_block_template() {

if ( isset( $queried_object->taxonomy ) && taxonomy_is_product_attribute( $queried_object->taxonomy ) &&
! BlockTemplateUtils::theme_has_template( ProductAttributeTemplate::SLUG ) &&
$this->block_template_is_available( 'archive-product' )
$this->block_template_is_available( ProductAttributeTemplate::SLUG )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
}
Expand Down
31 changes: 19 additions & 12 deletions src/Utils/BlockTemplateUtils.php
Expand Up @@ -202,8 +202,8 @@ public static function build_template_result_from_file( $template_file, $templat
$template->source = $template_file->source ? $template_file->source : 'plugin';
$template->slug = $template_file->slug;
$template->type = $template_type;
$template->title = self::template_has_title( $template_file ) ? $template_file->title : self::get_block_template_title( $template_file->slug );
$template->description = self::template_has_title( $template_file ) ? $template_file->description : self::get_block_template_description( $template_file->slug );
$template->title = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug );
$template->description = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug );
$template->status = 'publish';
$template->has_theme_file = true;
$template->origin = $template_file->source;
Expand Down Expand Up @@ -465,6 +465,23 @@ public static function template_is_eligible_for_product_archive_fallback( $templ
return in_array( $template_slug, self::ELIGIBLE_FOR_ARCHIVE_PRODUCT_FALLBACK, true );
}

public static function template_is_eligible_for_product_archive_fallback_from_db( $template_slug, $db_templates ) {
$eligible_for_fallback = self::template_is_eligible_for_product_archive_fallback($template_slug);
if ( ! $eligible_for_fallback ) {
return false;
}

$array_filter = array_filter(
$db_templates,
function ( $template ) use ( $template_slug ) {
return self::template_is_eligible_for_product_archive_fallback( $template_slug )
&& $template->slug === 'archive-product';
}
);

return count( $array_filter ) > 0;
}

/**
* Checks if we can fall back to the `archive-product` file template for a given slug in the current theme.
*
Expand Down Expand Up @@ -598,14 +615,4 @@ public static function should_use_blockified_product_grid_templates() {

return wc_string_to_bool( $use_blockified_templates );
}

/**
* Returns whether the passed `$template` has a title, and it's different from the slug.
*
* @param object $template The template object.
* @return boolean
*/
public static function template_has_title( $template ) {
return ! empty( $template->title ) && $template->title !== $template->slug;
}
}
5 changes: 5 additions & 0 deletions templates/templates/taxonomy-product_attribute.html
@@ -0,0 +1,5 @@
<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group"><!-- wp:woocommerce/legacy-template {"template":"taxonomy-product_attribute"} /--></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->
5 changes: 5 additions & 0 deletions templates/templates/taxonomy-product_cat.html
@@ -0,0 +1,5 @@
taxonomy-product_tag.html<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group"><!-- wp:woocommerce/legacy-template {"template":"taxonomy-product_cat"} /--></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->
5 changes: 5 additions & 0 deletions templates/templates/taxonomy-product_tag.html
@@ -0,0 +1,5 @@
<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group"><!-- wp:woocommerce/legacy-template {"template":"taxonomy-product_tag"} /--></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

0 comments on commit 875ff18

Please sign in to comment.