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