Skip to content

Commit ecc13df

Browse files
ellatrixgutenbergplugin
authored andcommitted
Template activation: remove fake post type for registered templates (#72674)
1 parent f065954 commit ecc13df

File tree

15 files changed

+39
-78
lines changed

15 files changed

+39
-78
lines changed

backport-changelog/6.9/8063.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ https://github.com/WordPress/wordpress-develop/pull/8063
1111
* https://github.com/WordPress/gutenberg/pull/72141
1212
* https://github.com/WordPress/gutenberg/pull/72223
1313
* https://github.com/WordPress/gutenberg/pull/72285
14+
* https://github.com/WordPress/gutenberg/pull/72674

lib/compat/wordpress-6.9/class-gutenberg-rest-static-templates-controller.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
class Gutenberg_REST_Static_Templates_Controller extends WP_REST_Templates_Controller {
4+
public function __construct() {
5+
$this->rest_base = 'wp_registered_template';
6+
$this->namespace = 'wp/v2';
7+
}
8+
49
public function register_routes() {
510
// Lists all templates.
611
register_rest_route(
@@ -81,9 +86,8 @@ public function get_items( $request ) {
8186
$query_result = gutenberg_get_registered_block_templates( $query );
8287
$templates = array();
8388
foreach ( $query_result as $template ) {
84-
$item = $this->prepare_item_for_response( $template, $request );
85-
$item->data['type'] = 'wp_registered_template';
86-
$templates[] = $this->prepare_response_for_collection( $item );
89+
$item = $this->prepare_item_for_response( $template, $request );
90+
$templates[] = $this->prepare_response_for_collection( $item );
8791
}
8892

8993
return rest_ensure_response( $templates );
@@ -97,8 +101,6 @@ public function get_item( $request ) {
97101
}
98102

99103
$item = $this->prepare_item_for_response( $template, $request );
100-
// adjust the template type here instead
101-
$item->data['type'] = 'wp_registered_template';
102104
return rest_ensure_response( $item );
103105
}
104106
}

lib/compat/wordpress-6.9/preload.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ static function ( $path ) {
2121
}
2222
);
2323
}
24-
25-
$paths[] = '/wp/v2/wp_registered_template?context=edit';
2624
}
2725

2826
return $paths;

lib/compat/wordpress-6.9/template-activate.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ function gutenberg_maintain_templates_routes() {
3535
$wp_post_types['wp_template']->rest_base = 'wp_template';
3636
$controller->register_routes();
3737

38+
$registered_template_controller = new Gutenberg_REST_Static_Templates_Controller();
39+
$registered_template_controller->register_routes();
40+
3841
// Add the same field as wp_registered_template.
3942
register_rest_field(
4043
'wp_template',
@@ -79,12 +82,6 @@ function gutenberg_maintain_templates_routes() {
7982
* @global array $wp_post_types List of post types.
8083
*/
8184
function gutenberg_setup_static_template() {
82-
global $wp_post_types;
83-
$wp_post_types['wp_registered_template'] = clone $wp_post_types['wp_template'];
84-
$wp_post_types['wp_registered_template']->name = 'wp_registered_template';
85-
$wp_post_types['wp_registered_template']->rest_base = 'wp_registered_template';
86-
$wp_post_types['wp_registered_template']->rest_controller_class = 'Gutenberg_REST_Static_Templates_Controller';
87-
8885
register_setting(
8986
'reading',
9087
'active_templates',

packages/core-data/src/entities.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ export const rootEntitiesConfig = [
202202
plural: 'statuses',
203203
key: 'slug',
204204
},
205+
{
206+
label: __( 'Registered Templates' ),
207+
name: 'registeredTemplate',
208+
kind: 'root',
209+
baseURL: '/wp/v2/wp_registered_template',
210+
key: 'id',
211+
},
205212
];
206213

207214
export const deprecatedEntities = {

packages/core-data/src/private-selectors.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -240,38 +240,13 @@ export const getTemplateId = createRegistrySelector(
240240
// First see if the post/page has an assigned template and fetch it.
241241
const currentTemplateSlug = editedEntity.template;
242242
if ( currentTemplateSlug ) {
243-
const userTemplates = select( STORE_NAME ).getEntityRecords(
244-
'postType',
245-
'wp_template',
246-
{ per_page: -1 }
247-
);
248-
if ( ! userTemplates ) {
249-
return;
250-
}
251-
const userTemplateWithSlug = userTemplates.find(
252-
( { slug } ) => slug === currentTemplateSlug
253-
);
254-
255-
if ( userTemplateWithSlug ) {
256-
return userTemplateWithSlug.id;
257-
}
258-
259-
const registeredTemplates = select( STORE_NAME ).getEntityRecords(
260-
'postType',
261-
'wp_registered_template',
262-
{ per_page: -1 }
263-
);
264-
265-
if ( ! registeredTemplates ) {
266-
return;
267-
}
268-
269-
const registeredTemplateWithSlug = registeredTemplates.find(
270-
( { slug } ) => slug === currentTemplateSlug
271-
);
272-
273-
if ( registeredTemplateWithSlug ) {
274-
return registeredTemplateWithSlug.id;
243+
const currentTemplate = select( STORE_NAME )
244+
.getEntityRecords( 'postType', 'wp_template', {
245+
per_page: -1,
246+
} )
247+
?.find( ( { slug } ) => slug === currentTemplateSlug );
248+
if ( currentTemplate ) {
249+
return currentTemplate.id;
275250
}
276251
}
277252
// If no template is assigned, use the default template.

packages/core-data/src/resolvers.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export const getEntityRecords =
329329
// the registered templates and rewrites IDs in the form of
330330
// `theme-slug/template-slug`. When turned off, we only fetch
331331
// database templates (posts). To fetch registered templates without
332-
// edits applied, use the `wp_registered_template` entity.
332+
// edits applied, use the `registeredTemplate` entity.
333333
const { combinedTemplates = true } = query;
334334

335335
if (
@@ -899,10 +899,6 @@ export const getDefaultTemplateId =
899899
// Endpoint may return an empty object if no template is found.
900900
if ( id ) {
901901
template.id = id;
902-
template.type =
903-
typeof id === 'string'
904-
? 'wp_registered_template'
905-
: 'wp_template';
906902
registry.batch( () => {
907903
dispatch.receiveDefaultTemplateId( query, id );
908904
dispatch.receiveEntityRecords( 'postType', template.type, [

packages/edit-post/src/components/layout/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ const DESIGN_POST_TYPES = [
8484
'wp_template_part',
8585
'wp_block',
8686
'wp_navigation',
87-
'wp_registered_template',
8887
];
8988

9089
function useEditorStyles( ...additionalStyles ) {

packages/edit-site/src/components/page-templates/fields.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ const { useEntityRecordsWithPermissions } = unlock( corePrivateApis );
4141
function useAllDefaultTemplateTypes() {
4242
const defaultTemplateTypes = useDefaultTemplateTypes();
4343
const { records: staticRecords } = useEntityRecordsWithPermissions(
44-
'postType',
45-
'wp_registered_template',
46-
{ per_page: -1 }
44+
'root',
45+
'registeredTemplate'
4746
);
4847
return [
4948
...defaultTemplateTypes,

packages/edit-site/src/components/page-templates/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ export default function PageTemplates() {
9393
combinedTemplates: false,
9494
} );
9595
const { records: staticRecords, isResolving: isLoadingStaticData } =
96-
useEntityRecordsWithPermissions( 'postType', 'wp_registered_template', {
97-
per_page: -1,
98-
} );
96+
useEntityRecordsWithPermissions( 'root', 'registeredTemplate' );
9997

10098
const activeTemplates = useMemo( () => {
10199
const _active = [ ...staticRecords ].filter(
@@ -325,7 +323,7 @@ export default function PageTemplates() {
325323
onChangeSelection={ onChangeSelection }
326324
isItemClickable={ () => true }
327325
onClickItem={ ( item ) => {
328-
if ( item.type === 'wp_registered_template' ) {
326+
if ( typeof item.id === 'string' ) {
329327
setSelectedRegisteredTemplate( item );
330328
} else {
331329
history.navigate(

0 commit comments

Comments
 (0)