Make WordPress Core

Changeset 58452


Ignore:
Timestamp:
06/21/2024 01:04:47 PM (10 months ago)
Author:
jorgefilipecosta
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    r56586 r58452  
    245245        if ( rest_is_field_included( 'rest_namespace', $fields ) ) {
    246246            $data['rest_namespace'] = $namespace;
     247        }
     248
     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;
    247255        }
    248256
     
    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        );
  • trunk/tests/phpunit/tests/rest-api/rest-post-types-controller.php

    r56746 r58452  
    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' );
     
    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' );
     
    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
     
    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 );
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r58340 r58452  
    1301513015        "rest_base": "posts",
    1301613016        "rest_namespace": "wp/v2",
     13017        "template": [],
     13018        "template_lock": false,
    1301713019        "_links": {
    1301813020            "collection": [
     
    1304513047        "rest_base": "pages",
    1304613048        "rest_namespace": "wp/v2",
     13049        "template": [],
     13050        "template_lock": false,
    1304713051        "_links": {
    1304813052            "collection": [
     
    1307513079        "rest_base": "media",
    1307613080        "rest_namespace": "wp/v2",
     13081        "template": [],
     13082        "template_lock": false,
    1307713083        "_links": {
    1307813084            "collection": [
     
    1310713113        "rest_base": "menu-items",
    1310813114        "rest_namespace": "wp/v2",
     13115        "template": [],
     13116        "template_lock": false,
    1310913117        "_links": {
    1311013118            "collection": [
     
    1313913147        "rest_base": "blocks",
    1314013148        "rest_namespace": "wp/v2",
     13149        "template": [],
     13150        "template_lock": false,
    1314113151        "_links": {
    1314213152            "collection": [
     
    1316913179        "rest_base": "templates",
    1317013180        "rest_namespace": "wp/v2",
     13181        "template": [],
     13182        "template_lock": false,
    1317113183        "_links": {
    1317213184            "collection": [
     
    1319913211        "rest_base": "template-parts",
    1320013212        "rest_namespace": "wp/v2",
     13213        "template": [],
     13214        "template_lock": false,
    1320113215        "_links": {
    1320213216            "collection": [
     
    1322913243        "rest_base": "global-styles",
    1323013244        "rest_namespace": "wp/v2",
     13245        "template": [],
     13246        "template_lock": false,
    1323113247        "_links": {
    1323213248            "collection": [
     
    1325913275        "rest_base": "navigation",
    1326013276        "rest_namespace": "wp/v2",
     13277        "template": [],
     13278        "template_lock": false,
    1326113279        "_links": {
    1326213280            "collection": [
     
    1328913307        "rest_base": "font-families",
    1329013308        "rest_namespace": "wp/v2",
     13309        "template": [],
     13310        "template_lock": false,
    1329113311        "_links": {
    1329213312            "collection": [
     
    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": [
     
    1335313375    ],
    1335413376    "rest_base": "posts",
    13355     "rest_namespace": "wp/v2"
     13377    "rest_namespace": "wp/v2",
     13378    "template": [],
     13379    "template_lock": false
    1335613380};
    1335713381
Note: See TracChangeset for help on using the changeset viewer.