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 |
||
| 19 | class Manager { |
||
| 20 | |||
| 21 | const SECRETS_MISSING = 'secrets_missing'; |
||
| 22 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
| 23 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
| 24 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
| 25 | const JETPACK_MASTER_USER = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The procedure that should be run to generate secrets. |
||
| 29 | * |
||
| 30 | * @var Callable |
||
| 31 | */ |
||
| 32 | protected $secret_callable; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * A copy of the raw POST data for signature verification purposes. |
||
| 36 | * |
||
| 37 | * @var String |
||
| 38 | */ |
||
| 39 | protected $raw_post_data; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Verification data needs to be stored to properly verify everything. |
||
| 43 | * |
||
| 44 | * @var Object |
||
| 45 | */ |
||
| 46 | private $xmlrpc_verification = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Plugin management object. |
||
| 50 | * |
||
| 51 | * @var Plugin |
||
| 52 | */ |
||
| 53 | private $plugin = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Initialize the object. |
||
| 57 | * Make sure to call the "Configure" first. |
||
| 58 | * |
||
| 59 | * @param string $plugin_slug Slug of the plugin using the connection (optional, but encouraged). |
||
|
|
|||
| 60 | * |
||
| 61 | * @see \Automattic\Jetpack\Config |
||
| 62 | */ |
||
| 63 | public function __construct( $plugin_slug = null ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initializes required listeners. This is done separately from the constructors |
||
| 71 | * because some objects sometimes need to instantiate separate objects of this class. |
||
| 72 | * |
||
| 73 | * @todo Implement a proper nonce verification. |
||
| 74 | */ |
||
| 75 | public static function configure() { |
||
| 76 | $manager = new self(); |
||
| 77 | |||
| 78 | $manager->setup_xmlrpc_handlers( |
||
| 79 | $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 80 | $manager->is_active(), |
||
| 81 | $manager->verify_xml_rpc_signature() |
||
| 82 | ); |
||
| 83 | |||
| 84 | if ( $manager->is_active() ) { |
||
| 85 | add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) ); |
||
| 86 | } else { |
||
| 87 | add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
||
| 88 | } |
||
| 89 | |||
| 90 | add_action( 'jetpack_clean_nonces', array( $manager, 'clean_nonces' ) ); |
||
| 91 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
| 92 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
| 93 | } |
||
| 94 | |||
| 95 | add_filter( |
||
| 96 | 'jetpack_constant_default_value', |
||
| 97 | __NAMESPACE__ . '\Utils::jetpack_api_constant_filter', |
||
| 98 | 10, |
||
| 99 | 2 |
||
| 100 | ); |
||
| 101 | |||
| 102 | add_action( 'plugins_loaded', __NAMESPACE__ . '\Plugin_Storage::configure', 100 ); |
||
| 103 | |||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Sets up the XMLRPC request handlers. |
||
| 108 | * |
||
| 109 | * @param array $request_params incoming request parameters. |
||
| 110 | * @param Boolean $is_active whether the connection is currently active. |
||
| 111 | * @param Boolean $is_signed whether the signature check has been successful. |
||
| 112 | * @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
||
| 113 | */ |
||
| 114 | public function setup_xmlrpc_handlers( |
||
| 115 | $request_params, |
||
| 116 | $is_active, |
||
| 117 | $is_signed, |
||
| 118 | \Jetpack_XMLRPC_Server $xmlrpc_server = null |
||
| 119 | ) { |
||
| 120 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ), 1000, 2 ); |
||
| 121 | |||
| 122 | if ( |
||
| 123 | ! isset( $request_params['for'] ) |
||
| 124 | || 'jetpack' !== $request_params['for'] |
||
| 125 | ) { |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | |||
| 129 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms. |
||
| 130 | if ( |
||
| 131 | isset( $request_params['jetpack'] ) |
||
| 132 | && 'comms' === $request_params['jetpack'] |
||
| 133 | ) { |
||
| 134 | if ( ! Constants::is_defined( 'XMLRPC_REQUEST' ) ) { |
||
| 135 | // Use the real constant here for WordPress' sake. |
||
| 136 | define( 'XMLRPC_REQUEST', true ); |
||
| 137 | } |
||
| 138 | |||
| 139 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
| 140 | |||
| 141 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
| 142 | } |
||
| 143 | |||
| 144 | if ( ! Constants::get_constant( 'XMLRPC_REQUEST' ) ) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | // Display errors can cause the XML to be not well formed. |
||
| 148 | @ini_set( 'display_errors', false ); // phpcs:ignore |
||
| 149 | |||
| 150 | if ( $xmlrpc_server ) { |
||
| 151 | $this->xmlrpc_server = $xmlrpc_server; |
||
| 152 | } else { |
||
| 153 | $this->xmlrpc_server = new \Jetpack_XMLRPC_Server(); |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->require_jetpack_authentication(); |
||
| 157 | |||
| 158 | if ( $is_active ) { |
||
| 159 | // Hack to preserve $HTTP_RAW_POST_DATA. |
||
| 160 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 161 | |||
| 162 | if ( $is_signed ) { |
||
| 163 | // The actual API methods. |
||
| 164 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
| 165 | } else { |
||
| 166 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
| 167 | // active Jetpack connection, so that additional users can link their account. |
||
| 168 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | // The bootstrap API methods. |
||
| 172 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
| 173 | |||
| 174 | if ( $is_signed ) { |
||
| 175 | // The jetpack Provision method is available for blog-token-signed requests. |
||
| 176 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
||
| 177 | } else { |
||
| 178 | new XMLRPC_Connector( $this ); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
| 183 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
| 184 | return true; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Initializes the REST API connector on the init hook. |
||
| 189 | */ |
||
| 190 | public function initialize_rest_api_registration_connector() { |
||
| 191 | new REST_Connector( $this ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
| 196 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
| 197 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
| 198 | * which is accessible via a different URI. Most of the below is copied directly |
||
| 199 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
| 200 | * |
||
| 201 | * @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
||
| 202 | */ |
||
| 203 | public function alternate_xmlrpc() { |
||
| 204 | // phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
||
| 205 | // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited |
||
| 206 | global $HTTP_RAW_POST_DATA; |
||
| 207 | |||
| 208 | // Some browser-embedded clients send cookies. We don't want them. |
||
| 209 | $_COOKIE = array(); |
||
| 210 | |||
| 211 | // A fix for mozBlog and other cases where '<?xml' isn't on the very first line. |
||
| 212 | if ( isset( $HTTP_RAW_POST_DATA ) ) { |
||
| 213 | $HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
||
| 214 | } |
||
| 215 | |||
| 216 | // phpcs:enable |
||
| 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 ) { |
||
| 248 | $jetpack_methods = array(); |
||
| 249 | |||
| 250 | foreach ( $methods as $method => $callback ) { |
||
| 251 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
| 252 | $jetpack_methods[ $method ] = $callback; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | return $jetpack_methods; |
||
| 257 | } |
||
| 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() { |
||
| 264 | // Don't let anyone authenticate. |
||
| 265 | $_COOKIE = array(); |
||
| 266 | remove_all_filters( 'authenticate' ); |
||
| 267 | remove_all_actions( 'wp_login_failed' ); |
||
| 268 | |||
| 269 | if ( $this->is_active() ) { |
||
| 270 | // Allow Jetpack authentication. |
||
| 271 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
| 272 | } |
||
| 273 | } |
||
| 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 ) { |
||
| 284 | if ( is_a( $user, '\\WP_User' ) ) { |
||
| 285 | return $user; |
||
| 286 | } |
||
| 287 | |||
| 288 | $token_details = $this->verify_xml_rpc_signature(); |
||
| 289 | |||
| 290 | if ( ! $token_details ) { |
||
| 291 | return $user; |
||
| 292 | } |
||
| 293 | |||
| 294 | if ( 'user' !== $token_details['type'] ) { |
||
| 295 | return $user; |
||
| 296 | } |
||
| 297 | |||
| 298 | if ( ! $token_details['user_id'] ) { |
||
| 299 | return $user; |
||
| 300 | } |
||
| 301 | |||
| 302 | nocache_headers(); |
||
| 303 | |||
| 304 | return new \WP_User( $token_details['user_id'] ); |
||
| 305 | } |
||
| 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 | * Error codes: |
||
| 321 | * - malformed_token |
||
| 322 | * - malformed_user_id |
||
| 323 | * - unknown_token |
||
| 324 | * - could_not_sign |
||
| 325 | * - invalid_nonce |
||
| 326 | * - signature_mismatch |
||
| 327 | * |
||
| 328 | * @since 7.5.0 |
||
| 329 | * |
||
| 330 | * @param WP_Error $signature_verification_error The verification error |
||
| 331 | */ |
||
| 332 | do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Verifies the signature of the current request. |
||
| 341 | * |
||
| 342 | * This function has side effects and should not be used. Instead, |
||
| 343 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
| 344 | * |
||
| 345 | * @internal |
||
| 346 | * @todo Refactor to use proper nonce verification. |
||
| 347 | */ |
||
| 348 | private function internal_verify_xml_rpc_signature() { |
||
| 349 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
| 350 | // It's not for us. |
||
| 351 | if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { |
||
| 352 | return false; |
||
| 353 | } |
||
| 354 | |||
| 355 | $signature_details = array( |
||
| 356 | 'token' => isset( $_GET['token'] ) ? wp_unslash( $_GET['token'] ) : '', |
||
| 357 | 'timestamp' => isset( $_GET['timestamp'] ) ? wp_unslash( $_GET['timestamp'] ) : '', |
||
| 358 | 'nonce' => isset( $_GET['nonce'] ) ? wp_unslash( $_GET['nonce'] ) : '', |
||
| 359 | 'body_hash' => isset( $_GET['body-hash'] ) ? wp_unslash( $_GET['body-hash'] ) : '', |
||
| 360 | 'method' => wp_unslash( $_SERVER['REQUEST_METHOD'] ), |
||
| 361 | 'url' => wp_unslash( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ), // Temp - will get real signature URL later. |
||
| 362 | 'signature' => isset( $_GET['signature'] ) ? wp_unslash( $_GET['signature'] ) : '', |
||
| 363 | ); |
||
| 364 | |||
| 365 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
||
| 366 | @list( $token_key, $version, $user_id ) = explode( ':', wp_unslash( $_GET['token'] ) ); |
||
| 367 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
||
| 368 | |||
| 369 | $jetpack_api_version = Constants::get_constant( 'JETPACK__API_VERSION' ); |
||
| 370 | |||
| 371 | if ( |
||
| 372 | empty( $token_key ) |
||
| 373 | || |
||
| 374 | empty( $version ) || strval( $jetpack_api_version ) !== $version ) { |
||
| 375 | return new \WP_Error( 'malformed_token', 'Malformed token in request', compact( 'signature_details' ) ); |
||
| 376 | } |
||
| 377 | |||
| 378 | if ( '0' === $user_id ) { |
||
| 379 | $token_type = 'blog'; |
||
| 380 | $user_id = 0; |
||
| 381 | } else { |
||
| 382 | $token_type = 'user'; |
||
| 383 | if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { |
||
| 384 | return new \WP_Error( |
||
| 385 | 'malformed_user_id', |
||
| 386 | 'Malformed user_id in request', |
||
| 387 | compact( 'signature_details' ) |
||
| 388 | ); |
||
| 389 | } |
||
| 390 | $user_id = (int) $user_id; |
||
| 391 | |||
| 392 | $user = new \WP_User( $user_id ); |
||
| 393 | if ( ! $user || ! $user->exists() ) { |
||
| 394 | return new \WP_Error( |
||
| 395 | 'unknown_user', |
||
| 396 | sprintf( 'User %d does not exist', $user_id ), |
||
| 397 | compact( 'signature_details' ) |
||
| 398 | ); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | $token = $this->get_access_token( $user_id, $token_key, false ); |
||
| 403 | if ( is_wp_error( $token ) ) { |
||
| 404 | $token->add_data( compact( 'signature_details' ) ); |
||
| 405 | return $token; |
||
| 406 | } elseif ( ! $token ) { |
||
| 407 | return new \WP_Error( |
||
| 408 | 'unknown_token', |
||
| 409 | sprintf( 'Token %s:%s:%d does not exist', $token_key, $version, $user_id ), |
||
| 410 | compact( 'signature_details' ) |
||
| 411 | ); |
||
| 412 | } |
||
| 413 | |||
| 414 | $jetpack_signature = new \Jetpack_Signature( $token->secret, (int) \Jetpack_Options::get_option( 'time_diff' ) ); |
||
| 415 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
||
| 416 | if ( isset( $_POST['_jetpack_is_multipart'] ) ) { |
||
| 417 | $post_data = $_POST; |
||
| 418 | $file_hashes = array(); |
||
| 419 | foreach ( $post_data as $post_data_key => $post_data_value ) { |
||
| 420 | if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { |
||
| 421 | continue; |
||
| 422 | } |
||
| 423 | $post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); |
||
| 424 | $file_hashes[ $post_data_key ] = $post_data_value; |
||
| 425 | } |
||
| 426 | |||
| 427 | foreach ( $file_hashes as $post_data_key => $post_data_value ) { |
||
| 428 | unset( $post_data[ "_jetpack_file_hmac_{$post_data_key}" ] ); |
||
| 429 | $post_data[ $post_data_key ] = $post_data_value; |
||
| 430 | } |
||
| 431 | |||
| 432 | ksort( $post_data ); |
||
| 433 | |||
| 434 | $body = http_build_query( stripslashes_deep( $post_data ) ); |
||
| 435 | } elseif ( is_null( $this->raw_post_data ) ) { |
||
| 436 | $body = file_get_contents( 'php://input' ); |
||
| 437 | } else { |
||
| 438 | $body = null; |
||
| 439 | } |
||
| 440 | // phpcs:enable |
||
| 441 | |||
| 442 | $signature = $jetpack_signature->sign_current_request( |
||
| 443 | array( 'body' => is_null( $body ) ? $this->raw_post_data : $body ) |
||
| 444 | ); |
||
| 445 | |||
| 446 | $signature_details['url'] = $jetpack_signature->current_request_url; |
||
| 447 | |||
| 448 | if ( ! $signature ) { |
||
| 449 | return new \WP_Error( |
||
| 450 | 'could_not_sign', |
||
| 451 | 'Unknown signature error', |
||
| 452 | compact( 'signature_details' ) |
||
| 453 | ); |
||
| 454 | } elseif ( is_wp_error( $signature ) ) { |
||
| 455 | return $signature; |
||
| 456 | } |
||
| 457 | |||
| 458 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
| 459 | $timestamp = (int) $_GET['timestamp']; |
||
| 460 | $nonce = stripslashes( (string) $_GET['nonce'] ); |
||
| 461 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
||
| 462 | |||
| 463 | // Use up the nonce regardless of whether the signature matches. |
||
| 464 | if ( ! $this->add_nonce( $timestamp, $nonce ) ) { |
||
| 465 | return new \WP_Error( |
||
| 466 | 'invalid_nonce', |
||
| 467 | 'Could not add nonce', |
||
| 468 | compact( 'signature_details' ) |
||
| 469 | ); |
||
| 470 | } |
||
| 471 | |||
| 472 | // Be careful about what you do with this debugging data. |
||
| 473 | // If a malicious requester has access to the expected signature, |
||
| 474 | // bad things might be possible. |
||
| 475 | $signature_details['expected'] = $signature; |
||
| 476 | |||
| 477 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 478 | if ( ! hash_equals( $signature, $_GET['signature'] ) ) { |
||
| 479 | return new \WP_Error( |
||
| 480 | 'signature_mismatch', |
||
| 481 | 'Signature mismatch', |
||
| 482 | compact( 'signature_details' ) |
||
| 483 | ); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Action for additional token checking. |
||
| 488 | * |
||
| 489 | * @since 7.7.0 |
||
| 490 | * |
||
| 491 | * @param array $post_data request data. |
||
| 492 | * @param array $token_data token data. |
||
| 493 | */ |
||
| 494 | return apply_filters( |
||
| 495 | 'jetpack_signature_check_token', |
||
| 496 | array( |
||
| 497 | 'type' => $token_type, |
||
| 498 | 'token_key' => $token_key, |
||
| 499 | 'user_id' => $token->external_user_id, |
||
| 500 | ), |
||
| 501 | $token, |
||
| 502 | $this->raw_post_data |
||
| 503 | ); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Returns true if the current site is connected to WordPress.com. |
||
| 508 | * |
||
| 509 | * @return Boolean is the site connected? |
||
| 510 | */ |
||
| 511 | public function is_active() { |
||
| 512 | return (bool) $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
||
| 517 | * |
||
| 518 | * @access public |
||
| 519 | * |
||
| 520 | * @return bool |
||
| 521 | */ |
||
| 522 | public function is_registered() { |
||
| 523 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
||
| 524 | $has_token = $this->is_active(); |
||
| 525 | return $blog_id && $has_token; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Checks to see if the connection owner of the site is missing. |
||
| 530 | * |
||
| 531 | * @return bool |
||
| 532 | */ |
||
| 533 | public function is_missing_connection_owner() { |
||
| 534 | $connection_owner = $this->get_connection_owner_id(); |
||
| 535 | if ( ! get_user_by( 'id', $connection_owner ) ) { |
||
| 536 | return true; |
||
| 537 | } |
||
| 538 | |||
| 539 | return false; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns true if the user with the specified identifier is connected to |
||
| 544 | * WordPress.com. |
||
| 545 | * |
||
| 546 | * @param Integer|Boolean $user_id the user identifier. |
||
| 547 | * @return Boolean is the user connected? |
||
| 548 | */ |
||
| 549 | public function is_user_connected( $user_id = false ) { |
||
| 550 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
| 551 | if ( ! $user_id ) { |
||
| 552 | return false; |
||
| 553 | } |
||
| 554 | |||
| 555 | return (bool) $this->get_access_token( $user_id ); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Returns the local user ID of the connection owner. |
||
| 560 | * |
||
| 561 | * @return string|int Returns the ID of the connection owner or False if no connection owner found. |
||
| 562 | */ |
||
| 563 | View Code Duplication | public function get_connection_owner_id() { |
|
| 564 | $user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 565 | $connection_owner = false; |
||
| 566 | if ( $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) ) { |
||
| 567 | $connection_owner = $user_token->external_user_id; |
||
| 568 | } |
||
| 569 | |||
| 570 | return $connection_owner; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns an array of user_id's that have user tokens for communicating with wpcom. |
||
| 575 | * Able to select by specific capability. |
||
| 576 | * |
||
| 577 | * @param string $capability The capability of the user. |
||
| 578 | * @return array Array of WP_User objects if found. |
||
| 579 | */ |
||
| 580 | public function get_connected_users( $capability = 'any' ) { |
||
| 581 | $connected_users = array(); |
||
| 582 | $connected_user_ids = array_keys( \Jetpack_Options::get_option( 'user_tokens' ) ); |
||
| 583 | |||
| 584 | if ( ! empty( $connected_user_ids ) ) { |
||
| 585 | foreach ( $connected_user_ids as $id ) { |
||
| 586 | // Check for capability. |
||
| 587 | if ( 'any' !== $capability && ! user_can( $id, $capability ) ) { |
||
| 588 | continue; |
||
| 589 | } |
||
| 590 | |||
| 591 | $connected_users[] = get_userdata( $id ); |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | return $connected_users; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Get the wpcom user data of the current|specified connected user. |
||
| 600 | * |
||
| 601 | * @todo Refactor to properly load the XMLRPC client independently. |
||
| 602 | * |
||
| 603 | * @param Integer $user_id the user identifier. |
||
| 604 | * @return Object the user object. |
||
| 605 | */ |
||
| 606 | View Code Duplication | public function get_connected_user_data( $user_id = null ) { |
|
| 607 | if ( ! $user_id ) { |
||
| 608 | $user_id = get_current_user_id(); |
||
| 609 | } |
||
| 610 | |||
| 611 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 612 | $cached_user_data = get_transient( $transient_key ); |
||
| 613 | |||
| 614 | if ( $cached_user_data ) { |
||
| 615 | return $cached_user_data; |
||
| 616 | } |
||
| 617 | |||
| 618 | $xml = new \Jetpack_IXR_Client( |
||
| 619 | array( |
||
| 620 | 'user_id' => $user_id, |
||
| 621 | ) |
||
| 622 | ); |
||
| 623 | $xml->query( 'wpcom.getUser' ); |
||
| 624 | if ( ! $xml->isError() ) { |
||
| 625 | $user_data = $xml->getResponse(); |
||
| 626 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
| 627 | return $user_data; |
||
| 628 | } |
||
| 629 | |||
| 630 | return false; |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Returns a user object of the connection owner. |
||
| 635 | * |
||
| 636 | * @return object|false False if no connection owner found. |
||
| 637 | */ |
||
| 638 | View Code Duplication | public function get_connection_owner() { |
|
| 639 | $user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 640 | |||
| 641 | $connection_owner = false; |
||
| 642 | if ( $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) ) { |
||
| 643 | $connection_owner = get_userdata( $user_token->external_user_id ); |
||
| 644 | } |
||
| 645 | |||
| 646 | return $connection_owner; |
||
| 647 | } |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Returns true if the provided user is the Jetpack connection owner. |
||
| 651 | * If user ID is not specified, the current user will be used. |
||
| 652 | * |
||
| 653 | * @param Integer|Boolean $user_id the user identifier. False for current user. |
||
| 654 | * @return Boolean True the user the connection owner, false otherwise. |
||
| 655 | */ |
||
| 656 | View Code Duplication | public function is_connection_owner( $user_id = false ) { |
|
| 657 | if ( ! $user_id ) { |
||
| 658 | $user_id = get_current_user_id(); |
||
| 659 | } |
||
| 660 | |||
| 661 | $user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
| 662 | |||
| 663 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id; |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Connects the user with a specified ID to a WordPress.com user using the |
||
| 668 | * remote login flow. |
||
| 669 | * |
||
| 670 | * @access public |
||
| 671 | * |
||
| 672 | * @param Integer $user_id (optional) the user identifier, defaults to current user. |
||
| 673 | * @param String $redirect_url the URL to redirect the user to for processing, defaults to |
||
| 674 | * admin_url(). |
||
| 675 | * @return WP_Error only in case of a failed user lookup. |
||
| 676 | */ |
||
| 677 | public function connect_user( $user_id = null, $redirect_url = null ) { |
||
| 678 | $user = null; |
||
| 679 | if ( null === $user_id ) { |
||
| 680 | $user = wp_get_current_user(); |
||
| 681 | } else { |
||
| 682 | $user = get_user_by( 'ID', $user_id ); |
||
| 683 | } |
||
| 684 | |||
| 685 | if ( empty( $user ) ) { |
||
| 686 | return new \WP_Error( 'user_not_found', 'Attempting to connect a non-existent user.' ); |
||
| 687 | } |
||
| 688 | |||
| 689 | if ( null === $redirect_url ) { |
||
| 690 | $redirect_url = admin_url(); |
||
| 691 | } |
||
| 692 | |||
| 693 | // Using wp_redirect intentionally because we're redirecting outside. |
||
| 694 | wp_redirect( $this->get_authorization_url( $user ) ); // phpcs:ignore WordPress.Security.SafeRedirect |
||
| 695 | exit(); |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Unlinks the current user from the linked WordPress.com user. |
||
| 700 | * |
||
| 701 | * @access public |
||
| 702 | * @static |
||
| 703 | * |
||
| 704 | * @todo Refactor to properly load the XMLRPC client independently. |
||
| 705 | * |
||
| 706 | * @param Integer $user_id the user identifier. |
||
| 707 | * @return Boolean Whether the disconnection of the user was successful. |
||
| 708 | */ |
||
| 709 | public static function disconnect_user( $user_id = null ) { |
||
| 710 | $tokens = \Jetpack_Options::get_option( 'user_tokens' ); |
||
| 711 | if ( ! $tokens ) { |
||
| 712 | return false; |
||
| 713 | } |
||
| 714 | |||
| 715 | $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
||
| 716 | |||
| 717 | if ( \Jetpack_Options::get_option( 'master_user' ) === $user_id ) { |
||
| 718 | return false; |
||
| 719 | } |
||
| 720 | |||
| 721 | if ( ! isset( $tokens[ $user_id ] ) ) { |
||
| 722 | return false; |
||
| 723 | } |
||
| 724 | |||
| 725 | $xml = new \Jetpack_IXR_Client( compact( 'user_id' ) ); |
||
| 726 | $xml->query( 'jetpack.unlink_user', $user_id ); |
||
| 727 | |||
| 728 | unset( $tokens[ $user_id ] ); |
||
| 729 | |||
| 730 | \Jetpack_Options::update_option( 'user_tokens', $tokens ); |
||
| 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 ) { |
||
| 751 | $api_base = Constants::get_constant( 'JETPACK__API_BASE' ); |
||
| 752 | $api_version = '/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/'; |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Filters whether the connection manager should use the iframe authorization |
||
| 756 | * flow instead of the regular redirect-based flow. |
||
| 757 | * |
||
| 758 | * @since 8.3.0 |
||
| 759 | * |
||
| 760 | * @param Boolean $is_iframe_flow_used should the iframe flow be used, defaults to false. |
||
| 761 | */ |
||
| 762 | $iframe_flow = apply_filters( 'jetpack_use_iframe_authorization_flow', false ); |
||
| 763 | |||
| 764 | // Do not modify anything that is not related to authorize requests. |
||
| 765 | if ( 'authorize' === $relative_url && $iframe_flow ) { |
||
| 766 | $relative_url = 'authorize_iframe'; |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Filters the API URL that Jetpack uses for server communication. |
||
| 771 | * |
||
| 772 | * @since 8.0.0 |
||
| 773 | * |
||
| 774 | * @param String $url the generated URL. |
||
| 775 | * @param String $relative_url the relative URL that was passed as an argument. |
||
| 776 | * @param String $api_base the API base string that is being used. |
||
| 777 | * @param String $api_version the API version string that is being used. |
||
| 778 | */ |
||
| 779 | return apply_filters( |
||
| 780 | 'jetpack_api_url', |
||
| 781 | rtrim( $api_base . $relative_url, '/\\' ) . $api_version, |
||
| 782 | $relative_url, |
||
| 783 | $api_base, |
||
| 784 | $api_version |
||
| 785 | ); |
||
| 786 | } |
||
| 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() { |
||
| 794 | $base = preg_replace( |
||
| 795 | '#(https?://[^?/]+)(/?.*)?$#', |
||
| 796 | '\\1', |
||
| 797 | Constants::get_constant( 'JETPACK__API_BASE' ) |
||
| 798 | ); |
||
| 799 | return untrailingslashit( $base ) . '/xmlrpc.php'; |
||
| 800 | } |
||
| 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 Integer zero on success, or a bitmask on failure. |
||
| 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 | ) |
||
| 862 | ); |
||
| 863 | |||
| 864 | $args = array( |
||
| 865 | 'method' => 'POST', |
||
| 866 | 'body' => $body, |
||
| 867 | 'headers' => array( |
||
| 868 | 'Accept' => 'application/json', |
||
| 869 | ), |
||
| 870 | 'timeout' => $timeout, |
||
| 871 | ); |
||
| 872 | |||
| 873 | $args['body'] = $this->apply_activation_source_to_args( $args['body'] ); |
||
| 874 | |||
| 875 | // TODO: fix URLs for bad hosts. |
||
| 876 | $response = Client::_wp_remote_request( |
||
| 877 | $this->api_url( $api_endpoint ), |
||
| 878 | $args, |
||
| 879 | true |
||
| 880 | ); |
||
| 881 | |||
| 882 | // Make sure the response is valid and does not contain any Jetpack errors. |
||
| 883 | $registration_details = $this->validate_remote_register_response( $response ); |
||
| 884 | |||
| 885 | if ( is_wp_error( $registration_details ) ) { |
||
| 886 | return $registration_details; |
||
| 887 | } elseif ( ! $registration_details ) { |
||
| 888 | return new \WP_Error( |
||
| 889 | 'unknown_error', |
||
| 890 | 'Unknown error registering your Jetpack site.', |
||
| 891 | wp_remote_retrieve_response_code( $response ) |
||
| 892 | ); |
||
| 893 | } |
||
| 894 | |||
| 895 | if ( empty( $registration_details->jetpack_secret ) || ! is_string( $registration_details->jetpack_secret ) ) { |
||
| 896 | return new \WP_Error( |
||
| 897 | 'jetpack_secret', |
||
| 898 | 'Unable to validate registration of your Jetpack site.', |
||
| 899 | wp_remote_retrieve_response_code( $response ) |
||
| 900 | ); |
||
| 901 | } |
||
| 902 | |||
| 903 | if ( isset( $registration_details->jetpack_public ) ) { |
||
| 904 | $jetpack_public = (int) $registration_details->jetpack_public; |
||
| 905 | } else { |
||
| 906 | $jetpack_public = false; |
||
| 907 | } |
||
| 908 | |||
| 909 | \Jetpack_Options::update_options( |
||
| 910 | array( |
||
| 911 | 'id' => (int) $registration_details->jetpack_id, |
||
| 912 | 'blog_token' => (string) $registration_details->jetpack_secret, |
||
| 913 | 'public' => $jetpack_public, |
||
| 914 | ) |
||
| 915 | ); |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Fires when a site is registered on WordPress.com. |
||
| 919 | * |
||
| 920 | * @since 3.7.0 |
||
| 921 | * |
||
| 922 | * @param int $json->jetpack_id Jetpack Blog ID. |
||
| 923 | * @param string $json->jetpack_secret Jetpack Blog Token. |
||
| 924 | * @param int|bool $jetpack_public Is the site public. |
||
| 925 | */ |
||
| 926 | do_action( |
||
| 927 | 'jetpack_site_registered', |
||
| 928 | $registration_details->jetpack_id, |
||
| 929 | $registration_details->jetpack_secret, |
||
| 930 | $jetpack_public |
||
| 931 | ); |
||
| 932 | |||
| 933 | if ( isset( $registration_details->token ) ) { |
||
| 934 | /** |
||
| 935 | * Fires when a user token is sent along with the registration data. |
||
| 936 | * |
||
| 937 | * @since 7.6.0 |
||
| 938 | * |
||
| 939 | * @param object $token the administrator token for the newly registered site. |
||
| 940 | */ |
||
| 941 | do_action( 'jetpack_site_registered_user_token', $registration_details->token ); |
||
| 942 | } |
||
| 943 | |||
| 944 | return true; |
||
| 945 | } |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Takes the response from the Jetpack register new site endpoint and |
||
| 949 | * verifies it worked properly. |
||
| 950 | * |
||
| 951 | * @since 2.6 |
||
| 952 | * |
||
| 953 | * @param Mixed $response the response object, or the error object. |
||
| 954 | * @return string|WP_Error A JSON object on success or WP_Error on failures |
||
| 955 | **/ |
||
| 956 | protected function validate_remote_register_response( $response ) { |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * Adds a used nonce to a list of known nonces. |
||
| 1028 | * |
||
| 1029 | * @param int $timestamp the current request timestamp. |
||
| 1030 | * @param string $nonce the nonce value. |
||
| 1031 | * @return bool whether the nonce is unique or not. |
||
| 1032 | */ |
||
| 1033 | public function add_nonce( $timestamp, $nonce ) { |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Cleans nonces that were saved when calling ::add_nonce. |
||
| 1074 | * |
||
| 1075 | * @todo Properly prepare the query before executing it. |
||
| 1076 | * |
||
| 1077 | * @param bool $all whether to clean even non-expired nonces. |
||
| 1078 | */ |
||
| 1079 | public function clean_nonces( $all = false ) { |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 1103 | * |
||
| 1104 | * Based on local php max_execution_time in php.ini |
||
| 1105 | * |
||
| 1106 | * @since 5.4 |
||
| 1107 | * @return int |
||
| 1108 | **/ |
||
| 1109 | public function get_max_execution_time() { |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Sets a minimum request timeout, and returns the current timeout |
||
| 1121 | * |
||
| 1122 | * @since 5.4 |
||
| 1123 | * @param Integer $min_timeout the minimum timeout value. |
||
| 1124 | **/ |
||
| 1125 | View Code Duplication | public function set_min_time_limit( $min_timeout ) { |
|
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Get our assumed site creation date. |
||
| 1136 | * Calculated based on the earlier date of either: |
||
| 1137 | * - Earliest admin user registration date. |
||
| 1138 | * - Earliest date of post of any post type. |
||
| 1139 | * |
||
| 1140 | * @since 7.2.0 |
||
| 1141 | * |
||
| 1142 | * @return string Assumed site creation date and time. |
||
| 1143 | */ |
||
| 1144 | public function get_assumed_site_creation_date() { |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * Adds the activation source string as a parameter to passed arguments. |
||
| 1186 | * |
||
| 1187 | * @todo Refactor to use rawurlencode() instead of urlencode(). |
||
| 1188 | * |
||
| 1189 | * @param array $args arguments that need to have the source added. |
||
| 1190 | * @return array $amended arguments. |
||
| 1191 | */ |
||
| 1192 | View Code Duplication | public static function apply_activation_source_to_args( $args ) { |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Returns the callable that would be used to generate secrets. |
||
| 1210 | * |
||
| 1211 | * @return Callable a function that returns a secure string to be used as a secret. |
||
| 1212 | */ |
||
| 1213 | protected function get_secret_callable() { |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Runs the wp_generate_password function with the required parameters. This is the |
||
| 1228 | * default implementation of the secret callable, can be overridden using the |
||
| 1229 | * jetpack_connection_secret_generator filter. |
||
| 1230 | * |
||
| 1231 | * @return String $secret value. |
||
| 1232 | */ |
||
| 1233 | private function secret_callable_method() { |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Generates two secret tokens and the end of life timestamp for them. |
||
| 1239 | * |
||
| 1240 | * @param String $action The action name. |
||
| 1241 | * @param Integer $user_id The user identifier. |
||
| 1242 | * @param Integer $exp Expiration time in seconds. |
||
| 1243 | */ |
||
| 1244 | public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
| 1276 | |||
| 1277 | /** |
||
| 1278 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 1279 | * |
||
| 1280 | * @param String $action The action name. |
||
| 1281 | * @param Integer $user_id The user identifier. |
||
| 1282 | * @return string|array an array of secrets or an error string. |
||
| 1283 | */ |
||
| 1284 | public function get_secrets( $action, $user_id ) { |
||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Deletes secret tokens in case they, for example, have expired. |
||
| 1305 | * |
||
| 1306 | * @param String $action The action name. |
||
| 1307 | * @param Integer $user_id The user identifier. |
||
| 1308 | */ |
||
| 1309 | public function delete_secrets( $action, $user_id ) { |
||
| 1320 | |||
| 1321 | /** |
||
| 1322 | * Deletes all connection tokens and transients from the local Jetpack site. |
||
| 1323 | * If the plugin object has been provided in the constructor, the function first checks |
||
| 1324 | * whether it's the only active connection. |
||
| 1325 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1326 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1327 | * |
||
| 1328 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1329 | * |
||
| 1330 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1331 | */ |
||
| 1332 | public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
||
| 1333 | View Code Duplication | if ( ! $ignore_connected_plugins && null !== $this->plugin && ! $this->plugin->is_only() ) { |
|
| 1334 | return false; |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * Fires upon the disconnect attempt. |
||
| 1339 | * Return `false` to prevent the disconnect. |
||
| 1340 | * |
||
| 1341 | * @since 8.7.0 |
||
| 1342 | */ |
||
| 1343 | if ( ! apply_filters( 'jetpack_connection_delete_all_tokens', true, $this ) ) { |
||
| 1366 | |||
| 1367 | /** |
||
| 1368 | * Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
||
| 1369 | * If the plugin object has been provided in the constructor, the function first check |
||
| 1370 | * whether it's the only active connection. |
||
| 1371 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1372 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1373 | * |
||
| 1374 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1375 | * |
||
| 1376 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1377 | */ |
||
| 1378 | public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * Disconnect the plugin and remove the tokens. |
||
| 1401 | * This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
||
| 1402 | * This is a proxy method to simplify the Connection package API. |
||
| 1403 | * |
||
| 1404 | * @see Manager::disable_plugin() |
||
| 1405 | * @see Manager::disconnect_site_wpcom() |
||
| 1406 | * @see Manager::delete_all_connection_tokens() |
||
| 1407 | * |
||
| 1408 | * @return bool |
||
| 1409 | */ |
||
| 1410 | public function remove_connection() { |
||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Responds to a WordPress.com call to register the current site. |
||
| 1420 | * Should be changed to protected. |
||
| 1421 | * |
||
| 1422 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
| 1423 | */ |
||
| 1424 | public function handle_registration( array $registration_data ) { |
||
| 1432 | |||
| 1433 | /** |
||
| 1434 | * Verify a Previously Generated Secret. |
||
| 1435 | * |
||
| 1436 | * @param string $action The type of secret to verify. |
||
| 1437 | * @param string $secret_1 The secret string to compare to what is stored. |
||
| 1438 | * @param int $user_id The user ID of the owner of the secret. |
||
| 1439 | * @return \WP_Error|string WP_Error on failure, secret_2 on success. |
||
| 1440 | */ |
||
| 1441 | public function verify_secrets( $action, $secret_1, $user_id ) { |
||
| 1577 | |||
| 1578 | /** |
||
| 1579 | * Responds to a WordPress.com call to authorize the current user. |
||
| 1580 | * Should be changed to protected. |
||
| 1581 | */ |
||
| 1582 | public function handle_authorization() { |
||
| 1585 | |||
| 1586 | /** |
||
| 1587 | * Obtains the auth token. |
||
| 1588 | * |
||
| 1589 | * @param array $data The request data. |
||
| 1590 | * @return object|\WP_Error Returns the auth token on success. |
||
| 1591 | * Returns a \WP_Error on failure. |
||
| 1592 | */ |
||
| 1593 | public function get_token( $data ) { |
||
| 1734 | |||
| 1735 | /** |
||
| 1736 | * Increases the request timeout value to 30 seconds. |
||
| 1737 | * |
||
| 1738 | * @return int Returns 30. |
||
| 1739 | */ |
||
| 1740 | public function increase_timeout() { |
||
| 1743 | |||
| 1744 | /** |
||
| 1745 | * Builds a URL to the Jetpack connection auth page. |
||
| 1746 | * |
||
| 1747 | * @param WP_User $user (optional) defaults to the current logged in user. |
||
| 1748 | * @param String $redirect (optional) a redirect URL to use instead of the default. |
||
| 1749 | * @return string Connect URL. |
||
| 1750 | */ |
||
| 1751 | public function get_authorization_url( $user = null, $redirect = null ) { |
||
| 1837 | |||
| 1838 | /** |
||
| 1839 | * Authorizes the user by obtaining and storing the user token. |
||
| 1840 | * |
||
| 1841 | * @param array $data The request data. |
||
| 1842 | * @return string|\WP_Error Returns a string on success. |
||
| 1843 | * Returns a \WP_Error on failure. |
||
| 1844 | */ |
||
| 1845 | public function authorize( $data = array() ) { |
||
| 1931 | |||
| 1932 | /** |
||
| 1933 | * Disconnects from the Jetpack servers. |
||
| 1934 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 1935 | */ |
||
| 1936 | public function disconnect_site() { |
||
| 1939 | |||
| 1940 | /** |
||
| 1941 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
| 1942 | * |
||
| 1943 | * @param string $text The string to hash. |
||
| 1944 | * @return string |
||
| 1945 | */ |
||
| 1946 | public function sha1_base64( $text ) { |
||
| 1949 | |||
| 1950 | /** |
||
| 1951 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 1952 | * |
||
| 1953 | * @param string $domain The domain to check. |
||
| 1954 | * |
||
| 1955 | * @return bool|WP_Error |
||
| 1956 | */ |
||
| 1957 | public function is_usable_domain( $domain ) { |
||
| 2044 | |||
| 2045 | /** |
||
| 2046 | * Gets the requested token. |
||
| 2047 | * |
||
| 2048 | * Tokens are one of two types: |
||
| 2049 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
| 2050 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
| 2051 | * are not associated with a user account. They represent the site's connection with |
||
| 2052 | * the Jetpack servers. |
||
| 2053 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
| 2054 | * |
||
| 2055 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
| 2056 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
| 2057 | * over the network; it's used only for signing things. |
||
| 2058 | * |
||
| 2059 | * Blog Tokens can be "Normal" or "Special". |
||
| 2060 | * * Normal: The result of a normal connection flow. They look like |
||
| 2061 | * "{$random_string_1}.{$random_string_2}" |
||
| 2062 | * That is, $token_key and $private are both random strings. |
||
| 2063 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
| 2064 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
| 2065 | * constant (rare). |
||
| 2066 | * * Special: A connection token for sites that have gone through an alternative |
||
| 2067 | * connection flow. They look like: |
||
| 2068 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
| 2069 | * That is, $private is a random string and $token_key has a special structure with |
||
| 2070 | * lots of semicolons. |
||
| 2071 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
| 2072 | * JETPACK_BLOG_TOKEN constant. |
||
| 2073 | * |
||
| 2074 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
| 2075 | * Special Blog Tokens always do. |
||
| 2076 | * |
||
| 2077 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
| 2078 | * order: |
||
| 2079 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 2080 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
| 2081 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 2082 | * |
||
| 2083 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 2084 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 2085 | * @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. |
||
| 2086 | * |
||
| 2087 | * @return object|false |
||
| 2088 | */ |
||
| 2089 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
| 2175 | |||
| 2176 | /** |
||
| 2177 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
||
| 2178 | * since it is passed by reference to various methods. |
||
| 2179 | * Capture it here so we can verify the signature later. |
||
| 2180 | * |
||
| 2181 | * @param array $methods an array of available XMLRPC methods. |
||
| 2182 | * @return array the same array, since this method doesn't add or remove anything. |
||
| 2183 | */ |
||
| 2184 | public function xmlrpc_methods( $methods ) { |
||
| 2188 | |||
| 2189 | /** |
||
| 2190 | * Resets the raw post data parameter for testing purposes. |
||
| 2191 | */ |
||
| 2192 | public function reset_raw_post_data() { |
||
| 2195 | |||
| 2196 | /** |
||
| 2197 | * Registering an additional method. |
||
| 2198 | * |
||
| 2199 | * @param array $methods an array of available XMLRPC methods. |
||
| 2200 | * @return array the amended array in case the method is added. |
||
| 2201 | */ |
||
| 2202 | public function public_xmlrpc_methods( $methods ) { |
||
| 2208 | |||
| 2209 | /** |
||
| 2210 | * Handles a getOptions XMLRPC method call. |
||
| 2211 | * |
||
| 2212 | * @param array $args method call arguments. |
||
| 2213 | * @return an amended XMLRPC server options array. |
||
| 2214 | */ |
||
| 2215 | public function jetpack_get_options( $args ) { |
||
| 2256 | |||
| 2257 | /** |
||
| 2258 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
| 2259 | * |
||
| 2260 | * @param array $options standard Core options. |
||
| 2261 | * @return array amended options. |
||
| 2262 | */ |
||
| 2263 | public function xmlrpc_options( $options ) { |
||
| 2281 | |||
| 2282 | /** |
||
| 2283 | * Resets the saved authentication state in between testing requests. |
||
| 2284 | */ |
||
| 2285 | public function reset_saved_auth_state() { |
||
| 2288 | |||
| 2289 | /** |
||
| 2290 | * Sign a user role with the master access token. |
||
| 2291 | * If not specified, will default to the current user. |
||
| 2292 | * |
||
| 2293 | * @access public |
||
| 2294 | * |
||
| 2295 | * @param string $role User role. |
||
| 2296 | * @param int $user_id ID of the user. |
||
| 2297 | * @return string Signed user role. |
||
| 2298 | */ |
||
| 2299 | public function sign_role( $role, $user_id = null ) { |
||
| 2315 | |||
| 2316 | /** |
||
| 2317 | * Set the plugin instance. |
||
| 2318 | * |
||
| 2319 | * @param Plugin $plugin_instance The plugin instance. |
||
| 2320 | * |
||
| 2321 | * @return $this |
||
| 2322 | */ |
||
| 2323 | public function set_plugin_instance( Plugin $plugin_instance ) { |
||
| 2328 | |||
| 2329 | /** |
||
| 2330 | * Retrieve the plugin management object. |
||
| 2331 | * |
||
| 2332 | * @return Plugin |
||
| 2333 | */ |
||
| 2334 | public function get_plugin() { |
||
| 2337 | |||
| 2338 | /** |
||
| 2339 | * Get all connected plugins information, excluding those disconnected by user. |
||
| 2340 | * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
||
| 2341 | * Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
||
| 2342 | * so please make sure not to run the method too early in the code. |
||
| 2343 | * |
||
| 2344 | * @return array|WP_Error |
||
| 2345 | */ |
||
| 2346 | public function get_connected_plugins() { |
||
| 2355 | |||
| 2356 | /** |
||
| 2357 | * Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
||
| 2358 | * Note: this method does not remove any access tokens. |
||
| 2359 | * |
||
| 2360 | * @return bool |
||
| 2361 | */ |
||
| 2362 | public function disable_plugin() { |
||
| 2369 | |||
| 2370 | /** |
||
| 2371 | * Force plugin reconnect after user-initiated disconnect. |
||
| 2372 | * After its called, the plugin will be allowed to use the connection again. |
||
| 2373 | * Note: this method does not initialize access tokens. |
||
| 2374 | * |
||
| 2375 | * @return bool |
||
| 2376 | */ |
||
| 2377 | public function enable_plugin() { |
||
| 2384 | |||
| 2385 | /** |
||
| 2386 | * Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
||
| 2387 | * If no plugin slug was passed into the constructor, always returns true. |
||
| 2388 | * |
||
| 2389 | * @return bool |
||
| 2390 | */ |
||
| 2391 | public function is_plugin_enabled() { |
||
| 2398 | |||
| 2399 | } |
||
| 2400 |
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.