author | ymh <ymh.work@gmail.com> |
Tue, 15 Oct 2019 15:48:13 +0200 | |
changeset 13 | d255fe9cd479 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Error API. |
|
4 |
* |
|
5 |
* Contains the WP_Error class and the is_wp_error() function. |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
*/ |
|
9 |
||
10 |
/** |
|
11 |
* WordPress Error class. |
|
12 |
* |
|
13 |
* Container for checking for WordPress errors and error messages. Return |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* WP_Error and use is_wp_error() to check if this class is returned. Many |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* core WordPress functions pass this class in the event of an error and |
0 | 16 |
* if not handled properly will result in code errors. |
17 |
* |
|
18 |
* @since 2.1.0 |
|
19 |
*/ |
|
20 |
class WP_Error { |
|
21 |
/** |
|
22 |
* Stores the list of errors. |
|
23 |
* |
|
24 |
* @since 2.1.0 |
|
25 |
* @var array |
|
26 |
*/ |
|
5 | 27 |
public $errors = array(); |
0 | 28 |
|
29 |
/** |
|
30 |
* Stores the list of data for error codes. |
|
31 |
* |
|
32 |
* @since 2.1.0 |
|
33 |
* @var array |
|
34 |
*/ |
|
5 | 35 |
public $error_data = array(); |
0 | 36 |
|
37 |
/** |
|
5 | 38 |
* Initialize the error. |
0 | 39 |
* |
5 | 40 |
* If `$code` is empty, the other parameters will be ignored. |
41 |
* When `$code` is not empty, `$message` will be used even if |
|
42 |
* it is empty. The `$data` parameter will be used only if it |
|
43 |
* is not empty. |
|
0 | 44 |
* |
5 | 45 |
* Though the class is constructed with a single error code and |
46 |
* message, multiple codes can be added using the `add()` method. |
|
0 | 47 |
* |
48 |
* @since 2.1.0 |
|
49 |
* |
|
50 |
* @param string|int $code Error code |
|
51 |
* @param string $message Error message |
|
52 |
* @param mixed $data Optional. Error data. |
|
53 |
*/ |
|
5 | 54 |
public function __construct( $code = '', $message = '', $data = '' ) { |
9 | 55 |
if ( empty( $code ) ) { |
0 | 56 |
return; |
9 | 57 |
} |
0 | 58 |
|
9 | 59 |
$this->errors[ $code ][] = $message; |
0 | 60 |
|
9 | 61 |
if ( ! empty( $data ) ) { |
62 |
$this->error_data[ $code ] = $data; |
|
63 |
} |
|
0 | 64 |
} |
65 |
||
66 |
/** |
|
67 |
* Retrieve all error codes. |
|
68 |
* |
|
69 |
* @since 2.1.0 |
|
70 |
* |
|
71 |
* @return array List of error codes, if available. |
|
72 |
*/ |
|
5 | 73 |
public function get_error_codes() { |
9 | 74 |
if ( ! $this->has_errors() ) { |
0 | 75 |
return array(); |
9 | 76 |
} |
0 | 77 |
|
9 | 78 |
return array_keys( $this->errors ); |
0 | 79 |
} |
80 |
||
81 |
/** |
|
82 |
* Retrieve first error code available. |
|
83 |
* |
|
84 |
* @since 2.1.0 |
|
85 |
* |
|
86 |
* @return string|int Empty string, if no error codes. |
|
87 |
*/ |
|
5 | 88 |
public function get_error_code() { |
0 | 89 |
$codes = $this->get_error_codes(); |
90 |
||
9 | 91 |
if ( empty( $codes ) ) { |
0 | 92 |
return ''; |
9 | 93 |
} |
0 | 94 |
|
95 |
return $codes[0]; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Retrieve all error messages or error messages matching code. |
|
100 |
* |
|
101 |
* @since 2.1.0 |
|
102 |
* |
|
103 |
* @param string|int $code Optional. Retrieve messages matching code, if exists. |
|
104 |
* @return array Error strings on success, or empty array on failure (if using code parameter). |
|
105 |
*/ |
|
9 | 106 |
public function get_error_messages( $code = '' ) { |
0 | 107 |
// Return all messages if no code specified. |
9 | 108 |
if ( empty( $code ) ) { |
0 | 109 |
$all_messages = array(); |
9 | 110 |
foreach ( (array) $this->errors as $code => $messages ) { |
111 |
$all_messages = array_merge( $all_messages, $messages ); |
|
112 |
} |
|
0 | 113 |
|
114 |
return $all_messages; |
|
115 |
} |
|
116 |
||
9 | 117 |
if ( isset( $this->errors[ $code ] ) ) { |
118 |
return $this->errors[ $code ]; |
|
119 |
} else { |
|
0 | 120 |
return array(); |
9 | 121 |
} |
0 | 122 |
} |
123 |
||
124 |
/** |
|
125 |
* Get single error message. |
|
126 |
* |
|
127 |
* This will get the first message available for the code. If no code is |
|
128 |
* given then the first code available will be used. |
|
129 |
* |
|
130 |
* @since 2.1.0 |
|
131 |
* |
|
132 |
* @param string|int $code Optional. Error code to retrieve message. |
|
133 |
* @return string |
|
134 |
*/ |
|
9 | 135 |
public function get_error_message( $code = '' ) { |
136 |
if ( empty( $code ) ) { |
|
0 | 137 |
$code = $this->get_error_code(); |
9 | 138 |
} |
139 |
$messages = $this->get_error_messages( $code ); |
|
140 |
if ( empty( $messages ) ) { |
|
0 | 141 |
return ''; |
9 | 142 |
} |
0 | 143 |
return $messages[0]; |
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Retrieve error data for error code. |
|
148 |
* |
|
149 |
* @since 2.1.0 |
|
150 |
* |
|
151 |
* @param string|int $code Optional. Error code. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* @return mixed Error data, if it exists. |
0 | 153 |
*/ |
9 | 154 |
public function get_error_data( $code = '' ) { |
155 |
if ( empty( $code ) ) { |
|
0 | 156 |
$code = $this->get_error_code(); |
9 | 157 |
} |
0 | 158 |
|
9 | 159 |
if ( isset( $this->error_data[ $code ] ) ) { |
160 |
return $this->error_data[ $code ]; |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Verify if the instance contains errors. |
|
166 |
* |
|
167 |
* @since 5.1.0 |
|
168 |
* |
|
169 |
* @return bool |
|
170 |
*/ |
|
171 |
public function has_errors() { |
|
172 |
if ( ! empty( $this->errors ) ) { |
|
173 |
return true; |
|
174 |
} |
|
175 |
return false; |
|
0 | 176 |
} |
177 |
||
178 |
/** |
|
5 | 179 |
* Add an error or append additional message to an existing error. |
0 | 180 |
* |
181 |
* @since 2.1.0 |
|
182 |
* |
|
183 |
* @param string|int $code Error code. |
|
184 |
* @param string $message Error message. |
|
185 |
* @param mixed $data Optional. Error data. |
|
186 |
*/ |
|
9 | 187 |
public function add( $code, $message, $data = '' ) { |
188 |
$this->errors[ $code ][] = $message; |
|
189 |
if ( ! empty( $data ) ) { |
|
190 |
$this->error_data[ $code ] = $data; |
|
191 |
} |
|
0 | 192 |
} |
193 |
||
194 |
/** |
|
195 |
* Add data for error code. |
|
196 |
* |
|
197 |
* The error code can only contain one error data. |
|
198 |
* |
|
199 |
* @since 2.1.0 |
|
200 |
* |
|
201 |
* @param mixed $data Error data. |
|
202 |
* @param string|int $code Error code. |
|
203 |
*/ |
|
9 | 204 |
public function add_data( $data, $code = '' ) { |
205 |
if ( empty( $code ) ) { |
|
0 | 206 |
$code = $this->get_error_code(); |
9 | 207 |
} |
0 | 208 |
|
9 | 209 |
$this->error_data[ $code ] = $data; |
0 | 210 |
} |
5 | 211 |
|
212 |
/** |
|
213 |
* Removes the specified error. |
|
214 |
* |
|
215 |
* This function removes all error messages associated with the specified |
|
216 |
* error code, along with any error data for that code. |
|
217 |
* |
|
218 |
* @since 4.1.0 |
|
219 |
* |
|
220 |
* @param string|int $code Error code. |
|
221 |
*/ |
|
222 |
public function remove( $code ) { |
|
223 |
unset( $this->errors[ $code ] ); |
|
224 |
unset( $this->error_data[ $code ] ); |
|
225 |
} |
|
0 | 226 |
} |