From 59acd9a781f9306d17969789d055fc8aa3d22b1e Mon Sep 17 00:00:00 2001 From: Copons Date: Fri, 20 Nov 2020 15:25:14 +0000 Subject: [PATCH] Only request templates and template parts for the current theme --- lib/templates.php | 13 ++++++++++++- .../navigation-panel/menus/template-parts.js | 10 ++++------ .../navigation-panel/menus/templates.js | 16 ++++++++-------- .../navigation-panel/new-template-dropdown.js | 7 ++++--- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/templates.php b/lib/templates.php index d99d146e9eff4..1b79c5410cabb 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -324,7 +324,7 @@ function filter_rest_wp_template_collection_params( $query_params ) { apply_filters( 'rest_wp_template_collection_params', 'filter_rest_wp_template_collection_params', 99, 1 ); /** - * Filter for supporting the `resolved` parameter in `wp_template` queries. + * Filter for supporting the `resolved` and `theme` parameter in `wp_template` queries. * * @param array $args The query arguments. * @param WP_REST_Request $request The request object. @@ -350,6 +350,17 @@ function filter_rest_wp_template_query( $args, $request ) { $args['post_status'] = array( 'publish', 'auto-draft' ); } + if ( $request['theme'] ) { + $tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array(); + $tax_query[] = array( + 'taxonomy' => 'wp_theme', + 'field' => 'slug', + 'terms' => $request['theme'], + ); + + $args['tax_query'] = $tax_query; + } + return $args; } add_filter( 'rest_wp_template_query', 'filter_rest_wp_template_query', 99, 2 ); diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/template-parts.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/template-parts.js index ad597671aba41..61305d045d041 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/template-parts.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/template-parts.js @@ -21,15 +21,13 @@ import { MENU_ROOT, MENU_TEMPLATE_PARTS } from '../constants'; export default function TemplatePartsMenu() { const templateParts = useSelect( ( select ) => { - const unfilteredTemplateParts = + const theme = select( 'core' ).getCurrentTheme()?.stylesheet; + return ( select( 'core' ).getEntityRecords( 'postType', 'wp_template_part', { status: [ 'publish', 'auto-draft' ], per_page: -1, - } ) || []; - const currentTheme = select( 'core' ).getCurrentTheme()?.stylesheet; - return unfilteredTemplateParts.filter( - ( item ) => - item.status === 'publish' || item.wp_theme_slug === currentTheme + theme, + } ) || [] ); }, [] ); diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js index f1f188b6d7a3b..77a331c847d3c 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js @@ -32,14 +32,14 @@ import NewTemplateDropdown from '../new-template-dropdown'; import TemplateNavigationItem from '../template-navigation-item'; export default function TemplatesMenu() { - const templates = useSelect( - ( select ) => - select( 'core' ).getEntityRecords( 'postType', 'wp_template', { - status: TEMPLATES_STATUSES, - per_page: -1, - } ), - [] - ); + const templates = useSelect( ( select ) => { + const theme = select( 'core' ).getCurrentTheme()?.stylesheet; + return select( 'core' ).getEntityRecords( 'postType', 'wp_template', { + status: TEMPLATES_STATUSES, + per_page: -1, + theme, + } ); + }, [] ); const generalTemplates = templates?.filter( ( { slug } ) => TEMPLATES_GENERAL.includes( slug ) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/new-template-dropdown.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/new-template-dropdown.js index 88903d2d2d40c..151c13161e017 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/new-template-dropdown.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/new-template-dropdown.js @@ -24,17 +24,18 @@ import { TEMPLATES_STATUSES } from './constants'; export default function NewTemplateDropdown() { const { defaultTemplateTypes, templates } = useSelect( ( select ) => { + const theme = select( 'core' ).getCurrentTheme()?.stylesheet; const { __experimentalGetDefaultTemplateTypes: getDefaultTemplateTypes, } = select( 'core/editor' ); - const templateEntities = select( 'core' ).getEntityRecords( + const _templates = select( 'core' ).getEntityRecords( 'postType', 'wp_template', - { status: TEMPLATES_STATUSES, per_page: -1 } + { status: TEMPLATES_STATUSES, per_page: -1, theme } ); return { defaultTemplateTypes: getDefaultTemplateTypes(), - templates: templateEntities, + templates: _templates, }; }, [] ); const { addTemplate } = useDispatch( 'core/edit-site' );