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: has_archive to post types rest endpoint. #3294

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 @@ -198,6 +198,10 @@ public function prepare_item_for_response( $item, $request ) {
$data['hierarchical'] = $post_type->hierarchical;
}

if ( rest_is_field_included( 'hierarchical', $fields ) ) {
$data['has_archive'] = $post_type->has_archive;
}

if ( rest_is_field_included( 'visibility', $fields ) ) {
$data['visibility'] = array(
'show_in_nav_menus' => (bool) $post_type->show_in_nav_menus,
Expand Down Expand Up @@ -353,6 +357,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',
Expand Down
4 changes: 3 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 @@ -167,7 +167,7 @@ public function test_get_item_schema() {
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertCount( 13, $properties, 'Schema should have 13 properties' );
$this->assertCount( 14, $properties, 'Schema should have 14 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 @@ -176,6 +176,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'name', $properties, '`name` should be included in the schema' );
$this->assertArrayHasKey( 'slug', $properties, '`slug` should be included in the schema' );
$this->assertArrayHasKey( 'supports', $properties, '`supports` should be included in the schema' );
$this->assertArrayHasKey( 'has_archive', $properties, '`has_archive` should be included in the schema' );
$this->assertArrayHasKey( 'taxonomies', $properties, '`taxonomies` should be included in the schema' );
$this->assertArrayHasKey( 'rest_base', $properties, '`rest_base` should be included in the schema' );
$this->assertArrayHasKey( 'rest_namespace', $properties, '`rest_namespace` should be included in the schema' );
Expand Down Expand Up @@ -230,6 +231,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'] );
Expand Down
9 changes: 9 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -11663,6 +11663,7 @@ mockedApiResponse.TypesCollection = {
"post": {
"description": "",
"hierarchical": false,
"has_archive": false,
"name": "Posts",
"slug": "post",
"icon": "dashicons-admin-post",
Expand Down Expand Up @@ -11695,6 +11696,7 @@ mockedApiResponse.TypesCollection = {
"page": {
"description": "",
"hierarchical": true,
"has_archive": false,
"name": "Pages",
"slug": "page",
"icon": "dashicons-admin-page",
Expand Down Expand Up @@ -11724,6 +11726,7 @@ mockedApiResponse.TypesCollection = {
"attachment": {
"description": "",
"hierarchical": false,
"has_archive": false,
"name": "Media",
"slug": "attachment",
"icon": "dashicons-admin-media",
Expand Down Expand Up @@ -11753,6 +11756,7 @@ mockedApiResponse.TypesCollection = {
"nav_menu_item": {
"description": "",
"hierarchical": false,
"has_archive": false,
"name": "Navigation Menu Items",
"slug": "nav_menu_item",
"icon": null,
Expand Down Expand Up @@ -11784,6 +11788,7 @@ mockedApiResponse.TypesCollection = {
"wp_block": {
"description": "",
"hierarchical": false,
"has_archive": false,
"name": "Reusable blocks",
"slug": "wp_block",
"icon": null,
Expand Down Expand Up @@ -11813,6 +11818,7 @@ mockedApiResponse.TypesCollection = {
"wp_template": {
"description": "Templates to include in your theme.",
"hierarchical": false,
"has_archive": false,
"name": "Templates",
"slug": "wp_template",
"icon": null,
Expand Down Expand Up @@ -11842,6 +11848,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",
"icon": null,
Expand Down Expand Up @@ -11871,6 +11878,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",
"icon": null,
Expand Down Expand Up @@ -11902,6 +11910,7 @@ mockedApiResponse.TypesCollection = {
mockedApiResponse.TypeModel = {
"description": "",
"hierarchical": false,
"has_archive": false,
"name": "Posts",
"slug": "post",
"icon": "dashicons-admin-post",
Expand Down