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 | if ( ! Jetpack::is_active() ) { |
||
| 33 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
||
| 34 | } |
||
| 35 | |||
| 36 | if ( isset( $args[0] ) && 'full' !== $args[0] ) { |
||
| 37 | /* translators: %s is a command like "prompt" */ |
||
| 38 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) ); |
||
| 39 | } |
||
| 40 | |||
| 41 | $master_user_email = Jetpack::get_master_user_email(); |
||
| 42 | |||
| 43 | /* |
||
| 44 | * Are they asking for all data? |
||
| 45 | * |
||
| 46 | * Loop through heartbeat data and organize by priority. |
||
| 47 | */ |
||
| 48 | $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false; |
||
| 49 | if ( $all_data ) { |
||
|
|
|||
| 50 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
| 51 | WP_CLI::line( sprintf( __( "The Jetpack Version is %s", 'jetpack' ), JETPACK__VERSION ) ); |
||
| 52 | WP_CLI::line( sprintf( __( "The WordPress.com blog_id is %d", 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
| 53 | WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) ); |
||
| 54 | |||
| 55 | // Heartbeat data |
||
| 56 | WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) ); |
||
| 57 | |||
| 58 | // Get the filtered heartbeat data. |
||
| 59 | // Filtered so we can color/list by severity |
||
| 60 | $stats = Jetpack::jetpack_check_heartbeat_data(); |
||
| 61 | |||
| 62 | // Display red flags first |
||
| 63 | foreach ( $stats['bad'] as $stat => $value ) { |
||
| 64 | printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
| 65 | } |
||
| 66 | |||
| 67 | // Display caution warnings next |
||
| 68 | foreach ( $stats['caution'] as $stat => $value ) { |
||
| 69 | printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
| 70 | } |
||
| 71 | |||
| 72 | // The rest of the results are good! |
||
| 73 | foreach ( $stats['good'] as $stat => $value ) { |
||
| 74 | |||
| 75 | // Modules should get special spacing for aestetics |
||
| 76 | if ( strpos( $stat, 'odule-' ) ) { |
||
| 77 | printf( "%-'.30s %s\n", $stat, $value ); |
||
| 78 | usleep( 4000 ); // For dramatic effect lolz |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | printf( "%-'.16s %s\n", $stat, $value ); |
||
| 82 | usleep( 4000 ); // For dramatic effect lolz |
||
| 83 | } |
||
| 84 | } else { |
||
| 85 | // Just the basics |
||
| 86 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
| 87 | WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) ); |
||
| 88 | WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
| 89 | WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) ); |
||
| 90 | WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); |
||
| 91 | } |
||
| 92 | } |
||
| 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 ) { |
||
| 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 | } 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 | 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 ) { |
||
| 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 ) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Provision a site using a Jetpack Partner license |
||
| 815 | * |
||
| 816 | * Returns JSON blob |
||
| 817 | * |
||
| 818 | * ## OPTIONS |
||
| 819 | * |
||
| 820 | * <token_json> |
||
| 821 | * : JSON blob of WPCOM API token |
||
| 822 | * --user_id=<user_id> |
||
| 823 | * : Local ID of user to connect as (if omitted, user will be required to redirect via wp-admin) |
||
| 824 | * [--plan=<plan_name>] |
||
| 825 | * : Slug of the requested plan, e.g. premium |
||
| 826 | * [--wpcom_user_id=<user_id>] |
||
| 827 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
| 828 | * [--force_register=<register>] |
||
| 829 | * : Whether to force a site to register |
||
| 830 | * |
||
| 831 | * ## EXAMPLES |
||
| 832 | * |
||
| 833 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 834 | * { success: true } |
||
| 835 | * |
||
| 836 | * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--force_register=<register>] |
||
| 837 | */ |
||
| 838 | public function partner_provision( $args, $named_args ) { |
||
| 839 | list( $token_json ) = $args; |
||
| 840 | |||
| 841 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 842 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 843 | } |
||
| 844 | |||
| 845 | if ( isset( $token->error ) ) { |
||
| 846 | $message = isset( $token->message ) |
||
| 847 | ? $token->message |
||
| 848 | : ''; |
||
| 849 | $this->partner_provision_error( new WP_Error( $token->error, $message ) ); |
||
| 850 | } |
||
| 851 | |||
| 852 | if ( ! isset( $token->access_token ) ) { |
||
| 853 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 854 | } |
||
| 855 | |||
| 856 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 857 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 858 | |||
| 859 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 860 | // this code mostly copied from Jetpack::admin_page_load |
||
| 861 | Jetpack::maybe_set_version_option(); |
||
| 862 | $registered = Jetpack::try_registration(); |
||
| 863 | if ( is_wp_error( $registered ) ) { |
||
| 864 | $this->partner_provision_error( $registered ); |
||
| 865 | } elseif ( ! $registered ) { |
||
| 866 | $this->partner_provision_error( new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ) ); |
||
| 867 | } |
||
| 868 | |||
| 869 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 870 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 871 | } |
||
| 872 | |||
| 873 | // if the user isn't specified, but we have a current master user, then set that to current user |
||
| 874 | if ( ! get_current_user_id() && $master_user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
| 875 | wp_set_current_user( $master_user_id ); |
||
| 876 | } |
||
| 877 | |||
| 878 | $site_icon = ( function_exists( 'has_site_icon') && has_site_icon() ) |
||
| 879 | ? get_site_icon_url() |
||
| 880 | : false; |
||
| 881 | |||
| 882 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 883 | if ( apply_filters( 'jetpack_start_enable_sso', true ) ) { |
||
| 884 | $redirect_uri = add_query_arg( |
||
| 885 | array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ), |
||
| 886 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 887 | ); |
||
| 888 | } else { |
||
| 889 | $redirect_uri = admin_url(); |
||
| 890 | } |
||
| 891 | |||
| 892 | $request_body = array( |
||
| 893 | 'jp_version' => JETPACK__VERSION, |
||
| 894 | 'redirect_uri' => $redirect_uri |
||
| 895 | ); |
||
| 896 | |||
| 897 | if ( $site_icon ) { |
||
| 898 | $request_body['site_icon'] = $site_icon; |
||
| 899 | } |
||
| 900 | |||
| 901 | if ( get_current_user_id() ) { |
||
| 902 | $user = wp_get_current_user(); |
||
| 903 | |||
| 904 | // role |
||
| 905 | $role = Jetpack::translate_current_user_to_role(); |
||
| 906 | $signed_role = Jetpack::sign_role( $role ); |
||
| 907 | |||
| 908 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 909 | |||
| 910 | // Jetpack auth stuff |
||
| 911 | $request_body['scope'] = $signed_role; |
||
| 912 | $request_body['secret'] = $secrets['secret_1']; |
||
| 913 | |||
| 914 | // User stuff |
||
| 915 | $request_body['user_id'] = $user->ID; |
||
| 916 | $request_body['user_email'] = $user->user_email; |
||
| 917 | $request_body['user_login'] = $user->user_login; |
||
| 918 | } |
||
| 919 | |||
| 920 | // optional additional params |
||
| 921 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 922 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 923 | } |
||
| 924 | |||
| 925 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 926 | $request_body['plan'] = $named_args['plan']; |
||
| 927 | } |
||
| 928 | |||
| 929 | $request = array( |
||
| 930 | 'headers' => array( |
||
| 931 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 932 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 933 | ), |
||
| 934 | 'timeout' => 60, |
||
| 935 | 'method' => 'POST', |
||
| 936 | 'body' => json_encode( $request_body ) |
||
| 937 | ); |
||
| 938 | |||
| 939 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-provision', $this->get_api_host(), $blog_id ); |
||
| 940 | |||
| 941 | // add calypso env if set |
||
| 942 | if ( getenv( 'CALYPSO_ENV' ) ) { |
||
| 943 | $url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url ); |
||
| 944 | } |
||
| 945 | |||
| 946 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 947 | |||
| 948 | if ( is_wp_error( $result ) ) { |
||
| 949 | $this->partner_provision_error( $result ); |
||
| 950 | } |
||
| 951 | |||
| 952 | $response_code = wp_remote_retrieve_response_code( $result ); |
||
| 953 | $body_json = json_decode( wp_remote_retrieve_body( $result ) ); |
||
| 954 | |||
| 955 | if( 200 !== $response_code ) { |
||
| 956 | if ( isset( $body_json->error ) ) { |
||
| 957 | $this->partner_provision_error( new WP_Error( $body_json->error, $body_json->message ) ); |
||
| 958 | } else { |
||
| 959 | $this->partner_provision_error( new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) ) ); |
||
| 960 | } |
||
| 961 | } |
||
| 962 | |||
| 963 | if ( isset( $body_json->access_token ) ) { |
||
| 964 | // authorize user and enable SSO |
||
| 965 | Jetpack::update_user_token( $user->ID, sprintf( '%s.%d', $body_json->access_token, $user->ID ), true ); |
||
| 966 | |||
| 967 | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
||
| 968 | Jetpack::delete_active_modules(); |
||
| 969 | Jetpack::activate_default_modules( 999, 1, $active_modules, false ); |
||
| 970 | } else { |
||
| 971 | Jetpack::activate_default_modules( false, false, array(), false ); |
||
| 972 | } |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Auto-enable SSO module for new Jetpack Start connections |
||
| 976 | * |
||
| 977 | * @since 5.0.0 |
||
| 978 | * |
||
| 979 | * @param bool $enable_sso Whether to enable the SSO module. Default to true. |
||
| 980 | */ |
||
| 981 | if ( apply_filters( 'jetpack_start_enable_sso', true ) ) { |
||
| 982 | Jetpack::activate_module( 'sso', false, false ); |
||
| 983 | } |
||
| 984 | } |
||
| 985 | |||
| 986 | WP_CLI::log( json_encode( $body_json ) ); |
||
| 987 | } |
||
| 988 | |||
| 989 | private function get_api_host() { |
||
| 993 | |||
| 994 | private function partner_provision_error( $error ) { |
||
| 1002 | } |
||
| 1003 | |||
| 1040 |
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: