Ticket #38398: 38398.patch
File 38398.patch, 9.4 KB (added by , 7 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
1 1 <?php 2 /** 3 * REST API: WP_REST_Users_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 2 9 3 10 /** 4 11 * Access users … … 8 15 /** 9 16 * Instance of a user meta fields object. 10 17 * 18 * @since 4.7.0 11 19 * @access protected 12 20 * @var WP_REST_User_Meta_Fields 13 21 */ 14 22 protected $meta; 15 23 24 /** 25 * Constructor. 26 * 27 * @since 4.7.0 28 */ 16 29 public function __construct() { 17 30 $this->namespace = 'wp/v2'; 18 31 $this->rest_base = 'users'; … … 21 34 } 22 35 23 36 /** 24 * Register the routes for the objects of the controller. 37 * Registers the routes for the objects of the controller. 38 * 39 * @since 4.7.0 25 40 */ 26 41 public function register_routes() { 27 42 … … 83 98 /** 84 99 * Permissions check for getting all users. 85 100 * 101 * @since 4.7.0 102 * 86 103 * @param WP_REST_Request $request Full details about the request. 87 104 * @return WP_Error|boolean 88 105 */ … … 104 121 } 105 122 106 123 /** 107 * Get all users124 * Gets all users 108 125 * 126 * @since 4.7.0 127 * 109 128 * @param WP_REST_Request $request Full details about the request. 110 129 * @return WP_Error|WP_REST_Response 111 130 */ … … 170 189 } 171 190 172 191 /** 173 * Filter arguments, before passing to WP_User_Query, when querying users via the REST API.192 * Filters arguments, before passing to WP_User_Query, when querying users via the REST API. 174 193 * 194 * @since 4.7.0 195 * 175 196 * @see https://developer.wordpress.org/reference/classes/wp_user_query/ 176 197 * 177 198 * @param array $prepared_args Array of arguments for WP_User_Query. … … 197 218 198 219 $total_users = $query->get_total(); 199 220 if ( $total_users < 1 ) { 200 // Out-of-bounds, run the query again without LIMIT for total count 221 // Out-of-bounds, run the query again without LIMIT for total count. 201 222 unset( $prepared_args['number'], $prepared_args['offset'] ); 202 223 $count_query = new WP_User_Query( $prepared_args ); 203 224 $total_users = $count_query->get_total(); … … 225 246 } 226 247 227 248 /** 228 * Check if a given request has access to read a user249 * Checks if a given request has access to read a user 229 250 * 251 * @since 4.7.0 252 * 230 253 * @param WP_REST_Request $request Full details about the request. 231 254 * @return WP_Error|boolean 232 255 */ … … 254 277 } 255 278 256 279 /** 257 * Get a single user280 * Gets a single user 258 281 * 282 * @since 4.7.0 283 * 259 284 * @param WP_REST_Request $request Full details about the request. 260 285 * @return WP_Error|WP_REST_Response 261 286 */ … … 274 299 } 275 300 276 301 /** 277 * Get the current user302 * Gets the current user 278 303 * 304 * @since 4.7.0 305 * 279 306 * @param WP_REST_Request $request Full details about the request. 280 307 * @return WP_Error|WP_REST_Response 281 308 */ … … 295 322 } 296 323 297 324 /** 298 * Check if a given request has access create users325 * Checks if a given request has access create users 299 326 * 327 * @since 4.7.0 328 * 300 329 * @param WP_REST_Request $request Full details about the request. 301 330 * @return WP_Error|boolean 302 331 */ … … 310 339 } 311 340 312 341 /** 313 * Create a single user342 * Creates a single user 314 343 * 344 * @since 4.7.0 345 * 315 346 * @param WP_REST_Request $request Full details about the request. 316 347 * @return WP_Error|WP_REST_Response 317 348 */ … … 375 406 /** 376 407 * Fires after a user is created or updated via the REST API. 377 408 * 409 * @since 4.7.0 410 * 378 411 * @param WP_User $user Data used to create the user. 379 412 * @param WP_REST_Request $request Request object. 380 413 * @param boolean $creating True when creating user, false when updating user. … … 391 424 } 392 425 393 426 /** 394 * Check if a given request has access update a user427 * Checks if a given request has access update a user 395 428 * 429 * @since 4.7.0 430 * 396 431 * @param WP_REST_Request $request Full details about the request. 397 432 * @return WP_Error|boolean 398 433 */ … … 412 447 } 413 448 414 449 /** 415 * Update a single user450 * Updates a single user 416 451 * 452 * @since 4.7.0 453 * 417 454 * @param WP_REST_Request $request Full details about the request. 418 455 * @return WP_Error|WP_REST_Response 419 456 */ … … 446 483 447 484 $user = $this->prepare_item_for_database( $request ); 448 485 449 // Ensure we're operating on the same user we already checked 486 // Ensure we're operating on the same user we already checked. 450 487 $user->ID = $id; 451 488 452 489 $user_id = wp_update_user( $user ); … … 482 519 } 483 520 484 521 /** 485 * Check if a given request has access delete a user522 * Checks if a given request has access delete a user 486 523 * 524 * @since 4.7.0 525 * 487 526 * @param WP_REST_Request $request Full details about the request. 488 527 * @return WP_Error|boolean 489 528 */ … … 499 538 } 500 539 501 540 /** 502 * Delete a single user541 * Deletes a single user 503 542 * 543 * @since 4.7.0 544 * 504 545 * @param WP_REST_Request $request Full details about the request. 505 546 * @return WP_Error|WP_REST_Response 506 547 */ … … 509 550 $reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null; 510 551 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 511 552 512 // We don't support trashing for this type, error out 553 // We don't support trashing for this type, error out. 513 554 if ( ! $force ) { 514 555 return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing.' ), array( 'status' => 501 ) ); 515 556 } … … 540 581 /** 541 582 * Fires after a user is deleted via the REST API. 542 583 * 584 * @since 4.7.0 585 * 543 586 * @param WP_User $user The user data. 544 587 * @param WP_REST_Response $response The response returned from the API. 545 588 * @param WP_REST_Request $request The request sent to the API. … … 550 593 } 551 594 552 595 /** 553 * Prepare a single user output for response596 * Prepares a single user output for response 554 597 * 555 * @param object $user User object. 598 * @since 4.7.0 599 * 600 * @param object $user User object. 556 601 * @param WP_REST_Request $request Request object. 557 602 * @return WP_REST_Response $response Response data. 558 603 */ … … 634 679 $data = $this->add_additional_fields_to_object( $data, $request ); 635 680 $data = $this->filter_response_by_context( $data, $context ); 636 681 637 // Wrap the data in a response object 682 // Wrap the data in a response object. 638 683 $response = rest_ensure_response( $data ); 639 684 640 685 $response->add_links( $this->prepare_links( $user ) ); 641 686 642 687 /** 643 * Filter user data returned from the REST API.688 * Filters user data returned from the REST API. 644 689 * 690 * @since 4.7.0 691 * 645 692 * @param WP_REST_Response $response The response object. 646 693 * @param object $user User object used to create response. 647 694 * @param WP_REST_Request $request Request object. … … 650 697 } 651 698 652 699 /** 653 * Prepare links for the request.700 * Prepares links for the request. 654 701 * 702 * @since 4.7.0 703 * 655 704 * @param WP_Post $user User object. 656 705 * @return array Links for the given user. 657 706 */ … … 669 718 } 670 719 671 720 /** 672 * Prepare a single user for create or update721 * Prepares a single user for create or update 673 722 * 723 * @since 4.7.0 724 * 674 725 * @param WP_REST_Request $request Request object. 675 726 * @return object $prepared_user User object. 676 727 */ … … 723 774 } 724 775 725 776 /** 726 * Filter user data before inserting user via the REST API.777 * Filters user data before inserting user via the REST API. 727 778 * 779 * @since 4.7.0 780 * 728 781 * @param object $prepared_user User object. 729 782 * @param WP_REST_Request $request Request object. 730 783 */ … … 732 785 } 733 786 734 787 /** 735 * Determine if the current user is allowed to make the desired roles change.788 * Determines if the current user is allowed to make the desired roles change. 736 789 * 790 * @since 4.7.0 791 * 737 792 * @param integer $user_id User ID. 738 793 * @param array $roles New user roles. 739 794 * @return WP_Error|boolean … … 754 809 return new WP_Error( 'rest_user_invalid_role', __( 'You cannot give resource that role.' ), array( 'status' => rest_authorization_required_code() ) ); 755 810 } 756 811 757 // The new role must be editable by the logged-in user.758 759 812 /** Include admin functions to get access to get_editable_roles() */ 760 813 require_once ABSPATH . 'wp-admin/includes/admin.php'; 761 814 815 // The new role must be editable by the logged-in user. 762 816 $editable_roles = get_editable_roles(); 763 817 if ( empty( $editable_roles[ $role ] ) ) { 764 818 return new WP_Error( 'rest_user_invalid_role', __( 'You cannot give resource that role.' ), array( 'status' => 403 ) ); … … 766 820 } 767 821 768 822 return true; 769 770 823 } 771 824 772 825 /** 773 * Get the User's schema, conforming to JSON Schema826 * Gets the User's schema, conforming to JSON Schema 774 827 * 828 * @since 4.7.0 829 * 775 830 * @return array 776 831 */ 777 832 public function get_item_schema() { … … 878 933 'password' => array( 879 934 'description' => __( 'Password for the resource (never included).' ), 880 935 'type' => 'string', 881 'context' => array(), // Password is never displayed 936 'context' => array(), // Password is never displayed. 882 937 'required' => true, 883 938 ), 884 939 'capabilities' => array( … … 924 979 } 925 980 926 981 /** 927 * Get the query params for collections982 * Gets the query params for collections 928 983 * 984 * @since 4.7.0 985 * 929 986 * @return array 930 987 */ 931 988 public function get_collection_params() {