|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Sets up the Connection REST API endpoints. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-connection |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Status; |
|
11
|
|
|
use Jetpack_XMLRPC_Server; |
|
12
|
|
|
use WP_Error; |
|
13
|
|
|
use WP_REST_Request; |
|
14
|
|
|
use WP_REST_Response; |
|
15
|
|
|
use WP_REST_Server; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Registers the REST routes for Connections. |
|
19
|
|
|
*/ |
|
20
|
|
|
class REST_Connector { |
|
21
|
|
|
/** |
|
22
|
|
|
* The Connection Manager. |
|
23
|
|
|
* |
|
24
|
|
|
* @var Manager |
|
25
|
|
|
*/ |
|
26
|
|
|
private $connection; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* This property stores the localized "Insufficient Permissions" error message. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string Generic error message when user is not allowed to perform an action. |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $user_permissions_error_msg; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Manager $connection The Connection Manager. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( Manager $connection ) { |
|
41
|
|
|
$this->connection = $connection; |
|
42
|
|
|
|
|
43
|
|
|
self::$user_permissions_error_msg = esc_html__( |
|
44
|
|
|
'You do not have the correct user permissions to perform this action. |
|
45
|
|
|
Please contact your site admin if you think this is a mistake.', |
|
46
|
|
|
'jetpack' |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
if ( ! $this->connection->is_active() ) { |
|
50
|
|
|
// Register a site. |
|
51
|
|
|
register_rest_route( |
|
52
|
|
|
'jetpack/v4', |
|
53
|
|
|
'/verify_registration', |
|
54
|
|
|
array( |
|
55
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
|
56
|
|
|
'callback' => array( $this, 'verify_registration' ), |
|
57
|
|
|
'permission_callback' => '__return_true', |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Authorize a remote user. |
|
63
|
|
|
register_rest_route( |
|
64
|
|
|
'jetpack/v4', |
|
65
|
|
|
'/remote_authorize', |
|
66
|
|
|
array( |
|
67
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
|
68
|
|
|
'callback' => __CLASS__ . '::remote_authorize', |
|
69
|
|
|
'permission_callback' => '__return_true', |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
// Get current connection status of Jetpack. |
|
74
|
|
|
register_rest_route( |
|
75
|
|
|
'jetpack/v4', |
|
76
|
|
|
'/connection', |
|
77
|
|
|
array( |
|
78
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
79
|
|
|
'callback' => __CLASS__ . '::connection_status', |
|
80
|
|
|
'permission_callback' => '__return_true', |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
// Get list of plugins that use the Jetpack connection. |
|
85
|
|
|
register_rest_route( |
|
86
|
|
|
'jetpack/v4', |
|
87
|
|
|
'/connection/plugins', |
|
88
|
|
|
array( |
|
89
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
90
|
|
|
'callback' => array( $this, 'get_connection_plugins' ), |
|
91
|
|
|
'permission_callback' => __CLASS__ . '::activate_plugins_permission_check', |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Handles verification that a site is registered. |
|
98
|
|
|
* |
|
99
|
|
|
* @since 5.4.0 |
|
100
|
|
|
* |
|
101
|
|
|
* @param \WP_REST_Request $request The request sent to the WP REST API. |
|
102
|
|
|
* |
|
103
|
|
|
* @return string|WP_Error |
|
104
|
|
|
*/ |
|
105
|
|
|
public function verify_registration( \WP_REST_Request $request ) { |
|
106
|
|
|
$registration_data = array( $request['secret_1'], $request['state'] ); |
|
107
|
|
|
|
|
108
|
|
|
return $this->connection->handle_registration( $registration_data ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Handles verification that a site is registered |
|
113
|
|
|
* |
|
114
|
|
|
* @since 5.4.0 |
|
115
|
|
|
* |
|
116
|
|
|
* @param WP_REST_Request $request The request sent to the WP REST API. |
|
117
|
|
|
* |
|
118
|
|
|
* @return array|wp-error |
|
|
|
|
|
|
119
|
|
|
*/ |
|
120
|
|
|
public static function remote_authorize( $request ) { |
|
121
|
|
|
$xmlrpc_server = new Jetpack_XMLRPC_Server(); |
|
122
|
|
|
$result = $xmlrpc_server->remote_authorize( $request ); |
|
123
|
|
|
|
|
124
|
|
|
if ( is_a( $result, 'IXR_Error' ) ) { |
|
125
|
|
|
$result = new WP_Error( $result->code, $result->message ); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $result; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get connection status for this Jetpack site. |
|
133
|
|
|
* |
|
134
|
|
|
* @since 4.3.0 |
|
135
|
|
|
* |
|
136
|
|
|
* @return WP_REST_Response Connection information. |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function connection_status() { |
|
139
|
|
|
$status = new Status(); |
|
140
|
|
|
$connection = new Manager(); |
|
141
|
|
|
|
|
142
|
|
|
return rest_ensure_response( |
|
143
|
|
|
array( |
|
144
|
|
|
'isActive' => $connection->is_active(), |
|
145
|
|
|
'isStaging' => $status->is_staging_site(), |
|
146
|
|
|
'isRegistered' => $connection->is_registered(), |
|
147
|
|
|
'devMode' => array( |
|
148
|
|
|
'isActive' => $status->is_development_mode(), |
|
149
|
|
|
'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG, |
|
150
|
|
|
'url' => site_url() && false === strpos( site_url(), '.' ), |
|
151
|
|
|
'filter' => apply_filters( 'jetpack_development_mode', false ), |
|
152
|
|
|
), |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get plugins connected to the Jetpack. |
|
160
|
|
|
* |
|
161
|
|
|
* @since 8.6.0 |
|
162
|
|
|
* |
|
163
|
|
|
* @return WP_REST_Response|WP_Error Response or error object, depending on the request result. |
|
164
|
|
|
*/ |
|
165
|
|
|
public function get_connection_plugins() { |
|
166
|
|
|
$plugins = $this->connection->get_connected_plugins(); |
|
167
|
|
|
|
|
168
|
|
|
if ( is_wp_error( $plugins ) ) { |
|
169
|
|
|
return $plugins; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
array_walk( |
|
173
|
|
|
$plugins, |
|
174
|
|
|
function( &$data, $slug ) { |
|
175
|
|
|
$data['slug'] = $slug; |
|
176
|
|
|
} |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
return rest_ensure_response( array_values( $plugins ) ); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Verify that user can view Jetpack admin page and can activate plugins. |
|
184
|
|
|
* |
|
185
|
|
|
* @since 8.8.0 |
|
186
|
|
|
* |
|
187
|
|
|
* @return bool|WP_Error Whether user has the capability 'jetpack_admin_page' and 'activate_plugins'. |
|
188
|
|
|
*/ |
|
189
|
|
|
public static function activate_plugins_permission_check() { |
|
190
|
|
|
if ( current_user_can( 'activate_plugins' ) ) { |
|
191
|
|
|
return true; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return new WP_Error( 'invalid_user_permission_activate_plugins', self::get_user_permissions_error_msg(), array( 'status' => rest_authorization_required_code() ) ); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns generic error message when user is not allowed to perform an action. |
|
199
|
|
|
* |
|
200
|
|
|
* @return string The error message. |
|
201
|
|
|
*/ |
|
202
|
|
|
public static function get_user_permissions_error_msg() { |
|
203
|
|
|
return self::$user_permissions_error_msg; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.