Skip to content

Commit

Permalink
Refactor BlockTemplatesController (#44537)
Browse files Browse the repository at this point in the history
* Cleanup BlockTemplatesController constructor

* Remove unnecessary elseif

* Remove unnecessary param comment

* Move Single Product Template responsibilities to the SingleProductTemplate.php file

* Move Mini-Cart Template reposnibility to the MiniCartTemplate.php file

* Create the other template files

* Code cleanup

* Move template redirect into template files

* PHP cleanup

* Add changelog entry

* Fix PHP tests

* Replace hardcoded 'archive-product' slug with ProductCatalogTemplate::SLUG

* Make it so AbstractTemplatePart extends AbstractTemplate

* Register templates in BlockTemplatesRegistry

* Fix slug usage in AbstractPageTemplate.php

* Add get_template_title and get_template_description methods to AbstractTemplate

* Cleanup

* Make init functions protected in template classes

* Avoid using static constants and methods in BlockTemplatesRegistry

* Avoid using static constants and methods in block template classes

* Fix lint errors

* Fix Single Product classes not being applied

* Fix tests

* Get BlockTemplatesRegistry directly from BlockTemplateUtils to simplify code

* Init BlockTemplatesRegistry and BlockTemplatesController from Bootstrap.php

* Fix wrong static::SLUG call

* Init template classes from BlockTemplatesRegistry

* Revert "Fix wrong static::SLUG call"

This reverts commit 866c9b9.
  • Loading branch information
Aljullu authored and Konamiman committed Mar 13, 2024
1 parent 9f6ae36 commit f27e131
Show file tree
Hide file tree
Showing 22 changed files with 883 additions and 428 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Significance: patch
Type: update
Comment: Updated the template logic used by block themes, to make it more performant and resilient.
244 changes: 24 additions & 220 deletions plugins/woocommerce/src/Blocks/BlockTemplatesController.php

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions plugins/woocommerce/src/Blocks/BlockTemplatesRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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.
*
* @internal
*/
class BlockTemplatesRegistry {

/**
* The array of registered templates.
*
* @var AbstractTemplate[]|AbstractTemplatePart[]
*/
private $templates = array();

/**
* Initialization method.
*/
public function init() {
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(),
);
} else {
$templates = array();
}
if ( BlockTemplateUtils::supports_block_templates( 'wp_template_part' ) ) {
$template_parts = array(
MiniCartTemplate::SLUG => new MiniCartTemplate(),
CheckoutHeaderTemplate::SLUG => new CheckoutHeaderTemplate(),
);
} else {
$template_parts = array();
}
$this->templates = array_merge( $templates, $template_parts );

// Init all templates.
foreach ( $this->templates as $template ) {
$template->init();
}
}

/**
* Returns the template matching the slug
*
* @param string $template_slug Slug of the template to retrieve.
*
* @return AbstractTemplate|AbstractTemplatePart|null
*/
public function get_template( $template_slug ) {
if ( array_key_exists( $template_slug, $this->templates ) ) {
$registered_template = $this->templates[ $template_slug ];
return $registered_template;
}
return null;
}
}
11 changes: 7 additions & 4 deletions plugins/woocommerce/src/Blocks/BlockTypes/ClassicTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace Automattic\WooCommerce\Blocks\BlockTypes;

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\OrderConfirmationTemplate;
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;
Expand Down Expand Up @@ -99,7 +102,7 @@ protected function render( $attributes, $content, $block ) {
$frontend_scripts::load_scripts();
}

if ( OrderConfirmationTemplate::get_slug() === $attributes['template'] ) {
if ( OrderConfirmationTemplate::SLUG === $attributes['template'] ) {
return $this->render_order_received();
}

Expand All @@ -109,9 +112,9 @@ protected function render( $attributes, $content, $block ) {

$valid = false;
$archive_templates = array(
'archive-product',
'taxonomy-product_cat',
'taxonomy-product_tag',
ProductCatalogTemplate::SLUG,
ProductCategoryTemplate::SLUG,
ProductTagTemplate::SLUG,
ProductAttributeTemplate::SLUG,
ProductSearchResultsTemplate::SLUG,
);
Expand Down
57 changes: 8 additions & 49 deletions plugins/woocommerce/src/Blocks/Domain/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use Automattic\WooCommerce\Blocks\AssetsController;
use Automattic\WooCommerce\Blocks\BlockPatterns;
use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry;
use Automattic\WooCommerce\Blocks\BlockTemplatesController;
use Automattic\WooCommerce\Blocks\BlockTypesController;
use Automattic\WooCommerce\Blocks\QueryFilters;
Expand All @@ -28,13 +29,7 @@
use Automattic\WooCommerce\Blocks\Payments\Integrations\PayPal;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use Automattic\WooCommerce\Blocks\Registry\Container;
use Automattic\WooCommerce\Blocks\Templates\CartTemplate;
use Automattic\WooCommerce\Blocks\Templates\CheckoutHeaderTemplate;
use Automattic\WooCommerce\Blocks\Templates\CheckoutTemplate;
use Automattic\WooCommerce\Blocks\Templates\ClassicTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Templates\OrderConfirmationTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
use Automattic\WooCommerce\StoreApi\RoutesController;
use Automattic\WooCommerce\StoreApi\SchemaController;
use Automattic\WooCommerce\StoreApi\StoreApi;
Expand Down Expand Up @@ -152,13 +147,8 @@ function() {
// regular rest requests to maintain compatibility with the store editor.
$this->container->get( BlockPatterns::class );
$this->container->get( BlockTypesController::class );
$this->container->get( BlockTemplatesController::class );
$this->container->get( ProductSearchResultsTemplate::class );
$this->container->get( ProductAttributeTemplate::class );
$this->container->get( CartTemplate::class );
$this->container->get( CheckoutTemplate::class );
$this->container->get( CheckoutHeaderTemplate::class );
$this->container->get( OrderConfirmationTemplate::class );
$this->container->get( BlockTemplatesRegistry::class )->init();
$this->container->get( BlockTemplatesController::class )->init();
$this->container->get( ClassicTemplatesCompatibility::class );
$this->container->get( ArchiveProductTemplatesCompatibility::class )->init();
$this->container->get( SingleProductTemplateCompatibility::class )->init();
Expand Down Expand Up @@ -259,45 +249,15 @@ function ( Container $container ) {
}
);
$this->container->register(
BlockTemplatesController::class,
BlockTemplatesRegistry::class,
function ( Container $container ) {
return new BlockTemplatesController( $container->get( Package::class ) );
}
);
$this->container->register(
ProductSearchResultsTemplate::class,
function () {
return new ProductSearchResultsTemplate();
}
);
$this->container->register(
ProductAttributeTemplate::class,
function () {
return new ProductAttributeTemplate();
}
);
$this->container->register(
CartTemplate::class,
function () {
return new CartTemplate();
return new BlockTemplatesRegistry();
}
);
$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();
BlockTemplatesController::class,
function ( Container $container ) {
return new BlockTemplatesController();
}
);
$this->container->register(
Expand All @@ -313,7 +273,6 @@ function () {
return new ArchiveProductTemplatesCompatibility();
}
);

$this->container->register(
SingleProductTemplateCompatibility::class,
function () {
Expand Down
31 changes: 3 additions & 28 deletions plugins/woocommerce/src/Blocks/Templates/AbstractPageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,15 @@
*
* @internal
*/
abstract class AbstractPageTemplate {
/**
* Page Template functionality is only initialized when using a block theme.
*/
public function __construct() {
if ( wc_current_theme_is_fse_theme() ) {
$this->init();
}
}

abstract class AbstractPageTemplate extends AbstractTemplate {
/**
* Initialization method.
*/
protected function init() {
public function init() {
add_filter( 'page_template_hierarchy', array( $this, 'page_template_hierarchy' ), 1 );
add_filter( 'pre_get_document_title', array( $this, 'page_template_title' ) );
}

/**
* Returns the template slug.
*
* @return string
*/
abstract public static function get_slug();

/**
* Returns the page object assigned to this template/page.
*
Expand All @@ -47,15 +31,6 @@ abstract protected function get_placeholder_page();
*/
abstract protected function is_active_template();

/**
* Should return the title of the page, or an empty string if the page title should not be changed.
*
* @return string
*/
public static function get_template_title() {
return '';
}

/**
* When the page should be displaying the template, add it to the hierarchy.
*
Expand All @@ -67,7 +42,7 @@ public static function get_template_title() {
*/
public function page_template_hierarchy( $templates ) {
if ( $this->is_active_template() ) {
array_unshift( $templates, $this->get_slug() );
array_unshift( $templates, static::SLUG );
}
return $templates;
}
Expand Down
38 changes: 38 additions & 0 deletions plugins/woocommerce/src/Blocks/Templates/AbstractTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Automattic\WooCommerce\Blocks\Templates;

/**
* AbstractTemplate class.
*
* Shared logic for templates.
*
* @internal
*/
abstract class AbstractTemplate {

/**
* The slug of the template.
*
* @var string
*/
const SLUG = '';

/**
* Initialization method.
*/
abstract public function init();

/**
* Should return the title of the template.
*
* @return string
*/
abstract public function get_template_title();

/**
* Should return the description of the template.
*
* @return string
*/
abstract public function get_template_description();
}
18 changes: 18 additions & 0 deletions plugins/woocommerce/src/Blocks/Templates/AbstractTemplatePart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Automattic\WooCommerce\Blocks\Templates;

/**
* AbstractTemplatePart class.
*
* Shared logic for templates parts.
*
* @internal
*/
abstract class AbstractTemplatePart extends AbstractTemplate {
/**
* The template part area where the template part belongs.
*
* @var string
*/
public $template_area;
}

0 comments on commit f27e131

Please sign in to comment.