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 ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Disconnect Jetpack Blogs or Users |
||
| 91 | * |
||
| 92 | * ## OPTIONS |
||
| 93 | * |
||
| 94 | * blog: Disconnect the entire blog. |
||
| 95 | * |
||
| 96 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 97 | * |
||
| 98 | * Please note, the primary account that the blog is connected |
||
| 99 | * to WordPress.com with cannot be disconnected without |
||
| 100 | * disconnecting the entire blog. |
||
| 101 | * |
||
| 102 | * ## EXAMPLES |
||
| 103 | * |
||
| 104 | * wp jetpack disconnect blog |
||
| 105 | * wp jetpack disconnect user 13 |
||
| 106 | * wp jetpack disconnect user username |
||
| 107 | * wp jetpack disconnect user [email protected] |
||
| 108 | * |
||
| 109 | * @synopsis <blog|user> [<user_identifier>] |
||
| 110 | */ |
||
| 111 | public function disconnect( $args, $assoc_args ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Reset Jetpack options and settings to default |
||
| 164 | * |
||
| 165 | * ## OPTIONS |
||
| 166 | * |
||
| 167 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 168 | * |
||
| 169 | * options: Resets all Jetpack options except: |
||
| 170 | * - All private options (Blog token, user token, etc...) |
||
| 171 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 172 | * - master_user |
||
| 173 | * - version |
||
| 174 | * - activated |
||
| 175 | * |
||
| 176 | * ## EXAMPLES |
||
| 177 | * |
||
| 178 | * wp jetpack reset options |
||
| 179 | * wp jetpack reset modules |
||
| 180 | * |
||
| 181 | * @synopsis <modules|options> |
||
| 182 | */ |
||
| 183 | public function reset( $args, $assoc_args ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Manage Jetpack Modules |
||
| 238 | * |
||
| 239 | * ## OPTIONS |
||
| 240 | * |
||
| 241 | * list : View all available modules, and their status. |
||
| 242 | * activate all : Activate all modules |
||
| 243 | * deactivate all: Deactivate all modules |
||
| 244 | * |
||
| 245 | * activate <module_slug> : Activate a module. |
||
| 246 | * deactivate <module_slug> : Deactivate a module. |
||
| 247 | * toggle <module_slug> : Toggle a module on or off. |
||
| 248 | * |
||
| 249 | * ## EXAMPLES |
||
| 250 | * |
||
| 251 | * wp jetpack module list |
||
| 252 | * wp jetpack module activate stats |
||
| 253 | * wp jetpack module deactivate stats |
||
| 254 | * wp jetpack module toggle stats |
||
| 255 | * |
||
| 256 | * wp jetpack module activate all |
||
| 257 | * wp jetpack module deactivate all |
||
| 258 | * |
||
| 259 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
| 260 | */ |
||
| 261 | public function module( $args, $assoc_args ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Manage Protect Settings |
||
| 330 | * |
||
| 331 | * ## OPTIONS |
||
| 332 | * |
||
| 333 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 334 | * |
||
| 335 | * |
||
| 336 | * ## EXAMPLES |
||
| 337 | * |
||
| 338 | * wp jetpack protect whitelist <ip address> |
||
| 339 | * wp jetpack protect whitelist list |
||
| 340 | * wp jetpack protect whitelist clear |
||
| 341 | * |
||
| 342 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 343 | */ |
||
| 344 | public function protect( $args, $assoc_args ) { |
||
| 345 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 346 | if ( ! in_array( $action, array( 'whitelist' ) ) ) { |
||
| 347 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 348 | } |
||
| 349 | // Check if module is active |
||
| 350 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
||
| 351 | 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__ ) ); |
||
| 352 | } |
||
| 353 | if ( in_array( $action, array( 'whitelist' ) ) ) { |
||
| 354 | if ( isset( $args[1] ) ) { |
||
| 355 | $action = 'whitelist'; |
||
| 356 | } else { |
||
| 357 | $action = 'prompt'; |
||
| 358 | } |
||
| 359 | } |
||
| 360 | switch ( $action ) { |
||
| 361 | case 'whitelist': |
||
| 362 | $whitelist = array(); |
||
| 363 | $new_ip = $args[1]; |
||
| 364 | $current_whitelist = get_site_option( 'jetpack_protect_whitelist' ); |
||
| 365 | |||
| 366 | // Build array of IPs that are already whitelisted. |
||
| 367 | // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get |
||
| 368 | // low & high range params for jetpack_protect_ip_address_is_in_range(); |
||
| 369 | foreach( $current_whitelist as $whitelisted ) { |
||
| 370 | |||
| 371 | // IP ranges |
||
| 372 | if ( $whitelisted->range ) { |
||
| 373 | |||
| 374 | // Is it already whitelisted? |
||
| 375 | if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { |
||
| 376 | WP_CLI::error( sprintf( __( "%s has already been whitelisted", 'jetpack' ), $new_ip ) ); |
||
| 377 | break; |
||
| 378 | } |
||
| 379 | $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; |
||
| 380 | |||
| 381 | } else { // Individual IPs |
||
| 382 | |||
| 383 | // Check if the IP is already whitelisted (single IP only) |
||
| 384 | if ( $new_ip == $whitelisted->ip_address ) { |
||
| 385 | WP_CLI::error( sprintf( __( "%s has already been whitelisted", 'jetpack' ), $new_ip ) ); |
||
| 386 | break; |
||
| 387 | } |
||
| 388 | $whitelist[] = $whitelisted->ip_address; |
||
| 389 | |||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /* |
||
| 394 | * List the whitelist |
||
| 395 | * Done here because it's easier to read the $whitelist array after it's been rebuilt |
||
| 396 | */ |
||
| 397 | if ( isset( $args[1] ) && 'list' == $args[1] ) { |
||
| 398 | View Code Duplication | if ( ! empty( $whitelist ) ) { |
|
| 399 | WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); |
||
| 400 | foreach ( $whitelist as $ip ) { |
||
| 401 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; |
||
| 402 | } |
||
| 403 | } else { |
||
| 404 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 405 | } |
||
| 406 | break; |
||
| 407 | } |
||
| 408 | |||
| 409 | /* |
||
| 410 | * Clear the whitelist |
||
| 411 | */ |
||
| 412 | if ( isset( $args[1] ) && 'clear' == $args[1] ) { |
||
| 413 | if ( ! empty( $whitelist ) ) { |
||
| 414 | $whitelist = array(); |
||
| 415 | jetpack_protect_save_whitelist( $whitelist ); |
||
| 416 | WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); |
||
| 417 | } else { |
||
| 418 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 419 | } |
||
| 420 | break; |
||
| 421 | } |
||
| 422 | |||
| 423 | // Append new IP to whitelist array |
||
| 424 | array_push( $whitelist, $new_ip ); |
||
| 425 | |||
| 426 | // Save whitelist if there are no errors |
||
| 427 | $result = jetpack_protect_save_whitelist( $whitelist ); |
||
| 428 | if ( is_wp_error( $result ) ) { |
||
| 429 | WP_CLI::error( __( $result, 'jetpack' ) ); |
||
| 430 | } |
||
| 431 | |||
| 432 | WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); |
||
| 433 | break; |
||
| 434 | case 'prompt': |
||
| 435 | WP_CLI::error( |
||
| 436 | __( 'No command found.', 'jetpack' ) . "\n" . |
||
| 437 | __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . |
||
| 438 | _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" . |
||
| 439 | _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
||
| 440 | ); |
||
| 441 | break; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Manage Jetpack Options |
||
| 447 | * |
||
| 448 | * ## OPTIONS |
||
| 449 | * |
||
| 450 | * list : List all jetpack options and their values |
||
| 451 | * delete : Delete an option |
||
| 452 | * - can only delete options that are white listed. |
||
| 453 | * update : update an option |
||
| 454 | * - can only update option strings |
||
| 455 | * get : get the value of an option |
||
| 456 | * |
||
| 457 | * ## EXAMPLES |
||
| 458 | * |
||
| 459 | * wp jetpack options list |
||
| 460 | * wp jetpack options get <option_name> |
||
| 461 | * wp jetpack options delete <option_name> |
||
| 462 | * wp jetpack options update <option_name> [<option_value>] |
||
| 463 | * |
||
| 464 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 465 | */ |
||
| 466 | public function options( $args, $assoc_args ) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Get the status of or start a new Jetpack sync. |
||
| 565 | * |
||
| 566 | * ## OPTIONS |
||
| 567 | * |
||
| 568 | * status : Print the current sync status |
||
| 569 | * start : Start a full sync from this site to WordPress.com |
||
| 570 | * |
||
| 571 | * ## EXAMPLES |
||
| 572 | * |
||
| 573 | * wp jetpack sync status |
||
| 574 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
| 575 | * |
||
| 576 | * @synopsis <status|start> [--<field>=<value>] |
||
| 577 | */ |
||
| 578 | public function sync( $args, $assoc_args ) { |
||
| 579 | if ( ! Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 580 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) ); |
||
| 581 | } |
||
| 582 | |||
| 583 | $action = isset( $args[0] ) ? $args[0] : 'status'; |
||
| 584 | |||
| 585 | switch ( $action ) { |
||
| 586 | case 'status': |
||
| 587 | $status = Jetpack_Sync_Actions::get_sync_status(); |
||
| 588 | $collection = array(); |
||
| 589 | foreach ( $status as $key => $item ) { |
||
| 590 | $collection[] = array( |
||
| 591 | 'option' => $key, |
||
| 592 | 'value' => is_scalar( $item ) ? $item : json_encode( $item ) |
||
| 593 | ); |
||
| 594 | } |
||
| 595 | |||
| 596 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
||
| 597 | break; |
||
| 598 | case 'start': |
||
| 599 | // Get the original settings so that we can restore them later |
||
| 600 | $original_settings = Jetpack_Sync_Settings::get_settings(); |
||
| 601 | |||
| 602 | // Initialize sync settigns so we can sync as quickly as possible |
||
| 603 | $sync_settings = wp_parse_args( |
||
| 604 | array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ), |
||
| 605 | array( |
||
| 606 | 'sync_wait_time' => 0, |
||
| 607 | 'enqueue_wait_time' => 0, |
||
| 608 | 'queue_max_writes_sec' => 10000, |
||
| 609 | 'max_queue_size_full_sync' => 100000 |
||
| 610 | ) |
||
| 611 | ); |
||
| 612 | Jetpack_Sync_Settings::update_settings( $sync_settings ); |
||
| 613 | |||
| 614 | // Convert comma-delimited string of modules to an array |
||
| 615 | if ( ! empty( $assoc_args['modules'] ) ) { |
||
| 616 | $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) ); |
||
| 617 | |||
| 618 | // Convert the array so that the keys are the module name and the value is true to indicate |
||
| 619 | // that we want to sync the module |
||
| 620 | $modules = array_map( '__return_true', array_flip( $modules ) ); |
||
| 621 | } |
||
| 622 | |||
| 623 | View Code Duplication | foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
|
| 624 | if ( |
||
| 625 | 'users' === $module_name && |
||
| 626 | isset( $assoc_args[ $module_name ] ) && |
||
| 627 | 'initial' === $assoc_args[ $module_name ] |
||
| 628 | ) { |
||
| 629 | $modules[ 'users' ] = 'initial'; |
||
| 630 | } elseif ( isset( $assoc_args[ $module_name ] ) ) { |
||
| 631 | $ids = explode( ',', $assoc_args[ $module_name ] ); |
||
| 632 | if ( count( $ids ) > 0 ) { |
||
| 633 | $modules[ $module_name ] = $ids; |
||
| 634 | } |
||
| 635 | } |
||
| 636 | } |
||
| 637 | |||
| 638 | if ( empty( $modules ) ) { |
||
| 639 | $modules = null; |
||
| 640 | } |
||
| 641 | |||
| 642 | // Kick off a full sync |
||
| 643 | if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) { |
||
| 644 | if ( $modules ) { |
||
| 645 | WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: ', 'jetpack' ), join( ', ', $modules ) ) ); |
||
| 646 | } else { |
||
| 647 | WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) ); |
||
| 648 | } |
||
| 649 | View Code Duplication | } else { |
|
| 650 | |||
| 651 | // Reset sync settings to original. |
||
| 652 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 653 | |||
| 654 | if ( $modules ) { |
||
| 655 | WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) ); |
||
| 656 | } else { |
||
| 657 | WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) ); |
||
| 658 | } |
||
| 659 | } |
||
| 660 | |||
| 661 | // Keep sending to WPCOM until there's nothing to send |
||
| 662 | $i = 1; |
||
| 663 | do { |
||
| 664 | $result = Jetpack_Sync_Actions::$sender->do_full_sync(); |
||
| 665 | if ( $result ) { |
||
| 666 | if ( 1 == $i++ ) { |
||
| 667 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
||
| 668 | } else { |
||
| 669 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
||
| 670 | } |
||
| 671 | } |
||
| 672 | } while ( $result ); |
||
| 673 | |||
| 674 | // Reset sync settings to original. |
||
| 675 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 676 | |||
| 677 | WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) ); |
||
| 678 | break; |
||
| 679 | } |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * List the contents of a specific Jetpack sync queue. |
||
| 684 | * |
||
| 685 | * ## OPTIONS |
||
| 686 | * |
||
| 687 | * peek : List the 100 front-most items on the queue. |
||
| 688 | * |
||
| 689 | * ## EXAMPLES |
||
| 690 | * |
||
| 691 | * wp jetpack sync_queue full_sync peek |
||
| 692 | * |
||
| 693 | * @synopsis <incremental|full_sync> <peek> |
||
| 694 | */ |
||
| 695 | public function sync_queue( $args, $assoc_args ) { |
||
| 745 | } |
||
| 746 | |||
| 747 | /* |
||
| 782 | } |
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: