From c01e9e6582fd245239b02aeec29e5952c0787ed7 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 11 Jan 2022 11:21:42 +0000 Subject: [PATCH 1/2] Allow relevant block templates to be listed in the edit product view --- .../admin/class-wc-admin-meta-boxes.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php b/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php index b983947833d..7f68ec4740f 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php @@ -68,7 +68,7 @@ class WC_Admin_Meta_Boxes { add_action( 'admin_notices', array( $this, 'output_errors' ) ); add_action( 'shutdown', array( $this, 'save_errors' ) ); - add_filter( 'theme_product_templates', array( $this, 'remove_block_templates' ), 10, 1 ); + add_filter( 'theme_product_templates', array( $this, 'filter_product_block_templates' ), 10, 1 ); } /** @@ -226,14 +226,15 @@ class WC_Admin_Meta_Boxes { } /** - * Remove block-based templates from the list of available templates for products. + * Remove irrelevant block templates from the list of available templates for products. + * This will also remove custom created templates. * * @param string[] $templates Array of template header names keyed by the template file name. * * @return string[] Templates array excluding block-based templates. */ - public function remove_block_templates( $templates ) { - if ( count( $templates ) === 0 || ! function_exists( 'gutenberg_get_block_template' ) ) { + public function filter_product_block_templates( $templates ) { + if ( count( $templates ) === 0 || ! wc_current_theme_is_fse_theme() || ! function_exists( 'gutenberg_get_block_template' ) ) { return $templates; } @@ -241,9 +242,15 @@ class WC_Admin_Meta_Boxes { $filtered_templates = array(); foreach ( $templates as $template_key => $template_name ) { - $gutenberg_template = gutenberg_get_block_template( $theme . '//' . $template_key ); + // Filter out the single-product.html template as this is a duplicate of "Default Template". + if ( 'single-product' === $template_key ) { + continue; + } - if ( ! $gutenberg_template ) { + $block_template = gutenberg_get_block_template( $theme . '//' . $template_key ); + + // If the block template has the product post type specified, include it. + if ( in_array( 'product', $block_template->post_types ) ) { $filtered_templates[ $template_key ] = $template_name; } } From 88a623639c9eb39f4ce2a09f7046d6dd6bb18640 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 14 Jan 2022 14:26:12 +0000 Subject: [PATCH 2/2] Code review feedback --- .../includes/admin/class-wc-admin-meta-boxes.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php b/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php index 7f68ec4740f..b8c39253cf3 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php @@ -68,7 +68,7 @@ class WC_Admin_Meta_Boxes { add_action( 'admin_notices', array( $this, 'output_errors' ) ); add_action( 'shutdown', array( $this, 'save_errors' ) ); - add_filter( 'theme_product_templates', array( $this, 'filter_product_block_templates' ), 10, 1 ); + add_filter( 'theme_product_templates', array( $this, 'remove_block_templates' ), 10, 1 ); } /** @@ -233,8 +233,8 @@ class WC_Admin_Meta_Boxes { * * @return string[] Templates array excluding block-based templates. */ - public function filter_product_block_templates( $templates ) { - if ( count( $templates ) === 0 || ! wc_current_theme_is_fse_theme() || ! function_exists( 'gutenberg_get_block_template' ) ) { + public function remove_block_templates( $templates ) { + if ( count( $templates ) === 0 || ! wc_current_theme_is_fse_theme() || ( ! function_exists( 'gutenberg_get_block_template' ) && ! function_exists( 'get_block_template' ) ) ) { return $templates; } @@ -247,10 +247,12 @@ class WC_Admin_Meta_Boxes { continue; } - $block_template = gutenberg_get_block_template( $theme . '//' . $template_key ); + $block_template = function_exists( 'gutenberg_get_block_template' ) ? + gutenberg_get_block_template( $theme . '//' . $template_key ) : + get_block_template( $theme . '//' . $template_key ); // If the block template has the product post type specified, include it. - if ( in_array( 'product', $block_template->post_types ) ) { + if ( $block_template && in_array( 'product', $block_template->post_types ) ) { $filtered_templates[ $template_key ] = $template_name; } }