Hide WooCommmerce block templates from Edit Post template dropdown (https://github.com/woocommerce/woocommerce-blocks/pull/5167)

* Hide WooCommmerce block templates from Edit Post template dropdown

* Add empty post_types value to template and update comments.
This commit is contained in:
Tom Cafferkey 2021-11-17 14:40:15 +00:00 committed by GitHub
parent e891cab38b
commit 3adfdd4d3b
2 changed files with 14 additions and 8 deletions

View File

@ -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;
}

View File

@ -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;
}