From ec54d62916c25da190a694347f07d89daa571921 Mon Sep 17 00:00:00 2001 From: Jorge Date: Tue, 20 Sep 2022 16:39:34 +0100 Subject: [PATCH] Add: has_archive to post types rest endpoint. --- .../endpoints/class-wp-rest-post-types-controller.php | 10 ++++++++++ .../tests/rest-api/rest-post-types-controller.php | 4 +++- tests/qunit/fixtures/wp-api-generated.js | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php index cb2daa4b292c6..993584997901f 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php @@ -198,6 +198,10 @@ public function prepare_item_for_response( $item, $request ) { $data['hierarchical'] = $post_type->hierarchical; } + if ( in_array( 'has_archive', $fields, true ) ) { + $data['has_archive'] = $post_type->has_archive; + } + if ( in_array( 'visibility', $fields, true ) ) { $data['visibility'] = array( 'show_in_nav_menus' => (bool) $post_type->show_in_nav_menus, @@ -348,6 +352,12 @@ public function get_item_schema() { 'context' => array( 'edit' ), 'readonly' => true, ), + 'has_archive' => array( + 'description' => __( 'If the value is a string, the value will be used as the archive slug. If the value is false the post type has no archive.' ), + 'type' => array( 'string', 'boolean' ), + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), 'taxonomies' => array( 'description' => __( 'Taxonomies associated with post type.' ), 'type' => 'array', diff --git a/tests/phpunit/tests/rest-api/rest-post-types-controller.php b/tests/phpunit/tests/rest-api/rest-post-types-controller.php index 0ab2a7b937005..3008ca87eedc8 100644 --- a/tests/phpunit/tests/rest-api/rest-post-types-controller.php +++ b/tests/phpunit/tests/rest-api/rest-post-types-controller.php @@ -161,7 +161,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 12, $properties ); + $this->assertCount( 13, $properties ); $this->assertArrayHasKey( 'capabilities', $properties ); $this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'hierarchical', $properties ); @@ -170,6 +170,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'slug', $properties ); $this->assertArrayHasKey( 'supports', $properties ); + $this->assertArrayHasKey( 'has_archive', $properties ); $this->assertArrayHasKey( 'taxonomies', $properties ); $this->assertArrayHasKey( 'rest_base', $properties ); $this->assertArrayHasKey( 'rest_namespace', $properties ); @@ -223,6 +224,7 @@ protected function check_post_type_obj( $context, $post_type_obj, $data, $links $this->assertSame( $post_type_obj->hierarchical, $data['hierarchical'] ); $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'] ); $links = test_rest_expand_compact_links( $links ); $this->assertSame( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] ); diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 4020b25da844d..1a8c4a0e33e42 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -11589,6 +11589,7 @@ mockedApiResponse.TypesCollection = { "post": { "description": "", "hierarchical": false, + "has_archive": false, "name": "Posts", "slug": "post", "taxonomies": [ @@ -11620,6 +11621,7 @@ mockedApiResponse.TypesCollection = { "page": { "description": "", "hierarchical": true, + "has_archive": false, "name": "Pages", "slug": "page", "taxonomies": [], @@ -11648,6 +11650,7 @@ mockedApiResponse.TypesCollection = { "attachment": { "description": "", "hierarchical": false, + "has_archive": false, "name": "Media", "slug": "attachment", "taxonomies": [], @@ -11676,6 +11679,7 @@ mockedApiResponse.TypesCollection = { "nav_menu_item": { "description": "", "hierarchical": false, + "has_archive": false, "name": "Navigation Menu Items", "slug": "nav_menu_item", "taxonomies": [ @@ -11706,6 +11710,7 @@ mockedApiResponse.TypesCollection = { "wp_block": { "description": "", "hierarchical": false, + "has_archive": false, "name": "Reusable blocks", "slug": "wp_block", "taxonomies": [], @@ -11734,6 +11739,7 @@ mockedApiResponse.TypesCollection = { "wp_template": { "description": "Templates to include in your theme.", "hierarchical": false, + "has_archive": false, "name": "Templates", "slug": "wp_template", "taxonomies": [], @@ -11762,6 +11768,7 @@ mockedApiResponse.TypesCollection = { "wp_template_part": { "description": "Template parts to include in your templates.", "hierarchical": false, + "has_archive": false, "name": "Template Parts", "slug": "wp_template_part", "taxonomies": [], @@ -11790,6 +11797,7 @@ mockedApiResponse.TypesCollection = { "wp_navigation": { "description": "Navigation menus that can be inserted into your site.", "hierarchical": false, + "has_archive": false, "name": "Navigation Menus", "slug": "wp_navigation", "taxonomies": [], @@ -11820,6 +11828,7 @@ mockedApiResponse.TypesCollection = { mockedApiResponse.TypeModel = { "description": "", "hierarchical": false, + "has_archive": false, "name": "Posts", "slug": "post", "taxonomies": [