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 |
||
| 23 | class Manager { |
||
| 24 | |||
| 25 | const SECRETS_MISSING = 'secrets_missing'; |
||
| 26 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
| 27 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
| 28 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
| 29 | const JETPACK_MASTER_USER = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The procedure that should be run to generate secrets. |
||
| 33 | * |
||
| 34 | * @var Callable |
||
| 35 | */ |
||
| 36 | protected $secret_callable; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * A copy of the raw POST data for signature verification purposes. |
||
| 40 | * |
||
| 41 | * @var String |
||
| 42 | */ |
||
| 43 | protected $raw_post_data; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Verification data needs to be stored to properly verify everything. |
||
| 47 | * |
||
| 48 | * @var Object |
||
| 49 | */ |
||
| 50 | private $xmlrpc_verification = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Plugin management object. |
||
| 54 | * |
||
| 55 | * @var Plugin |
||
| 56 | */ |
||
| 57 | private $plugin = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Initialize the object. |
||
| 61 | * Make sure to call the "Configure" first. |
||
| 62 | * |
||
| 63 | * @param string $plugin_slug Slug of the plugin using the connection (optional, but encouraged). |
||
|
|
|||
| 64 | * |
||
| 65 | * @see \Automattic\Jetpack\Config |
||
| 66 | */ |
||
| 67 | public function __construct( $plugin_slug = null ) { |
||
| 68 | if ( $plugin_slug && is_string( $plugin_slug ) ) { |
||
| 69 | $this->set_plugin_instance( new Plugin( $plugin_slug ) ); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Initializes required listeners. This is done separately from the constructors |
||
| 75 | * because some objects sometimes need to instantiate separate objects of this class. |
||
| 76 | * |
||
| 77 | * @todo Implement a proper nonce verification. |
||
| 78 | */ |
||
| 79 | public static function configure() { |
||
| 80 | $manager = new self(); |
||
| 81 | |||
| 82 | add_filter( |
||
| 83 | 'jetpack_constant_default_value', |
||
| 84 | __NAMESPACE__ . '\Utils::jetpack_api_constant_filter', |
||
| 85 | 10, |
||
| 86 | 2 |
||
| 87 | ); |
||
| 88 | |||
| 89 | $manager->setup_xmlrpc_handlers( |
||
| 90 | $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 91 | $manager->is_active(), |
||
| 92 | $manager->verify_xml_rpc_signature() |
||
| 93 | ); |
||
| 94 | |||
| 95 | $manager->error_handler = Error_Handler::get_instance(); |
||
| 96 | |||
| 97 | if ( $manager->is_active() ) { |
||
| 98 | add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) ); |
||
| 99 | } |
||
| 100 | |||
| 101 | add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
||
| 102 | |||
| 103 | add_action( 'jetpack_clean_nonces', array( $manager, 'clean_nonces' ) ); |
||
| 104 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
| 105 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
| 106 | } |
||
| 107 | |||
| 108 | add_action( 'plugins_loaded', __NAMESPACE__ . '\Plugin_Storage::configure', 100 ); |
||
| 109 | |||
| 110 | add_filter( 'map_meta_cap', array( $manager, 'jetpack_connection_custom_caps' ), 1, 4 ); |
||
| 111 | |||
| 112 | Heartbeat::init(); |
||
| 113 | add_filter( 'jetpack_heartbeat_stats_array', array( $manager, 'add_stats_to_heartbeat' ) ); |
||
| 114 | |||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Sets up the XMLRPC request handlers. |
||
| 119 | * |
||
| 120 | * @param array $request_params incoming request parameters. |
||
| 121 | * @param Boolean $is_active whether the connection is currently active. |
||
| 122 | * @param Boolean $is_signed whether the signature check has been successful. |
||
| 123 | * @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
||
| 124 | */ |
||
| 125 | public function setup_xmlrpc_handlers( |
||
| 126 | $request_params, |
||
| 127 | $is_active, |
||
| 128 | $is_signed, |
||
| 129 | \Jetpack_XMLRPC_Server $xmlrpc_server = null |
||
| 130 | ) { |
||
| 131 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ), 1000, 2 ); |
||
| 132 | |||
| 133 | if ( |
||
| 134 | ! isset( $request_params['for'] ) |
||
| 135 | || 'jetpack' !== $request_params['for'] |
||
| 136 | ) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms. |
||
| 141 | if ( |
||
| 142 | isset( $request_params['jetpack'] ) |
||
| 143 | && 'comms' === $request_params['jetpack'] |
||
| 144 | ) { |
||
| 145 | if ( ! Constants::is_defined( 'XMLRPC_REQUEST' ) ) { |
||
| 146 | // Use the real constant here for WordPress' sake. |
||
| 147 | define( 'XMLRPC_REQUEST', true ); |
||
| 148 | } |
||
| 149 | |||
| 150 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
| 151 | |||
| 152 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
| 153 | } |
||
| 154 | |||
| 155 | if ( ! Constants::get_constant( 'XMLRPC_REQUEST' ) ) { |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | // Display errors can cause the XML to be not well formed. |
||
| 159 | @ini_set( 'display_errors', false ); // phpcs:ignore |
||
| 160 | |||
| 161 | if ( $xmlrpc_server ) { |
||
| 162 | $this->xmlrpc_server = $xmlrpc_server; |
||
| 163 | } else { |
||
| 164 | $this->xmlrpc_server = new \Jetpack_XMLRPC_Server(); |
||
| 165 | } |
||
| 166 | |||
| 167 | $this->require_jetpack_authentication(); |
||
| 168 | |||
| 169 | if ( $is_active ) { |
||
| 170 | // Hack to preserve $HTTP_RAW_POST_DATA. |
||
| 171 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 172 | |||
| 173 | if ( $is_signed ) { |
||
| 174 | // The actual API methods. |
||
| 175 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
| 176 | } else { |
||
| 177 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
| 178 | // active Jetpack connection, so that additional users can link their account. |
||
| 179 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | // The bootstrap API methods. |
||
| 183 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
| 184 | |||
| 185 | if ( $is_signed ) { |
||
| 186 | // The jetpack Provision method is available for blog-token-signed requests. |
||
| 187 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
||
| 188 | } else { |
||
| 189 | new XMLRPC_Connector( $this ); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
| 194 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Initializes the REST API connector on the init hook. |
||
| 200 | */ |
||
| 201 | public function initialize_rest_api_registration_connector() { |
||
| 202 | new REST_Connector( $this ); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
| 207 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
| 208 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
| 209 | * which is accessible via a different URI. Most of the below is copied directly |
||
| 210 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
| 211 | * |
||
| 212 | * @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
||
| 213 | */ |
||
| 214 | public function alternate_xmlrpc() { |
||
| 215 | // Some browser-embedded clients send cookies. We don't want them. |
||
| 216 | $_COOKIE = array(); |
||
| 217 | |||
| 218 | include_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
| 219 | include_once ABSPATH . WPINC . '/class-IXR.php'; |
||
| 220 | include_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Filters the class used for handling XML-RPC requests. |
||
| 224 | * |
||
| 225 | * @since 3.1.0 |
||
| 226 | * |
||
| 227 | * @param string $class The name of the XML-RPC server class. |
||
| 228 | */ |
||
| 229 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
| 230 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class(); |
||
| 231 | |||
| 232 | // Fire off the request. |
||
| 233 | nocache_headers(); |
||
| 234 | $wp_xmlrpc_server->serve_request(); |
||
| 235 | |||
| 236 | exit; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
| 241 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
| 242 | * ensure that Core and other plugins' methods are not exposed. |
||
| 243 | * |
||
| 244 | * @param array $methods a list of registered WordPress XMLRPC methods. |
||
| 245 | * @return array filtered $methods |
||
| 246 | */ |
||
| 247 | public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Removes all other authentication methods not to allow other |
||
| 261 | * methods to validate unauthenticated requests. |
||
| 262 | */ |
||
| 263 | public function require_jetpack_authentication() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
| 277 | * |
||
| 278 | * @param WP_User|Mixed $user user object if authenticated. |
||
| 279 | * @param String $username username. |
||
| 280 | * @param String $password password string. |
||
| 281 | * @return WP_User|Mixed authenticated user or error. |
||
| 282 | */ |
||
| 283 | public function authenticate_jetpack( $user, $username, $password ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Verifies the signature of the current request. |
||
| 309 | * |
||
| 310 | * @return false|array |
||
| 311 | */ |
||
| 312 | public function verify_xml_rpc_signature() { |
||
| 313 | if ( is_null( $this->xmlrpc_verification ) ) { |
||
| 314 | $this->xmlrpc_verification = $this->internal_verify_xml_rpc_signature(); |
||
| 315 | |||
| 316 | if ( is_wp_error( $this->xmlrpc_verification ) ) { |
||
| 317 | /** |
||
| 318 | * Action for logging XMLRPC signature verification errors. This data is sensitive. |
||
| 319 | * |
||
| 320 | * @since 7.5.0 |
||
| 321 | * |
||
| 322 | * @param WP_Error $signature_verification_error The verification error |
||
| 323 | */ |
||
| 324 | do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
||
| 325 | |||
| 326 | Error_Handler::get_instance()->report_error( $this->xmlrpc_verification ); |
||
| 327 | |||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Verifies the signature of the current request. |
||
| 336 | * |
||
| 337 | * This function has side effects and should not be used. Instead, |
||
| 338 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
| 339 | * |
||
| 340 | * @internal |
||
| 341 | * @todo Refactor to use proper nonce verification. |
||
| 342 | */ |
||
| 343 | private function internal_verify_xml_rpc_signature() { |
||
| 344 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
| 345 | // It's not for us. |
||
| 346 | if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { |
||
| 347 | return false; |
||
| 348 | } |
||
| 349 | |||
| 350 | $signature_details = array( |
||
| 351 | 'token' => isset( $_GET['token'] ) ? wp_unslash( $_GET['token'] ) : '', |
||
| 352 | 'timestamp' => isset( $_GET['timestamp'] ) ? wp_unslash( $_GET['timestamp'] ) : '', |
||
| 353 | 'nonce' => isset( $_GET['nonce'] ) ? wp_unslash( $_GET['nonce'] ) : '', |
||
| 354 | 'body_hash' => isset( $_GET['body-hash'] ) ? wp_unslash( $_GET['body-hash'] ) : '', |
||
| 355 | 'method' => wp_unslash( $_SERVER['REQUEST_METHOD'] ), |
||
| 356 | 'url' => wp_unslash( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ), // Temp - will get real signature URL later. |
||
| 357 | 'signature' => isset( $_GET['signature'] ) ? wp_unslash( $_GET['signature'] ) : '', |
||
| 358 | ); |
||
| 359 | |||
| 360 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
||
| 361 | @list( $token_key, $version, $user_id ) = explode( ':', wp_unslash( $_GET['token'] ) ); |
||
| 362 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
||
| 363 | |||
| 364 | $jetpack_api_version = Constants::get_constant( 'JETPACK__API_VERSION' ); |
||
| 365 | |||
| 366 | if ( |
||
| 367 | empty( $token_key ) |
||
| 368 | || |
||
| 369 | empty( $version ) || strval( $jetpack_api_version ) !== $version ) { |
||
| 370 | return new \WP_Error( 'malformed_token', 'Malformed token in request', compact( 'signature_details' ) ); |
||
| 371 | } |
||
| 372 | |||
| 373 | if ( '0' === $user_id ) { |
||
| 374 | $token_type = 'blog'; |
||
| 375 | $user_id = 0; |
||
| 376 | } else { |
||
| 377 | $token_type = 'user'; |
||
| 378 | if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { |
||
| 379 | return new \WP_Error( |
||
| 380 | 'malformed_user_id', |
||
| 381 | 'Malformed user_id in request', |
||
| 382 | compact( 'signature_details' ) |
||
| 383 | ); |
||
| 384 | } |
||
| 385 | $user_id = (int) $user_id; |
||
| 386 | |||
| 387 | $user = new \WP_User( $user_id ); |
||
| 388 | if ( ! $user || ! $user->exists() ) { |
||
| 389 | return new \WP_Error( |
||
| 390 | 'unknown_user', |
||
| 391 | sprintf( 'User %d does not exist', $user_id ), |
||
| 392 | compact( 'signature_details' ) |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | $token = $this->get_access_token( $user_id, $token_key, false ); |
||
| 398 | if ( is_wp_error( $token ) ) { |
||
| 399 | $token->add_data( compact( 'signature_details' ) ); |
||
| 400 | return $token; |
||
| 401 | } elseif ( ! $token ) { |
||
| 402 | return new \WP_Error( |
||
| 403 | 'unknown_token', |
||
| 404 | sprintf( 'Token %s:%s:%d does not exist', $token_key, $version, $user_id ), |
||
| 405 | compact( 'signature_details' ) |
||
| 406 | ); |
||
| 407 | } |
||
| 408 | |||
| 409 | $jetpack_signature = new \Jetpack_Signature( $token->secret, (int) \Jetpack_Options::get_option( 'time_diff' ) ); |
||
| 410 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
||
| 411 | if ( isset( $_POST['_jetpack_is_multipart'] ) ) { |
||
| 412 | $post_data = $_POST; |
||
| 413 | $file_hashes = array(); |
||
| 414 | foreach ( $post_data as $post_data_key => $post_data_value ) { |
||
| 415 | if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { |
||
| 416 | continue; |
||
| 417 | } |
||
| 418 | $post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); |
||
| 419 | $file_hashes[ $post_data_key ] = $post_data_value; |
||
| 420 | } |
||
| 421 | |||
| 422 | foreach ( $file_hashes as $post_data_key => $post_data_value ) { |
||
| 423 | unset( $post_data[ "_jetpack_file_hmac_{$post_data_key}" ] ); |
||
| 424 | $post_data[ $post_data_key ] = $post_data_value; |
||
| 425 | } |
||
| 426 | |||
| 427 | ksort( $post_data ); |
||
| 428 | |||
| 429 | $body = http_build_query( stripslashes_deep( $post_data ) ); |
||
| 430 | } elseif ( is_null( $this->raw_post_data ) ) { |
||
| 431 | $body = file_get_contents( 'php://input' ); |
||
| 432 | } else { |
||
| 433 | $body = null; |
||
| 434 | } |
||
| 435 | // phpcs:enable |
||
| 436 | |||
| 437 | $signature = $jetpack_signature->sign_current_request( |
||
| 438 | array( 'body' => is_null( $body ) ? $this->raw_post_data : $body ) |
||
| 439 | ); |
||
| 440 | |||
| 441 | $signature_details['url'] = $jetpack_signature->current_request_url; |
||
| 442 | |||
| 443 | if ( ! $signature ) { |
||
| 444 | return new \WP_Error( |
||
| 445 | 'could_not_sign', |
||
| 446 | 'Unknown signature error', |
||
| 447 | compact( 'signature_details' ) |
||
| 448 | ); |
||
| 449 | } elseif ( is_wp_error( $signature ) ) { |
||
| 450 | return $signature; |
||
| 451 | } |
||
| 452 | |||
| 453 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
| 454 | $timestamp = (int) $_GET['timestamp']; |
||
| 455 | $nonce = stripslashes( (string) $_GET['nonce'] ); |
||
| 456 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
||
| 457 | |||
| 458 | // Use up the nonce regardless of whether the signature matches. |
||
| 459 | if ( ! $this->add_nonce( $timestamp, $nonce ) ) { |
||
| 460 | return new \WP_Error( |
||
| 461 | 'invalid_nonce', |
||
| 462 | 'Could not add nonce', |
||
| 463 | compact( 'signature_details' ) |
||
| 464 | ); |
||
| 465 | } |
||
| 466 | |||
| 467 | // Be careful about what you do with this debugging data. |
||
| 468 | // If a malicious requester has access to the expected signature, |
||
| 469 | // bad things might be possible. |
||
| 470 | $signature_details['expected'] = $signature; |
||
| 471 | |||
| 472 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 473 | if ( ! hash_equals( $signature, $_GET['signature'] ) ) { |
||
| 474 | return new \WP_Error( |
||
| 475 | 'signature_mismatch', |
||
| 476 | 'Signature mismatch', |
||
| 477 | compact( 'signature_details' ) |
||
| 478 | ); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Action for additional token checking. |
||
| 483 | * |
||
| 484 | * @since 7.7.0 |
||
| 485 | * |
||
| 486 | * @param array $post_data request data. |
||
| 487 | * @param array $token_data token data. |
||
| 488 | */ |
||
| 489 | return apply_filters( |
||
| 490 | 'jetpack_signature_check_token', |
||
| 491 | array( |
||
| 492 | 'type' => $token_type, |
||
| 493 | 'token_key' => $token_key, |
||
| 494 | 'user_id' => $token->external_user_id, |
||
| 495 | ), |
||
| 496 | $token, |
||
| 497 | $this->raw_post_data |
||
| 498 | ); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns true if the current site is connected to WordPress.com. |
||
| 503 | * |
||
| 504 | * @return Boolean is the site connected? |
||
| 505 | */ |
||
| 506 | public function is_active() { |
||
| 507 | return (bool) $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
||
| 512 | * |
||
| 513 | * @access public |
||
| 514 | * |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | public function is_registered() { |
||
| 518 | $has_blog_id = (bool) \Jetpack_Options::get_option( 'id' ); |
||
| 519 | $has_blog_token = (bool) $this->get_access_token( false ); |
||
| 520 | return $has_blog_id && $has_blog_token; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Checks to see if the connection owner of the site is missing. |
||
| 525 | * |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | public function is_missing_connection_owner() { |
||
| 529 | $connection_owner = $this->get_connection_owner_id(); |
||
| 530 | if ( ! get_user_by( 'id', $connection_owner ) ) { |
||
| 531 | return true; |
||
| 532 | } |
||
| 533 | |||
| 534 | return false; |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns true if the user with the specified identifier is connected to |
||
| 539 | * WordPress.com. |
||
| 540 | * |
||
| 541 | * @param Integer|Boolean $user_id the user identifier. |
||
| 542 | * @return Boolean is the user connected? |
||
| 543 | */ |
||
| 544 | public function is_user_connected( $user_id = false ) { |
||
| 545 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
| 546 | if ( ! $user_id ) { |
||
| 547 | return false; |
||
| 548 | } |
||
| 549 | |||
| 550 | return (bool) $this->get_access_token( $user_id ); |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Returns the local user ID of the connection owner. |
||
| 555 | * |
||
| 556 | * @return string|int Returns the ID of the connection owner or False if no connection owner found. |
||
| 557 | */ |
||
| 558 | View Code Duplication | public function get_connection_owner_id() { |
|
| 559 | $user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 560 | $connection_owner = false; |
||
| 561 | if ( $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) ) { |
||
| 562 | $connection_owner = $user_token->external_user_id; |
||
| 563 | } |
||
| 564 | |||
| 565 | return $connection_owner; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Returns an array of user_id's that have user tokens for communicating with wpcom. |
||
| 570 | * Able to select by specific capability. |
||
| 571 | * |
||
| 572 | * @param string $capability The capability of the user. |
||
| 573 | * @return array Array of WP_User objects if found. |
||
| 574 | */ |
||
| 575 | public function get_connected_users( $capability = 'any' ) { |
||
| 576 | $connected_users = array(); |
||
| 577 | $connected_user_ids = array_keys( \Jetpack_Options::get_option( 'user_tokens' ) ); |
||
| 578 | |||
| 579 | if ( ! empty( $connected_user_ids ) ) { |
||
| 580 | foreach ( $connected_user_ids as $id ) { |
||
| 581 | // Check for capability. |
||
| 582 | if ( 'any' !== $capability && ! user_can( $id, $capability ) ) { |
||
| 583 | continue; |
||
| 584 | } |
||
| 585 | |||
| 586 | $connected_users[] = get_userdata( $id ); |
||
| 587 | } |
||
| 588 | } |
||
| 589 | |||
| 590 | return $connected_users; |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Get the wpcom user data of the current|specified connected user. |
||
| 595 | * |
||
| 596 | * @todo Refactor to properly load the XMLRPC client independently. |
||
| 597 | * |
||
| 598 | * @param Integer $user_id the user identifier. |
||
| 599 | * @return Object the user object. |
||
| 600 | */ |
||
| 601 | View Code Duplication | public function get_connected_user_data( $user_id = null ) { |
|
| 602 | if ( ! $user_id ) { |
||
| 603 | $user_id = get_current_user_id(); |
||
| 604 | } |
||
| 605 | |||
| 606 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 607 | $cached_user_data = get_transient( $transient_key ); |
||
| 608 | |||
| 609 | if ( $cached_user_data ) { |
||
| 610 | return $cached_user_data; |
||
| 611 | } |
||
| 612 | |||
| 613 | $xml = new \Jetpack_IXR_Client( |
||
| 614 | array( |
||
| 615 | 'user_id' => $user_id, |
||
| 616 | ) |
||
| 617 | ); |
||
| 618 | $xml->query( 'wpcom.getUser' ); |
||
| 619 | if ( ! $xml->isError() ) { |
||
| 620 | $user_data = $xml->getResponse(); |
||
| 621 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
| 622 | return $user_data; |
||
| 623 | } |
||
| 624 | |||
| 625 | return false; |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Returns a user object of the connection owner. |
||
| 630 | * |
||
| 631 | * @return object|false False if no connection owner found. |
||
| 632 | */ |
||
| 633 | View Code Duplication | public function get_connection_owner() { |
|
| 634 | $user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 635 | |||
| 636 | $connection_owner = false; |
||
| 637 | if ( $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) ) { |
||
| 638 | $connection_owner = get_userdata( $user_token->external_user_id ); |
||
| 639 | } |
||
| 640 | |||
| 641 | return $connection_owner; |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Returns true if the provided user is the Jetpack connection owner. |
||
| 646 | * If user ID is not specified, the current user will be used. |
||
| 647 | * |
||
| 648 | * @param Integer|Boolean $user_id the user identifier. False for current user. |
||
| 649 | * @return Boolean True the user the connection owner, false otherwise. |
||
| 650 | */ |
||
| 651 | View Code Duplication | public function is_connection_owner( $user_id = false ) { |
|
| 652 | if ( ! $user_id ) { |
||
| 653 | $user_id = get_current_user_id(); |
||
| 654 | } |
||
| 655 | |||
| 656 | $user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 657 | |||
| 658 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id; |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Connects the user with a specified ID to a WordPress.com user using the |
||
| 663 | * remote login flow. |
||
| 664 | * |
||
| 665 | * @access public |
||
| 666 | * |
||
| 667 | * @param Integer $user_id (optional) the user identifier, defaults to current user. |
||
| 668 | * @param String $redirect_url the URL to redirect the user to for processing, defaults to |
||
| 669 | * admin_url(). |
||
| 670 | * @return WP_Error only in case of a failed user lookup. |
||
| 671 | */ |
||
| 672 | public function connect_user( $user_id = null, $redirect_url = null ) { |
||
| 673 | $user = null; |
||
| 674 | if ( null === $user_id ) { |
||
| 675 | $user = wp_get_current_user(); |
||
| 676 | } else { |
||
| 677 | $user = get_user_by( 'ID', $user_id ); |
||
| 678 | } |
||
| 679 | |||
| 680 | if ( empty( $user ) ) { |
||
| 681 | return new \WP_Error( 'user_not_found', 'Attempting to connect a non-existent user.' ); |
||
| 682 | } |
||
| 683 | |||
| 684 | if ( null === $redirect_url ) { |
||
| 685 | $redirect_url = admin_url(); |
||
| 686 | } |
||
| 687 | |||
| 688 | // Using wp_redirect intentionally because we're redirecting outside. |
||
| 689 | wp_redirect( $this->get_authorization_url( $user ) ); // phpcs:ignore WordPress.Security.SafeRedirect |
||
| 690 | exit(); |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Unlinks the current user from the linked WordPress.com user. |
||
| 695 | * |
||
| 696 | * @access public |
||
| 697 | * @static |
||
| 698 | * |
||
| 699 | * @todo Refactor to properly load the XMLRPC client independently. |
||
| 700 | * |
||
| 701 | * @param Integer $user_id the user identifier. |
||
| 702 | * @param bool $can_overwrite_primary_user Allow for the primary user to be disconnected. |
||
| 703 | * @return Boolean Whether the disconnection of the user was successful. |
||
| 704 | */ |
||
| 705 | public static function disconnect_user( $user_id = null, $can_overwrite_primary_user = false ) { |
||
| 706 | $tokens = Jetpack_Options::get_option( 'user_tokens' ); |
||
| 707 | if ( ! $tokens ) { |
||
| 708 | return false; |
||
| 709 | } |
||
| 710 | |||
| 711 | $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
||
| 712 | |||
| 713 | if ( Jetpack_Options::get_option( 'master_user' ) === $user_id && ! $can_overwrite_primary_user ) { |
||
| 714 | return false; |
||
| 715 | } |
||
| 716 | |||
| 717 | if ( ! isset( $tokens[ $user_id ] ) ) { |
||
| 718 | return false; |
||
| 719 | } |
||
| 720 | |||
| 721 | $xml = new \Jetpack_IXR_Client( compact( 'user_id' ) ); |
||
| 722 | $xml->query( 'jetpack.unlink_user', $user_id ); |
||
| 723 | |||
| 724 | unset( $tokens[ $user_id ] ); |
||
| 725 | |||
| 726 | Jetpack_Options::update_option( 'user_tokens', $tokens ); |
||
| 727 | |||
| 728 | // Delete cached connected user data. |
||
| 729 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 730 | delete_transient( $transient_key ); |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Fires after the current user has been unlinked from WordPress.com. |
||
| 734 | * |
||
| 735 | * @since 4.1.0 |
||
| 736 | * |
||
| 737 | * @param int $user_id The current user's ID. |
||
| 738 | */ |
||
| 739 | do_action( 'jetpack_unlinked_user', $user_id ); |
||
| 740 | |||
| 741 | return true; |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Returns the requested Jetpack API URL. |
||
| 746 | * |
||
| 747 | * @param String $relative_url the relative API path. |
||
| 748 | * @return String API URL. |
||
| 749 | */ |
||
| 750 | public function api_url( $relative_url ) { |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Returns the Jetpack XMLRPC WordPress.com API endpoint URL. |
||
| 790 | * |
||
| 791 | * @return String XMLRPC API URL. |
||
| 792 | */ |
||
| 793 | public function xmlrpc_api_url() { |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
| 804 | * remain public because the call to action comes from the current site, not from |
||
| 805 | * WordPress.com. |
||
| 806 | * |
||
| 807 | * @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
||
| 808 | * @return true|WP_Error The error object. |
||
| 809 | */ |
||
| 810 | public function register( $api_endpoint = 'register' ) { |
||
| 811 | add_action( 'pre_update_jetpack_option_register', array( '\\Jetpack_Options', 'delete_option' ) ); |
||
| 812 | $secrets = $this->generate_secrets( 'register', get_current_user_id(), 600 ); |
||
| 813 | |||
| 814 | if ( |
||
| 815 | empty( $secrets['secret_1'] ) || |
||
| 816 | empty( $secrets['secret_2'] ) || |
||
| 817 | empty( $secrets['exp'] ) |
||
| 818 | ) { |
||
| 819 | return new \WP_Error( 'missing_secrets' ); |
||
| 820 | } |
||
| 821 | |||
| 822 | // Better to try (and fail) to set a higher timeout than this system |
||
| 823 | // supports than to have register fail for more users than it should. |
||
| 824 | $timeout = $this->set_min_time_limit( 60 ) / 2; |
||
| 825 | |||
| 826 | $gmt_offset = get_option( 'gmt_offset' ); |
||
| 827 | if ( ! $gmt_offset ) { |
||
| 828 | $gmt_offset = 0; |
||
| 829 | } |
||
| 830 | |||
| 831 | $stats_options = get_option( 'stats_options' ); |
||
| 832 | $stats_id = isset( $stats_options['blog_id'] ) |
||
| 833 | ? $stats_options['blog_id'] |
||
| 834 | : null; |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Filters the request body for additional property addition. |
||
| 838 | * |
||
| 839 | * @since 7.7.0 |
||
| 840 | * |
||
| 841 | * @param array $post_data request data. |
||
| 842 | * @param Array $token_data token data. |
||
| 843 | */ |
||
| 844 | $body = apply_filters( |
||
| 845 | 'jetpack_register_request_body', |
||
| 846 | array( |
||
| 847 | 'siteurl' => site_url(), |
||
| 848 | 'home' => home_url(), |
||
| 849 | 'gmt_offset' => $gmt_offset, |
||
| 850 | 'timezone_string' => (string) get_option( 'timezone_string' ), |
||
| 851 | 'site_name' => (string) get_option( 'blogname' ), |
||
| 852 | 'secret_1' => $secrets['secret_1'], |
||
| 853 | 'secret_2' => $secrets['secret_2'], |
||
| 854 | 'site_lang' => get_locale(), |
||
| 855 | 'timeout' => $timeout, |
||
| 856 | 'stats_id' => $stats_id, |
||
| 857 | 'state' => get_current_user_id(), |
||
| 858 | 'site_created' => $this->get_assumed_site_creation_date(), |
||
| 859 | 'jetpack_version' => Constants::get_constant( 'JETPACK__VERSION' ), |
||
| 860 | 'ABSPATH' => Constants::get_constant( 'ABSPATH' ), |
||
| 861 | 'current_user_email' => wp_get_current_user()->user_email, |
||
| 862 | ) |
||
| 863 | ); |
||
| 864 | |||
| 865 | $args = array( |
||
| 866 | 'method' => 'POST', |
||
| 867 | 'body' => $body, |
||
| 868 | 'headers' => array( |
||
| 869 | 'Accept' => 'application/json', |
||
| 870 | ), |
||
| 871 | 'timeout' => $timeout, |
||
| 872 | ); |
||
| 873 | |||
| 874 | $args['body'] = $this->apply_activation_source_to_args( $args['body'] ); |
||
| 875 | |||
| 876 | // TODO: fix URLs for bad hosts. |
||
| 877 | $response = Client::_wp_remote_request( |
||
| 878 | $this->api_url( $api_endpoint ), |
||
| 879 | $args, |
||
| 880 | true |
||
| 881 | ); |
||
| 882 | |||
| 883 | // Make sure the response is valid and does not contain any Jetpack errors. |
||
| 884 | $registration_details = $this->validate_remote_register_response( $response ); |
||
| 885 | |||
| 886 | if ( is_wp_error( $registration_details ) ) { |
||
| 887 | return $registration_details; |
||
| 888 | } elseif ( ! $registration_details ) { |
||
| 889 | return new \WP_Error( |
||
| 890 | 'unknown_error', |
||
| 891 | 'Unknown error registering your Jetpack site.', |
||
| 892 | wp_remote_retrieve_response_code( $response ) |
||
| 893 | ); |
||
| 894 | } |
||
| 895 | |||
| 896 | if ( empty( $registration_details->jetpack_secret ) || ! is_string( $registration_details->jetpack_secret ) ) { |
||
| 897 | return new \WP_Error( |
||
| 898 | 'jetpack_secret', |
||
| 899 | 'Unable to validate registration of your Jetpack site.', |
||
| 900 | wp_remote_retrieve_response_code( $response ) |
||
| 901 | ); |
||
| 902 | } |
||
| 903 | |||
| 904 | if ( isset( $registration_details->jetpack_public ) ) { |
||
| 905 | $jetpack_public = (int) $registration_details->jetpack_public; |
||
| 906 | } else { |
||
| 907 | $jetpack_public = false; |
||
| 908 | } |
||
| 909 | |||
| 910 | \Jetpack_Options::update_options( |
||
| 911 | array( |
||
| 912 | 'id' => (int) $registration_details->jetpack_id, |
||
| 913 | 'blog_token' => (string) $registration_details->jetpack_secret, |
||
| 914 | 'public' => $jetpack_public, |
||
| 915 | ) |
||
| 916 | ); |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Fires when a site is registered on WordPress.com. |
||
| 920 | * |
||
| 921 | * @since 3.7.0 |
||
| 922 | * |
||
| 923 | * @param int $json->jetpack_id Jetpack Blog ID. |
||
| 924 | * @param string $json->jetpack_secret Jetpack Blog Token. |
||
| 925 | * @param int|bool $jetpack_public Is the site public. |
||
| 926 | */ |
||
| 927 | do_action( |
||
| 928 | 'jetpack_site_registered', |
||
| 929 | $registration_details->jetpack_id, |
||
| 930 | $registration_details->jetpack_secret, |
||
| 931 | $jetpack_public |
||
| 932 | ); |
||
| 933 | |||
| 934 | if ( isset( $registration_details->token ) ) { |
||
| 935 | /** |
||
| 936 | * Fires when a user token is sent along with the registration data. |
||
| 937 | * |
||
| 938 | * @since 7.6.0 |
||
| 939 | * |
||
| 940 | * @param object $token the administrator token for the newly registered site. |
||
| 941 | */ |
||
| 942 | do_action( 'jetpack_site_registered_user_token', $registration_details->token ); |
||
| 943 | } |
||
| 944 | |||
| 945 | return true; |
||
| 946 | } |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Takes the response from the Jetpack register new site endpoint and |
||
| 950 | * verifies it worked properly. |
||
| 951 | * |
||
| 952 | * @since 2.6 |
||
| 953 | * |
||
| 954 | * @param Mixed $response the response object, or the error object. |
||
| 955 | * @return string|WP_Error A JSON object on success or WP_Error on failures |
||
| 956 | **/ |
||
| 957 | protected function validate_remote_register_response( $response ) { |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Adds a used nonce to a list of known nonces. |
||
| 1029 | * |
||
| 1030 | * @param int $timestamp the current request timestamp. |
||
| 1031 | * @param string $nonce the nonce value. |
||
| 1032 | * @return bool whether the nonce is unique or not. |
||
| 1033 | */ |
||
| 1034 | public function add_nonce( $timestamp, $nonce ) { |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Cleans nonces that were saved when calling ::add_nonce. |
||
| 1075 | * |
||
| 1076 | * @todo Properly prepare the query before executing it. |
||
| 1077 | * |
||
| 1078 | * @param bool $all whether to clean even non-expired nonces. |
||
| 1079 | */ |
||
| 1080 | public function clean_nonces( $all = false ) { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Sets the Connection custom capabilities. |
||
| 1104 | * |
||
| 1105 | * @param string[] $caps Array of the user's capabilities. |
||
| 1106 | * @param string $cap Capability name. |
||
| 1107 | * @param int $user_id The user ID. |
||
| 1108 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
| 1109 | */ |
||
| 1110 | public function jetpack_connection_custom_caps( $caps, $cap, $user_id, $args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 1144 | * |
||
| 1145 | * Based on local php max_execution_time in php.ini |
||
| 1146 | * |
||
| 1147 | * @since 5.4 |
||
| 1148 | * @return int |
||
| 1149 | **/ |
||
| 1150 | public function get_max_execution_time() { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Sets a minimum request timeout, and returns the current timeout |
||
| 1162 | * |
||
| 1163 | * @since 5.4 |
||
| 1164 | * @param Integer $min_timeout the minimum timeout value. |
||
| 1165 | **/ |
||
| 1166 | View Code Duplication | public function set_min_time_limit( $min_timeout ) { |
|
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Get our assumed site creation date. |
||
| 1177 | * Calculated based on the earlier date of either: |
||
| 1178 | * - Earliest admin user registration date. |
||
| 1179 | * - Earliest date of post of any post type. |
||
| 1180 | * |
||
| 1181 | * @since 7.2.0 |
||
| 1182 | * |
||
| 1183 | * @return string Assumed site creation date and time. |
||
| 1184 | */ |
||
| 1185 | public function get_assumed_site_creation_date() { |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Adds the activation source string as a parameter to passed arguments. |
||
| 1227 | * |
||
| 1228 | * @todo Refactor to use rawurlencode() instead of urlencode(). |
||
| 1229 | * |
||
| 1230 | * @param array $args arguments that need to have the source added. |
||
| 1231 | * @return array $amended arguments. |
||
| 1232 | */ |
||
| 1233 | View Code Duplication | public static function apply_activation_source_to_args( $args ) { |
|
| 1248 | |||
| 1249 | /** |
||
| 1250 | * Returns the callable that would be used to generate secrets. |
||
| 1251 | * |
||
| 1252 | * @return Callable a function that returns a secure string to be used as a secret. |
||
| 1253 | */ |
||
| 1254 | protected function get_secret_callable() { |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Runs the wp_generate_password function with the required parameters. This is the |
||
| 1269 | * default implementation of the secret callable, can be overridden using the |
||
| 1270 | * jetpack_connection_secret_generator filter. |
||
| 1271 | * |
||
| 1272 | * @return String $secret value. |
||
| 1273 | */ |
||
| 1274 | private function secret_callable_method() { |
||
| 1277 | |||
| 1278 | /** |
||
| 1279 | * Generates two secret tokens and the end of life timestamp for them. |
||
| 1280 | * |
||
| 1281 | * @param String $action The action name. |
||
| 1282 | * @param Integer $user_id The user identifier. |
||
| 1283 | * @param Integer $exp Expiration time in seconds. |
||
| 1284 | */ |
||
| 1285 | public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
| 1317 | |||
| 1318 | /** |
||
| 1319 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 1320 | * |
||
| 1321 | * @param String $action The action name. |
||
| 1322 | * @param Integer $user_id The user identifier. |
||
| 1323 | * @return string|array an array of secrets or an error string. |
||
| 1324 | */ |
||
| 1325 | public function get_secrets( $action, $user_id ) { |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Deletes secret tokens in case they, for example, have expired. |
||
| 1346 | * |
||
| 1347 | * @param String $action The action name. |
||
| 1348 | * @param Integer $user_id The user identifier. |
||
| 1349 | */ |
||
| 1350 | public function delete_secrets( $action, $user_id ) { |
||
| 1361 | |||
| 1362 | /** |
||
| 1363 | * Deletes all connection tokens and transients from the local Jetpack site. |
||
| 1364 | * If the plugin object has been provided in the constructor, the function first checks |
||
| 1365 | * whether it's the only active connection. |
||
| 1366 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1367 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1368 | * |
||
| 1369 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1370 | * |
||
| 1371 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1372 | */ |
||
| 1373 | public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
||
| 1410 | |||
| 1411 | /** |
||
| 1412 | * Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
||
| 1413 | * If the plugin object has been provided in the constructor, the function first check |
||
| 1414 | * whether it's the only active connection. |
||
| 1415 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1416 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1417 | * |
||
| 1418 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1419 | * |
||
| 1420 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1421 | */ |
||
| 1422 | public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Disconnect the plugin and remove the tokens. |
||
| 1445 | * This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
||
| 1446 | * This is a proxy method to simplify the Connection package API. |
||
| 1447 | * |
||
| 1448 | * @see Manager::disable_plugin() |
||
| 1449 | * @see Manager::disconnect_site_wpcom() |
||
| 1450 | * @see Manager::delete_all_connection_tokens() |
||
| 1451 | * |
||
| 1452 | * @return bool |
||
| 1453 | */ |
||
| 1454 | public function remove_connection() { |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Completely clearing up the connection, and initiating reconnect. |
||
| 1464 | * |
||
| 1465 | * @return true|WP_Error True if reconnected successfully, a `WP_Error` object otherwise. |
||
| 1466 | */ |
||
| 1467 | public function reconnect() { |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * Validate the tokens, and refresh the invalid ones. |
||
| 1478 | * |
||
| 1479 | * @return string|true|WP_Error True if connection restored or string indicating what's to be done next. A `WP_Error` object otherwise. |
||
| 1480 | */ |
||
| 1481 | public function restore() { |
||
| 1501 | |||
| 1502 | /** |
||
| 1503 | * Determine whether we can restore the connection, or the full reconnect is needed. |
||
| 1504 | * |
||
| 1505 | * @param array $invalid_tokens The array the invalid tokens are stored in, provided by reference. |
||
| 1506 | * |
||
| 1507 | * @return bool `True` if the connection can be restored, `false` otherwise. |
||
| 1508 | */ |
||
| 1509 | public function can_restore( &$invalid_tokens ) { |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Perform the API request to validate the blog and user tokens. |
||
| 1532 | * |
||
| 1533 | * @param int|null $user_id ID of the user we need to validate token for. Current user's ID by default. |
||
| 1534 | * |
||
| 1535 | * @return array|false|WP_Error The API response: `array( 'blog_token_is_healthy' => true|false, 'user_token_is_healthy' => true|false )`. |
||
| 1536 | */ |
||
| 1537 | public function validate_tokens( $user_id = null ) { |
||
| 1568 | |||
| 1569 | /** |
||
| 1570 | * Responds to a WordPress.com call to register the current site. |
||
| 1571 | * Should be changed to protected. |
||
| 1572 | * |
||
| 1573 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
| 1574 | */ |
||
| 1575 | public function handle_registration( array $registration_data ) { |
||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * Verify a Previously Generated Secret. |
||
| 1586 | * |
||
| 1587 | * @param string $action The type of secret to verify. |
||
| 1588 | * @param string $secret_1 The secret string to compare to what is stored. |
||
| 1589 | * @param int $user_id The user ID of the owner of the secret. |
||
| 1590 | * @return \WP_Error|string WP_Error on failure, secret_2 on success. |
||
| 1591 | */ |
||
| 1592 | public function verify_secrets( $action, $secret_1, $user_id ) { |
||
| 1728 | |||
| 1729 | /** |
||
| 1730 | * Responds to a WordPress.com call to authorize the current user. |
||
| 1731 | * Should be changed to protected. |
||
| 1732 | */ |
||
| 1733 | public function handle_authorization() { |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Obtains the auth token. |
||
| 1739 | * |
||
| 1740 | * @param array $data The request data. |
||
| 1741 | * @return object|\WP_Error Returns the auth token on success. |
||
| 1742 | * Returns a \WP_Error on failure. |
||
| 1743 | */ |
||
| 1744 | public function get_token( $data ) { |
||
| 1885 | |||
| 1886 | /** |
||
| 1887 | * Increases the request timeout value to 30 seconds. |
||
| 1888 | * |
||
| 1889 | * @return int Returns 30. |
||
| 1890 | */ |
||
| 1891 | public function increase_timeout() { |
||
| 1894 | |||
| 1895 | /** |
||
| 1896 | * Builds a URL to the Jetpack connection auth page. |
||
| 1897 | * |
||
| 1898 | * @param WP_User $user (optional) defaults to the current logged in user. |
||
| 1899 | * @param String $redirect (optional) a redirect URL to use instead of the default. |
||
| 1900 | * @return string Connect URL. |
||
| 1901 | */ |
||
| 1902 | public function get_authorization_url( $user = null, $redirect = null ) { |
||
| 1988 | |||
| 1989 | /** |
||
| 1990 | * Authorizes the user by obtaining and storing the user token. |
||
| 1991 | * |
||
| 1992 | * @param array $data The request data. |
||
| 1993 | * @return string|\WP_Error Returns a string on success. |
||
| 1994 | * Returns a \WP_Error on failure. |
||
| 1995 | */ |
||
| 1996 | public function authorize( $data = array() ) { |
||
| 2082 | |||
| 2083 | /** |
||
| 2084 | * Disconnects from the Jetpack servers. |
||
| 2085 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 2086 | */ |
||
| 2087 | public function disconnect_site() { |
||
| 2090 | |||
| 2091 | /** |
||
| 2092 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
| 2093 | * |
||
| 2094 | * @param string $text The string to hash. |
||
| 2095 | * @return string |
||
| 2096 | */ |
||
| 2097 | public function sha1_base64( $text ) { |
||
| 2100 | |||
| 2101 | /** |
||
| 2102 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 2103 | * |
||
| 2104 | * @param string $domain The domain to check. |
||
| 2105 | * |
||
| 2106 | * @return bool|WP_Error |
||
| 2107 | */ |
||
| 2108 | public function is_usable_domain( $domain ) { |
||
| 2195 | |||
| 2196 | /** |
||
| 2197 | * Gets the requested token. |
||
| 2198 | * |
||
| 2199 | * Tokens are one of two types: |
||
| 2200 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
| 2201 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
| 2202 | * are not associated with a user account. They represent the site's connection with |
||
| 2203 | * the Jetpack servers. |
||
| 2204 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
| 2205 | * |
||
| 2206 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
| 2207 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
| 2208 | * over the network; it's used only for signing things. |
||
| 2209 | * |
||
| 2210 | * Blog Tokens can be "Normal" or "Special". |
||
| 2211 | * * Normal: The result of a normal connection flow. They look like |
||
| 2212 | * "{$random_string_1}.{$random_string_2}" |
||
| 2213 | * That is, $token_key and $private are both random strings. |
||
| 2214 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
| 2215 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
| 2216 | * constant (rare). |
||
| 2217 | * * Special: A connection token for sites that have gone through an alternative |
||
| 2218 | * connection flow. They look like: |
||
| 2219 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
| 2220 | * That is, $private is a random string and $token_key has a special structure with |
||
| 2221 | * lots of semicolons. |
||
| 2222 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
| 2223 | * JETPACK_BLOG_TOKEN constant. |
||
| 2224 | * |
||
| 2225 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
| 2226 | * Special Blog Tokens always do. |
||
| 2227 | * |
||
| 2228 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
| 2229 | * order: |
||
| 2230 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 2231 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
| 2232 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 2233 | * |
||
| 2234 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 2235 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 2236 | * @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. |
||
| 2237 | * |
||
| 2238 | * @return object|false |
||
| 2239 | */ |
||
| 2240 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
| 2335 | |||
| 2336 | /** |
||
| 2337 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
||
| 2338 | * since it is passed by reference to various methods. |
||
| 2339 | * Capture it here so we can verify the signature later. |
||
| 2340 | * |
||
| 2341 | * @param array $methods an array of available XMLRPC methods. |
||
| 2342 | * @return array the same array, since this method doesn't add or remove anything. |
||
| 2343 | */ |
||
| 2344 | public function xmlrpc_methods( $methods ) { |
||
| 2348 | |||
| 2349 | /** |
||
| 2350 | * Resets the raw post data parameter for testing purposes. |
||
| 2351 | */ |
||
| 2352 | public function reset_raw_post_data() { |
||
| 2355 | |||
| 2356 | /** |
||
| 2357 | * Registering an additional method. |
||
| 2358 | * |
||
| 2359 | * @param array $methods an array of available XMLRPC methods. |
||
| 2360 | * @return array the amended array in case the method is added. |
||
| 2361 | */ |
||
| 2362 | public function public_xmlrpc_methods( $methods ) { |
||
| 2368 | |||
| 2369 | /** |
||
| 2370 | * Handles a getOptions XMLRPC method call. |
||
| 2371 | * |
||
| 2372 | * @param array $args method call arguments. |
||
| 2373 | * @return an amended XMLRPC server options array. |
||
| 2374 | */ |
||
| 2375 | public function jetpack_get_options( $args ) { |
||
| 2416 | |||
| 2417 | /** |
||
| 2418 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
| 2419 | * |
||
| 2420 | * @param array $options standard Core options. |
||
| 2421 | * @return array amended options. |
||
| 2422 | */ |
||
| 2423 | public function xmlrpc_options( $options ) { |
||
| 2441 | |||
| 2442 | /** |
||
| 2443 | * Resets the saved authentication state in between testing requests. |
||
| 2444 | */ |
||
| 2445 | public function reset_saved_auth_state() { |
||
| 2448 | |||
| 2449 | /** |
||
| 2450 | * Sign a user role with the master access token. |
||
| 2451 | * If not specified, will default to the current user. |
||
| 2452 | * |
||
| 2453 | * @access public |
||
| 2454 | * |
||
| 2455 | * @param string $role User role. |
||
| 2456 | * @param int $user_id ID of the user. |
||
| 2457 | * @return string Signed user role. |
||
| 2458 | */ |
||
| 2459 | public function sign_role( $role, $user_id = null ) { |
||
| 2475 | |||
| 2476 | /** |
||
| 2477 | * Set the plugin instance. |
||
| 2478 | * |
||
| 2479 | * @param Plugin $plugin_instance The plugin instance. |
||
| 2480 | * |
||
| 2481 | * @return $this |
||
| 2482 | */ |
||
| 2483 | public function set_plugin_instance( Plugin $plugin_instance ) { |
||
| 2488 | |||
| 2489 | /** |
||
| 2490 | * Retrieve the plugin management object. |
||
| 2491 | * |
||
| 2492 | * @return Plugin |
||
| 2493 | */ |
||
| 2494 | public function get_plugin() { |
||
| 2497 | |||
| 2498 | /** |
||
| 2499 | * Get all connected plugins information, excluding those disconnected by user. |
||
| 2500 | * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
||
| 2501 | * Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
||
| 2502 | * so please make sure not to run the method too early in the code. |
||
| 2503 | * |
||
| 2504 | * @return array|WP_Error |
||
| 2505 | */ |
||
| 2506 | public function get_connected_plugins() { |
||
| 2515 | |||
| 2516 | /** |
||
| 2517 | * Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
||
| 2518 | * Note: this method does not remove any access tokens. |
||
| 2519 | * |
||
| 2520 | * @return bool |
||
| 2521 | */ |
||
| 2522 | public function disable_plugin() { |
||
| 2529 | |||
| 2530 | /** |
||
| 2531 | * Force plugin reconnect after user-initiated disconnect. |
||
| 2532 | * After its called, the plugin will be allowed to use the connection again. |
||
| 2533 | * Note: this method does not initialize access tokens. |
||
| 2534 | * |
||
| 2535 | * @return bool |
||
| 2536 | */ |
||
| 2537 | public function enable_plugin() { |
||
| 2544 | |||
| 2545 | /** |
||
| 2546 | * Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
||
| 2547 | * If no plugin slug was passed into the constructor, always returns true. |
||
| 2548 | * |
||
| 2549 | * @return bool |
||
| 2550 | */ |
||
| 2551 | public function is_plugin_enabled() { |
||
| 2558 | |||
| 2559 | /** |
||
| 2560 | * Perform the API request to refresh the blog token. |
||
| 2561 | * Note that we are making this request on behalf of the Jetpack master user, |
||
| 2562 | * given they were (most probably) the ones that registered the site at the first place. |
||
| 2563 | * |
||
| 2564 | * @return WP_Error|bool The result of updating the blog_token option. |
||
| 2565 | */ |
||
| 2566 | public static function refresh_blog_token() { |
||
| 2617 | |||
| 2618 | /** |
||
| 2619 | * Disconnect the user from WP.com, and initiate the reconnect process. |
||
| 2620 | * |
||
| 2621 | * @return bool |
||
| 2622 | */ |
||
| 2623 | public static function refresh_user_token() { |
||
| 2630 | |||
| 2631 | /** |
||
| 2632 | * Fetches a signed token. |
||
| 2633 | * |
||
| 2634 | * @param object $token the token. |
||
| 2635 | * @return WP_Error|string a signed token |
||
| 2636 | */ |
||
| 2637 | public function get_signed_token( $token ) { |
||
| 2685 | |||
| 2686 | /** |
||
| 2687 | * If connection is active, add the list of plugins using connection to the heartbeat (except Jetpack itself) |
||
| 2688 | * |
||
| 2689 | * @param array $stats The Heartbeat stats array. |
||
| 2690 | * @return array $stats |
||
| 2691 | */ |
||
| 2692 | public function add_stats_to_heartbeat( $stats ) { |
||
| 2707 | |||
| 2708 | } |
||
| 2709 |
This check looks for
@paramannotations 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.