Make WordPress Core

Ticket #38607: 38607.2.diff

File 38607.2.diff, 6.8 KB (added by joehoyle, 9 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

     
    148148        public function prepare_item_for_response( $post_type, $request ) {
    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,
    153154                        'description'  => $post_type->description,
     
    156157                        'name'         => $post_type->label,
    157158                        'slug'         => $post_type->name,
    158159                        'taxonomies'   => array_values( $taxonomies ),
     160                        'rest_base'    => $base,
    159161                );
    160162                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    161163                $data    = $this->add_additional_fields_to_object( $data, $request );
     
    164166                // Wrap the data in a response object.
    165167                $response = rest_ensure_response( $data );
    166168
    167                 $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
    168 
    169169                $response->add_links( array(
    170170                        'collection' => array(
    171171                                'href'   => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
     
    251251                                        'context'      => array( 'view', 'edit' ),
    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                );
    256262                return $this->add_additional_fields_schema( $schema );
  • src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

     
    177177         * @return WP_REST_Response Response object.
    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,
    183183                        'slug'         => $taxonomy->name,
     
    187187                        'types'        => $taxonomy->object_type,
    188188                        'show_cloud'   => $taxonomy->show_tagcloud,
    189189                        'hierarchical' => $taxonomy->hierarchical,
     190                        'rest_base'    => $base,
    190191                );
    191192
    192193                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
     
    196197                // Wrap the data in a response object.
    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(
    202202                                'href'                  => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
     
    285285                                        'context'      => array( 'view', 'edit' ),
    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                );
    290296                return $this->add_additional_fields_schema( $schema );
  • tests/phpunit/tests/rest-api/rest-post-types-controller.php

     
    119119                $response = $this->server->dispatch( $request );
    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 );
    125125                $this->assertArrayHasKey( 'hierarchical', $properties );
     
    127127                $this->assertArrayHasKey( 'name', $properties );
    128128                $this->assertArrayHasKey( 'slug', $properties );
    129129                $this->assertArrayHasKey( 'taxonomies', $properties );
     130                $this->assertArrayHasKey( 'rest_base', $properties );
    130131        }
    131132
    132133        public function test_get_additional_field_registration() {
     
    170171                $this->assertEquals( $post_type_obj->name, $data['slug'] );
    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 );
    175177                $this->assertEquals( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );
  • tests/phpunit/tests/rest-api/rest-taxonomies-controller.php

     
    4545                $this->assertEquals( 'Tags', $data['post_tag']['name'] );
    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
    5051        public function test_get_items_invalid_permission_for_context() {
     
    134135                $response = $this->server->dispatch( $request );
    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 );
    140141                $this->assertArrayHasKey( 'hierarchical', $properties );
     
    143144                $this->assertArrayHasKey( 'slug', $properties );
    144145                $this->assertArrayHasKey( 'show_cloud', $properties );
    145146                $this->assertArrayHasKey( 'types', $properties );
     147                $this->assertArrayHasKey( 'rest_base', $properties );
    146148        }
    147149
    148150        public function tearDown() {
     
    168170                $this->assertEquals( $tax_obj->name, $data['slug'] );
    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 );
    173176                if ( 'edit' === $context ) {