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

Add: Template and template_lock to post types endpoint. #6865

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ public function prepare_item_for_response( $item, $request ) {
$data['rest_namespace'] = $namespace;
}

if ( rest_is_field_included( 'template', $fields ) ) {
$data['template'] = $post_type->template ?? array();
}

if ( rest_is_field_included( 'template_lock', $fields ) ) {
$data['template_lock'] = ! empty( $post_type->template_lock ) ? $post_type->template_lock : false;
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down Expand Up @@ -407,6 +415,19 @@ public function get_item_schema() {
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'template' => array(
'type' => array( 'array' ),
'description' => __( 'The block template associated with the post type.' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'template_lock' => array(
'type' => array( 'string', 'boolean' ),
'enum' => array( 'all', 'insert', 'contentOnly', false ),
'description' => __( 'The template_lock associated with the post type, or false if none.' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
),
);

Expand Down
27 changes: 26 additions & 1 deletion tests/phpunit/tests/rest-api/rest-post-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ public function test_get_item_cpt() {
$this->check_post_type_object_response( 'view', $response, 'cpt' );
}

/**
* @ticket 61477
*/
public function test_get_item_template_cpt() {
Copy link
Member

Choose a reason for hiding this comment

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

@ticket annotation is missing.

register_post_type(
'cpt_template',
array(
'show_in_rest' => true,
'rest_base' => 'cpt_template',
'rest_namespace' => 'wordpress/v1',
'template' => array(
array( 'core/paragraph', array( 'placeholder' => 'Content' ) ),
),
'template_lock' => 'all',
)
);
$request = new WP_REST_Request( 'GET', '/wp/v2/types/cpt_template' );
$response = rest_get_server()->dispatch( $request );
$this->check_post_type_object_response( 'view', $response, 'cpt_template' );
}

public function test_get_item_page() {
$request = new WP_REST_Request( 'GET', '/wp/v2/types/page' );
$response = rest_get_server()->dispatch( $request );
Expand Down Expand Up @@ -165,7 +186,7 @@ public function test_get_item_schema() {
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertCount( 14, $properties, 'Schema should have 14 properties' );
$this->assertCount( 16, $properties, 'Schema should have 16 properties' );
$this->assertArrayHasKey( 'capabilities', $properties, '`capabilities` should be included in the schema' );
$this->assertArrayHasKey( 'description', $properties, '`description` should be included in the schema' );
$this->assertArrayHasKey( 'hierarchical', $properties, '`hierarchical` should be included in the schema' );
Expand All @@ -180,6 +201,8 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'rest_namespace', $properties, '`rest_namespace` should be included in the schema' );
$this->assertArrayHasKey( 'visibility', $properties, '`visibility` should be included in the schema' );
$this->assertArrayHasKey( 'icon', $properties, '`icon` should be included in the schema' );
$this->assertArrayHasKey( 'template', $properties, '`template` should be included in the schema' );
$this->assertArrayHasKey( 'template_lock', $properties, '`template_lock` should be included in the schema' );
}

public function test_get_additional_field_registration() {
Expand Down Expand Up @@ -230,6 +253,8 @@ protected function check_post_type_obj( $context, $post_type_obj, $data, $links
$this->assertSame( $post_type_obj->rest_base, $data['rest_base'] );
$this->assertSame( $post_type_obj->rest_namespace, $data['rest_namespace'] );
$this->assertSame( $post_type_obj->has_archive, $data['has_archive'] );
$this->assertSame( $post_type_obj->template ?? array(), $data['template'] );
$this->assertSame( ! empty( $post_type_obj->template_lock ) ? $post_type_obj->template_lock : false, $data['template_lock'] );

$links = test_rest_expand_compact_links( $links );
$this->assertSame( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );
Expand Down
26 changes: 25 additions & 1 deletion tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -13014,6 +13014,8 @@ mockedApiResponse.TypesCollection = {
],
"rest_base": "posts",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13044,6 +13046,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "pages",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13074,6 +13078,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "media",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13106,6 +13112,8 @@ mockedApiResponse.TypesCollection = {
],
"rest_base": "menu-items",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13138,6 +13146,8 @@ mockedApiResponse.TypesCollection = {
],
"rest_base": "blocks",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13168,6 +13178,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "templates",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13198,6 +13210,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "template-parts",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13228,6 +13242,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "global-styles",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13258,6 +13274,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "navigation",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13288,6 +13306,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "font-families",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13318,6 +13338,8 @@ mockedApiResponse.TypesCollection = {
"taxonomies": [],
"rest_base": "font-families/(?P<font_family_id>[\\d]+)/font-faces",
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false,
"_links": {
"collection": [
{
Expand Down Expand Up @@ -13352,7 +13374,9 @@ mockedApiResponse.TypeModel = {
"post_tag"
],
"rest_base": "posts",
"rest_namespace": "wp/v2"
"rest_namespace": "wp/v2",
"template": [],
"template_lock": false
};

mockedApiResponse.StatusesCollection = {
Expand Down
Loading