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 var $errors = array();
32 * Stores the list of data for error codes.
38 var $error_data = array();
41 * Constructor - Sets up error message.
43 * If code parameter is empty then nothing will be done. It is possible to
44 * add multiple messages to the same code, but with other methods in the
47 * All parameters are optional, but if the code parameter is set, then the
48 * data parameter is optional.
52 * @param string|int $code Error code
53 * @param string $message Error message
54 * @param mixed $data Optional. Error data.
57 function __construct($code = '', $message = '', $data = '') {
61 $this->errors[$code][] = $message;
64 $this->error_data[$code] = $data;
68 * Retrieve all error codes.
73 * @return array List of error codes, if available.
75 function get_error_codes() {
76 if ( empty($this->errors) )
79 return array_keys($this->errors);
83 * Retrieve first error code available.
88 * @return string|int Empty string, if no error codes.
90 function get_error_code() {
91 $codes = $this->get_error_codes();
100 * Retrieve all error messages or error messages matching code.
104 * @param string|int $code Optional. Retrieve messages matching code, if exists.
105 * @return array Error strings on success, or empty array on failure (if using code parameter).
107 function get_error_messages($code = '') {
108 // Return all messages if no code specified.
109 if ( empty($code) ) {
110 $all_messages = array();
111 foreach ( (array) $this->errors as $code => $messages )
112 $all_messages = array_merge($all_messages, $messages);
114 return $all_messages;
117 if ( isset($this->errors[$code]) )
118 return $this->errors[$code];
124 * Get single error message.
126 * This will get the first message available for the code. If no code is
127 * given then the first code available will be used.
131 * @param string|int $code Optional. Error code to retrieve message.
134 function get_error_message($code = '') {
136 $code = $this->get_error_code();
137 $messages = $this->get_error_messages($code);
138 if ( empty($messages) )
144 * Retrieve error data for error code.
148 * @param string|int $code Optional. Error code.
149 * @return mixed Null, if no errors.
151 function get_error_data($code = '') {
153 $code = $this->get_error_code();
155 if ( isset($this->error_data[$code]) )
156 return $this->error_data[$code];
161 * Append more error messages to list of error messages.
166 * @param string|int $code Error code.
167 * @param string $message Error message.
168 * @param mixed $data Optional. Error data.
170 function add($code, $message, $data = '') {
171 $this->errors[$code][] = $message;
172 if ( ! empty($data) )
173 $this->error_data[$code] = $data;
177 * Add data for error code.
179 * The error code can only contain one error data.
183 * @param mixed $data Error data.
184 * @param string|int $code Error code.
186 function add_data($data, $code = '') {
188 $code = $this->get_error_code();
190 $this->error_data[$code] = $data;
195 * Check whether variable is a WordPress Error.
197 * Looks at the object and if a WP_Error class. Does not check to see if the
198 * parent is also WP_Error, so can't inherit WP_Error and still use this
203 * @param mixed $thing Check if unknown variable is WordPress Error object.
204 * @return bool True, if WP_Error. False, if not WP_Error.
206 function is_wp_error($thing) {
207 if ( is_object($thing) && is_a($thing, 'WP_Error') )