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_CLI 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_CLI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Jetpack_CLI extends WP_CLI_Command { |
||
| 9 | |||
| 10 | // Aesthetics |
||
| 11 | public $green_open = "\033[32m"; |
||
| 12 | public $red_open = "\033[31m"; |
||
| 13 | public $yellow_open = "\033[33m"; |
||
| 14 | public $color_close = "\033[0m"; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Get Jetpack Details |
||
| 18 | * |
||
| 19 | * ## OPTIONS |
||
| 20 | * |
||
| 21 | * empty: Leave it empty for basic stats |
||
| 22 | * |
||
| 23 | * full: View full stats. It's the data from the heartbeat |
||
| 24 | * |
||
| 25 | * ## EXAMPLES |
||
| 26 | * |
||
| 27 | * wp jetpack status |
||
| 28 | * wp jetpack status full |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | public function status( $args, $assoc_args ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Disconnect Jetpack Blogs or Users |
||
| 92 | * |
||
| 93 | * ## OPTIONS |
||
| 94 | * |
||
| 95 | * blog: Disconnect the entire blog. |
||
| 96 | * |
||
| 97 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 98 | * |
||
| 99 | * Please note, the primary account that the blog is connected |
||
| 100 | * to WordPress.com with cannot be disconnected without |
||
| 101 | * disconnecting the entire blog. |
||
| 102 | * |
||
| 103 | * ## EXAMPLES |
||
| 104 | * |
||
| 105 | * wp jetpack disconnect blog |
||
| 106 | * wp jetpack disconnect user 13 |
||
| 107 | * wp jetpack disconnect user username |
||
| 108 | * wp jetpack disconnect user [email protected] |
||
| 109 | * |
||
| 110 | * @synopsis <blog|user> [<user_identifier>] |
||
| 111 | */ |
||
| 112 | public function disconnect( $args, $assoc_args ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Reset Jetpack options and settings to default |
||
| 167 | * |
||
| 168 | * ## OPTIONS |
||
| 169 | * |
||
| 170 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 171 | * |
||
| 172 | * options: Resets all Jetpack options except: |
||
| 173 | * - All private options (Blog token, user token, etc...) |
||
| 174 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 175 | * - master_user |
||
| 176 | * - version |
||
| 177 | * - activated |
||
| 178 | * |
||
| 179 | * ## EXAMPLES |
||
| 180 | * |
||
| 181 | * wp jetpack reset options |
||
| 182 | * wp jetpack reset modules |
||
| 183 | * |
||
| 184 | * @synopsis <modules|options> |
||
| 185 | */ |
||
| 186 | public function reset( $args, $assoc_args ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Manage Jetpack Modules |
||
| 244 | * |
||
| 245 | * ## OPTIONS |
||
| 246 | * |
||
| 247 | * list : View all available modules, and their status. |
||
| 248 | * activate all : Activate all modules |
||
| 249 | * deactivate all: Deactivate all modules |
||
| 250 | * |
||
| 251 | * activate <module_slug> : Activate a module. |
||
| 252 | * deactivate <module_slug> : Deactivate a module. |
||
| 253 | * toggle <module_slug> : Toggle a module on or off. |
||
| 254 | * |
||
| 255 | * ## EXAMPLES |
||
| 256 | * |
||
| 257 | * wp jetpack module list |
||
| 258 | * wp jetpack module activate stats |
||
| 259 | * wp jetpack module deactivate stats |
||
| 260 | * wp jetpack module toggle stats |
||
| 261 | * |
||
| 262 | * wp jetpack module activate all |
||
| 263 | * wp jetpack module deactivate all |
||
| 264 | * |
||
| 265 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
| 266 | */ |
||
| 267 | public function module( $args, $assoc_args ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Manage Protect Settings |
||
| 337 | * |
||
| 338 | * ## OPTIONS |
||
| 339 | * |
||
| 340 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 341 | * |
||
| 342 | * |
||
| 343 | * ## EXAMPLES |
||
| 344 | * |
||
| 345 | * wp jetpack protect whitelist <ip address> |
||
| 346 | * wp jetpack protect whitelist list |
||
| 347 | * wp jetpack protect whitelist clear |
||
| 348 | * |
||
| 349 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 350 | */ |
||
| 351 | public function protect( $args, $assoc_args ) { |
||
| 352 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 353 | if ( ! in_array( $action, array( 'whitelist' ) ) ) { |
||
| 354 | /* translators: %s is a command like "prompt" */ |
||
| 355 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 356 | } |
||
| 357 | // Check if module is active |
||
| 358 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
||
| 359 | WP_CLI::error( sprintf( _x( '%s is not active. You can activate it with "wp jetpack module activate %s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) ); |
||
| 360 | } |
||
| 361 | if ( in_array( $action, array( 'whitelist' ) ) ) { |
||
| 362 | if ( isset( $args[1] ) ) { |
||
| 363 | $action = 'whitelist'; |
||
| 364 | } else { |
||
| 365 | $action = 'prompt'; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | switch ( $action ) { |
||
| 369 | case 'whitelist': |
||
| 370 | $whitelist = array(); |
||
| 371 | $new_ip = $args[1]; |
||
| 372 | $current_whitelist = get_site_option( 'jetpack_protect_whitelist' ); |
||
| 373 | |||
| 374 | // Build array of IPs that are already whitelisted. |
||
| 375 | // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get |
||
| 376 | // low & high range params for jetpack_protect_ip_address_is_in_range(); |
||
| 377 | foreach( $current_whitelist as $whitelisted ) { |
||
| 378 | |||
| 379 | // IP ranges |
||
| 380 | if ( $whitelisted->range ) { |
||
| 381 | |||
| 382 | // Is it already whitelisted? |
||
| 383 | if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { |
||
| 384 | /* translators: %s is an IP address */ |
||
| 385 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 386 | break; |
||
| 387 | } |
||
| 388 | $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; |
||
| 389 | |||
| 390 | } else { // Individual IPs |
||
| 391 | |||
| 392 | // Check if the IP is already whitelisted (single IP only) |
||
| 393 | if ( $new_ip == $whitelisted->ip_address ) { |
||
| 394 | /* translators: %s is an IP address */ |
||
| 395 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 396 | break; |
||
| 397 | } |
||
| 398 | $whitelist[] = $whitelisted->ip_address; |
||
| 399 | |||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | /* |
||
| 404 | * List the whitelist |
||
| 405 | * Done here because it's easier to read the $whitelist array after it's been rebuilt |
||
| 406 | */ |
||
| 407 | if ( isset( $args[1] ) && 'list' == $args[1] ) { |
||
| 408 | if ( ! empty( $whitelist ) ) { |
||
| 409 | WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); |
||
| 410 | foreach ( $whitelist as $ip ) { |
||
| 411 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; |
||
| 412 | } |
||
| 413 | } else { |
||
| 414 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 415 | } |
||
| 416 | break; |
||
| 417 | } |
||
| 418 | |||
| 419 | /* |
||
| 420 | * Clear the whitelist |
||
| 421 | */ |
||
| 422 | if ( isset( $args[1] ) && 'clear' == $args[1] ) { |
||
| 423 | if ( ! empty( $whitelist ) ) { |
||
| 424 | $whitelist = array(); |
||
| 425 | jetpack_protect_save_whitelist( $whitelist ); |
||
| 426 | WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); |
||
| 427 | } else { |
||
| 428 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 429 | } |
||
| 430 | break; |
||
| 431 | } |
||
| 432 | |||
| 433 | // Append new IP to whitelist array |
||
| 434 | array_push( $whitelist, $new_ip ); |
||
| 435 | |||
| 436 | // Save whitelist if there are no errors |
||
| 437 | $result = jetpack_protect_save_whitelist( $whitelist ); |
||
| 438 | if ( is_wp_error( $result ) ) { |
||
| 439 | WP_CLI::error( __( $result, 'jetpack' ) ); |
||
| 440 | } |
||
| 441 | |||
| 442 | /* translators: %s is an IP address */ |
||
| 443 | WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); |
||
| 444 | break; |
||
| 445 | case 'prompt': |
||
| 446 | WP_CLI::error( |
||
| 447 | __( 'No command found.', 'jetpack' ) . "\n" . |
||
| 448 | __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . |
||
| 449 | _x( 'You can save a range of IPs {low_range}-{high_range}. No spaces allowed. (example: 1.1.1.1-2.2.2.2)', 'Instructions on how to whitelist IP ranges - low_range/high_range should be translated.', 'jetpack' ) . "\n" . |
||
| 450 | _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
||
| 451 | ); |
||
| 452 | break; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Manage Jetpack Options |
||
| 458 | * |
||
| 459 | * ## OPTIONS |
||
| 460 | * |
||
| 461 | * list : List all jetpack options and their values |
||
| 462 | * delete : Delete an option |
||
| 463 | * - can only delete options that are white listed. |
||
| 464 | * update : update an option |
||
| 465 | * - can only update option strings |
||
| 466 | * get : get the value of an option |
||
| 467 | * |
||
| 468 | * ## EXAMPLES |
||
| 469 | * |
||
| 470 | * wp jetpack options list |
||
| 471 | * wp jetpack options get <option_name> |
||
| 472 | * wp jetpack options delete <option_name> |
||
| 473 | * wp jetpack options update <option_name> [<option_value>] |
||
| 474 | * |
||
| 475 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 476 | */ |
||
| 477 | public function options( $args, $assoc_args ) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Get the status of or start a new Jetpack sync. |
||
| 577 | * |
||
| 578 | * ## OPTIONS |
||
| 579 | * |
||
| 580 | * status : Print the current sync status |
||
| 581 | * start : Start a full sync from this site to WordPress.com |
||
| 582 | * |
||
| 583 | * ## EXAMPLES |
||
| 584 | * |
||
| 585 | * wp jetpack sync status |
||
| 586 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
| 587 | * |
||
| 588 | * @synopsis <status|start> [--<field>=<value>] |
||
| 589 | */ |
||
| 590 | public function sync( $args, $assoc_args ) { |
||
| 591 | if ( ! Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 592 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) ); |
||
| 593 | } |
||
| 594 | |||
| 595 | $action = isset( $args[0] ) ? $args[0] : 'status'; |
||
| 596 | |||
| 597 | switch ( $action ) { |
||
| 598 | case 'status': |
||
| 599 | $status = Jetpack_Sync_Actions::get_sync_status(); |
||
| 600 | $collection = array(); |
||
| 601 | foreach ( $status as $key => $item ) { |
||
| 602 | $collection[] = array( |
||
| 603 | 'option' => $key, |
||
| 604 | 'value' => is_scalar( $item ) ? $item : json_encode( $item ) |
||
| 605 | ); |
||
| 606 | } |
||
| 607 | |||
| 608 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
||
| 609 | break; |
||
| 610 | case 'start': |
||
| 611 | // Get the original settings so that we can restore them later |
||
| 612 | $original_settings = Jetpack_Sync_Settings::get_settings(); |
||
| 613 | |||
| 614 | // Initialize sync settigns so we can sync as quickly as possible |
||
| 615 | $sync_settings = wp_parse_args( |
||
| 616 | array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ), |
||
| 617 | array( |
||
| 618 | 'sync_wait_time' => 0, |
||
| 619 | 'enqueue_wait_time' => 0, |
||
| 620 | 'queue_max_writes_sec' => 10000, |
||
| 621 | 'max_queue_size_full_sync' => 100000 |
||
| 622 | ) |
||
| 623 | ); |
||
| 624 | Jetpack_Sync_Settings::update_settings( $sync_settings ); |
||
| 625 | |||
| 626 | // Convert comma-delimited string of modules to an array |
||
| 627 | if ( ! empty( $assoc_args['modules'] ) ) { |
||
| 628 | $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) ); |
||
| 629 | |||
| 630 | // Convert the array so that the keys are the module name and the value is true to indicate |
||
| 631 | // that we want to sync the module |
||
| 632 | $modules = array_map( '__return_true', array_flip( $modules ) ); |
||
| 633 | } |
||
| 634 | |||
| 635 | View Code Duplication | foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
|
| 636 | if ( |
||
| 637 | 'users' === $module_name && |
||
| 638 | isset( $assoc_args[ $module_name ] ) && |
||
| 639 | 'initial' === $assoc_args[ $module_name ] |
||
| 640 | ) { |
||
| 641 | $modules[ 'users' ] = 'initial'; |
||
| 642 | } elseif ( isset( $assoc_args[ $module_name ] ) ) { |
||
| 643 | $ids = explode( ',', $assoc_args[ $module_name ] ); |
||
| 644 | if ( count( $ids ) > 0 ) { |
||
| 645 | $modules[ $module_name ] = $ids; |
||
| 646 | } |
||
| 647 | } |
||
| 648 | } |
||
| 649 | |||
| 650 | if ( empty( $modules ) ) { |
||
| 651 | $modules = null; |
||
| 652 | } |
||
| 653 | |||
| 654 | // Kick off a full sync |
||
| 655 | if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) { |
||
| 656 | View Code Duplication | if ( $modules ) { |
|
| 657 | WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) ); |
||
| 658 | } else { |
||
| 659 | WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) ); |
||
| 660 | } |
||
| 661 | View Code Duplication | } else { |
|
| 662 | |||
| 663 | // Reset sync settings to original. |
||
| 664 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 665 | |||
| 666 | if ( $modules ) { |
||
| 667 | WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) ); |
||
| 668 | } else { |
||
| 669 | WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) ); |
||
| 670 | } |
||
| 671 | } |
||
| 672 | |||
| 673 | // Keep sending to WPCOM until there's nothing to send |
||
| 674 | $i = 1; |
||
| 675 | do { |
||
| 676 | $result = Jetpack_Sync_Actions::$sender->do_full_sync(); |
||
| 677 | if ( $result ) { |
||
| 678 | if ( 1 == $i++ ) { |
||
| 679 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
||
| 680 | } else { |
||
| 681 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
||
| 682 | } |
||
| 683 | } |
||
| 684 | } while ( $result ); |
||
| 685 | |||
| 686 | // Reset sync settings to original. |
||
| 687 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 688 | |||
| 689 | WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) ); |
||
| 690 | break; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * List the contents of a specific Jetpack sync queue. |
||
| 696 | * |
||
| 697 | * ## OPTIONS |
||
| 698 | * |
||
| 699 | * peek : List the 100 front-most items on the queue. |
||
| 700 | * |
||
| 701 | * ## EXAMPLES |
||
| 702 | * |
||
| 703 | * wp jetpack sync_queue full_sync peek |
||
| 704 | * |
||
| 705 | * @synopsis <incremental|full_sync> <peek> |
||
| 706 | */ |
||
| 707 | public function sync_queue( $args, $assoc_args ) { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
| 761 | * |
||
| 762 | * Returns success or error JSON |
||
| 763 | * |
||
| 764 | * <token_json> |
||
| 765 | * : JSON blob of WPCOM API token |
||
| 766 | */ |
||
| 767 | public function partner_cancel( $args, $named_args ) { |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Provision a site using a Jetpack Partner license |
||
| 810 | * |
||
| 811 | * Returns JSON blob |
||
| 812 | * |
||
| 813 | * ## OPTIONS |
||
| 814 | * |
||
| 815 | * <token_json> |
||
| 816 | * : JSON blob of WPCOM API token |
||
| 817 | * --user_id=<user_id> |
||
| 818 | * : Local ID of user to connect as (if omitted, user will be required to redirect via wp-admin) |
||
| 819 | * [--plan=<plan_name>] |
||
| 820 | * : Slug of the requested plan, e.g. premium |
||
| 821 | * [--wpcom_user_id=<user_id>] |
||
| 822 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
| 823 | * [--force_register=<register>] |
||
| 824 | * : Whether to force a site to register |
||
| 825 | * |
||
| 826 | * ## EXAMPLES |
||
| 827 | * |
||
| 828 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 829 | * { success: true } |
||
| 830 | * |
||
| 831 | * @synopsis <token_json> --user_id=<user_id> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--force_register=<register>] |
||
| 832 | */ |
||
| 833 | public function partner_provision( $args, $named_args ) { |
||
| 963 | |||
| 964 | private function get_api_host() { |
||
| 968 | |||
| 969 | private function partner_provision_error( $error ) { |
||
| 977 | } |
||
| 978 | |||
| 1015 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: