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

Fix: Post_type template is not used when creating a page in site editor. #62488

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
46 changes: 46 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,49 @@ function gutenberg_register_edit_site_export_controller_endpoints() {
}

add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' );

if ( ! function_exists( 'gutenberg_register_wp_rest_post_types_controller_fields' ) ) {
/**
* Adds `template` and `template_lock` fields to WP_REST_Post_Types_Controller class.
*/
function gutenberg_register_wp_rest_post_types_controller_fields() {
register_rest_field(
'type',
'template',
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) && ! empty( $post_type->template ) ) {
return $post_type->template;
}
},
'schema' => array(
'type' => 'array',
'description' => __( 'The template associated with the post type.', 'gutenberg' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the phrasing reflect that the template is optional and refers to block templates as opposed to a wp_template? For example:

-The template associated with the post type.
+The block template associated with the post type, if it exists.

'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
register_rest_field(
'type',
'template_lock',
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) && ! empty( $post_type->template_lock ) && false !== $post_type->template_lock ) {
return $post_type->template_lock;
}
},
'schema' => array(
'type' => 'string',
'enum' => array( 'all', 'insert', 'contentOnly' ),
'description' => __( 'The template_lock specified for the post type.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_post_types_controller_fields' );
Copy link
Member

Choose a reason for hiding this comment

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

Should we be worried about changes to the REST API this late in the release cycle? This PR will need a backport PR asap.

Copy link
Member Author

Choose a reason for hiding this comment

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

We are only adding two fields that will for sure be required, so I don't think we have back-compatibility problems or unexpected issues. I'm preparing a backport PR, but I don't have a strong option. we can opt for not include this fix in WP 6.6 if you think it is safer.

14 changes: 13 additions & 1 deletion packages/edit-site/src/components/add-new-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
TextControl,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useRegistry } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { decodeEntities } from '@wordpress/html-entities';
import { serialize, synchronizeBlocksWithTemplate } from '@wordpress/blocks';

export default function AddNewPageModal( { onSave, onClose } ) {
const [ isCreatingPage, setIsCreatingPage ] = useState( false );
Expand All @@ -22,6 +23,7 @@ export default function AddNewPageModal( { onSave, onClose } ) {
const { saveEntityRecord } = useDispatch( coreStore );
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );
const { resolveSelect } = useRegistry();

async function createPage( event ) {
event.preventDefault();
Expand All @@ -31,13 +33,23 @@ export default function AddNewPageModal( { onSave, onClose } ) {
}
setIsCreatingPage( true );
try {
const pagePostType =
await resolveSelect( coreStore ).getPostType( 'page' );
const newPage = await saveEntityRecord(
'postType',
'page',
{
status: 'draft',
title,
slug: title || __( 'No title' ),
content: !! pagePostType.template
? serialize(
synchronizeBlocksWithTemplate(
[],
pagePostType.template
)
)
: undefined,
},
{ throwOnError: true }
);
Expand Down
Loading