1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Jetpack Connection manager class file. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants; |
11
|
|
|
use Automattic\Jetpack\Heartbeat; |
12
|
|
|
use Automattic\Jetpack\Roles; |
13
|
|
|
use Automattic\Jetpack\Status; |
14
|
|
|
use Automattic\Jetpack\Tracking; |
15
|
|
|
use WP_Error; |
16
|
|
|
use WP_User; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The Jetpack Connection Manager class that is used as a single gateway between WordPress.com |
20
|
|
|
* and Jetpack. |
21
|
|
|
*/ |
22
|
|
|
class Manager { |
23
|
|
|
/** |
24
|
|
|
* A copy of the raw POST data for signature verification purposes. |
25
|
|
|
* |
26
|
|
|
* @var String |
27
|
|
|
*/ |
28
|
|
|
protected $raw_post_data; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Verification data needs to be stored to properly verify everything. |
32
|
|
|
* |
33
|
|
|
* @var Object |
34
|
|
|
*/ |
35
|
|
|
private $xmlrpc_verification = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Plugin management object. |
39
|
|
|
* |
40
|
|
|
* @var Plugin |
41
|
|
|
*/ |
42
|
|
|
private $plugin = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initialize the object. |
46
|
|
|
* Make sure to call the "Configure" first. |
47
|
|
|
* |
48
|
|
|
* @param string $plugin_slug Slug of the plugin using the connection (optional, but encouraged). |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @see \Automattic\Jetpack\Config |
51
|
|
|
*/ |
52
|
|
|
public function __construct( $plugin_slug = null ) { |
53
|
|
|
if ( $plugin_slug && is_string( $plugin_slug ) ) { |
|
|
|
|
54
|
|
|
$this->set_plugin_instance( new Plugin( $plugin_slug ) ); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initializes required listeners. This is done separately from the constructors |
60
|
|
|
* because some objects sometimes need to instantiate separate objects of this class. |
61
|
|
|
* |
62
|
|
|
* @todo Implement a proper nonce verification. |
63
|
|
|
*/ |
64
|
|
|
public static function configure() { |
65
|
|
|
$manager = new self(); |
66
|
|
|
|
67
|
|
|
add_filter( |
68
|
|
|
'jetpack_constant_default_value', |
69
|
|
|
__NAMESPACE__ . '\Utils::jetpack_api_constant_filter', |
70
|
|
|
10, |
71
|
|
|
2 |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$manager->setup_xmlrpc_handlers( |
75
|
|
|
$_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
76
|
|
|
$manager->is_active(), // TODO deprecate this. |
77
|
|
|
$manager->verify_xml_rpc_signature() |
|
|
|
|
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$manager->error_handler = Error_Handler::get_instance(); |
|
|
|
|
81
|
|
|
|
82
|
|
|
if ( $manager->is_connected() ) { |
83
|
|
|
add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
87
|
|
|
|
88
|
|
|
( new Nonce_Handler() )->init_schedule(); |
89
|
|
|
|
90
|
|
|
add_action( 'plugins_loaded', __NAMESPACE__ . '\Plugin_Storage::configure', 100 ); |
91
|
|
|
|
92
|
|
|
add_filter( 'map_meta_cap', array( $manager, 'jetpack_connection_custom_caps' ), 1, 4 ); |
93
|
|
|
|
94
|
|
|
Heartbeat::init(); |
95
|
|
|
add_filter( 'jetpack_heartbeat_stats_array', array( $manager, 'add_stats_to_heartbeat' ) ); |
96
|
|
|
|
97
|
|
|
Webhooks::init( $manager ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sets up the XMLRPC request handlers. |
102
|
|
|
* |
103
|
|
|
* @param array $request_params incoming request parameters. |
104
|
|
|
* @param Boolean $is_active whether the connection is currently active. |
105
|
|
|
* @param Boolean $is_signed whether the signature check has been successful. |
106
|
|
|
* @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
|
|
|
|
107
|
|
|
*/ |
108
|
|
|
public function setup_xmlrpc_handlers( |
109
|
|
|
$request_params, |
110
|
|
|
$is_active, |
111
|
|
|
$is_signed, |
112
|
|
|
\Jetpack_XMLRPC_Server $xmlrpc_server = null |
113
|
|
|
) { |
114
|
|
|
add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ), 1000, 2 ); |
115
|
|
|
|
116
|
|
|
if ( |
117
|
|
|
! isset( $request_params['for'] ) |
118
|
|
|
|| 'jetpack' !== $request_params['for'] |
119
|
|
|
) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Alternate XML-RPC, via ?for=jetpack&jetpack=comms. |
124
|
|
|
if ( |
125
|
|
|
isset( $request_params['jetpack'] ) |
126
|
|
|
&& 'comms' === $request_params['jetpack'] |
127
|
|
|
) { |
128
|
|
|
if ( ! Constants::is_defined( 'XMLRPC_REQUEST' ) ) { |
129
|
|
|
// Use the real constant here for WordPress' sake. |
130
|
|
|
define( 'XMLRPC_REQUEST', true ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
134
|
|
|
|
135
|
|
|
add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ( ! Constants::get_constant( 'XMLRPC_REQUEST' ) ) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
// Display errors can cause the XML to be not well formed. |
142
|
|
|
@ini_set( 'display_errors', false ); // phpcs:ignore |
|
|
|
|
143
|
|
|
|
144
|
|
|
if ( $xmlrpc_server ) { |
145
|
|
|
$this->xmlrpc_server = $xmlrpc_server; |
|
|
|
|
146
|
|
|
} else { |
147
|
|
|
$this->xmlrpc_server = new \Jetpack_XMLRPC_Server(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->require_jetpack_authentication(); |
151
|
|
|
|
152
|
|
|
if ( $is_active ) { |
153
|
|
|
// Hack to preserve $HTTP_RAW_POST_DATA. |
154
|
|
|
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
155
|
|
|
|
156
|
|
|
if ( $is_signed ) { |
157
|
|
|
// The actual API methods. |
158
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
159
|
|
|
} else { |
160
|
|
|
// The jetpack.authorize method should be available for unauthenticated users on a site with an |
161
|
|
|
// active Jetpack connection, so that additional users can link their account. |
162
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
163
|
|
|
} |
164
|
|
|
} else { |
165
|
|
|
// The bootstrap API methods. |
166
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
167
|
|
|
|
168
|
|
|
if ( $is_signed ) { |
169
|
|
|
// The jetpack Provision method is available for blog-token-signed requests. |
170
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
171
|
|
|
} else { |
172
|
|
|
new XMLRPC_Connector( $this ); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
177
|
|
|
add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Initializes the REST API connector on the init hook. |
183
|
|
|
*/ |
184
|
|
|
public function initialize_rest_api_registration_connector() { |
185
|
|
|
new REST_Connector( $this ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
190
|
|
|
* and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
191
|
|
|
* security/firewall policies, we provide our own alternate XML RPC API endpoint |
192
|
|
|
* which is accessible via a different URI. Most of the below is copied directly |
193
|
|
|
* from /xmlrpc.php so that we're replicating it as closely as possible. |
194
|
|
|
* |
195
|
|
|
* @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
196
|
|
|
*/ |
197
|
|
|
public function alternate_xmlrpc() { |
198
|
|
|
// Some browser-embedded clients send cookies. We don't want them. |
199
|
|
|
$_COOKIE = array(); |
200
|
|
|
|
201
|
|
|
include_once ABSPATH . 'wp-admin/includes/admin.php'; |
202
|
|
|
include_once ABSPATH . WPINC . '/class-IXR.php'; |
203
|
|
|
include_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Filters the class used for handling XML-RPC requests. |
207
|
|
|
* |
208
|
|
|
* @since 3.1.0 |
209
|
|
|
* |
210
|
|
|
* @param string $class The name of the XML-RPC server class. |
211
|
|
|
*/ |
212
|
|
|
$wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
213
|
|
|
$wp_xmlrpc_server = new $wp_xmlrpc_server_class(); |
214
|
|
|
|
215
|
|
|
// Fire off the request. |
216
|
|
|
nocache_headers(); |
217
|
|
|
$wp_xmlrpc_server->serve_request(); |
218
|
|
|
|
219
|
|
|
exit; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Removes all XML-RPC methods that are not `jetpack.*`. |
224
|
|
|
* Only used in our alternate XML-RPC endpoint, where we want to |
225
|
|
|
* ensure that Core and other plugins' methods are not exposed. |
226
|
|
|
* |
227
|
|
|
* @param array $methods a list of registered WordPress XMLRPC methods. |
228
|
|
|
* @return array filtered $methods |
229
|
|
|
*/ |
230
|
|
|
public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
231
|
|
|
$jetpack_methods = array(); |
232
|
|
|
|
233
|
|
|
foreach ( $methods as $method => $callback ) { |
234
|
|
|
if ( 0 === strpos( $method, 'jetpack.' ) ) { |
235
|
|
|
$jetpack_methods[ $method ] = $callback; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $jetpack_methods; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Removes all other authentication methods not to allow other |
244
|
|
|
* methods to validate unauthenticated requests. |
245
|
|
|
*/ |
246
|
|
|
public function require_jetpack_authentication() { |
247
|
|
|
// Don't let anyone authenticate. |
248
|
|
|
$_COOKIE = array(); |
249
|
|
|
remove_all_filters( 'authenticate' ); |
250
|
|
|
remove_all_actions( 'wp_login_failed' ); |
251
|
|
|
|
252
|
|
|
if ( $this->is_connected() ) { |
253
|
|
|
// Allow Jetpack authentication. |
254
|
|
|
add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Authenticates XML-RPC and other requests from the Jetpack Server |
260
|
|
|
* |
261
|
|
|
* @param WP_User|Mixed $user user object if authenticated. |
262
|
|
|
* @param String $username username. |
263
|
|
|
* @param String $password password string. |
264
|
|
|
* @return WP_User|Mixed authenticated user or error. |
265
|
|
|
*/ |
266
|
|
|
public function authenticate_jetpack( $user, $username, $password ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
267
|
|
|
if ( is_a( $user, '\\WP_User' ) ) { |
268
|
|
|
return $user; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$token_details = $this->verify_xml_rpc_signature(); |
272
|
|
|
|
273
|
|
|
if ( ! $token_details ) { |
274
|
|
|
return $user; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if ( 'user' !== $token_details['type'] ) { |
278
|
|
|
return $user; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if ( ! $token_details['user_id'] ) { |
282
|
|
|
return $user; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
nocache_headers(); |
286
|
|
|
|
287
|
|
|
return new \WP_User( $token_details['user_id'] ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Verifies the signature of the current request. |
292
|
|
|
* |
293
|
|
|
* @return false|array |
294
|
|
|
*/ |
295
|
|
|
public function verify_xml_rpc_signature() { |
296
|
|
|
if ( is_null( $this->xmlrpc_verification ) ) { |
297
|
|
|
$this->xmlrpc_verification = $this->internal_verify_xml_rpc_signature(); |
298
|
|
|
|
299
|
|
|
if ( is_wp_error( $this->xmlrpc_verification ) ) { |
300
|
|
|
/** |
301
|
|
|
* Action for logging XMLRPC signature verification errors. This data is sensitive. |
302
|
|
|
* |
303
|
|
|
* @since 7.5.0 |
304
|
|
|
* |
305
|
|
|
* @param WP_Error $signature_verification_error The verification error |
306
|
|
|
*/ |
307
|
|
|
do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
308
|
|
|
|
309
|
|
|
Error_Handler::get_instance()->report_error( $this->xmlrpc_verification ); |
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Verifies the signature of the current request. |
319
|
|
|
* |
320
|
|
|
* This function has side effects and should not be used. Instead, |
321
|
|
|
* use the memoized version `->verify_xml_rpc_signature()`. |
322
|
|
|
* |
323
|
|
|
* @internal |
324
|
|
|
* @todo Refactor to use proper nonce verification. |
325
|
|
|
*/ |
326
|
|
|
private function internal_verify_xml_rpc_signature() { |
327
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
328
|
|
|
// It's not for us. |
329
|
|
|
if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$signature_details = array( |
334
|
|
|
'token' => isset( $_GET['token'] ) ? wp_unslash( $_GET['token'] ) : '', |
335
|
|
|
'timestamp' => isset( $_GET['timestamp'] ) ? wp_unslash( $_GET['timestamp'] ) : '', |
336
|
|
|
'nonce' => isset( $_GET['nonce'] ) ? wp_unslash( $_GET['nonce'] ) : '', |
337
|
|
|
'body_hash' => isset( $_GET['body-hash'] ) ? wp_unslash( $_GET['body-hash'] ) : '', |
338
|
|
|
'method' => wp_unslash( $_SERVER['REQUEST_METHOD'] ), |
339
|
|
|
'url' => wp_unslash( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ), // Temp - will get real signature URL later. |
340
|
|
|
'signature' => isset( $_GET['signature'] ) ? wp_unslash( $_GET['signature'] ) : '', |
341
|
|
|
); |
342
|
|
|
|
343
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
344
|
|
|
@list( $token_key, $version, $user_id ) = explode( ':', wp_unslash( $_GET['token'] ) ); |
|
|
|
|
345
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
346
|
|
|
|
347
|
|
|
$jetpack_api_version = Constants::get_constant( 'JETPACK__API_VERSION' ); |
348
|
|
|
|
349
|
|
|
if ( |
350
|
|
|
empty( $token_key ) |
351
|
|
|
|| |
352
|
|
|
empty( $version ) || (string) $jetpack_api_version !== $version ) { |
353
|
|
|
return new \WP_Error( 'malformed_token', 'Malformed token in request', compact( 'signature_details' ) ); |
|
|
|
|
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if ( '0' === $user_id ) { |
357
|
|
|
$token_type = 'blog'; |
358
|
|
|
$user_id = 0; |
359
|
|
|
} else { |
360
|
|
|
$token_type = 'user'; |
361
|
|
|
if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { |
362
|
|
|
return new \WP_Error( |
363
|
|
|
'malformed_user_id', |
|
|
|
|
364
|
|
|
'Malformed user_id in request', |
365
|
|
|
compact( 'signature_details' ) |
366
|
|
|
); |
367
|
|
|
} |
368
|
|
|
$user_id = (int) $user_id; |
369
|
|
|
|
370
|
|
|
$user = new \WP_User( $user_id ); |
371
|
|
|
if ( ! $user || ! $user->exists() ) { |
372
|
|
|
return new \WP_Error( |
373
|
|
|
'unknown_user', |
|
|
|
|
374
|
|
|
sprintf( 'User %d does not exist', $user_id ), |
375
|
|
|
compact( 'signature_details' ) |
376
|
|
|
); |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$token = $this->get_tokens()->get_access_token( $user_id, $token_key, false ); |
381
|
|
|
if ( is_wp_error( $token ) ) { |
382
|
|
|
$token->add_data( compact( 'signature_details' ) ); |
383
|
|
|
return $token; |
384
|
|
|
} elseif ( ! $token ) { |
385
|
|
|
return new \WP_Error( |
386
|
|
|
'unknown_token', |
|
|
|
|
387
|
|
|
sprintf( 'Token %s:%s:%d does not exist', $token_key, $version, $user_id ), |
388
|
|
|
compact( 'signature_details' ) |
389
|
|
|
); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
$jetpack_signature = new \Jetpack_Signature( $token->secret, (int) \Jetpack_Options::get_option( 'time_diff' ) ); |
393
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing |
394
|
|
|
if ( isset( $_POST['_jetpack_is_multipart'] ) ) { |
395
|
|
|
$post_data = $_POST; |
396
|
|
|
$file_hashes = array(); |
397
|
|
|
foreach ( $post_data as $post_data_key => $post_data_value ) { |
398
|
|
|
if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { |
399
|
|
|
continue; |
400
|
|
|
} |
401
|
|
|
$post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); |
402
|
|
|
$file_hashes[ $post_data_key ] = $post_data_value; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
foreach ( $file_hashes as $post_data_key => $post_data_value ) { |
406
|
|
|
unset( $post_data[ "_jetpack_file_hmac_{$post_data_key}" ] ); |
407
|
|
|
$post_data[ $post_data_key ] = $post_data_value; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
ksort( $post_data ); |
411
|
|
|
|
412
|
|
|
$body = http_build_query( stripslashes_deep( $post_data ) ); |
413
|
|
|
} elseif ( is_null( $this->raw_post_data ) ) { |
414
|
|
|
$body = file_get_contents( 'php://input' ); |
415
|
|
|
} else { |
416
|
|
|
$body = null; |
417
|
|
|
} |
418
|
|
|
// phpcs:enable |
419
|
|
|
|
420
|
|
|
$signature = $jetpack_signature->sign_current_request( |
421
|
|
|
array( 'body' => is_null( $body ) ? $this->raw_post_data : $body ) |
422
|
|
|
); |
423
|
|
|
|
424
|
|
|
$signature_details['url'] = $jetpack_signature->current_request_url; |
425
|
|
|
|
426
|
|
|
if ( ! $signature ) { |
427
|
|
|
return new \WP_Error( |
428
|
|
|
'could_not_sign', |
|
|
|
|
429
|
|
|
'Unknown signature error', |
430
|
|
|
compact( 'signature_details' ) |
431
|
|
|
); |
432
|
|
|
} elseif ( is_wp_error( $signature ) ) { |
433
|
|
|
return $signature; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
437
|
|
|
$timestamp = (int) $_GET['timestamp']; |
438
|
|
|
$nonce = stripslashes( (string) $_GET['nonce'] ); |
439
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
440
|
|
|
|
441
|
|
|
// Use up the nonce regardless of whether the signature matches. |
442
|
|
|
if ( ! ( new Nonce_Handler() )->add( $timestamp, $nonce ) ) { |
443
|
|
|
return new \WP_Error( |
444
|
|
|
'invalid_nonce', |
|
|
|
|
445
|
|
|
'Could not add nonce', |
446
|
|
|
compact( 'signature_details' ) |
447
|
|
|
); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
// Be careful about what you do with this debugging data. |
451
|
|
|
// If a malicious requester has access to the expected signature, |
452
|
|
|
// bad things might be possible. |
453
|
|
|
$signature_details['expected'] = $signature; |
454
|
|
|
|
455
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
456
|
|
|
if ( ! hash_equals( $signature, $_GET['signature'] ) ) { |
457
|
|
|
return new \WP_Error( |
458
|
|
|
'signature_mismatch', |
|
|
|
|
459
|
|
|
'Signature mismatch', |
460
|
|
|
compact( 'signature_details' ) |
461
|
|
|
); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Action for additional token checking. |
466
|
|
|
* |
467
|
|
|
* @since 7.7.0 |
468
|
|
|
* |
469
|
|
|
* @param array $post_data request data. |
470
|
|
|
* @param array $token_data token data. |
471
|
|
|
*/ |
472
|
|
|
return apply_filters( |
473
|
|
|
'jetpack_signature_check_token', |
474
|
|
|
array( |
475
|
|
|
'type' => $token_type, |
476
|
|
|
'token_key' => $token_key, |
477
|
|
|
'user_id' => $token->external_user_id, |
478
|
|
|
), |
479
|
|
|
$token, |
|
|
|
|
480
|
|
|
$this->raw_post_data |
481
|
|
|
); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Returns true if the current site is connected to WordPress.com and has the minimum requirements to enable Jetpack UI. |
486
|
|
|
* |
487
|
|
|
* @return Boolean is the site connected? |
488
|
|
|
*/ |
489
|
|
|
public function is_active() { |
490
|
|
|
if ( ( new Status() )->is_no_user_testing_mode() ) { |
491
|
|
|
return $this->is_connected(); |
492
|
|
|
} |
493
|
|
|
return (bool) $this->get_tokens()->get_access_token( true ); |
|
|
|
|
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Obtains an instance of the Tokens class. |
498
|
|
|
* |
499
|
|
|
* @return Tokens the Tokens object |
500
|
|
|
*/ |
501
|
|
|
public function get_tokens() { |
502
|
|
|
return new Tokens(); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
507
|
|
|
* |
508
|
|
|
* @access public |
509
|
|
|
* @deprecated 9.2.0 Use is_connected instead |
510
|
|
|
* @see Manager::is_connected |
511
|
|
|
* |
512
|
|
|
* @return bool |
513
|
|
|
*/ |
514
|
|
|
public function is_registered() { |
515
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.2' ); |
516
|
|
|
return $this->is_connected(); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Returns true if the site has both a token and a blog id, which indicates a site has been connected. |
521
|
|
|
* |
522
|
|
|
* @access public |
523
|
|
|
* @since 9.2.0 |
524
|
|
|
* |
525
|
|
|
* @return bool |
526
|
|
|
*/ |
527
|
|
|
public function is_connected() { |
528
|
|
|
$has_blog_id = (bool) \Jetpack_Options::get_option( 'id' ); |
529
|
|
|
$has_blog_token = (bool) $this->get_tokens()->get_access_token(); |
530
|
|
|
return $has_blog_id && $has_blog_token; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Returns true if the site has at least one connected administrator. |
535
|
|
|
* |
536
|
|
|
* @access public |
537
|
|
|
* @since 9.2.0 |
538
|
|
|
* |
539
|
|
|
* @return bool |
540
|
|
|
*/ |
541
|
|
|
public function has_connected_admin() { |
542
|
|
|
return (bool) count( $this->get_connected_users( 'manage_options' ) ); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Returns true if the site has any connected user. |
547
|
|
|
* |
548
|
|
|
* @access public |
549
|
|
|
* @since 9.2.0 |
550
|
|
|
* |
551
|
|
|
* @return bool |
552
|
|
|
*/ |
553
|
|
|
public function has_connected_user() { |
554
|
|
|
return (bool) count( $this->get_connected_users() ); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Returns an array of user_id's that have user tokens for communicating with wpcom. |
559
|
|
|
* Able to select by specific capability. |
560
|
|
|
* |
561
|
|
|
* @param string $capability The capability of the user. |
562
|
|
|
* @return array Array of WP_User objects if found. |
563
|
|
|
*/ |
564
|
|
|
public function get_connected_users( $capability = 'any' ) { |
565
|
|
|
return $this->get_tokens()->get_connected_users( $capability ); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Returns true if the site has a connected Blog owner (master_user). |
570
|
|
|
* |
571
|
|
|
* @access public |
572
|
|
|
* @since 9.2.0 |
573
|
|
|
* |
574
|
|
|
* @return bool |
575
|
|
|
*/ |
576
|
|
|
public function has_connected_owner() { |
577
|
|
|
return (bool) $this->get_connection_owner_id(); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* Returns true if the site is connected only at a site level. |
582
|
|
|
* |
583
|
|
|
* Note that we are explicitly checking for the existence of the master_user option in order to account for cases where we don't have any user tokens (user-level connection) but the master_user option is set, which could be the result of a problematic user connection. |
584
|
|
|
* |
585
|
|
|
* @access public |
586
|
|
|
* @since 9.6.0 |
587
|
|
|
* |
588
|
|
|
* @return bool |
589
|
|
|
*/ |
590
|
|
|
public function is_userless() { |
591
|
|
|
return $this->is_connected() && ! $this->has_connected_user() && ! \Jetpack_Options::get_option( 'master_user' ); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Checks to see if the connection owner of the site is missing. |
596
|
|
|
* |
597
|
|
|
* @return bool |
598
|
|
|
*/ |
599
|
|
|
public function is_missing_connection_owner() { |
600
|
|
|
$connection_owner = $this->get_connection_owner_id(); |
601
|
|
|
if ( ! get_user_by( 'id', $connection_owner ) ) { |
602
|
|
|
return true; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
return false; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Returns true if the user with the specified identifier is connected to |
610
|
|
|
* WordPress.com. |
611
|
|
|
* |
612
|
|
|
* @param int $user_id the user identifier. Default is the current user. |
|
|
|
|
613
|
|
|
* @return bool Boolean is the user connected? |
614
|
|
|
*/ |
615
|
|
|
public function is_user_connected( $user_id = false ) { |
616
|
|
|
$user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
617
|
|
|
if ( ! $user_id ) { |
618
|
|
|
return false; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
return (bool) $this->get_tokens()->get_access_token( $user_id ); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* Returns the local user ID of the connection owner. |
626
|
|
|
* |
627
|
|
|
* @return bool|int Returns the ID of the connection owner or False if no connection owner found. |
628
|
|
|
*/ |
629
|
|
|
public function get_connection_owner_id() { |
630
|
|
|
$owner = $this->get_connection_owner(); |
631
|
|
|
return $owner instanceof \WP_User ? $owner->ID : false; |
|
|
|
|
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Get the wpcom user data of the current|specified connected user. |
636
|
|
|
* |
637
|
|
|
* @todo Refactor to properly load the XMLRPC client independently. |
638
|
|
|
* |
639
|
|
|
* @param Integer $user_id the user identifier. |
|
|
|
|
640
|
|
|
* @return Object the user object. |
641
|
|
|
*/ |
642
|
|
|
public function get_connected_user_data( $user_id = null ) { |
643
|
|
|
if ( ! $user_id ) { |
|
|
|
|
644
|
|
|
$user_id = get_current_user_id(); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
$transient_key = "jetpack_connected_user_data_$user_id"; |
648
|
|
|
$cached_user_data = get_transient( $transient_key ); |
649
|
|
|
|
650
|
|
|
if ( $cached_user_data ) { |
651
|
|
|
return $cached_user_data; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
$xml = new \Jetpack_IXR_Client( |
655
|
|
|
array( |
656
|
|
|
'user_id' => $user_id, |
657
|
|
|
) |
658
|
|
|
); |
659
|
|
|
$xml->query( 'wpcom.getUser' ); |
660
|
|
|
if ( ! $xml->isError() ) { |
661
|
|
|
$user_data = $xml->getResponse(); |
662
|
|
|
set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
663
|
|
|
return $user_data; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
return false; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Returns a user object of the connection owner. |
671
|
|
|
* |
672
|
|
|
* @return WP_User|false False if no connection owner found. |
673
|
|
|
*/ |
674
|
|
|
public function get_connection_owner() { |
675
|
|
|
|
676
|
|
|
$user_id = \Jetpack_Options::get_option( 'master_user' ); |
677
|
|
|
|
678
|
|
|
if ( ! $user_id ) { |
679
|
|
|
return false; |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
// Make sure user is connected. |
683
|
|
|
$user_token = $this->get_tokens()->get_access_token( $user_id ); |
684
|
|
|
|
685
|
|
|
$connection_owner = false; |
686
|
|
|
|
687
|
|
|
if ( $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) ) { |
688
|
|
|
$connection_owner = get_userdata( $user_token->external_user_id ); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
return $connection_owner; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Returns true if the provided user is the Jetpack connection owner. |
696
|
|
|
* If user ID is not specified, the current user will be used. |
697
|
|
|
* |
698
|
|
|
* @param Integer|Boolean $user_id the user identifier. False for current user. |
699
|
|
|
* @return Boolean True the user the connection owner, false otherwise. |
700
|
|
|
*/ |
701
|
|
|
public function is_connection_owner( $user_id = false ) { |
702
|
|
|
if ( ! $user_id ) { |
703
|
|
|
$user_id = get_current_user_id(); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
return ( (int) $user_id ) === $this->get_connection_owner_id(); |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* Connects the user with a specified ID to a WordPress.com user using the |
711
|
|
|
* remote login flow. |
712
|
|
|
* |
713
|
|
|
* @access public |
714
|
|
|
* |
715
|
|
|
* @param Integer $user_id (optional) the user identifier, defaults to current user. |
|
|
|
|
716
|
|
|
* @param String $redirect_url the URL to redirect the user to for processing, defaults to |
|
|
|
|
717
|
|
|
* admin_url(). |
718
|
|
|
* @return WP_Error only in case of a failed user lookup. |
719
|
|
|
*/ |
720
|
|
|
public function connect_user( $user_id = null, $redirect_url = null ) { |
721
|
|
|
$user = null; |
|
|
|
|
722
|
|
|
if ( null === $user_id ) { |
723
|
|
|
$user = wp_get_current_user(); |
724
|
|
|
} else { |
725
|
|
|
$user = get_user_by( 'ID', $user_id ); |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
if ( empty( $user ) ) { |
729
|
|
|
return new \WP_Error( 'user_not_found', 'Attempting to connect a non-existent user.' ); |
|
|
|
|
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
if ( null === $redirect_url ) { |
733
|
|
|
$redirect_url = admin_url(); |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
// Using wp_redirect intentionally because we're redirecting outside. |
737
|
|
|
wp_redirect( $this->get_authorization_url( $user, $redirect_url ) ); // phpcs:ignore WordPress.Security.SafeRedirect |
738
|
|
|
exit(); |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
/** |
742
|
|
|
* Unlinks the current user from the linked WordPress.com user. |
743
|
|
|
* |
744
|
|
|
* @access public |
745
|
|
|
* @static |
746
|
|
|
* |
747
|
|
|
* @todo Refactor to properly load the XMLRPC client independently. |
748
|
|
|
* |
749
|
|
|
* @param Integer $user_id the user identifier. |
|
|
|
|
750
|
|
|
* @param bool $can_overwrite_primary_user Allow for the primary user to be disconnected. |
751
|
|
|
* @return Boolean Whether the disconnection of the user was successful. |
752
|
|
|
*/ |
753
|
|
|
public function disconnect_user( $user_id = null, $can_overwrite_primary_user = false ) { |
754
|
|
|
$user_id = empty( $user_id ) ? get_current_user_id() : (int) $user_id; |
755
|
|
|
|
756
|
|
|
$result = $this->get_tokens()->disconnect_user( $user_id, $can_overwrite_primary_user ); |
757
|
|
|
|
758
|
|
|
if ( $result ) { |
759
|
|
|
$xml = new \Jetpack_IXR_Client( compact( 'user_id' ) ); |
760
|
|
|
$xml->query( 'jetpack.unlink_user', $user_id ); |
761
|
|
|
|
762
|
|
|
// Delete cached connected user data. |
763
|
|
|
$transient_key = "jetpack_connected_user_data_$user_id"; |
764
|
|
|
delete_transient( $transient_key ); |
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* Fires after the current user has been unlinked from WordPress.com. |
768
|
|
|
* |
769
|
|
|
* @since 4.1.0 |
770
|
|
|
* |
771
|
|
|
* @param int $user_id The current user's ID. |
772
|
|
|
*/ |
773
|
|
|
do_action( 'jetpack_unlinked_user', $user_id ); |
774
|
|
|
} |
775
|
|
|
return $result; |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Returns the requested Jetpack API URL. |
780
|
|
|
* |
781
|
|
|
* @param String $relative_url the relative API path. |
782
|
|
|
* @return String API URL. |
783
|
|
|
*/ |
784
|
|
|
public function api_url( $relative_url ) { |
785
|
|
|
$api_base = Constants::get_constant( 'JETPACK__API_BASE' ); |
786
|
|
|
$api_version = '/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/'; |
787
|
|
|
|
788
|
|
|
/** |
789
|
|
|
* Filters whether the connection manager should use the iframe authorization |
790
|
|
|
* flow instead of the regular redirect-based flow. |
791
|
|
|
* |
792
|
|
|
* @since 8.3.0 |
793
|
|
|
* |
794
|
|
|
* @param Boolean $is_iframe_flow_used should the iframe flow be used, defaults to false. |
795
|
|
|
*/ |
796
|
|
|
$iframe_flow = apply_filters( 'jetpack_use_iframe_authorization_flow', false ); |
797
|
|
|
|
798
|
|
|
// Do not modify anything that is not related to authorize requests. |
799
|
|
|
if ( 'authorize' === $relative_url && $iframe_flow ) { |
800
|
|
|
$relative_url = 'authorize_iframe'; |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* Filters the API URL that Jetpack uses for server communication. |
805
|
|
|
* |
806
|
|
|
* @since 8.0.0 |
807
|
|
|
* |
808
|
|
|
* @param String $url the generated URL. |
809
|
|
|
* @param String $relative_url the relative URL that was passed as an argument. |
810
|
|
|
* @param String $api_base the API base string that is being used. |
811
|
|
|
* @param String $api_version the API version string that is being used. |
812
|
|
|
*/ |
813
|
|
|
return apply_filters( |
814
|
|
|
'jetpack_api_url', |
815
|
|
|
rtrim( $api_base . $relative_url, '/\\' ) . $api_version, |
816
|
|
|
$relative_url, |
|
|
|
|
817
|
|
|
$api_base, |
818
|
|
|
$api_version |
819
|
|
|
); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* Returns the Jetpack XMLRPC WordPress.com API endpoint URL. |
824
|
|
|
* |
825
|
|
|
* @return String XMLRPC API URL. |
826
|
|
|
*/ |
827
|
|
|
public function xmlrpc_api_url() { |
828
|
|
|
$base = preg_replace( |
829
|
|
|
'#(https?://[^?/]+)(/?.*)?$#', |
830
|
|
|
'\\1', |
831
|
|
|
Constants::get_constant( 'JETPACK__API_BASE' ) |
832
|
|
|
); |
833
|
|
|
return untrailingslashit( $base ) . '/xmlrpc.php'; |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* Attempts Jetpack registration which sets up the site for connection. Should |
838
|
|
|
* remain public because the call to action comes from the current site, not from |
839
|
|
|
* WordPress.com. |
840
|
|
|
* |
841
|
|
|
* @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
842
|
|
|
* @return true|WP_Error The error object. |
843
|
|
|
*/ |
844
|
|
|
public function register( $api_endpoint = 'register' ) { |
845
|
|
|
add_action( 'pre_update_jetpack_option_register', array( '\\Jetpack_Options', 'delete_option' ) ); |
846
|
|
|
$secrets = ( new Secrets() )->generate( 'register', get_current_user_id(), 600 ); |
847
|
|
|
|
848
|
|
|
if ( false === $secrets ) { |
849
|
|
|
return new WP_Error( 'cannot_save_secrets', __( 'Jetpack experienced an issue trying to save options (cannot_save_secrets). We suggest that you contact your hosting provider, and ask them for help checking that the options table is writable on your site.', 'jetpack' ) ); |
|
|
|
|
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
if ( |
853
|
|
|
empty( $secrets['secret_1'] ) || |
854
|
|
|
empty( $secrets['secret_2'] ) || |
855
|
|
|
empty( $secrets['exp'] ) |
856
|
|
|
) { |
857
|
|
|
return new \WP_Error( 'missing_secrets' ); |
|
|
|
|
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
// Better to try (and fail) to set a higher timeout than this system |
861
|
|
|
// supports than to have register fail for more users than it should. |
862
|
|
|
$timeout = $this->set_min_time_limit( 60 ) / 2; |
863
|
|
|
|
864
|
|
|
$gmt_offset = get_option( 'gmt_offset' ); |
865
|
|
|
if ( ! $gmt_offset ) { |
866
|
|
|
$gmt_offset = 0; |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
$stats_options = get_option( 'stats_options' ); |
870
|
|
|
$stats_id = isset( $stats_options['blog_id'] ) |
871
|
|
|
? $stats_options['blog_id'] |
872
|
|
|
: null; |
873
|
|
|
|
874
|
|
|
/** |
875
|
|
|
* Filters the request body for additional property addition. |
876
|
|
|
* |
877
|
|
|
* @since 7.7.0 |
878
|
|
|
* |
879
|
|
|
* @param array $post_data request data. |
880
|
|
|
* @param Array $token_data token data. |
881
|
|
|
*/ |
882
|
|
|
$body = apply_filters( |
883
|
|
|
'jetpack_register_request_body', |
884
|
|
|
array( |
885
|
|
|
'siteurl' => site_url(), |
886
|
|
|
'home' => home_url(), |
887
|
|
|
'gmt_offset' => $gmt_offset, |
888
|
|
|
'timezone_string' => (string) get_option( 'timezone_string' ), |
889
|
|
|
'site_name' => (string) get_option( 'blogname' ), |
890
|
|
|
'secret_1' => $secrets['secret_1'], |
891
|
|
|
'secret_2' => $secrets['secret_2'], |
892
|
|
|
'site_lang' => get_locale(), |
893
|
|
|
'timeout' => $timeout, |
894
|
|
|
'stats_id' => $stats_id, |
895
|
|
|
'state' => get_current_user_id(), |
896
|
|
|
'site_created' => $this->get_assumed_site_creation_date(), |
897
|
|
|
'jetpack_version' => Constants::get_constant( 'JETPACK__VERSION' ), |
898
|
|
|
'ABSPATH' => Constants::get_constant( 'ABSPATH' ), |
899
|
|
|
'current_user_email' => wp_get_current_user()->user_email, |
900
|
|
|
'connect_plugin' => $this->get_plugin() ? $this->get_plugin()->get_slug() : null, |
901
|
|
|
) |
902
|
|
|
); |
903
|
|
|
|
904
|
|
|
$args = array( |
905
|
|
|
'method' => 'POST', |
906
|
|
|
'body' => $body, |
907
|
|
|
'headers' => array( |
908
|
|
|
'Accept' => 'application/json', |
909
|
|
|
), |
910
|
|
|
'timeout' => $timeout, |
911
|
|
|
); |
912
|
|
|
|
913
|
|
|
$args['body'] = $this->apply_activation_source_to_args( $args['body'] ); |
914
|
|
|
|
915
|
|
|
// TODO: fix URLs for bad hosts. |
916
|
|
|
$response = Client::_wp_remote_request( |
917
|
|
|
$this->api_url( $api_endpoint ), |
918
|
|
|
$args, |
919
|
|
|
true |
920
|
|
|
); |
921
|
|
|
|
922
|
|
|
// Make sure the response is valid and does not contain any Jetpack errors. |
923
|
|
|
$registration_details = $this->validate_remote_register_response( $response ); |
924
|
|
|
|
925
|
|
|
if ( is_wp_error( $registration_details ) ) { |
926
|
|
|
return $registration_details; |
927
|
|
|
} elseif ( ! $registration_details ) { |
928
|
|
|
return new \WP_Error( |
929
|
|
|
'unknown_error', |
|
|
|
|
930
|
|
|
'Unknown error registering your Jetpack site.', |
931
|
|
|
wp_remote_retrieve_response_code( $response ) |
932
|
|
|
); |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
if ( empty( $registration_details->jetpack_secret ) || ! is_string( $registration_details->jetpack_secret ) ) { |
936
|
|
|
return new \WP_Error( |
937
|
|
|
'jetpack_secret', |
|
|
|
|
938
|
|
|
'Unable to validate registration of your Jetpack site.', |
939
|
|
|
wp_remote_retrieve_response_code( $response ) |
940
|
|
|
); |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
if ( isset( $registration_details->jetpack_public ) ) { |
944
|
|
|
$jetpack_public = (int) $registration_details->jetpack_public; |
945
|
|
|
} else { |
946
|
|
|
$jetpack_public = false; |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
\Jetpack_Options::update_options( |
950
|
|
|
array( |
951
|
|
|
'id' => (int) $registration_details->jetpack_id, |
952
|
|
|
'public' => $jetpack_public, |
953
|
|
|
) |
954
|
|
|
); |
955
|
|
|
|
956
|
|
|
$this->get_tokens()->update_blog_token( (string) $registration_details->jetpack_secret ); |
957
|
|
|
|
958
|
|
|
/** |
959
|
|
|
* Fires when a site is registered on WordPress.com. |
960
|
|
|
* |
961
|
|
|
* @since 3.7.0 |
962
|
|
|
* |
963
|
|
|
* @param int $json->jetpack_id Jetpack Blog ID. |
964
|
|
|
* @param string $json->jetpack_secret Jetpack Blog Token. |
965
|
|
|
* @param int|bool $jetpack_public Is the site public. |
966
|
|
|
*/ |
967
|
|
|
do_action( |
968
|
|
|
'jetpack_site_registered', |
969
|
|
|
$registration_details->jetpack_id, |
970
|
|
|
$registration_details->jetpack_secret, |
971
|
|
|
$jetpack_public |
972
|
|
|
); |
973
|
|
|
|
974
|
|
|
if ( isset( $registration_details->token ) ) { |
975
|
|
|
/** |
976
|
|
|
* Fires when a user token is sent along with the registration data. |
977
|
|
|
* |
978
|
|
|
* @since 7.6.0 |
979
|
|
|
* |
980
|
|
|
* @param object $token the administrator token for the newly registered site. |
981
|
|
|
*/ |
982
|
|
|
do_action( 'jetpack_site_registered_user_token', $registration_details->token ); |
983
|
|
|
} |
984
|
|
|
|
985
|
|
|
return true; |
986
|
|
|
} |
987
|
|
|
|
988
|
|
|
/** |
989
|
|
|
* Takes the response from the Jetpack register new site endpoint and |
990
|
|
|
* verifies it worked properly. |
991
|
|
|
* |
992
|
|
|
* @since 2.6 |
993
|
|
|
* |
994
|
|
|
* @param Mixed $response the response object, or the error object. |
995
|
|
|
* @return string|WP_Error A JSON object on success or WP_Error on failures |
996
|
|
|
**/ |
997
|
|
|
protected function validate_remote_register_response( $response ) { |
998
|
|
|
if ( is_wp_error( $response ) ) { |
999
|
|
|
return new \WP_Error( |
1000
|
|
|
'register_http_request_failed', |
|
|
|
|
1001
|
|
|
$response->get_error_message() |
1002
|
|
|
); |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
1006
|
|
|
$entity = wp_remote_retrieve_body( $response ); |
1007
|
|
|
|
1008
|
|
|
if ( $entity ) { |
1009
|
|
|
$registration_response = json_decode( $entity ); |
1010
|
|
|
} else { |
1011
|
|
|
$registration_response = false; |
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
$code_type = (int) ( $code / 100 ); |
1015
|
|
|
if ( 5 === $code_type ) { |
1016
|
|
|
return new \WP_Error( 'wpcom_5??', $code ); |
|
|
|
|
1017
|
|
|
} elseif ( 408 === $code ) { |
1018
|
|
|
return new \WP_Error( 'wpcom_408', $code ); |
|
|
|
|
1019
|
|
|
} elseif ( ! empty( $registration_response->error ) ) { |
1020
|
|
|
if ( |
1021
|
|
|
'xml_rpc-32700' === $registration_response->error |
1022
|
|
|
&& ! function_exists( 'xml_parser_create' ) |
1023
|
|
|
) { |
1024
|
|
|
$error_description = __( "PHP's XML extension is not available. Jetpack requires the XML extension to communicate with WordPress.com. Please contact your hosting provider to enable PHP's XML extension.", 'jetpack' ); |
1025
|
|
|
} else { |
1026
|
|
|
$error_description = isset( $registration_response->error_description ) |
1027
|
|
|
? (string) $registration_response->error_description |
1028
|
|
|
: ''; |
1029
|
|
|
} |
1030
|
|
|
|
1031
|
|
|
return new \WP_Error( |
1032
|
|
|
(string) $registration_response->error, |
|
|
|
|
1033
|
|
|
$error_description, |
1034
|
|
|
$code |
1035
|
|
|
); |
1036
|
|
|
} elseif ( 200 !== $code ) { |
1037
|
|
|
return new \WP_Error( 'wpcom_bad_response', $code ); |
|
|
|
|
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
// Jetpack ID error block. |
1041
|
|
|
if ( empty( $registration_response->jetpack_id ) ) { |
1042
|
|
|
return new \WP_Error( |
1043
|
|
|
'jetpack_id', |
|
|
|
|
1044
|
|
|
/* translators: %s is an error message string */ |
1045
|
|
|
sprintf( __( 'Error Details: Jetpack ID is empty. Do not publicly post this error message! %s', 'jetpack' ), $entity ), |
1046
|
|
|
$entity |
1047
|
|
|
); |
1048
|
|
|
} elseif ( ! is_scalar( $registration_response->jetpack_id ) ) { |
1049
|
|
|
return new \WP_Error( |
1050
|
|
|
'jetpack_id', |
|
|
|
|
1051
|
|
|
/* translators: %s is an error message string */ |
1052
|
|
|
sprintf( __( 'Error Details: Jetpack ID is not a scalar. Do not publicly post this error message! %s', 'jetpack' ), $entity ), |
1053
|
|
|
$entity |
1054
|
|
|
); |
1055
|
|
View Code Duplication |
} elseif ( preg_match( '/[^0-9]/', $registration_response->jetpack_id ) ) { |
1056
|
|
|
return new \WP_Error( |
1057
|
|
|
'jetpack_id', |
|
|
|
|
1058
|
|
|
/* translators: %s is an error message string */ |
1059
|
|
|
sprintf( __( 'Error Details: Jetpack ID begins with a numeral. Do not publicly post this error message! %s', 'jetpack' ), $entity ), |
1060
|
|
|
$entity |
1061
|
|
|
); |
1062
|
|
|
} |
1063
|
|
|
|
1064
|
|
|
return $registration_response; |
1065
|
|
|
} |
1066
|
|
|
|
1067
|
|
|
/** |
1068
|
|
|
* Adds a used nonce to a list of known nonces. |
1069
|
|
|
* |
1070
|
|
|
* @param int $timestamp the current request timestamp. |
1071
|
|
|
* @param string $nonce the nonce value. |
1072
|
|
|
* @return bool whether the nonce is unique or not. |
1073
|
|
|
* |
1074
|
|
|
* @deprecated since 9.5.0 |
1075
|
|
|
* @see Nonce_Handler::add() |
1076
|
|
|
*/ |
1077
|
|
|
public function add_nonce( $timestamp, $nonce ) { |
1078
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5.0', 'Automattic\\Jetpack\\Connection\\Nonce_Handler::add' ); |
1079
|
|
|
return ( new Nonce_Handler() )->add( $timestamp, $nonce ); |
1080
|
|
|
} |
1081
|
|
|
|
1082
|
|
|
/** |
1083
|
|
|
* Cleans nonces that were saved when calling ::add_nonce. |
1084
|
|
|
* |
1085
|
|
|
* @todo Properly prepare the query before executing it. |
1086
|
|
|
* |
1087
|
|
|
* @param bool $all whether to clean even non-expired nonces. |
1088
|
|
|
* |
1089
|
|
|
* @deprecated since 9.5.0 |
1090
|
|
|
* @see Nonce_Handler::clean_all() |
1091
|
|
|
*/ |
1092
|
|
|
public function clean_nonces( $all = false ) { |
1093
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5.0', 'Automattic\\Jetpack\\Connection\\Nonce_Handler::clean_all' ); |
1094
|
|
|
( new Nonce_Handler() )->clean_all( $all ? PHP_INT_MAX : ( time() - Nonce_Handler::LIFETIME ) ); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
/** |
1098
|
|
|
* Sets the Connection custom capabilities. |
1099
|
|
|
* |
1100
|
|
|
* @param string[] $caps Array of the user's capabilities. |
1101
|
|
|
* @param string $cap Capability name. |
1102
|
|
|
* @param int $user_id The user ID. |
1103
|
|
|
* @param array $args Adds the context to the cap. Typically the object ID. |
1104
|
|
|
*/ |
1105
|
|
|
public function jetpack_connection_custom_caps( $caps, $cap, $user_id, $args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
1106
|
|
|
switch ( $cap ) { |
1107
|
|
|
case 'jetpack_connect': |
1108
|
|
|
case 'jetpack_reconnect': |
1109
|
|
|
$is_offline_mode = ( new Status() )->is_offline_mode(); |
1110
|
|
|
if ( $is_offline_mode ) { |
1111
|
|
|
$caps = array( 'do_not_allow' ); |
1112
|
|
|
break; |
1113
|
|
|
} |
1114
|
|
|
// Pass through. If it's not offline mode, these should match disconnect. |
1115
|
|
|
// Let users disconnect if it's offline mode, just in case things glitch. |
1116
|
|
|
case 'jetpack_disconnect': |
1117
|
|
|
/** |
1118
|
|
|
* Filters the jetpack_disconnect capability. |
1119
|
|
|
* |
1120
|
|
|
* @since 8.7.0 |
1121
|
|
|
* |
1122
|
|
|
* @param array An array containing the capability name. |
1123
|
|
|
*/ |
1124
|
|
|
$caps = apply_filters( 'jetpack_disconnect_cap', array( 'manage_options' ) ); |
1125
|
|
|
break; |
1126
|
|
View Code Duplication |
case 'jetpack_connect_user': |
1127
|
|
|
$is_offline_mode = ( new Status() )->is_offline_mode(); |
1128
|
|
|
if ( $is_offline_mode ) { |
1129
|
|
|
$caps = array( 'do_not_allow' ); |
1130
|
|
|
break; |
1131
|
|
|
} |
1132
|
|
|
// With user-less connections in mind, non-admin users can connect their account only if a connection owner exists. |
1133
|
|
|
$caps = $this->has_connected_owner() ? array( 'read' ) : array( 'manage_options' ); |
1134
|
|
|
break; |
1135
|
|
|
} |
1136
|
|
|
return $caps; |
1137
|
|
|
} |
1138
|
|
|
|
1139
|
|
|
/** |
1140
|
|
|
* Builds the timeout limit for queries talking with the wpcom servers. |
1141
|
|
|
* |
1142
|
|
|
* Based on local php max_execution_time in php.ini |
1143
|
|
|
* |
1144
|
|
|
* @since 5.4 |
1145
|
|
|
* @return int |
1146
|
|
|
**/ |
1147
|
|
|
public function get_max_execution_time() { |
1148
|
|
|
$timeout = (int) ini_get( 'max_execution_time' ); |
1149
|
|
|
|
1150
|
|
|
// Ensure exec time set in php.ini. |
1151
|
|
|
if ( ! $timeout ) { |
1152
|
|
|
$timeout = 30; |
1153
|
|
|
} |
1154
|
|
|
return $timeout; |
1155
|
|
|
} |
1156
|
|
|
|
1157
|
|
|
/** |
1158
|
|
|
* Sets a minimum request timeout, and returns the current timeout |
1159
|
|
|
* |
1160
|
|
|
* @since 5.4 |
1161
|
|
|
* @param Integer $min_timeout the minimum timeout value. |
1162
|
|
|
**/ |
1163
|
|
View Code Duplication |
public function set_min_time_limit( $min_timeout ) { |
1164
|
|
|
$timeout = $this->get_max_execution_time(); |
1165
|
|
|
if ( $timeout < $min_timeout ) { |
1166
|
|
|
$timeout = $min_timeout; |
1167
|
|
|
set_time_limit( $timeout ); |
1168
|
|
|
} |
1169
|
|
|
return $timeout; |
1170
|
|
|
} |
1171
|
|
|
|
1172
|
|
|
/** |
1173
|
|
|
* Get our assumed site creation date. |
1174
|
|
|
* Calculated based on the earlier date of either: |
1175
|
|
|
* - Earliest admin user registration date. |
1176
|
|
|
* - Earliest date of post of any post type. |
1177
|
|
|
* |
1178
|
|
|
* @since 7.2.0 |
1179
|
|
|
* |
1180
|
|
|
* @return string Assumed site creation date and time. |
1181
|
|
|
*/ |
1182
|
|
|
public function get_assumed_site_creation_date() { |
1183
|
|
|
$cached_date = get_transient( 'jetpack_assumed_site_creation_date' ); |
1184
|
|
|
if ( ! empty( $cached_date ) ) { |
1185
|
|
|
return $cached_date; |
1186
|
|
|
} |
1187
|
|
|
|
1188
|
|
|
$earliest_registered_users = get_users( |
1189
|
|
|
array( |
1190
|
|
|
'role' => 'administrator', |
1191
|
|
|
'orderby' => 'user_registered', |
1192
|
|
|
'order' => 'ASC', |
1193
|
|
|
'fields' => array( 'user_registered' ), |
1194
|
|
|
'number' => 1, |
1195
|
|
|
) |
1196
|
|
|
); |
1197
|
|
|
$earliest_registration_date = $earliest_registered_users[0]->user_registered; |
1198
|
|
|
|
1199
|
|
|
$earliest_posts = get_posts( |
1200
|
|
|
array( |
1201
|
|
|
'posts_per_page' => 1, |
1202
|
|
|
'post_type' => 'any', |
1203
|
|
|
'post_status' => 'any', |
1204
|
|
|
'orderby' => 'date', |
1205
|
|
|
'order' => 'ASC', |
1206
|
|
|
) |
1207
|
|
|
); |
1208
|
|
|
|
1209
|
|
|
// If there are no posts at all, we'll count only on user registration date. |
1210
|
|
|
if ( $earliest_posts ) { |
1211
|
|
|
$earliest_post_date = $earliest_posts[0]->post_date; |
1212
|
|
|
} else { |
1213
|
|
|
$earliest_post_date = PHP_INT_MAX; |
1214
|
|
|
} |
1215
|
|
|
|
1216
|
|
|
$assumed_date = min( $earliest_registration_date, $earliest_post_date ); |
1217
|
|
|
set_transient( 'jetpack_assumed_site_creation_date', $assumed_date ); |
1218
|
|
|
|
1219
|
|
|
return $assumed_date; |
1220
|
|
|
} |
1221
|
|
|
|
1222
|
|
|
/** |
1223
|
|
|
* Adds the activation source string as a parameter to passed arguments. |
1224
|
|
|
* |
1225
|
|
|
* @todo Refactor to use rawurlencode() instead of urlencode(). |
1226
|
|
|
* |
1227
|
|
|
* @param array $args arguments that need to have the source added. |
1228
|
|
|
* @return array $amended arguments. |
1229
|
|
|
*/ |
1230
|
|
View Code Duplication |
public static function apply_activation_source_to_args( $args ) { |
1231
|
|
|
list( $activation_source_name, $activation_source_keyword ) = get_option( 'jetpack_activation_source' ); |
1232
|
|
|
|
1233
|
|
|
if ( $activation_source_name ) { |
1234
|
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode |
1235
|
|
|
$args['_as'] = urlencode( $activation_source_name ); |
1236
|
|
|
} |
1237
|
|
|
|
1238
|
|
|
if ( $activation_source_keyword ) { |
1239
|
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode |
1240
|
|
|
$args['_ak'] = urlencode( $activation_source_keyword ); |
1241
|
|
|
} |
1242
|
|
|
|
1243
|
|
|
return $args; |
1244
|
|
|
} |
1245
|
|
|
|
1246
|
|
|
/** |
1247
|
|
|
* Generates two secret tokens and the end of life timestamp for them. |
1248
|
|
|
* |
1249
|
|
|
* @param String $action The action name. |
1250
|
|
|
* @param Integer $user_id The user identifier. |
|
|
|
|
1251
|
|
|
* @param Integer $exp Expiration time in seconds. |
1252
|
|
|
*/ |
1253
|
|
|
public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
1254
|
|
|
return ( new Secrets() )->generate( $action, $user_id, $exp ); |
1255
|
|
|
} |
1256
|
|
|
|
1257
|
|
|
/** |
1258
|
|
|
* Returns two secret tokens and the end of life timestamp for them. |
1259
|
|
|
* |
1260
|
|
|
* @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->get() instead. |
1261
|
|
|
* |
1262
|
|
|
* @param String $action The action name. |
1263
|
|
|
* @param Integer $user_id The user identifier. |
1264
|
|
|
* @return string|array an array of secrets or an error string. |
1265
|
|
|
*/ |
1266
|
|
|
public function get_secrets( $action, $user_id ) { |
1267
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Secrets->get' ); |
1268
|
|
|
return ( new Secrets() )->get( $action, $user_id ); |
1269
|
|
|
} |
1270
|
|
|
|
1271
|
|
|
/** |
1272
|
|
|
* Deletes secret tokens in case they, for example, have expired. |
1273
|
|
|
* |
1274
|
|
|
* @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->delete() instead. |
1275
|
|
|
* |
1276
|
|
|
* @param String $action The action name. |
1277
|
|
|
* @param Integer $user_id The user identifier. |
1278
|
|
|
*/ |
1279
|
|
|
public function delete_secrets( $action, $user_id ) { |
1280
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Secrets->delete' ); |
1281
|
|
|
( new Secrets() )->delete( $action, $user_id ); |
1282
|
|
|
} |
1283
|
|
|
|
1284
|
|
|
/** |
1285
|
|
|
* Deletes all connection tokens and transients from the local Jetpack site. |
1286
|
|
|
* If the plugin object has been provided in the constructor, the function first checks |
1287
|
|
|
* whether it's the only active connection. |
1288
|
|
|
* If there are any other connections, the function will do nothing and return `false` |
1289
|
|
|
* (unless `$ignore_connected_plugins` is set to `true`). |
1290
|
|
|
* |
1291
|
|
|
* @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
1292
|
|
|
* |
1293
|
|
|
* @return bool True if disconnected successfully, false otherwise. |
1294
|
|
|
*/ |
1295
|
|
|
public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
1296
|
|
|
// refuse to delete if we're not the last Jetpack plugin installed. |
1297
|
|
View Code Duplication |
if ( ! $ignore_connected_plugins && null !== $this->plugin && ! $this->plugin->is_only() ) { |
1298
|
|
|
return false; |
1299
|
|
|
} |
1300
|
|
|
|
1301
|
|
|
/** |
1302
|
|
|
* Fires upon the disconnect attempt. |
1303
|
|
|
* Return `false` to prevent the disconnect. |
1304
|
|
|
* |
1305
|
|
|
* @since 8.7.0 |
1306
|
|
|
*/ |
1307
|
|
|
if ( ! apply_filters( 'jetpack_connection_delete_all_tokens', true ) ) { |
1308
|
|
|
return false; |
1309
|
|
|
} |
1310
|
|
|
|
1311
|
|
|
\Jetpack_Options::delete_option( |
1312
|
|
|
array( |
1313
|
|
|
'master_user', |
1314
|
|
|
'time_diff', |
1315
|
|
|
'fallback_no_verify_ssl_certs', |
1316
|
|
|
) |
1317
|
|
|
); |
1318
|
|
|
|
1319
|
|
|
( new Secrets() )->delete_all(); |
1320
|
|
|
$this->get_tokens()->delete_all(); |
1321
|
|
|
|
1322
|
|
|
// Delete cached connected user data. |
1323
|
|
|
$transient_key = 'jetpack_connected_user_data_' . get_current_user_id(); |
1324
|
|
|
delete_transient( $transient_key ); |
1325
|
|
|
|
1326
|
|
|
// Delete all XML-RPC errors. |
1327
|
|
|
Error_Handler::get_instance()->delete_all_errors(); |
1328
|
|
|
|
1329
|
|
|
return true; |
1330
|
|
|
} |
1331
|
|
|
|
1332
|
|
|
/** |
1333
|
|
|
* Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
1334
|
|
|
* If the plugin object has been provided in the constructor, the function first check |
1335
|
|
|
* whether it's the only active connection. |
1336
|
|
|
* If there are any other connections, the function will do nothing and return `false` |
1337
|
|
|
* (unless `$ignore_connected_plugins` is set to `true`). |
1338
|
|
|
* |
1339
|
|
|
* @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
1340
|
|
|
* |
1341
|
|
|
* @return bool True if disconnected successfully, false otherwise. |
1342
|
|
|
*/ |
1343
|
|
|
public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
1344
|
|
View Code Duplication |
if ( ! $ignore_connected_plugins && null !== $this->plugin && ! $this->plugin->is_only() ) { |
1345
|
|
|
return false; |
1346
|
|
|
} |
1347
|
|
|
|
1348
|
|
|
/** |
1349
|
|
|
* Fires upon the disconnect attempt. |
1350
|
|
|
* Return `false` to prevent the disconnect. |
1351
|
|
|
* |
1352
|
|
|
* @since 8.7.0 |
1353
|
|
|
*/ |
1354
|
|
|
if ( ! apply_filters( 'jetpack_connection_disconnect_site_wpcom', true, $this ) ) { |
|
|
|
|
1355
|
|
|
return false; |
1356
|
|
|
} |
1357
|
|
|
|
1358
|
|
|
$xml = new \Jetpack_IXR_Client(); |
1359
|
|
|
$xml->query( 'jetpack.deregister', get_current_user_id() ); |
1360
|
|
|
|
1361
|
|
|
return true; |
1362
|
|
|
} |
1363
|
|
|
|
1364
|
|
|
/** |
1365
|
|
|
* Disconnect the plugin and remove the tokens. |
1366
|
|
|
* This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
1367
|
|
|
* This is a proxy method to simplify the Connection package API. |
1368
|
|
|
* |
1369
|
|
|
* @see Manager::disable_plugin() |
1370
|
|
|
* @see Manager::disconnect_site_wpcom() |
1371
|
|
|
* @see Manager::delete_all_connection_tokens() |
1372
|
|
|
* |
1373
|
|
|
* @return bool |
1374
|
|
|
*/ |
1375
|
|
|
public function remove_connection() { |
1376
|
|
|
$this->disable_plugin(); |
1377
|
|
|
$this->disconnect_site_wpcom(); |
1378
|
|
|
$this->delete_all_connection_tokens(); |
1379
|
|
|
|
1380
|
|
|
return true; |
1381
|
|
|
} |
1382
|
|
|
|
1383
|
|
|
/** |
1384
|
|
|
* Completely clearing up the connection, and initiating reconnect. |
1385
|
|
|
* |
1386
|
|
|
* @return true|WP_Error True if reconnected successfully, a `WP_Error` object otherwise. |
1387
|
|
|
*/ |
1388
|
|
|
public function reconnect() { |
1389
|
|
|
( new Tracking() )->record_user_event( 'restore_connection_reconnect' ); |
1390
|
|
|
|
1391
|
|
|
$this->disconnect_site_wpcom( true ); |
1392
|
|
|
$this->delete_all_connection_tokens( true ); |
1393
|
|
|
|
1394
|
|
|
return $this->register(); |
1395
|
|
|
} |
1396
|
|
|
|
1397
|
|
|
/** |
1398
|
|
|
* Validate the tokens, and refresh the invalid ones. |
1399
|
|
|
* |
1400
|
|
|
* @return string|true|WP_Error True if connection restored or string indicating what's to be done next. A `WP_Error` object otherwise. |
1401
|
|
|
*/ |
1402
|
|
|
public function restore() { |
1403
|
|
|
// If this is a userless connection we need to trigger a full reconnection as our only secure means of |
1404
|
|
|
// communication with WPCOM, aka the blog token, is compromised. |
1405
|
|
|
if ( $this->is_userless() ) { |
1406
|
|
|
return $this->reconnect(); |
1407
|
|
|
} |
1408
|
|
|
|
1409
|
|
|
$validate_tokens_response = $this->get_tokens()->validate(); |
1410
|
|
|
|
1411
|
|
|
$blog_token_healthy = $validate_tokens_response['blog_token']['is_healthy']; |
1412
|
|
|
$user_token_healthy = $validate_tokens_response['user_token']['is_healthy']; |
1413
|
|
|
|
1414
|
|
|
// Tokens are both valid, or both invalid. We can't fix the problem we don't see, so the full reconnection is needed. |
1415
|
|
|
if ( $blog_token_healthy === $user_token_healthy ) { |
1416
|
|
|
$result = $this->reconnect(); |
1417
|
|
|
return ( true === $result ) ? 'authorize' : $result; |
1418
|
|
|
} |
1419
|
|
|
|
1420
|
|
|
if ( ! $blog_token_healthy ) { |
1421
|
|
|
return $this->refresh_blog_token(); |
1422
|
|
|
} |
1423
|
|
|
|
1424
|
|
|
if ( ! $user_token_healthy ) { |
1425
|
|
|
return ( true === $this->refresh_user_token() ) ? 'authorize' : false; |
1426
|
|
|
} |
1427
|
|
|
|
1428
|
|
|
return false; |
1429
|
|
|
} |
1430
|
|
|
|
1431
|
|
|
/** |
1432
|
|
|
* Responds to a WordPress.com call to register the current site. |
1433
|
|
|
* Should be changed to protected. |
1434
|
|
|
* |
1435
|
|
|
* @param array $registration_data Array of [ secret_1, user_id ]. |
1436
|
|
|
*/ |
1437
|
|
|
public function handle_registration( array $registration_data ) { |
1438
|
|
|
list( $registration_secret_1, $registration_user_id ) = $registration_data; |
1439
|
|
|
if ( empty( $registration_user_id ) ) { |
1440
|
|
|
return new \WP_Error( 'registration_state_invalid', __( 'Invalid Registration State', 'jetpack' ), 400 ); |
|
|
|
|
1441
|
|
|
} |
1442
|
|
|
|
1443
|
|
|
return ( new Secrets() )->verify( 'register', $registration_secret_1, (int) $registration_user_id ); |
1444
|
|
|
} |
1445
|
|
|
|
1446
|
|
|
/** |
1447
|
|
|
* Perform the API request to validate the blog and user tokens. |
1448
|
|
|
* |
1449
|
|
|
* @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->validate_tokens() instead. |
1450
|
|
|
* |
1451
|
|
|
* @param int|null $user_id ID of the user we need to validate token for. Current user's ID by default. |
1452
|
|
|
* |
1453
|
|
|
* @return array|false|WP_Error The API response: `array( 'blog_token_is_healthy' => true|false, 'user_token_is_healthy' => true|false )`. |
1454
|
|
|
*/ |
1455
|
|
|
public function validate_tokens( $user_id = null ) { |
1456
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Tokens->validate' ); |
1457
|
|
|
return $this->get_tokens()->validate( $user_id ); |
1458
|
|
|
} |
1459
|
|
|
|
1460
|
|
|
/** |
1461
|
|
|
* Verify a Previously Generated Secret. |
1462
|
|
|
* |
1463
|
|
|
* @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->verify() instead. |
1464
|
|
|
* |
1465
|
|
|
* @param string $action The type of secret to verify. |
1466
|
|
|
* @param string $secret_1 The secret string to compare to what is stored. |
1467
|
|
|
* @param int $user_id The user ID of the owner of the secret. |
1468
|
|
|
* @return \WP_Error|string WP_Error on failure, secret_2 on success. |
1469
|
|
|
*/ |
1470
|
|
|
public function verify_secrets( $action, $secret_1, $user_id ) { |
1471
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Secrets->verify' ); |
1472
|
|
|
return ( new Secrets() )->verify( $action, $secret_1, $user_id ); |
1473
|
|
|
} |
1474
|
|
|
|
1475
|
|
|
/** |
1476
|
|
|
* Responds to a WordPress.com call to authorize the current user. |
1477
|
|
|
* Should be changed to protected. |
1478
|
|
|
*/ |
1479
|
|
|
public function handle_authorization() { |
1480
|
|
|
|
1481
|
|
|
} |
1482
|
|
|
|
1483
|
|
|
/** |
1484
|
|
|
* Obtains the auth token. |
1485
|
|
|
* |
1486
|
|
|
* @param array $data The request data. |
1487
|
|
|
* @return object|\WP_Error Returns the auth token on success. |
1488
|
|
|
* Returns a \WP_Error on failure. |
1489
|
|
|
*/ |
1490
|
|
|
public function get_token( $data ) { |
1491
|
|
|
return $this->get_tokens()->get( $data, $this->api_url( 'token' ) ); |
1492
|
|
|
} |
1493
|
|
|
|
1494
|
|
|
/** |
1495
|
|
|
* Builds a URL to the Jetpack connection auth page. |
1496
|
|
|
* |
1497
|
|
|
* @param WP_User $user (optional) defaults to the current logged in user. |
|
|
|
|
1498
|
|
|
* @param String $redirect (optional) a redirect URL to use instead of the default. |
|
|
|
|
1499
|
|
|
* @return string Connect URL. |
1500
|
|
|
*/ |
1501
|
|
|
public function get_authorization_url( $user = null, $redirect = null ) { |
1502
|
|
|
|
1503
|
|
|
if ( empty( $user ) ) { |
1504
|
|
|
$user = wp_get_current_user(); |
1505
|
|
|
} |
1506
|
|
|
|
1507
|
|
|
$roles = new Roles(); |
1508
|
|
|
$role = $roles->translate_user_to_role( $user ); |
1509
|
|
|
$signed_role = $this->get_tokens()->sign_role( $role ); |
1510
|
|
|
|
1511
|
|
|
/** |
1512
|
|
|
* Filter the URL of the first time the user gets redirected back to your site for connection |
1513
|
|
|
* data processing. |
1514
|
|
|
* |
1515
|
|
|
* @since 8.0.0 |
1516
|
|
|
* |
1517
|
|
|
* @param string $redirect_url Defaults to the site admin URL. |
1518
|
|
|
*/ |
1519
|
|
|
$processing_url = apply_filters( 'jetpack_connect_processing_url', admin_url( 'admin.php' ) ); |
1520
|
|
|
|
1521
|
|
|
/** |
1522
|
|
|
* Filter the URL to redirect the user back to when the authorization process |
1523
|
|
|
* is complete. |
1524
|
|
|
* |
1525
|
|
|
* @since 8.0.0 |
1526
|
|
|
* |
1527
|
|
|
* @param string $redirect_url Defaults to the site URL. |
1528
|
|
|
*/ |
1529
|
|
|
$redirect = apply_filters( 'jetpack_connect_redirect_url', $redirect ); |
1530
|
|
|
|
1531
|
|
|
$secrets = ( new Secrets() )->generate( 'authorize', $user->ID, 2 * HOUR_IN_SECONDS ); |
1532
|
|
|
|
1533
|
|
|
/** |
1534
|
|
|
* Filter the type of authorization. |
1535
|
|
|
* 'calypso' completes authorization on wordpress.com/jetpack/connect |
1536
|
|
|
* while 'jetpack' ( or any other value ) completes the authorization at jetpack.wordpress.com. |
1537
|
|
|
* |
1538
|
|
|
* @since 4.3.3 |
1539
|
|
|
* |
1540
|
|
|
* @param string $auth_type Defaults to 'calypso', can also be 'jetpack'. |
1541
|
|
|
*/ |
1542
|
|
|
$auth_type = apply_filters( 'jetpack_auth_type', 'calypso' ); |
1543
|
|
|
|
1544
|
|
|
/** |
1545
|
|
|
* Filters the user connection request data for additional property addition. |
1546
|
|
|
* |
1547
|
|
|
* @since 8.0.0 |
1548
|
|
|
* |
1549
|
|
|
* @param array $request_data request data. |
1550
|
|
|
*/ |
1551
|
|
|
$body = apply_filters( |
1552
|
|
|
'jetpack_connect_request_body', |
1553
|
|
|
array( |
1554
|
|
|
'response_type' => 'code', |
1555
|
|
|
'client_id' => \Jetpack_Options::get_option( 'id' ), |
1556
|
|
|
'redirect_uri' => add_query_arg( |
1557
|
|
|
array( |
1558
|
|
|
'handler' => 'jetpack-connection-webhooks', |
1559
|
|
|
'action' => 'authorize', |
1560
|
|
|
'_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
1561
|
|
|
'redirect' => $redirect ? rawurlencode( $redirect ) : false, |
1562
|
|
|
), |
1563
|
|
|
esc_url( $processing_url ) |
1564
|
|
|
), |
1565
|
|
|
'state' => $user->ID, |
1566
|
|
|
'scope' => $signed_role, |
1567
|
|
|
'user_email' => $user->user_email, |
1568
|
|
|
'user_login' => $user->user_login, |
1569
|
|
|
'is_active' => $this->is_active(), // TODO Deprecate this. |
1570
|
|
|
'jp_version' => Constants::get_constant( 'JETPACK__VERSION' ), |
1571
|
|
|
'auth_type' => $auth_type, |
1572
|
|
|
'secret' => $secrets['secret_1'], |
1573
|
|
|
'blogname' => get_option( 'blogname' ), |
1574
|
|
|
'site_url' => site_url(), |
1575
|
|
|
'home_url' => home_url(), |
1576
|
|
|
'site_icon' => get_site_icon_url(), |
1577
|
|
|
'site_lang' => get_locale(), |
1578
|
|
|
'site_created' => $this->get_assumed_site_creation_date(), |
1579
|
|
|
) |
1580
|
|
|
); |
1581
|
|
|
|
1582
|
|
|
$body = $this->apply_activation_source_to_args( urlencode_deep( $body ) ); |
1583
|
|
|
|
1584
|
|
|
$api_url = $this->api_url( 'authorize' ); |
1585
|
|
|
|
1586
|
|
|
return add_query_arg( $body, $api_url ); |
1587
|
|
|
} |
1588
|
|
|
|
1589
|
|
|
/** |
1590
|
|
|
* Authorizes the user by obtaining and storing the user token. |
1591
|
|
|
* |
1592
|
|
|
* @param array $data The request data. |
1593
|
|
|
* @return string|\WP_Error Returns a string on success. |
1594
|
|
|
* Returns a \WP_Error on failure. |
1595
|
|
|
*/ |
1596
|
|
|
public function authorize( $data = array() ) { |
1597
|
|
|
/** |
1598
|
|
|
* Action fired when user authorization starts. |
1599
|
|
|
* |
1600
|
|
|
* @since 8.0.0 |
1601
|
|
|
*/ |
1602
|
|
|
do_action( 'jetpack_authorize_starting' ); |
1603
|
|
|
|
1604
|
|
|
$roles = new Roles(); |
1605
|
|
|
$role = $roles->translate_current_user_to_role(); |
1606
|
|
|
|
1607
|
|
|
if ( ! $role ) { |
1608
|
|
|
return new \WP_Error( 'no_role', 'Invalid request.', 400 ); |
|
|
|
|
1609
|
|
|
} |
1610
|
|
|
|
1611
|
|
|
$cap = $roles->translate_role_to_cap( $role ); |
1612
|
|
|
if ( ! $cap ) { |
1613
|
|
|
return new \WP_Error( 'no_cap', 'Invalid request.', 400 ); |
|
|
|
|
1614
|
|
|
} |
1615
|
|
|
|
1616
|
|
|
if ( ! empty( $data['error'] ) ) { |
1617
|
|
|
return new \WP_Error( $data['error'], 'Error included in the request.', 400 ); |
|
|
|
|
1618
|
|
|
} |
1619
|
|
|
|
1620
|
|
|
if ( ! isset( $data['state'] ) ) { |
1621
|
|
|
return new \WP_Error( 'no_state', 'Request must include state.', 400 ); |
|
|
|
|
1622
|
|
|
} |
1623
|
|
|
|
1624
|
|
|
if ( ! ctype_digit( $data['state'] ) ) { |
1625
|
|
|
return new \WP_Error( $data['error'], 'State must be an integer.', 400 ); |
|
|
|
|
1626
|
|
|
} |
1627
|
|
|
|
1628
|
|
|
$current_user_id = get_current_user_id(); |
1629
|
|
|
if ( $current_user_id !== (int) $data['state'] ) { |
1630
|
|
|
return new \WP_Error( 'wrong_state', 'State does not match current user.', 400 ); |
|
|
|
|
1631
|
|
|
} |
1632
|
|
|
|
1633
|
|
|
if ( empty( $data['code'] ) ) { |
1634
|
|
|
return new \WP_Error( 'no_code', 'Request must include an authorization code.', 400 ); |
|
|
|
|
1635
|
|
|
} |
1636
|
|
|
|
1637
|
|
|
$token = $this->get_tokens()->get( $data, $this->api_url( 'token' ) ); |
1638
|
|
|
|
1639
|
|
View Code Duplication |
if ( is_wp_error( $token ) ) { |
1640
|
|
|
$code = $token->get_error_code(); |
|
|
|
|
1641
|
|
|
if ( empty( $code ) ) { |
1642
|
|
|
$code = 'invalid_token'; |
1643
|
|
|
} |
1644
|
|
|
return new \WP_Error( $code, $token->get_error_message(), 400 ); |
|
|
|
|
1645
|
|
|
} |
1646
|
|
|
|
1647
|
|
|
if ( ! $token ) { |
1648
|
|
|
return new \WP_Error( 'no_token', 'Error generating token.', 400 ); |
|
|
|
|
1649
|
|
|
} |
1650
|
|
|
|
1651
|
|
|
$is_connection_owner = ! $this->has_connected_owner(); |
1652
|
|
|
|
1653
|
|
|
$this->get_tokens()->update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_connection_owner ); |
1654
|
|
|
|
1655
|
|
|
/** |
1656
|
|
|
* Fires after user has successfully received an auth token. |
1657
|
|
|
* |
1658
|
|
|
* @since 3.9.0 |
1659
|
|
|
*/ |
1660
|
|
|
do_action( 'jetpack_user_authorized' ); |
1661
|
|
|
|
1662
|
|
|
if ( ! $is_connection_owner ) { |
1663
|
|
|
/** |
1664
|
|
|
* Action fired when a secondary user has been authorized. |
1665
|
|
|
* |
1666
|
|
|
* @since 8.0.0 |
1667
|
|
|
*/ |
1668
|
|
|
do_action( 'jetpack_authorize_ending_linked' ); |
1669
|
|
|
return 'linked'; |
1670
|
|
|
} |
1671
|
|
|
|
1672
|
|
|
/** |
1673
|
|
|
* Action fired when the master user has been authorized. |
1674
|
|
|
* |
1675
|
|
|
* @since 8.0.0 |
1676
|
|
|
* |
1677
|
|
|
* @param array $data The request data. |
1678
|
|
|
*/ |
1679
|
|
|
do_action( 'jetpack_authorize_ending_authorized', $data ); |
1680
|
|
|
|
1681
|
|
|
\Jetpack_Options::delete_raw_option( 'jetpack_last_connect_url_check' ); |
1682
|
|
|
|
1683
|
|
|
( new Nonce_Handler() )->reschedule(); |
1684
|
|
|
|
1685
|
|
|
return 'authorized'; |
1686
|
|
|
} |
1687
|
|
|
|
1688
|
|
|
/** |
1689
|
|
|
* Disconnects from the Jetpack servers. |
1690
|
|
|
* Forgets all connection details and tells the Jetpack servers to do the same. |
1691
|
|
|
*/ |
1692
|
|
|
public function disconnect_site() { |
1693
|
|
|
|
1694
|
|
|
} |
1695
|
|
|
|
1696
|
|
|
/** |
1697
|
|
|
* The Base64 Encoding of the SHA1 Hash of the Input. |
1698
|
|
|
* |
1699
|
|
|
* @param string $text The string to hash. |
1700
|
|
|
* @return string |
1701
|
|
|
*/ |
1702
|
|
|
public function sha1_base64( $text ) { |
1703
|
|
|
return base64_encode( sha1( $text, true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
1704
|
|
|
} |
1705
|
|
|
|
1706
|
|
|
/** |
1707
|
|
|
* This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
1708
|
|
|
* |
1709
|
|
|
* @param string $domain The domain to check. |
1710
|
|
|
* |
1711
|
|
|
* @return bool|WP_Error |
1712
|
|
|
*/ |
1713
|
|
|
public function is_usable_domain( $domain ) { |
1714
|
|
|
|
1715
|
|
|
// If it's empty, just fail out. |
1716
|
|
|
if ( ! $domain ) { |
1717
|
|
|
return new \WP_Error( |
1718
|
|
|
'fail_domain_empty', |
|
|
|
|
1719
|
|
|
/* translators: %1$s is a domain name. */ |
1720
|
|
|
sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is empty.', 'jetpack' ), $domain ) |
1721
|
|
|
); |
1722
|
|
|
} |
1723
|
|
|
|
1724
|
|
|
/** |
1725
|
|
|
* Skips the usuable domain check when connecting a site. |
1726
|
|
|
* |
1727
|
|
|
* Allows site administrators with domains that fail gethostname-based checks to pass the request to WP.com |
1728
|
|
|
* |
1729
|
|
|
* @since 4.1.0 |
1730
|
|
|
* |
1731
|
|
|
* @param bool If the check should be skipped. Default false. |
1732
|
|
|
*/ |
1733
|
|
|
if ( apply_filters( 'jetpack_skip_usuable_domain_check', false ) ) { |
1734
|
|
|
return true; |
1735
|
|
|
} |
1736
|
|
|
|
1737
|
|
|
// None of the explicit localhosts. |
1738
|
|
|
$forbidden_domains = array( |
1739
|
|
|
'wordpress.com', |
1740
|
|
|
'localhost', |
1741
|
|
|
'localhost.localdomain', |
1742
|
|
|
'127.0.0.1', |
1743
|
|
|
'local.wordpress.test', // VVV pattern. |
1744
|
|
|
'local.wordpress-trunk.test', // VVV pattern. |
1745
|
|
|
'src.wordpress-develop.test', // VVV pattern. |
1746
|
|
|
'build.wordpress-develop.test', // VVV pattern. |
1747
|
|
|
); |
1748
|
|
View Code Duplication |
if ( in_array( $domain, $forbidden_domains, true ) ) { |
1749
|
|
|
return new \WP_Error( |
1750
|
|
|
'fail_domain_forbidden', |
|
|
|
|
1751
|
|
|
sprintf( |
1752
|
|
|
/* translators: %1$s is a domain name. */ |
1753
|
|
|
__( |
1754
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it is in the forbidden array.', |
1755
|
|
|
'jetpack' |
1756
|
|
|
), |
1757
|
|
|
$domain |
1758
|
|
|
) |
1759
|
|
|
); |
1760
|
|
|
} |
1761
|
|
|
|
1762
|
|
|
// No .test or .local domains. |
1763
|
|
View Code Duplication |
if ( preg_match( '#\.(test|local)$#i', $domain ) ) { |
1764
|
|
|
return new \WP_Error( |
1765
|
|
|
'fail_domain_tld', |
|
|
|
|
1766
|
|
|
sprintf( |
1767
|
|
|
/* translators: %1$s is a domain name. */ |
1768
|
|
|
__( |
1769
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it uses an invalid top level domain.', |
1770
|
|
|
'jetpack' |
1771
|
|
|
), |
1772
|
|
|
$domain |
1773
|
|
|
) |
1774
|
|
|
); |
1775
|
|
|
} |
1776
|
|
|
|
1777
|
|
|
// No WPCOM subdomains. |
1778
|
|
View Code Duplication |
if ( preg_match( '#\.WordPress\.com$#i', $domain ) ) { |
1779
|
|
|
return new \WP_Error( |
1780
|
|
|
'fail_subdomain_wpcom', |
|
|
|
|
1781
|
|
|
sprintf( |
1782
|
|
|
/* translators: %1$s is a domain name. */ |
1783
|
|
|
__( |
1784
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it is a subdomain of WordPress.com.', |
1785
|
|
|
'jetpack' |
1786
|
|
|
), |
1787
|
|
|
$domain |
1788
|
|
|
) |
1789
|
|
|
); |
1790
|
|
|
} |
1791
|
|
|
|
1792
|
|
|
// If PHP was compiled without support for the Filter module (very edge case). |
1793
|
|
|
if ( ! function_exists( 'filter_var' ) ) { |
1794
|
|
|
// Just pass back true for now, and let wpcom sort it out. |
1795
|
|
|
return true; |
1796
|
|
|
} |
1797
|
|
|
|
1798
|
|
|
return true; |
1799
|
|
|
} |
1800
|
|
|
|
1801
|
|
|
/** |
1802
|
|
|
* Gets the requested token. |
1803
|
|
|
* |
1804
|
|
|
* @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->get_access_token() instead. |
1805
|
|
|
* |
1806
|
|
|
* @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
1807
|
|
|
* @param string|false $token_key If provided, check that the token matches the provided input. |
1808
|
|
|
* @param bool|true $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found. |
1809
|
|
|
* |
1810
|
|
|
* @return object|false |
1811
|
|
|
* |
1812
|
|
|
* @see $this->get_tokens()->get_access_token() |
1813
|
|
|
*/ |
1814
|
|
|
public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
1815
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Tokens->get_access_token' ); |
1816
|
|
|
return $this->get_tokens()->get_access_token( $user_id, $token_key, $suppress_errors ); |
1817
|
|
|
} |
1818
|
|
|
|
1819
|
|
|
/** |
1820
|
|
|
* In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
1821
|
|
|
* since it is passed by reference to various methods. |
1822
|
|
|
* Capture it here so we can verify the signature later. |
1823
|
|
|
* |
1824
|
|
|
* @param array $methods an array of available XMLRPC methods. |
1825
|
|
|
* @return array the same array, since this method doesn't add or remove anything. |
1826
|
|
|
*/ |
1827
|
|
|
public function xmlrpc_methods( $methods ) { |
1828
|
|
|
$this->raw_post_data = isset( $GLOBALS['HTTP_RAW_POST_DATA'] ) ? $GLOBALS['HTTP_RAW_POST_DATA'] : null; |
1829
|
|
|
return $methods; |
1830
|
|
|
} |
1831
|
|
|
|
1832
|
|
|
/** |
1833
|
|
|
* Resets the raw post data parameter for testing purposes. |
1834
|
|
|
*/ |
1835
|
|
|
public function reset_raw_post_data() { |
1836
|
|
|
$this->raw_post_data = null; |
1837
|
|
|
} |
1838
|
|
|
|
1839
|
|
|
/** |
1840
|
|
|
* Registering an additional method. |
1841
|
|
|
* |
1842
|
|
|
* @param array $methods an array of available XMLRPC methods. |
1843
|
|
|
* @return array the amended array in case the method is added. |
1844
|
|
|
*/ |
1845
|
|
|
public function public_xmlrpc_methods( $methods ) { |
1846
|
|
|
if ( array_key_exists( 'wp.getOptions', $methods ) ) { |
1847
|
|
|
$methods['wp.getOptions'] = array( $this, 'jetpack_get_options' ); |
1848
|
|
|
} |
1849
|
|
|
return $methods; |
1850
|
|
|
} |
1851
|
|
|
|
1852
|
|
|
/** |
1853
|
|
|
* Handles a getOptions XMLRPC method call. |
1854
|
|
|
* |
1855
|
|
|
* @param array $args method call arguments. |
1856
|
|
|
* @return an amended XMLRPC server options array. |
1857
|
|
|
*/ |
1858
|
|
|
public function jetpack_get_options( $args ) { |
1859
|
|
|
global $wp_xmlrpc_server; |
1860
|
|
|
|
1861
|
|
|
$wp_xmlrpc_server->escape( $args ); |
1862
|
|
|
|
1863
|
|
|
$username = $args[1]; |
1864
|
|
|
$password = $args[2]; |
1865
|
|
|
|
1866
|
|
|
$user = $wp_xmlrpc_server->login( $username, $password ); |
1867
|
|
|
if ( ! $user ) { |
1868
|
|
|
return $wp_xmlrpc_server->error; |
1869
|
|
|
} |
1870
|
|
|
|
1871
|
|
|
$options = array(); |
1872
|
|
|
$user_data = $this->get_connected_user_data(); |
1873
|
|
|
if ( is_array( $user_data ) ) { |
1874
|
|
|
$options['jetpack_user_id'] = array( |
1875
|
|
|
'desc' => __( 'The WP.com user ID of the connected user', 'jetpack' ), |
1876
|
|
|
'readonly' => true, |
1877
|
|
|
'value' => $user_data['ID'], |
1878
|
|
|
); |
1879
|
|
|
$options['jetpack_user_login'] = array( |
1880
|
|
|
'desc' => __( 'The WP.com username of the connected user', 'jetpack' ), |
1881
|
|
|
'readonly' => true, |
1882
|
|
|
'value' => $user_data['login'], |
1883
|
|
|
); |
1884
|
|
|
$options['jetpack_user_email'] = array( |
1885
|
|
|
'desc' => __( 'The WP.com user email of the connected user', 'jetpack' ), |
1886
|
|
|
'readonly' => true, |
1887
|
|
|
'value' => $user_data['email'], |
1888
|
|
|
); |
1889
|
|
|
$options['jetpack_user_site_count'] = array( |
1890
|
|
|
'desc' => __( 'The number of sites of the connected WP.com user', 'jetpack' ), |
1891
|
|
|
'readonly' => true, |
1892
|
|
|
'value' => $user_data['site_count'], |
1893
|
|
|
); |
1894
|
|
|
} |
1895
|
|
|
$wp_xmlrpc_server->blog_options = array_merge( $wp_xmlrpc_server->blog_options, $options ); |
1896
|
|
|
$args = stripslashes_deep( $args ); |
1897
|
|
|
return $wp_xmlrpc_server->wp_getOptions( $args ); |
1898
|
|
|
} |
1899
|
|
|
|
1900
|
|
|
/** |
1901
|
|
|
* Adds Jetpack-specific options to the output of the XMLRPC options method. |
1902
|
|
|
* |
1903
|
|
|
* @param array $options standard Core options. |
1904
|
|
|
* @return array amended options. |
1905
|
|
|
*/ |
1906
|
|
|
public function xmlrpc_options( $options ) { |
1907
|
|
|
$jetpack_client_id = false; |
1908
|
|
|
if ( $this->is_connected() ) { |
1909
|
|
|
$jetpack_client_id = \Jetpack_Options::get_option( 'id' ); |
1910
|
|
|
} |
1911
|
|
|
$options['jetpack_version'] = array( |
1912
|
|
|
'desc' => __( 'Jetpack Plugin Version', 'jetpack' ), |
1913
|
|
|
'readonly' => true, |
1914
|
|
|
'value' => Constants::get_constant( 'JETPACK__VERSION' ), |
1915
|
|
|
); |
1916
|
|
|
|
1917
|
|
|
$options['jetpack_client_id'] = array( |
1918
|
|
|
'desc' => __( 'The Client ID/WP.com Blog ID of this site', 'jetpack' ), |
1919
|
|
|
'readonly' => true, |
1920
|
|
|
'value' => $jetpack_client_id, |
1921
|
|
|
); |
1922
|
|
|
return $options; |
1923
|
|
|
} |
1924
|
|
|
|
1925
|
|
|
/** |
1926
|
|
|
* Resets the saved authentication state in between testing requests. |
1927
|
|
|
*/ |
1928
|
|
|
public function reset_saved_auth_state() { |
1929
|
|
|
$this->xmlrpc_verification = null; |
1930
|
|
|
} |
1931
|
|
|
|
1932
|
|
|
/** |
1933
|
|
|
* Sign a user role with the master access token. |
1934
|
|
|
* If not specified, will default to the current user. |
1935
|
|
|
* |
1936
|
|
|
* @access public |
1937
|
|
|
* |
1938
|
|
|
* @param string $role User role. |
1939
|
|
|
* @param int $user_id ID of the user. |
|
|
|
|
1940
|
|
|
* @return string Signed user role. |
1941
|
|
|
*/ |
1942
|
|
|
public function sign_role( $role, $user_id = null ) { |
1943
|
|
|
return $this->get_tokens()->sign_role( $role, $user_id ); |
1944
|
|
|
} |
1945
|
|
|
|
1946
|
|
|
/** |
1947
|
|
|
* Set the plugin instance. |
1948
|
|
|
* |
1949
|
|
|
* @param Plugin $plugin_instance The plugin instance. |
1950
|
|
|
* |
1951
|
|
|
* @return $this |
1952
|
|
|
*/ |
1953
|
|
|
public function set_plugin_instance( Plugin $plugin_instance ) { |
1954
|
|
|
$this->plugin = $plugin_instance; |
1955
|
|
|
|
1956
|
|
|
return $this; |
1957
|
|
|
} |
1958
|
|
|
|
1959
|
|
|
/** |
1960
|
|
|
* Retrieve the plugin management object. |
1961
|
|
|
* |
1962
|
|
|
* @return Plugin |
1963
|
|
|
*/ |
1964
|
|
|
public function get_plugin() { |
1965
|
|
|
return $this->plugin; |
1966
|
|
|
} |
1967
|
|
|
|
1968
|
|
|
/** |
1969
|
|
|
* Get all connected plugins information, excluding those disconnected by user. |
1970
|
|
|
* WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
1971
|
|
|
* Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
1972
|
|
|
* so please make sure not to run the method too early in the code. |
1973
|
|
|
* |
1974
|
|
|
* @return array|WP_Error |
1975
|
|
|
*/ |
1976
|
|
|
public function get_connected_plugins() { |
1977
|
|
|
$maybe_plugins = Plugin_Storage::get_all( true ); |
1978
|
|
|
|
1979
|
|
|
if ( $maybe_plugins instanceof WP_Error ) { |
1980
|
|
|
return $maybe_plugins; |
1981
|
|
|
} |
1982
|
|
|
|
1983
|
|
|
return $maybe_plugins; |
1984
|
|
|
} |
1985
|
|
|
|
1986
|
|
|
/** |
1987
|
|
|
* Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
1988
|
|
|
* Note: this method does not remove any access tokens. |
1989
|
|
|
* |
1990
|
|
|
* @return bool |
1991
|
|
|
*/ |
1992
|
|
|
public function disable_plugin() { |
1993
|
|
|
if ( ! $this->plugin ) { |
1994
|
|
|
return false; |
1995
|
|
|
} |
1996
|
|
|
|
1997
|
|
|
return $this->plugin->disable(); |
1998
|
|
|
} |
1999
|
|
|
|
2000
|
|
|
/** |
2001
|
|
|
* Force plugin reconnect after user-initiated disconnect. |
2002
|
|
|
* After its called, the plugin will be allowed to use the connection again. |
2003
|
|
|
* Note: this method does not initialize access tokens. |
2004
|
|
|
* |
2005
|
|
|
* @return bool |
2006
|
|
|
*/ |
2007
|
|
|
public function enable_plugin() { |
2008
|
|
|
if ( ! $this->plugin ) { |
2009
|
|
|
return false; |
2010
|
|
|
} |
2011
|
|
|
|
2012
|
|
|
return $this->plugin->enable(); |
2013
|
|
|
} |
2014
|
|
|
|
2015
|
|
|
/** |
2016
|
|
|
* Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
2017
|
|
|
* If no plugin slug was passed into the constructor, always returns true. |
2018
|
|
|
* |
2019
|
|
|
* @return bool |
2020
|
|
|
*/ |
2021
|
|
|
public function is_plugin_enabled() { |
2022
|
|
|
if ( ! $this->plugin ) { |
2023
|
|
|
return true; |
2024
|
|
|
} |
2025
|
|
|
|
2026
|
|
|
return $this->plugin->is_enabled(); |
2027
|
|
|
} |
2028
|
|
|
|
2029
|
|
|
/** |
2030
|
|
|
* Perform the API request to refresh the blog token. |
2031
|
|
|
* Note that we are making this request on behalf of the Jetpack master user, |
2032
|
|
|
* given they were (most probably) the ones that registered the site at the first place. |
2033
|
|
|
* |
2034
|
|
|
* @return WP_Error|bool The result of updating the blog_token option. |
2035
|
|
|
*/ |
2036
|
|
|
/** |
2037
|
|
|
* Perform the API request to refresh the blog token. |
2038
|
|
|
* Note that we are making this request on behalf of the Jetpack master user, |
2039
|
|
|
* given they were (most probably) the ones that registered the site at the first place. |
2040
|
|
|
* |
2041
|
|
|
* @return WP_Error|bool The result of updating the blog_token option. |
2042
|
|
|
*/ |
2043
|
|
|
public function refresh_blog_token() { |
2044
|
|
|
( new Tracking() )->record_user_event( 'restore_connection_refresh_blog_token' ); |
2045
|
|
|
|
2046
|
|
|
$blog_id = \Jetpack_Options::get_option( 'id' ); |
2047
|
|
|
if ( ! $blog_id ) { |
2048
|
|
|
return new WP_Error( 'site_not_registered', 'Site not registered.' ); |
|
|
|
|
2049
|
|
|
} |
2050
|
|
|
|
2051
|
|
|
$url = sprintf( |
2052
|
|
|
'%s/%s/v%s/%s', |
2053
|
|
|
Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ), |
2054
|
|
|
'wpcom', |
2055
|
|
|
'2', |
2056
|
|
|
'sites/' . $blog_id . '/jetpack-refresh-blog-token' |
2057
|
|
|
); |
2058
|
|
|
$method = 'POST'; |
2059
|
|
|
$user_id = get_current_user_id(); |
2060
|
|
|
|
2061
|
|
|
$response = Client::remote_request( compact( 'url', 'method', 'user_id' ) ); |
2062
|
|
|
|
2063
|
|
|
if ( is_wp_error( $response ) ) { |
2064
|
|
|
return new WP_Error( 'refresh_blog_token_http_request_failed', $response->get_error_message() ); |
|
|
|
|
2065
|
|
|
} |
2066
|
|
|
|
2067
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
2068
|
|
|
$entity = wp_remote_retrieve_body( $response ); |
2069
|
|
|
|
2070
|
|
|
if ( $entity ) { |
2071
|
|
|
$json = json_decode( $entity ); |
2072
|
|
|
} else { |
2073
|
|
|
$json = false; |
2074
|
|
|
} |
2075
|
|
|
|
2076
|
|
View Code Duplication |
if ( 200 !== $code ) { |
2077
|
|
|
if ( empty( $json->code ) ) { |
2078
|
|
|
return new WP_Error( 'unknown', '', $code ); |
|
|
|
|
2079
|
|
|
} |
2080
|
|
|
|
2081
|
|
|
/* translators: Error description string. */ |
2082
|
|
|
$error_description = isset( $json->message ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->message ) : ''; |
2083
|
|
|
|
2084
|
|
|
return new WP_Error( (string) $json->code, $error_description, $code ); |
|
|
|
|
2085
|
|
|
} |
2086
|
|
|
|
2087
|
|
|
if ( empty( $json->jetpack_secret ) || ! is_scalar( $json->jetpack_secret ) ) { |
2088
|
|
|
return new WP_Error( 'jetpack_secret', '', $code ); |
|
|
|
|
2089
|
|
|
} |
2090
|
|
|
|
2091
|
|
|
return $this->get_tokens()->update_blog_token( (string) $json->jetpack_secret ); |
2092
|
|
|
} |
2093
|
|
|
|
2094
|
|
|
/** |
2095
|
|
|
* Disconnect the user from WP.com, and initiate the reconnect process. |
2096
|
|
|
* |
2097
|
|
|
* @return bool |
2098
|
|
|
*/ |
2099
|
|
|
public function refresh_user_token() { |
2100
|
|
|
( new Tracking() )->record_user_event( 'restore_connection_refresh_user_token' ); |
2101
|
|
|
$this->disconnect_user( null, true ); |
2102
|
|
|
return true; |
2103
|
|
|
} |
2104
|
|
|
|
2105
|
|
|
/** |
2106
|
|
|
* Fetches a signed token. |
2107
|
|
|
* |
2108
|
|
|
* @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->get_signed_token() instead. |
2109
|
|
|
* |
2110
|
|
|
* @param object $token the token. |
2111
|
|
|
* @return WP_Error|string a signed token |
2112
|
|
|
*/ |
2113
|
|
|
public function get_signed_token( $token ) { |
2114
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Tokens->get_signed_token' ); |
2115
|
|
|
return $this->get_tokens()->get_signed_token( $token ); |
2116
|
|
|
} |
2117
|
|
|
|
2118
|
|
|
/** |
2119
|
|
|
* If the site-level connection is active, add the list of plugins using connection to the heartbeat (except Jetpack itself) |
2120
|
|
|
* |
2121
|
|
|
* @param array $stats The Heartbeat stats array. |
2122
|
|
|
* @return array $stats |
2123
|
|
|
*/ |
2124
|
|
|
public function add_stats_to_heartbeat( $stats ) { |
2125
|
|
|
|
2126
|
|
|
if ( ! $this->is_connected() ) { |
2127
|
|
|
return $stats; |
2128
|
|
|
} |
2129
|
|
|
|
2130
|
|
|
$active_plugins_using_connection = Plugin_Storage::get_all(); |
2131
|
|
|
foreach ( array_keys( $active_plugins_using_connection ) as $plugin_slug ) { |
2132
|
|
|
if ( 'jetpack' !== $plugin_slug ) { |
2133
|
|
|
$stats_group = isset( $active_plugins_using_connection['jetpack'] ) ? 'combined-connection' : 'standalone-connection'; |
2134
|
|
|
$stats[ $stats_group ][] = $plugin_slug; |
2135
|
|
|
} |
2136
|
|
|
} |
2137
|
|
|
return $stats; |
2138
|
|
|
} |
2139
|
|
|
} |
2140
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.