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 ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Tests the active connection |
||
| 96 | * |
||
| 97 | * Does a two-way test to verify that the local site can communicate with remote Jetpack/WP.com servers and that Jetpack/WP.com servers can talk to the local site. |
||
| 98 | * |
||
| 99 | * ## EXAMPLES |
||
| 100 | * |
||
| 101 | * wp jetpack test-connection |
||
| 102 | * |
||
| 103 | * @subcommand test-connection |
||
| 104 | */ |
||
| 105 | public function test_connection( $args, $assoc_args ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Disconnect Jetpack Blogs or Users |
||
| 138 | * |
||
| 139 | * ## OPTIONS |
||
| 140 | * |
||
| 141 | * blog: Disconnect the entire blog. |
||
| 142 | * |
||
| 143 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 144 | * |
||
| 145 | * Please note, the primary account that the blog is connected |
||
| 146 | * to WordPress.com with cannot be disconnected without |
||
| 147 | * disconnecting the entire blog. |
||
| 148 | * |
||
| 149 | * ## EXAMPLES |
||
| 150 | * |
||
| 151 | * wp jetpack disconnect blog |
||
| 152 | * wp jetpack disconnect user 13 |
||
| 153 | * wp jetpack disconnect user username |
||
| 154 | * wp jetpack disconnect user [email protected] |
||
| 155 | * |
||
| 156 | * @synopsis <blog|user> [<user_identifier>] |
||
| 157 | */ |
||
| 158 | public function disconnect( $args, $assoc_args ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Reset Jetpack options and settings to default |
||
| 213 | * |
||
| 214 | * ## OPTIONS |
||
| 215 | * |
||
| 216 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 217 | * |
||
| 218 | * options: Resets all Jetpack options except: |
||
| 219 | * - All private options (Blog token, user token, etc...) |
||
| 220 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 221 | * - master_user |
||
| 222 | * - version |
||
| 223 | * - activated |
||
| 224 | * |
||
| 225 | * ## EXAMPLES |
||
| 226 | * |
||
| 227 | * wp jetpack reset options |
||
| 228 | * wp jetpack reset modules |
||
| 229 | * |
||
| 230 | * @synopsis <modules|options> |
||
| 231 | */ |
||
| 232 | public function reset( $args, $assoc_args ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Manage Jetpack Modules |
||
| 290 | * |
||
| 291 | * ## OPTIONS |
||
| 292 | * |
||
| 293 | * list : View all available modules, and their status. |
||
| 294 | * activate all : Activate all modules |
||
| 295 | * deactivate all: Deactivate all modules |
||
| 296 | * |
||
| 297 | * activate <module_slug> : Activate a module. |
||
| 298 | * deactivate <module_slug> : Deactivate a module. |
||
| 299 | * toggle <module_slug> : Toggle a module on or off. |
||
| 300 | * |
||
| 301 | * ## EXAMPLES |
||
| 302 | * |
||
| 303 | * wp jetpack module list |
||
| 304 | * wp jetpack module activate stats |
||
| 305 | * wp jetpack module deactivate stats |
||
| 306 | * wp jetpack module toggle stats |
||
| 307 | * |
||
| 308 | * wp jetpack module activate all |
||
| 309 | * wp jetpack module deactivate all |
||
| 310 | * |
||
| 311 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
| 312 | */ |
||
| 313 | public function module( $args, $assoc_args ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Manage Protect Settings |
||
| 383 | * |
||
| 384 | * ## OPTIONS |
||
| 385 | * |
||
| 386 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 387 | * |
||
| 388 | * |
||
| 389 | * ## EXAMPLES |
||
| 390 | * |
||
| 391 | * wp jetpack protect whitelist <ip address> |
||
| 392 | * wp jetpack protect whitelist list |
||
| 393 | * wp jetpack protect whitelist clear |
||
| 394 | * |
||
| 395 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 396 | */ |
||
| 397 | public function protect( $args, $assoc_args ) { |
||
| 398 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 399 | if ( ! in_array( $action, array( 'whitelist' ) ) ) { |
||
| 400 | /* translators: %s is a command like "prompt" */ |
||
| 401 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 402 | } |
||
| 403 | // Check if module is active |
||
| 404 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
||
| 405 | 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__ ) ); |
||
| 406 | } |
||
| 407 | if ( in_array( $action, array( 'whitelist' ) ) ) { |
||
| 408 | if ( isset( $args[1] ) ) { |
||
| 409 | $action = 'whitelist'; |
||
| 410 | } else { |
||
| 411 | $action = 'prompt'; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | switch ( $action ) { |
||
| 415 | case 'whitelist': |
||
| 416 | $whitelist = array(); |
||
| 417 | $new_ip = $args[1]; |
||
| 418 | $current_whitelist = get_site_option( 'jetpack_protect_whitelist', array() ); |
||
| 419 | |||
| 420 | // Build array of IPs that are already whitelisted. |
||
| 421 | // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get |
||
| 422 | // low & high range params for jetpack_protect_ip_address_is_in_range(); |
||
| 423 | foreach( $current_whitelist as $whitelisted ) { |
||
| 424 | |||
| 425 | // IP ranges |
||
| 426 | if ( $whitelisted->range ) { |
||
| 427 | |||
| 428 | // Is it already whitelisted? |
||
| 429 | if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { |
||
| 430 | /* translators: %s is an IP address */ |
||
| 431 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 432 | break; |
||
| 433 | } |
||
| 434 | $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; |
||
| 435 | |||
| 436 | } else { // Individual IPs |
||
| 437 | |||
| 438 | // Check if the IP is already whitelisted (single IP only) |
||
| 439 | if ( $new_ip == $whitelisted->ip_address ) { |
||
| 440 | /* translators: %s is an IP address */ |
||
| 441 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 442 | break; |
||
| 443 | } |
||
| 444 | $whitelist[] = $whitelisted->ip_address; |
||
| 445 | |||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /* |
||
| 450 | * List the whitelist |
||
| 451 | * Done here because it's easier to read the $whitelist array after it's been rebuilt |
||
| 452 | */ |
||
| 453 | if ( isset( $args[1] ) && 'list' == $args[1] ) { |
||
| 454 | if ( ! empty( $whitelist ) ) { |
||
| 455 | WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); |
||
| 456 | foreach ( $whitelist as $ip ) { |
||
| 457 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; |
||
| 458 | } |
||
| 459 | } else { |
||
| 460 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 461 | } |
||
| 462 | break; |
||
| 463 | } |
||
| 464 | |||
| 465 | /* |
||
| 466 | * Clear the whitelist |
||
| 467 | */ |
||
| 468 | if ( isset( $args[1] ) && 'clear' == $args[1] ) { |
||
| 469 | View Code Duplication | if ( ! empty( $whitelist ) ) { |
|
| 470 | $whitelist = array(); |
||
| 471 | jetpack_protect_save_whitelist( $whitelist ); |
||
| 472 | WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); |
||
| 473 | } else { |
||
| 474 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 475 | } |
||
| 476 | break; |
||
| 477 | } |
||
| 478 | |||
| 479 | // Append new IP to whitelist array |
||
| 480 | array_push( $whitelist, $new_ip ); |
||
| 481 | |||
| 482 | // Save whitelist if there are no errors |
||
| 483 | $result = jetpack_protect_save_whitelist( $whitelist ); |
||
| 484 | if ( is_wp_error( $result ) ) { |
||
| 485 | WP_CLI::error( __( $result, 'jetpack' ) ); |
||
| 486 | } |
||
| 487 | |||
| 488 | /* translators: %s is an IP address */ |
||
| 489 | WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); |
||
| 490 | break; |
||
| 491 | case 'prompt': |
||
| 492 | WP_CLI::error( |
||
| 493 | __( 'No command found.', 'jetpack' ) . "\n" . |
||
| 494 | __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . |
||
| 495 | _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" . |
||
| 496 | _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
||
| 497 | ); |
||
| 498 | break; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Manage Jetpack Options |
||
| 504 | * |
||
| 505 | * ## OPTIONS |
||
| 506 | * |
||
| 507 | * list : List all jetpack options and their values |
||
| 508 | * delete : Delete an option |
||
| 509 | * - can only delete options that are white listed. |
||
| 510 | * update : update an option |
||
| 511 | * - can only update option strings |
||
| 512 | * get : get the value of an option |
||
| 513 | * |
||
| 514 | * ## EXAMPLES |
||
| 515 | * |
||
| 516 | * wp jetpack options list |
||
| 517 | * wp jetpack options get <option_name> |
||
| 518 | * wp jetpack options delete <option_name> |
||
| 519 | * wp jetpack options update <option_name> [<option_value>] |
||
| 520 | * |
||
| 521 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 522 | */ |
||
| 523 | public function options( $args, $assoc_args ) { |
||
| 524 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
| 525 | $safe_to_modify = Jetpack_Options::get_options_for_reset(); |
||
| 526 | |||
| 527 | // Jumpstart is special |
||
| 528 | array_push( $safe_to_modify, 'jumpstart' ); |
||
| 529 | |||
| 530 | // Is the option flagged as unsafe? |
||
| 531 | $flagged = ! in_array( $args[1], $safe_to_modify ); |
||
| 532 | |||
| 533 | View Code Duplication | if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) { |
|
| 534 | /* translators: %s is a command like "prompt" */ |
||
| 535 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 536 | } |
||
| 537 | |||
| 538 | if ( isset( $args[0] ) ) { |
||
| 539 | if ( 'get' == $args[0] && isset( $args[1] ) ) { |
||
| 540 | $action = 'get'; |
||
| 541 | } else if ( 'delete' == $args[0] && isset( $args[1] ) ) { |
||
| 542 | $action = 'delete'; |
||
| 543 | View Code Duplication | } else if ( 'update' == $args[0] && isset( $args[1] ) ) { |
|
| 544 | $action = 'update'; |
||
| 545 | } else { |
||
| 546 | $action = 'list'; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | // Bail if the option isn't found |
||
| 551 | $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false; |
||
| 552 | View Code Duplication | if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) { |
|
| 553 | WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); |
||
| 554 | } |
||
| 555 | |||
| 556 | // Let's print_r the option if it's an array |
||
| 557 | // Used in the 'get' and 'list' actions |
||
| 558 | $option = is_array( $option ) ? print_r( $option ) : $option; |
||
| 559 | |||
| 560 | switch ( $action ) { |
||
| 561 | case 'get': |
||
| 562 | WP_CLI::success( "\t" . $option ); |
||
| 563 | break; |
||
| 564 | case 'delete': |
||
| 565 | jetpack_cli_are_you_sure( $flagged ); |
||
| 566 | |||
| 567 | Jetpack_Options::delete_option( $args[1] ); |
||
| 568 | WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) ); |
||
| 569 | break; |
||
| 570 | case 'update': |
||
| 571 | jetpack_cli_are_you_sure( $flagged ); |
||
| 572 | |||
| 573 | // Updating arrays would get pretty tricky... |
||
| 574 | $value = Jetpack_Options::get_option( $args[1] ); |
||
| 575 | if ( $value && is_array( $value ) ) { |
||
| 576 | WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) ); |
||
| 577 | } |
||
| 578 | |||
| 579 | Jetpack_Options::update_option( $args[1], $args[2] ); |
||
| 580 | WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) ); |
||
| 581 | break; |
||
| 582 | case 'list': |
||
| 583 | $options_compact = Jetpack_Options::get_option_names(); |
||
| 584 | $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' ); |
||
| 585 | $options_private = Jetpack_Options::get_option_names( 'private' ); |
||
| 586 | $options = array_merge( $options_compact, $options_non_compact, $options_private ); |
||
| 587 | |||
| 588 | // Table headers |
||
| 589 | WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) ); |
||
| 590 | |||
| 591 | // List out the options and their values |
||
| 592 | // Tell them if the value is empty or not |
||
| 593 | // Tell them if it's an array |
||
| 594 | foreach ( $options as $option ) { |
||
| 595 | $value = Jetpack_Options::get_option( $option ); |
||
| 596 | if ( ! $value ) { |
||
| 597 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' ); |
||
| 598 | continue; |
||
| 599 | } |
||
| 600 | |||
| 601 | if ( ! is_array( $value ) ) { |
||
| 602 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value ); |
||
| 603 | } else if ( is_array( $value ) ) { |
||
| 604 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' ); |
||
| 605 | } |
||
| 606 | } |
||
| 607 | $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}'; |
||
| 608 | $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}'; |
||
| 609 | |||
| 610 | WP_CLI::success( |
||
| 611 | _x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" . |
||
| 612 | str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" . |
||
| 613 | str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" . |
||
| 614 | str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" . |
||
| 615 | _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n" |
||
| 616 | ); |
||
| 617 | break; |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get the status of or start a new Jetpack sync. |
||
| 623 | * |
||
| 624 | * ## OPTIONS |
||
| 625 | * |
||
| 626 | * status : Print the current sync status |
||
| 627 | * start : Start a full sync from this site to WordPress.com |
||
| 628 | * |
||
| 629 | * ## EXAMPLES |
||
| 630 | * |
||
| 631 | * wp jetpack sync status |
||
| 632 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
| 633 | * |
||
| 634 | * @synopsis <status|start> [--<field>=<value>] |
||
| 635 | */ |
||
| 636 | public function sync( $args, $assoc_args ) { |
||
| 637 | if ( ! Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 638 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) ); |
||
| 639 | } |
||
| 640 | |||
| 641 | $action = isset( $args[0] ) ? $args[0] : 'status'; |
||
| 642 | |||
| 643 | switch ( $action ) { |
||
| 644 | case 'status': |
||
| 645 | $status = Jetpack_Sync_Actions::get_sync_status(); |
||
| 646 | $collection = array(); |
||
| 647 | foreach ( $status as $key => $item ) { |
||
| 648 | $collection[] = array( |
||
| 649 | 'option' => $key, |
||
| 650 | 'value' => is_scalar( $item ) ? $item : json_encode( $item ) |
||
| 651 | ); |
||
| 652 | } |
||
| 653 | |||
| 654 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
||
| 655 | break; |
||
| 656 | case 'start': |
||
| 657 | // Get the original settings so that we can restore them later |
||
| 658 | $original_settings = Jetpack_Sync_Settings::get_settings(); |
||
| 659 | |||
| 660 | // Initialize sync settigns so we can sync as quickly as possible |
||
| 661 | $sync_settings = wp_parse_args( |
||
| 662 | array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ), |
||
| 663 | array( |
||
| 664 | 'sync_wait_time' => 0, |
||
| 665 | 'enqueue_wait_time' => 0, |
||
| 666 | 'queue_max_writes_sec' => 10000, |
||
| 667 | 'max_queue_size_full_sync' => 100000 |
||
| 668 | ) |
||
| 669 | ); |
||
| 670 | Jetpack_Sync_Settings::update_settings( $sync_settings ); |
||
| 671 | |||
| 672 | // Convert comma-delimited string of modules to an array |
||
| 673 | View Code Duplication | if ( ! empty( $assoc_args['modules'] ) ) { |
|
| 674 | $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) ); |
||
| 675 | |||
| 676 | // Convert the array so that the keys are the module name and the value is true to indicate |
||
| 677 | // that we want to sync the module |
||
| 678 | $modules = array_map( '__return_true', array_flip( $modules ) ); |
||
| 679 | } |
||
| 680 | |||
| 681 | View Code Duplication | foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
|
| 682 | if ( |
||
| 683 | 'users' === $module_name && |
||
| 684 | isset( $assoc_args[ $module_name ] ) && |
||
| 685 | 'initial' === $assoc_args[ $module_name ] |
||
| 686 | ) { |
||
| 687 | $modules[ 'users' ] = 'initial'; |
||
| 688 | } elseif ( isset( $assoc_args[ $module_name ] ) ) { |
||
| 689 | $ids = explode( ',', $assoc_args[ $module_name ] ); |
||
| 690 | if ( count( $ids ) > 0 ) { |
||
| 691 | $modules[ $module_name ] = $ids; |
||
| 692 | } |
||
| 693 | } |
||
| 694 | } |
||
| 695 | |||
| 696 | if ( empty( $modules ) ) { |
||
| 697 | $modules = null; |
||
| 698 | } |
||
| 699 | |||
| 700 | // Kick off a full sync |
||
| 701 | if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) { |
||
| 702 | View Code Duplication | if ( $modules ) { |
|
| 703 | WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) ); |
||
| 704 | } else { |
||
| 705 | WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) ); |
||
| 706 | } |
||
| 707 | View Code Duplication | } else { |
|
| 708 | |||
| 709 | // Reset sync settings to original. |
||
| 710 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 711 | |||
| 712 | if ( $modules ) { |
||
| 713 | WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) ); |
||
| 714 | } else { |
||
| 715 | WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) ); |
||
| 716 | } |
||
| 717 | } |
||
| 718 | |||
| 719 | // Keep sending to WPCOM until there's nothing to send |
||
| 720 | $i = 1; |
||
| 721 | do { |
||
| 722 | $result = Jetpack_Sync_Actions::$sender->do_full_sync(); |
||
| 723 | if ( is_wp_error( $result ) ) { |
||
| 724 | $queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() ); |
||
| 725 | if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) { |
||
| 726 | WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) ); |
||
| 727 | } |
||
| 728 | } else { |
||
| 729 | if ( 1 == $i ) { |
||
| 730 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
||
| 731 | } else { |
||
| 732 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
||
| 733 | } |
||
| 734 | } |
||
| 735 | $i++; |
||
| 736 | } while ( $result && ! is_wp_error( $result ) ); |
||
| 737 | |||
| 738 | // Reset sync settings to original. |
||
| 739 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 740 | |||
| 741 | WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) ); |
||
| 742 | break; |
||
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * List the contents of a specific Jetpack sync queue. |
||
| 748 | * |
||
| 749 | * ## OPTIONS |
||
| 750 | * |
||
| 751 | * peek : List the 100 front-most items on the queue. |
||
| 752 | * |
||
| 753 | * ## EXAMPLES |
||
| 754 | * |
||
| 755 | * wp jetpack sync_queue full_sync peek |
||
| 756 | * |
||
| 757 | * @synopsis <incremental|full_sync> <peek> |
||
| 758 | */ |
||
| 759 | public function sync_queue( $args, $assoc_args ) { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
| 813 | * |
||
| 814 | * Returns success or error JSON |
||
| 815 | * |
||
| 816 | * <token_json> |
||
| 817 | * : JSON blob of WPCOM API token |
||
| 818 | */ |
||
| 819 | public function partner_cancel( $args, $named_args ) { |
||
| 820 | list( $token_json ) = $args; |
||
| 821 | |||
| 822 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 823 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 824 | } |
||
| 825 | |||
| 826 | if ( isset( $token->error ) ) { |
||
| 827 | $this->partner_provision_error( new WP_Error( $token->error, $token->message ) ); |
||
| 828 | } |
||
| 829 | |||
| 830 | if ( ! isset( $token->access_token ) ) { |
||
| 831 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 832 | } |
||
| 833 | |||
| 834 | $site_identifier = Jetpack_Options::get_option( 'id' ); |
||
| 835 | |||
| 836 | if ( ! $site_identifier ) { |
||
| 837 | $site_identifier = Jetpack::build_raw_urls( get_home_url() ); |
||
| 838 | } |
||
| 839 | |||
| 840 | $request = array( |
||
| 841 | 'headers' => array( |
||
| 842 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 843 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 844 | ), |
||
| 845 | 'timeout' => 60, |
||
| 846 | 'method' => 'POST', |
||
| 847 | ); |
||
| 848 | |||
| 849 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier ); |
||
| 850 | |||
| 851 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 852 | |||
| 853 | Jetpack_Options::delete_option( 'onboarding' ); |
||
| 854 | |||
| 855 | if ( is_wp_error( $result ) ) { |
||
| 856 | $this->partner_provision_error( $result ); |
||
| 857 | } |
||
| 858 | |||
| 859 | WP_CLI::log( wp_remote_retrieve_body( $result ) ); |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Provision a site using a Jetpack Partner license |
||
| 864 | * |
||
| 865 | * Returns JSON blob |
||
| 866 | * |
||
| 867 | * ## OPTIONS |
||
| 868 | * |
||
| 869 | * <token_json> |
||
| 870 | * : JSON blob of WPCOM API token |
||
| 871 | * --user_id=<user_id> |
||
| 872 | * : Local ID of user to connect as (if omitted, user will be required to redirect via wp-admin) |
||
| 873 | * [--plan=<plan_name>] |
||
| 874 | * : Slug of the requested plan, e.g. premium |
||
| 875 | * [--wpcom_user_id=<user_id>] |
||
| 876 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
| 877 | * [--onboarding=<onboarding>] |
||
| 878 | * : Guide the user through an onboarding wizard |
||
| 879 | * [--force_register=<register>] |
||
| 880 | * : Whether to force a site to register |
||
| 881 | * [--force_connect=<force_connect>] |
||
| 882 | * : Force JPS to not reuse existing credentials |
||
| 883 | * |
||
| 884 | * ## EXAMPLES |
||
| 885 | * |
||
| 886 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 887 | * { success: true } |
||
| 888 | * |
||
| 889 | * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--onboarding=<onboarding>] [--force_register=<register>] [--force_connect=<force_connect>] |
||
| 890 | */ |
||
| 891 | public function partner_provision( $args, $named_args ) { |
||
| 892 | list( $token_json ) = $args; |
||
| 893 | |||
| 894 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 895 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 896 | } |
||
| 897 | |||
| 898 | if ( isset( $token->error ) ) { |
||
| 899 | $message = isset( $token->message ) |
||
| 900 | ? $token->message |
||
| 901 | : ''; |
||
| 902 | $this->partner_provision_error( new WP_Error( $token->error, $message ) ); |
||
| 903 | } |
||
| 904 | |||
| 905 | if ( ! isset( $token->access_token ) ) { |
||
| 906 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 907 | } |
||
| 908 | |||
| 909 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 910 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 911 | |||
| 912 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 913 | // this code mostly copied from Jetpack::admin_page_load |
||
| 914 | Jetpack::maybe_set_version_option(); |
||
| 915 | $registered = Jetpack::try_registration(); |
||
| 916 | if ( is_wp_error( $registered ) ) { |
||
| 917 | $this->partner_provision_error( $registered ); |
||
| 918 | } elseif ( ! $registered ) { |
||
| 919 | $this->partner_provision_error( new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ) ); |
||
| 920 | } |
||
| 921 | |||
| 922 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 923 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 924 | } |
||
| 925 | |||
| 926 | // if the user isn't specified, but we have a current master user, then set that to current user |
||
| 927 | if ( ! get_current_user_id() && $master_user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
| 928 | wp_set_current_user( $master_user_id ); |
||
| 929 | } |
||
| 930 | |||
| 931 | $site_icon = ( function_exists( 'has_site_icon') && has_site_icon() ) |
||
| 932 | ? get_site_icon_url() |
||
| 933 | : false; |
||
| 934 | |||
| 935 | $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); |
||
| 936 | |||
| 937 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 938 | if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { |
||
| 939 | $redirect_uri = add_query_arg( |
||
| 940 | array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ), |
||
| 941 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 942 | ); |
||
| 943 | } else { |
||
| 944 | $redirect_uri = admin_url(); |
||
| 945 | } |
||
| 946 | |||
| 947 | $request_body = array( |
||
| 948 | 'jp_version' => JETPACK__VERSION, |
||
| 949 | 'redirect_uri' => $redirect_uri |
||
| 950 | ); |
||
| 951 | |||
| 952 | if ( $site_icon ) { |
||
| 953 | $request_body['site_icon'] = $site_icon; |
||
| 954 | } |
||
| 955 | |||
| 956 | if ( get_current_user_id() ) { |
||
| 957 | $user = wp_get_current_user(); |
||
| 958 | |||
| 959 | // role |
||
| 960 | $role = Jetpack::translate_current_user_to_role(); |
||
| 961 | $signed_role = Jetpack::sign_role( $role ); |
||
| 962 | |||
| 963 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 964 | |||
| 965 | // Jetpack auth stuff |
||
| 966 | $request_body['scope'] = $signed_role; |
||
| 967 | $request_body['secret'] = $secrets['secret_1']; |
||
| 968 | |||
| 969 | // User stuff |
||
| 970 | $request_body['user_id'] = $user->ID; |
||
| 971 | $request_body['user_email'] = $user->user_email; |
||
| 972 | $request_body['user_login'] = $user->user_login; |
||
| 973 | } |
||
| 974 | |||
| 975 | // optional additional params |
||
| 976 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 977 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 978 | } |
||
| 979 | |||
| 980 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 981 | $request_body['plan'] = $named_args['plan']; |
||
| 982 | } |
||
| 983 | |||
| 984 | View Code Duplication | if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { |
|
| 985 | $request_body['onboarding'] = intval( $named_args['onboarding'] ); |
||
| 986 | } |
||
| 987 | |||
| 988 | View Code Duplication | if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) { |
|
| 989 | $request_body['force_connect'] = intval( $named_args['force_connect'] ); |
||
| 990 | } |
||
| 991 | |||
| 992 | if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) { |
||
| 993 | Jetpack::create_onboarding_token(); |
||
| 994 | } |
||
| 995 | |||
| 996 | $request = array( |
||
| 997 | 'headers' => array( |
||
| 998 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 999 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 1000 | ), |
||
| 1001 | 'timeout' => 60, |
||
| 1002 | 'method' => 'POST', |
||
| 1003 | 'body' => json_encode( $request_body ) |
||
| 1004 | ); |
||
| 1005 | |||
| 1006 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-provision', $this->get_api_host(), $blog_id ); |
||
| 1007 | |||
| 1008 | // add calypso env if set |
||
| 1009 | if ( getenv( 'CALYPSO_ENV' ) ) { |
||
| 1010 | $url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url ); |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 1014 | |||
| 1015 | if ( is_wp_error( $result ) ) { |
||
| 1016 | $this->partner_provision_error( $result ); |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | $response_code = wp_remote_retrieve_response_code( $result ); |
||
| 1020 | $body_json = json_decode( wp_remote_retrieve_body( $result ) ); |
||
| 1021 | |||
| 1022 | if( 200 !== $response_code ) { |
||
| 1023 | if ( isset( $body_json->error ) ) { |
||
| 1024 | $this->partner_provision_error( new WP_Error( $body_json->error, $body_json->message ) ); |
||
| 1025 | } else { |
||
| 1026 | $this->partner_provision_error( new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) ) ); |
||
| 1027 | } |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | if ( isset( $body_json->access_token ) ) { |
||
| 1031 | // authorize user and enable SSO |
||
| 1032 | Jetpack::update_user_token( $user->ID, sprintf( '%s.%d', $body_json->access_token, $user->ID ), true ); |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Auto-enable SSO module for new Jetpack Start connections |
||
| 1036 | * |
||
| 1037 | * @since 5.0.0 |
||
| 1038 | * |
||
| 1039 | * @param bool $enable_sso Whether to enable the SSO module. Default to true. |
||
| 1040 | */ |
||
| 1041 | $other_modules = apply_filters( 'jetpack_start_enable_sso', true ) |
||
| 1042 | ? array( 'sso' ) |
||
| 1043 | : array(); |
||
| 1044 | |||
| 1045 | View Code Duplication | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
|
| 1046 | Jetpack::delete_active_modules(); |
||
| 1047 | Jetpack::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), false ); |
||
| 1048 | } else { |
||
| 1049 | Jetpack::activate_default_modules( false, false, $other_modules, false ); |
||
| 1050 | } |
||
| 1051 | } |
||
| 1052 | |||
| 1053 | WP_CLI::log( json_encode( $body_json ) ); |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | private function get_api_host() { |
||
| 1060 | |||
| 1061 | private function partner_provision_error( $error ) { |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | /* |
||
| 1107 |
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: