Changeset 50150 for trunk/src/wp-includes/rest-api.php
- Timestamp:
- 02/02/2021 05:26:06 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r50065 r50150 3183 3183 return $endpoint_args; 3184 3184 } 3185 3186 3187 /** 3188 * Converts an error to a response object. 3189 * 3190 * This iterates over all error codes and messages to change it into a flat 3191 * array. This enables simpler client behaviour, as it is represented as a 3192 * list in JSON rather than an object/map. 3193 * 3194 * @since 5.7.0 3195 * 3196 * @param WP_Error $error WP_Error instance. 3197 * 3198 * @return WP_REST_Response List of associative arrays with code and message keys. 3199 */ 3200 function rest_convert_error_to_response( $error ) { 3201 $status = array_reduce( 3202 $error->get_all_error_data(), 3203 function ( $status, $error_data ) { 3204 return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status; 3205 }, 3206 500 3207 ); 3208 3209 $errors = array(); 3210 3211 foreach ( (array) $error->errors as $code => $messages ) { 3212 $all_data = $error->get_all_error_data( $code ); 3213 $last_data = array_pop( $all_data ); 3214 3215 foreach ( (array) $messages as $message ) { 3216 $formatted = array( 3217 'code' => $code, 3218 'message' => $message, 3219 'data' => $last_data, 3220 ); 3221 3222 if ( $all_data ) { 3223 $formatted['additional_data'] = $all_data; 3224 } 3225 3226 $errors[] = $formatted; 3227 } 3228 } 3229 3230 $data = $errors[0]; 3231 if ( count( $errors ) > 1 ) { 3232 // Remove the primary error. 3233 array_shift( $errors ); 3234 $data['additional_errors'] = $errors; 3235 } 3236 3237 return new WP_REST_Response( $data, $status ); 3238 }
Note: See TracChangeset
for help on using the changeset viewer.