|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The Jetpack Connection error class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-connection |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The Jetpack Connection Errors that handles errors |
|
12
|
|
|
* |
|
13
|
|
|
* This class handles the following workflow: |
|
14
|
|
|
* |
|
15
|
|
|
* 1. A XML-RCP request with an invalid signature triggers a error |
|
16
|
|
|
* 2. Applies a gate to only process each error code once an hour to avoid overflow |
|
17
|
|
|
* 3. It stores the error on the database, but we don't know yet if this is a valid error, because |
|
18
|
|
|
* we can't confirm it came from WP.com. |
|
19
|
|
|
* 4. It encrypts the error details and send it to thw wp.com server |
|
20
|
|
|
* 5. wp.com checks it and, if valid, sends a new request back to this site using the verify_xml_rpc_error REST endpoint |
|
21
|
|
|
* 6. This endpoint add this error to the Verified errors in the database |
|
22
|
|
|
* 7. Triggers a workflow depending on the error (display user an error message, do some self healing, etc.) |
|
23
|
|
|
* |
|
24
|
|
|
* Errors are stored in the database as options in the following format: |
|
25
|
|
|
* |
|
26
|
|
|
* [ |
|
27
|
|
|
* $error_code => [ |
|
28
|
|
|
* $user_id => [ |
|
29
|
|
|
* $error_details |
|
30
|
|
|
* ] |
|
31
|
|
|
* ] |
|
32
|
|
|
* ] |
|
33
|
|
|
* |
|
34
|
|
|
* For each error code we store a maximum of 5 errors for 5 different user ids. |
|
35
|
|
|
* |
|
36
|
|
|
* An user ID can be |
|
37
|
|
|
* * 0 for blog tokens |
|
38
|
|
|
* * positive integer for user tokens |
|
39
|
|
|
* * 'invalid' for malformed tokens |
|
40
|
|
|
* |
|
41
|
|
|
* @since 8.7.0 |
|
42
|
|
|
*/ |
|
43
|
|
|
class Error_Handler { |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The name of the option that stores the errors |
|
47
|
|
|
* |
|
48
|
|
|
* @since 8.7.0 |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
const STORED_ERRORS_OPTION = 'jetpack_connection_xmlrpc_errors'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The name of the option that stores the errors |
|
56
|
|
|
* |
|
57
|
|
|
* @since 8.7.0 |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
const STORED_VERIFIED_ERRORS_OPTION = 'jetpack_connection_xmlrpc_verified_errors'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The prefix of the transient that controls the gate for each error code |
|
65
|
|
|
* |
|
66
|
|
|
* @since 8.7.0 |
|
67
|
|
|
* |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
const ERROR_REPORTING_GATE = 'jetpack_connection_error_reporting_gate_'; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Time in seconds a test should live in the database before being discarded |
|
74
|
|
|
* |
|
75
|
|
|
* @since 8.7.0 |
|
76
|
|
|
*/ |
|
77
|
|
|
const ERROR_LIFE_TIME = DAY_IN_SECONDS; |
|
78
|
|
|
/** |
|
79
|
|
|
* List of known errors. Only error codes in this list will be handled |
|
80
|
|
|
* |
|
81
|
|
|
* @since 8.7.0 |
|
82
|
|
|
* |
|
83
|
|
|
* @var array |
|
84
|
|
|
*/ |
|
85
|
|
|
public $known_errors = array( |
|
86
|
|
|
'malformed_token', |
|
87
|
|
|
'malformed_user_id', |
|
88
|
|
|
'unknown_user', |
|
89
|
|
|
'no_user_tokens', |
|
90
|
|
|
'empty_master_user_option', |
|
91
|
|
|
'no_token_for_user', |
|
92
|
|
|
'token_malformed', |
|
93
|
|
|
'user_id_mismatch', |
|
94
|
|
|
'no_possible_tokens', |
|
95
|
|
|
'no_valid_token', |
|
96
|
|
|
'unknown_token', |
|
97
|
|
|
'could_not_sign', |
|
98
|
|
|
'invalid_scheme', |
|
99
|
|
|
'invalid_secret', |
|
100
|
|
|
'invalid_token', |
|
101
|
|
|
'token_mismatch', |
|
102
|
|
|
'invalid_body', |
|
103
|
|
|
'invalid_signature', |
|
104
|
|
|
'invalid_body_hash', |
|
105
|
|
|
'invalid_nonce', |
|
106
|
|
|
'signature_mismatch', |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Holds the instance of this singleton class |
|
111
|
|
|
* |
|
112
|
|
|
* @since 8.7.0 |
|
113
|
|
|
* |
|
114
|
|
|
* @var Error_Handler $instance |
|
115
|
|
|
*/ |
|
116
|
|
|
public static $instance = null; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Initialize instance, hookds and load verified errors handlers |
|
120
|
|
|
* |
|
121
|
|
|
* @since 8.7.0 |
|
122
|
|
|
*/ |
|
123
|
|
|
private function __construct() { |
|
124
|
|
|
defined( 'JETPACK__ERRORS_PUBLIC_KEY' ) || define( 'JETPACK__ERRORS_PUBLIC_KEY', 'KdZY80axKX+nWzfrOcizf0jqiFHnrWCl9X8yuaClKgM=' ); |
|
125
|
|
|
|
|
126
|
|
|
add_action( 'rest_api_init', array( $this, 'register_verify_error_endpoint' ) ); |
|
127
|
|
|
|
|
128
|
|
|
$this->handle_verified_errors(); |
|
129
|
|
|
|
|
130
|
|
|
// If the site gets reconnected, clear errors. |
|
131
|
|
|
add_action( 'jetpack_site_registered', array( $this, 'delete_all_errors' ) ); |
|
132
|
|
|
add_action( 'jetpack_get_site_data_success', array( $this, 'delete_all_errors' ) ); |
|
133
|
|
|
add_filter( 'jetpack_connection_disconnect_site_wpcom', array( $this, 'delete_all_errors_and_return_unfiltered_value' ) ); |
|
134
|
|
|
add_filter( 'jetpack_connection_delete_all_tokens', array( $this, 'delete_all_errors_and_return_unfiltered_value' ) ); |
|
135
|
|
|
add_action( 'jetpack_unlinked_user', array( $this, 'delete_all_errors' ) ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Gets the list of verified errors and act upon them |
|
140
|
|
|
* |
|
141
|
|
|
* @since 8.7.0 |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
public function handle_verified_errors() { |
|
146
|
|
|
$verified_errors = $this->get_verified_errors(); |
|
147
|
|
|
foreach ( array_keys( $verified_errors ) as $error_code ) { |
|
148
|
|
|
|
|
149
|
|
|
$error_found = false; |
|
150
|
|
|
|
|
151
|
|
|
switch ( $error_code ) { |
|
152
|
|
|
case 'malformed_token': |
|
153
|
|
|
case 'token_malformed': |
|
154
|
|
|
case 'no_possible_tokens': |
|
155
|
|
|
case 'no_valid_token': |
|
156
|
|
|
case 'unknown_token': |
|
157
|
|
|
case 'could_not_sign': |
|
158
|
|
|
case 'invalid_token': |
|
159
|
|
|
case 'token_mismatch': |
|
160
|
|
|
case 'invalid_signature': |
|
161
|
|
|
case 'signature_mismatch': |
|
162
|
|
|
case 'no_user_tokens': |
|
163
|
|
|
case 'no_token_for_user': |
|
164
|
|
|
add_action( 'admin_notices', array( $this, 'generic_admin_notice_error' ) ); |
|
165
|
|
|
add_action( 'react_connection_errors_initial_state', array( $this, 'jetpack_react_dashboard_error' ) ); |
|
166
|
|
|
$error_found = true; |
|
167
|
|
|
} |
|
168
|
|
|
if ( $error_found ) { |
|
169
|
|
|
// Since we are only generically handling errors, we don't need to trigger error messages for each one of them. |
|
170
|
|
|
break; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Gets the instance of this singleton class |
|
177
|
|
|
* |
|
178
|
|
|
* @since 8.7.0 |
|
179
|
|
|
* |
|
180
|
|
|
* @return Error_Handler $instance |
|
181
|
|
|
*/ |
|
182
|
|
|
public static function get_instance() { |
|
183
|
|
|
if ( is_null( self::$instance ) ) { |
|
184
|
|
|
self::$instance = new self(); |
|
185
|
|
|
} |
|
186
|
|
|
return self::$instance; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Keep track of a connection error that was encountered |
|
191
|
|
|
* |
|
192
|
|
|
* @since 8.7.0 |
|
193
|
|
|
* |
|
194
|
|
|
* @param \WP_Error $error the error object. |
|
195
|
|
|
* @param boolean $force Force the report, even if should_report_error is false. |
|
196
|
|
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
|
|
public function report_error( \WP_Error $error, $force = false ) { |
|
199
|
|
|
if ( in_array( $error->get_error_code(), $this->known_errors, true ) && $this->should_report_error( $error ) || $force ) { |
|
|
|
|
|
|
200
|
|
|
$stored_error = $this->store_error( $error ); |
|
201
|
|
|
if ( $stored_error ) { |
|
202
|
|
|
$this->send_error_to_wpcom( $stored_error ); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Checks the status of the gate |
|
209
|
|
|
* |
|
210
|
|
|
* This protects the site (and WPCOM) against over loads. |
|
211
|
|
|
* |
|
212
|
|
|
* @since 8.7.0 |
|
213
|
|
|
* |
|
214
|
|
|
* @param \WP_Error $error the error object. |
|
215
|
|
|
* @return boolean $should_report True if gate is open and the error should be reported. |
|
216
|
|
|
*/ |
|
217
|
|
|
public function should_report_error( \WP_Error $error ) { |
|
218
|
|
|
|
|
219
|
|
|
if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
|
220
|
|
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Whether to bypass the gate for XML-RPC error handling |
|
225
|
|
|
* |
|
226
|
|
|
* By default, we only process XML-RPC errors once an hour for each error code. |
|
227
|
|
|
* This is done to avoid overflows. If you need to disable this gate, you can set this variable to true. |
|
228
|
|
|
* |
|
229
|
|
|
* This filter is useful for unit testing |
|
230
|
|
|
* |
|
231
|
|
|
* @since 8.7.0 |
|
232
|
|
|
* |
|
233
|
|
|
* @param boolean $bypass_gate whether to bypass the gate. Default is false, do not bypass. |
|
234
|
|
|
*/ |
|
235
|
|
|
$bypass_gate = apply_filters( 'jetpack_connection_bypass_error_reporting_gate', false ); |
|
236
|
|
|
if ( true === $bypass_gate ) { |
|
237
|
|
|
return true; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
$transient = self::ERROR_REPORTING_GATE . $error->get_error_code(); |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
if ( get_transient( $transient ) ) { |
|
243
|
|
|
return false; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
set_transient( $transient, true, HOUR_IN_SECONDS ); |
|
247
|
|
|
return true; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Stores the error in the database so we know there is an issue and can inform the user |
|
252
|
|
|
* |
|
253
|
|
|
* @since 8.7.0 |
|
254
|
|
|
* |
|
255
|
|
|
* @param \WP_Error $error the error object. |
|
256
|
|
|
* @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
|
257
|
|
|
*/ |
|
258
|
|
|
public function store_error( \WP_Error $error ) { |
|
259
|
|
|
|
|
260
|
|
|
$stored_errors = $this->get_stored_errors(); |
|
261
|
|
|
$error_array = $this->wp_error_to_array( $error ); |
|
262
|
|
|
$error_code = $error->get_error_code(); |
|
|
|
|
|
|
263
|
|
|
$user_id = $error_array['user_id']; |
|
264
|
|
|
|
|
265
|
|
|
if ( ! isset( $stored_errors[ $error_code ] ) || ! is_array( $stored_errors[ $error_code ] ) ) { |
|
266
|
|
|
$stored_errors[ $error_code ] = array(); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
$stored_errors[ $error_code ][ $user_id ] = $error_array; |
|
270
|
|
|
|
|
271
|
|
|
// Let's store a maximum of 5 different user ids for each error code. |
|
272
|
|
|
if ( count( $stored_errors[ $error_code ] ) > 5 ) { |
|
273
|
|
|
// array_shift will destroy keys here because they are numeric, so manually remove first item. |
|
274
|
|
|
$keys = array_keys( $stored_errors[ $error_code ] ); |
|
275
|
|
|
unset( $stored_errors[ $error_code ][ $keys[0] ] ); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
if ( update_option( self::STORED_ERRORS_OPTION, $stored_errors ) ) { |
|
279
|
|
|
return $error_array; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
return false; |
|
283
|
|
|
|
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Converts a WP_Error object in the array representation we store in the database |
|
288
|
|
|
* |
|
289
|
|
|
* @since 8.7.0 |
|
290
|
|
|
* |
|
291
|
|
|
* @param \WP_Error $error the error object. |
|
292
|
|
|
* @return boolean|array False if error is invalid or the error array |
|
293
|
|
|
*/ |
|
294
|
|
|
public function wp_error_to_array( \WP_Error $error ) { |
|
295
|
|
|
|
|
296
|
|
|
$data = $error->get_error_data(); |
|
|
|
|
|
|
297
|
|
|
|
|
298
|
|
|
if ( ! isset( $data['signature_details'] ) || ! is_array( $data['signature_details'] ) ) { |
|
299
|
|
|
return false; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
$data = $data['signature_details']; |
|
303
|
|
|
|
|
304
|
|
|
if ( ! isset( $data['token'] ) || empty( $data['token'] ) ) { |
|
305
|
|
|
return false; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
$user_id = $this->get_user_id_from_token( $data['token'] ); |
|
309
|
|
|
|
|
310
|
|
|
$error_array = array( |
|
311
|
|
|
'error_code' => $error->get_error_code(), |
|
|
|
|
|
|
312
|
|
|
'user_id' => $user_id, |
|
313
|
|
|
'error_message' => $error->get_error_message(), |
|
|
|
|
|
|
314
|
|
|
'error_data' => $data, |
|
315
|
|
|
'timestamp' => time(), |
|
316
|
|
|
'nonce' => wp_generate_password( 10, false ), |
|
317
|
|
|
); |
|
318
|
|
|
|
|
319
|
|
|
return $error_array; |
|
320
|
|
|
|
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Sends the error to WP.com to be verified |
|
325
|
|
|
* |
|
326
|
|
|
* @since 8.7.0 |
|
327
|
|
|
* |
|
328
|
|
|
* @param array $error_array The array representation of the error as it is stored in the database. |
|
329
|
|
|
* @return bool |
|
330
|
|
|
*/ |
|
331
|
|
|
public function send_error_to_wpcom( $error_array ) { |
|
332
|
|
|
|
|
333
|
|
|
$blog_id = \Jetpack_Options::get_option( 'id' ); |
|
334
|
|
|
|
|
335
|
|
|
$encrypted_data = $this->encrypt_data_to_wpcom( $error_array ); |
|
336
|
|
|
|
|
337
|
|
|
if ( false === $encrypted_data ) { |
|
338
|
|
|
return false; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
$args = array( |
|
342
|
|
|
'body' => array( |
|
343
|
|
|
'error_data' => $encrypted_data, |
|
344
|
|
|
), |
|
345
|
|
|
); |
|
346
|
|
|
|
|
347
|
|
|
// send encrypted data to WP.com Public-API v2. |
|
348
|
|
|
wp_remote_post( "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/jetpack-report-error/", $args ); |
|
349
|
|
|
return true; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Encrypt data to be sent over to WP.com |
|
354
|
|
|
* |
|
355
|
|
|
* @since 8.7.0 |
|
356
|
|
|
* |
|
357
|
|
|
* @param array|string $data the data to be encoded. |
|
358
|
|
|
* @return boolean|string The encoded string on success, false on failure |
|
359
|
|
|
*/ |
|
360
|
|
|
public function encrypt_data_to_wpcom( $data ) { |
|
361
|
|
|
|
|
362
|
|
|
try { |
|
363
|
|
|
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
364
|
|
|
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
365
|
|
|
$encrypted_data = base64_encode( sodium_crypto_box_seal( wp_json_encode( $data ), base64_decode( JETPACK__ERRORS_PUBLIC_KEY ) ) ); |
|
366
|
|
|
// phpcs:enable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
367
|
|
|
// phpcs:enable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
368
|
|
|
} catch ( \SodiumException $e ) { |
|
|
|
|
|
|
369
|
|
|
// error encrypting data. |
|
370
|
|
|
return false; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
return $encrypted_data; |
|
374
|
|
|
|
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Extracts the user ID from a token |
|
379
|
|
|
* |
|
380
|
|
|
* @since 8.7.0 |
|
381
|
|
|
* |
|
382
|
|
|
* @param string $token the token used to make the xml-rpc request. |
|
383
|
|
|
* @return string $the user id or `invalid` if user id not present. |
|
384
|
|
|
*/ |
|
385
|
|
|
public function get_user_id_from_token( $token ) { |
|
386
|
|
|
$parsed_token = explode( ':', wp_unslash( $token ) ); |
|
387
|
|
|
|
|
388
|
|
|
if ( isset( $parsed_token[2] ) && ctype_digit( $parsed_token[2] ) ) { |
|
389
|
|
|
$user_id = $parsed_token[2]; |
|
390
|
|
|
} else { |
|
391
|
|
|
$user_id = 'invalid'; |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
return $user_id; |
|
395
|
|
|
|
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Gets the reported errors stored in the database |
|
400
|
|
|
* |
|
401
|
|
|
* @since 8.7.0 |
|
402
|
|
|
* |
|
403
|
|
|
* @return array $errors |
|
404
|
|
|
*/ |
|
405
|
|
View Code Duplication |
public function get_stored_errors() { |
|
406
|
|
|
|
|
407
|
|
|
$stored_errors = get_option( self::STORED_ERRORS_OPTION ); |
|
408
|
|
|
|
|
409
|
|
|
if ( ! is_array( $stored_errors ) ) { |
|
410
|
|
|
$stored_errors = array(); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
$stored_errors = $this->garbage_collector( $stored_errors ); |
|
414
|
|
|
|
|
415
|
|
|
return $stored_errors; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Gets the verified errors stored in the database |
|
420
|
|
|
* |
|
421
|
|
|
* @since 8.7.0 |
|
422
|
|
|
* |
|
423
|
|
|
* @return array $errors |
|
424
|
|
|
*/ |
|
425
|
|
View Code Duplication |
public function get_verified_errors() { |
|
426
|
|
|
|
|
427
|
|
|
$verified_errors = get_option( self::STORED_VERIFIED_ERRORS_OPTION ); |
|
428
|
|
|
|
|
429
|
|
|
if ( ! is_array( $verified_errors ) ) { |
|
430
|
|
|
$verified_errors = array(); |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
$verified_errors = $this->garbage_collector( $verified_errors ); |
|
434
|
|
|
|
|
435
|
|
|
return $verified_errors; |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* Removes expired errors from the array |
|
440
|
|
|
* |
|
441
|
|
|
* This method is called by get_stored_errors and get_verified errors and filters their result |
|
442
|
|
|
* Whenever a new error is stored to the database or verified, this will be triggered and the |
|
443
|
|
|
* expired error will be permantently removed from the database |
|
444
|
|
|
* |
|
445
|
|
|
* @since 8.7.0 |
|
446
|
|
|
* |
|
447
|
|
|
* @param array $errors array of errors as stored in the database. |
|
448
|
|
|
* @return array |
|
449
|
|
|
*/ |
|
450
|
|
|
private function garbage_collector( $errors ) { |
|
451
|
|
|
foreach ( $errors as $error_code => $users ) { |
|
452
|
|
|
foreach ( $users as $user_id => $error ) { |
|
453
|
|
|
if ( self::ERROR_LIFE_TIME < time() - (int) $error['timestamp'] ) { |
|
454
|
|
|
unset( $errors[ $error_code ][ $user_id ] ); |
|
455
|
|
|
} |
|
456
|
|
|
} |
|
457
|
|
|
} |
|
458
|
|
|
// Clear empty error codes. |
|
459
|
|
|
$errors = array_filter( |
|
460
|
|
|
$errors, |
|
461
|
|
|
function( $user_errors ) { |
|
462
|
|
|
return ! empty( $user_errors ); |
|
463
|
|
|
} |
|
464
|
|
|
); |
|
465
|
|
|
return $errors; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* Delete all stored and verified errors from the database |
|
470
|
|
|
* |
|
471
|
|
|
* @since 8.7.0 |
|
472
|
|
|
* |
|
473
|
|
|
* @return void |
|
474
|
|
|
*/ |
|
475
|
|
|
public function delete_all_errors() { |
|
476
|
|
|
$this->delete_stored_errors(); |
|
477
|
|
|
$this->delete_verified_errors(); |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
/** |
|
481
|
|
|
* Delete all stored and verified errors from the database and returns unfiltered value |
|
482
|
|
|
* |
|
483
|
|
|
* This is used to hook into a couple of filters that expect true to not short circuit the disconnection flow |
|
484
|
|
|
* |
|
485
|
|
|
* @since 8.9.0 |
|
486
|
|
|
* |
|
487
|
|
|
* @param mixed $check The input sent by the filter. |
|
488
|
|
|
* @return boolean |
|
489
|
|
|
*/ |
|
490
|
|
|
public function delete_all_errors_and_return_unfiltered_value( $check ) { |
|
491
|
|
|
$this->delete_all_errors(); |
|
492
|
|
|
return $check; |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
/** |
|
496
|
|
|
* Delete the reported errors stored in the database |
|
497
|
|
|
* |
|
498
|
|
|
* @since 8.7.0 |
|
499
|
|
|
* |
|
500
|
|
|
* @return boolean True, if option is successfully deleted. False on failure. |
|
501
|
|
|
*/ |
|
502
|
|
|
public function delete_stored_errors() { |
|
503
|
|
|
return delete_option( self::STORED_ERRORS_OPTION ); |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
/** |
|
507
|
|
|
* Delete the verified errors stored in the database |
|
508
|
|
|
* |
|
509
|
|
|
* @since 8.7.0 |
|
510
|
|
|
* |
|
511
|
|
|
* @return boolean True, if option is successfully deleted. False on failure. |
|
512
|
|
|
*/ |
|
513
|
|
|
public function delete_verified_errors() { |
|
514
|
|
|
return delete_option( self::STORED_VERIFIED_ERRORS_OPTION ); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* Gets an error based on the nonce |
|
519
|
|
|
* |
|
520
|
|
|
* Receives a nonce and finds the related error. |
|
521
|
|
|
* |
|
522
|
|
|
* @since 8.7.0 |
|
523
|
|
|
* |
|
524
|
|
|
* @param string $nonce The nonce created for the error we want to get. |
|
525
|
|
|
* @return null|array Returns the error array representation or null if error not found. |
|
526
|
|
|
*/ |
|
527
|
|
|
public function get_error_by_nonce( $nonce ) { |
|
528
|
|
|
$errors = $this->get_stored_errors(); |
|
529
|
|
|
foreach ( $errors as $user_group ) { |
|
530
|
|
|
foreach ( $user_group as $error ) { |
|
531
|
|
|
if ( $error['nonce'] === $nonce ) { |
|
532
|
|
|
return $error; |
|
533
|
|
|
} |
|
534
|
|
|
} |
|
535
|
|
|
} |
|
536
|
|
|
return null; |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
/** |
|
540
|
|
|
* Adds an error to the verified error list |
|
541
|
|
|
* |
|
542
|
|
|
* @since 8.7.0 |
|
543
|
|
|
* |
|
544
|
|
|
* @param array $error The error array, as it was saved in the unverified errors list. |
|
545
|
|
|
* @return void |
|
546
|
|
|
*/ |
|
547
|
|
|
public function verify_error( $error ) { |
|
548
|
|
|
|
|
549
|
|
|
$verified_errors = $this->get_verified_errors(); |
|
550
|
|
|
$error_code = $error['error_code']; |
|
551
|
|
|
$user_id = $error['user_id']; |
|
552
|
|
|
|
|
553
|
|
|
if ( ! isset( $verified_errors[ $error_code ] ) ) { |
|
554
|
|
|
$verified_errors[ $error_code ] = array(); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
$verified_errors[ $error_code ][ $user_id ] = $error; |
|
558
|
|
|
|
|
559
|
|
|
update_option( self::STORED_VERIFIED_ERRORS_OPTION, $verified_errors ); |
|
560
|
|
|
|
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* Register REST API end point for error hanlding. |
|
565
|
|
|
* |
|
566
|
|
|
* @since 8.7.0 |
|
567
|
|
|
* |
|
568
|
|
|
* @return void |
|
569
|
|
|
*/ |
|
570
|
|
|
public function register_verify_error_endpoint() { |
|
571
|
|
|
register_rest_route( |
|
572
|
|
|
'jetpack/v4', |
|
573
|
|
|
'/verify_xmlrpc_error', |
|
574
|
|
|
array( |
|
575
|
|
|
'methods' => \WP_REST_Server::CREATABLE, |
|
576
|
|
|
'callback' => array( $this, 'verify_xml_rpc_error' ), |
|
577
|
|
|
'permission_callback' => '__return_true', |
|
578
|
|
|
'args' => array( |
|
579
|
|
|
'nonce' => array( |
|
580
|
|
|
'required' => true, |
|
581
|
|
|
'type' => 'string', |
|
582
|
|
|
), |
|
583
|
|
|
), |
|
584
|
|
|
) |
|
585
|
|
|
); |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
/** |
|
589
|
|
|
* Handles verification that a xml rpc error is legit and came from WordPres.com |
|
590
|
|
|
* |
|
591
|
|
|
* @since 8.7.0 |
|
592
|
|
|
* |
|
593
|
|
|
* @param \WP_REST_Request $request The request sent to the WP REST API. |
|
594
|
|
|
* |
|
595
|
|
|
* @return boolean |
|
596
|
|
|
*/ |
|
597
|
|
|
public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
|
598
|
|
|
|
|
599
|
|
|
$error = $this->get_error_by_nonce( $request['nonce'] ); |
|
600
|
|
|
|
|
601
|
|
|
if ( $error ) { |
|
602
|
|
|
$this->verify_error( $error ); |
|
603
|
|
|
return new \WP_REST_Response( true, 200 ); |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
return new \WP_REST_Response( false, 200 ); |
|
607
|
|
|
|
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
/** |
|
611
|
|
|
* Prints a generic error notice for all connection errors |
|
612
|
|
|
* |
|
613
|
|
|
* @since 8.9.0 |
|
614
|
|
|
* |
|
615
|
|
|
* @return void |
|
616
|
|
|
*/ |
|
617
|
|
|
public function generic_admin_notice_error() { |
|
618
|
|
|
// do not add admin notice to the jetpack dashboard. |
|
619
|
|
|
global $pagenow; |
|
620
|
|
|
if ( 'admin.php' === $pagenow || isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) { // phpcs:ignore |
|
621
|
|
|
return; |
|
622
|
|
|
} |
|
623
|
|
|
|
|
624
|
|
|
if ( ! current_user_can( 'jetpack_connect' ) ) { |
|
625
|
|
|
return; |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
/** |
|
629
|
|
|
* Filters the message to be displayed in the admin notices area when there's a xmlrpc error. |
|
630
|
|
|
* |
|
631
|
|
|
* By default we don't display any errors. |
|
632
|
|
|
* |
|
633
|
|
|
* Return an empty value to disable the message. |
|
634
|
|
|
* |
|
635
|
|
|
* @since 8.9.0 |
|
636
|
|
|
* |
|
637
|
|
|
* @param string $message The error message. |
|
638
|
|
|
* @param array $errors The array of errors. See Automattic\Jetpack\Connection\Error_Handler for details on the array structure. |
|
639
|
|
|
*/ |
|
640
|
|
|
$message = apply_filters( 'jetpack_connection_error_notice_message', '', $this->get_verified_errors() ); |
|
|
|
|
|
|
641
|
|
|
|
|
642
|
|
|
/** |
|
643
|
|
|
* Fires inside the admin_notices hook just before displaying the error message for a broken connection. |
|
644
|
|
|
* |
|
645
|
|
|
* If you want to disable the default message from being displayed, return an emtpy value in the jetpack_connection_error_notice_message filter. |
|
646
|
|
|
* |
|
647
|
|
|
* @since 8.9.0 |
|
648
|
|
|
* |
|
649
|
|
|
* @param array $errors The array of errors. See Automattic\Jetpack\Connection\Error_Handler for details on the array structure. |
|
650
|
|
|
*/ |
|
651
|
|
|
do_action( 'jetpack_connection_error_notice', $this->get_verified_errors() ); |
|
652
|
|
|
|
|
653
|
|
|
if ( empty( $message ) ) { |
|
654
|
|
|
return; |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
?> |
|
658
|
|
|
<div class="notice notice-error is-dismissible jetpack-message jp-connect" style="display:block !important;"> |
|
659
|
|
|
<p><?php echo esc_html( $message ); ?></p> |
|
660
|
|
|
</div> |
|
661
|
|
|
<?php |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
|
|
/** |
|
665
|
|
|
* Adds the error message to the Jetpack React Dashboard |
|
666
|
|
|
* |
|
667
|
|
|
* @since 8.9.0 |
|
668
|
|
|
* |
|
669
|
|
|
* @param array $errors The array of errors. See Automattic\Jetpack\Connection\Error_Handler for details on the array structure. |
|
670
|
|
|
* @return array |
|
671
|
|
|
*/ |
|
672
|
|
|
public function jetpack_react_dashboard_error( $errors ) { |
|
673
|
|
|
|
|
674
|
|
|
$errors[] = array( |
|
675
|
|
|
'code' => 'xmlrpc_error', |
|
676
|
|
|
'message' => __( 'Your connection with WordPress.com seems to be broken. If you\'re experiencing issues, please try reconnecting.', 'jetpack' ), |
|
677
|
|
|
'action' => 'reconnect', |
|
678
|
|
|
); |
|
679
|
|
|
return $errors; |
|
680
|
|
|
} |
|
681
|
|
|
|
|
682
|
|
|
} |
|
683
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.