Make WordPress Core


Ignore:
Timestamp:
06/21/2024 01:04:47 PM (11 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 );
Note: See TracChangeset for help on using the changeset viewer.