diff --git a/plugins/woocommerce-blocks/src/BlockTemplatesController.php b/plugins/woocommerce-blocks/src/BlockTemplatesController.php index 2b5c408b6d4..453807dac6c 100644 --- a/plugins/woocommerce-blocks/src/BlockTemplatesController.php +++ b/plugins/woocommerce-blocks/src/BlockTemplatesController.php @@ -36,9 +36,9 @@ class BlockTemplatesController { * Initialization method. */ protected function init() { - add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); add_action( 'template_redirect', array( $this, 'render_block_template' ) ); add_filter( 'pre_get_block_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); + add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); } /** @@ -156,6 +156,16 @@ class BlockTemplatesController { continue; } + // If the current $post_type is set (e.g. on an Edit Post screen), and isn't included in the available post_types + // on the template file, then lets skip it so that it doesn't get added. This is typically used to hide templates + // in the template dropdown on the Edit Post page. + if ( $post_type && + isset( $template_file->post_types ) && + ! in_array( $post_type, $template_file->post_types, true ) + ) { + continue; + } + // It would be custom if the template was modified in the editor, so if it's not custom we can load it from // the filesystem. if ( 'custom' !== $template_file->source ) { @@ -166,13 +176,6 @@ class BlockTemplatesController { continue; } - if ( $post_type && - isset( $template->post_types ) && - ! in_array( $post_type, $template->post_types, true ) - ) { - continue; - } - $is_not_custom = false === array_search( wp_get_theme()->get_stylesheet() . '//' . $template_file->slug, array_column( $query_result, 'id' ), @@ -317,6 +320,7 @@ class BlockTemplatesController { 'source' => 'woocommerce', 'title' => BlockTemplateUtils::convert_slug_to_title( $template_slug ), 'description' => '', + 'post_types' => array(), // Don't appear in any Edit Post template selector dropdown. ); $templates[] = (object) $new_template_item; } diff --git a/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php b/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php index ff690d50fbb..23c090a3b3f 100644 --- a/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php +++ b/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php @@ -108,6 +108,7 @@ class BlockTemplateUtils { $template->status = $post->post_status; $template->has_theme_file = $has_theme_file; $template->is_custom = true; + $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. return $template; } @@ -135,6 +136,7 @@ class BlockTemplateUtils { $template->status = 'publish'; $template->has_theme_file = true; $template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are. + $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. return $template; }