From be458b7e0b8e261c3050f71d32e140b4257cf83d Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 25 Oct 2021 11:15:45 +0100 Subject: [PATCH 1/6] Add render logic to BlockTemplatesController --- src/BlockTemplatesController.php | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 8225f989acb..910c02b7f06 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -37,6 +37,7 @@ public function __construct() { */ protected function init() { add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); + add_action( 'wp', array( $this, 'render_block_template' ) ); } /** @@ -103,4 +104,37 @@ public function theme_has_template( $template_name ) { return is_readable( get_template_directory() . '/block-templates/' . $template_name . '.html' ) || is_readable( get_stylesheet_directory() . '/block-templates/' . $template_name . '.html' ); } + + /** + * Checks whether a block template with that name exists in Woo Blocks + * + * @param string $template_name Template to check. + * @return boolean + */ + public function default_block_template_is_available( $template_name ) { + if ( ! $template_name ) { + return false; + } + + return is_readable( + $this->templates_directory . '/' . $template_name . '.html' + ); + } + + /** + * Renders the default block template from Woo Blocks if no theme templates exist. + */ + public function render_block_template() { + if ( is_embed() || ! gutenberg_supports_block_templates() ) { + return; + } + + if ( + is_singular( 'product' ) && + ! $this->theme_has_template( 'single-product' ) && + $this->default_block_template_is_available( 'single-product' ) + ) { + add_filter( 'wc_has_block_template', '__return_true', 10, 0 ); + } + } } From 652a5330210063520c756c5962ae2e2e9fdc423a Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 26 Oct 2021 16:07:37 +0100 Subject: [PATCH 2/6] Comment out action to test e2e tests --- src/BlockTemplatesController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 910c02b7f06..289f903f210 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -37,7 +37,6 @@ public function __construct() { */ protected function init() { add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); - add_action( 'wp', array( $this, 'render_block_template' ) ); } /** From 1c49561b0bbefa14aaa310aa9d6c45cbe8db5f8f Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 26 Oct 2021 16:27:24 +0100 Subject: [PATCH 3/6] Add add_action back into initialise render method --- src/BlockTemplatesController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 289f903f210..910c02b7f06 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -37,6 +37,7 @@ public function __construct() { */ protected function init() { add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); + add_action( 'wp', array( $this, 'render_block_template' ) ); } /** From 16b307a627a6bffdae4cb0fd1f13bfa2a4480fc3 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 29 Oct 2021 07:34:24 +0100 Subject: [PATCH 4/6] Check function exists before using it --- src/BlockTemplatesController.php | 2 +- src/Utils/BlockTemplateUtils.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 910c02b7f06..951a4ae6714 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -125,7 +125,7 @@ public function default_block_template_is_available( $template_name ) { * Renders the default block template from Woo Blocks if no theme templates exist. */ public function render_block_template() { - if ( is_embed() || ! gutenberg_supports_block_templates() ) { + if ( is_embed() || function_exists( 'gutenberg_supports_block_templates' ) && ! gutenberg_supports_block_templates() ) { return; } diff --git a/src/Utils/BlockTemplateUtils.php b/src/Utils/BlockTemplateUtils.php index a2dffb7b4ec..72a70db8fae 100644 --- a/src/Utils/BlockTemplateUtils.php +++ b/src/Utils/BlockTemplateUtils.php @@ -83,7 +83,7 @@ public static function gutenberg_inject_theme_attribute_in_content( $template_co * @return WP_Block_Template Template. */ public static function gutenberg_build_template_result_from_file( $template_file, $template_type ) { - $default_template_types = gutenberg_get_default_template_types(); + $default_template_types = function_exists( 'gutenberg_get_default_template_types' ) ? gutenberg_get_default_template_types() : array(); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $template_content = file_get_contents( $template_file['path'] ); $theme = wp_get_theme()->get_stylesheet(); From 6ecd5328ebf794072d8fa8535dfe683ca122e5ba Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 29 Oct 2021 07:57:09 +0100 Subject: [PATCH 5/6] Change hook from wp to template_redirect --- src/BlockTemplatesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 951a4ae6714..7148e6a8602 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -37,7 +37,7 @@ public function __construct() { */ protected function init() { add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); - add_action( 'wp', array( $this, 'render_block_template' ) ); + add_action( 'template_redirect', array( $this, 'render_block_template' ) ); } /** From ff4384fc4d0c7ec011a560f3344aa5ec6c446805 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 29 Oct 2021 12:28:01 +0100 Subject: [PATCH 6/6] Update src/BlockTemplatesController.php Co-authored-by: Tung Du --- src/BlockTemplatesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 7148e6a8602..5260be3d13d 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -125,7 +125,7 @@ public function default_block_template_is_available( $template_name ) { * Renders the default block template from Woo Blocks if no theme templates exist. */ public function render_block_template() { - if ( is_embed() || function_exists( 'gutenberg_supports_block_templates' ) && ! gutenberg_supports_block_templates() ) { + if ( is_embed() || ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) { return; }