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 | 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 ) { |
||
| 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 ) { |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Manages your Jetpack sitemap |
||
| 1058 | * |
||
| 1059 | * ## OPTIONS |
||
| 1060 | * |
||
| 1061 | * rebuild : Rebuild all sitemaps |
||
| 1062 | * --purge : if set, will remove all existing sitemap data before rebuilding |
||
| 1063 | * |
||
| 1064 | * ## EXAMPLES |
||
| 1065 | * |
||
| 1066 | * wp jetpack sitemap rebuild |
||
| 1067 | * |
||
| 1068 | * @subcommand sitemap |
||
| 1069 | * @synopsis <rebuild> [--purge] |
||
| 1070 | */ |
||
| 1071 | public function sitemap( $args, $assoc_args ) { |
||
| 1090 | |||
| 1091 | private function get_api_host() { |
||
| 1095 | |||
| 1096 | private function partner_provision_error( $error ) { |
||
| 1104 | } |
||
| 1105 | |||
| 1142 |
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: