From b7c4c6859861ea0bed21ba05e0f41f8a27af384f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Thu, 29 Aug 2024 08:54:28 +0200 Subject: [PATCH] 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) --- .../changelog/fix-46527-compatibility-layer-init | 4 ++++ .../woocommerce/src/Blocks/Domain/Bootstrap.php | 14 -------------- .../Templates/AbstractTemplateCompatibility.php | 4 ---- .../Blocks/Templates/ProductAttributeTemplate.php | 4 ++++ .../Blocks/Templates/ProductCatalogTemplate.php | 4 ++++ .../Blocks/Templates/ProductCategoryTemplate.php | 4 ++++ .../Templates/ProductSearchResultsTemplate.php | 4 ++++ .../src/Blocks/Templates/ProductTagTemplate.php | 4 ++++ .../src/Blocks/Templates/SingleProductTemplate.php | 3 +++ .../SingleProductTemplateCompatibility.php | 1 - 10 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-46527-compatibility-layer-init diff --git a/plugins/woocommerce/changelog/fix-46527-compatibility-layer-init b/plugins/woocommerce/changelog/fix-46527-compatibility-layer-init new file mode 100644 index 00000000000..91dbcabb154 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-46527-compatibility-layer-init @@ -0,0 +1,4 @@ +Significance: patch +Type: performance + +Conditionally initialize block template compatibility classes diff --git a/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php b/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php index 7966386aceb..dbf929aa3fb 100644 --- a/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php +++ b/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php @@ -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 ) { diff --git a/plugins/woocommerce/src/Blocks/Templates/AbstractTemplateCompatibility.php b/plugins/woocommerce/src/Blocks/Templates/AbstractTemplateCompatibility.php index b06892291c3..cbd427547b0 100644 --- a/plugins/woocommerce/src/Blocks/Templates/AbstractTemplateCompatibility.php +++ b/plugins/woocommerce/src/Blocks/Templates/AbstractTemplateCompatibility.php @@ -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( diff --git a/plugins/woocommerce/src/Blocks/Templates/ProductAttributeTemplate.php b/plugins/woocommerce/src/Blocks/Templates/ProductAttributeTemplate.php index 828133704a3..153eadb3c9f 100644 --- a/plugins/woocommerce/src/Blocks/Templates/ProductAttributeTemplate.php +++ b/plugins/woocommerce/src/Blocks/Templates/ProductAttributeTemplate.php @@ -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] ) ) { diff --git a/plugins/woocommerce/src/Blocks/Templates/ProductCatalogTemplate.php b/plugins/woocommerce/src/Blocks/Templates/ProductCatalogTemplate.php index 2f3d3383ccb..7059ed379b9 100644 --- a/plugins/woocommerce/src/Blocks/Templates/ProductCatalogTemplate.php +++ b/plugins/woocommerce/src/Blocks/Templates/ProductCatalogTemplate.php @@ -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] ) ) { diff --git a/plugins/woocommerce/src/Blocks/Templates/ProductCategoryTemplate.php b/plugins/woocommerce/src/Blocks/Templates/ProductCategoryTemplate.php index 604cbb51aa7..233d7c2a877 100644 --- a/plugins/woocommerce/src/Blocks/Templates/ProductCategoryTemplate.php +++ b/plugins/woocommerce/src/Blocks/Templates/ProductCategoryTemplate.php @@ -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] ) ) { diff --git a/plugins/woocommerce/src/Blocks/Templates/ProductSearchResultsTemplate.php b/plugins/woocommerce/src/Blocks/Templates/ProductSearchResultsTemplate.php index ea8daab4768..c421fa7a0a0 100644 --- a/plugins/woocommerce/src/Blocks/Templates/ProductSearchResultsTemplate.php +++ b/plugins/woocommerce/src/Blocks/Templates/ProductSearchResultsTemplate.php @@ -1,6 +1,7 @@ init(); + $templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) ); if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { diff --git a/plugins/woocommerce/src/Blocks/Templates/ProductTagTemplate.php b/plugins/woocommerce/src/Blocks/Templates/ProductTagTemplate.php index 6049f429dcd..fef90fdc5ad 100644 --- a/plugins/woocommerce/src/Blocks/Templates/ProductTagTemplate.php +++ b/plugins/woocommerce/src/Blocks/Templates/ProductTagTemplate.php @@ -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] ) ) { diff --git a/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php b/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php index 69ef307fc3a..839ed177e09 100644 --- a/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php +++ b/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php @@ -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 ) { diff --git a/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplateCompatibility.php b/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplateCompatibility.php index f48d7697798..caa7b691576 100644 --- a/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplateCompatibility.php +++ b/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplateCompatibility.php @@ -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. *