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 ) { |
||
| 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 ) { |
||
| 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 | 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 ( is_wp_error( $result ) ) { |
||
| 682 | $queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() ); |
||
| 683 | if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) { |
||
| 684 | WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) ); |
||
| 685 | } |
||
| 686 | } else { |
||
| 687 | if ( 1 == $i ) { |
||
| 688 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
||
| 689 | } else { |
||
| 690 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
||
| 691 | } |
||
| 692 | } |
||
| 693 | $i++; |
||
| 694 | } while ( $result && ! is_wp_error( $result ) ); |
||
| 695 | |||
| 696 | // Reset sync settings to original. |
||
| 697 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 698 | |||
| 699 | WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) ); |
||
| 700 | break; |
||
| 701 | } |
||
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * List the contents of a specific Jetpack sync queue. |
||
| 706 | * |
||
| 707 | * ## OPTIONS |
||
| 708 | * |
||
| 709 | * peek : List the 100 front-most items on the queue. |
||
| 710 | * |
||
| 711 | * ## EXAMPLES |
||
| 712 | * |
||
| 713 | * wp jetpack sync_queue full_sync peek |
||
| 714 | * |
||
| 715 | * @synopsis <incremental|full_sync> <peek> |
||
| 716 | */ |
||
| 717 | public function sync_queue( $args, $assoc_args ) { |
||
| 718 | if ( ! Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 719 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) ); |
||
| 720 | } |
||
| 721 | |||
| 722 | $queue_name = isset( $args[0] ) ? $args[0] : 'sync'; |
||
| 723 | $action = isset( $args[1] ) ? $args[1] : 'peek'; |
||
| 724 | |||
| 725 | // We map the queue name that way we can support more friendly queue names in the commands, but still use |
||
| 726 | // the queue name that the code expects. |
||
| 727 | $queue_name_map = $allowed_queues = array( |
||
| 728 | 'incremental' => 'sync', |
||
| 729 | 'full' => 'full_sync', |
||
| 730 | ); |
||
| 731 | $mapped_queue_name = isset( $queue_name_map[ $queue_name ] ) ? $queue_name_map[ $queue_name ] : $queue_name; |
||
| 732 | |||
| 733 | switch( $action ) { |
||
| 734 | case 'peek': |
||
| 735 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; |
||
| 736 | $queue = new Jetpack_Sync_Queue( $mapped_queue_name ); |
||
| 737 | $items = $queue->peek( 100 ); |
||
| 738 | |||
| 739 | if ( empty( $items ) ) { |
||
| 740 | /* translators: %s is the name of the queue, either 'incremental' or 'full' */ |
||
| 741 | WP_CLI::log( sprintf( __( 'Nothing is in the queue: %s', 'jetpack' ), $queue_name ) ); |
||
| 742 | } else { |
||
| 743 | $collection = array(); |
||
| 744 | foreach ( $items as $item ) { |
||
| 745 | $collection[] = array( |
||
| 746 | 'action' => $item[0], |
||
| 747 | 'args' => json_encode( $item[1] ), |
||
| 748 | 'current_user_id' => $item[2], |
||
| 749 | 'microtime' => $item[3], |
||
| 750 | 'importing' => (string) $item[4], |
||
| 751 | ); |
||
| 752 | } |
||
| 753 | WP_CLI\Utils\format_items( |
||
| 754 | 'table', |
||
| 755 | $collection, |
||
| 756 | array( |
||
| 757 | 'action', |
||
| 758 | 'args', |
||
| 759 | 'current_user_id', |
||
| 760 | 'microtime', |
||
| 761 | 'importing', |
||
| 762 | ) |
||
| 763 | ); |
||
| 764 | } |
||
| 765 | break; |
||
| 766 | } |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
| 771 | * |
||
| 772 | * Returns success or error JSON |
||
| 773 | * |
||
| 774 | * <token_json> |
||
| 775 | * : JSON blob of WPCOM API token |
||
| 776 | */ |
||
| 777 | public function partner_cancel( $args, $named_args ) { |
||
| 778 | list( $token_json ) = $args; |
||
| 779 | |||
| 780 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 781 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 782 | } |
||
| 783 | |||
| 784 | if ( isset( $token->error ) ) { |
||
| 785 | $this->partner_provision_error( new WP_Error( $token->error, $token->message ) ); |
||
| 786 | } |
||
| 787 | |||
| 788 | if ( ! isset( $token->access_token ) ) { |
||
| 789 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 790 | } |
||
| 791 | |||
| 792 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 793 | |||
| 794 | if ( ! $blog_id ) { |
||
| 795 | $this->partner_provision_error( new WP_Error( 'site_not_registered', __( 'This site is not connected to Jetpack', 'jetpack' ) ) ); |
||
| 796 | } |
||
| 797 | |||
| 798 | $request = array( |
||
| 799 | 'headers' => array( |
||
| 800 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 801 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 802 | ), |
||
| 803 | 'timeout' => 60, |
||
| 804 | 'method' => 'POST', |
||
| 805 | 'body' => json_encode( array( 'site_id' => $blog_id ) ) |
||
| 806 | ); |
||
| 807 | |||
| 808 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-cancel', $this->get_api_host(), $blog_id ); |
||
| 809 | |||
| 810 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 811 | |||
| 812 | if ( is_wp_error( $result ) ) { |
||
| 813 | $this->partner_provision_error( $result ); |
||
| 814 | } |
||
| 815 | |||
| 816 | WP_CLI::log( json_encode( $result ) ); |
||
| 817 | } |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Provision a site using a Jetpack Partner license |
||
| 821 | * |
||
| 822 | * Returns JSON blob |
||
| 823 | * |
||
| 824 | * ## OPTIONS |
||
| 825 | * |
||
| 826 | * <token_json> |
||
| 827 | * : JSON blob of WPCOM API token |
||
| 828 | * --user_id=<user_id> |
||
| 829 | * : Local ID of user to connect as (if omitted, user will be required to redirect via wp-admin) |
||
| 830 | * [--plan=<plan_name>] |
||
| 831 | * : Slug of the requested plan, e.g. premium |
||
| 832 | * [--wpcom_user_id=<user_id>] |
||
| 833 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
| 834 | * [--force_register=<register>] |
||
| 835 | * : Whether to force a site to register |
||
| 836 | * [--onboarding=<onboarding>] |
||
| 837 | * : Guide the user through an onboarding wizard |
||
| 838 | * |
||
| 839 | * ## EXAMPLES |
||
| 840 | * |
||
| 841 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 842 | * { success: true } |
||
| 843 | * |
||
| 844 | * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--force_register=<register>] [--onboarding=<onboarding>] |
||
| 845 | */ |
||
| 846 | public function partner_provision( $args, $named_args ) { |
||
| 847 | list( $token_json ) = $args; |
||
| 848 | |||
| 849 | View Code Duplication | if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { |
|
| 850 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
||
| 851 | } |
||
| 852 | |||
| 853 | if ( isset( $token->error ) ) { |
||
| 854 | $message = isset( $token->message ) |
||
| 855 | ? $token->message |
||
| 856 | : ''; |
||
| 857 | $this->partner_provision_error( new WP_Error( $token->error, $message ) ); |
||
| 858 | } |
||
| 859 | |||
| 860 | if ( ! isset( $token->access_token ) ) { |
||
| 861 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
||
| 862 | } |
||
| 863 | |||
| 864 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 865 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 866 | |||
| 867 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 868 | // this code mostly copied from Jetpack::admin_page_load |
||
| 869 | Jetpack::maybe_set_version_option(); |
||
| 870 | $registered = Jetpack::try_registration(); |
||
| 871 | if ( is_wp_error( $registered ) ) { |
||
| 872 | $this->partner_provision_error( $registered ); |
||
| 873 | } elseif ( ! $registered ) { |
||
| 874 | $this->partner_provision_error( new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ) ); |
||
| 875 | } |
||
| 876 | |||
| 877 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 878 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 879 | } |
||
| 880 | |||
| 881 | // if the user isn't specified, but we have a current master user, then set that to current user |
||
| 882 | if ( ! get_current_user_id() && $master_user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
| 883 | wp_set_current_user( $master_user_id ); |
||
| 884 | } |
||
| 885 | |||
| 886 | $site_icon = ( function_exists( 'has_site_icon') && has_site_icon() ) |
||
| 887 | ? get_site_icon_url() |
||
| 888 | : false; |
||
| 889 | |||
| 890 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 891 | if ( apply_filters( 'jetpack_start_enable_sso', true ) ) { |
||
| 892 | $redirect_uri = add_query_arg( |
||
| 893 | array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ), |
||
| 894 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 895 | ); |
||
| 896 | } else { |
||
| 897 | $redirect_uri = admin_url(); |
||
| 898 | } |
||
| 899 | |||
| 900 | $request_body = array( |
||
| 901 | 'jp_version' => JETPACK__VERSION, |
||
| 902 | 'redirect_uri' => $redirect_uri |
||
| 903 | ); |
||
| 904 | |||
| 905 | if ( $site_icon ) { |
||
| 906 | $request_body['site_icon'] = $site_icon; |
||
| 907 | } |
||
| 908 | |||
| 909 | if ( get_current_user_id() ) { |
||
| 910 | $user = wp_get_current_user(); |
||
| 911 | |||
| 912 | // role |
||
| 913 | $role = Jetpack::translate_current_user_to_role(); |
||
| 914 | $signed_role = Jetpack::sign_role( $role ); |
||
| 915 | |||
| 916 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 917 | |||
| 918 | // Jetpack auth stuff |
||
| 919 | $request_body['scope'] = $signed_role; |
||
| 920 | $request_body['secret'] = $secrets['secret_1']; |
||
| 921 | |||
| 922 | // User stuff |
||
| 923 | $request_body['user_id'] = $user->ID; |
||
| 924 | $request_body['user_email'] = $user->user_email; |
||
| 925 | $request_body['user_login'] = $user->user_login; |
||
| 926 | } |
||
| 927 | |||
| 928 | // optional additional params |
||
| 929 | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
||
| 930 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 931 | } |
||
| 932 | |||
| 933 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 934 | $request_body['plan'] = $named_args['plan']; |
||
| 935 | } |
||
| 936 | |||
| 937 | View Code Duplication | if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { |
|
| 938 | $request_body['onboarding'] = intval( $named_args['onboarding'] ); |
||
| 939 | } |
||
| 940 | |||
| 941 | $request = array( |
||
| 942 | 'headers' => array( |
||
| 943 | 'Authorization' => "Bearer " . $token->access_token, |
||
| 944 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 945 | ), |
||
| 946 | 'timeout' => 60, |
||
| 947 | 'method' => 'POST', |
||
| 948 | 'body' => json_encode( $request_body ) |
||
| 949 | ); |
||
| 950 | |||
| 951 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-provision', $this->get_api_host(), $blog_id ); |
||
| 952 | |||
| 953 | // add calypso env if set |
||
| 954 | if ( getenv( 'CALYPSO_ENV' ) ) { |
||
| 955 | $url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url ); |
||
| 956 | } |
||
| 957 | |||
| 958 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 959 | |||
| 960 | if ( is_wp_error( $result ) ) { |
||
| 961 | $this->partner_provision_error( $result ); |
||
| 962 | } |
||
| 963 | |||
| 964 | $response_code = wp_remote_retrieve_response_code( $result ); |
||
| 965 | $body_json = json_decode( wp_remote_retrieve_body( $result ) ); |
||
| 966 | |||
| 967 | if( 200 !== $response_code ) { |
||
| 968 | if ( isset( $body_json->error ) ) { |
||
| 969 | $this->partner_provision_error( new WP_Error( $body_json->error, $body_json->message ) ); |
||
| 970 | } else { |
||
| 971 | $this->partner_provision_error( new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) ) ); |
||
| 972 | } |
||
| 973 | } |
||
| 974 | |||
| 975 | if ( isset( $body_json->access_token ) ) { |
||
| 976 | // authorize user and enable SSO |
||
| 977 | Jetpack::update_user_token( $user->ID, sprintf( '%s.%d', $body_json->access_token, $user->ID ), true ); |
||
| 978 | |||
| 979 | View Code Duplication | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
|
| 980 | Jetpack::delete_active_modules(); |
||
| 981 | Jetpack::activate_default_modules( 999, 1, $active_modules, false ); |
||
| 982 | } else { |
||
| 983 | Jetpack::activate_default_modules( false, false, array(), false ); |
||
| 984 | } |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Auto-enable SSO module for new Jetpack Start connections |
||
| 988 | * |
||
| 989 | * @since 5.0.0 |
||
| 990 | * |
||
| 991 | * @param bool $enable_sso Whether to enable the SSO module. Default to true. |
||
| 992 | */ |
||
| 993 | if ( apply_filters( 'jetpack_start_enable_sso', true ) ) { |
||
| 994 | Jetpack::activate_module( 'sso', false, false ); |
||
| 995 | } |
||
| 996 | } |
||
| 997 | |||
| 998 | WP_CLI::log( json_encode( $body_json ) ); |
||
| 999 | } |
||
| 1000 | |||
| 1001 | private function get_api_host() { |
||
| 1005 | |||
| 1006 | private function partner_provision_error( $error ) { |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | /* |
||
| 1017 | * Standard "ask for permission to continue" function. |
||
| 1018 | * If action cancelled, ask if they need help. |
||
| 1052 |
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: