Changeset 43445
- Timestamp:
- 07/13/2018 06:50:51 AM (6 years ago)
- Location:
- branches/4.9
- Files:
-
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.9
-
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r43438 r43445 285 285 public function prepare_item_for_response( $post, $request ) { 286 286 $response = parent::prepare_item_for_response( $post, $request ); 287 $data = $response->get_data(); 288 289 $data['description'] = array( 290 'raw' => $post->post_content, 287 $fields = $this->get_fields_for_response( $request ); 288 $data = $response->get_data(); 289 290 if ( in_array( 'description', $fields, true ) ) { 291 $data['description'] = array( 292 'raw' => $post->post_content, 293 /** This filter is documented in wp-includes/post-template.php */ 294 'rendered' => apply_filters( 'the_content', $post->post_content ), 295 ); 296 } 297 298 if ( in_array( 'caption', $fields, true ) ) { 291 299 /** This filter is documented in wp-includes/post-template.php */ 292 'rendered' => apply_filters( 'the_content', $post->post_content ), 293 ); 294 295 /** This filter is documented in wp-includes/post-template.php */ 296 $caption = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) ); 297 $data['caption'] = array( 298 'raw' => $post->post_excerpt, 299 'rendered' => $caption, 300 ); 301 302 $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); 303 $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' : 'file'; 304 $data['mime_type'] = $post->post_mime_type; 305 $data['media_details'] = wp_get_attachment_metadata( $post->ID ); 306 $data['post'] = ! empty( $post->post_parent ) ? (int) $post->post_parent : null; 307 $data['source_url'] = wp_get_attachment_url( $post->ID ); 308 309 // Ensure empty details is an empty object. 310 if ( empty( $data['media_details'] ) ) { 311 $data['media_details'] = new stdClass; 312 } elseif ( ! empty( $data['media_details']['sizes'] ) ) { 313 314 foreach ( $data['media_details']['sizes'] as $size => &$size_data ) { 315 316 if ( isset( $size_data['mime-type'] ) ) { 317 $size_data['mime_type'] = $size_data['mime-type']; 318 unset( $size_data['mime-type'] ); 300 $caption = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) ); 301 $data['caption'] = array( 302 'raw' => $post->post_excerpt, 303 'rendered' => $caption, 304 ); 305 } 306 307 if ( in_array( 'alt_text', $fields, true ) ) { 308 $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); 309 } 310 311 if ( in_array( 'media_type', $fields, true ) ) { 312 $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' : 'file'; 313 } 314 315 if ( in_array( 'mime_type', $fields, true ) ) { 316 $data['mime_type'] = $post->post_mime_type; 317 } 318 319 if ( in_array( 'media_details', $fields, true ) ) { 320 $data['media_details'] = wp_get_attachment_metadata( $post->ID ); 321 322 // Ensure empty details is an empty object. 323 if ( empty( $data['media_details'] ) ) { 324 $data['media_details'] = new stdClass; 325 } elseif ( ! empty( $data['media_details']['sizes'] ) ) { 326 327 foreach ( $data['media_details']['sizes'] as $size => &$size_data ) { 328 329 if ( isset( $size_data['mime-type'] ) ) { 330 $size_data['mime_type'] = $size_data['mime-type']; 331 unset( $size_data['mime-type'] ); 332 } 333 334 // Use the same method image_downsize() does. 335 $image_src = wp_get_attachment_image_src( $post->ID, $size ); 336 if ( ! $image_src ) { 337 continue; 338 } 339 340 $size_data['source_url'] = $image_src[0]; 319 341 } 320 342 321 // Use the same method image_downsize() does. 322 $image_src = wp_get_attachment_image_src( $post->ID, $size ); 323 if ( ! $image_src ) { 324 continue; 343 $full_src = wp_get_attachment_image_src( $post->ID, 'full' ); 344 345 if ( ! empty( $full_src ) ) { 346 $data['media_details']['sizes']['full'] = array( 347 'file' => wp_basename( $full_src[0] ), 348 'width' => $full_src[1], 349 'height' => $full_src[2], 350 'mime_type' => $post->post_mime_type, 351 'source_url' => $full_src[0], 352 ); 325 353 } 326 327 $size_data['source_url'] = $image_src[0]; 328 } 329 330 $full_src = wp_get_attachment_image_src( $post->ID, 'full' ); 331 332 if ( ! empty( $full_src ) ) { 333 $data['media_details']['sizes']['full'] = array( 334 'file' => wp_basename( $full_src[0] ), 335 'width' => $full_src[1], 336 'height' => $full_src[2], 337 'mime_type' => $post->post_mime_type, 338 'source_url' => $full_src[0], 339 ); 340 } 341 } else { 342 $data['media_details']['sizes'] = new stdClass; 354 } else { 355 $data['media_details']['sizes'] = new stdClass; 356 } 357 } 358 359 if ( in_array( 'post', $fields, true ) ) { 360 $data['post'] = ! empty( $post->post_parent ) ? (int) $post->post_parent : null; 361 } 362 363 if ( in_array( 'source_url', $fields, true ) ) { 364 $data['source_url'] = wp_get_attachment_url( $post->ID ); 343 365 } 344 366 -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
r41735 r43445 852 852 */ 853 853 public function prepare_item_for_response( $comment, $request ) { 854 $data = array( 855 'id' => (int) $comment->comment_ID, 856 'post' => (int) $comment->comment_post_ID, 857 'parent' => (int) $comment->comment_parent, 858 'author' => (int) $comment->user_id, 859 'author_name' => $comment->comment_author, 860 'author_email' => $comment->comment_author_email, 861 'author_url' => $comment->comment_author_url, 862 'author_ip' => $comment->comment_author_IP, 863 'author_user_agent' => $comment->comment_agent, 864 'date' => mysql_to_rfc3339( $comment->comment_date ), 865 'date_gmt' => mysql_to_rfc3339( $comment->comment_date_gmt ), 866 'content' => array( 854 855 $fields = $this->get_fields_for_response( $request ); 856 $data = array(); 857 858 if ( in_array( 'id', $fields, true ) ) { 859 $data['id'] = (int) $comment->comment_ID; 860 } 861 862 if ( in_array( 'post', $fields, true ) ) { 863 $data['post'] = (int) $comment->comment_post_ID; 864 } 865 866 if ( in_array( 'parent', $fields, true ) ) { 867 $data['parent'] = (int) $comment->comment_parent; 868 } 869 870 if ( in_array( 'author', $fields, true ) ) { 871 $data['author'] = (int) $comment->user_id; 872 } 873 874 if ( in_array( 'author_name', $fields, true ) ) { 875 $data['author_name'] = $comment->comment_author; 876 } 877 878 if ( in_array( 'author_email', $fields, true ) ) { 879 $data['author_email'] = $comment->comment_author_email; 880 } 881 882 if ( in_array( 'author_url', $fields, true ) ) { 883 $data['author_url'] = $comment->comment_author_url; 884 } 885 886 if ( in_array( 'author_ip', $fields, true ) ) { 887 $data['author_ip'] = $comment->comment_author_IP; 888 } 889 890 if ( in_array( 'author_user_agent', $fields, true ) ) { 891 $data['author_user_agent'] = $comment->comment_agent; 892 } 893 894 if ( in_array( 'date', $fields, true ) ) { 895 $data['date'] = mysql_to_rfc3339( $comment->comment_date ); 896 } 897 898 if ( in_array( 'date_gmt', $fields, true ) ) { 899 $data['date_gmt'] = mysql_to_rfc3339( $comment->comment_date_gmt ); 900 } 901 902 if ( in_array( 'content', $fields, true ) ) { 903 $data['content'] = array( 867 904 /** This filter is documented in wp-includes/comment-template.php */ 868 905 'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ), 869 906 'raw' => $comment->comment_content, 870 ), 871 'link' => get_comment_link( $comment ), 872 'status' => $this->prepare_status_response( $comment->comment_approved ), 873 'type' => get_comment_type( $comment->comment_ID ), 874 ); 875 876 $schema = $this->get_item_schema(); 877 878 if ( ! empty( $schema['properties']['author_avatar_urls'] ) ) { 907 ); 908 } 909 910 if ( in_array( 'link', $fields, true ) ) { 911 $data['link'] = get_comment_link( $comment ); 912 } 913 914 if ( in_array( 'status', $fields, true ) ) { 915 $data['status'] = $this->prepare_status_response( $comment->comment_approved ); 916 } 917 918 if ( in_array( 'type', $fields, true ) ) { 919 $data['type'] = get_comment_type( $comment->comment_ID ); 920 } 921 922 if ( in_array( 'author_avatar_urls', $fields, true ) ) { 879 923 $data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email ); 880 924 } 881 925 882 if ( ! empty( $schema['properties']['meta']) ) {926 if ( in_array( 'meta', $fields, true ) ) { 883 927 $data['meta'] = $this->meta->get_value( $comment->comment_ID, $request ); 884 928 } -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
r42000 r43445 507 507 508 508 /** 509 * Gets an array of fields to be included on the response. 510 * 511 * Included fields are based on item schema and `_fields=` request argument. 512 * 513 * @since 4.9.6 514 * 515 * @param WP_REST_Request $request Full details about the request. 516 * @return array Fields to be included in the response. 517 */ 518 public function get_fields_for_response( $request ) { 519 $schema = $this->get_item_schema(); 520 $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array(); 521 if ( ! isset( $request['_fields'] ) ) { 522 return $fields; 523 } 524 $requested_fields = is_array( $request['_fields'] ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] ); 525 if ( 0 === count( $requested_fields ) ) { 526 return $fields; 527 } 528 // Trim off outside whitespace from the comma delimited list. 529 $requested_fields = array_map( 'trim', $requested_fields ); 530 // Always persist 'id', because it can be needed for add_additional_fields_to_object(). 531 if ( in_array( 'id', $fields, true ) ) { 532 $requested_fields[] = 'id'; 533 } 534 return array_intersect( $fields, $requested_fields ); 535 } 536 537 /** 509 538 * Retrieves an array of endpoint arguments from the item schema for the controller. 510 539 * -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php
r41731 r43445 196 196 public function prepare_item_for_response( $status, $request ) { 197 197 198 $data = array( 199 'name' => $status->label, 200 'private' => (bool) $status->private, 201 'protected' => (bool) $status->protected, 202 'public' => (bool) $status->public, 203 'queryable' => (bool) $status->publicly_queryable, 204 'show_in_list' => (bool) $status->show_in_admin_all_list, 205 'slug' => $status->name, 206 ); 198 $fields = $this->get_fields_for_response( $request ); 199 $data = array(); 200 201 if ( in_array( 'name', $fields, true ) ) { 202 $data['name'] = $status->label; 203 } 204 205 if ( in_array( 'private', $fields, true ) ) { 206 $data['private'] = (bool) $status->private; 207 } 208 209 if ( in_array( 'protected', $fields, true ) ) { 210 $data['protected'] = (bool) $status->protected; 211 } 212 213 if ( in_array( 'public', $fields, true ) ) { 214 $data['public'] = (bool) $status->public; 215 } 216 217 if ( in_array( 'queryable', $fields, true ) ) { 218 $data['queryable'] = (bool) $status->publicly_queryable; 219 } 220 221 if ( in_array( 'show_in_list', $fields, true ) ) { 222 $data['show_in_list'] = (bool) $status->show_in_admin_all_list; 223 } 224 225 if ( in_array( 'slug', $fields, true ) ) { 226 $data['slug'] = $status->name; 227 } 207 228 208 229 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php
r43038 r43445 152 152 $supports = get_all_post_type_supports( $post_type->name ); 153 153 154 $data = array( 155 'capabilities' => $post_type->cap, 156 'description' => $post_type->description, 157 'hierarchical' => $post_type->hierarchical, 158 'viewable' => is_post_type_viewable( $post_type ), 159 'labels' => $post_type->labels, 160 'name' => $post_type->label, 161 'slug' => $post_type->name, 162 'supports' => $supports, 163 'taxonomies' => array_values( $taxonomies ), 164 'rest_base' => $base, 165 ); 154 $fields = $this->get_fields_for_response( $request ); 155 $data = array(); 156 157 if ( in_array( 'capabilities', $fields, true ) ) { 158 $data['capabilities'] = $post_type->cap; 159 } 160 161 if ( in_array( 'description', $fields, true ) ) { 162 $data['description'] = $post_type->description; 163 } 164 165 if ( in_array( 'hierarchical', $fields, true ) ) { 166 $data['hierarchical'] = $post_type->hierarchical; 167 } 168 169 if ( in_array( 'viewable', $fields, true ) ) { 170 $data['viewable'] = is_post_type_viewable( $post_type ); 171 } 172 173 if ( in_array( 'labels', $fields, true ) ) { 174 $data['labels'] = $post_type->labels; 175 } 176 177 if ( in_array( 'name', $fields, true ) ) { 178 $data['name'] = $post_type->label; 179 } 180 181 if ( in_array( 'slug', $fields, true ) ) { 182 $data['slug'] = $post_type->name; 183 } 184 185 if ( in_array( 'supports', $fields, true ) ) { 186 $data['supports'] = $supports; 187 } 188 189 if ( in_array( 'taxonomies', $fields, true ) ) { 190 $data['taxonomies'] = array_values( $taxonomies ); 191 } 192 193 if ( in_array( 'rest_base', $fields, true ) ) { 194 $data['rest_base'] = $base; 195 } 196 166 197 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 167 198 $data = $this->add_additional_fields_to_object( $data, $request ); -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r43442 r43445 1401 1401 setup_postdata( $post ); 1402 1402 1403 $ schema = $this->get_item_schema();1403 $fields = $this->get_fields_for_response( $request ); 1404 1404 1405 1405 // Base fields for every post. 1406 1406 $data = array(); 1407 1407 1408 if ( ! empty( $schema['properties']['id']) ) {1408 if ( in_array( 'id', $fields, true ) ) { 1409 1409 $data['id'] = $post->ID; 1410 1410 } 1411 1411 1412 if ( ! empty( $schema['properties']['date']) ) {1412 if ( in_array( 'date', $fields, true ) ) { 1413 1413 $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); 1414 1414 } 1415 1415 1416 if ( ! empty( $schema['properties']['date_gmt']) ) {1416 if ( in_array( 'date_gmt', $fields, true ) ) { 1417 1417 // For drafts, `post_date_gmt` may not be set, indicating that the 1418 1418 // date of the draft should be updated each time it is saved (see … … 1427 1427 } 1428 1428 1429 if ( ! empty( $schema['properties']['guid']) ) {1429 if ( in_array( 'guid', $fields, true ) ) { 1430 1430 $data['guid'] = array( 1431 1431 /** This filter is documented in wp-includes/post-template.php */ … … 1435 1435 } 1436 1436 1437 if ( ! empty( $schema['properties']['modified']) ) {1437 if ( in_array( 'modified', $fields, true ) ) { 1438 1438 $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); 1439 1439 } 1440 1440 1441 if ( ! empty( $schema['properties']['modified_gmt']) ) {1441 if ( in_array( 'modified_gmt', $fields, true ) ) { 1442 1442 // For drafts, `post_modified_gmt` may not be set (see 1443 1443 // `post_date_gmt` comments above). In this case, shim the value … … 1452 1452 } 1453 1453 1454 if ( ! empty( $schema['properties']['password']) ) {1454 if ( in_array( 'password', $fields, true ) ) { 1455 1455 $data['password'] = $post->post_password; 1456 1456 } 1457 1457 1458 if ( ! empty( $schema['properties']['slug']) ) {1458 if ( in_array( 'slug', $fields, true ) ) { 1459 1459 $data['slug'] = $post->post_name; 1460 1460 } 1461 1461 1462 if ( ! empty( $schema['properties']['status']) ) {1462 if ( in_array( 'status', $fields, true ) ) { 1463 1463 $data['status'] = $post->post_status; 1464 1464 } 1465 1465 1466 if ( ! empty( $schema['properties']['type']) ) {1466 if ( in_array( 'type', $fields, true ) ) { 1467 1467 $data['type'] = $post->post_type; 1468 1468 } 1469 1469 1470 if ( ! empty( $schema['properties']['link']) ) {1470 if ( in_array( 'link', $fields, true ) ) { 1471 1471 $data['link'] = get_permalink( $post->ID ); 1472 1472 } 1473 1473 1474 if ( ! empty( $schema['properties']['title']) ) {1474 if ( in_array( 'title', $fields, true ) ) { 1475 1475 add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); 1476 1476 … … 1492 1492 } 1493 1493 1494 if ( ! empty( $schema['properties']['content']) ) {1494 if ( in_array( 'content', $fields, true ) ) { 1495 1495 $data['content'] = array( 1496 1496 'raw' => $post->post_content, … … 1501 1501 } 1502 1502 1503 if ( ! empty( $schema['properties']['excerpt']) ) {1503 if ( in_array( 'excerpt', $fields, true ) ) { 1504 1504 /** This filter is documented in wp-includes/post-template.php */ 1505 1505 $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) ); … … 1516 1516 } 1517 1517 1518 if ( ! empty( $schema['properties']['author']) ) {1518 if ( in_array( 'author', $fields, true ) ) { 1519 1519 $data['author'] = (int) $post->post_author; 1520 1520 } 1521 1521 1522 if ( ! empty( $schema['properties']['featured_media']) ) {1522 if ( in_array( 'featured_media', $fields, true ) ) { 1523 1523 $data['featured_media'] = (int) get_post_thumbnail_id( $post->ID ); 1524 1524 } 1525 1525 1526 if ( ! empty( $schema['properties']['parent']) ) {1526 if ( in_array( 'parent', $fields, true ) ) { 1527 1527 $data['parent'] = (int) $post->post_parent; 1528 1528 } 1529 1529 1530 if ( ! empty( $schema['properties']['menu_order']) ) {1530 if ( in_array( 'menu_order', $fields, true ) ) { 1531 1531 $data['menu_order'] = (int) $post->menu_order; 1532 1532 } 1533 1533 1534 if ( ! empty( $schema['properties']['comment_status']) ) {1534 if ( in_array( 'comment_status', $fields, true ) ) { 1535 1535 $data['comment_status'] = $post->comment_status; 1536 1536 } 1537 1537 1538 if ( ! empty( $schema['properties']['ping_status']) ) {1538 if ( in_array( 'ping_status', $fields, true ) ) { 1539 1539 $data['ping_status'] = $post->ping_status; 1540 1540 } 1541 1541 1542 if ( ! empty( $schema['properties']['sticky']) ) {1542 if ( in_array( 'sticky', $fields, true ) ) { 1543 1543 $data['sticky'] = is_sticky( $post->ID ); 1544 1544 } 1545 1545 1546 if ( ! empty( $schema['properties']['template']) ) {1546 if ( in_array( 'template', $fields, true ) ) { 1547 1547 if ( $template = get_page_template_slug( $post->ID ) ) { 1548 1548 $data['template'] = $template; … … 1552 1552 } 1553 1553 1554 if ( ! empty( $schema['properties']['format']) ) {1554 if ( in_array( 'format', $fields, true ) ) { 1555 1555 $data['format'] = get_post_format( $post->ID ); 1556 1556 … … 1561 1561 } 1562 1562 1563 if ( ! empty( $schema['properties']['meta']) ) {1563 if ( in_array( 'meta', $fields, true ) ) { 1564 1564 $data['meta'] = $this->meta->get_value( $post->ID, $request ); 1565 1565 } … … 1570 1570 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 1571 1571 1572 if ( ! empty( $schema['properties'][ $base ]) ) {1572 if ( in_array( $base, $fields, true ) ) { 1573 1573 $terms = get_the_terms( $post, $taxonomy->name ); 1574 1574 $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array(); -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
r41731 r43445 332 332 setup_postdata( $post ); 333 333 334 $schema = $this->get_item_schema(); 335 336 $data = array(); 337 338 if ( ! empty( $schema['properties']['author'] ) ) { 334 $fields = $this->get_fields_for_response( $request ); 335 $data = array(); 336 337 if ( in_array( 'author', $fields, true ) ) { 339 338 $data['author'] = (int) $post->post_author; 340 339 } 341 340 342 if ( ! empty( $schema['properties']['date']) ) {341 if ( in_array( 'date', $fields, true ) ) { 343 342 $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); 344 343 } 345 344 346 if ( ! empty( $schema['properties']['date_gmt']) ) {345 if ( in_array( 'date_gmt', $fields, true ) ) { 347 346 $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); 348 347 } 349 348 350 if ( ! empty( $schema['properties']['id']) ) {349 if ( in_array( 'id', $fields, true ) ) { 351 350 $data['id'] = $post->ID; 352 351 } 353 352 354 if ( ! empty( $schema['properties']['modified']) ) {353 if ( in_array( 'modified', $fields, true ) ) { 355 354 $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); 356 355 } 357 356 358 if ( ! empty( $schema['properties']['modified_gmt']) ) {357 if ( in_array( 'modified_gmt', $fields, true ) ) { 359 358 $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); 360 359 } 361 360 362 if ( ! empty( $schema['properties']['parent']) ) {361 if ( in_array( 'parent', $fields, true ) ) { 363 362 $data['parent'] = (int) $post->post_parent; 364 363 } 365 364 366 if ( ! empty( $schema['properties']['slug']) ) {365 if ( in_array( 'slug', $fields, true ) ) { 367 366 $data['slug'] = $post->post_name; 368 367 } 369 368 370 if ( ! empty( $schema['properties']['guid']) ) {369 if ( in_array( 'guid', $fields, true ) ) { 371 370 $data['guid'] = array( 372 371 /** This filter is documented in wp-includes/post-template.php */ … … 376 375 } 377 376 378 if ( ! empty( $schema['properties']['title']) ) {377 if ( in_array( 'title', $fields, true ) ) { 379 378 $data['title'] = array( 380 379 'raw' => $post->post_title, … … 383 382 } 384 383 385 if ( ! empty( $schema['properties']['content']) ) {384 if ( in_array( 'content', $fields, true ) ) { 386 385 387 386 $data['content'] = array( … … 392 391 } 393 392 394 if ( ! empty( $schema['properties']['excerpt']) ) {393 if ( in_array( 'excerpt', $fields, true ) ) { 395 394 $data['excerpt'] = array( 396 395 'raw' => $post->post_excerpt, -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
r43443 r43445 178 178 public function prepare_item_for_response( $taxonomy, $request ) { 179 179 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 180 $data = array( 181 'name' => $taxonomy->label, 182 'slug' => $taxonomy->name, 183 'capabilities' => $taxonomy->cap, 184 'description' => $taxonomy->description, 185 'labels' => $taxonomy->labels, 186 'types' => $taxonomy->object_type, 187 'show_cloud' => $taxonomy->show_tagcloud, 188 'hierarchical' => $taxonomy->hierarchical, 189 'rest_base' => $base, 190 ); 180 181 $fields = $this->get_fields_for_response( $request ); 182 $data = array(); 183 184 if ( in_array( 'name', $fields, true ) ) { 185 $data['name'] = $taxonomy->label; 186 } 187 188 if ( in_array( 'slug', $fields, true ) ) { 189 $data['slug'] = $taxonomy->name; 190 } 191 192 if ( in_array( 'capabilities', $fields, true ) ) { 193 $data['capabilities'] = $taxonomy->cap; 194 } 195 196 if ( in_array( 'description', $fields, true ) ) { 197 $data['description'] = $taxonomy->description; 198 } 199 200 if ( in_array( 'labels', $fields, true ) ) { 201 $data['labels'] = $taxonomy->labels; 202 } 203 204 if ( in_array( 'types', $fields, true ) ) { 205 $data['types'] = $taxonomy->object_type; 206 } 207 208 if ( in_array( 'show_cloud', $fields, true ) ) { 209 $data['show_cloud'] = $taxonomy->show_tagcloud; 210 } 211 212 if ( in_array( 'hierarchical', $fields, true ) ) { 213 $data['hierarchical'] = $taxonomy->hierarchical; 214 } 215 216 if ( in_array( 'rest_base', $fields, true ) ) { 217 $data['rest_base'] = $base; 218 } 219 220 if ( in_array( 'visibility', $fields, true ) ) { 221 $data['visibility'] = array( 222 'public' => (bool) $taxonomy->public, 223 'publicly_queryable' => (bool) $taxonomy->publicly_queryable, 224 'show_admin_column' => (bool) $taxonomy->show_admin_column, 225 'show_in_nav_menus' => (bool) $taxonomy->show_in_nav_menus, 226 'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit, 227 'show_ui' => (bool) $taxonomy->show_ui, 228 ); 229 } 191 230 192 231 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
r43443 r43445 686 686 public function prepare_item_for_response( $item, $request ) { 687 687 688 $ schema = $this->get_item_schema();688 $fields = $this->get_fields_for_response( $request ); 689 689 $data = array(); 690 690 691 if ( ! empty( $schema['properties']['id']) ) {691 if ( in_array( 'id', $fields, true ) ) { 692 692 $data['id'] = (int) $item->term_id; 693 693 } 694 694 695 if ( ! empty( $schema['properties']['count']) ) {695 if ( in_array( 'count', $fields, true ) ) { 696 696 $data['count'] = (int) $item->count; 697 697 } 698 698 699 if ( ! empty( $schema['properties']['description']) ) {699 if ( in_array( 'description', $fields, true ) ) { 700 700 $data['description'] = $item->description; 701 701 } 702 702 703 if ( ! empty( $schema['properties']['link']) ) {703 if ( in_array( 'link', $fields, true ) ) { 704 704 $data['link'] = get_term_link( $item ); 705 705 } 706 706 707 if ( ! empty( $schema['properties']['name']) ) {707 if ( in_array( 'name', $fields, true ) ) { 708 708 $data['name'] = $item->name; 709 709 } 710 710 711 if ( ! empty( $schema['properties']['slug']) ) {711 if ( in_array( 'slug', $fields, true ) ) { 712 712 $data['slug'] = $item->slug; 713 713 } 714 714 715 if ( ! empty( $schema['properties']['taxonomy']) ) {715 if ( in_array( 'taxonomy', $fields, true ) ) { 716 716 $data['taxonomy'] = $item->taxonomy; 717 717 } 718 718 719 if ( ! empty( $schema['properties']['parent']) ) {719 if ( in_array( 'parent', $fields, true ) ) { 720 720 $data['parent'] = (int) $item->parent; 721 721 } 722 722 723 if ( ! empty( $schema['properties']['meta']) ) {723 if ( in_array( 'meta', $fields, true ) ) { 724 724 $data['meta'] = $this->meta->get_value( $item->term_id, $request ); 725 725 } -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
r43138 r43445 838 838 839 839 $data = array(); 840 $ schema = $this->get_item_schema();841 842 if ( ! empty( $schema['properties']['id']) ) {840 $fields = $this->get_fields_for_response( $request ); 841 842 if ( in_array( 'id', $fields, true ) ) { 843 843 $data['id'] = $user->ID; 844 844 } 845 845 846 if ( ! empty( $schema['properties']['username']) ) {846 if ( in_array( 'username', $fields, true ) ) { 847 847 $data['username'] = $user->user_login; 848 848 } 849 849 850 if ( ! empty( $schema['properties']['name']) ) {850 if ( in_array( 'name', $fields, true ) ) { 851 851 $data['name'] = $user->display_name; 852 852 } 853 853 854 if ( ! empty( $schema['properties']['first_name']) ) {854 if ( in_array( 'first_name', $fields, true ) ) { 855 855 $data['first_name'] = $user->first_name; 856 856 } 857 857 858 if ( ! empty( $schema['properties']['last_name']) ) {858 if ( in_array( 'last_name', $fields, true ) ) { 859 859 $data['last_name'] = $user->last_name; 860 860 } 861 861 862 if ( ! empty( $schema['properties']['email']) ) {862 if ( in_array( 'email', $fields, true ) ) { 863 863 $data['email'] = $user->user_email; 864 864 } 865 865 866 if ( ! empty( $schema['properties']['url']) ) {866 if ( in_array( 'url', $fields, true ) ) { 867 867 $data['url'] = $user->user_url; 868 868 } 869 869 870 if ( ! empty( $schema['properties']['description']) ) {870 if ( in_array( 'description', $fields, true ) ) { 871 871 $data['description'] = $user->description; 872 872 } 873 873 874 if ( ! empty( $schema['properties']['link']) ) {874 if ( in_array( 'link', $fields, true ) ) { 875 875 $data['link'] = get_author_posts_url( $user->ID, $user->user_nicename ); 876 876 } 877 877 878 if ( ! empty( $schema['properties']['locale']) ) {878 if ( in_array( 'locale', $fields, true ) ) { 879 879 $data['locale'] = get_user_locale( $user ); 880 880 } 881 881 882 if ( ! empty( $schema['properties']['nickname']) ) {882 if ( in_array( 'nickname', $fields, true ) ) { 883 883 $data['nickname'] = $user->nickname; 884 884 } 885 885 886 if ( ! empty( $schema['properties']['slug']) ) {886 if ( in_array( 'slug', $fields, true ) ) { 887 887 $data['slug'] = $user->user_nicename; 888 888 } 889 889 890 if ( ! empty( $schema['properties']['roles']) ) {890 if ( in_array( 'roles', $fields, true ) ) { 891 891 // Defensively call array_values() to ensure an array is returned. 892 892 $data['roles'] = array_values( $user->roles ); 893 893 } 894 894 895 if ( ! empty( $schema['properties']['registered_date']) ) {895 if ( in_array( 'registered_date', $fields, true ) ) { 896 896 $data['registered_date'] = date( 'c', strtotime( $user->user_registered ) ); 897 897 } 898 898 899 if ( ! empty( $schema['properties']['capabilities']) ) {899 if ( in_array( 'capabilities', $fields, true ) ) { 900 900 $data['capabilities'] = (object) $user->allcaps; 901 901 } 902 902 903 if ( ! empty( $schema['properties']['extra_capabilities']) ) {903 if ( in_array( 'extra_capabilities', $fields, true ) ) { 904 904 $data['extra_capabilities'] = (object) $user->caps; 905 905 } 906 906 907 if ( ! empty( $schema['properties']['avatar_urls']) ) {907 if ( in_array( 'avatar_urls', $fields, true ) ) { 908 908 $data['avatar_urls'] = rest_get_avatar_urls( $user->user_email ); 909 909 } 910 910 911 if ( ! empty( $schema['properties']['meta']) ) {911 if ( in_array( 'meta', $fields, true ) ) { 912 912 $data['meta'] = $this->meta->get_value( $user->ID, $request ); 913 913 } -
branches/4.9/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r43438 r43445 1075 1075 } 1076 1076 1077 public function test_prepare_item_limit_fields() { 1078 $attachment_id = $this->factory->attachment->create_object( 1079 $this->test_file, 0, array( 1080 'post_mime_type' => 'image/jpeg', 1081 'post_excerpt' => 'A sample caption', 1082 'post_author' => self::$editor_id, 1083 ) 1084 ); 1085 wp_set_current_user( self::$editor_id ); 1086 $endpoint = new WP_REST_Attachments_Controller( 'post' ); 1087 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) ); 1088 $request->set_param( 'context', 'edit' ); 1089 $request->set_param( '_fields', 'id,slug' ); 1090 $obj = get_post( $attachment_id ); 1091 $response = $endpoint->prepare_item_for_response( $obj, $request ); 1092 $this->assertEquals( array( 1093 'id', 1094 'slug', 1095 ), array_keys( $response->get_data() ) ); 1096 } 1097 1077 1098 public function test_get_item_schema() { 1078 1099 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ); -
branches/4.9/tests/phpunit/tests/rest-api/rest-categories-controller.php
r43443 r43445 828 828 } 829 829 830 public function test_prepare_item_limit_fields() { 831 $request = new WP_REST_Request; 832 $endpoint = new WP_REST_Terms_Controller( 'category' ); 833 $request->set_param( '_fields', 'id,name' ); 834 $term = get_term( 1, 'category' ); 835 $response = $endpoint->prepare_item_for_response( $term, $request ); 836 $this->assertEquals( array( 837 'id', 838 'name', 839 ), array_keys( $response->get_data() ) ); 840 } 841 830 842 public function test_prepare_taxonomy_term_child() { 831 843 $child = $this->factory->category->create( array( -
branches/4.9/tests/phpunit/tests/rest-api/rest-comments-controller.php
r40101 r43445 794 794 $data = $response->get_data(); 795 795 $this->check_comment_data( $data, 'edit', $response->get_links() ); 796 } 797 798 public function test_prepare_item_limit_fields() { 799 wp_set_current_user( self::$admin_id ); 800 $endpoint = new WP_REST_Comments_Controller; 801 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 802 $request->set_param( 'context', 'edit' ); 803 $request->set_param( '_fields', 'id,status' ); 804 $obj = get_comment( self::$approved_id ); 805 $response = $endpoint->prepare_item_for_response( $obj, $request ); 806 $this->assertEquals( array( 807 'id', 808 'status', 809 ), array_keys( $response->get_data() ) ); 796 810 } 797 811 -
branches/4.9/tests/phpunit/tests/rest-api/rest-controller.php
r39161 r43445 191 191 $this->assertEquals( 'a', $args['somedefault']['default'] ); 192 192 } 193 194 public function test_get_fields_for_response() { 195 $controller = new WP_REST_Test_Controller(); 196 $request = new WP_REST_Request( 'GET', '/wp/v2/testroute' ); 197 $fields = $controller->get_fields_for_response( $request ); 198 $this->assertEquals( array( 199 'somestring', 200 'someinteger', 201 'someboolean', 202 'someurl', 203 'somedate', 204 'someemail', 205 'someenum', 206 'someargoptions', 207 'somedefault', 208 ), $fields ); 209 $request->set_param( '_fields', 'somestring,someinteger' ); 210 $fields = $controller->get_fields_for_response( $request ); 211 $this->assertEquals( array( 212 'somestring', 213 'someinteger', 214 ), $fields ); 215 } 193 216 } -
branches/4.9/tests/phpunit/tests/rest-api/rest-pages-controller.php
r41228 r43445 334 334 public function test_prepare_item() { 335 335 336 } 337 338 public function test_prepare_item_limit_fields() { 339 wp_set_current_user( self::$editor_id ); 340 $page_id = $this->factory->post->create( 341 array( 342 'post_status' => 'publish', 343 'post_type' => 'page', 344 ) 345 ); 346 $endpoint = new WP_REST_Posts_Controller( 'page' ); 347 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) ); 348 $request->set_param( 'context', 'edit' ); 349 $request->set_param( '_fields', 'id,slug' ); 350 $obj = get_post( $page_id ); 351 $response = $endpoint->prepare_item_for_response( $obj, $request ); 352 $this->assertEquals( array( 353 'id', 354 'slug', 355 ), array_keys( $response->get_data() ) ); 336 356 } 337 357 -
branches/4.9/tests/phpunit/tests/rest-api/rest-post-statuses-controller.php
r41176 r43445 128 128 $data = $endpoint->prepare_item_for_response( $obj, $request ); 129 129 $this->check_post_status_obj( $obj, $data->get_data(), $data->get_links() ); 130 } 131 132 public function test_prepare_item_limit_fields() { 133 $obj = get_post_status_object( 'publish' ); 134 $request = new WP_REST_Request; 135 $endpoint = new WP_REST_Post_Statuses_Controller; 136 $request->set_param( 'context', 'edit' ); 137 $request->set_param( '_fields', 'id,name' ); 138 $response = $endpoint->prepare_item_for_response( $obj, $request ); 139 $this->assertEquals( array( 140 // 'id' doesn't exist in this context. 141 'name', 142 ), array_keys( $response->get_data() ) ); 130 143 } 131 144 -
branches/4.9/tests/phpunit/tests/rest-api/rest-post-types-controller.php
r43038 r43445 122 122 $response = $endpoint->prepare_item_for_response( $obj, $request ); 123 123 $this->check_post_type_obj( 'edit', $obj, $response->get_data(), $response->get_links() ); 124 } 125 126 public function test_prepare_item_limit_fields() { 127 $obj = get_post_type_object( 'post' ); 128 $request = new WP_REST_Request; 129 $endpoint = new WP_REST_Post_Types_Controller; 130 $request->set_param( 'context', 'edit' ); 131 $request->set_param( '_fields', 'id,name' ); 132 $response = $endpoint->prepare_item_for_response( $obj, $request ); 133 $this->assertEquals( array( 134 // 'id' doesn't exist in this context. 135 'name', 136 ), array_keys( $response->get_data() ) ); 124 137 } 125 138 -
branches/4.9/tests/phpunit/tests/rest-api/rest-posts-controller.php
r43442 r43445 1298 1298 1299 1299 $this->check_get_post_response( $response, 'edit' ); 1300 } 1301 1302 public function test_prepare_item_limit_fields() { 1303 wp_set_current_user( self::$editor_id ); 1304 $endpoint = new WP_REST_Posts_Controller( 'post' ); 1305 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1306 $request->set_param( 'context', 'edit' ); 1307 $request->set_param( '_fields', 'id,slug' ); 1308 $obj = get_post( self::$post_id ); 1309 $response = $endpoint->prepare_item_for_response( $obj, $request ); 1310 $this->assertEquals( array( 1311 'id', 1312 'slug', 1313 ), array_keys( $response->get_data() ) ); 1300 1314 } 1301 1315 -
branches/4.9/tests/phpunit/tests/rest-api/rest-revisions-controller.php
r41219 r43445 224 224 } 225 225 226 public function test_prepare_item_limit_fields() { 227 wp_set_current_user( self::$editor_id ); 228 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 229 $endpoint = new WP_REST_Revisions_Controller( 'post' ); 230 $request->set_param( 'context', 'edit' ); 231 $request->set_param( '_fields', 'id,slug' ); 232 $revision = get_post( $this->revision_id1 ); 233 $response = $endpoint->prepare_item_for_response( $revision, $request ); 234 $this->assertEquals( array( 235 'id', 236 'slug', 237 ), array_keys( $response->get_data() ) ); 238 } 239 226 240 public function test_get_item_schema() { 227 241 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' ); -
branches/4.9/tests/phpunit/tests/rest-api/rest-tags-controller.php
r43443 r43445 926 926 } 927 927 928 public function test_prepare_item_limit_fields() { 929 $request = new WP_REST_Request; 930 $endpoint = new WP_REST_Terms_Controller( 'post_tag' ); 931 $request->set_param( '_fields', 'id,name' ); 932 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 933 $response = $endpoint->prepare_item_for_response( $term, $request ); 934 $this->assertEquals( array( 935 'id', 936 'name', 937 ), array_keys( $response->get_data() ) ); 938 } 939 928 940 public function test_get_item_schema() { 929 941 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' ); -
branches/4.9/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php
r43443 r43445 178 178 $response = $endpoint->prepare_item_for_response( $tax, $request ); 179 179 $this->check_taxonomy_object( 'edit', $tax, $response->get_data(), $response->get_links() ); 180 } 181 182 public function test_prepare_item_limit_fields() { 183 $tax = get_taxonomy( 'category' ); 184 $request = new WP_REST_Request; 185 $endpoint = new WP_REST_Taxonomies_Controller; 186 $request->set_param( 'context', 'edit' ); 187 $request->set_param( '_fields', 'id,name' ); 188 $response = $endpoint->prepare_item_for_response( $tax, $request ); 189 $this->assertEquals( array( 190 // 'id' doesn't exist in this context. 191 'name', 192 ), array_keys( $response->get_data() ) ); 180 193 } 181 194 -
branches/4.9/tests/phpunit/tests/rest-api/rest-users-controller.php
r43067 r43445 762 762 $data = $this->endpoint->prepare_item_for_response( $user, $request ); 763 763 $this->check_get_user_response( $data, 'edit' ); 764 } 765 766 public function test_prepare_item_limit_fields() { 767 wp_set_current_user( self::$user ); 768 $request = new WP_REST_Request; 769 $request->set_param( 'context', 'edit' ); 770 $request->set_param( '_fields', 'id,name' ); 771 $user = get_user_by( 'id', get_current_user_id() ); 772 $response = $this->endpoint->prepare_item_for_response( $user, $request ); 773 $this->assertEquals( array( 774 'id', 775 'name', 776 ), array_keys( $response->get_data() ) ); 764 777 } 765 778
Note: See TracChangeset
for help on using the changeset viewer.