Skip to content

Commit 5d41bda

Browse files
REST API: Add template and template_lock to post types endpoint.
Adds template and template_lock property of the post type to post types REST API endpoint. This change allows us to fix a bug where the template of a page is not respected when creating a new page on the site editor. Props jorgefilipecosta, oandregal, timothyblynjacobs, mukesh27. Fixes #61477. git-svn-id: https://develop.svn.wordpress.org/trunk@58452 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 903b2ec commit 5d41bda

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ public function prepare_item_for_response( $item, $request ) {
246246
$data['rest_namespace'] = $namespace;
247247
}
248248

249+
if ( rest_is_field_included( 'template', $fields ) ) {
250+
$data['template'] = $post_type->template ?? array();
251+
}
252+
253+
if ( rest_is_field_included( 'template_lock', $fields ) ) {
254+
$data['template_lock'] = ! empty( $post_type->template_lock ) ? $post_type->template_lock : false;
255+
}
256+
249257
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
250258
$data = $this->add_additional_fields_to_object( $data, $request );
251259
$data = $this->filter_response_by_context( $data, $context );
@@ -407,6 +415,19 @@ public function get_item_schema() {
407415
'context' => array( 'view', 'edit', 'embed' ),
408416
'readonly' => true,
409417
),
418+
'template' => array(
419+
'type' => array( 'array' ),
420+
'description' => __( 'The block template associated with the post type.' ),
421+
'readonly' => true,
422+
'context' => array( 'view', 'edit', 'embed' ),
423+
),
424+
'template_lock' => array(
425+
'type' => array( 'string', 'boolean' ),
426+
'enum' => array( 'all', 'insert', 'contentOnly', false ),
427+
'description' => __( 'The template_lock associated with the post type, or false if none.' ),
428+
'readonly' => true,
429+
'context' => array( 'view', 'edit', 'embed' ),
430+
),
410431
),
411432
);
412433

tests/phpunit/tests/rest-api/rest-post-types-controller.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ public function test_get_item_cpt() {
7777
$this->check_post_type_object_response( 'view', $response, 'cpt' );
7878
}
7979

80+
/**
81+
* @ticket 61477
82+
*/
83+
public function test_get_item_template_cpt() {
84+
register_post_type(
85+
'cpt_template',
86+
array(
87+
'show_in_rest' => true,
88+
'rest_base' => 'cpt_template',
89+
'rest_namespace' => 'wordpress/v1',
90+
'template' => array(
91+
array( 'core/paragraph', array( 'placeholder' => 'Content' ) ),
92+
),
93+
'template_lock' => 'all',
94+
)
95+
);
96+
$request = new WP_REST_Request( 'GET', '/wp/v2/types/cpt_template' );
97+
$response = rest_get_server()->dispatch( $request );
98+
$this->check_post_type_object_response( 'view', $response, 'cpt_template' );
99+
}
100+
80101
public function test_get_item_page() {
81102
$request = new WP_REST_Request( 'GET', '/wp/v2/types/page' );
82103
$response = rest_get_server()->dispatch( $request );
@@ -165,7 +186,7 @@ public function test_get_item_schema() {
165186
$data = $response->get_data();
166187
$properties = $data['schema']['properties'];
167188

168-
$this->assertCount( 14, $properties, 'Schema should have 14 properties' );
189+
$this->assertCount( 16, $properties, 'Schema should have 16 properties' );
169190
$this->assertArrayHasKey( 'capabilities', $properties, '`capabilities` should be included in the schema' );
170191
$this->assertArrayHasKey( 'description', $properties, '`description` should be included in the schema' );
171192
$this->assertArrayHasKey( 'hierarchical', $properties, '`hierarchical` should be included in the schema' );
@@ -180,6 +201,8 @@ public function test_get_item_schema() {
180201
$this->assertArrayHasKey( 'rest_namespace', $properties, '`rest_namespace` should be included in the schema' );
181202
$this->assertArrayHasKey( 'visibility', $properties, '`visibility` should be included in the schema' );
182203
$this->assertArrayHasKey( 'icon', $properties, '`icon` should be included in the schema' );
204+
$this->assertArrayHasKey( 'template', $properties, '`template` should be included in the schema' );
205+
$this->assertArrayHasKey( 'template_lock', $properties, '`template_lock` should be included in the schema' );
183206
}
184207

185208
public function test_get_additional_field_registration() {
@@ -230,6 +253,8 @@ protected function check_post_type_obj( $context, $post_type_obj, $data, $links
230253
$this->assertSame( $post_type_obj->rest_base, $data['rest_base'] );
231254
$this->assertSame( $post_type_obj->rest_namespace, $data['rest_namespace'] );
232255
$this->assertSame( $post_type_obj->has_archive, $data['has_archive'] );
256+
$this->assertSame( $post_type_obj->template ?? array(), $data['template'] );
257+
$this->assertSame( ! empty( $post_type_obj->template_lock ) ? $post_type_obj->template_lock : false, $data['template_lock'] );
233258

234259
$links = test_rest_expand_compact_links( $links );
235260
$this->assertSame( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13014,6 +13014,8 @@ mockedApiResponse.TypesCollection = {
1301413014
],
1301513015
"rest_base": "posts",
1301613016
"rest_namespace": "wp/v2",
13017+
"template": [],
13018+
"template_lock": false,
1301713019
"_links": {
1301813020
"collection": [
1301913021
{
@@ -13044,6 +13046,8 @@ mockedApiResponse.TypesCollection = {
1304413046
"taxonomies": [],
1304513047
"rest_base": "pages",
1304613048
"rest_namespace": "wp/v2",
13049+
"template": [],
13050+
"template_lock": false,
1304713051
"_links": {
1304813052
"collection": [
1304913053
{
@@ -13074,6 +13078,8 @@ mockedApiResponse.TypesCollection = {
1307413078
"taxonomies": [],
1307513079
"rest_base": "media",
1307613080
"rest_namespace": "wp/v2",
13081+
"template": [],
13082+
"template_lock": false,
1307713083
"_links": {
1307813084
"collection": [
1307913085
{
@@ -13106,6 +13112,8 @@ mockedApiResponse.TypesCollection = {
1310613112
],
1310713113
"rest_base": "menu-items",
1310813114
"rest_namespace": "wp/v2",
13115+
"template": [],
13116+
"template_lock": false,
1310913117
"_links": {
1311013118
"collection": [
1311113119
{
@@ -13138,6 +13146,8 @@ mockedApiResponse.TypesCollection = {
1313813146
],
1313913147
"rest_base": "blocks",
1314013148
"rest_namespace": "wp/v2",
13149+
"template": [],
13150+
"template_lock": false,
1314113151
"_links": {
1314213152
"collection": [
1314313153
{
@@ -13168,6 +13178,8 @@ mockedApiResponse.TypesCollection = {
1316813178
"taxonomies": [],
1316913179
"rest_base": "templates",
1317013180
"rest_namespace": "wp/v2",
13181+
"template": [],
13182+
"template_lock": false,
1317113183
"_links": {
1317213184
"collection": [
1317313185
{
@@ -13198,6 +13210,8 @@ mockedApiResponse.TypesCollection = {
1319813210
"taxonomies": [],
1319913211
"rest_base": "template-parts",
1320013212
"rest_namespace": "wp/v2",
13213+
"template": [],
13214+
"template_lock": false,
1320113215
"_links": {
1320213216
"collection": [
1320313217
{
@@ -13228,6 +13242,8 @@ mockedApiResponse.TypesCollection = {
1322813242
"taxonomies": [],
1322913243
"rest_base": "global-styles",
1323013244
"rest_namespace": "wp/v2",
13245+
"template": [],
13246+
"template_lock": false,
1323113247
"_links": {
1323213248
"collection": [
1323313249
{
@@ -13258,6 +13274,8 @@ mockedApiResponse.TypesCollection = {
1325813274
"taxonomies": [],
1325913275
"rest_base": "navigation",
1326013276
"rest_namespace": "wp/v2",
13277+
"template": [],
13278+
"template_lock": false,
1326113279
"_links": {
1326213280
"collection": [
1326313281
{
@@ -13288,6 +13306,8 @@ mockedApiResponse.TypesCollection = {
1328813306
"taxonomies": [],
1328913307
"rest_base": "font-families",
1329013308
"rest_namespace": "wp/v2",
13309+
"template": [],
13310+
"template_lock": false,
1329113311
"_links": {
1329213312
"collection": [
1329313313
{
@@ -13318,6 +13338,8 @@ mockedApiResponse.TypesCollection = {
1331813338
"taxonomies": [],
1331913339
"rest_base": "font-families/(?P<font_family_id>[\\d]+)/font-faces",
1332013340
"rest_namespace": "wp/v2",
13341+
"template": [],
13342+
"template_lock": false,
1332113343
"_links": {
1332213344
"collection": [
1332313345
{
@@ -13352,7 +13374,9 @@ mockedApiResponse.TypeModel = {
1335213374
"post_tag"
1335313375
],
1335413376
"rest_base": "posts",
13355-
"rest_namespace": "wp/v2"
13377+
"rest_namespace": "wp/v2",
13378+
"template": [],
13379+
"template_lock": false
1335613380
};
1335713381

1335813382
mockedApiResponse.StatusesCollection = {

0 commit comments

Comments
 (0)