equal
deleted
inserted
replaced
9 |
9 |
10 /** |
10 /** |
11 * WordPress Error class. |
11 * WordPress Error class. |
12 * |
12 * |
13 * Container for checking for WordPress errors and error messages. Return |
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. |
14 * WP_Error and use is_wp_error() to check if this class is returned. Many |
15 * Many core WordPress functions pass this class in the event of an error and |
15 * core WordPress functions pass this class in the event of an error and |
16 * if not handled properly will result in code errors. |
16 * if not handled properly will result in code errors. |
17 * |
17 * |
18 * @package WordPress |
|
19 * @since 2.1.0 |
18 * @since 2.1.0 |
20 */ |
19 */ |
21 class WP_Error { |
20 class WP_Error { |
22 /** |
21 /** |
23 * Stores the list of errors. |
22 * Stores the list of errors. |
64 |
63 |
65 /** |
64 /** |
66 * Retrieve all error codes. |
65 * Retrieve all error codes. |
67 * |
66 * |
68 * @since 2.1.0 |
67 * @since 2.1.0 |
69 * @access public |
|
70 * |
68 * |
71 * @return array List of error codes, if available. |
69 * @return array List of error codes, if available. |
72 */ |
70 */ |
73 public function get_error_codes() { |
71 public function get_error_codes() { |
74 if ( empty($this->errors) ) |
72 if ( empty($this->errors) ) |
79 |
77 |
80 /** |
78 /** |
81 * Retrieve first error code available. |
79 * Retrieve first error code available. |
82 * |
80 * |
83 * @since 2.1.0 |
81 * @since 2.1.0 |
84 * @access public |
|
85 * |
82 * |
86 * @return string|int Empty string, if no error codes. |
83 * @return string|int Empty string, if no error codes. |
87 */ |
84 */ |
88 public function get_error_code() { |
85 public function get_error_code() { |
89 $codes = $this->get_error_codes(); |
86 $codes = $this->get_error_codes(); |
142 * Retrieve error data for error code. |
139 * Retrieve error data for error code. |
143 * |
140 * |
144 * @since 2.1.0 |
141 * @since 2.1.0 |
145 * |
142 * |
146 * @param string|int $code Optional. Error code. |
143 * @param string|int $code Optional. Error code. |
147 * @return mixed Null, if no errors. |
144 * @return mixed Error data, if it exists. |
148 */ |
145 */ |
149 public function get_error_data($code = '') { |
146 public function get_error_data($code = '') { |
150 if ( empty($code) ) |
147 if ( empty($code) ) |
151 $code = $this->get_error_code(); |
148 $code = $this->get_error_code(); |
152 |
149 |
153 if ( isset($this->error_data[$code]) ) |
150 if ( isset($this->error_data[$code]) ) |
154 return $this->error_data[$code]; |
151 return $this->error_data[$code]; |
155 return null; |
|
156 } |
152 } |
157 |
153 |
158 /** |
154 /** |
159 * Add an error or append additional message to an existing error. |
155 * Add an error or append additional message to an existing error. |
160 * |
156 * |
161 * @since 2.1.0 |
157 * @since 2.1.0 |
162 * @access public |
|
163 * |
158 * |
164 * @param string|int $code Error code. |
159 * @param string|int $code Error code. |
165 * @param string $message Error message. |
160 * @param string $message Error message. |
166 * @param mixed $data Optional. Error data. |
161 * @param mixed $data Optional. Error data. |
167 */ |
162 */ |
201 public function remove( $code ) { |
196 public function remove( $code ) { |
202 unset( $this->errors[ $code ] ); |
197 unset( $this->errors[ $code ] ); |
203 unset( $this->error_data[ $code ] ); |
198 unset( $this->error_data[ $code ] ); |
204 } |
199 } |
205 } |
200 } |
206 |
|
207 /** |
|
208 * Check whether variable is a WordPress Error. |
|
209 * |
|
210 * Returns true if $thing is an object of the WP_Error class. |
|
211 * |
|
212 * @since 2.1.0 |
|
213 * |
|
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. |
|
216 */ |
|
217 function is_wp_error( $thing ) { |
|
218 return ( $thing instanceof WP_Error ); |
|
219 } |
|