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

Site Editor: Only request templates and template parts for the current theme #27152

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 12 additions & 1 deletion lib/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} ) || []
);
}, [] );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down