5 * Contains the WP_Error class and the is_wp_error() function.
11 * WordPress Error class.
13 * Container for checking for WordPress errors and error messages. Return
14 * WP_Error and use {@link is_wp_error()} to check if this class is returned.
15 * Many core WordPress functions pass this class in the event of an error and
16 * if not handled properly will result in code errors.
23 * Stores the list of errors.
28 public $errors = array();
31 * Stores the list of data for error codes.
36 public $error_data = array();
39 * Initialize the error.
41 * If `$code` is empty, the other parameters will be ignored.
42 * When `$code` is not empty, `$message` will be used even if
43 * it is empty. The `$data` parameter will be used only if it
46 * Though the class is constructed with a single error code and
47 * message, multiple codes can be added using the `add()` method.
51 * @param string|int $code Error code
52 * @param string $message Error message
53 * @param mixed $data Optional. Error data.
55 public function __construct( $code = '', $message = '', $data = '' ) {
59 $this->errors[$code][] = $message;
62 $this->error_data[$code] = $data;
66 * Retrieve all error codes.
71 * @return array List of error codes, if available.
73 public function get_error_codes() {
74 if ( empty($this->errors) )
77 return array_keys($this->errors);
81 * Retrieve first error code available.
86 * @return string|int Empty string, if no error codes.
88 public function get_error_code() {
89 $codes = $this->get_error_codes();
98 * Retrieve all error messages or error messages matching code.
102 * @param string|int $code Optional. Retrieve messages matching code, if exists.
103 * @return array Error strings on success, or empty array on failure (if using code parameter).
105 public function get_error_messages($code = '') {
106 // Return all messages if no code specified.
107 if ( empty($code) ) {
108 $all_messages = array();
109 foreach ( (array) $this->errors as $code => $messages )
110 $all_messages = array_merge($all_messages, $messages);
112 return $all_messages;
115 if ( isset($this->errors[$code]) )
116 return $this->errors[$code];
122 * Get single error message.
124 * This will get the first message available for the code. If no code is
125 * given then the first code available will be used.
129 * @param string|int $code Optional. Error code to retrieve message.
132 public function get_error_message($code = '') {
134 $code = $this->get_error_code();
135 $messages = $this->get_error_messages($code);
136 if ( empty($messages) )
142 * Retrieve error data for error code.
146 * @param string|int $code Optional. Error code.
147 * @return mixed Null, if no errors.
149 public function get_error_data($code = '') {
151 $code = $this->get_error_code();
153 if ( isset($this->error_data[$code]) )
154 return $this->error_data[$code];
159 * Add an error or append additional message to an existing error.
164 * @param string|int $code Error code.
165 * @param string $message Error message.
166 * @param mixed $data Optional. Error data.
168 public function add($code, $message, $data = '') {
169 $this->errors[$code][] = $message;
170 if ( ! empty($data) )
171 $this->error_data[$code] = $data;
175 * Add data for error code.
177 * The error code can only contain one error data.
181 * @param mixed $data Error data.
182 * @param string|int $code Error code.
184 public function add_data($data, $code = '') {
186 $code = $this->get_error_code();
188 $this->error_data[$code] = $data;
192 * Removes the specified error.
194 * This function removes all error messages associated with the specified
195 * error code, along with any error data for that code.
199 * @param string|int $code Error code.
201 public function remove( $code ) {
202 unset( $this->errors[ $code ] );
203 unset( $this->error_data[ $code ] );
208 * Check whether variable is a WordPress Error.
210 * Returns true if $thing is an object of the WP_Error class.
214 * @param mixed $thing Check if unknown variable is a WP_Error object.
215 * @return bool True, if WP_Error. False, if not WP_Error.
217 function is_wp_error( $thing ) {
218 return ( $thing instanceof WP_Error );