Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

class Gutenberg_REST_Static_Templates_Controller extends WP_REST_Templates_Controller {
public function __construct() {
$this->rest_base = 'wp_registered_template';
$this->namespace = 'wp/v2';
}

public function register_routes() {
// Lists all templates.
register_rest_route(
Expand Down Expand Up @@ -81,9 +86,8 @@
$query_result = gutenberg_get_registered_block_templates( $query );
$templates = array();
foreach ( $query_result as $template ) {
$item = $this->prepare_item_for_response( $template, $request );

Check warning on line 89 in lib/compat/wordpress-6.9/class-gutenberg-rest-static-templates-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 8 spaces but found 15 spaces
$item->data['type'] = 'wp_registered_template';
$templates[] = $this->prepare_response_for_collection( $item );

Check warning on line 90 in lib/compat/wordpress-6.9/class-gutenberg-rest-static-templates-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 1 space but found 8 spaces
}

return rest_ensure_response( $templates );
Expand All @@ -97,8 +101,6 @@
}

$item = $this->prepare_item_for_response( $template, $request );
// adjust the template type here instead
$item->data['type'] = 'wp_registered_template';
return rest_ensure_response( $item );
}
}
2 changes: 0 additions & 2 deletions lib/compat/wordpress-6.9/preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ static function ( $path ) {
}
);
}

$paths[] = '/wp/v2/wp_registered_template?context=edit';
}

return $paths;
Expand Down
9 changes: 3 additions & 6 deletions lib/compat/wordpress-6.9/template-activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function gutenberg_maintain_templates_routes() {
$wp_post_types['wp_template']->rest_base = 'wp_template';
$controller->register_routes();

$registered_template_controller = new Gutenberg_REST_Static_Templates_Controller();
$registered_template_controller->register_routes();

// Add the same field as wp_registered_template.
register_rest_field(
'wp_template',
Expand Down Expand Up @@ -79,12 +82,6 @@ function gutenberg_maintain_templates_routes() {
* @global array $wp_post_types List of post types.
*/
function gutenberg_setup_static_template() {
global $wp_post_types;
$wp_post_types['wp_registered_template'] = clone $wp_post_types['wp_template'];
$wp_post_types['wp_registered_template']->name = 'wp_registered_template';
$wp_post_types['wp_registered_template']->rest_base = 'wp_registered_template';
$wp_post_types['wp_registered_template']->rest_controller_class = 'Gutenberg_REST_Static_Templates_Controller';

register_setting(
'reading',
'active_templates',
Expand Down
9 changes: 9 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ export const rootEntitiesConfig = [
plural: 'statuses',
key: 'slug',
},
{
label: __( 'Registered Templates' ),
name: 'registeredTemplate',
kind: 'root',
baseURL: '/wp/v2/wp_registered_template',
baseURLParams: { context: 'edit' },
plural: 'registeredTemplates',
key: 'id',
},
];

export const deprecatedEntities = {
Expand Down
39 changes: 7 additions & 32 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,38 +240,13 @@ export const getTemplateId = createRegistrySelector(
// First see if the post/page has an assigned template and fetch it.
const currentTemplateSlug = editedEntity.template;
if ( currentTemplateSlug ) {
const userTemplates = select( STORE_NAME ).getEntityRecords(
'postType',
'wp_template',
{ per_page: -1 }
);
if ( ! userTemplates ) {
return;
}
const userTemplateWithSlug = userTemplates.find(
( { slug } ) => slug === currentTemplateSlug
);

if ( userTemplateWithSlug ) {
return userTemplateWithSlug.id;
}

const registeredTemplates = select( STORE_NAME ).getEntityRecords(
'postType',
'wp_registered_template',
{ per_page: -1 }
);

if ( ! registeredTemplates ) {
return;
}

const registeredTemplateWithSlug = registeredTemplates.find(
( { slug } ) => slug === currentTemplateSlug
);

if ( registeredTemplateWithSlug ) {
return registeredTemplateWithSlug.id;
const currentTemplate = select( STORE_NAME )
.getEntityRecords( 'postType', 'wp_template', {
per_page: -1,
} )
?.find( ( { slug } ) => slug === currentTemplateSlug );
if ( currentTemplate ) {
return currentTemplate.id;
Copy link
Member Author

Choose a reason for hiding this comment

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

This should have been done in #72661 because it specifically handles the template property on a post object.

}
}
// If no template is assigned, use the default template.
Expand Down
6 changes: 1 addition & 5 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export const getEntityRecords =
// the registered templates and rewrites IDs in the form of
// `theme-slug/template-slug`. When turned off, we only fetch
// database templates (posts). To fetch registered templates without
// edits applied, use the `wp_registered_template` entity.
// edits applied, use the `registeredTemplate` entity.
const { combinedTemplates = true } = query;

if (
Expand Down Expand Up @@ -899,10 +899,6 @@ export const getDefaultTemplateId =
// Endpoint may return an empty object if no template is found.
if ( id ) {
template.id = id;
template.type =
typeof id === 'string'
? 'wp_registered_template'
: 'wp_template';
registry.batch( () => {
dispatch.receiveDefaultTemplateId( query, id );
dispatch.receiveEntityRecords( 'postType', template.type, [
Expand Down
1 change: 0 additions & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const DESIGN_POST_TYPES = [
'wp_template_part',
'wp_block',
'wp_navigation',
'wp_registered_template',
];

function useEditorStyles( ...additionalStyles ) {
Expand Down
5 changes: 2 additions & 3 deletions packages/edit-site/src/components/page-templates/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ const { useEntityRecordsWithPermissions } = unlock( corePrivateApis );
function useAllDefaultTemplateTypes() {
const defaultTemplateTypes = useDefaultTemplateTypes();
const { records: staticRecords } = useEntityRecordsWithPermissions(
'postType',
'wp_registered_template',
{ per_page: -1 }
'root',
'registeredTemplate'
);
return [
...defaultTemplateTypes,
Expand Down
6 changes: 2 additions & 4 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ export default function PageTemplates() {
combinedTemplates: false,
} );
const { records: staticRecords, isResolving: isLoadingStaticData } =
useEntityRecordsWithPermissions( 'postType', 'wp_registered_template', {
per_page: -1,
} );
useEntityRecordsWithPermissions( 'root', 'registeredTemplate' );

const activeTemplates = useMemo( () => {
const _active = [ ...staticRecords ].filter(
Expand Down Expand Up @@ -325,7 +323,7 @@ export default function PageTemplates() {
onChangeSelection={ onChangeSelection }
isItemClickable={ () => true }
onClickItem={ ( item ) => {
if ( item.type === 'wp_registered_template' ) {
if ( typeof item.id === 'string' ) {
setSelectedRegisteredTemplate( item );
} else {
history.navigate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ export default function DataviewsTemplatesSidebarContent() {
const {
query: { activeView = 'active' },
} = useLocation();
const { records } = useEntityRecords(
'postType',
'wp_registered_template',
{
per_page: -1,
}
);
const { records } = useEntityRecords( 'root', 'registeredTemplate' );
const firstItemPerAuthorText = useMemo( () => {
const firstItemPerAuthor = records?.reduce( ( acc, template ) => {
const author = template.author_text;
Expand Down
5 changes: 1 addition & 4 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
const defaultBlockContext = useMemo( () => {
const postContext = {};
// If it is a template, try to inherit the post type from the name.
if (
post.type === 'wp_template' ||
post.type === 'wp_registered_template'
) {
if ( post.type === 'wp_template' ) {
if ( post.slug === 'page' ) {
postContext.postType = 'page';
} else if ( post.slug === 'single' ) {
Expand Down
5 changes: 4 additions & 1 deletion packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,10 @@ export function updateEditorSettings( settings ) {
export const setRenderingMode =
( mode ) =>
( { dispatch, registry, select } ) => {
if ( select.__unstableIsEditorReady() ) {
if (
select.__unstableIsEditorReady() &&
! select.getEditorSettings().isPreviewMode
Copy link
Contributor

Choose a reason for hiding this comment

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

@ellatrix: based on

There's a little fix we have to do in setRenderingMode: since these templates are not posts and not editable, we shouldn't call editPost because that will error (tries to get a post type entity). The fix is to check if the editor is in preview mode.

Wouldn't that mean that it's worth still calling clearSelectedBlock?

) {
// We clear the block selection but we also need to clear the selection from the core store.
registry.dispatch( blockEditorStore ).clearSelectedBlock();
dispatch.editPost( { selection: undefined }, { undoIgnore: true } );
Expand Down
10 changes: 3 additions & 7 deletions packages/fields/src/actions/duplicate-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ const duplicatePost: Action< BasePost > = {
return;
}

const isTemplate =
item.type === 'wp_template' ||
item.type === 'wp_registered_template';
const isTemplate = item.type === 'wp_template';

const newItemObject = {
status: isTemplate ? 'publish' : 'draft',
Expand Down Expand Up @@ -108,9 +106,7 @@ const duplicatePost: Action< BasePost > = {
try {
const newItem = await saveEntityRecord(
'postType',
item.type === 'wp_registered_template'
? 'wp_template'
: item.type,
item.type,
newItemObject,
{ throwOnError: true }
);
Expand Down Expand Up @@ -149,7 +145,7 @@ const duplicatePost: Action< BasePost > = {
return (
<form onSubmit={ createPage }>
<VStack spacing={ 3 }>
{ item.type === 'wp_registered_template' && (
{ typeof item.id === 'string' && (
<div>
{ __(
'You are about to duplicate a bundled template. Changes will not be live until you activate the new template.'
Expand Down
Loading