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

Check if the current theme has a product-archive block template in the absence of a product taxonomy block template. #5380

Merged
Merged
40 changes: 18 additions & 22 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ public function get_single_block_template( $template, $id, $template_type ) {
* @return array
*/
public function add_block_templates( $query_result, $query, $template_type ) {
if (
( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) &&
( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() )
) {
if ( ! BlockTemplateUtils::supports_block_templates() ) {
return $query_result;
}

Expand Down Expand Up @@ -339,21 +336,25 @@ function ( $template ) use ( $template_slug ) {
continue;
}

// If the theme has an archive-product.html template, but not a taxonomy-product_cat.html template let's use the themes archive-product.html template.
if ( 'taxonomy-product_cat' === $template_slug && ! BlockTemplateUtils::theme_has_template( 'taxonomy-product_cat' ) && BlockTemplateUtils::theme_has_template( 'archive-product' ) ) {
$template_file = get_stylesheet_directory() . '/' . self::TEMPLATES_DIR_NAME . '/archive-product.html';
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, true );
continue;
}

// If the theme has an archive-product.html template, but not a taxonomy-product_tag.html template let's use the themes archive-product.html template.
if ( 'taxonomy-product_tag' === $template_slug && ! BlockTemplateUtils::theme_has_template( 'taxonomy-product_tag' ) && BlockTemplateUtils::theme_has_template( 'archive-product' ) ) {
$template_file = get_stylesheet_directory() . '/' . self::TEMPLATES_DIR_NAME . '/archive-product.html';
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, true );
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.
$new_template_item = array(
'slug' => $template_slug,
'id' => 'woocommerce//' . $template_slug,
'path' => $template_file,
'type' => $template_type,
'theme' => 'woocommerce',
'source' => 'plugin',
'title' => BlockTemplateUtils::convert_slug_to_title( $template_slug ),
'description' => '',
'post_types' => array(), // Don't appear in any Edit Post template selector dropdown.
);
$templates[] = (object) $new_template_item;
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug );
}

return $templates;
}

Expand All @@ -371,7 +372,6 @@ public function get_block_templates( $slugs = array(), $template_type = 'wp_temp
return array_merge( $templates_from_db, $templates_from_woo );
}


/**
* Gets the directory where templates of a specific template type can be found.
*
Expand Down Expand Up @@ -409,11 +409,7 @@ public function block_template_is_available( $template_name, $template_type = 'w
* Renders the default block template from Woo Blocks if no theme templates exist.
*/
public function render_block_template() {
if (
is_embed() ||
( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) &&
( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() )
) {
if ( is_embed() || ! BlockTemplateUtils::supports_block_templates() ) {
return;
}

Expand Down
58 changes: 54 additions & 4 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,59 @@ public static function gutenberg_build_template_result_from_post( $post ) {
*/
public static function gutenberg_build_template_result_from_file( $template_file, $template_type ) {
$template_file = (object) $template_file;

// If the theme has an archive-products.html template but does not have product taxonomy templates
// then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend.
$template_is_from_theme = 'theme' === $template_file->source ? true : false;
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
$theme_name = wp_get_theme()->get( 'Name' );

// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$template_content = file_get_contents( $template_file->path );
$template = new \WP_Block_Template();
$template->id = 'woocommerce//' . $template_file->slug;
$template->theme = 'woocommerce/woocommerce';
$template->id = $template_is_from_theme ? strtolower( $theme_name ) . '//' . $template_file->slug : 'woocommerce//' . $template_file->slug;
$template->theme = $template_is_from_theme ? $theme_name : 'woocommerce/woocommerce';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think theme should be the theme slug, not the name. Ie, when using Twenty Twenty-Two it makes a Rest API request to an endpoint with a space and capital letters:

imatge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch Ive used TextDomain instead of Name instead and this fixes this.

$template->content = self::gutenberg_inject_theme_attribute_in_content( $template_content );
$template->source = 'plugin';
$template->source = $template_file->source || 'plugin';
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
$template->slug = $template_file->slug;
$template->type = $template_type;
$template->title = ! empty( $template_file->title ) ? $template_file->title : self::convert_slug_to_title( $template_file->slug );
$template->status = 'publish';
$template->has_theme_file = true;
$template->origin = 'plugin';
$template->origin = $template_file->source;
$template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
$template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
$template->area = 'uncategorized';
return $template;
}

/**
* Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any.
*
* @param string $template_file Block template file path.
* @param string $template_type wp_template or wp_template_part.
* @param string $template_slug Block template slug e.g. single-product.
* @param bool $template_is_from_theme If the block template file is being loaded from the current theme instead of Woo Blocks.
*
* @return object Block template object.
*/
public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) {
$theme_name = strtolower( wp_get_theme()->get( 'Name' ) );

$new_template_item = array(
'slug' => $template_slug,
'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : 'woocommerce//' . $template_slug,
'path' => $template_file,
'type' => $template_type,
'theme' => $template_is_from_theme ? $theme_name : 'woocommerce',
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
'source' => $template_is_from_theme ? 'theme' : 'plugin',
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
'title' => self::convert_slug_to_title( $template_slug ),
'description' => '',
'post_types' => array(), // Don't appear in any Edit Post template selector dropdown.
);

return (object) $new_template_item;
}

/**
* Finds all nested template part file paths in a theme's directory.
*
Expand Down Expand Up @@ -229,4 +263,20 @@ public static function theme_has_template_part( $template_name ) {
return is_readable( get_template_directory() . '/block-template-parts/' . $template_name . '.html' ) ||
is_readable( get_stylesheet_directory() . '/block-template-parts/' . $template_name . '.html' );
}

/**
* Checks to see if they are using a compatible version of WP, or if not they have a compatible version of the Gutenberg plugin installed.
*
* @return boolean
*/
public static function supports_block_templates() {
if (
( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) &&
( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() )
) {
return false;
}

return true;
}
}