Make WordPress Core

Changeset 39097


Ignore:
Timestamp:
11/02/2016 09:50:08 PM (10 years ago)
Author:
joehoyle
Message:

REST API: Include taxonomies as an attribute of post types.

Add the taxonomies for a post type to the /wp/v2/types endpoint, so clients know which taxonomies are available for which post types.

Props danielbachhuber.
Fixes #38438, #38631.

Location:
trunk
Files:
2 edited

Legend:

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

    r39025 r39097  
    147147         */
    148148        public function prepare_item_for_response( $post_type, $request ) {
     149                $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
     150                $taxonomies = wp_list_pluck( $taxonomies, 'name' );
    149151                $data = array(
    150152                        'capabilities' => $post_type->cap,
     
    154156                        'name'         => $post_type->label,
    155157                        'slug'         => $post_type->name,
     158                        'taxonomies'   => array_values( $taxonomies ),
    156159                );
    157160                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
     
    204207                                        'description'  => __( 'All capabilities used by the resource.' ),
    205208                                        'type'         => 'array',
     209                                        'items'        => array(
     210                                                'type' => 'string',
     211                                        ),
    206212                                        'context'      => array( 'edit' ),
    207213                                        'readonly'     => true,
     
    237243                                        'readonly'     => true,
    238244                                ),
     245                                'taxonomies'       => array(
     246                                        'description'  => __( 'Taxonomies associated with resource.' ),
     247                                        'type'         => 'array',
     248                                        'items'        => array(
     249                                                'type' => 'string',
     250                                        ),
     251                                        'context'      => array( 'view', 'edit' ),
     252                                        'readonly'     => true,
     253                                ),
    239254                        ),
    240255                );
  • trunk/tests/phpunit/tests/rest-api/rest-post-types-controller.php

    r38832 r39097  
    5959                $response = $this->server->dispatch( $request );
    6060                $this->check_post_type_object_response( 'view', $response );
     61                $data = $response->get_data();
     62                $this->assertEquals( array( 'category', 'post_tag' ), $data['taxonomies'] );
     63        }
     64
     65        public function test_get_item_page() {
     66                $request = new WP_REST_Request( 'GET', '/wp/v2/types/page' );
     67                $response = $this->server->dispatch( $request );
     68                $this->check_post_type_object_response( 'view', $response, 'page' );
     69                $data = $response->get_data();
     70                $this->assertEquals( array(), $data['taxonomies'] );
    6171        }
    6272
     
    110120                $data = $response->get_data();
    111121                $properties = $data['schema']['properties'];
    112                 $this->assertEquals( 6, count( $properties ) );
     122                $this->assertEquals( 7, count( $properties ) );
    113123                $this->assertArrayHasKey( 'capabilities', $properties );
    114124                $this->assertArrayHasKey( 'description', $properties );
     
    117127                $this->assertArrayHasKey( 'name', $properties );
    118128                $this->assertArrayHasKey( 'slug', $properties );
     129                $this->assertArrayHasKey( 'taxonomies', $properties );
    119130        }
    120131
     
    173184        }
    174185
    175         protected function check_post_type_object_response( $context, $response ) {
     186        protected function check_post_type_object_response( $context, $response, $post_type = 'post' ) {
    176187                $this->assertEquals( 200, $response->get_status() );
    177188                $data = $response->get_data();
    178                 $obj = get_post_type_object( 'post' );
     189                $obj = get_post_type_object( $post_type );
    179190                $this->check_post_type_obj( $context, $obj, $data, $response->get_links() );
    180191        }
Note: See TracChangeset for help on using the changeset viewer.