Changeset 39033
- Timestamp:
- 10/30/2016 06:20:58 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
r38974 r39033 1 1 <?php 2 3 2 /** 4 * Access terms associated with a taxonomy. 3 * REST API: WP_REST_Terms_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used to managed terms associated with a taxonomy via the REST API. 12 * 13 * @since 4.7.0 14 * 15 * @see WP_REST_Controller 5 16 */ 6 17 class WP_REST_Terms_Controller extends WP_REST_Controller { … … 9 20 * Taxonomy key. 10 21 * 22 * @since 4.7.0 11 23 * @access protected 12 24 * @var string … … 17 29 * Instance of a term meta fields object. 18 30 * 31 * @since 4.7.0 19 32 * @access protected 20 33 * @var WP_REST_Term_Meta_Fields … … 25 38 * Column to have the terms be sorted by. 26 39 * 40 * @since 4.7.0 27 41 * @access protected 28 42 * @var string … … 33 47 * Number of terms that were found. 34 48 * 49 * @since 4.7.0 35 50 * @access protected 36 51 * @var int … … 40 55 /** 41 56 * Constructor. 57 * 58 * @since 4.7.0 59 * @access public 42 60 * 43 61 * @param string $taxonomy Taxonomy key. … … 54 72 /** 55 73 * Registers the routes for the objects of the controller. 74 * 75 * @since 4.7.0 76 * @access public 77 * 78 * @see register_rest_route() 56 79 */ 57 80 public function register_routes() { … … 72 95 'schema' => array( $this, 'get_public_item_schema' ), 73 96 ) ); 97 74 98 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 75 99 array( … … 105 129 * Checks if a request has access to read terms in the specified taxonomy. 106 130 * 107 * @param WP_REST_Request $request Full details about the request. 108 * @return WP_Error|boolean 131 * @since 4.7.0 132 * @access public 133 * 134 * @param WP_REST_Request $request Full details about the request. 135 * @return bool|WP_Error True if the request has read access, otherwise false or WP_Error object. 109 136 */ 110 137 public function get_items_permissions_check( $request ) { … … 120 147 121 148 /** 122 * Gets terms associated with a taxonomy. 149 * Retrieves terms associated with a taxonomy. 150 * 151 * @since 4.7.0 152 * @access public 123 153 * 124 154 * @param WP_REST_Request $request Full details about the request. 125 * @return WP_REST_Response|WP_Error 155 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 126 156 */ 127 157 public function get_items( $request ) { … … 130 160 $registered = $this->get_collection_params(); 131 161 132 // This array defines mappings between public API query parameters whose 133 // values are accepted as-passed, and their internal WP_Query parameter 134 // name equivalents (some are the same). Only values which are also 135 // present in $registered will be set. 162 /* 163 * This array defines mappings between public API query parameters whose 164 * values are accepted as-passed, and their internal WP_Query parameter 165 * name equivalents (some are the same). Only values which are also 166 * present in $registered will be set. 167 */ 136 168 $parameter_mappings = array( 137 169 'exclude' => 'exclude', … … 148 180 $prepared_args = array(); 149 181 150 // For each known parameter which is both registered and present in the request, 151 // set the parameter's value on the query $prepared_args. 182 /* 183 * For each known parameter which is both registered and present in the request, 184 * set the parameter's value on the query $prepared_args. 185 */ 152 186 foreach ( $parameter_mappings as $api_param => $wp_param ) { 153 187 if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { … … 178 212 * Filters the query arguments before passing them to get_terms(). 179 213 * 214 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 215 * 180 216 * Enables adding extra arguments or setting defaults for a terms 181 217 * collection request. 182 218 * 183 * @see https://developer.wordpress.org/reference/functions/get_terms/ 219 * @since 4.7.0 220 * 221 * @link https://developer.wordpress.org/reference/functions/get_terms/ 184 222 * 185 223 * @param array $prepared_args Array of arguments to be … … 199 237 200 238 $count_args = $prepared_args; 239 201 240 unset( $count_args['number'], $count_args['offset'] ); 241 202 242 $total_terms = wp_count_terms( $this->taxonomy, $count_args ); 203 243 204 // wp_count_terms can return a falsy value when the term has no children 244 // wp_count_terms can return a falsy value when the term has no children. 205 245 if ( ! $total_terms ) { 206 246 $total_terms = 0; … … 208 248 209 249 $response = array(); 250 210 251 foreach ( $query_result as $term ) { 211 252 $data = $this->prepare_item_for_response( $term, $request ); … … 217 258 // Store pagination values for headers. 218 259 $per_page = (int) $prepared_args['number']; 219 $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );260 $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); 220 261 221 262 $response->header( 'X-WP-Total', (int) $total_terms ); 263 222 264 $max_pages = ceil( $total_terms / $per_page ); 265 223 266 $response->header( 'X-WP-TotalPages', (int) $max_pages ); 224 267 … … 226 269 if ( $page > 1 ) { 227 270 $prev_page = $page - 1; 271 228 272 if ( $prev_page > $max_pages ) { 229 273 $prev_page = $max_pages; 230 274 } 275 231 276 $prev_link = add_query_arg( 'page', $prev_page, $base ); 232 277 $response->link_header( 'prev', $prev_link ); … … 235 280 $next_page = $page + 1; 236 281 $next_link = add_query_arg( 'page', $next_page, $base ); 282 237 283 $response->link_header( 'next', $next_link ); 238 284 } … … 244 290 * Checks if a request has access to read the specified term. 245 291 * 246 * @param WP_REST_Request $request Full details about the request. 247 * @return WP_Error|boolean 292 * @since 4.7.0 293 * @access public 294 * 295 * @param WP_REST_Request $request Full details about the request. 296 * @return bool|WP_Error True if the request has read access for the item, otherwise false or WP_Error object. 248 297 */ 249 298 public function get_item_permissions_check( $request ) { … … 261 310 * Gets a single term from a taxonomy. 262 311 * 263 * @param WP_REST_Request $request Full details about the request 264 * @return WP_REST_Request|WP_Error 312 * @since 4.7.0 313 * @access public 314 * 315 * @param WP_REST_Request $request Full details about the request. 316 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 265 317 */ 266 318 public function get_item( $request ) { 267 319 268 320 $term = get_term( (int) $request['id'], $this->taxonomy ); 321 269 322 if ( ! $term || $term->taxonomy !== $this->taxonomy ) { 270 323 return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) ); 271 324 } 325 272 326 if ( is_wp_error( $term ) ) { 273 327 return $term; … … 282 336 * Checks if a request has access to create a term. 283 337 * 284 * @param WP_REST_Request $request Full details about the request. 285 * @return WP_Error|boolean 338 * @since 4.7.0 339 * @access public 340 * 341 * @param WP_REST_Request $request Full details about the request. 342 * @return bool|WP_Error True if the request has access to create items, false or WP_Error object otherwise. 286 343 */ 287 344 public function create_item_permissions_check( $request ) { … … 302 359 * Creates a single term in a taxonomy. 303 360 * 304 * @param WP_REST_Request $request Full details about the request 305 * @return WP_REST_Request|WP_Error 361 * @since 4.7.0 362 * @access public 363 * 364 * @param WP_REST_Request $request Full details about the request. 365 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 306 366 */ 307 367 public function create_item( $request ) { … … 322 382 $term = wp_insert_term( $prepared_term->name, $this->taxonomy, $prepared_term ); 323 383 if ( is_wp_error( $term ) ) { 324 325 384 /* 326 385 * If we're going to inform the client that the term already exists, … … 340 399 * Fires after a single term is created or updated via the REST API. 341 400 * 401 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 402 * 403 * @since 4.7.0 404 * 342 405 * @param WP_Term $term Inserted Term object. 343 406 * @param WP_REST_Request $request Request object. 344 * @param bool ean$creating True when creating term, false when updating.407 * @param bool $creating True when creating term, false when updating. 345 408 */ 346 409 do_action( "rest_insert_{$this->taxonomy}", $term, $request, true ); … … 349 412 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 350 413 $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] ); 414 351 415 if ( is_wp_error( $meta_update ) ) { 352 416 return $meta_update; … … 355 419 356 420 $fields_update = $this->update_additional_fields_for_object( $term, $request ); 421 357 422 if ( is_wp_error( $fields_update ) ) { 358 423 return $fields_update; … … 360 425 361 426 $request->set_param( 'context', 'view' ); 427 362 428 $response = $this->prepare_item_for_response( $term, $request ); 363 429 $response = rest_ensure_response( $response ); 430 364 431 $response->set_status( 201 ); 365 432 $response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) ); 433 366 434 return $response; 367 435 } … … 370 438 * Checks if a request has access to update the specified term. 371 439 * 372 * @param WP_REST_Request $request Full details about the request. 373 * @return WP_Error|boolean 440 * @since 4.7.0 441 * @access public 442 * 443 * @param WP_REST_Request $request Full details about the request. 444 * @return bool|WP_Error True if the request has access to update the item, false or WP_Error object otherwise. 374 445 */ 375 446 public function update_item_permissions_check( $request ) { … … 380 451 381 452 $term = get_term( (int) $request['id'], $this->taxonomy ); 453 382 454 if ( ! $term ) { 383 455 return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) ); … … 394 466 * Updates a single term from a taxonomy. 395 467 * 396 * @param WP_REST_Request $request Full details about the request 397 * @return WP_REST_Request|WP_Error 468 * @since 4.7.0 469 * @access public 470 * 471 * @param WP_REST_Request $request Full details about the request. 472 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 398 473 */ 399 474 public function update_item( $request ) { … … 417 492 if ( ! empty( $prepared_term ) ) { 418 493 $update = wp_update_term( $term->term_id, $term->taxonomy, (array) $prepared_term ); 494 419 495 if ( is_wp_error( $update ) ) { 420 496 return $update; … … 430 506 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 431 507 $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] ); 508 432 509 if ( is_wp_error( $meta_update ) ) { 433 510 return $meta_update; … … 436 513 437 514 $fields_update = $this->update_additional_fields_for_object( $term, $request ); 515 438 516 if ( is_wp_error( $fields_update ) ) { 439 517 return $fields_update; … … 441 519 442 520 $request->set_param( 'context', 'view' ); 521 443 522 $response = $this->prepare_item_for_response( $term, $request ); 523 444 524 return rest_ensure_response( $response ); 445 525 } … … 448 528 * Checks if a request has access to delete the specified term. 449 529 * 450 * @param WP_REST_Request $request Full details about the request. 451 * @return WP_Error|boolean 530 * @since 4.7.0 531 * @access public 532 * 533 * @param WP_REST_Request $request Full details about the request. 534 * @return bool|WP_Error True if the request has access to delete the item, otherwise false or WP_Error object. 452 535 */ 453 536 public function delete_item_permissions_check( $request ) { … … 455 538 return false; 456 539 } 540 457 541 $term = get_term( (int) $request['id'], $this->taxonomy ); 542 458 543 if ( ! $term ) { 459 544 return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) ); … … 463 548 return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you cannot delete resource.' ), array( 'status' => rest_authorization_required_code() ) ); 464 549 } 550 465 551 return true; 466 552 } … … 469 555 * Deletes a single term from a taxonomy. 470 556 * 471 * @param WP_REST_Request $request Full details about the request 472 * @return WP_REST_Response|WP_Error 557 * @since 4.7.0 558 * @access public 559 * 560 * @param WP_REST_Request $request Full details about the request. 561 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 473 562 */ 474 563 public function delete_item( $request ) { … … 482 571 483 572 $term = get_term( (int) $request['id'], $this->taxonomy ); 573 484 574 $request->set_param( 'context', 'view' ); 575 485 576 $response = $this->prepare_item_for_response( $term, $request ); 486 577 487 578 $retval = wp_delete_term( $term->term_id, $term->taxonomy ); 579 488 580 if ( ! $retval ) { 489 581 return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) ); … … 492 584 /** 493 585 * Fires after a single term is deleted via the REST API. 586 * 587 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 588 * 589 * @since 4.7.0 494 590 * 495 591 * @param WP_Term $term The deleted term. … … 505 601 * Prepares a single term for create or update. 506 602 * 603 * @since 4.7.0 604 * @access public 605 * 507 606 * @param WP_REST_Request $request Request object. 508 607 * @return object $prepared_term Term object. … … 530 629 if ( isset( $request['parent'] ) && ! empty( $schema['properties']['parent'] ) ) { 531 630 $parent_term_id = 0; 532 $parent_term = get_term( (int) $request['parent'], $this->taxonomy );631 $parent_term = get_term( (int) $request['parent'], $this->taxonomy ); 533 632 534 633 if ( $parent_term ) { … … 541 640 /** 542 641 * Filters term data before inserting term via the REST API. 642 * 643 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 644 * 645 * @since 4.7.0 543 646 * 544 647 * @param object $prepared_term Term object. … … 551 654 * Prepares a single term output for response. 552 655 * 656 * @since 4.7.0 657 * @access public 658 * 553 659 * @param obj $item Term object. 554 660 * @param WP_REST_Request $request Request object. 555 * @return WP_REST_Response $response 661 * @return WP_REST_Response $response Response object. 556 662 */ 557 663 public function prepare_item_for_response( $item, $request ) { 558 664 559 665 $schema = $this->get_item_schema(); 560 $data = array(); 666 $data = array(); 667 561 668 if ( ! empty( $schema['properties']['id'] ) ) { 562 669 $data['id'] = (int) $item->term_id; 563 670 } 671 564 672 if ( ! empty( $schema['properties']['count'] ) ) { 565 673 $data['count'] = (int) $item->count; 566 674 } 675 567 676 if ( ! empty( $schema['properties']['description'] ) ) { 568 677 $data['description'] = $item->description; 569 678 } 679 570 680 if ( ! empty( $schema['properties']['link'] ) ) { 571 681 $data['link'] = get_term_link( $item ); 572 682 } 683 573 684 if ( ! empty( $schema['properties']['name'] ) ) { 574 685 $data['name'] = $item->name; 575 686 } 687 576 688 if ( ! empty( $schema['properties']['slug'] ) ) { 577 689 $data['slug'] = $item->slug; 578 690 } 691 579 692 if ( ! empty( $schema['properties']['taxonomy'] ) ) { 580 693 $data['taxonomy'] = $item->taxonomy; 581 694 } 695 582 696 if ( ! empty( $schema['properties']['parent'] ) ) { 583 697 $data['parent'] = (int) $item->parent; 584 698 } 699 585 700 if ( ! empty( $schema['properties']['meta'] ) ) { 586 701 $data['meta'] = $this->meta->get_value( $item->term_id, $request ); … … 588 703 589 704 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 590 $data = $this->add_additional_fields_to_object( $data, $request );591 $data = $this->filter_response_by_context( $data, $context );705 $data = $this->add_additional_fields_to_object( $data, $request ); 706 $data = $this->filter_response_by_context( $data, $context ); 592 707 593 708 $response = rest_ensure_response( $data ); … … 598 713 * Filters a term item returned from the API. 599 714 * 715 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 716 * 600 717 * Allows modification of the term data right before it is returned. 718 * 719 * @since 4.7.0 601 720 * 602 721 * @param WP_REST_Response $response The response object. … … 610 729 * Prepares links for the request. 611 730 * 731 * @since 4.7.0 732 * @access protected 733 * 612 734 * @param object $term Term object. 613 735 * @return array Links for the given term. … … 629 751 if ( $term->parent ) { 630 752 $parent_term = get_term( (int) $term->parent, $term->taxonomy ); 753 631 754 if ( $parent_term ) { 632 755 $links['up'] = array( … … 638 761 639 762 $taxonomy_obj = get_taxonomy( $term->taxonomy ); 763 640 764 if ( empty( $taxonomy_obj->object_type ) ) { 641 765 return $links; … … 643 767 644 768 $post_type_links = array(); 769 645 770 foreach ( $taxonomy_obj->object_type as $type ) { 646 771 $post_type_object = get_post_type_object( $type ); 772 647 773 if ( empty( $post_type_object->show_in_rest ) ) { 648 774 continue; 649 775 } 776 650 777 $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; 651 778 $post_type_links[] = array( … … 653 780 ); 654 781 } 782 655 783 if ( ! empty( $post_type_links ) ) { 656 784 $links['https://api.w.org/post_type'] = $post_type_links; … … 661 789 662 790 /** 663 * Gets the term's schema, conforming to JSON Schema. 664 * 665 * @return array 791 * Retrieves the term's schema, conforming to JSON Schema. 792 * 793 * @since 4.7.0 794 * @access public 795 * 796 * @return array Item schema data. 666 797 */ 667 798 public function get_item_schema() { … … 724 855 ), 725 856 ); 857 726 858 $taxonomy = get_taxonomy( $this->taxonomy ); 859 727 860 if ( $taxonomy->hierarchical ) { 728 861 $schema['properties']['parent'] = array( … … 734 867 735 868 $schema['properties']['meta'] = $this->meta->get_field_schema(); 869 736 870 return $this->add_additional_fields_schema( $schema ); 737 871 } 738 872 739 873 /** 740 * Gets the query params for collections. 741 * 742 * @return array 874 * Retrieves the query params for collections. 875 * 876 * @since 4.7.0 877 * @access public 878 * 879 * @return array Collection parameters. 743 880 */ 744 881 public function get_collection_params() { … … 754 891 'sanitize_callback' => 'wp_parse_id_list', 755 892 ); 893 756 894 $query_params['include'] = array( 757 895 'description' => __( 'Limit result set to specific ids.' ), … … 760 898 'sanitize_callback' => 'wp_parse_id_list', 761 899 ); 900 762 901 if ( ! $taxonomy->hierarchical ) { 763 902 $query_params['offset'] = array( … … 768 907 ); 769 908 } 909 770 910 $query_params['order'] = array( 771 911 'description' => __( 'Order sort attribute ascending or descending.' ), … … 779 919 'validate_callback' => 'rest_validate_request_arg', 780 920 ); 921 781 922 $query_params['orderby'] = array( 782 923 'description' => __( 'Sort collection by resource attribute.' ), … … 795 936 'validate_callback' => 'rest_validate_request_arg', 796 937 ); 938 797 939 $query_params['hide_empty'] = array( 798 940 'description' => __( 'Whether to hide resources not assigned to any posts.' ), … … 802 944 'validate_callback' => 'rest_validate_request_arg', 803 945 ); 946 804 947 if ( $taxonomy->hierarchical ) { 805 948 $query_params['parent'] = array( … … 810 953 ); 811 954 } 955 812 956 $query_params['post'] = array( 813 957 'description' => __( 'Limit result set to resources assigned to a specific post.' ), … … 816 960 'validate_callback' => 'rest_validate_request_arg', 817 961 ); 962 818 963 $query_params['slug'] = array( 819 964 'description' => __( 'Limit result set to resources with a specific slug.' ), … … 821 966 'validate_callback' => 'rest_validate_request_arg', 822 967 ); 968 823 969 return $query_params; 824 970 } … … 827 973 * Checks that the taxonomy is valid. 828 974 * 975 * @since 4.7.0 976 * @access protected 977 * 829 978 * @param string $taxonomy Taxonomy to check. 830 * @return WP_Error|boolean979 * @return bool Whether the taxonomy is allowed for REST management. 831 980 */ 832 981 protected function check_is_taxonomy_allowed( $taxonomy ) {
Note: See TracChangeset
for help on using the changeset viewer.