Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates: Only include templates for the current post types #35802

Merged
merged 5 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand All @@ -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;
Expand All @@ -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' );
Expand All @@ -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.
*
Expand All @@ -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.
*/
Expand All @@ -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,
Expand Down Expand Up @@ -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' ),
Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public function get_items( $request ) {
if ( isset( $request['area'] ) ) {
$query['area'] = $request['area'];
}
if ( isset( $request['post_type'] ) ) {
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
$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 );
Expand Down Expand Up @@ -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',
),
);
}

Expand Down
7 changes: 7 additions & 0 deletions lib/full-site-editing/class-wp-block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 7 additions & 5 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change hasn't been backported properly into Core yet.

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 );
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