diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index e017fc9dfd5e6..5d67b13eb4eda 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -253,10 +253,16 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t $template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug']; $template->status = 'publish'; $template->has_theme_file = true; + $template->is_custom = true; if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) { $template->description = $default_template_types[ $template_file['slug'] ]['description']; $template->title = $default_template_types[ $template_file['slug'] ]['title']; + $template->is_custom = false; + } + + if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) { + $template->post_types = $template_file['postTypes']; } if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) { @@ -274,7 +280,8 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t * @return WP_Block_Template|WP_Error Template. */ function _gutenberg_build_template_result_from_post( $post ) { - $terms = get_the_terms( $post, 'wp_theme' ); + $default_template_types = gutenberg_get_default_template_types(); + $terms = get_the_terms( $post, 'wp_theme' ); if ( is_wp_error( $terms ) ) { return $terms; @@ -300,6 +307,11 @@ function _gutenberg_build_template_result_from_post( $post ) { $template->title = $post->post_title; $template->status = $post->post_status; $template->has_theme_file = $has_theme_file; + $template->is_custom = true; + + if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { + $template->is_custom = false; + } if ( 'wp_template_part' === $post->post_type ) { $type_terms = get_the_terms( $post, 'wp_template_part_area' ); @@ -317,9 +329,10 @@ function _gutenberg_build_template_result_from_post( $post ) { * @param array $query { * Optional. Arguments to retrieve templates. * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). + * @type string $post_type Post type to get the templates for. * } * @param array $template_type wp_template or wp_template_part. * @@ -340,6 +353,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t * * @type array $slug__in List of slugs to include. * @type int $wp_id Post ID of customized template. + * @type string $post_type Post type to get the templates for. * } * @param array $template_type wp_template or wp_template_part. */ @@ -348,6 +362,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t return $templates; } + $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; $wp_query_args = array( 'post_status' => array( 'auto-draft', 'draft', 'publish' ), 'post_type' => $template_type, @@ -387,14 +402,33 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t foreach ( $template_query->posts as $post ) { $template = _gutenberg_build_template_result_from_post( $post ); - if ( ! is_wp_error( $template ) ) { - $query_result[] = $template; + if ( is_wp_error( $template ) ) { + continue; + } + + if ( $post_type && ! $template->is_custom ) { + continue; } + + $query_result[] = $template; } if ( ! isset( $query['wp_id'] ) ) { $template_files = _gutenberg_get_template_files( $template_type ); foreach ( $template_files as $template_file ) { + $template = _gutenberg_build_template_result_from_file( $template_file, $template_type ); + + if ( $post_type && ! $template->is_custom ) { + 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' ), @@ -406,7 +440,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t ! isset( $query['area'] ) || $template_file['area'] === $query['area']; $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; if ( $should_include ) { - $query_result[] = _gutenberg_build_template_result_from_file( $template_file, $template_type ); + $query_result[] = $template; } } } diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php index 20072d1253af5..dafb15509bd01 100644 --- a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -141,6 +141,10 @@ public function get_items( $request ) { if ( isset( $request['area'] ) ) { $query['area'] = $request['area']; } + if ( isset( $request['post_type'] ) ) { + $query['post_type'] = $request['post_type']; + } + $templates = array(); foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) { $data = $this->prepare_item_for_response( $template, $request ); @@ -483,11 +487,19 @@ protected function get_available_actions() { */ public function get_collection_params() { return array( - 'context' => $this->get_context_param(), - 'wp_id' => array( + 'context' => $this->get_context_param(), + 'wp_id' => array( 'description' => __( 'Limit to the specified post id.', 'gutenberg' ), 'type' => 'integer', ), + 'area' => array( + 'description' => __( 'Limit to the specified template part area.', 'gutenberg' ), + 'type' => 'string', + ), + 'post_type' => array( + 'description' => __( 'Post type to get the templates for.', 'gutenberg' ), + 'type' => 'string', + ), ); } diff --git a/lib/full-site-editing/class-wp-block-template.php b/lib/full-site-editing/class-wp-block-template.php index d8c9d5c54a2b9..176e1105554df 100644 --- a/lib/full-site-editing/class-wp-block-template.php +++ b/lib/full-site-editing/class-wp-block-template.php @@ -87,4 +87,11 @@ class WP_Block_Template { * @var boolean */ public $has_theme_file; + + /** + * Whether a template is a custom template. + * + * @var bool + */ + public $is_custom = true; } diff --git a/lib/full-site-editing/page-templates.php b/lib/full-site-editing/page-templates.php index caac07dc98481..1e2fc63264b1b 100644 --- a/lib/full-site-editing/page-templates.php +++ b/lib/full-site-editing/page-templates.php @@ -8,20 +8,22 @@ /** * Load the page templates in Gutenberg. * - * @param array $templates Page templates. + * @param string[] $templates Page templates. + * @param WP_Theme $theme WP_Theme instance. + * @param WP_Post $post The post being edited, provided for context, or null. + * @param string $post_type Post type to get the templates for. * @return array (Maybe) modified page templates array. */ -function gutenberg_load_block_page_templates( $templates ) { +function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_type ) { if ( ! gutenberg_supports_block_templates() ) { return $templates; } - $block_templates = gutenberg_get_block_templates( array(), 'wp_template' ); + $block_templates = gutenberg_get_block_templates( array( 'post_type' => $post_type ), 'wp_template' ); foreach ( $block_templates as $template ) { - // TODO: exclude templates that are not concerned by the current post type. $templates[ $template->slug ] = $template->title; } return $templates; } -add_filter( 'theme_templates', 'gutenberg_load_block_page_templates' ); +add_filter( 'theme_templates', 'gutenberg_load_block_page_templates', 10, 4 ); diff --git a/packages/edit-post/src/components/sidebar/template/index.js b/packages/edit-post/src/components/sidebar/template/index.js index db4f55403da39..73c046afbaaaf 100644 --- a/packages/edit-post/src/components/sidebar/template/index.js +++ b/packages/edit-post/src/components/sidebar/template/index.js @@ -47,13 +47,15 @@ export function TemplatePanel() { getCurrentPostType, } = select( editorStore ); const { getPostType, getEntityRecords, canUser } = select( coreStore ); - const _isViewable = - getPostType( getCurrentPostType() )?.viewable ?? false; + const currentPostType = getCurrentPostType(); + const _isViewable = getPostType( currentPostType )?.viewable ?? false; const _supportsTemplateMode = select( editorStore ).getEditorSettings().supportsTemplateMode && _isViewable; - const wpTemplates = getEntityRecords( 'postType', 'wp_template' ); + const wpTemplates = getEntityRecords( 'postType', 'wp_template', { + post_type: currentPostType, + } ); const newAvailableTemplates = fromPairs( ( wpTemplates || [] ).map( ( { slug, title } ) => [