Register templates in BlockTemplatesRegistry

This commit is contained in:
Albert Juhé Lluveras 2024-02-22 09:29:07 +01:00
parent 4c8ab693a3
commit 48800e5c55
5 changed files with 40 additions and 102 deletions

View File

@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks;
use Automattic\WooCommerce\Blocks\Templates\ProductCatalogTemplate;
use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/**
@ -23,6 +24,8 @@ class BlockTemplatesController {
*/
public function __construct() {
$this->init();
BlockTemplatesRegistry::register_templates();
}
/**

View File

@ -1,8 +1,20 @@
<?php
namespace Automattic\WooCommerce\Blocks;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
use Automattic\WooCommerce\Blocks\Templates\AbstractTemplate;
use Automattic\WooCommerce\Blocks\Templates\AbstractTemplatePart;
use Automattic\WooCommerce\Blocks\Templates\MiniCartTemplate;
use Automattic\WooCommerce\Blocks\Templates\CartTemplate;
use Automattic\WooCommerce\Blocks\Templates\CheckoutTemplate;
use Automattic\WooCommerce\Blocks\Templates\CheckoutHeaderTemplate;
use Automattic\WooCommerce\Blocks\Templates\OrderConfirmationTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductCatalogTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductCategoryTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductTagTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplate;
/**
* BlockTemplatesRegistry class.
@ -19,12 +31,29 @@ class BlockTemplatesRegistry {
private static $templates = array();
/**
* Method to register a template.
*
* @param AbstractTemplate|AbstractTemplatePart $template Template to register.
* Registers all templates used by WooCommerce.
*/
public static function register_template( $template ) {
self::$templates[ $template::SLUG ] = $template;
public static function register_templates() {
if ( BlockTemplateUtils::supports_block_templates( 'wp_template' ) ) {
$templates = array(
ProductCatalogTemplate::SLUG => new ProductCatalogTemplate(),
ProductCategoryTemplate::SLUG => new ProductCategoryTemplate(),
ProductTagTemplate::SLUG => new ProductTagTemplate(),
ProductAttributeTemplate::SLUG => new ProductAttributeTemplate(),
ProductSearchResultsTemplate::SLUG => new ProductSearchResultsTemplate(),
CartTemplate::SLUG => new CartTemplate(),
CheckoutTemplate::SLUG => new CheckoutTemplate(),
OrderConfirmationTemplate::SLUG => new OrderConfirmationTemplate(),
SingleProductTemplate::SLUG => new SingleProductTemplate(),
);
}
if ( BlockTemplateUtils::supports_block_templates( 'wp_template_part' ) ) {
$template_parts = array(
MiniCartTemplate::SLUG => new MiniCartTemplate(),
CheckoutHeaderTemplate::SLUG => new CheckoutHeaderTemplate(),
);
}
self::$templates = array_merge( $templates, $template_parts );
}
/**
@ -35,6 +64,9 @@ class BlockTemplatesRegistry {
* @return AbstractTemplate|AbstractTemplatePart|null
*/
public static function get_template( $template_slug ) {
if ( count( self::$templates ) === 0 ) {
self::register_templates();
}
if ( array_key_exists( $template_slug, self::$templates ) ) {
$registered_template = self::$templates[ $template_slug ];
return $registered_template;

View File

@ -6,7 +6,6 @@ use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use Automattic\WooCommerce\Blocks\AssetsController;
use Automattic\WooCommerce\Blocks\BlockPatterns;
use Automattic\WooCommerce\Blocks\BlockTemplatesController;
use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry;
use Automattic\WooCommerce\Blocks\BlockTypesController;
use Automattic\WooCommerce\Blocks\QueryFilters;
use Automattic\WooCommerce\Blocks\Domain\Services\CreateAccount;
@ -28,22 +27,11 @@ use Automattic\WooCommerce\Blocks\Payments\Integrations\Cheque;
use Automattic\WooCommerce\Blocks\Payments\Integrations\PayPal;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use Automattic\WooCommerce\Blocks\Registry\Container;
use Automattic\WooCommerce\Blocks\Templates\MiniCartTemplate;
use Automattic\WooCommerce\Blocks\Templates\CartTemplate;
use Automattic\WooCommerce\Blocks\Templates\CheckoutTemplate;
use Automattic\WooCommerce\Blocks\Templates\CheckoutHeaderTemplate;
use Automattic\WooCommerce\Blocks\Templates\OrderConfirmationTemplate;
use Automattic\WooCommerce\Blocks\Templates\ClassicTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductCatalogTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductCategoryTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductTagTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
use Automattic\WooCommerce\StoreApi\RoutesController;
use Automattic\WooCommerce\StoreApi\SchemaController;
use Automattic\WooCommerce\StoreApi\StoreApi;
use Automattic\WooCommerce\Blocks\Shipping\ShippingController;
use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplate;
use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility;
use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Domain\Services\OnboardingTasks\TasksController;
@ -158,19 +146,8 @@ class Bootstrap {
$this->container->get( BlockPatterns::class );
$this->container->get( BlockTypesController::class );
$this->container->get( BlockTemplatesController::class );
$this->container->get( ProductCatalogTemplate::class );
$this->container->get( ProductCategoryTemplate::class );
$this->container->get( ProductTagTemplate::class );
$this->container->get( ProductAttributeTemplate::class );
$this->container->get( ProductSearchResultsTemplate::class );
$this->container->get( MiniCartTemplate::class );
$this->container->get( CartTemplate::class );
$this->container->get( CheckoutTemplate::class );
$this->container->get( CheckoutHeaderTemplate::class );
$this->container->get( OrderConfirmationTemplate::class );
$this->container->get( ClassicTemplatesCompatibility::class );
$this->container->get( ArchiveProductTemplatesCompatibility::class )->init();
$this->container->get( SingleProductTemplate::class );
$this->container->get( SingleProductTemplateCompatibility::class )->init();
$this->container->get( Notices::class )->init();
}
@ -273,72 +250,6 @@ class Bootstrap {
return new BlockTemplatesController( $container->get( Package::class ) );
}
);
$this->container->register(
BlockTemplatesRegistry::class,
function () {
return new BlockTemplatesRegistry();
}
);
$this->container->register(
ProductAttributeTemplate::class,
function () {
return new ProductAttributeTemplate();
}
);
$this->container->register(
ProductCatalogTemplate::class,
function () {
return new ProductCatalogTemplate();
}
);
$this->container->register(
ProductCategoryTemplate::class,
function () {
return new ProductCategoryTemplate();
}
);
$this->container->register(
ProductTagTemplate::class,
function () {
return new ProductTagTemplate();
}
);
$this->container->register(
ProductSearchResultsTemplate::class,
function () {
return new ProductSearchResultsTemplate();
}
);
$this->container->register(
MiniCartTemplate::class,
function () {
return new MiniCartTemplate();
}
);
$this->container->register(
CartTemplate::class,
function () {
return new CartTemplate();
}
);
$this->container->register(
CheckoutTemplate::class,
function () {
return new CheckoutTemplate();
}
);
$this->container->register(
CheckoutHeaderTemplate::class,
function () {
return new CheckoutHeaderTemplate();
}
);
$this->container->register(
OrderConfirmationTemplate::class,
function () {
return new OrderConfirmationTemplate();
}
);
$this->container->register(
ClassicTemplatesCompatibility::class,
function ( Container $container ) {
@ -352,12 +263,6 @@ class Bootstrap {
return new ArchiveProductTemplatesCompatibility();
}
);
$this->container->register(
SingleProductTemplate::class,
function () {
return new SingleProductTemplate();
}
);
$this->container->register(
SingleProductTemplateCompatibility::class,
function () {

View File

@ -39,7 +39,6 @@ abstract class AbstractTemplate {
*/
public function __construct() {
if ( BlockTemplateUtils::supports_block_templates( 'wp_template' ) ) {
BlockTemplatesRegistry::register_template( $this );
$this->init();
}
}

View File

@ -24,7 +24,6 @@ abstract class AbstractTemplatePart extends AbstractTemplate {
*/
public function __construct() {
if ( BlockTemplateUtils::supports_block_templates( 'wp_template_part' ) ) {
BlockTemplatesRegistry::register_template( $this );
$this->init();
}
}