- Timestamp:
- 01/29/2020 12:43:23 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
r47050 r47122 174 174 } 175 175 176 return new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) ); 176 return new WP_Error( 177 'rest_invalid_param', 178 __( 'Invalid user parameter(s).' ), 179 array( 'status' => 400 ) 180 ); 177 181 } 178 182 … … 188 192 // Check if roles is specified in GET request and if user can list users. 189 193 if ( ! empty( $request['roles'] ) && ! current_user_can( 'list_users' ) ) { 190 return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to filter users by role.' ), array( 'status' => rest_authorization_required_code() ) ); 194 return new WP_Error( 195 'rest_user_cannot_view', 196 __( 'Sorry, you are not allowed to filter users by role.' ), 197 array( 'status' => rest_authorization_required_code() ) 198 ); 191 199 } 192 200 193 201 if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) { 194 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) ); 202 return new WP_Error( 203 'rest_forbidden_context', 204 __( 'Sorry, you are not allowed to list users.' ), 205 array( 'status' => rest_authorization_required_code() ) 206 ); 195 207 } 196 208 197 209 if ( in_array( $request['orderby'], array( 'email', 'registered_date' ), true ) && ! current_user_can( 'list_users' ) ) { 198 return new WP_Error( 'rest_forbidden_orderby', __( 'Sorry, you are not allowed to order users by this parameter.' ), array( 'status' => rest_authorization_required_code() ) ); 210 return new WP_Error( 211 'rest_forbidden_orderby', 212 __( 'Sorry, you are not allowed to order users by this parameter.' ), 213 array( 'status' => rest_authorization_required_code() ) 214 ); 199 215 } 200 216 … … 209 225 } 210 226 211 return new WP_Error( 'rest_forbidden_who', __( 'Sorry, you are not allowed to query users by this parameter.' ), array( 'status' => rest_authorization_required_code() ) ); 227 return new WP_Error( 228 'rest_forbidden_who', 229 __( 'Sorry, you are not allowed to query users by this parameter.' ), 230 array( 'status' => rest_authorization_required_code() ) 231 ); 212 232 } 213 233 … … 359 379 */ 360 380 protected function get_user( $id ) { 361 $error = new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) ); 381 $error = new WP_Error( 382 'rest_user_invalid_id', 383 __( 'Invalid user ID.' ), 384 array( 'status' => 404 ) 385 ); 386 362 387 if ( (int) $id <= 0 ) { 363 388 return $error; … … 397 422 398 423 if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) { 399 return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) ); 424 return new WP_Error( 425 'rest_user_cannot_view', 426 __( 'Sorry, you are not allowed to list users.' ), 427 array( 'status' => rest_authorization_required_code() ) 428 ); 400 429 } elseif ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) { 401 return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) ); 430 return new WP_Error( 431 'rest_user_cannot_view', 432 __( 'Sorry, you are not allowed to list users.' ), 433 array( 'status' => rest_authorization_required_code() ) 434 ); 402 435 } 403 436 … … 437 470 438 471 if ( empty( $current_user_id ) ) { 439 return new WP_Error( 'rest_not_logged_in', __( 'You are not currently logged in.' ), array( 'status' => 401 ) ); 472 return new WP_Error( 473 'rest_not_logged_in', 474 __( 'You are not currently logged in.' ), 475 array( 'status' => 401 ) 476 ); 440 477 } 441 478 … … 458 495 459 496 if ( ! current_user_can( 'create_users' ) ) { 460 return new WP_Error( 'rest_cannot_create_user', __( 'Sorry, you are not allowed to create new users.' ), array( 'status' => rest_authorization_required_code() ) ); 497 return new WP_Error( 498 'rest_cannot_create_user', 499 __( 'Sorry, you are not allowed to create new users.' ), 500 array( 'status' => rest_authorization_required_code() ) 501 ); 461 502 } 462 503 … … 474 515 public function create_item( $request ) { 475 516 if ( ! empty( $request['id'] ) ) { 476 return new WP_Error( 'rest_user_exists', __( 'Cannot create existing user.' ), array( 'status' => 400 ) ); 517 return new WP_Error( 518 'rest_user_exists', 519 __( 'Cannot create existing user.' ), 520 array( 'status' => 400 ) 521 ); 477 522 } 478 523 … … 493 538 494 539 if ( is_wp_error( $ret['errors'] ) && $ret['errors']->has_errors() ) { 495 $error = new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) ); 540 $error = new WP_Error( 541 'rest_invalid_param', 542 __( 'Invalid user parameter(s).' ), 543 array( 'status' => 400 ) 544 ); 545 496 546 foreach ( $ret['errors']->errors as $code => $messages ) { 497 547 foreach ( $messages as $message ) { 498 548 $error->add( $code, $message ); 499 549 } 550 500 551 $error_data = $error->get_error_data( $code ); 552 501 553 if ( $error_data ) { 502 554 $error->add_data( $error_data, $code ); … … 511 563 512 564 if ( ! $user_id ) { 513 return new WP_Error( 'rest_user_create', __( 'Error creating new user.' ), array( 'status' => 500 ) ); 565 return new WP_Error( 566 'rest_user_create', 567 __( 'Error creating new user.' ), 568 array( 'status' => 500 ) 569 ); 514 570 } 515 571 … … 603 659 if ( ! empty( $request['roles'] ) ) { 604 660 if ( ! current_user_can( 'promote_user', $user->ID ) ) { 605 return new WP_Error( 'rest_cannot_edit_roles', __( 'Sorry, you are not allowed to edit roles of this user.' ), array( 'status' => rest_authorization_required_code() ) ); 661 return new WP_Error( 662 'rest_cannot_edit_roles', 663 __( 'Sorry, you are not allowed to edit roles of this user.' ), 664 array( 'status' => rest_authorization_required_code() ) 665 ); 606 666 } 607 667 … … 616 676 617 677 if ( ! current_user_can( 'edit_user', $user->ID ) ) { 618 return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this user.' ), array( 'status' => rest_authorization_required_code() ) ); 678 return new WP_Error( 679 'rest_cannot_edit', 680 __( 'Sorry, you are not allowed to edit this user.' ), 681 array( 'status' => rest_authorization_required_code() ) 682 ); 619 683 } 620 684 … … 639 703 640 704 if ( ! $user ) { 641 return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) ); 705 return new WP_Error( 706 'rest_user_invalid_id', 707 __( 'Invalid user ID.' ), 708 array( 'status' => 404 ) 709 ); 642 710 } 643 711 … … 645 713 646 714 if ( $owner_id && $owner_id !== $id ) { 647 return new WP_Error( 'rest_user_invalid_email', __( 'Invalid email address.' ), array( 'status' => 400 ) ); 715 return new WP_Error( 716 'rest_user_invalid_email', 717 __( 'Invalid email address.' ), 718 array( 'status' => 400 ) 719 ); 648 720 } 649 721 650 722 if ( ! empty( $request['username'] ) && $request['username'] !== $user->user_login ) { 651 return new WP_Error( 'rest_user_invalid_argument', __( "Username isn't editable." ), array( 'status' => 400 ) ); 723 return new WP_Error( 724 'rest_user_invalid_argument', 725 __( "Username isn't editable." ), 726 array( 'status' => 400 ) 727 ); 652 728 } 653 729 654 730 if ( ! empty( $request['slug'] ) && $request['slug'] !== $user->user_nicename && get_user_by( 'slug', $request['slug'] ) ) { 655 return new WP_Error( 'rest_user_invalid_slug', __( 'Invalid slug.' ), array( 'status' => 400 ) ); 731 return new WP_Error( 732 'rest_user_invalid_slug', 733 __( 'Invalid slug.' ), 734 array( 'status' => 400 ) 735 ); 656 736 } 657 737 … … 755 835 756 836 if ( ! current_user_can( 'delete_user', $user->ID ) ) { 757 return new WP_Error( 'rest_user_cannot_delete', __( 'Sorry, you are not allowed to delete this user.' ), array( 'status' => rest_authorization_required_code() ) ); 837 return new WP_Error( 838 'rest_user_cannot_delete', 839 __( 'Sorry, you are not allowed to delete this user.' ), 840 array( 'status' => rest_authorization_required_code() ) 841 ); 758 842 } 759 843 … … 772 856 // We don't support delete requests in multisite. 773 857 if ( is_multisite() ) { 774 return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 501 ) ); 775 } 858 return new WP_Error( 859 'rest_cannot_delete', 860 __( 'The user cannot be deleted.' ), 861 array( 'status' => 501 ) 862 ); 863 } 864 776 865 $user = $this->get_user( $request['id'] ); 866 777 867 if ( is_wp_error( $user ) ) { 778 868 return $user; … … 785 875 // We don't support trashing for users. 786 876 if ( ! $force ) { 787 /* translators: %s: force=true */ 788 return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Users do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) ); 877 return new WP_Error( 878 'rest_trash_not_supported', 879 /* translators: %s: force=true */ 880 sprintf( __( "Users do not support trashing. Set '%s' to delete." ), 'force=true' ), 881 array( 'status' => 501 ) 882 ); 789 883 } 790 884 791 885 if ( ! empty( $reassign ) ) { 792 886 if ( $reassign === $id || ! get_userdata( $reassign ) ) { 793 return new WP_Error( 'rest_user_invalid_reassign', __( 'Invalid user ID for reassignment.' ), array( 'status' => 400 ) ); 887 return new WP_Error( 888 'rest_user_invalid_reassign', 889 __( 'Invalid user ID for reassignment.' ), 890 array( 'status' => 400 ) 891 ); 794 892 } 795 893 } … … 805 903 806 904 if ( ! $result ) { 807 return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 500 ) ); 905 return new WP_Error( 906 'rest_cannot_delete', 907 __( 'The user cannot be deleted.' ), 908 array( 'status' => 500 ) 909 ); 808 910 } 809 911 … … 1001 1103 $schema = $this->get_item_schema(); 1002 1104 1003 // required arguments.1105 // Required arguments. 1004 1106 if ( isset( $request['email'] ) && ! empty( $schema['properties']['email'] ) ) { 1005 1107 $prepared_user->user_email = $request['email']; … … 1014 1116 } 1015 1117 1016 // optional arguments.1118 // Optional arguments. 1017 1119 if ( isset( $request['id'] ) ) { 1018 1120 $prepared_user->ID = absint( $request['id'] ); … … 1051 1153 } 1052 1154 1053 // setting roles will be handled outside of this function.1155 // Setting roles will be handled outside of this function. 1054 1156 if ( isset( $request['roles'] ) ) { 1055 1157 $prepared_user->role = false; … … 1083 1185 1084 1186 if ( ! isset( $wp_roles->role_objects[ $role ] ) ) { 1085 /* translators: %s: Role key. */ 1086 return new WP_Error( 'rest_user_invalid_role', sprintf( __( 'The role %s does not exist.' ), $role ), array( 'status' => 400 ) ); 1187 return new WP_Error( 1188 'rest_user_invalid_role', 1189 /* translators: %s: Role key. */ 1190 sprintf( __( 'The role %s does not exist.' ), $role ), 1191 array( 'status' => 400 ) 1192 ); 1087 1193 } 1088 1194 … … 1098 1204 && ! $potential_role->has_cap( 'edit_users' ) 1099 1205 ) { 1100 return new WP_Error( 'rest_user_invalid_role', __( 'Sorry, you are not allowed to give users that role.' ), array( 'status' => rest_authorization_required_code() ) ); 1206 return new WP_Error( 1207 'rest_user_invalid_role', 1208 __( 'Sorry, you are not allowed to give users that role.' ), 1209 array( 'status' => rest_authorization_required_code() ) 1210 ); 1101 1211 } 1102 1212 … … 1108 1218 1109 1219 if ( empty( $editable_roles[ $role ] ) ) { 1110 return new WP_Error( 'rest_user_invalid_role', __( 'Sorry, you are not allowed to give users that role.' ), array( 'status' => 403 ) ); 1220 return new WP_Error( 1221 'rest_user_invalid_role', 1222 __( 'Sorry, you are not allowed to give users that role.' ), 1223 array( 'status' => 403 ) 1224 ); 1111 1225 } 1112 1226 } … … 1131 1245 1132 1246 if ( ! validate_username( $username ) ) { 1133 return new WP_Error( 'rest_user_invalid_username', __( 'Username contains invalid characters.' ), array( 'status' => 400 ) ); 1247 return new WP_Error( 1248 'rest_user_invalid_username', 1249 __( 'Username contains invalid characters.' ), 1250 array( 'status' => 400 ) 1251 ); 1134 1252 } 1135 1253 … … 1138 1256 1139 1257 if ( in_array( strtolower( $username ), array_map( 'strtolower', $illegal_logins ), true ) ) { 1140 return new WP_Error( 'rest_user_invalid_username', __( 'Sorry, that username is not allowed.' ), array( 'status' => 400 ) ); 1258 return new WP_Error( 1259 'rest_user_invalid_username', 1260 __( 'Sorry, that username is not allowed.' ), 1261 array( 'status' => 400 ) 1262 ); 1141 1263 } 1142 1264 … … 1160 1282 1161 1283 if ( empty( $password ) ) { 1162 return new WP_Error( 'rest_user_invalid_password', __( 'Passwords cannot be empty.' ), array( 'status' => 400 ) ); 1284 return new WP_Error( 1285 'rest_user_invalid_password', 1286 __( 'Passwords cannot be empty.' ), 1287 array( 'status' => 400 ) 1288 ); 1163 1289 } 1164 1290 1165 1291 if ( false !== strpos( $password, '\\' ) ) { 1166 return new WP_Error( 'rest_user_invalid_password', __( 'Passwords cannot contain the "\\" character.' ), array( 'status' => 400 ) ); 1292 return new WP_Error( 1293 'rest_user_invalid_password', 1294 __( 'Passwords cannot contain the "\\" character.' ), 1295 array( 'status' => 400 ) 1296 ); 1167 1297 } 1168 1298 … … 1339 1469 1340 1470 $this->schema = $schema; 1471 1341 1472 return $this->add_additional_fields_schema( $this->schema ); 1342 1473 }
Note: See TracChangeset
for help on using the changeset viewer.