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 ) { |
||
| 32 | |||
| 33 | WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_site_url() ) ) ); |
||
| 34 | |||
| 35 | if ( ! Jetpack::is_active() ) { |
||
| 36 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
||
| 37 | } |
||
| 38 | |||
| 39 | View Code Duplication | if ( isset( $args[0] ) && 'full' !== $args[0] ) { |
|
| 40 | /* translators: %s is a command like "prompt" */ |
||
| 41 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) ); |
||
| 42 | } |
||
| 43 | |||
| 44 | $master_user_email = Jetpack::get_master_user_email(); |
||
| 45 | |||
| 46 | /* |
||
| 47 | * Are they asking for all data? |
||
| 48 | * |
||
| 49 | * Loop through heartbeat data and organize by priority. |
||
| 50 | */ |
||
| 51 | $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false; |
||
| 52 | if ( $all_data ) { |
||
|
|
|||
| 53 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
| 54 | WP_CLI::line( sprintf( __( "The Jetpack Version is %s", 'jetpack' ), JETPACK__VERSION ) ); |
||
| 55 | WP_CLI::line( sprintf( __( "The WordPress.com blog_id is %d", 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
| 56 | WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) ); |
||
| 57 | |||
| 58 | // Heartbeat data |
||
| 59 | WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) ); |
||
| 60 | |||
| 61 | // Get the filtered heartbeat data. |
||
| 62 | // Filtered so we can color/list by severity |
||
| 63 | $stats = Jetpack::jetpack_check_heartbeat_data(); |
||
| 64 | |||
| 65 | // Display red flags first |
||
| 66 | foreach ( $stats['bad'] as $stat => $value ) { |
||
| 67 | printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
| 68 | } |
||
| 69 | |||
| 70 | // Display caution warnings next |
||
| 71 | foreach ( $stats['caution'] as $stat => $value ) { |
||
| 72 | printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
| 73 | } |
||
| 74 | |||
| 75 | // The rest of the results are good! |
||
| 76 | foreach ( $stats['good'] as $stat => $value ) { |
||
| 77 | |||
| 78 | // Modules should get special spacing for aestetics |
||
| 79 | if ( strpos( $stat, 'odule-' ) ) { |
||
| 80 | printf( "%-'.30s %s\n", $stat, $value ); |
||
| 81 | usleep( 4000 ); // For dramatic effect lolz |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | printf( "%-'.16s %s\n", $stat, $value ); |
||
| 85 | usleep( 4000 ); // For dramatic effect lolz |
||
| 86 | } |
||
| 87 | } else { |
||
| 88 | // Just the basics |
||
| 89 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
| 90 | WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) ); |
||
| 91 | WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
| 92 | WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) ); |
||
| 93 | WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Tests the active connection |
||
| 99 | * |
||
| 100 | * 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. |
||
| 101 | * |
||
| 102 | * ## EXAMPLES |
||
| 103 | * |
||
| 104 | * wp jetpack test-connection |
||
| 105 | * |
||
| 106 | * @subcommand test-connection |
||
| 107 | */ |
||
| 108 | public function test_connection( $args, $assoc_args ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Disconnect Jetpack Blogs or Users |
||
| 144 | * |
||
| 145 | * ## OPTIONS |
||
| 146 | * |
||
| 147 | * blog: Disconnect the entire blog. |
||
| 148 | * |
||
| 149 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 150 | * |
||
| 151 | * Please note, the primary account that the blog is connected |
||
| 152 | * to WordPress.com with cannot be disconnected without |
||
| 153 | * disconnecting the entire blog. |
||
| 154 | * |
||
| 155 | * ## EXAMPLES |
||
| 156 | * |
||
| 157 | * wp jetpack disconnect blog |
||
| 158 | * wp jetpack disconnect user 13 |
||
| 159 | * wp jetpack disconnect user username |
||
| 160 | * wp jetpack disconnect user [email protected] |
||
| 161 | * |
||
| 162 | * @synopsis <blog|user> [<user_identifier>] |
||
| 163 | */ |
||
| 164 | public function disconnect( $args, $assoc_args ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Reset Jetpack options and settings to default |
||
| 222 | * |
||
| 223 | * ## OPTIONS |
||
| 224 | * |
||
| 225 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 226 | * |
||
| 227 | * options: Resets all Jetpack options except: |
||
| 228 | * - All private options (Blog token, user token, etc...) |
||
| 229 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 230 | * - master_user |
||
| 231 | * - version |
||
| 232 | * - activated |
||
| 233 | * |
||
| 234 | * ## EXAMPLES |
||
| 235 | * |
||
| 236 | * wp jetpack reset options |
||
| 237 | * wp jetpack reset modules |
||
| 238 | * |
||
| 239 | * @synopsis <modules|options> |
||
| 240 | */ |
||
| 241 | public function reset( $args, $assoc_args ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Manage Jetpack Modules |
||
| 302 | * |
||
| 303 | * ## OPTIONS |
||
| 304 | * |
||
| 305 | * list : View all available modules, and their status. |
||
| 306 | * activate all : Activate all modules |
||
| 307 | * deactivate all: Deactivate all modules |
||
| 308 | * |
||
| 309 | * activate <module_slug> : Activate a module. |
||
| 310 | * deactivate <module_slug> : Deactivate a module. |
||
| 311 | * toggle <module_slug> : Toggle a module on or off. |
||
| 312 | * |
||
| 313 | * ## EXAMPLES |
||
| 314 | * |
||
| 315 | * wp jetpack module list |
||
| 316 | * wp jetpack module activate stats |
||
| 317 | * wp jetpack module deactivate stats |
||
| 318 | * wp jetpack module toggle stats |
||
| 319 | * |
||
| 320 | * wp jetpack module activate all |
||
| 321 | * wp jetpack module deactivate all |
||
| 322 | * |
||
| 323 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
| 324 | */ |
||
| 325 | public function module( $args, $assoc_args ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Manage Protect Settings |
||
| 391 | * |
||
| 392 | * ## OPTIONS |
||
| 393 | * |
||
| 394 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 395 | * |
||
| 396 | * |
||
| 397 | * ## EXAMPLES |
||
| 398 | * |
||
| 399 | * wp jetpack protect whitelist <ip address> |
||
| 400 | * wp jetpack protect whitelist list |
||
| 401 | * wp jetpack protect whitelist clear |
||
| 402 | * |
||
| 403 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 404 | */ |
||
| 405 | public function protect( $args, $assoc_args ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Manage Jetpack Options |
||
| 512 | * |
||
| 513 | * ## OPTIONS |
||
| 514 | * |
||
| 515 | * list : List all jetpack options and their values |
||
| 516 | * delete : Delete an option |
||
| 517 | * - can only delete options that are white listed. |
||
| 518 | * update : update an option |
||
| 519 | * - can only update option strings |
||
| 520 | * get : get the value of an option |
||
| 521 | * |
||
| 522 | * ## EXAMPLES |
||
| 523 | * |
||
| 524 | * wp jetpack options list |
||
| 525 | * wp jetpack options get <option_name> |
||
| 526 | * wp jetpack options delete <option_name> |
||
| 527 | * wp jetpack options update <option_name> [<option_value>] |
||
| 528 | * |
||
| 529 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 530 | */ |
||
| 531 | public function options( $args, $assoc_args ) { |
||
| 532 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
| 533 | $safe_to_modify = Jetpack_Options::get_options_for_reset(); |
||
| 534 | |||
| 535 | // Jumpstart is special |
||
| 536 | array_push( $safe_to_modify, 'jumpstart' ); |
||
| 537 | |||
| 538 | // Is the option flagged as unsafe? |
||
| 539 | $flagged = ! in_array( $args[1], $safe_to_modify ); |
||
| 540 | |||
| 541 | View Code Duplication | if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) { |
|
| 542 | /* translators: %s is a command like "prompt" */ |
||
| 543 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 544 | } |
||
| 545 | |||
| 546 | if ( isset( $args[0] ) ) { |
||
| 547 | if ( 'get' == $args[0] && isset( $args[1] ) ) { |
||
| 548 | $action = 'get'; |
||
| 549 | } else if ( 'delete' == $args[0] && isset( $args[1] ) ) { |
||
| 550 | $action = 'delete'; |
||
| 551 | View Code Duplication | } else if ( 'update' == $args[0] && isset( $args[1] ) ) { |
|
| 552 | $action = 'update'; |
||
| 553 | } else { |
||
| 554 | $action = 'list'; |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | // Bail if the option isn't found |
||
| 559 | $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false; |
||
| 560 | View Code Duplication | if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) { |
|
| 561 | WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); |
||
| 562 | } |
||
| 563 | |||
| 564 | // Let's print_r the option if it's an array |
||
| 565 | // Used in the 'get' and 'list' actions |
||
| 566 | $option = is_array( $option ) ? print_r( $option ) : $option; |
||
| 567 | |||
| 568 | switch ( $action ) { |
||
| 569 | case 'get': |
||
| 570 | WP_CLI::success( "\t" . $option ); |
||
| 571 | break; |
||
| 572 | case 'delete': |
||
| 573 | jetpack_cli_are_you_sure( $flagged ); |
||
| 574 | |||
| 575 | Jetpack_Options::delete_option( $args[1] ); |
||
| 576 | WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) ); |
||
| 577 | break; |
||
| 578 | case 'update': |
||
| 579 | jetpack_cli_are_you_sure( $flagged ); |
||
| 580 | |||
| 581 | // Updating arrays would get pretty tricky... |
||
| 582 | $value = Jetpack_Options::get_option( $args[1] ); |
||
| 583 | if ( $value && is_array( $value ) ) { |
||
| 584 | WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) ); |
||
| 585 | } |
||
| 586 | |||
| 587 | Jetpack_Options::update_option( $args[1], $args[2] ); |
||
| 588 | WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) ); |
||
| 589 | break; |
||
| 590 | case 'list': |
||
| 591 | $options_compact = Jetpack_Options::get_option_names(); |
||
| 592 | $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' ); |
||
| 593 | $options_private = Jetpack_Options::get_option_names( 'private' ); |
||
| 594 | $options = array_merge( $options_compact, $options_non_compact, $options_private ); |
||
| 595 | |||
| 596 | // Table headers |
||
| 597 | WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) ); |
||
| 598 | |||
| 599 | // List out the options and their values |
||
| 600 | // Tell them if the value is empty or not |
||
| 601 | // Tell them if it's an array |
||
| 602 | foreach ( $options as $option ) { |
||
| 603 | $value = Jetpack_Options::get_option( $option ); |
||
| 604 | if ( ! $value ) { |
||
| 605 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' ); |
||
| 606 | continue; |
||
| 607 | } |
||
| 608 | |||
| 609 | if ( ! is_array( $value ) ) { |
||
| 610 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value ); |
||
| 611 | } else if ( is_array( $value ) ) { |
||
| 612 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' ); |
||
| 613 | } |
||
| 614 | } |
||
| 615 | $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}'; |
||
| 616 | $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}'; |
||
| 617 | |||
| 618 | WP_CLI::success( |
||
| 619 | _x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" . |
||
| 620 | str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" . |
||
| 621 | str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" . |
||
| 622 | str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" . |
||
| 623 | _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n" |
||
| 624 | ); |
||
| 625 | break; |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get the status of or start a new Jetpack sync. |
||
| 631 | * |
||
| 632 | * ## OPTIONS |
||
| 633 | * |
||
| 634 | * status : Print the current sync status |
||
| 635 | * start : Start a full sync from this site to WordPress.com |
||
| 636 | * |
||
| 637 | * ## EXAMPLES |
||
| 638 | * |
||
| 639 | * wp jetpack sync status |
||
| 640 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
| 641 | * |
||
| 642 | * @synopsis <status|start> [--<field>=<value>] |
||
| 643 | */ |
||
| 644 | public function sync( $args, $assoc_args ) { |
||
| 753 | |||
| 754 | /** |
||
| 755 | * List the contents of a specific Jetpack sync queue. |
||
| 756 | * |
||
| 757 | * ## OPTIONS |
||
| 758 | * |
||
| 759 | * peek : List the 100 front-most items on the queue. |
||
| 760 | * |
||
| 761 | * ## EXAMPLES |
||
| 762 | * |
||
| 763 | * wp jetpack sync_queue full_sync peek |
||
| 764 | * |
||
| 765 | * @synopsis <incremental|full_sync> <peek> |
||
| 766 | */ |
||
| 767 | public function sync_queue( $args, $assoc_args ) { |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
| 821 | * |
||
| 822 | * Returns success or error JSON |
||
| 823 | * |
||
| 824 | * <token_json> |
||
| 825 | * : JSON blob of WPCOM API token |
||
| 826 | * [--partner-tracking-id=<partner_tracking_id>] |
||
| 827 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
| 828 | * |
||
| 829 | * * @synopsis <token_json> [--partner-tracking-id=<partner_tracking_id>] |
||
| 830 | */ |
||
| 831 | public function partner_cancel( $args, $named_args ) { |
||
| 832 | list( $token_json ) = $args; |
||
| 833 | |||
| 834 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 835 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 836 | } |
||
| 837 | |||
| 838 | if ( isset( $token->error ) ) { |
||
| 839 | $this->partner_provision_error( new WP_Error( $token->error, $token->message ) ); |
||
| 840 | } |
||
| 841 | |||
| 842 | if ( ! isset( $token->access_token ) ) { |
||
| 843 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 844 | } |
||
| 845 | |||
| 846 | $site_identifier = Jetpack_Options::get_option( 'id' ); |
||
| 847 | |||
| 848 | if ( ! $site_identifier ) { |
||
| 849 | $site_identifier = Jetpack::build_raw_urls( get_home_url() ); |
||
| 850 | } |
||
| 851 | |||
| 852 | $request = array( |
||
| 853 | 'headers' => array( |
||
| 854 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 855 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 856 | ), |
||
| 857 | 'timeout' => 60, |
||
| 858 | 'method' => 'POST', |
||
| 859 | ); |
||
| 860 | |||
| 861 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier ); |
||
| 862 | View Code Duplication | if ( ! empty( $named_args ) && ! empty( $named_args['partner-tracking-id'] ) ) { |
|
| 863 | $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner-tracking-id'], $url ) ); |
||
| 864 | } |
||
| 865 | |||
| 866 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 867 | |||
| 868 | Jetpack_Options::delete_option( 'onboarding' ); |
||
| 869 | |||
| 870 | if ( is_wp_error( $result ) ) { |
||
| 871 | $this->partner_provision_error( $result ); |
||
| 872 | } |
||
| 873 | |||
| 874 | WP_CLI::log( wp_remote_retrieve_body( $result ) ); |
||
| 875 | } |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Provision a site using a Jetpack Partner license |
||
| 879 | * |
||
| 880 | * Returns JSON blob |
||
| 881 | * |
||
| 882 | * ## OPTIONS |
||
| 883 | * |
||
| 884 | * <token_json> |
||
| 885 | * : JSON blob of WPCOM API token |
||
| 886 | * --user_id=<user_id> |
||
| 887 | * : Local ID of user to connect as (if omitted, user will be required to redirect via wp-admin) |
||
| 888 | * [--plan=<plan_name>] |
||
| 889 | * : Slug of the requested plan, e.g. premium |
||
| 890 | * [--wpcom_user_id=<user_id>] |
||
| 891 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
| 892 | * [--wpcom_user_email=<wpcom_user_email>] |
||
| 893 | * : Override the email we send to WordPress.com for registration |
||
| 894 | * [--onboarding=<onboarding>] |
||
| 895 | * : Guide the user through an onboarding wizard |
||
| 896 | * [--force_register=<register>] |
||
| 897 | * : Whether to force a site to register |
||
| 898 | * [--force_connect=<force_connect>] |
||
| 899 | * : Force JPS to not reuse existing credentials |
||
| 900 | * [--home_url=<home_url>] |
||
| 901 | * : Overrides the home option via the home_url filter, or the WP_HOME constant |
||
| 902 | * [--site_url=<site_url>] |
||
| 903 | * : Overrides the siteurl option via the site_url filter, or the WP_SITEURL constant |
||
| 904 | * [--partner-tracking-id=<partner_tracking_id>] |
||
| 905 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
| 906 | * |
||
| 907 | * ## EXAMPLES |
||
| 908 | * |
||
| 909 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 910 | * { success: true } |
||
| 911 | * |
||
| 912 | * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--onboarding=<onboarding>] [--force_register=<register>] [--force_connect=<force_connect>] [--home_url=<home_url>] [--site_url=<site_url>] [--wpcom_user_email=<wpcom_user_email>] [--partner-tracking-id=<partner_tracking_id>] |
||
| 913 | */ |
||
| 914 | public function partner_provision( $args, $named_args ) { |
||
| 915 | list( $token_json ) = $args; |
||
| 916 | |||
| 917 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 918 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 919 | } |
||
| 920 | |||
| 921 | if ( isset( $token->error ) ) { |
||
| 922 | $message = isset( $token->message ) |
||
| 923 | ? $token->message |
||
| 924 | : ''; |
||
| 925 | $this->partner_provision_error( new WP_Error( $token->error, $message ) ); |
||
| 926 | } |
||
| 927 | |||
| 928 | if ( ! isset( $token->access_token ) ) { |
||
| 929 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 930 | } |
||
| 931 | |||
| 932 | $url_args = array( |
||
| 933 | 'home_url' => 'WP_HOME', |
||
| 934 | 'site_url' => 'WP_SITEURL', |
||
| 935 | ); |
||
| 936 | |||
| 937 | foreach ( $url_args as $url_arg => $constant_name ) { |
||
| 938 | // Anonymous functions were introduced in 5.3.0. So, if we're running on |
||
| 939 | // >= 5.3.0, use an anonymous function to set the home/siteurl values. |
||
| 940 | // |
||
| 941 | // Otherwise, fallback to setting the home/siteurl value via the WP_HOME and |
||
| 942 | // WP_SITEURL constants if the constant hasn't already been defined. |
||
| 943 | if ( isset( $named_args[ $url_arg ] ) ) { |
||
| 944 | if ( version_compare( phpversion(), '5.3.0', '>=') ) { |
||
| 945 | add_filter( $url_arg, function( $url ) use ( $url_arg, $named_args ) { |
||
| 946 | return $named_args[ $url_arg ]; |
||
| 947 | }, 11 ); |
||
| 948 | } else if ( ! defined( $constant_name ) ) { |
||
| 949 | define( $constant_name, $named_args[ $url_arg ] ); |
||
| 950 | } |
||
| 951 | } |
||
| 952 | } |
||
| 953 | |||
| 954 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 955 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 956 | |||
| 957 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 958 | // this code mostly copied from Jetpack::admin_page_load |
||
| 959 | Jetpack::maybe_set_version_option(); |
||
| 960 | $registered = Jetpack::try_registration(); |
||
| 961 | if ( is_wp_error( $registered ) ) { |
||
| 962 | $this->partner_provision_error( $registered ); |
||
| 963 | } elseif ( ! $registered ) { |
||
| 964 | $this->partner_provision_error( new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ) ); |
||
| 965 | } |
||
| 966 | |||
| 967 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 968 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 969 | } |
||
| 970 | |||
| 971 | // if the user isn't specified, but we have a current master user, then set that to current user |
||
| 972 | if ( ! get_current_user_id() && $master_user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
| 973 | wp_set_current_user( $master_user_id ); |
||
| 974 | } |
||
| 975 | |||
| 976 | $site_icon = ( function_exists( 'has_site_icon') && has_site_icon() ) |
||
| 977 | ? get_site_icon_url() |
||
| 978 | : false; |
||
| 979 | |||
| 980 | $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); |
||
| 981 | |||
| 982 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 983 | if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { |
||
| 984 | $redirect_uri = add_query_arg( |
||
| 985 | array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ), |
||
| 986 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 987 | ); |
||
| 988 | } else { |
||
| 989 | $redirect_uri = admin_url(); |
||
| 990 | } |
||
| 991 | |||
| 992 | $request_body = array( |
||
| 993 | 'jp_version' => JETPACK__VERSION, |
||
| 994 | 'redirect_uri' => $redirect_uri |
||
| 995 | ); |
||
| 996 | |||
| 997 | if ( $site_icon ) { |
||
| 998 | $request_body['site_icon'] = $site_icon; |
||
| 999 | } |
||
| 1000 | |||
| 1001 | if ( get_current_user_id() ) { |
||
| 1002 | $user = wp_get_current_user(); |
||
| 1003 | |||
| 1004 | // role |
||
| 1005 | $role = Jetpack::translate_current_user_to_role(); |
||
| 1006 | $signed_role = Jetpack::sign_role( $role ); |
||
| 1007 | |||
| 1008 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 1009 | |||
| 1010 | // Jetpack auth stuff |
||
| 1011 | $request_body['scope'] = $signed_role; |
||
| 1012 | $request_body['secret'] = $secrets['secret_1']; |
||
| 1013 | |||
| 1014 | // User stuff |
||
| 1015 | $request_body['user_id'] = $user->ID; |
||
| 1016 | $request_body['user_email'] = $user->user_email; |
||
| 1017 | $request_body['user_login'] = $user->user_login; |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | // optional additional params |
||
| 1021 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 1022 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | // override email of selected user |
||
| 1026 | View Code Duplication | if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) { |
|
| 1027 | $request_body['user_email'] = $named_args['wpcom_user_email']; |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 1031 | $request_body['plan'] = $named_args['plan']; |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | View Code Duplication | if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { |
|
| 1035 | $request_body['onboarding'] = intval( $named_args['onboarding'] ); |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | View Code Duplication | if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) { |
|
| 1039 | $request_body['force_connect'] = intval( $named_args['force_connect'] ); |
||
| 1040 | } |
||
| 1041 | |||
| 1042 | if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) { |
||
| 1043 | Jetpack::create_onboarding_token(); |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | $request = array( |
||
| 1047 | 'headers' => array( |
||
| 1048 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 1049 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 1050 | ), |
||
| 1051 | 'timeout' => 60, |
||
| 1052 | 'method' => 'POST', |
||
| 1053 | 'body' => json_encode( $request_body ) |
||
| 1054 | ); |
||
| 1055 | |||
| 1056 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-provision', $this->get_api_host(), $blog_id ); |
||
| 1057 | View Code Duplication | if ( ! empty( $named_args['partner-tracking-id'] ) ) { |
|
| 1058 | $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner-tracking-id'], $url ) ); |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | // add calypso env if set |
||
| 1062 | if ( getenv( 'CALYPSO_ENV' ) ) { |
||
| 1063 | $url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url ); |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 1067 | |||
| 1068 | if ( is_wp_error( $result ) ) { |
||
| 1069 | $this->partner_provision_error( $result ); |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | $response_code = wp_remote_retrieve_response_code( $result ); |
||
| 1073 | $body_json = json_decode( wp_remote_retrieve_body( $result ) ); |
||
| 1074 | |||
| 1075 | if( 200 !== $response_code ) { |
||
| 1076 | if ( isset( $body_json->error ) ) { |
||
| 1077 | $this->partner_provision_error( new WP_Error( $body_json->error, $body_json->message ) ); |
||
| 1078 | } else { |
||
| 1079 | $this->partner_provision_error( new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) ) ); |
||
| 1080 | } |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | if ( isset( $body_json->access_token ) ) { |
||
| 1084 | // authorize user and enable SSO |
||
| 1085 | Jetpack::update_user_token( $user->ID, sprintf( '%s.%d', $body_json->access_token, $user->ID ), true ); |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Auto-enable SSO module for new Jetpack Start connections |
||
| 1089 | * |
||
| 1090 | * @since 5.0.0 |
||
| 1091 | * |
||
| 1092 | * @param bool $enable_sso Whether to enable the SSO module. Default to true. |
||
| 1093 | */ |
||
| 1094 | $other_modules = apply_filters( 'jetpack_start_enable_sso', true ) |
||
| 1095 | ? array( 'sso' ) |
||
| 1096 | : array(); |
||
| 1097 | |||
| 1098 | View Code Duplication | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
|
| 1099 | Jetpack::delete_active_modules(); |
||
| 1100 | Jetpack::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), false ); |
||
| 1101 | } else { |
||
| 1102 | Jetpack::activate_default_modules( false, false, $other_modules, false ); |
||
| 1103 | } |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | WP_CLI::log( json_encode( $body_json ) ); |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Manages your Jetpack sitemap |
||
| 1111 | * |
||
| 1112 | * ## OPTIONS |
||
| 1113 | * |
||
| 1114 | * rebuild : Rebuild all sitemaps |
||
| 1115 | * --purge : if set, will remove all existing sitemap data before rebuilding |
||
| 1116 | * |
||
| 1117 | * ## EXAMPLES |
||
| 1118 | * |
||
| 1119 | * wp jetpack sitemap rebuild |
||
| 1120 | * |
||
| 1121 | * @subcommand sitemap |
||
| 1122 | * @synopsis <rebuild> [--purge] |
||
| 1123 | */ |
||
| 1124 | public function sitemap( $args, $assoc_args ) { |
||
| 1143 | |||
| 1144 | private function get_api_host() { |
||
| 1148 | |||
| 1149 | private function partner_provision_error( $error ) { |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | /* |
||
| 1195 |
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: