From c406f1991efb62cd77cc8550357cb79d63635250 Mon Sep 17 00:00:00 2001 From: Moon Date: Fri, 9 Aug 2024 03:08:02 -0700 Subject: [PATCH] Manually init template registry and controller for non-block themes (#50507) * Manually init template registry and cotnroller when get_query_template returns nothing * Add changefile(s) from automation for the following project(s): woocommerce * Validate template before including it * Move class registration to init method * Remove container registration from coming soon handler * Lint * Moved registration to register_dependencies * Change blocks init condition to only when it's not initialized --------- Co-authored-by: github-actions Co-authored-by: Adrian Duffell <9312929+adrianduffell@users.noreply.github.com> Co-authored-by: Ilyas Foo --- .../changelog/50507-update-fix-empty-template | 4 ++++ .../src/Blocks/Domain/Bootstrap.php | 24 +++++++++---------- .../ComingSoon/ComingSoonRequestHandler.php | 21 ++++++++++++---- 3 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 plugins/woocommerce/changelog/50507-update-fix-empty-template diff --git a/plugins/woocommerce/changelog/50507-update-fix-empty-template b/plugins/woocommerce/changelog/50507-update-fix-empty-template new file mode 100644 index 00000000000..316efe3e3a7 --- /dev/null +++ b/plugins/woocommerce/changelog/50507-update-fix-empty-template @@ -0,0 +1,4 @@ +Significance: patch +Type: fix +Comment: Manually initialize block template registry and controller for non-block themes. + diff --git a/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php b/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php index 7badad23aa7..7966386aceb 100644 --- a/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php +++ b/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php @@ -132,18 +132,6 @@ class Bootstrap { $is_store_api_request = wc()->is_store_api_request(); if ( ! $is_store_api_request && ( wc_current_theme_is_fse_theme() || current_theme_supports( 'block-template-parts' ) ) ) { - $this->container->register( - BlockTemplatesRegistry::class, - function () { - return new BlockTemplatesRegistry(); - } - ); - $this->container->register( - BlockTemplatesController::class, - function () { - return new BlockTemplatesController(); - } - ); $this->container->get( BlockTemplatesRegistry::class )->init(); $this->container->get( BlockTemplatesController::class )->init(); } @@ -448,6 +436,18 @@ class Bootstrap { return new QueryFilters(); } ); + $this->container->register( + BlockTemplatesRegistry::class, + function () { + return new BlockTemplatesRegistry(); + } + ); + $this->container->register( + BlockTemplatesController::class, + function () { + return new BlockTemplatesController(); + } + ); } /** diff --git a/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php b/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php index 31057ab11bd..f0465b18d0f 100644 --- a/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php +++ b/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php @@ -2,6 +2,9 @@ namespace Automattic\WooCommerce\Internal\ComingSoon; use Automattic\WooCommerce\Admin\Features\Features; +use Automattic\WooCommerce\Blocks\BlockTemplatesController; +use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry; +use Automattic\WooCommerce\Blocks\Package as BlocksPackage; /** * Handles the template_include hook to determine whether the current page needs @@ -48,13 +51,21 @@ class ComingSoonRequestHandler { // A coming soon page needs to be displayed. Don't cache this response. nocache_headers(); + $is_fse_theme = wc_current_theme_is_fse_theme(); + $is_store_coming_soon = $this->coming_soon_helper->is_store_coming_soon(); + + if ( ! $is_fse_theme && ! current_theme_supports( 'block-template-parts' ) ) { + // Initialize block templates for use in classic theme. + BlocksPackage::init(); + $container = BlocksPackage::container(); + $container->get( BlockTemplatesRegistry::class )->init(); + $container->get( BlockTemplatesController::class )->init(); + } + add_theme_support( 'block-templates' ); $coming_soon_template = get_query_template( 'coming-soon' ); - $is_fse_theme = wc_current_theme_is_fse_theme(); - $is_store_coming_soon = $this->coming_soon_helper->is_store_coming_soon(); - if ( ! $is_fse_theme && $is_store_coming_soon ) { get_header(); } @@ -66,7 +77,9 @@ class ComingSoonRequestHandler { } ); - include $coming_soon_template; + if ( ! empty( $coming_soon_template ) && file_exists( $coming_soon_template ) ) { + include $coming_soon_template; + } if ( ! $is_fse_theme && $is_store_coming_soon ) { get_footer();