1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Jetpack Connection error handler class for invalid blog tokens |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection\Error_Handlers; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class handles all the error codes that indicates a broken blog token and |
12
|
|
|
* suggests the user to reconnect. |
13
|
|
|
* |
14
|
|
|
* @since 8.7.0 |
15
|
|
|
*/ |
16
|
|
|
class Invalid_Blog_Token { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set up hooks |
20
|
|
|
* |
21
|
|
|
* @since 8.7.0 |
22
|
|
|
* |
23
|
|
|
* @param array $errors The array containing verified errors stored in the database. |
24
|
|
|
*/ |
25
|
|
|
public function __construct( $errors ) { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Filters the message to be displayed in the admin notices area when there's a invalid blog token xmlrpc error |
29
|
|
|
* |
30
|
|
|
* Return an empty value to disable the message. |
31
|
|
|
* |
32
|
|
|
* @since 8.7.0 |
33
|
|
|
* |
34
|
|
|
* @param string $message The error message. |
35
|
|
|
*/ |
36
|
|
|
$this->message = apply_filters( 'jetpack_connection_invalid_blog_token_admin_notice', __( 'Your connection with WordPress.com seems to be broken. If you\'re experiencing issues, please try reconnecting.', 'jetpack' ) ); |
|
|
|
|
37
|
|
|
|
38
|
|
|
if ( empty( $this->message ) ) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// In this class, we will only handle errors with the blog token, so ignoring if there are only errors with user tokens. |
43
|
|
|
if ( ! isset( $errors[0] ) && ! isset( $errors['invalid'] ) ) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
add_action( 'react_connection_errors_initial_state', array( $this, 'jetpack_react_dashboard_error' ) ); |
48
|
|
|
// do not add admin notice to the jetpack dashboard. |
49
|
|
|
global $pagenow; |
50
|
|
|
if ( 'admin.php' === $pagenow || isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) { // phpcs:ignore |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Prints an admin notice for the blog token error |
59
|
|
|
* |
60
|
|
|
* @since 8.7.0 |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function admin_notice() { |
65
|
|
|
|
66
|
|
|
if ( ! current_user_can( 'jetpack_connect' ) ) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
?> |
71
|
|
|
<div class="notice notice-error is-dismissible jetpack-message jp-connect" style="display:block !important;"> |
72
|
|
|
<p><?php echo esc_html( $this->message ); ?></p> |
73
|
|
|
</div> |
74
|
|
|
<?php |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Adds the error message to the Jetpack React Dashboard |
79
|
|
|
* |
80
|
|
|
* @param array $errors The array of errors. |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function jetpack_react_dashboard_error( $errors ) { |
84
|
|
|
|
85
|
|
|
$errors[] = array( |
86
|
|
|
'code' => 'invalid_blog_token', |
87
|
|
|
'message' => $this->message, |
88
|
|
|
'action' => 'reconnect', |
89
|
|
|
); |
90
|
|
|
return $errors; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: