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