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 Jetpack_XMLRPC_Server 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 Jetpack_XMLRPC_Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Jetpack_XMLRPC_Server { |
||
| 7 | /** |
||
| 8 | * The current error object |
||
| 9 | */ |
||
| 10 | public $error = null; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The current user |
||
| 14 | */ |
||
| 15 | public $user = null; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Whitelist of the XML-RPC methods available to the Jetpack Server. If the |
||
| 19 | * user is not authenticated (->login()) then the methods are never added, |
||
| 20 | * so they will get a "does not exist" error. |
||
| 21 | */ |
||
| 22 | function xmlrpc_methods( $core_methods ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Whitelist of the bootstrap XML-RPC methods |
||
| 74 | */ |
||
| 75 | function bootstrap_xmlrpc_methods() { |
||
| 82 | |||
| 83 | function authorize_xmlrpc_methods() { |
||
| 88 | |||
| 89 | function provision_xmlrpc_methods() { |
||
| 96 | |||
| 97 | function remote_authorize( $request ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to |
||
| 139 | * register this site so that a plan can be provisioned. |
||
| 140 | * |
||
| 141 | * @param array $request An array containing at minimum nonce and local_user keys. |
||
| 142 | * |
||
| 143 | * @return WP_Error|array |
||
| 144 | */ |
||
| 145 | public function remote_register( $request ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to |
||
| 222 | * register this site so that a plan can be provisioned. |
||
| 223 | * |
||
| 224 | * @param array $request An array containing at minimum a nonce key and a local_username key. |
||
| 225 | * |
||
| 226 | * @return WP_Error|array |
||
| 227 | */ |
||
| 228 | public function remote_provision( $request ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Given an array containing a local user identifier and a nonce, will attempt to fetch and set |
||
| 287 | * an access token for the given user. |
||
| 288 | * |
||
| 289 | * @param array $request An array containing local_user and nonce keys at minimum. |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | public function remote_connect( $request, $ixr_client = false ) { |
||
| 293 | if ( Jetpack::is_active() ) { |
||
| 294 | return $this->error( |
||
| 295 | new WP_Error( |
||
| 296 | 'already_connected', |
||
| 297 | __( 'Jetpack is already connected.', 'jetpack' ), |
||
| 298 | 400 |
||
| 299 | ), |
||
| 300 | 'jpc_remote_register_fail' |
||
| 301 | ); |
||
| 302 | } |
||
| 303 | |||
| 304 | $user = $this->fetch_and_verify_local_user( $request ); |
||
| 305 | |||
| 306 | if ( ! $user || is_wp_error( $user ) || is_a( $user, 'IXR_Error' ) ) { |
||
| 307 | return $this->error( |
||
| 308 | new WP_Error( |
||
| 309 | 'input_error', |
||
| 310 | __( 'Valid user is required.', 'jetpack' ), |
||
| 311 | 400 |
||
| 312 | ), |
||
| 313 | 'jpc_remote_connect_fail' |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | if ( empty( $request['nonce'] ) ) { |
||
| 318 | return $this->error( |
||
| 319 | new WP_Error( |
||
| 320 | 'input_error', |
||
| 321 | __( 'A non-empty nonce must be supplied.', 'jetpack' ), |
||
| 322 | 400 |
||
| 323 | ), |
||
| 324 | 'jpc_remote_connect_fail' |
||
| 325 | ); |
||
| 326 | } |
||
| 327 | |||
| 328 | if ( ! $ixr_client ) { |
||
| 329 | Jetpack::load_xml_rpc_client(); |
||
| 330 | $ixr_client = new Jetpack_IXR_Client(); |
||
| 331 | } |
||
| 332 | $ixr_client->query( 'jetpack.getUserAccessToken', array( |
||
|
|
|||
| 333 | 'nonce' => sanitize_text_field( $request['nonce'] ), |
||
| 334 | 'external_user_id' => $user->ID, |
||
| 335 | ) ); |
||
| 336 | |||
| 337 | $token = $ixr_client->isError() ? false : $ixr_client->getResponse(); |
||
| 338 | if ( empty( $token ) ) { |
||
| 339 | return $this->error( |
||
| 340 | new WP_Error( |
||
| 341 | 'token_fetch_failed', |
||
| 342 | __( 'Failed to fetch user token from WordPress.com.', 'jetpack' ), |
||
| 343 | 400 |
||
| 344 | ), |
||
| 345 | 'jpc_remote_connect_fail' |
||
| 346 | ); |
||
| 347 | } |
||
| 348 | $token = sanitize_text_field( $token ); |
||
| 349 | |||
| 350 | Jetpack::update_user_token( $user->ID, sprintf( '%s.%d', $token, $user->ID ), true ); |
||
| 351 | |||
| 352 | $this->do_post_authorization(); |
||
| 353 | |||
| 354 | return Jetpack::is_active(); |
||
| 355 | } |
||
| 356 | |||
| 357 | private function fetch_and_verify_local_user( $request ) { |
||
| 358 | if ( empty( $request['local_user'] ) ) { |
||
| 359 | return $this->error( |
||
| 360 | new Jetpack_Error( |
||
| 361 | 'local_user_missing', |
||
| 362 | __( 'The required "local_user" parameter is missing.', 'jetpack' ), |
||
| 363 | 400 |
||
| 364 | ), |
||
| 365 | 'jpc_remote_provision_fail' |
||
| 366 | ); |
||
| 367 | } |
||
| 368 | |||
| 369 | // local user is used to look up by login, email or ID |
||
| 370 | $local_user_info = $request['local_user']; |
||
| 371 | |||
| 372 | $user = get_user_by( 'login', $local_user_info ); |
||
| 373 | |||
| 374 | if ( ! $user ) { |
||
| 375 | $user = get_user_by( 'email', $local_user_info ); |
||
| 376 | } |
||
| 377 | |||
| 378 | if ( ! $user ) { |
||
| 379 | $user = get_user_by( 'ID', $local_user_info ); |
||
| 380 | } |
||
| 381 | |||
| 382 | return $user; |
||
| 383 | } |
||
| 384 | |||
| 385 | private function tracks_record_error( $name, $error, $user = null ) { |
||
| 386 | if ( is_wp_error( $error ) ) { |
||
| 387 | JetpackTracking::record_user_event( $name, array( |
||
| 388 | 'error_code' => $error->get_error_code(), |
||
| 389 | 'error_message' => $error->get_error_message() |
||
| 390 | ), $user ); |
||
| 391 | } elseif( is_a( $error, 'IXR_Error' ) ) { |
||
| 392 | JetpackTracking::record_user_event( $name, array( |
||
| 393 | 'error_code' => $error->code, |
||
| 394 | 'error_message' => $error->message |
||
| 395 | ), $user ); |
||
| 396 | } |
||
| 397 | |||
| 398 | return $error; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Verifies that Jetpack.WordPress.com received a registration request from this site |
||
| 403 | */ |
||
| 404 | function verify_registration( $data ) { |
||
| 405 | // failure modes will be recorded in tracks in the verify_action method |
||
| 406 | return $this->verify_action( array( 'register', $data[0], $data[1] ) ); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure |
||
| 411 | * |
||
| 412 | * Possible error_codes: |
||
| 413 | * |
||
| 414 | * verify_secret_1_missing |
||
| 415 | * verify_secret_1_malformed |
||
| 416 | * verify_secrets_missing: verification secrets are not found in database |
||
| 417 | * verify_secrets_incomplete: verification secrets are only partially found in database |
||
| 418 | * verify_secrets_expired: verification secrets have expired |
||
| 419 | * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com |
||
| 420 | * state_missing: required parameter of state not found |
||
| 421 | * state_malformed: state is not a digit |
||
| 422 | * invalid_state: state in request does not match the stored state |
||
| 423 | * |
||
| 424 | * The 'authorize' and 'register' actions have additional error codes |
||
| 425 | * |
||
| 426 | * state_missing: a state ( user id ) was not supplied |
||
| 427 | * state_malformed: state is not the correct data type |
||
| 428 | * invalid_state: supplied state does not match the stored state |
||
| 429 | */ |
||
| 430 | function verify_action( $params ) { |
||
| 431 | $action = $params[0]; |
||
| 432 | $verify_secret = $params[1]; |
||
| 433 | $state = isset( $params[2] ) ? $params[2] : ''; |
||
| 434 | $user = get_user_by( 'id', $state ); |
||
| 435 | JetpackTracking::record_user_event( 'jpc_verify_' . $action . '_begin', array(), $user ); |
||
| 436 | $tracks_failure_event_name = 'jpc_verify_' . $action . '_fail'; |
||
| 437 | |||
| 438 | if ( empty( $verify_secret ) ) { |
||
| 439 | return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 440 | } else if ( ! is_string( $verify_secret ) ) { |
||
| 441 | return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 442 | } else if ( empty( $state ) ) { |
||
| 443 | return $this->error( new Jetpack_Error( 'state_missing', sprintf( 'The required "%s" parameter is missing.', 'state' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 444 | } else if ( ! ctype_digit( $state ) ) { |
||
| 445 | return $this->error( new Jetpack_Error( 'state_malformed', sprintf( 'The required "%s" parameter is malformed.', 'state' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 446 | } |
||
| 447 | |||
| 448 | $secrets = Jetpack::get_secrets( $action, $state ); |
||
| 449 | |||
| 450 | if ( ! $secrets ) { |
||
| 451 | Jetpack::delete_secrets( $action, $state ); |
||
| 452 | return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification secrets not found', 400 ), $tracks_failure_event_name, $user ); |
||
| 453 | } |
||
| 454 | |||
| 455 | if ( is_wp_error( $secrets ) ) { |
||
| 456 | Jetpack::delete_secrets( $action, $state ); |
||
| 457 | return $this->error( new Jetpack_Error( $secrets->get_error_code(), $secrets->get_error_message(), 400 ), $tracks_failure_event_name, $user ); |
||
| 458 | } |
||
| 459 | |||
| 460 | if ( empty( $secrets['secret_1'] ) || empty( $secrets['secret_2'] ) || empty( $secrets['exp'] ) ) { |
||
| 461 | Jetpack::delete_secrets( $action, $state ); |
||
| 462 | return $this->error( new Jetpack_Error( 'verify_secrets_incomplete', 'Verification secrets are incomplete', 400 ), $tracks_failure_event_name, $user ); |
||
| 463 | } |
||
| 464 | |||
| 465 | if ( ! hash_equals( $verify_secret, $secrets['secret_1'] ) ) { |
||
| 466 | Jetpack::delete_secrets( $action, $state ); |
||
| 467 | return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ), $tracks_failure_event_name, $user ); |
||
| 468 | } |
||
| 469 | |||
| 470 | Jetpack::delete_secrets( $action, $state ); |
||
| 471 | |||
| 472 | JetpackTracking::record_user_event( 'jpc_verify_' . $action . '_success', array(), $user ); |
||
| 473 | |||
| 474 | return $secrets['secret_2']; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Wrapper for wp_authenticate( $username, $password ); |
||
| 479 | * |
||
| 480 | * @return WP_User|bool |
||
| 481 | */ |
||
| 482 | function login() { |
||
| 483 | Jetpack::init()->require_jetpack_authentication(); |
||
| 484 | $user = wp_authenticate( 'username', 'password' ); |
||
| 485 | if ( is_wp_error( $user ) ) { |
||
| 486 | if ( 'authentication_failed' == $user->get_error_code() ) { // Generic error could mean most anything. |
||
| 487 | $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 ); |
||
| 488 | } else { |
||
| 489 | $this->error = $user; |
||
| 490 | } |
||
| 491 | return false; |
||
| 492 | } else if ( !$user ) { // Shouldn't happen. |
||
| 493 | $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 ); |
||
| 494 | return false; |
||
| 495 | } |
||
| 496 | |||
| 497 | return $user; |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Returns the current error as an IXR_Error |
||
| 502 | * |
||
| 503 | * @return bool|IXR_Error |
||
| 504 | */ |
||
| 505 | function error( $error = null, $tracks_event_name = null, $user = null ) { |
||
| 506 | // record using Tracks |
||
| 507 | if ( null !== $tracks_event_name ) { |
||
| 508 | $this->tracks_record_error( $tracks_event_name, $error, $user ); |
||
| 509 | } |
||
| 510 | |||
| 511 | if ( !is_null( $error ) ) { |
||
| 512 | $this->error = $error; |
||
| 513 | } |
||
| 514 | |||
| 515 | if ( is_wp_error( $this->error ) ) { |
||
| 516 | $code = $this->error->get_error_data(); |
||
| 517 | if ( !$code ) { |
||
| 518 | $code = -10520; |
||
| 519 | } |
||
| 520 | $message = sprintf( 'Jetpack: [%s] %s', $this->error->get_error_code(), $this->error->get_error_message() ); |
||
| 521 | return new IXR_Error( $code, $message ); |
||
| 522 | } else if ( is_a( $this->error, 'IXR_Error' ) ) { |
||
| 523 | return $this->error; |
||
| 524 | } |
||
| 525 | |||
| 526 | return false; |
||
| 527 | } |
||
| 528 | |||
| 529 | /* API Methods */ |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Just authenticates with the given Jetpack credentials. |
||
| 533 | * |
||
| 534 | * @return string The current Jetpack version number |
||
| 535 | */ |
||
| 536 | function test_connection() { |
||
| 537 | return JETPACK__VERSION; |
||
| 538 | } |
||
| 539 | |||
| 540 | function test_api_user_code( $args ) { |
||
| 541 | $client_id = (int) $args[0]; |
||
| 542 | $user_id = (int) $args[1]; |
||
| 543 | $nonce = (string) $args[2]; |
||
| 544 | $verify = (string) $args[3]; |
||
| 545 | |||
| 546 | /* debugging |
||
| 547 | error_log( "CLIENT: $client_id" ); |
||
| 548 | error_log( "USER: $user_id" ); |
||
| 549 | error_log( "NONCE: $nonce" ); |
||
| 550 | error_log( "VERIFY: $verify" ); |
||
| 551 | */ |
||
| 552 | |||
| 553 | return Jetpack::verify_request_on_behalf_of_client( compact( |
||
| 554 | 'client_id', |
||
| 555 | 'user_id', |
||
| 556 | 'nonce', |
||
| 557 | 'verify' |
||
| 558 | ) ); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Disconnect this blog from the connected wordpress.com account |
||
| 563 | * @return boolean |
||
| 564 | */ |
||
| 565 | function disconnect_blog() { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Unlink a user from WordPress.com |
||
| 580 | * |
||
| 581 | * This will fail if called by the Master User. |
||
| 582 | */ |
||
| 583 | function unlink_user() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Returns any object that is able to be synced |
||
| 590 | */ |
||
| 591 | function sync_object( $args ) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Returns the home URL and site URL for the current site which can be used on the WPCOM side for |
||
| 605 | * IDC mitigation to decide whether sync should be allowed if the home and siteurl values differ between WPCOM |
||
| 606 | * and the remote Jetpack site. |
||
| 607 | * |
||
| 608 | * @return array |
||
| 609 | */ |
||
| 610 | function validate_urls_for_idc_mitigation() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Returns what features are available. Uses the slug of the module files. |
||
| 620 | * |
||
| 621 | * @return array |
||
| 622 | */ |
||
| 623 | View Code Duplication | function features_available() { |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Returns what features are enabled. Uses the slug of the modules files. |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | View Code Duplication | function features_enabled() { |
|
| 647 | |||
| 648 | function update_attachment_parent( $args ) { |
||
| 657 | |||
| 658 | function json_api( $args = array() ) { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Handles authorization actions after connecting a site, such as enabling modules. |
||
| 755 | * |
||
| 756 | * This do_post_authorization() is used in this class, as opposed to calling |
||
| 757 | * Jetpack::handle_post_authorization_actions() directly so that we can mock this method as necessary. |
||
| 758 | * |
||
| 759 | * @return void |
||
| 760 | */ |
||
| 761 | public function do_post_authorization() { |
||
| 766 | } |
||
| 767 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: