author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
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 = '' ) { |
0 | 55 |
if ( empty($code) ) |
56 |
return; |
|
57 |
||
58 |
$this->errors[$code][] = $message; |
|
59 |
||
60 |
if ( ! empty($data) ) |
|
61 |
$this->error_data[$code] = $data; |
|
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* Retrieve all error codes. |
|
66 |
* |
|
67 |
* @since 2.1.0 |
|
68 |
* |
|
69 |
* @return array List of error codes, if available. |
|
70 |
*/ |
|
5 | 71 |
public function get_error_codes() { |
0 | 72 |
if ( empty($this->errors) ) |
73 |
return array(); |
|
74 |
||
75 |
return array_keys($this->errors); |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Retrieve first error code available. |
|
80 |
* |
|
81 |
* @since 2.1.0 |
|
82 |
* |
|
83 |
* @return string|int Empty string, if no error codes. |
|
84 |
*/ |
|
5 | 85 |
public function get_error_code() { |
0 | 86 |
$codes = $this->get_error_codes(); |
87 |
||
88 |
if ( empty($codes) ) |
|
89 |
return ''; |
|
90 |
||
91 |
return $codes[0]; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Retrieve all error messages or error messages matching code. |
|
96 |
* |
|
97 |
* @since 2.1.0 |
|
98 |
* |
|
99 |
* @param string|int $code Optional. Retrieve messages matching code, if exists. |
|
100 |
* @return array Error strings on success, or empty array on failure (if using code parameter). |
|
101 |
*/ |
|
5 | 102 |
public function get_error_messages($code = '') { |
0 | 103 |
// Return all messages if no code specified. |
104 |
if ( empty($code) ) { |
|
105 |
$all_messages = array(); |
|
106 |
foreach ( (array) $this->errors as $code => $messages ) |
|
107 |
$all_messages = array_merge($all_messages, $messages); |
|
108 |
||
109 |
return $all_messages; |
|
110 |
} |
|
111 |
||
112 |
if ( isset($this->errors[$code]) ) |
|
113 |
return $this->errors[$code]; |
|
114 |
else |
|
115 |
return array(); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Get single error message. |
|
120 |
* |
|
121 |
* This will get the first message available for the code. If no code is |
|
122 |
* given then the first code available will be used. |
|
123 |
* |
|
124 |
* @since 2.1.0 |
|
125 |
* |
|
126 |
* @param string|int $code Optional. Error code to retrieve message. |
|
127 |
* @return string |
|
128 |
*/ |
|
5 | 129 |
public function get_error_message($code = '') { |
0 | 130 |
if ( empty($code) ) |
131 |
$code = $this->get_error_code(); |
|
132 |
$messages = $this->get_error_messages($code); |
|
133 |
if ( empty($messages) ) |
|
134 |
return ''; |
|
135 |
return $messages[0]; |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Retrieve error data for error code. |
|
140 |
* |
|
141 |
* @since 2.1.0 |
|
142 |
* |
|
143 |
* @param string|int $code Optional. Error code. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
* @return mixed Error data, if it exists. |
0 | 145 |
*/ |
5 | 146 |
public function get_error_data($code = '') { |
0 | 147 |
if ( empty($code) ) |
148 |
$code = $this->get_error_code(); |
|
149 |
||
150 |
if ( isset($this->error_data[$code]) ) |
|
151 |
return $this->error_data[$code]; |
|
152 |
} |
|
153 |
||
154 |
/** |
|
5 | 155 |
* Add an error or append additional message to an existing error. |
0 | 156 |
* |
157 |
* @since 2.1.0 |
|
158 |
* |
|
159 |
* @param string|int $code Error code. |
|
160 |
* @param string $message Error message. |
|
161 |
* @param mixed $data Optional. Error data. |
|
162 |
*/ |
|
5 | 163 |
public function add($code, $message, $data = '') { |
0 | 164 |
$this->errors[$code][] = $message; |
165 |
if ( ! empty($data) ) |
|
166 |
$this->error_data[$code] = $data; |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Add data for error code. |
|
171 |
* |
|
172 |
* The error code can only contain one error data. |
|
173 |
* |
|
174 |
* @since 2.1.0 |
|
175 |
* |
|
176 |
* @param mixed $data Error data. |
|
177 |
* @param string|int $code Error code. |
|
178 |
*/ |
|
5 | 179 |
public function add_data($data, $code = '') { |
0 | 180 |
if ( empty($code) ) |
181 |
$code = $this->get_error_code(); |
|
182 |
||
183 |
$this->error_data[$code] = $data; |
|
184 |
} |
|
5 | 185 |
|
186 |
/** |
|
187 |
* Removes the specified error. |
|
188 |
* |
|
189 |
* This function removes all error messages associated with the specified |
|
190 |
* error code, along with any error data for that code. |
|
191 |
* |
|
192 |
* @since 4.1.0 |
|
193 |
* |
|
194 |
* @param string|int $code Error code. |
|
195 |
*/ |
|
196 |
public function remove( $code ) { |
|
197 |
unset( $this->errors[ $code ] ); |
|
198 |
unset( $this->error_data[ $code ] ); |
|
199 |
} |
|
0 | 200 |
} |