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 | * Disconnect Jetpack Blogs or Users |
||
| 96 | * |
||
| 97 | * ## OPTIONS |
||
| 98 | * |
||
| 99 | * blog: Disconnect the entire blog. |
||
| 100 | * |
||
| 101 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 102 | * |
||
| 103 | * Please note, the primary account that the blog is connected |
||
| 104 | * to WordPress.com with cannot be disconnected without |
||
| 105 | * disconnecting the entire blog. |
||
| 106 | * |
||
| 107 | * ## EXAMPLES |
||
| 108 | * |
||
| 109 | * wp jetpack disconnect blog |
||
| 110 | * wp jetpack disconnect user 13 |
||
| 111 | * wp jetpack disconnect user username |
||
| 112 | * wp jetpack disconnect user [email protected] |
||
| 113 | * |
||
| 114 | * @synopsis <blog|user> [<user_identifier>] |
||
| 115 | */ |
||
| 116 | public function disconnect( $args, $assoc_args ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Reset Jetpack options and settings to default |
||
| 171 | * |
||
| 172 | * ## OPTIONS |
||
| 173 | * |
||
| 174 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 175 | * |
||
| 176 | * options: Resets all Jetpack options except: |
||
| 177 | * - All private options (Blog token, user token, etc...) |
||
| 178 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 179 | * - master_user |
||
| 180 | * - version |
||
| 181 | * - activated |
||
| 182 | * |
||
| 183 | * ## EXAMPLES |
||
| 184 | * |
||
| 185 | * wp jetpack reset options |
||
| 186 | * wp jetpack reset modules |
||
| 187 | * |
||
| 188 | * @synopsis <modules|options> |
||
| 189 | */ |
||
| 190 | public function reset( $args, $assoc_args ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Manage Jetpack Modules |
||
| 248 | * |
||
| 249 | * ## OPTIONS |
||
| 250 | * |
||
| 251 | * list : View all available modules, and their status. |
||
| 252 | * activate all : Activate all modules |
||
| 253 | * deactivate all: Deactivate all modules |
||
| 254 | * |
||
| 255 | * activate <module_slug> : Activate a module. |
||
| 256 | * deactivate <module_slug> : Deactivate a module. |
||
| 257 | * toggle <module_slug> : Toggle a module on or off. |
||
| 258 | * |
||
| 259 | * ## EXAMPLES |
||
| 260 | * |
||
| 261 | * wp jetpack module list |
||
| 262 | * wp jetpack module activate stats |
||
| 263 | * wp jetpack module deactivate stats |
||
| 264 | * wp jetpack module toggle stats |
||
| 265 | * |
||
| 266 | * wp jetpack module activate all |
||
| 267 | * wp jetpack module deactivate all |
||
| 268 | * |
||
| 269 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
| 270 | */ |
||
| 271 | public function module( $args, $assoc_args ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Manage Protect Settings |
||
| 341 | * |
||
| 342 | * ## OPTIONS |
||
| 343 | * |
||
| 344 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 345 | * |
||
| 346 | * |
||
| 347 | * ## EXAMPLES |
||
| 348 | * |
||
| 349 | * wp jetpack protect whitelist <ip address> |
||
| 350 | * wp jetpack protect whitelist list |
||
| 351 | * wp jetpack protect whitelist clear |
||
| 352 | * |
||
| 353 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 354 | */ |
||
| 355 | public function protect( $args, $assoc_args ) { |
||
| 356 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 357 | if ( ! in_array( $action, array( 'whitelist' ) ) ) { |
||
| 358 | /* translators: %s is a command like "prompt" */ |
||
| 359 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 360 | } |
||
| 361 | // Check if module is active |
||
| 362 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
||
| 363 | 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__ ) ); |
||
| 364 | } |
||
| 365 | if ( in_array( $action, array( 'whitelist' ) ) ) { |
||
| 366 | if ( isset( $args[1] ) ) { |
||
| 367 | $action = 'whitelist'; |
||
| 368 | } else { |
||
| 369 | $action = 'prompt'; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | switch ( $action ) { |
||
| 373 | case 'whitelist': |
||
| 374 | $whitelist = array(); |
||
| 375 | $new_ip = $args[1]; |
||
| 376 | $current_whitelist = get_site_option( 'jetpack_protect_whitelist' ); |
||
| 377 | |||
| 378 | // Build array of IPs that are already whitelisted. |
||
| 379 | // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get |
||
| 380 | // low & high range params for jetpack_protect_ip_address_is_in_range(); |
||
| 381 | foreach( $current_whitelist as $whitelisted ) { |
||
| 382 | |||
| 383 | // IP ranges |
||
| 384 | if ( $whitelisted->range ) { |
||
| 385 | |||
| 386 | // Is it already whitelisted? |
||
| 387 | if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { |
||
| 388 | /* translators: %s is an IP address */ |
||
| 389 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 390 | break; |
||
| 391 | } |
||
| 392 | $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; |
||
| 393 | |||
| 394 | } else { // Individual IPs |
||
| 395 | |||
| 396 | // Check if the IP is already whitelisted (single IP only) |
||
| 397 | if ( $new_ip == $whitelisted->ip_address ) { |
||
| 398 | /* translators: %s is an IP address */ |
||
| 399 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 400 | break; |
||
| 401 | } |
||
| 402 | $whitelist[] = $whitelisted->ip_address; |
||
| 403 | |||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | /* |
||
| 408 | * List the whitelist |
||
| 409 | * Done here because it's easier to read the $whitelist array after it's been rebuilt |
||
| 410 | */ |
||
| 411 | if ( isset( $args[1] ) && 'list' == $args[1] ) { |
||
| 412 | if ( ! empty( $whitelist ) ) { |
||
| 413 | WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); |
||
| 414 | foreach ( $whitelist as $ip ) { |
||
| 415 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; |
||
| 416 | } |
||
| 417 | } else { |
||
| 418 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 419 | } |
||
| 420 | break; |
||
| 421 | } |
||
| 422 | |||
| 423 | /* |
||
| 424 | * Clear the whitelist |
||
| 425 | */ |
||
| 426 | if ( isset( $args[1] ) && 'clear' == $args[1] ) { |
||
| 427 | View Code Duplication | if ( ! empty( $whitelist ) ) { |
|
| 428 | $whitelist = array(); |
||
| 429 | jetpack_protect_save_whitelist( $whitelist ); |
||
| 430 | WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); |
||
| 431 | } else { |
||
| 432 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 433 | } |
||
| 434 | break; |
||
| 435 | } |
||
| 436 | |||
| 437 | // Append new IP to whitelist array |
||
| 438 | array_push( $whitelist, $new_ip ); |
||
| 439 | |||
| 440 | // Save whitelist if there are no errors |
||
| 441 | $result = jetpack_protect_save_whitelist( $whitelist ); |
||
| 442 | if ( is_wp_error( $result ) ) { |
||
| 443 | WP_CLI::error( __( $result, 'jetpack' ) ); |
||
| 444 | } |
||
| 445 | |||
| 446 | /* translators: %s is an IP address */ |
||
| 447 | WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); |
||
| 448 | break; |
||
| 449 | case 'prompt': |
||
| 450 | WP_CLI::error( |
||
| 451 | __( 'No command found.', 'jetpack' ) . "\n" . |
||
| 452 | __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . |
||
| 453 | _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" . |
||
| 454 | _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
||
| 455 | ); |
||
| 456 | break; |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Manage Jetpack Options |
||
| 462 | * |
||
| 463 | * ## OPTIONS |
||
| 464 | * |
||
| 465 | * list : List all jetpack options and their values |
||
| 466 | * delete : Delete an option |
||
| 467 | * - can only delete options that are white listed. |
||
| 468 | * update : update an option |
||
| 469 | * - can only update option strings |
||
| 470 | * get : get the value of an option |
||
| 471 | * |
||
| 472 | * ## EXAMPLES |
||
| 473 | * |
||
| 474 | * wp jetpack options list |
||
| 475 | * wp jetpack options get <option_name> |
||
| 476 | * wp jetpack options delete <option_name> |
||
| 477 | * wp jetpack options update <option_name> [<option_value>] |
||
| 478 | * |
||
| 479 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 480 | */ |
||
| 481 | public function options( $args, $assoc_args ) { |
||
| 482 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
| 483 | $safe_to_modify = Jetpack::get_jetpack_options_for_reset(); |
||
| 484 | |||
| 485 | // Jumpstart is special |
||
| 486 | array_push( $safe_to_modify, 'jumpstart' ); |
||
| 487 | |||
| 488 | // Is the option flagged as unsafe? |
||
| 489 | $flagged = ! in_array( $args[1], $safe_to_modify ); |
||
| 490 | |||
| 491 | View Code Duplication | if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) { |
|
| 492 | /* translators: %s is a command like "prompt" */ |
||
| 493 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 494 | } |
||
| 495 | |||
| 496 | if ( isset( $args[0] ) ) { |
||
| 497 | if ( 'get' == $args[0] && isset( $args[1] ) ) { |
||
| 498 | $action = 'get'; |
||
| 499 | } else if ( 'delete' == $args[0] && isset( $args[1] ) ) { |
||
| 500 | $action = 'delete'; |
||
| 501 | View Code Duplication | } else if ( 'update' == $args[0] && isset( $args[1] ) ) { |
|
| 502 | $action = 'update'; |
||
| 503 | } else { |
||
| 504 | $action = 'list'; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | // Bail if the option isn't found |
||
| 509 | $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false; |
||
| 510 | View Code Duplication | if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) { |
|
| 511 | WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); |
||
| 512 | } |
||
| 513 | |||
| 514 | // Let's print_r the option if it's an array |
||
| 515 | // Used in the 'get' and 'list' actions |
||
| 516 | $option = is_array( $option ) ? print_r( $option ) : $option; |
||
| 517 | |||
| 518 | switch ( $action ) { |
||
| 519 | case 'get': |
||
| 520 | WP_CLI::success( "\t" . $option ); |
||
| 521 | break; |
||
| 522 | case 'delete': |
||
| 523 | jetpack_cli_are_you_sure( $flagged ); |
||
| 524 | |||
| 525 | Jetpack_Options::delete_option( $args[1] ); |
||
| 526 | WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) ); |
||
| 527 | break; |
||
| 528 | case 'update': |
||
| 529 | jetpack_cli_are_you_sure( $flagged ); |
||
| 530 | |||
| 531 | // Updating arrays would get pretty tricky... |
||
| 532 | $value = Jetpack_Options::get_option( $args[1] ); |
||
| 533 | if ( $value && is_array( $value ) ) { |
||
| 534 | WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) ); |
||
| 535 | } |
||
| 536 | |||
| 537 | Jetpack_Options::update_option( $args[1], $args[2] ); |
||
| 538 | WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) ); |
||
| 539 | break; |
||
| 540 | case 'list': |
||
| 541 | $options_compact = Jetpack_Options::get_option_names(); |
||
| 542 | $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' ); |
||
| 543 | $options_private = Jetpack_Options::get_option_names( 'private' ); |
||
| 544 | $options = array_merge( $options_compact, $options_non_compact, $options_private ); |
||
| 545 | |||
| 546 | // Table headers |
||
| 547 | WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) ); |
||
| 548 | |||
| 549 | // List out the options and their values |
||
| 550 | // Tell them if the value is empty or not |
||
| 551 | // Tell them if it's an array |
||
| 552 | foreach ( $options as $option ) { |
||
| 553 | $value = Jetpack_Options::get_option( $option ); |
||
| 554 | if ( ! $value ) { |
||
| 555 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' ); |
||
| 556 | continue; |
||
| 557 | } |
||
| 558 | |||
| 559 | if ( ! is_array( $value ) ) { |
||
| 560 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value ); |
||
| 561 | } else if ( is_array( $value ) ) { |
||
| 562 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' ); |
||
| 563 | } |
||
| 564 | } |
||
| 565 | $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}'; |
||
| 566 | $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}'; |
||
| 567 | |||
| 568 | WP_CLI::success( |
||
| 569 | _x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" . |
||
| 570 | str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" . |
||
| 571 | str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" . |
||
| 572 | str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" . |
||
| 573 | _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n" |
||
| 574 | ); |
||
| 575 | break; |
||
| 576 | } |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the status of or start a new Jetpack sync. |
||
| 581 | * |
||
| 582 | * ## OPTIONS |
||
| 583 | * |
||
| 584 | * status : Print the current sync status |
||
| 585 | * start : Start a full sync from this site to WordPress.com |
||
| 586 | * |
||
| 587 | * ## EXAMPLES |
||
| 588 | * |
||
| 589 | * wp jetpack sync status |
||
| 590 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
| 591 | * |
||
| 592 | * @synopsis <status|start> [--<field>=<value>] |
||
| 593 | */ |
||
| 594 | public function sync( $args, $assoc_args ) { |
||
| 595 | if ( ! Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 596 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) ); |
||
| 597 | } |
||
| 598 | |||
| 599 | $action = isset( $args[0] ) ? $args[0] : 'status'; |
||
| 600 | |||
| 601 | switch ( $action ) { |
||
| 602 | case 'status': |
||
| 603 | $status = Jetpack_Sync_Actions::get_sync_status(); |
||
| 604 | $collection = array(); |
||
| 605 | foreach ( $status as $key => $item ) { |
||
| 606 | $collection[] = array( |
||
| 607 | 'option' => $key, |
||
| 608 | 'value' => is_scalar( $item ) ? $item : json_encode( $item ) |
||
| 609 | ); |
||
| 610 | } |
||
| 611 | |||
| 612 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
||
| 613 | break; |
||
| 614 | case 'start': |
||
| 615 | // Get the original settings so that we can restore them later |
||
| 616 | $original_settings = Jetpack_Sync_Settings::get_settings(); |
||
| 617 | |||
| 618 | // Initialize sync settigns so we can sync as quickly as possible |
||
| 619 | $sync_settings = wp_parse_args( |
||
| 620 | array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ), |
||
| 621 | array( |
||
| 622 | 'sync_wait_time' => 0, |
||
| 623 | 'enqueue_wait_time' => 0, |
||
| 624 | 'queue_max_writes_sec' => 10000, |
||
| 625 | 'max_queue_size_full_sync' => 100000 |
||
| 626 | ) |
||
| 627 | ); |
||
| 628 | Jetpack_Sync_Settings::update_settings( $sync_settings ); |
||
| 629 | |||
| 630 | // Convert comma-delimited string of modules to an array |
||
| 631 | View Code Duplication | if ( ! empty( $assoc_args['modules'] ) ) { |
|
| 632 | $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) ); |
||
| 633 | |||
| 634 | // Convert the array so that the keys are the module name and the value is true to indicate |
||
| 635 | // that we want to sync the module |
||
| 636 | $modules = array_map( '__return_true', array_flip( $modules ) ); |
||
| 637 | } |
||
| 638 | |||
| 639 | View Code Duplication | foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
|
| 640 | if ( |
||
| 641 | 'users' === $module_name && |
||
| 642 | isset( $assoc_args[ $module_name ] ) && |
||
| 643 | 'initial' === $assoc_args[ $module_name ] |
||
| 644 | ) { |
||
| 645 | $modules[ 'users' ] = 'initial'; |
||
| 646 | } elseif ( isset( $assoc_args[ $module_name ] ) ) { |
||
| 647 | $ids = explode( ',', $assoc_args[ $module_name ] ); |
||
| 648 | if ( count( $ids ) > 0 ) { |
||
| 649 | $modules[ $module_name ] = $ids; |
||
| 650 | } |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | if ( empty( $modules ) ) { |
||
| 655 | $modules = null; |
||
| 656 | } |
||
| 657 | |||
| 658 | // Kick off a full sync |
||
| 659 | if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) { |
||
| 660 | View Code Duplication | if ( $modules ) { |
|
| 661 | WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) ); |
||
| 662 | } else { |
||
| 663 | WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) ); |
||
| 664 | } |
||
| 665 | View Code Duplication | } else { |
|
| 666 | |||
| 667 | // Reset sync settings to original. |
||
| 668 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 669 | |||
| 670 | if ( $modules ) { |
||
| 671 | WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) ); |
||
| 672 | } else { |
||
| 673 | WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) ); |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | // Keep sending to WPCOM until there's nothing to send |
||
| 678 | $i = 1; |
||
| 679 | do { |
||
| 680 | $result = Jetpack_Sync_Actions::$sender->do_full_sync(); |
||
| 681 | if ( $result ) { |
||
| 682 | if ( 1 == $i++ ) { |
||
| 683 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
||
| 684 | } else { |
||
| 685 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
||
| 686 | } |
||
| 687 | } |
||
| 688 | } while ( $result ); |
||
| 689 | |||
| 690 | // Reset sync settings to original. |
||
| 691 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 692 | |||
| 693 | WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) ); |
||
| 694 | break; |
||
| 695 | } |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * List the contents of a specific Jetpack sync queue. |
||
| 700 | * |
||
| 701 | * ## OPTIONS |
||
| 702 | * |
||
| 703 | * peek : List the 100 front-most items on the queue. |
||
| 704 | * |
||
| 705 | * ## EXAMPLES |
||
| 706 | * |
||
| 707 | * wp jetpack sync_queue full_sync peek |
||
| 708 | * |
||
| 709 | * @synopsis <incremental|full_sync> <peek> |
||
| 710 | */ |
||
| 711 | public function sync_queue( $args, $assoc_args ) { |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
| 765 | * |
||
| 766 | * Returns success or error JSON |
||
| 767 | * |
||
| 768 | * <token_json> |
||
| 769 | * : JSON blob of WPCOM API token |
||
| 770 | */ |
||
| 771 | public function partner_cancel( $args, $named_args ) { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Provision a site using a Jetpack Partner license |
||
| 814 | * |
||
| 815 | * Returns JSON blob |
||
| 816 | * |
||
| 817 | * ## OPTIONS |
||
| 818 | * |
||
| 819 | * <token_json> |
||
| 820 | * : JSON blob of WPCOM API token |
||
| 821 | * --user_id=<user_id> |
||
| 822 | * : Local ID of user to connect as (if omitted, user will be required to redirect via wp-admin) |
||
| 823 | * [--plan=<plan_name>] |
||
| 824 | * : Slug of the requested plan, e.g. premium |
||
| 825 | * [--wpcom_user_id=<user_id>] |
||
| 826 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
| 827 | * [--force_register=<register>] |
||
| 828 | * : Whether to force a site to register |
||
| 829 | * |
||
| 830 | * ## EXAMPLES |
||
| 831 | * |
||
| 832 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 833 | * { success: true } |
||
| 834 | * |
||
| 835 | * @synopsis <token_json> --user_id=<user_id> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--force_register=<register>] |
||
| 836 | */ |
||
| 837 | public function partner_provision( $args, $named_args ) { |
||
| 838 | list( $token_json ) = $args; |
||
| 839 | |||
| 840 | $user_id = $named_args['user_id']; |
||
| 841 | |||
| 842 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 843 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 844 | } |
||
| 845 | |||
| 846 | if ( isset( $token->error ) ) { |
||
| 847 | $message = isset( $token->message ) |
||
| 848 | ? $token->message |
||
| 849 | : ''; |
||
| 850 | $this->partner_provision_error( new WP_Error( $token->error, $message ) ); |
||
| 851 | } |
||
| 852 | |||
| 853 | if ( ! isset( $token->access_token ) ) { |
||
| 854 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 855 | } |
||
| 856 | |||
| 857 | if ( empty( $user_id ) ) { |
||
| 858 | $this->partner_provision_error( new WP_Error( 'missing_user_id', __( 'Missing user ID', 'jetpack' ) ) ); |
||
| 859 | } |
||
| 860 | |||
| 861 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 862 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 863 | |||
| 864 | // we need to set the current user because: |
||
| 865 | // 1) register uses it as the "state" variable |
||
| 866 | // 2) authorize uses it to construct state variable and also to check if site is authorized for this user, and to send role |
||
| 867 | // 3) ultimately, it is this user who receives the plan |
||
| 868 | wp_set_current_user( $user_id ); |
||
| 869 | $user = wp_get_current_user(); |
||
| 870 | |||
| 871 | if ( empty( $user ) ) { |
||
| 872 | $this->partner_provision_error( new WP_Error( 'missing_user', sprintf( __( "User %s doesn't exist", 'jetpack' ), $user_id ) ) ); |
||
| 873 | } |
||
| 874 | |||
| 875 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 876 | // this code mostly copied from Jetpack::admin_page_load |
||
| 877 | Jetpack::maybe_set_version_option(); |
||
| 878 | $registered = Jetpack::try_registration(); |
||
| 879 | if ( is_wp_error( $registered ) ) { |
||
| 880 | $this->partner_provision_error( $registered ); |
||
| 881 | } elseif ( ! $registered ) { |
||
| 882 | $this->partner_provision_error( new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ) ); |
||
| 883 | } |
||
| 884 | |||
| 885 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 886 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 887 | } |
||
| 888 | |||
| 889 | // role |
||
| 890 | $role = Jetpack::translate_current_user_to_role(); |
||
| 891 | $signed_role = Jetpack::sign_role( $role ); |
||
| 892 | |||
| 893 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 894 | |||
| 895 | $site_icon = ( function_exists( 'has_site_icon') && has_site_icon() ) |
||
| 896 | ? get_site_icon_url() |
||
| 897 | : false; |
||
| 898 | |||
| 899 | $sso_url = add_query_arg( |
||
| 900 | array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ), |
||
| 901 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 902 | ); |
||
| 903 | |||
| 904 | $request_body = array( |
||
| 905 | 'jp_version' => JETPACK__VERSION, |
||
| 906 | |||
| 907 | // Jetpack auth stuff |
||
| 908 | 'scope' => $signed_role, |
||
| 909 | 'secret' => $secrets['secret_1'], |
||
| 910 | |||
| 911 | // User stuff |
||
| 912 | 'user_id' => $user->ID, |
||
| 913 | 'user_email' => $user->user_email, |
||
| 914 | 'user_login' => $user->user_login, |
||
| 915 | |||
| 916 | // Blog meta stuff |
||
| 917 | 'site_icon' => $site_icon, |
||
| 918 | |||
| 919 | // Then come back to this URL |
||
| 920 | 'redirect_uri' => $sso_url |
||
| 921 | ); |
||
| 922 | |||
| 923 | // optional additional params |
||
| 924 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 925 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 926 | } |
||
| 927 | |||
| 928 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 929 | $request_body['plan'] = $named_args['plan']; |
||
| 930 | } |
||
| 931 | |||
| 932 | $request = array( |
||
| 933 | 'headers' => array( |
||
| 934 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 935 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 936 | ), |
||
| 937 | 'method' => 'POST', |
||
| 938 | 'body' => json_encode( $request_body ) |
||
| 939 | ); |
||
| 940 | |||
| 941 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-provision', $this->get_api_host(), $blog_id ); |
||
| 942 | |||
| 943 | // add calypso env if set |
||
| 944 | if ( getenv( 'CALYPSO_ENV' ) ) { |
||
| 945 | $url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url ); |
||
| 946 | } |
||
| 947 | |||
| 948 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 949 | |||
| 950 | if ( is_wp_error( $result ) ) { |
||
| 951 | $this->partner_provision_error( $result ); |
||
| 952 | } |
||
| 953 | |||
| 954 | $response_code = wp_remote_retrieve_response_code( $result ); |
||
| 955 | $body_json = json_decode( wp_remote_retrieve_body( $result ) ); |
||
| 956 | |||
| 957 | if( 200 !== $response_code ) { |
||
| 958 | if ( isset( $body_json->error ) ) { |
||
| 959 | $this->partner_provision_error( new WP_Error( $body_json->error, $body_json->message ) ); |
||
| 960 | } else { |
||
| 961 | error_log(print_r($result,1)); |
||
| 962 | $this->partner_provision_error( new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) ) ); |
||
| 963 | } |
||
| 964 | } |
||
| 965 | |||
| 966 | WP_CLI::log( $body_json->next_url ); |
||
| 967 | |||
| 968 | WP_CLI::log( json_encode( $body_json ) ); |
||
| 969 | } |
||
| 970 | |||
| 971 | private function get_api_host() { |
||
| 975 | |||
| 976 | private function partner_provision_error( $error ) { |
||
| 984 | } |
||
| 985 | |||
| 986 | /* |
||
| 987 | * Standard "ask for permission to continue" function. |
||
| 1022 |
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: