Make WordPress Core


Ignore:
Timestamp:
01/29/2020 12:43:23 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Docs: Improve inline comments per the documentation standards.

Includes minor code layout fixes for better readability.

See #48303.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r47050 r47122  
    174174        }
    175175
    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        );
    177181    }
    178182
     
    188192        // Check if roles is specified in GET request and if user can list users.
    189193        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            );
    191199        }
    192200
    193201        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            );
    195207        }
    196208
    197209        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            );
    199215        }
    200216
     
    209225            }
    210226
    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            );
    212232        }
    213233
     
    359379     */
    360380    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
    362387        if ( (int) $id <= 0 ) {
    363388            return $error;
     
    397422
    398423        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            );
    400429        } 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            );
    402435        }
    403436
     
    437470
    438471        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            );
    440477        }
    441478
     
    458495
    459496        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            );
    461502        }
    462503
     
    474515    public function create_item( $request ) {
    475516        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            );
    477522        }
    478523
     
    493538
    494539            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
    496546                foreach ( $ret['errors']->errors as $code => $messages ) {
    497547                    foreach ( $messages as $message ) {
    498548                        $error->add( $code, $message );
    499549                    }
     550
    500551                    $error_data = $error->get_error_data( $code );
     552
    501553                    if ( $error_data ) {
    502554                        $error->add_data( $error_data, $code );
     
    511563
    512564            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                );
    514570            }
    515571
     
    603659        if ( ! empty( $request['roles'] ) ) {
    604660            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                );
    606666            }
    607667
     
    616676
    617677        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            );
    619683        }
    620684
     
    639703
    640704        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            );
    642710        }
    643711
     
    645713
    646714        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            );
    648720        }
    649721
    650722        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            );
    652728        }
    653729
    654730        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            );
    656736        }
    657737
     
    755835
    756836        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            );
    758842        }
    759843
     
    772856        // We don't support delete requests in multisite.
    773857        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
    776865        $user = $this->get_user( $request['id'] );
     866
    777867        if ( is_wp_error( $user ) ) {
    778868            return $user;
     
    785875        // We don't support trashing for users.
    786876        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            );
    789883        }
    790884
    791885        if ( ! empty( $reassign ) ) {
    792886            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                );
    794892            }
    795893        }
     
    805903
    806904        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            );
    808910        }
    809911
     
    10011103        $schema = $this->get_item_schema();
    10021104
    1003         // required arguments.
     1105        // Required arguments.
    10041106        if ( isset( $request['email'] ) && ! empty( $schema['properties']['email'] ) ) {
    10051107            $prepared_user->user_email = $request['email'];
     
    10141116        }
    10151117
    1016         // optional arguments.
     1118        // Optional arguments.
    10171119        if ( isset( $request['id'] ) ) {
    10181120            $prepared_user->ID = absint( $request['id'] );
     
    10511153        }
    10521154
    1053         // setting roles will be handled outside of this function.
     1155        // Setting roles will be handled outside of this function.
    10541156        if ( isset( $request['roles'] ) ) {
    10551157            $prepared_user->role = false;
     
    10831185
    10841186            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                );
    10871193            }
    10881194
     
    10981204                && ! $potential_role->has_cap( 'edit_users' )
    10991205            ) {
    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                );
    11011211            }
    11021212
     
    11081218
    11091219            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                );
    11111225            }
    11121226        }
     
    11311245
    11321246        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            );
    11341252        }
    11351253
     
    11381256
    11391257        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            );
    11411263        }
    11421264
     
    11601282
    11611283        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            );
    11631289        }
    11641290
    11651291        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            );
    11671297        }
    11681298
     
    13391469
    13401470        $this->schema = $schema;
     1471
    13411472        return $this->add_additional_fields_schema( $this->schema );
    13421473    }
Note: See TracChangeset for help on using the changeset viewer.