Skip to content

Commit

Permalink
Make it so Products by Category and Products by Tag aren't listed in …
Browse files Browse the repository at this point in the history
…the Site Editor templates list by default (IV)
  • Loading branch information
Aljullu committed Mar 20, 2024
1 parent 10b77b9 commit 8145c9b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 97 deletions.
27 changes: 0 additions & 27 deletions plugins/woocommerce/src/Blocks/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function init() {
add_filter( 'get_block_template', array( $this, 'add_block_template_details' ), 10, 3 );
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
add_filter( 'current_theme_supports-block-templates', array( $this, 'remove_block_template_support_for_shop_page' ) );
add_filter( 'taxonomy_template_hierarchy', array( $this, 'add_archive_product_to_eligible_for_fallback_templates' ), 10, 1 );
add_action( 'after_switch_theme', array( $this, 'check_should_use_blockified_product_grid_templates' ), 10, 2 );

if ( wc_current_theme_is_fse_theme() ) {
Expand Down Expand Up @@ -116,32 +115,6 @@ public function render_woocommerce_template_part( $attributes ) {
return function_exists( '\gutenberg_render_block_core_template_part' ) ? \gutenberg_render_block_core_template_part( $attributes ) : \render_block_core_template_part( $attributes );
}

/**
* Adds the `archive-product` template to the `taxonomy-product_cat`, `taxonomy-product_tag`, `taxonomy-attribute`
* templates to be able to fall back to it.
*
* @param array $template_hierarchy A list of template candidates, in descending order of priority.
*/
public function add_archive_product_to_eligible_for_fallback_templates( $template_hierarchy ) {
$template_slugs = array_map(
'_strip_template_file_suffix',
$template_hierarchy
);

$templates_eligible_for_fallback = array_filter(
$template_slugs,
function( $template_slug ) {
return BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $template_slug );
}
);

if ( count( $templates_eligible_for_fallback ) > 0 ) {
$template_hierarchy[] = ProductCatalogTemplate::SLUG;
}

return $template_hierarchy;
}

/**
* Checks the old and current themes and determines if the "wc_blocks_use_blockified_product_grid_block_as_template"
* option need to be updated accordingly.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Automattic\WooCommerce\Blocks\Templates;

use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;

/**
Expand All @@ -19,16 +18,6 @@ abstract class AbstractTemplateWithFallback extends AbstractTemplate {
*/
public $fallback_template;

/**
* Template part functionality is only initialized when using a theme that supports template parts.
*/
public function __construct() {
if ( BlockTemplateUtils::supports_block_templates( 'wp_template_part' ) ) {
BlockTemplatesRegistry::register_template( $this );
$this->init();
}
}

/**
* Initialization method.
*/
Expand Down Expand Up @@ -108,22 +97,6 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
}
}

if ( count( $posts ) > 0 ) {
$template = _build_block_template_result_from_post( $posts[0] );
$directory = BlockTemplateUtils::get_templates_directory( 'wp_template' );
$template_file_path = $directory . '/' . $this->fallback_template . '.html';
$theme_slug = wp_get_theme()->get_stylesheet();

// Add fallback content when creating the page.
if ( $template->id === $theme_slug . '//' . self::SLUG && ( ! isset( $template->content ) || '' === $template->content ) ) {
$fallback_template_content = file_get_contents( $template_file_path );
$template->content = BlockTemplateUtils::inject_theme_attribute_in_content( $fallback_template_content );
// Remove the term description block from the archive-product template
// as the Product Catalog/Shop page doesn't have a description.
$template->content = str_replace( '<!-- wp:term-description {"align":"wide"} /-->', '', $template->content );
}
}

return $template;
}

Expand All @@ -137,49 +110,15 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
* @param array $templates Templates that match the pages_template_hierarchy.
*/
public function template_hierarchy( $templates ) {
if ( $this->is_active_template() ) {
array_unshift( $templates, $this->fallback_template );
array_unshift( $templates, self::SLUG );

// Make it searching for a template returns the default WooCommerce template.
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
}
return $templates;
}

/**
* Add the block template objects to be used.
*
* @param array $query_result Array of template objects.
* @param array $query Optional. Arguments to retrieve templates.
* @param string $template_type wp_template or wp_template_part.
* @return array
*/
public function add_block_templates( $query_result, $query, $template_type ) {
// Is it's not the same slug, do nothing.
if ( isset( $query['slug__in'] ) && ! in_array( self::SLUG, $query['slug__in'], true ) ) {
return $query_result;
}
// If it's not the same template type, do nothing.
if ( 'wp_template' !== $template_type ) {
return $query_result;
}
// If the theme has the template, do nothing.
if ( BlockTemplateUtils::theme_has_template( self::SLUG ) ) {
return $query_result;
}
$directory = BlockTemplateUtils::get_templates_directory( 'wp_template' );
$template_file_path = $directory . '/' . $this->fallback_template . '.html';
// If template is in DB, do nothing.
if ( count( $query_result ) > 0 ) {
return $query_result;
$index = array_search( static::SLUG, $templates );
if (
false !== $index && (
! array_key_exists( $index + 1, $templates ) || $templates[ $index + 1 ] !== $this->fallback_template
) ) {
array_splice( $templates, $index + 1, 0, 'archive-product' );
}

$template_object = BlockTemplateUtils::create_new_block_template_object( $template_file_path, 'wp_template', self::SLUG, true );
$template = BlockTemplateUtils::build_template_result_from_file( $template_object, 'wp_template' );
$query_result[] = $template;

return $query_result;
return $templates;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ProductAttributeTemplate extends AbstractTemplate {
* Initialization method.
*/
public function init() {
parent::init();
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
add_filter( 'taxonomy_template_hierarchy', array( $this, 'update_taxonomy_template_hierarchy' ), 1, 3 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @internal
*/
class ProductCategoryTemplate extends AbstractTemplate {
class ProductCategoryTemplate extends AbstractTemplateWithFallback {

/**
* The slug of the template.
Expand All @@ -29,6 +29,7 @@ class ProductCategoryTemplate extends AbstractTemplate {
* Initialization method.
*/
public function init() {
parent::init();
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ProductSearchResultsTemplate extends AbstractTemplate {
* Initialization method.
*/
public function init() {
parent::init();
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
add_filter( 'search_template_hierarchy', array( $this, 'update_search_template_hierarchy' ), 10, 3 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @internal
*/
class ProductTagTemplate extends AbstractTemplate {
class ProductTagTemplate extends AbstractTemplateWithFallback {

/**
* The slug of the template.
Expand All @@ -29,6 +29,7 @@ class ProductTagTemplate extends AbstractTemplate {
* Initialization method.
*/
public function init() {
parent::init();
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
}

Expand Down

0 comments on commit 8145c9b

Please sign in to comment.