-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor BlockTemplatesController (#44537)
* 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
Showing
22 changed files
with
883 additions
and
428 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
plugins/woocommerce/changelog/44537-update-BlockTemplatesController-refactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
244
plugins/woocommerce/src/Blocks/BlockTemplatesController.php
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
plugins/woocommerce/src/Blocks/Templates/AbstractTemplate.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
plugins/woocommerce/src/Blocks/Templates/AbstractTemplatePart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.