Conditionally initialize block template compatibility classes (#50785)

* Don't initialize the block template compatibility classes in classic themes

* Conditionally initialize the block template compatibility classes when the template is being rendered

* Add changelog file

* Remove unnecessary blank line

* Conditionally initialize the block template compatibility classes when the template is being rendered (II)
This commit is contained in:
Albert Juhé Lluveras 2024-08-29 08:54:28 +02:00 committed by GitHub
parent 055cf09cc1
commit b7c4c68598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 27 additions and 19 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: performance
Conditionally initialize block template compatibility classes

View File

@ -169,8 +169,6 @@ class Bootstrap {
$this->container->get( BlockPatterns::class );
$this->container->get( BlockTypesController::class );
$this->container->get( ClassicTemplatesCompatibility::class );
$this->container->get( ArchiveProductTemplatesCompatibility::class )->init();
$this->container->get( SingleProductTemplateCompatibility::class )->init();
$this->container->get( Notices::class )->init();
$this->container->get( PTKPatternsStore::class );
$this->container->get( TemplateOptions::class )->init();
@ -276,18 +274,6 @@ class Bootstrap {
return new ClassicTemplatesCompatibility( $asset_data_registry );
}
);
$this->container->register(
ArchiveProductTemplatesCompatibility::class,
function () {
return new ArchiveProductTemplatesCompatibility();
}
);
$this->container->register(
SingleProductTemplateCompatibility::class,
function () {
return new SingleProductTemplateCompatibility();
}
);
$this->container->register(
DraftOrders::class,
function ( Container $container ) {

View File

@ -21,10 +21,6 @@ abstract class AbstractTemplateCompatibility {
* Initialization method.
*/
public function init() {
if ( ! wc_current_theme_is_fse_theme() ) {
return;
}
$this->set_hook_data();
add_filter(

View File

@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/**
@ -61,6 +62,9 @@ class ProductAttributeTemplate extends AbstractTemplate {
}
if ( isset( $queried_object->taxonomy ) && taxonomy_is_product_attribute( $queried_object->taxonomy ) ) {
$compatibility_layer = new ArchiveProductTemplatesCompatibility();
$compatibility_layer->init();
$templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) );
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {

View File

@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/**
@ -49,6 +50,9 @@ class ProductCatalogTemplate extends AbstractTemplate {
*/
public function render_block_template() {
if ( ! is_embed() && ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) ) {
$compatibility_layer = new ArchiveProductTemplatesCompatibility();
$compatibility_layer->init();
$templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) );
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {

View File

@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/**
@ -55,6 +56,9 @@ class ProductCategoryTemplate extends AbstractTemplate {
*/
public function render_block_template() {
if ( ! is_embed() && is_product_taxonomy() && is_tax( 'product_cat' ) ) {
$compatibility_layer = new ArchiveProductTemplatesCompatibility();
$compatibility_layer->init();
$templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) );
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {

View File

@ -1,6 +1,7 @@
<?php
namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/**
@ -48,6 +49,9 @@ class ProductSearchResultsTemplate extends AbstractTemplate {
*/
public function render_block_template() {
if ( ! is_embed() && is_post_type_archive( 'product' ) && is_search() ) {
$compatibility_layer = new ArchiveProductTemplatesCompatibility();
$compatibility_layer->init();
$templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) );
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {

View File

@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/**
@ -55,6 +56,9 @@ class ProductTagTemplate extends AbstractTemplate {
*/
public function render_block_template() {
if ( ! is_embed() && is_product_taxonomy() && is_tax( 'product_tag' ) ) {
$compatibility_layer = new ArchiveProductTemplatesCompatibility();
$compatibility_layer->init();
$templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) );
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {

View File

@ -51,6 +51,9 @@ class SingleProductTemplate extends AbstractTemplate {
if ( ! is_embed() && is_singular( 'product' ) ) {
global $post;
$compatibility_layer = new SingleProductTemplateCompatibility();
$compatibility_layer->init();
$valid_slugs = array( self::SLUG );
$single_product_slug = 'product' === $post->post_type && $post->post_name ? 'single-product-' . $post->post_name : '';
if ( $single_product_slug ) {

View File

@ -14,7 +14,6 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
const IS_FIRST_BLOCK = '__wooCommerceIsFirstBlock';
const IS_LAST_BLOCK = '__wooCommerceIsLastBlock';
/**
* Inject hooks to rendered content of corresponding blocks.
*