diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index f1fc55089a1..bdcf719c98f 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( 'template_redirect', array( $this, 'render_block_template' ) ); } /** @@ -105,4 +106,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() || ! function_exists( 'gutenberg_supports_block_templates' ) || ! 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 ); + } + } } 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();