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 |
||
| 24 | class Manager { |
||
| 25 | /** |
||
| 26 | * A copy of the raw POST data for signature verification purposes. |
||
| 27 | * |
||
| 28 | * @var String |
||
| 29 | */ |
||
| 30 | protected $raw_post_data; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Verification data needs to be stored to properly verify everything. |
||
| 34 | * |
||
| 35 | * @var Object |
||
| 36 | */ |
||
| 37 | private $xmlrpc_verification = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Plugin management object. |
||
| 41 | * |
||
| 42 | * @var Plugin |
||
| 43 | */ |
||
| 44 | private $plugin = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Holds extra parameters that will be sent along in the register request body. |
||
| 48 | * |
||
| 49 | * Use Manager::add_register_request_param to add values to this array. |
||
| 50 | * |
||
| 51 | * @since 9.7.0 |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private static $extra_register_params = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Initialize the object. |
||
| 58 | * Make sure to call the "Configure" first. |
||
| 59 | * |
||
| 60 | * @param string $plugin_slug Slug of the plugin using the connection (optional, but encouraged). |
||
|
|
|||
| 61 | * |
||
| 62 | * @see \Automattic\Jetpack\Config |
||
| 63 | */ |
||
| 64 | public function __construct( $plugin_slug = null ) { |
||
| 65 | if ( $plugin_slug && is_string( $plugin_slug ) ) { |
||
| 66 | $this->set_plugin_instance( new Plugin( $plugin_slug ) ); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Initializes required listeners. This is done separately from the constructors |
||
| 72 | * because some objects sometimes need to instantiate separate objects of this class. |
||
| 73 | * |
||
| 74 | * @todo Implement a proper nonce verification. |
||
| 75 | */ |
||
| 76 | public static function configure() { |
||
| 77 | $manager = new self(); |
||
| 78 | |||
| 79 | add_filter( |
||
| 80 | 'jetpack_constant_default_value', |
||
| 81 | __NAMESPACE__ . '\Utils::jetpack_api_constant_filter', |
||
| 82 | 10, |
||
| 83 | 2 |
||
| 84 | ); |
||
| 85 | |||
| 86 | $manager->setup_xmlrpc_handlers( |
||
| 87 | $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 88 | $manager->has_connected_owner(), |
||
| 89 | $manager->verify_xml_rpc_signature() |
||
| 90 | ); |
||
| 91 | |||
| 92 | $manager->error_handler = Error_Handler::get_instance(); |
||
| 93 | |||
| 94 | if ( $manager->is_connected() ) { |
||
| 95 | add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) ); |
||
| 96 | } |
||
| 97 | |||
| 98 | add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
||
| 99 | |||
| 100 | ( new Nonce_Handler() )->init_schedule(); |
||
| 101 | |||
| 102 | add_action( 'plugins_loaded', __NAMESPACE__ . '\Plugin_Storage::configure', 100 ); |
||
| 103 | |||
| 104 | add_filter( 'map_meta_cap', array( $manager, 'jetpack_connection_custom_caps' ), 1, 4 ); |
||
| 105 | |||
| 106 | Heartbeat::init(); |
||
| 107 | add_filter( 'jetpack_heartbeat_stats_array', array( $manager, 'add_stats_to_heartbeat' ) ); |
||
| 108 | |||
| 109 | Webhooks::init( $manager ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Sets up the XMLRPC request handlers. |
||
| 114 | * |
||
| 115 | * @since 9.6.0 Deprecate $is_active param. |
||
| 116 | * |
||
| 117 | * @param array $request_params incoming request parameters. |
||
| 118 | * @param bool $has_connected_owner Whether the site has a connected owner. |
||
| 119 | * @param bool $is_signed whether the signature check has been successful. |
||
| 120 | * @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
||
| 121 | */ |
||
| 122 | public function setup_xmlrpc_handlers( |
||
| 123 | $request_params, |
||
| 124 | $has_connected_owner, |
||
| 125 | $is_signed, |
||
| 126 | \Jetpack_XMLRPC_Server $xmlrpc_server = null |
||
| 127 | ) { |
||
| 128 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ), 1000, 2 ); |
||
| 129 | |||
| 130 | if ( |
||
| 131 | ! isset( $request_params['for'] ) |
||
| 132 | || 'jetpack' !== $request_params['for'] |
||
| 133 | ) { |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms. |
||
| 138 | if ( |
||
| 139 | isset( $request_params['jetpack'] ) |
||
| 140 | && 'comms' === $request_params['jetpack'] |
||
| 141 | ) { |
||
| 142 | if ( ! Constants::is_defined( 'XMLRPC_REQUEST' ) ) { |
||
| 143 | // Use the real constant here for WordPress' sake. |
||
| 144 | define( 'XMLRPC_REQUEST', true ); |
||
| 145 | } |
||
| 146 | |||
| 147 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
| 148 | |||
| 149 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
| 150 | } |
||
| 151 | |||
| 152 | if ( ! Constants::get_constant( 'XMLRPC_REQUEST' ) ) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | // Display errors can cause the XML to be not well formed. |
||
| 156 | @ini_set( 'display_errors', false ); // phpcs:ignore |
||
| 157 | |||
| 158 | if ( $xmlrpc_server ) { |
||
| 159 | $this->xmlrpc_server = $xmlrpc_server; |
||
| 160 | } else { |
||
| 161 | $this->xmlrpc_server = new \Jetpack_XMLRPC_Server(); |
||
| 162 | } |
||
| 163 | |||
| 164 | $this->require_jetpack_authentication(); |
||
| 165 | |||
| 166 | if ( $is_signed ) { |
||
| 167 | // If the site is connected either at a site or user level and the request is signed, expose the methods. |
||
| 168 | // The callback is responsible to determine whether the request is signed with blog or user token and act accordingly. |
||
| 169 | // The actual API methods. |
||
| 170 | $callback = array( $this->xmlrpc_server, 'xmlrpc_methods' ); |
||
| 171 | |||
| 172 | // Hack to preserve $HTTP_RAW_POST_DATA. |
||
| 173 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 174 | |||
| 175 | } elseif ( $has_connected_owner && ! $is_signed ) { |
||
| 176 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
| 177 | // active Jetpack connection, so that additional users can link their account. |
||
| 178 | $callback = array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ); |
||
| 179 | |||
| 180 | } else { |
||
| 181 | // Any other unsigned request should expose the bootstrap methods. |
||
| 182 | $callback = array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ); |
||
| 183 | new XMLRPC_Connector( $this ); |
||
| 184 | } |
||
| 185 | |||
| 186 | add_filter( 'xmlrpc_methods', $callback ); |
||
| 187 | |||
| 188 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
| 189 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
| 190 | return true; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Initializes the REST API connector on the init hook. |
||
| 195 | */ |
||
| 196 | public function initialize_rest_api_registration_connector() { |
||
| 197 | new REST_Connector( $this ); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
| 202 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
| 203 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
| 204 | * which is accessible via a different URI. Most of the below is copied directly |
||
| 205 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
| 206 | * |
||
| 207 | * @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
||
| 208 | */ |
||
| 209 | public function alternate_xmlrpc() { |
||
| 210 | // Some browser-embedded clients send cookies. We don't want them. |
||
| 211 | $_COOKIE = array(); |
||
| 212 | |||
| 213 | include_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
| 214 | include_once ABSPATH . WPINC . '/class-IXR.php'; |
||
| 215 | include_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Filters the class used for handling XML-RPC requests. |
||
| 219 | * |
||
| 220 | * @since 3.1.0 |
||
| 221 | * |
||
| 222 | * @param string $class The name of the XML-RPC server class. |
||
| 223 | */ |
||
| 224 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
| 225 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class(); |
||
| 226 | |||
| 227 | // Fire off the request. |
||
| 228 | nocache_headers(); |
||
| 229 | $wp_xmlrpc_server->serve_request(); |
||
| 230 | |||
| 231 | exit; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
| 236 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
| 237 | * ensure that Core and other plugins' methods are not exposed. |
||
| 238 | * |
||
| 239 | * @param array $methods a list of registered WordPress XMLRPC methods. |
||
| 240 | * @return array filtered $methods |
||
| 241 | */ |
||
| 242 | public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
| 243 | $jetpack_methods = array(); |
||
| 244 | |||
| 245 | foreach ( $methods as $method => $callback ) { |
||
| 246 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
| 247 | $jetpack_methods[ $method ] = $callback; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | return $jetpack_methods; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Removes all other authentication methods not to allow other |
||
| 256 | * methods to validate unauthenticated requests. |
||
| 257 | */ |
||
| 258 | public function require_jetpack_authentication() { |
||
| 259 | // Don't let anyone authenticate. |
||
| 260 | $_COOKIE = array(); |
||
| 261 | remove_all_filters( 'authenticate' ); |
||
| 262 | remove_all_actions( 'wp_login_failed' ); |
||
| 263 | |||
| 264 | if ( $this->is_connected() ) { |
||
| 265 | // Allow Jetpack authentication. |
||
| 266 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
| 272 | * |
||
| 273 | * @param WP_User|Mixed $user user object if authenticated. |
||
| 274 | * @param String $username username. |
||
| 275 | * @param String $password password string. |
||
| 276 | * @return WP_User|Mixed authenticated user or error. |
||
| 277 | */ |
||
| 278 | public function authenticate_jetpack( $user, $username, $password ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 279 | if ( is_a( $user, '\\WP_User' ) ) { |
||
| 280 | return $user; |
||
| 281 | } |
||
| 282 | |||
| 283 | $token_details = $this->verify_xml_rpc_signature(); |
||
| 284 | |||
| 285 | if ( ! $token_details ) { |
||
| 286 | return $user; |
||
| 287 | } |
||
| 288 | |||
| 289 | if ( 'user' !== $token_details['type'] ) { |
||
| 290 | return $user; |
||
| 291 | } |
||
| 292 | |||
| 293 | if ( ! $token_details['user_id'] ) { |
||
| 294 | return $user; |
||
| 295 | } |
||
| 296 | |||
| 297 | nocache_headers(); |
||
| 298 | |||
| 299 | return new \WP_User( $token_details['user_id'] ); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Verifies the signature of the current request. |
||
| 304 | * |
||
| 305 | * @return false|array |
||
| 306 | */ |
||
| 307 | public function verify_xml_rpc_signature() { |
||
| 308 | if ( is_null( $this->xmlrpc_verification ) ) { |
||
| 309 | $this->xmlrpc_verification = $this->internal_verify_xml_rpc_signature(); |
||
| 310 | |||
| 311 | if ( is_wp_error( $this->xmlrpc_verification ) ) { |
||
| 312 | /** |
||
| 313 | * Action for logging XMLRPC signature verification errors. This data is sensitive. |
||
| 314 | * |
||
| 315 | * @since 7.5.0 |
||
| 316 | * |
||
| 317 | * @param WP_Error $signature_verification_error The verification error |
||
| 318 | */ |
||
| 319 | do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
||
| 320 | |||
| 321 | Error_Handler::get_instance()->report_error( $this->xmlrpc_verification ); |
||
| 322 | |||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Verifies the signature of the current request. |
||
| 331 | * |
||
| 332 | * This function has side effects and should not be used. Instead, |
||
| 333 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
| 334 | * |
||
| 335 | * @internal |
||
| 336 | * @todo Refactor to use proper nonce verification. |
||
| 337 | */ |
||
| 338 | private function internal_verify_xml_rpc_signature() { |
||
| 339 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
| 340 | // It's not for us. |
||
| 341 | if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | $signature_details = array( |
||
| 346 | 'token' => isset( $_GET['token'] ) ? wp_unslash( $_GET['token'] ) : '', |
||
| 347 | 'timestamp' => isset( $_GET['timestamp'] ) ? wp_unslash( $_GET['timestamp'] ) : '', |
||
| 348 | 'nonce' => isset( $_GET['nonce'] ) ? wp_unslash( $_GET['nonce'] ) : '', |
||
| 349 | 'body_hash' => isset( $_GET['body-hash'] ) ? wp_unslash( $_GET['body-hash'] ) : '', |
||
| 350 | 'method' => wp_unslash( $_SERVER['REQUEST_METHOD'] ), |
||
| 351 | 'url' => wp_unslash( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ), // Temp - will get real signature URL later. |
||
| 352 | 'signature' => isset( $_GET['signature'] ) ? wp_unslash( $_GET['signature'] ) : '', |
||
| 353 | ); |
||
| 354 | |||
| 355 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
||
| 356 | @list( $token_key, $version, $user_id ) = explode( ':', wp_unslash( $_GET['token'] ) ); |
||
| 357 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
||
| 358 | |||
| 359 | $jetpack_api_version = Constants::get_constant( 'JETPACK__API_VERSION' ); |
||
| 360 | |||
| 361 | if ( |
||
| 362 | empty( $token_key ) |
||
| 363 | || |
||
| 364 | empty( $version ) || (string) $jetpack_api_version !== $version ) { |
||
| 365 | return new \WP_Error( 'malformed_token', 'Malformed token in request', compact( 'signature_details' ) ); |
||
| 366 | } |
||
| 367 | |||
| 368 | if ( '0' === $user_id ) { |
||
| 369 | $token_type = 'blog'; |
||
| 370 | $user_id = 0; |
||
| 371 | } else { |
||
| 372 | $token_type = 'user'; |
||
| 373 | if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { |
||
| 374 | return new \WP_Error( |
||
| 375 | 'malformed_user_id', |
||
| 376 | 'Malformed user_id in request', |
||
| 377 | compact( 'signature_details' ) |
||
| 378 | ); |
||
| 379 | } |
||
| 380 | $user_id = (int) $user_id; |
||
| 381 | |||
| 382 | $user = new \WP_User( $user_id ); |
||
| 383 | if ( ! $user || ! $user->exists() ) { |
||
| 384 | return new \WP_Error( |
||
| 385 | 'unknown_user', |
||
| 386 | sprintf( 'User %d does not exist', $user_id ), |
||
| 387 | compact( 'signature_details' ) |
||
| 388 | ); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | $token = $this->get_tokens()->get_access_token( $user_id, $token_key, false ); |
||
| 393 | if ( is_wp_error( $token ) ) { |
||
| 394 | $token->add_data( compact( 'signature_details' ) ); |
||
| 395 | return $token; |
||
| 396 | } elseif ( ! $token ) { |
||
| 397 | return new \WP_Error( |
||
| 398 | 'unknown_token', |
||
| 399 | sprintf( 'Token %s:%s:%d does not exist', $token_key, $version, $user_id ), |
||
| 400 | compact( 'signature_details' ) |
||
| 401 | ); |
||
| 402 | } |
||
| 403 | |||
| 404 | $jetpack_signature = new \Jetpack_Signature( $token->secret, (int) \Jetpack_Options::get_option( 'time_diff' ) ); |
||
| 405 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
||
| 406 | if ( isset( $_POST['_jetpack_is_multipart'] ) ) { |
||
| 407 | $post_data = $_POST; |
||
| 408 | $file_hashes = array(); |
||
| 409 | foreach ( $post_data as $post_data_key => $post_data_value ) { |
||
| 410 | if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { |
||
| 411 | continue; |
||
| 412 | } |
||
| 413 | $post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); |
||
| 414 | $file_hashes[ $post_data_key ] = $post_data_value; |
||
| 415 | } |
||
| 416 | |||
| 417 | foreach ( $file_hashes as $post_data_key => $post_data_value ) { |
||
| 418 | unset( $post_data[ "_jetpack_file_hmac_{$post_data_key}" ] ); |
||
| 419 | $post_data[ $post_data_key ] = $post_data_value; |
||
| 420 | } |
||
| 421 | |||
| 422 | ksort( $post_data ); |
||
| 423 | |||
| 424 | $body = http_build_query( stripslashes_deep( $post_data ) ); |
||
| 425 | } elseif ( is_null( $this->raw_post_data ) ) { |
||
| 426 | $body = file_get_contents( 'php://input' ); |
||
| 427 | } else { |
||
| 428 | $body = null; |
||
| 429 | } |
||
| 430 | // phpcs:enable |
||
| 431 | |||
| 432 | $signature = $jetpack_signature->sign_current_request( |
||
| 433 | array( 'body' => is_null( $body ) ? $this->raw_post_data : $body ) |
||
| 434 | ); |
||
| 435 | |||
| 436 | $signature_details['url'] = $jetpack_signature->current_request_url; |
||
| 437 | |||
| 438 | if ( ! $signature ) { |
||
| 439 | return new \WP_Error( |
||
| 440 | 'could_not_sign', |
||
| 441 | 'Unknown signature error', |
||
| 442 | compact( 'signature_details' ) |
||
| 443 | ); |
||
| 444 | } elseif ( is_wp_error( $signature ) ) { |
||
| 445 | return $signature; |
||
| 446 | } |
||
| 447 | |||
| 448 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
| 449 | $timestamp = (int) $_GET['timestamp']; |
||
| 450 | $nonce = stripslashes( (string) $_GET['nonce'] ); |
||
| 451 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
||
| 452 | |||
| 453 | // Use up the nonce regardless of whether the signature matches. |
||
| 454 | if ( ! ( new Nonce_Handler() )->add( $timestamp, $nonce ) ) { |
||
| 455 | return new \WP_Error( |
||
| 456 | 'invalid_nonce', |
||
| 457 | 'Could not add nonce', |
||
| 458 | compact( 'signature_details' ) |
||
| 459 | ); |
||
| 460 | } |
||
| 461 | |||
| 462 | // Be careful about what you do with this debugging data. |
||
| 463 | // If a malicious requester has access to the expected signature, |
||
| 464 | // bad things might be possible. |
||
| 465 | $signature_details['expected'] = $signature; |
||
| 466 | |||
| 467 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 468 | if ( ! hash_equals( $signature, $_GET['signature'] ) ) { |
||
| 469 | return new \WP_Error( |
||
| 470 | 'signature_mismatch', |
||
| 471 | 'Signature mismatch', |
||
| 472 | compact( 'signature_details' ) |
||
| 473 | ); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Action for additional token checking. |
||
| 478 | * |
||
| 479 | * @since 7.7.0 |
||
| 480 | * |
||
| 481 | * @param array $post_data request data. |
||
| 482 | * @param array $token_data token data. |
||
| 483 | */ |
||
| 484 | return apply_filters( |
||
| 485 | 'jetpack_signature_check_token', |
||
| 486 | array( |
||
| 487 | 'type' => $token_type, |
||
| 488 | 'token_key' => $token_key, |
||
| 489 | 'user_id' => $token->external_user_id, |
||
| 490 | ), |
||
| 491 | $token, |
||
| 492 | $this->raw_post_data |
||
| 493 | ); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Returns true if the current site is connected to WordPress.com and has the minimum requirements to enable Jetpack UI. |
||
| 498 | * |
||
| 499 | * This method is deprecated since Jetpack 9.6.0. Please use has_connected_owner instead. |
||
| 500 | * |
||
| 501 | * Since this method has a wide spread use, we decided not to throw any deprecation warnings for now. |
||
| 502 | * |
||
| 503 | * @deprecated 9.6.0 |
||
| 504 | * @see Manager::has_connected_owner |
||
| 505 | * @return Boolean is the site connected? |
||
| 506 | */ |
||
| 507 | public function is_active() { |
||
| 508 | return (bool) $this->get_tokens()->get_access_token( true ); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Obtains an instance of the Tokens class. |
||
| 513 | * |
||
| 514 | * @return Tokens the Tokens object |
||
| 515 | */ |
||
| 516 | public function get_tokens() { |
||
| 517 | return new Tokens(); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
||
| 522 | * |
||
| 523 | * @access public |
||
| 524 | * @deprecated 9.2.0 Use is_connected instead |
||
| 525 | * @see Manager::is_connected |
||
| 526 | * |
||
| 527 | * @return bool |
||
| 528 | */ |
||
| 529 | public function is_registered() { |
||
| 530 | _deprecated_function( __METHOD__, 'jetpack-9.2' ); |
||
| 531 | return $this->is_connected(); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Returns true if the site has both a token and a blog id, which indicates a site has been connected. |
||
| 536 | * |
||
| 537 | * @access public |
||
| 538 | * @since 9.2.0 |
||
| 539 | * |
||
| 540 | * @return bool |
||
| 541 | */ |
||
| 542 | public function is_connected() { |
||
| 543 | $has_blog_id = (bool) \Jetpack_Options::get_option( 'id' ); |
||
| 544 | $has_blog_token = (bool) $this->get_tokens()->get_access_token(); |
||
| 545 | return $has_blog_id && $has_blog_token; |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Returns true if the site has at least one connected administrator. |
||
| 550 | * |
||
| 551 | * @access public |
||
| 552 | * @since 9.2.0 |
||
| 553 | * |
||
| 554 | * @return bool |
||
| 555 | */ |
||
| 556 | public function has_connected_admin() { |
||
| 557 | return (bool) count( $this->get_connected_users( 'manage_options' ) ); |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns true if the site has any connected user. |
||
| 562 | * |
||
| 563 | * @access public |
||
| 564 | * @since 9.2.0 |
||
| 565 | * |
||
| 566 | * @return bool |
||
| 567 | */ |
||
| 568 | public function has_connected_user() { |
||
| 569 | return (bool) count( $this->get_connected_users() ); |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Returns an array of user_id's that have user tokens for communicating with wpcom. |
||
| 574 | * Able to select by specific capability. |
||
| 575 | * |
||
| 576 | * @param string $capability The capability of the user. |
||
| 577 | * @return array Array of WP_User objects if found. |
||
| 578 | */ |
||
| 579 | public function get_connected_users( $capability = 'any' ) { |
||
| 580 | return $this->get_tokens()->get_connected_users( $capability ); |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Returns true if the site has a connected Blog owner (master_user). |
||
| 585 | * |
||
| 586 | * @access public |
||
| 587 | * @since 9.2.0 |
||
| 588 | * |
||
| 589 | * @return bool |
||
| 590 | */ |
||
| 591 | public function has_connected_owner() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Returns true if the site is connected only at a site level. |
||
| 597 | * |
||
| 598 | * Note that we are explicitly checking for the existence of the master_user option in order to account for cases where we don't have any user tokens (user-level connection) but the master_user option is set, which could be the result of a problematic user connection. |
||
| 599 | * |
||
| 600 | * @access public |
||
| 601 | * @since 9.6.0 |
||
| 602 | * @deprecated 9.8.0 |
||
| 603 | * |
||
| 604 | * @return bool |
||
| 605 | */ |
||
| 606 | public function is_userless() { |
||
| 607 | _deprecated_function( __METHOD__, 'jetpack-9.8.0', 'Automattic\\Jetpack\\Connection\\Manager::is_site_connection' ); |
||
| 608 | return $this->is_site_connection(); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Returns true if the site is connected only at a site level. |
||
| 613 | * |
||
| 614 | * Note that we are explicitly checking for the existence of the master_user option in order to account for cases where we don't have any user tokens (user-level connection) but the master_user option is set, which could be the result of a problematic user connection. |
||
| 615 | * |
||
| 616 | * @access public |
||
| 617 | * @since 9.8.0 |
||
| 618 | * |
||
| 619 | * @return bool |
||
| 620 | */ |
||
| 621 | public function is_site_connection() { |
||
| 622 | return $this->is_connected() && ! $this->has_connected_user() && ! \Jetpack_Options::get_option( 'master_user' ); |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Checks to see if the connection owner of the site is missing. |
||
| 627 | * |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | public function is_missing_connection_owner() { |
||
| 631 | $connection_owner = $this->get_connection_owner_id(); |
||
| 632 | if ( ! get_user_by( 'id', $connection_owner ) ) { |
||
| 633 | return true; |
||
| 634 | } |
||
| 635 | |||
| 636 | return false; |
||
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Returns true if the user with the specified identifier is connected to |
||
| 641 | * WordPress.com. |
||
| 642 | * |
||
| 643 | * @param int $user_id the user identifier. Default is the current user. |
||
| 644 | * @return bool Boolean is the user connected? |
||
| 645 | */ |
||
| 646 | public function is_user_connected( $user_id = false ) { |
||
| 647 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
| 648 | if ( ! $user_id ) { |
||
| 649 | return false; |
||
| 650 | } |
||
| 651 | |||
| 652 | return (bool) $this->get_tokens()->get_access_token( $user_id ); |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Returns the local user ID of the connection owner. |
||
| 657 | * |
||
| 658 | * @return bool|int Returns the ID of the connection owner or False if no connection owner found. |
||
| 659 | */ |
||
| 660 | public function get_connection_owner_id() { |
||
| 661 | $owner = $this->get_connection_owner(); |
||
| 662 | return $owner instanceof \WP_User ? $owner->ID : false; |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Get the wpcom user data of the current|specified connected user. |
||
| 667 | * |
||
| 668 | * @todo Refactor to properly load the XMLRPC client independently. |
||
| 669 | * |
||
| 670 | * @param Integer $user_id the user identifier. |
||
| 671 | * @return Object the user object. |
||
| 672 | */ |
||
| 673 | public function get_connected_user_data( $user_id = null ) { |
||
| 674 | if ( ! $user_id ) { |
||
| 675 | $user_id = get_current_user_id(); |
||
| 676 | } |
||
| 677 | |||
| 678 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 679 | $cached_user_data = get_transient( $transient_key ); |
||
| 680 | |||
| 681 | if ( $cached_user_data ) { |
||
| 682 | return $cached_user_data; |
||
| 683 | } |
||
| 684 | |||
| 685 | $xml = new Jetpack_IXR_Client( |
||
| 686 | array( |
||
| 687 | 'user_id' => $user_id, |
||
| 688 | ) |
||
| 689 | ); |
||
| 690 | $xml->query( 'wpcom.getUser' ); |
||
| 691 | if ( ! $xml->isError() ) { |
||
| 692 | $user_data = $xml->getResponse(); |
||
| 693 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
| 694 | return $user_data; |
||
| 695 | } |
||
| 696 | |||
| 697 | return false; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns a user object of the connection owner. |
||
| 702 | * |
||
| 703 | * @return WP_User|false False if no connection owner found. |
||
| 704 | */ |
||
| 705 | public function get_connection_owner() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Returns true if the provided user is the Jetpack connection owner. |
||
| 727 | * If user ID is not specified, the current user will be used. |
||
| 728 | * |
||
| 729 | * @param Integer|Boolean $user_id the user identifier. False for current user. |
||
| 730 | * @return Boolean True the user the connection owner, false otherwise. |
||
| 731 | */ |
||
| 732 | public function is_connection_owner( $user_id = false ) { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Connects the user with a specified ID to a WordPress.com user using the |
||
| 742 | * remote login flow. |
||
| 743 | * |
||
| 744 | * @access public |
||
| 745 | * |
||
| 746 | * @param Integer $user_id (optional) the user identifier, defaults to current user. |
||
| 747 | * @param String $redirect_url the URL to redirect the user to for processing, defaults to |
||
| 748 | * admin_url(). |
||
| 749 | * @return WP_Error only in case of a failed user lookup. |
||
| 750 | */ |
||
| 751 | public function connect_user( $user_id = null, $redirect_url = null ) { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Unlinks the current user from the linked WordPress.com user. |
||
| 774 | * |
||
| 775 | * @access public |
||
| 776 | * @static |
||
| 777 | * |
||
| 778 | * @todo Refactor to properly load the XMLRPC client independently. |
||
| 779 | * |
||
| 780 | * @param Integer $user_id the user identifier. |
||
| 781 | * @param bool $can_overwrite_primary_user Allow for the primary user to be disconnected. |
||
| 782 | * @return Boolean Whether the disconnection of the user was successful. |
||
| 783 | */ |
||
| 784 | public function disconnect_user( $user_id = null, $can_overwrite_primary_user = false ) { |
||
| 785 | $user_id = empty( $user_id ) ? get_current_user_id() : (int) $user_id; |
||
| 786 | |||
| 787 | // Attempt to disconnect the user from WordPress.com. |
||
| 788 | $is_disconnected_from_wpcom = $this->unlink_user_from_wpcom( $user_id ); |
||
| 789 | if ( ! $is_disconnected_from_wpcom ) { |
||
| 790 | return false; |
||
| 791 | } |
||
| 792 | |||
| 793 | // Disconnect the user locally. |
||
| 794 | $is_disconnected_locally = $this->get_tokens()->disconnect_user( $user_id, $can_overwrite_primary_user ); |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Request to wpcom for a user to be unlinked from their WordPress.com account |
||
| 815 | * |
||
| 816 | * @access public |
||
| 817 | * |
||
| 818 | * @param Integer $user_id the user identifier. |
||
| 819 | * |
||
| 820 | * @return Boolean Whether the disconnection of the user was successful. |
||
| 821 | */ |
||
| 822 | View Code Duplication | public function unlink_user_from_wpcom( $user_id ) { |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Update the connection owner. |
||
| 836 | * |
||
| 837 | * @since 9.9.0 |
||
| 838 | * |
||
| 839 | * @param Integer $new_owner_id The ID of the user to become the connection owner. |
||
| 840 | * |
||
| 841 | * @return true|WP_Error True if owner successfully changed, WP_Error otherwise. |
||
| 842 | */ |
||
| 843 | public function update_connection_owner( $new_owner_id ) { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Request to WPCOM to update the connection owner. |
||
| 892 | * |
||
| 893 | * @since 9.9.0 |
||
| 894 | * |
||
| 895 | * @param Integer $new_owner_id The ID of the user to become the connection owner. |
||
| 896 | * |
||
| 897 | * @return Boolean Whether the ownership transfer was successful. |
||
| 898 | */ |
||
| 899 | View Code Duplication | public function update_connection_owner_wpcom( $new_owner_id ) { |
|
| 918 | |||
| 919 | /** |
||
| 920 | * Returns the requested Jetpack API URL. |
||
| 921 | * |
||
| 922 | * @param String $relative_url the relative API path. |
||
| 923 | * @return String API URL. |
||
| 924 | */ |
||
| 925 | public function api_url( $relative_url ) { |
||
| 962 | |||
| 963 | /** |
||
| 964 | * Returns the Jetpack XMLRPC WordPress.com API endpoint URL. |
||
| 965 | * |
||
| 966 | * @return String XMLRPC API URL. |
||
| 967 | */ |
||
| 968 | public function xmlrpc_api_url() { |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
| 979 | * remain public because the call to action comes from the current site, not from |
||
| 980 | * WordPress.com. |
||
| 981 | * |
||
| 982 | * @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
||
| 983 | * @return true|WP_Error The error object. |
||
| 984 | */ |
||
| 985 | public function register( $api_endpoint = 'register' ) { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Attempts Jetpack registration. |
||
| 1151 | * |
||
| 1152 | * @param bool $tos_agree Whether the user agreed to TOS. |
||
| 1153 | * |
||
| 1154 | * @return bool|WP_Error |
||
| 1155 | */ |
||
| 1156 | public function try_registration( $tos_agree = true ) { |
||
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Adds a parameter to the register request body |
||
| 1198 | * |
||
| 1199 | * @since 9.7.0 |
||
| 1200 | * |
||
| 1201 | * @param string $name The name of the parameter to be added. |
||
| 1202 | * @param string $value The value of the parameter to be added. |
||
| 1203 | * |
||
| 1204 | * @throws \InvalidArgumentException If supplied arguments are not strings. |
||
| 1205 | * @return void |
||
| 1206 | */ |
||
| 1207 | public function add_register_request_param( $name, $value ) { |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Takes the response from the Jetpack register new site endpoint and |
||
| 1216 | * verifies it worked properly. |
||
| 1217 | * |
||
| 1218 | * @since 2.6 |
||
| 1219 | * |
||
| 1220 | * @param Mixed $response the response object, or the error object. |
||
| 1221 | * @return string|WP_Error A JSON object on success or WP_Error on failures |
||
| 1222 | **/ |
||
| 1223 | protected function validate_remote_register_response( $response ) { |
||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Adds a used nonce to a list of known nonces. |
||
| 1295 | * |
||
| 1296 | * @param int $timestamp the current request timestamp. |
||
| 1297 | * @param string $nonce the nonce value. |
||
| 1298 | * @return bool whether the nonce is unique or not. |
||
| 1299 | * |
||
| 1300 | * @deprecated since 9.5.0 |
||
| 1301 | * @see Nonce_Handler::add() |
||
| 1302 | */ |
||
| 1303 | public function add_nonce( $timestamp, $nonce ) { |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Cleans nonces that were saved when calling ::add_nonce. |
||
| 1310 | * |
||
| 1311 | * @todo Properly prepare the query before executing it. |
||
| 1312 | * |
||
| 1313 | * @param bool $all whether to clean even non-expired nonces. |
||
| 1314 | * |
||
| 1315 | * @deprecated since 9.5.0 |
||
| 1316 | * @see Nonce_Handler::clean_all() |
||
| 1317 | */ |
||
| 1318 | public function clean_nonces( $all = false ) { |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * Sets the Connection custom capabilities. |
||
| 1325 | * |
||
| 1326 | * @param string[] $caps Array of the user's capabilities. |
||
| 1327 | * @param string $cap Capability name. |
||
| 1328 | * @param int $user_id The user ID. |
||
| 1329 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
| 1330 | */ |
||
| 1331 | public function jetpack_connection_custom_caps( $caps, $cap, $user_id, $args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 1367 | * |
||
| 1368 | * Based on local php max_execution_time in php.ini |
||
| 1369 | * |
||
| 1370 | * @since 5.4 |
||
| 1371 | * @return int |
||
| 1372 | **/ |
||
| 1373 | public function get_max_execution_time() { |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * Sets a minimum request timeout, and returns the current timeout |
||
| 1385 | * |
||
| 1386 | * @since 5.4 |
||
| 1387 | * @param Integer $min_timeout the minimum timeout value. |
||
| 1388 | **/ |
||
| 1389 | View Code Duplication | public function set_min_time_limit( $min_timeout ) { |
|
| 1397 | |||
| 1398 | /** |
||
| 1399 | * Get our assumed site creation date. |
||
| 1400 | * Calculated based on the earlier date of either: |
||
| 1401 | * - Earliest admin user registration date. |
||
| 1402 | * - Earliest date of post of any post type. |
||
| 1403 | * |
||
| 1404 | * @since 7.2.0 |
||
| 1405 | * |
||
| 1406 | * @return string Assumed site creation date and time. |
||
| 1407 | */ |
||
| 1408 | public function get_assumed_site_creation_date() { |
||
| 1447 | |||
| 1448 | /** |
||
| 1449 | * Adds the activation source string as a parameter to passed arguments. |
||
| 1450 | * |
||
| 1451 | * @todo Refactor to use rawurlencode() instead of urlencode(). |
||
| 1452 | * |
||
| 1453 | * @param array $args arguments that need to have the source added. |
||
| 1454 | * @return array $amended arguments. |
||
| 1455 | */ |
||
| 1456 | View Code Duplication | public static function apply_activation_source_to_args( $args ) { |
|
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Generates two secret tokens and the end of life timestamp for them. |
||
| 1474 | * |
||
| 1475 | * @param String $action The action name. |
||
| 1476 | * @param Integer $user_id The user identifier. |
||
| 1477 | * @param Integer $exp Expiration time in seconds. |
||
| 1478 | */ |
||
| 1479 | public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 1485 | * |
||
| 1486 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->get() instead. |
||
| 1487 | * |
||
| 1488 | * @param String $action The action name. |
||
| 1489 | * @param Integer $user_id The user identifier. |
||
| 1490 | * @return string|array an array of secrets or an error string. |
||
| 1491 | */ |
||
| 1492 | public function get_secrets( $action, $user_id ) { |
||
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Deletes secret tokens in case they, for example, have expired. |
||
| 1499 | * |
||
| 1500 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->delete() instead. |
||
| 1501 | * |
||
| 1502 | * @param String $action The action name. |
||
| 1503 | * @param Integer $user_id The user identifier. |
||
| 1504 | */ |
||
| 1505 | public function delete_secrets( $action, $user_id ) { |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Deletes all connection tokens and transients from the local Jetpack site. |
||
| 1512 | * If the plugin object has been provided in the constructor, the function first checks |
||
| 1513 | * whether it's the only active connection. |
||
| 1514 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1515 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1516 | * |
||
| 1517 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1518 | * |
||
| 1519 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1520 | */ |
||
| 1521 | public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
||
| 1557 | |||
| 1558 | /** |
||
| 1559 | * Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
||
| 1560 | * If the plugin object has been provided in the constructor, the function first check |
||
| 1561 | * whether it's the only active connection. |
||
| 1562 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1563 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1564 | * |
||
| 1565 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1566 | * |
||
| 1567 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1568 | */ |
||
| 1569 | public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
||
| 1589 | |||
| 1590 | /** |
||
| 1591 | * Disconnect the plugin and remove the tokens. |
||
| 1592 | * This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
||
| 1593 | * This is a proxy method to simplify the Connection package API. |
||
| 1594 | * |
||
| 1595 | * @see Manager::disable_plugin() |
||
| 1596 | * @see Manager::disconnect_site_wpcom() |
||
| 1597 | * @see Manager::delete_all_connection_tokens() |
||
| 1598 | * |
||
| 1599 | * @return bool |
||
| 1600 | */ |
||
| 1601 | public function remove_connection() { |
||
| 1608 | |||
| 1609 | /** |
||
| 1610 | * Completely clearing up the connection, and initiating reconnect. |
||
| 1611 | * |
||
| 1612 | * @return true|WP_Error True if reconnected successfully, a `WP_Error` object otherwise. |
||
| 1613 | */ |
||
| 1614 | public function reconnect() { |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Validate the tokens, and refresh the invalid ones. |
||
| 1625 | * |
||
| 1626 | * @return string|bool|WP_Error True if connection restored or string indicating what's to be done next. A `WP_Error` object or false otherwise. |
||
| 1627 | */ |
||
| 1628 | public function restore() { |
||
| 1664 | |||
| 1665 | /** |
||
| 1666 | * Responds to a WordPress.com call to register the current site. |
||
| 1667 | * Should be changed to protected. |
||
| 1668 | * |
||
| 1669 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
| 1670 | */ |
||
| 1671 | public function handle_registration( array $registration_data ) { |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * Perform the API request to validate the blog and user tokens. |
||
| 1682 | * |
||
| 1683 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->validate_tokens() instead. |
||
| 1684 | * |
||
| 1685 | * @param int|null $user_id ID of the user we need to validate token for. Current user's ID by default. |
||
| 1686 | * |
||
| 1687 | * @return array|false|WP_Error The API response: `array( 'blog_token_is_healthy' => true|false, 'user_token_is_healthy' => true|false )`. |
||
| 1688 | */ |
||
| 1689 | public function validate_tokens( $user_id = null ) { |
||
| 1693 | |||
| 1694 | /** |
||
| 1695 | * Verify a Previously Generated Secret. |
||
| 1696 | * |
||
| 1697 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->verify() instead. |
||
| 1698 | * |
||
| 1699 | * @param string $action The type of secret to verify. |
||
| 1700 | * @param string $secret_1 The secret string to compare to what is stored. |
||
| 1701 | * @param int $user_id The user ID of the owner of the secret. |
||
| 1702 | * @return \WP_Error|string WP_Error on failure, secret_2 on success. |
||
| 1703 | */ |
||
| 1704 | public function verify_secrets( $action, $secret_1, $user_id ) { |
||
| 1708 | |||
| 1709 | /** |
||
| 1710 | * Responds to a WordPress.com call to authorize the current user. |
||
| 1711 | * Should be changed to protected. |
||
| 1712 | */ |
||
| 1713 | public function handle_authorization() { |
||
| 1716 | |||
| 1717 | /** |
||
| 1718 | * Obtains the auth token. |
||
| 1719 | * |
||
| 1720 | * @param array $data The request data. |
||
| 1721 | * @return object|\WP_Error Returns the auth token on success. |
||
| 1722 | * Returns a \WP_Error on failure. |
||
| 1723 | */ |
||
| 1724 | public function get_token( $data ) { |
||
| 1727 | |||
| 1728 | /** |
||
| 1729 | * Builds a URL to the Jetpack connection auth page. |
||
| 1730 | * |
||
| 1731 | * @param WP_User $user (optional) defaults to the current logged in user. |
||
| 1732 | * @param String $redirect (optional) a redirect URL to use instead of the default. |
||
| 1733 | * @return string Connect URL. |
||
| 1734 | */ |
||
| 1735 | public function get_authorization_url( $user = null, $redirect = null ) { |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Authorizes the user by obtaining and storing the user token. |
||
| 1825 | * |
||
| 1826 | * @param array $data The request data. |
||
| 1827 | * @return string|\WP_Error Returns a string on success. |
||
| 1828 | * Returns a \WP_Error on failure. |
||
| 1829 | */ |
||
| 1830 | public function authorize( $data = array() ) { |
||
| 1921 | |||
| 1922 | /** |
||
| 1923 | * Disconnects from the Jetpack servers. |
||
| 1924 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 1925 | */ |
||
| 1926 | public function disconnect_site() { |
||
| 1929 | |||
| 1930 | /** |
||
| 1931 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
| 1932 | * |
||
| 1933 | * @param string $text The string to hash. |
||
| 1934 | * @return string |
||
| 1935 | */ |
||
| 1936 | public function sha1_base64( $text ) { |
||
| 1939 | |||
| 1940 | /** |
||
| 1941 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 1942 | * |
||
| 1943 | * @param string $domain The domain to check. |
||
| 1944 | * |
||
| 1945 | * @return bool|WP_Error |
||
| 1946 | */ |
||
| 1947 | public function is_usable_domain( $domain ) { |
||
| 2034 | |||
| 2035 | /** |
||
| 2036 | * Gets the requested token. |
||
| 2037 | * |
||
| 2038 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->get_access_token() instead. |
||
| 2039 | * |
||
| 2040 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 2041 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 2042 | * @param bool|true $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found. |
||
| 2043 | * |
||
| 2044 | * @return object|false |
||
| 2045 | * |
||
| 2046 | * @see $this->get_tokens()->get_access_token() |
||
| 2047 | */ |
||
| 2048 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
| 2052 | |||
| 2053 | /** |
||
| 2054 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
||
| 2055 | * since it is passed by reference to various methods. |
||
| 2056 | * Capture it here so we can verify the signature later. |
||
| 2057 | * |
||
| 2058 | * @param array $methods an array of available XMLRPC methods. |
||
| 2059 | * @return array the same array, since this method doesn't add or remove anything. |
||
| 2060 | */ |
||
| 2061 | public function xmlrpc_methods( $methods ) { |
||
| 2065 | |||
| 2066 | /** |
||
| 2067 | * Resets the raw post data parameter for testing purposes. |
||
| 2068 | */ |
||
| 2069 | public function reset_raw_post_data() { |
||
| 2072 | |||
| 2073 | /** |
||
| 2074 | * Registering an additional method. |
||
| 2075 | * |
||
| 2076 | * @param array $methods an array of available XMLRPC methods. |
||
| 2077 | * @return array the amended array in case the method is added. |
||
| 2078 | */ |
||
| 2079 | public function public_xmlrpc_methods( $methods ) { |
||
| 2085 | |||
| 2086 | /** |
||
| 2087 | * Handles a getOptions XMLRPC method call. |
||
| 2088 | * |
||
| 2089 | * @param array $args method call arguments. |
||
| 2090 | * @return an amended XMLRPC server options array. |
||
| 2091 | */ |
||
| 2092 | public function jetpack_get_options( $args ) { |
||
| 2133 | |||
| 2134 | /** |
||
| 2135 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
| 2136 | * |
||
| 2137 | * @param array $options standard Core options. |
||
| 2138 | * @return array amended options. |
||
| 2139 | */ |
||
| 2140 | public function xmlrpc_options( $options ) { |
||
| 2158 | |||
| 2159 | /** |
||
| 2160 | * Resets the saved authentication state in between testing requests. |
||
| 2161 | */ |
||
| 2162 | public function reset_saved_auth_state() { |
||
| 2165 | |||
| 2166 | /** |
||
| 2167 | * Sign a user role with the master access token. |
||
| 2168 | * If not specified, will default to the current user. |
||
| 2169 | * |
||
| 2170 | * @access public |
||
| 2171 | * |
||
| 2172 | * @param string $role User role. |
||
| 2173 | * @param int $user_id ID of the user. |
||
| 2174 | * @return string Signed user role. |
||
| 2175 | */ |
||
| 2176 | public function sign_role( $role, $user_id = null ) { |
||
| 2179 | |||
| 2180 | /** |
||
| 2181 | * Set the plugin instance. |
||
| 2182 | * |
||
| 2183 | * @param Plugin $plugin_instance The plugin instance. |
||
| 2184 | * |
||
| 2185 | * @return $this |
||
| 2186 | */ |
||
| 2187 | public function set_plugin_instance( Plugin $plugin_instance ) { |
||
| 2192 | |||
| 2193 | /** |
||
| 2194 | * Retrieve the plugin management object. |
||
| 2195 | * |
||
| 2196 | * @return Plugin|null |
||
| 2197 | */ |
||
| 2198 | public function get_plugin() { |
||
| 2201 | |||
| 2202 | /** |
||
| 2203 | * Get all connected plugins information, excluding those disconnected by user. |
||
| 2204 | * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
||
| 2205 | * Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
||
| 2206 | * so please make sure not to run the method too early in the code. |
||
| 2207 | * |
||
| 2208 | * @return array|WP_Error |
||
| 2209 | */ |
||
| 2210 | public function get_connected_plugins() { |
||
| 2219 | |||
| 2220 | /** |
||
| 2221 | * Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
||
| 2222 | * Note: this method does not remove any access tokens. |
||
| 2223 | * |
||
| 2224 | * @return bool |
||
| 2225 | */ |
||
| 2226 | public function disable_plugin() { |
||
| 2233 | |||
| 2234 | /** |
||
| 2235 | * Force plugin reconnect after user-initiated disconnect. |
||
| 2236 | * After its called, the plugin will be allowed to use the connection again. |
||
| 2237 | * Note: this method does not initialize access tokens. |
||
| 2238 | * |
||
| 2239 | * @return bool |
||
| 2240 | */ |
||
| 2241 | public function enable_plugin() { |
||
| 2248 | |||
| 2249 | /** |
||
| 2250 | * Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
||
| 2251 | * If no plugin slug was passed into the constructor, always returns true. |
||
| 2252 | * |
||
| 2253 | * @return bool |
||
| 2254 | */ |
||
| 2255 | public function is_plugin_enabled() { |
||
| 2262 | |||
| 2263 | /** |
||
| 2264 | * Perform the API request to refresh the blog token. |
||
| 2265 | * Note that we are making this request on behalf of the Jetpack master user, |
||
| 2266 | * given they were (most probably) the ones that registered the site at the first place. |
||
| 2267 | * |
||
| 2268 | * @return WP_Error|bool The result of updating the blog_token option. |
||
| 2269 | */ |
||
| 2270 | public function refresh_blog_token() { |
||
| 2320 | |||
| 2321 | /** |
||
| 2322 | * Disconnect the user from WP.com, and initiate the reconnect process. |
||
| 2323 | * |
||
| 2324 | * @return bool |
||
| 2325 | */ |
||
| 2326 | public function refresh_user_token() { |
||
| 2331 | |||
| 2332 | /** |
||
| 2333 | * Fetches a signed token. |
||
| 2334 | * |
||
| 2335 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->get_signed_token() instead. |
||
| 2336 | * |
||
| 2337 | * @param object $token the token. |
||
| 2338 | * @return WP_Error|string a signed token |
||
| 2339 | */ |
||
| 2340 | public function get_signed_token( $token ) { |
||
| 2344 | |||
| 2345 | /** |
||
| 2346 | * If the site-level connection is active, add the list of plugins using connection to the heartbeat (except Jetpack itself) |
||
| 2347 | * |
||
| 2348 | * @param array $stats The Heartbeat stats array. |
||
| 2349 | * @return array $stats |
||
| 2350 | */ |
||
| 2351 | public function add_stats_to_heartbeat( $stats ) { |
||
| 2366 | } |
||
| 2367 |
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.