Make WordPress Core

Changeset 39191


Ignore:
Timestamp:
11/10/2016 02:20:09 AM (10 years ago)
Author:
joehoyle
Message:

REST API: Add rest_base to response objects of wp/v2/taxonomies and wp/v2/types

Though we have the _links.collection available, having this value can be useful to know post type / taxonomy urls if you need to build them another way.

Props youknowriad, jnylen0.
Fixes #38607.

Location:
trunk
Files:
4 edited

Legend:

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

    r39106 r39191  
    149149                $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
    150150                $taxonomies = wp_list_pluck( $taxonomies, 'name' );
     151                $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
    151152                $data = array(
    152153                        'capabilities' => $post_type->cap,
     
    157158                        'slug'         => $post_type->name,
    158159                        'taxonomies'   => array_values( $taxonomies ),
     160                        'rest_base'    => $base,
    159161                );
    160162                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
     
    164166                // Wrap the data in a response object.
    165167                $response = rest_ensure_response( $data );
    166 
    167                 $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
    168168
    169169                $response->add_links( array(
     
    252252                                        'readonly'     => true,
    253253                                ),
     254                                'rest_base'            => array(
     255                                        'description'  => __( 'REST base route for the resource.' ),
     256                                        'type'         => 'string',
     257                                        'context'      => array( 'view', 'edit', 'embed' ),
     258                                        'readonly'     => true,
     259                                ),
    254260                        ),
    255261                );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r39106 r39191  
    178178         */
    179179        public function prepare_item_for_response( $taxonomy, $request ) {
    180 
     180                $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    181181                $data = array(
    182182                        'name'         => $taxonomy->label,
     
    188188                        'show_cloud'   => $taxonomy->show_tagcloud,
    189189                        'hierarchical' => $taxonomy->hierarchical,
     190                        'rest_base'    => $base,
    190191                );
    191192
     
    197198                $response = rest_ensure_response( $data );
    198199
    199                 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    200200                $response->add_links( array(
    201201                        'collection'                => array(
     
    286286                                        'readonly'     => true,
    287287                                ),
     288                                'rest_base'            => array(
     289                                        'description'  => __( 'REST base route for the resource.' ),
     290                                        'type'         => 'string',
     291                                        'context'      => array( 'view', 'edit', 'embed' ),
     292                                        'readonly'     => true,
     293                                ),
    288294                        ),
    289295                );
  • trunk/tests/phpunit/tests/rest-api/rest-post-types-controller.php

    r39097 r39191  
    120120                $data = $response->get_data();
    121121                $properties = $data['schema']['properties'];
    122                 $this->assertEquals( 7, count( $properties ) );
     122                $this->assertEquals( 8, count( $properties ) );
    123123                $this->assertArrayHasKey( 'capabilities', $properties );
    124124                $this->assertArrayHasKey( 'description', $properties );
     
    128128                $this->assertArrayHasKey( 'slug', $properties );
    129129                $this->assertArrayHasKey( 'taxonomies', $properties );
     130                $this->assertArrayHasKey( 'rest_base', $properties );
    130131        }
    131132
     
    171172                $this->assertEquals( $post_type_obj->description, $data['description'] );
    172173                $this->assertEquals( $post_type_obj->hierarchical, $data['hierarchical'] );
     174                $this->assertEquals( $post_type_obj->rest_base, $data['rest_base'] );
    173175
    174176                $links = test_rest_expand_compact_links( $links );
  • trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php

    r38832 r39191  
    4646                $this->assertEquals( 'post_tag', $data['post_tag']['slug'] );
    4747                $this->assertEquals( false, $data['post_tag']['hierarchical'] );
     48                $this->assertEquals( 'tags', $data['post_tag']['rest_base'] );
    4849        }
    4950
     
    135136                $data = $response->get_data();
    136137                $properties = $data['schema']['properties'];
    137                 $this->assertEquals( 8, count( $properties ) );
     138                $this->assertEquals( 9, count( $properties ) );
    138139                $this->assertArrayHasKey( 'capabilities', $properties );
    139140                $this->assertArrayHasKey( 'description', $properties );
     
    144145                $this->assertArrayHasKey( 'show_cloud', $properties );
    145146                $this->assertArrayHasKey( 'types', $properties );
     147                $this->assertArrayHasKey( 'rest_base', $properties );
    146148        }
    147149
     
    169171                $this->assertEquals( $tax_obj->description, $data['description'] );
    170172                $this->assertEquals( $tax_obj->hierarchical, $data['hierarchical'] );
     173                $this->assertEquals( $tax_obj->rest_base, $data['rest_base'] );
    171174                $this->assertEquals( rest_url( 'wp/v2/taxonomies' ), $links['collection'][0]['href'] );
    172175                $this->assertArrayHasKey( 'https://api.w.org/items', $links );
Note: See TracChangeset for help on using the changeset viewer.