Ticket #38607: 38607.2.diff
| File 38607.2.diff, 6.8 KB (added by , 9 years ago) |
|---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php
148 148 public function prepare_item_for_response( $post_type, $request ) { 149 149 $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) ); 150 150 $taxonomies = wp_list_pluck( $taxonomies, 'name' ); 151 $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name; 151 152 $data = array( 152 153 'capabilities' => $post_type->cap, 153 154 'description' => $post_type->description, … … 156 157 'name' => $post_type->label, 157 158 'slug' => $post_type->name, 158 159 'taxonomies' => array_values( $taxonomies ), 160 'rest_base' => $base, 159 161 ); 160 162 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 161 163 $data = $this->add_additional_fields_to_object( $data, $request ); … … 164 166 // Wrap the data in a response object. 165 167 $response = rest_ensure_response( $data ); 166 168 167 $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;168 169 169 $response->add_links( array( 170 170 'collection' => array( 171 171 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), … … 251 251 'context' => array( 'view', 'edit' ), 252 252 'readonly' => true, 253 253 ), 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 ), 254 260 ), 255 261 ); 256 262 return $this->add_additional_fields_schema( $schema ); -
src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
177 177 * @return WP_REST_Response Response object. 178 178 */ 179 179 public function prepare_item_for_response( $taxonomy, $request ) { 180 180 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 181 181 $data = array( 182 182 'name' => $taxonomy->label, 183 183 'slug' => $taxonomy->name, … … 187 187 'types' => $taxonomy->object_type, 188 188 'show_cloud' => $taxonomy->show_tagcloud, 189 189 'hierarchical' => $taxonomy->hierarchical, 190 'rest_base' => $base, 190 191 ); 191 192 192 193 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; … … 196 197 // Wrap the data in a response object. 197 198 $response = rest_ensure_response( $data ); 198 199 199 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;200 200 $response->add_links( array( 201 201 'collection' => array( 202 202 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), … … 285 285 'context' => array( 'view', 'edit' ), 286 286 'readonly' => true, 287 287 ), 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 ), 288 294 ), 289 295 ); 290 296 return $this->add_additional_fields_schema( $schema ); -
tests/phpunit/tests/rest-api/rest-post-types-controller.php
119 119 $response = $this->server->dispatch( $request ); 120 120 $data = $response->get_data(); 121 121 $properties = $data['schema']['properties']; 122 $this->assertEquals( 7, count( $properties ) );122 $this->assertEquals( 8, count( $properties ) ); 123 123 $this->assertArrayHasKey( 'capabilities', $properties ); 124 124 $this->assertArrayHasKey( 'description', $properties ); 125 125 $this->assertArrayHasKey( 'hierarchical', $properties ); … … 127 127 $this->assertArrayHasKey( 'name', $properties ); 128 128 $this->assertArrayHasKey( 'slug', $properties ); 129 129 $this->assertArrayHasKey( 'taxonomies', $properties ); 130 $this->assertArrayHasKey( 'rest_base', $properties ); 130 131 } 131 132 132 133 public function test_get_additional_field_registration() { … … 170 171 $this->assertEquals( $post_type_obj->name, $data['slug'] ); 171 172 $this->assertEquals( $post_type_obj->description, $data['description'] ); 172 173 $this->assertEquals( $post_type_obj->hierarchical, $data['hierarchical'] ); 174 $this->assertEquals( $post_type_obj->rest_base, $data['rest_base'] ); 173 175 174 176 $links = test_rest_expand_compact_links( $links ); 175 177 $this->assertEquals( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] ); -
tests/phpunit/tests/rest-api/rest-taxonomies-controller.php
45 45 $this->assertEquals( 'Tags', $data['post_tag']['name'] ); 46 46 $this->assertEquals( 'post_tag', $data['post_tag']['slug'] ); 47 47 $this->assertEquals( false, $data['post_tag']['hierarchical'] ); 48 $this->assertEquals( 'tags', $data['post_tag']['rest_base'] ); 48 49 } 49 50 50 51 public function test_get_items_invalid_permission_for_context() { … … 134 135 $response = $this->server->dispatch( $request ); 135 136 $data = $response->get_data(); 136 137 $properties = $data['schema']['properties']; 137 $this->assertEquals( 8, count( $properties ) );138 $this->assertEquals( 9, count( $properties ) ); 138 139 $this->assertArrayHasKey( 'capabilities', $properties ); 139 140 $this->assertArrayHasKey( 'description', $properties ); 140 141 $this->assertArrayHasKey( 'hierarchical', $properties ); … … 143 144 $this->assertArrayHasKey( 'slug', $properties ); 144 145 $this->assertArrayHasKey( 'show_cloud', $properties ); 145 146 $this->assertArrayHasKey( 'types', $properties ); 147 $this->assertArrayHasKey( 'rest_base', $properties ); 146 148 } 147 149 148 150 public function tearDown() { … … 168 170 $this->assertEquals( $tax_obj->name, $data['slug'] ); 169 171 $this->assertEquals( $tax_obj->description, $data['description'] ); 170 172 $this->assertEquals( $tax_obj->hierarchical, $data['hierarchical'] ); 173 $this->assertEquals( $tax_obj->rest_base, $data['rest_base'] ); 171 174 $this->assertEquals( rest_url( 'wp/v2/taxonomies' ), $links['collection'][0]['href'] ); 172 175 $this->assertArrayHasKey( 'https://api.w.org/items', $links ); 173 176 if ( 'edit' === $context ) {