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.
29 private $errors = array();
32 * Stores the list of data for error codes.
38 private $error_data = array();
41 * Initialize the error.
43 * If `$code` is empty, the other parameters will be ignored.
44 * When `$code` is not empty, `$message` will be used even if
45 * it is empty. The `$data` parameter will be used only if it
48 * Though the class is constructed with a single error code and
49 * message, multiple codes can be added using the `add()` method.
53 * @param string|int $code Error code
54 * @param string $message Error message
55 * @param mixed $data Optional. Error data.
58 public function __construct( $code = '', $message = '', $data = '' ) {
62 $this->errors[$code][] = $message;
65 $this->error_data[$code] = $data;
69 * Make private properties readable for backwards compatibility.
74 * @param string $name Property to get.
75 * @return mixed Property.
77 public function __get( $name ) {
82 * Make private properties settable for backwards compatibility.
87 * @param string $name Property to set.
88 * @param mixed $value Property value.
89 * @return mixed Newly-set property.
91 public function __set( $name, $value ) {
92 return $this->$name = $value;
96 * Make private properties checkable for backwards compatibility.
101 * @param string $name Property to check if set.
102 * @return bool Whether the property is set.
104 public function __isset( $name ) {
105 return isset( $this->$name );
109 * Make private properties un-settable for backwards compatibility.
114 * @param string $name Property to unset.
116 public function __unset( $name ) {
117 unset( $this->$name );
121 * Retrieve all error codes.
126 * @return array List of error codes, if available.
128 public function get_error_codes() {
129 if ( empty($this->errors) )
132 return array_keys($this->errors);
136 * Retrieve first error code available.
141 * @return string|int Empty string, if no error codes.
143 public function get_error_code() {
144 $codes = $this->get_error_codes();
153 * Retrieve all error messages or error messages matching code.
157 * @param string|int $code Optional. Retrieve messages matching code, if exists.
158 * @return array Error strings on success, or empty array on failure (if using code parameter).
160 public function get_error_messages($code = '') {
161 // Return all messages if no code specified.
162 if ( empty($code) ) {
163 $all_messages = array();
164 foreach ( (array) $this->errors as $code => $messages )
165 $all_messages = array_merge($all_messages, $messages);
167 return $all_messages;
170 if ( isset($this->errors[$code]) )
171 return $this->errors[$code];
177 * Get single error message.
179 * This will get the first message available for the code. If no code is
180 * given then the first code available will be used.
184 * @param string|int $code Optional. Error code to retrieve message.
187 public function get_error_message($code = '') {
189 $code = $this->get_error_code();
190 $messages = $this->get_error_messages($code);
191 if ( empty($messages) )
197 * Retrieve error data for error code.
201 * @param string|int $code Optional. Error code.
202 * @return mixed Null, if no errors.
204 public function get_error_data($code = '') {
206 $code = $this->get_error_code();
208 if ( isset($this->error_data[$code]) )
209 return $this->error_data[$code];
214 * Add an error or append additional message to an existing error.
219 * @param string|int $code Error code.
220 * @param string $message Error message.
221 * @param mixed $data Optional. Error data.
223 public function add($code, $message, $data = '') {
224 $this->errors[$code][] = $message;
225 if ( ! empty($data) )
226 $this->error_data[$code] = $data;
230 * Add data for error code.
232 * The error code can only contain one error data.
236 * @param mixed $data Error data.
237 * @param string|int $code Error code.
239 public function add_data($data, $code = '') {
241 $code = $this->get_error_code();
243 $this->error_data[$code] = $data;
248 * Check whether variable is a WordPress Error.
250 * Returns true if $thing is an object of the WP_Error class.
254 * @param mixed $thing Check if unknown variable is a WP_Error object.
255 * @return bool True, if WP_Error. False, if not WP_Error.
257 function is_wp_error($thing) {
258 if ( is_object($thing) && is_a($thing, 'WP_Error') )