Skip to content

Commit

Permalink
Fix: Post_type template is not used when creating a page in site edit…
Browse files Browse the repository at this point in the history
…or. (#62488)

Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: colorful-tones <colorful-tones@git.wordpress.org>
Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org>
Co-authored-by: bacoords <bacoords@git.wordpress.org>
  • Loading branch information
7 people authored Jun 20, 2024
1 parent 35db76a commit a054c20
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
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' ),
'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' );
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

0 comments on commit a054c20

Please sign in to comment.