Skip to content

Commit

Permalink
Get BlockTemplatesRegistry directly from BlockTemplateUtils to simpli…
Browse files Browse the repository at this point in the history
…fy code
  • Loading branch information
Aljullu committed Feb 26, 2024
1 parent 24a0e3d commit 845e751
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 114 deletions.
65 changes: 27 additions & 38 deletions plugins/woocommerce/src/Blocks/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Automattic\WooCommerce\Blocks;

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

/**
Expand All @@ -19,21 +18,11 @@ class BlockTemplatesController {
*/
const TEMPLATES_ROOT_DIR = 'templates';

/**
* Block templates registry.
*
* @var BlockTemplatesRegistry
*/
protected $block_templates_registry;

/**
* Constructor.
*
* @param BlockTemplatesRegistry $block_templates_registry Block templates registry.
*/
public function __construct( $block_templates_registry ) {
public function __construct() {
$this->init();
$this->block_templates_registry = $block_templates_registry;
}

/**
Expand Down Expand Up @@ -162,7 +151,7 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
$theme = $template_name_parts[0] ?? '';
$slug = $template_name_parts[1] ?? '';

if ( empty( $theme ) || empty( $slug ) || ! BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $this->block_templates_registry, $slug ) ) {
if ( empty( $theme ) || empty( $slug ) || ! BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $slug ) ) {
return null;
}

Expand Down Expand Up @@ -194,8 +183,8 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
if ( ! is_wp_error( $template ) ) {
$template->id = $theme . '//' . $slug;
$template->slug = $slug;
$template->title = BlockTemplateUtils::get_block_template_title( $this->block_templates_registry, $slug );
$template->description = BlockTemplateUtils::get_block_template_description( $this->block_templates_registry, $slug );
$template->title = BlockTemplateUtils::get_block_template_title( $slug );
$template->description = BlockTemplateUtils::get_block_template_description( $slug );
unset( $template->source );

return $template;
Expand All @@ -220,7 +209,7 @@ public function add_archive_product_to_eligible_for_fallback_templates( $templat
$templates_eligible_for_fallback = array_filter(
$template_slugs,
function( $template_slug ) {
return BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $this->block_templates_registry, $template_slug );
return BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $template_slug );
}
);

Expand Down Expand Up @@ -272,10 +261,10 @@ public function get_block_file_template( $template, $id, $template_type ) {
list( $template_id, $template_slug ) = $template_name_parts;

// If the theme has an archive-product.html template, but not a taxonomy-product_cat/tag/attribute.html template let's use the themes archive-product.html template.
if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_theme( $this->block_templates_registry, $template_slug ) ) {
if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_theme( $template_slug ) ) {
$template_path = BlockTemplateUtils::get_theme_template_path( ProductCatalogTemplate::SLUG );
$template_object = BlockTemplateUtils::create_new_block_template_object( $this->block_templates_registry, $template_path, $template_type, $template_slug, true );
return BlockTemplateUtils::build_template_result_from_file( $this->block_templates_registry, $template_object, $template_type );
$template_object = BlockTemplateUtils::create_new_block_template_object( $template_path, $template_type, $template_slug, true );
return BlockTemplateUtils::build_template_result_from_file( $template_object, $template_type );
}

// This is a real edge-case, we are supporting users who have saved templates under the deprecated slug. See its definition for more information.
Expand Down Expand Up @@ -304,8 +293,8 @@ public function get_block_file_template( $template, $id, $template_type ) {

$directory = BlockTemplateUtils::get_templates_directory( $template_type );
$template_file_path = $directory . '/' . $template_slug . '.html';
$template_object = BlockTemplateUtils::create_new_block_template_object( $this->block_templates_registry, $template_file_path, $template_type, $template_slug );
$template_built = BlockTemplateUtils::build_template_result_from_file( $this->block_templates_registry, $template_object, $template_type );
$template_object = BlockTemplateUtils::create_new_block_template_object( $template_file_path, $template_type, $template_slug );
$template_built = BlockTemplateUtils::build_template_result_from_file( $template_object, $template_type );

if ( null !== $template_built ) {
return $template_built;
Expand All @@ -328,13 +317,13 @@ public function add_block_template_details( $block_template, $id, $template_type
return $block_template;
}
if ( ! BlockTemplateUtils::template_has_title( $block_template ) ) {
$block_template->title = BlockTemplateUtils::get_block_template_title( $this->block_templates_registry, $block_template->slug );
$block_template->title = BlockTemplateUtils::get_block_template_title( $block_template->slug );
}
if ( ! $block_template->description ) {
$block_template->description = BlockTemplateUtils::get_block_template_description( $this->block_templates_registry, $block_template->slug );
$block_template->description = BlockTemplateUtils::get_block_template_description( $block_template->slug );
}
if ( ! $block_template->area || 'uncategorized' === $block_template->area ) {
$block_template->area = BlockTemplateUtils::get_block_template_area( $this->block_templates_registry, $block_template->slug, $template_type );
$block_template->area = BlockTemplateUtils::get_block_template_area( $block_template->slug, $template_type );
}
return $block_template;
}
Expand Down Expand Up @@ -363,7 +352,7 @@ public function add_block_templates( $query_result, $query, $template_type ) {
// 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.
if ( BlockTemplateUtils::set_has_theme_file_if_fallback_is_available( $this->block_templates_registry, $query_result, $template_file ) ) {
if ( BlockTemplateUtils::set_has_theme_file_if_fallback_is_available( $query_result, $template_file ) ) {
continue;
}

Expand Down Expand Up @@ -395,7 +384,7 @@ public function add_block_templates( $query_result, $query, $template_type ) {
! isset( $query['area'] ) || ( property_exists( $template_file, 'area' ) && $template_file->area === $query['area'] );
$should_include = $is_not_custom && $fits_slug_query && $fits_area_query;
if ( $should_include ) {
$template = BlockTemplateUtils::build_template_result_from_file( $this->block_templates_registry, $template_file, $template_type );
$template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
$query_result[] = $template;
}
}
Expand All @@ -417,13 +406,13 @@ public function add_block_templates( $query_result, $query, $template_type ) {
$query_result = array_map(
function( $template ) use ( $template_type ) {
if ( ! BlockTemplateUtils::template_has_title( $template ) ) {
$template->title = BlockTemplateUtils::get_block_template_title( $this->block_templates_registry, $template->slug );
$template->title = BlockTemplateUtils::get_block_template_title( $template->slug );
}
if ( ! $template->description ) {
$template->description = BlockTemplateUtils::get_block_template_description( $this->block_templates_registry, $template->slug );
$template->description = BlockTemplateUtils::get_block_template_description( $template->slug );
}
if ( ! $template->area || 'uncategorized' === $template->area ) {
$template->area = BlockTemplateUtils::get_block_template_area( $this->block_templates_registry, $template->slug, $template_type );
$template->area = BlockTemplateUtils::get_block_template_area( $template->slug, $template_type );
}

return $template;
Expand Down Expand Up @@ -496,35 +485,35 @@ function ( $template ) use ( $template_slug ) {
continue;
}

if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_db( $this->block_templates_registry, $template_slug, $already_found_templates ) ) {
$template = clone BlockTemplateUtils::get_fallback_template_from_db( $this->block_templates_registry, $template_slug, $already_found_templates );
if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_db( $template_slug, $already_found_templates ) ) {
$template = clone BlockTemplateUtils::get_fallback_template_from_db( $template_slug, $already_found_templates );
$template_id = explode( '//', $template->id );
$template->id = $template_id[0] . '//' . $template_slug;
$template->slug = $template_slug;
$template->title = BlockTemplateUtils::get_block_template_title( $this->block_templates_registry, $template_slug );
$template->description = BlockTemplateUtils::get_block_template_description( $this->block_templates_registry, $template_slug );
$template->title = BlockTemplateUtils::get_block_template_title( $template_slug );
$template->description = BlockTemplateUtils::get_block_template_description( $template_slug );
$templates[] = $template;
continue;
}

// If the theme has an archive-product.html template, but not a taxonomy-product_cat/tag/attribute.html template let's use the themes archive-product.html template.
if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_theme( $this->block_templates_registry, $template_slug ) ) {
if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_theme( $template_slug ) ) {
$template_file = BlockTemplateUtils::get_theme_template_path( ProductCatalogTemplate::SLUG );
$templates[] = BlockTemplateUtils::create_new_block_template_object( $this->block_templates_registry, $template_file, $template_type, $template_slug, true );
$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, 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( $this->block_templates_registry, $template_slug ) ) {
if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $template_slug ) ) {
$template_file = $this->get_template_path_from_woocommerce( ProductCatalogTemplate::SLUG );
$templates[] = BlockTemplateUtils::create_new_block_template_object( $this->block_templates_registry, $template_file, $template_type, $template_slug, false );
$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( $this->block_templates_registry, $template_file, $template_type, $template_slug );
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug );
}

return $templates;
Expand Down
2 changes: 1 addition & 1 deletion plugins/woocommerce/src/Blocks/Domain/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function ( Container $container ) {
$this->container->register(
BlockTemplatesController::class,
function ( Container $container ) {
return new BlockTemplatesController( $container->get( BlockTemplatesRegistry::class ) );
return new BlockTemplatesController();
}
);
$this->container->register(
Expand Down

0 comments on commit 845e751

Please sign in to comment.