Skip to content

Commit

Permalink
Templates: Only include templates for the current post types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Oct 20, 2021
1 parent f7b26ab commit 174829c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t
$template->title = $default_template_types[ $template_file['slug'] ]['title'];
}

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'] ) ) {
$template->area = $template_file['area'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,17 @@ public function get_items( $request ) {
if ( isset( $request['area'] ) ) {
$query['area'] = $request['area'];
}

$templates = array();
foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) {
if (
isset( $request['post_type'] ) &&
isset( $template->post_types ) &&
! in_array( $request['post_type'], $template->post_types, true )
) {
continue;
}

$data = $this->prepare_item_for_response( $template, $request );
$templates[] = $this->prepare_response_for_collection( $data );
}
Expand Down
14 changes: 10 additions & 4 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
/**
* 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' );
foreach ( $block_templates as $template ) {
// TODO: exclude templates that are not concerned by the current post type.
if ( isset( $template->post_types ) && ! in_array( $post_type, $template->post_types, true ) ) {
continue;
}

$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 );
8 changes: 5 additions & 3 deletions packages/edit-post/src/components/sidebar/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } ) => [
Expand Down

0 comments on commit 174829c

Please sign in to comment.