Changeset 49115 for trunk/src/wp-includes/class-wp-error.php
- Timestamp:
- 10/09/2020 10:20:50 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-error.php
r49022 r49115 2 2 /** 3 3 * WordPress Error API. 4 *5 * Contains the WP_Error class and the is_wp_error() function.6 4 * 7 5 * @package WordPress … … 28 26 29 27 /** 30 * Stores the list of data for error codes.28 * Stores the most-recently added data for each error code. 31 29 * 32 30 * @since 2.1.0 … … 36 34 37 35 /** 38 * Initialize the error. 36 * Stores previously added data added for error codes, oldest-to-newest by code. 37 * 38 * @since 5.6.0 39 * @var array[] 40 */ 41 protected $additional_data = array(); 42 43 /** 44 * Initializes the error. 39 45 * 40 46 * If `$code` is empty, the other parameters will be ignored. … … 61 67 62 68 /** 63 * Retrieve all error codes.69 * Retrieves all error codes. 64 70 * 65 71 * @since 2.1.0 … … 76 82 77 83 /** 78 * Retrieve first error code available.84 * Retrieves the first error code available. 79 85 * 80 86 * @since 2.1.0 … … 93 99 94 100 /** 95 * Retrieve all error messages or error messages matchingcode.101 * Retrieves all error messages, or the error messages for the given error code. 96 102 * 97 103 * @since 2.1.0 98 104 * 99 105 * @param string|int $code Optional. Retrieve messages matching code, if exists. 100 * @return array Error strings on success, or empty array on failure (if using code parameter).106 * @return array Error strings on success, or empty array if there are none. 101 107 */ 102 108 public function get_error_messages( $code = '' ) { … … 119 125 120 126 /** 121 * Get single error message.127 * Gets a single error message. 122 128 * 123 129 * This will get the first message available for the code. If no code is … … 127 133 * 128 134 * @param string|int $code Optional. Error code to retrieve message. 129 * @return string 135 * @return string The error message. 130 136 */ 131 137 public function get_error_message( $code = '' ) { … … 141 147 142 148 /** 143 * Retrieve error data forerror code.149 * Retrieves the most-recently added error data for an error code. 144 150 * 145 151 * @since 2.1.0 … … 159 165 160 166 /** 161 * Verif yif the instance contains errors.167 * Verifies if the instance contains errors. 162 168 * 163 169 * @since 5.1.0 164 170 * 165 * @return bool 171 * @return bool If the instance contains errors. 166 172 */ 167 173 public function has_errors() { … … 202 208 203 209 /** 204 * Add data for error code. 205 * 206 * The error code can only contain one error data. 207 * 208 * @since 2.1.0 210 * Adds data to an error with the given code. 211 * 212 * @since 2.1.0 213 * @since 5.6.0 Errors can now contain more than one item of error data. {@see WP_Error::$additional_data}. 209 214 * 210 215 * @param mixed $data Error data. … … 216 221 } 217 222 223 if ( isset( $this->error_data[ $code ] ) ) { 224 $this->additional_data[ $code ][] = $this->error_data[ $code ]; 225 } 226 218 227 $this->error_data[ $code ] = $data; 228 } 229 230 /** 231 * Retrieves all error data for an error code in the order in which the data was added. 232 * 233 * @since 5.6.0 234 * 235 * @param string|int $code Error code. 236 * @return mixed[] Array of error data, if it exists. 237 */ 238 public function get_all_error_data( $code = '' ) { 239 if ( empty( $code ) ) { 240 $code = $this->get_error_code(); 241 } 242 243 $data = array(); 244 245 if ( isset( $this->additional_data[ $code ] ) ) { 246 $data = $this->additional_data[ $code ]; 247 } 248 249 if ( isset( $this->error_data[ $code ] ) ) { 250 $data[] = $this->error_data[ $code ]; 251 } 252 253 return $data; 219 254 } 220 255 … … 232 267 unset( $this->errors[ $code ] ); 233 268 unset( $this->error_data[ $code ] ); 269 unset( $this->additional_data[ $code ] ); 270 } 271 272 /** 273 * Merges the errors in the given error object into this one. 274 * 275 * @since 5.6.0 276 * 277 * @param WP_Error $error Error object to merge. 278 */ 279 public function merge_from( WP_Error $error ) { 280 static::copy_errors( $error, $this ); 281 } 282 283 /** 284 * Exports the errors in this object into the given one. 285 * 286 * @since 5.6.0 287 * 288 * @param WP_Error $error Error object to export into. 289 */ 290 public function export_to( WP_Error $error ) { 291 static::copy_errors( $this, $error ); 292 } 293 294 /** 295 * Copies errors from one WP_Error instance to another. 296 * 297 * @since 5.6.0 298 * 299 * @param WP_Error $from From. 300 * @param WP_Error $to To. 301 */ 302 protected static function copy_errors( WP_Error $from, WP_Error $to ) { 303 foreach ( $from->get_error_codes() as $code ) { 304 foreach ( $from->get_error_messages( $code ) as $error_message ) { 305 $to->add( $code, $error_message ); 306 } 307 308 foreach ( $from->get_all_error_data( $code ) as $data ) { 309 $to->add_data( $data, $code ); 310 } 311 } 234 312 } 235 313 }
Note: See TracChangeset
for help on using the changeset viewer.