| 1 |  |  | <?php //phpcs:ignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class Jetpack_Provision { //phpcs:ignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 	 * Responsible for checking pre-conditions, registering site, and returning an array of details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	 * that can be used to provision a plan for the site. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	 * @param array $named_args The array of arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	 * @return WP_Error|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	public static function register_and_build_request_body( $named_args ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		$url_args = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 			'home_url' => 'WP_HOME', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 			'site_url' => 'WP_SITEURL', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		foreach ( $url_args as $url_arg => $constant_name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			// Anonymous functions were introduced in 5.3.0. So, if we're running on | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 			// >= 5.3.0, use an anonymous function to set the home/siteurl value%s. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 			// Otherwise, fallback to setting the home/siteurl value via the WP_HOME and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			// WP_SITEURL constants if the constant hasn't already been defined. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			if ( isset( $named_args[ $url_arg ] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 				if ( version_compare( phpversion(), '5.3.0', '>=' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 					add_filter( $url_arg, function() use ( $url_arg, $named_args ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 						return $named_args[ $url_arg ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 					}, 11 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 				} elseif ( ! defined( $constant_name ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 					define( $constant_name, $named_args[ $url_arg ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		// If Jetpack is currently connected, and is not in Safe Mode already, kick off a sync of the current | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		// functions/callables so that we can test if this site is in IDC. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		if ( Jetpack::is_active() && ! Jetpack::validate_sync_error_idc_option() && Jetpack_Sync_Actions::sync_allowed() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			Jetpack_Sync_Actions::do_full_sync( array( 'functions' => true ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			Jetpack_Sync_Actions::$sender->do_full_sync(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		if ( Jetpack::validate_sync_error_idc_option() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			return new WP_Error( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 				'site_in_safe_mode', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 				esc_html__( 'Can not provision a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$blog_id    = Jetpack_Options::get_option( 'id' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$blog_token = Jetpack_Options::get_option( 'blog_token' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			// This code mostly copied from Jetpack::admin_page_load. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			Jetpack::maybe_set_version_option(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			$registered = Jetpack::try_registration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			if ( is_wp_error( $registered ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 				return $registered; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			} elseif ( ! $registered ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 				return new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$blog_id    = Jetpack_Options::get_option( 'id' ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			$blog_token = Jetpack_Options::get_option( 'blog_token' ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		// If the user isn't specified, but we have a current master user, then set that to current user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$master_user_id = Jetpack_Options::get_option( 'master_user' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		if ( ! get_current_user_id() && $master_user_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			wp_set_current_user( $master_user_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		$site_icon = ( function_exists( 'has_site_icon' ) && has_site_icon() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			? get_site_icon_url() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			: false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		/** This filter is documented in class.jetpack-cli.php */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			$redirect_uri = add_query_arg( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 					'action'      => 'jetpack-sso', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 					'redirect_to' => rawurlencode( admin_url() ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 				wp_login_url() // TODO: come back to Jetpack dashboard? | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			$redirect_uri = admin_url(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$request_body = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			'jp_version'   => JETPACK__VERSION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			'redirect_uri' => $redirect_uri, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		if ( $site_icon ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			$request_body['site_icon'] = $site_icon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		if ( get_current_user_id() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			$user = wp_get_current_user(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			// Role. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$role        = Jetpack::translate_current_user_to_role(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			$signed_role = Jetpack::sign_role( $role ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$secrets = Jetpack::init()->generate_secrets( 'authorize' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			// Jetpack auth stuff. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			$request_body['scope']  = $signed_role; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$request_body['secret'] = $secrets['secret_1']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			// User stuff. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			$request_body['user_id']    = $user->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			$request_body['user_email'] = $user->user_email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			$request_body['user_login'] = $user->user_login; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		// Optional additional params. | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 121 |  | View Code Duplication | 		if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			$request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		// Override email of selected user. | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 126 |  | View Code Duplication | 		if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 			$request_body['user_email'] = $named_args['wpcom_user_email']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 130 |  | View Code Duplication | 		if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			$request_body['plan'] = $named_args['plan']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 134 |  | View Code Duplication | 		if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			$request_body['onboarding'] = intval( $named_args['onboarding'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 138 |  | View Code Duplication | 		if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			$request_body['force_connect'] = intval( $named_args['force_connect'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			Jetpack::create_onboarding_token(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		return $request_body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * Given an access token and an array of arguments, will provision a plan for this site. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * @param string $access_token The access token from the partner. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * @param array  $named_args   The arguments used for registering the site and then provisioning a plan. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * @return WP_Error|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	public static function partner_provision( $access_token, $named_args ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		// First, verify the token. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$verify_response = self::verify_token( $access_token ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		if ( is_wp_error( $verify_response ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			return $verify_response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$request_body = self::register_and_build_request_body( $named_args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		if ( is_wp_error( $request_body ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 			return $request_body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		$request = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 			'headers' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 				'Authorization' => "Bearer $access_token", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 				'Host'          => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 					? JETPACK__WPCOM_JSON_API_HOST_HEADER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 					: 'public-api.wordpress.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 			'timeout' => 60, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 			'method'  => 'POST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 			'body'    => wp_json_encode( $request_body ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		$blog_id = Jetpack_Options::get_option( 'id' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		$url     = esc_url_raw( sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 			'https://%s/rest/v1.3/jpphp/%d/partner-provision', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 			self::get_api_host(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 			$blog_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 188 |  | View Code Duplication | 		if ( ! empty( $named_args['partner_tracking_id'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 			$url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 		// Add calypso env if set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 		if ( getenv( 'CALYPSO_ENV' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 			$url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 		$result = Jetpack_Client::_wp_remote_request( $url, $request ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 		if ( is_wp_error( $result ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 			return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 		$response_code = wp_remote_retrieve_response_code( $result ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 		$body_json     = json_decode( wp_remote_retrieve_body( $result ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 206 |  | View Code Duplication | 		if ( 200 !== $response_code ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 			if ( isset( $body_json->error ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 				return new WP_Error( $body_json->error, $body_json->message ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 				return new WP_Error( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 					'server_error', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 					/* translators: %s is an HTTP status code retured from an API request. Ex. – 400 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 					sprintf( esc_html__( 'Request failed with code %s', 'jetpack' ), $response_code ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 		if ( isset( $body_json->access_token ) && is_user_logged_in() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 			// Check if this matches the existing token before replacing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 			$existing_token = Jetpack_Data::get_access_token( get_current_user_id() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 			if ( empty( $existing_token ) || $existing_token->secret !== $body_json->access_token ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 				self::authorize_user( get_current_user_id(), $body_json->access_token ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 		return $body_json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 228 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 229 |  |  | 	private static function authorize_user( $user_id, $access_token ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 230 |  |  | 		// authorize user and enable SSO | 
            
                                                                        
                            
            
                                    
            
            
                | 231 |  |  | 		Jetpack::update_user_token( $user_id, sprintf( '%s.%d', $access_token, $user_id ), true ); | 
            
                                                                        
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 233 |  |  | 		/** | 
            
                                                                        
                            
            
                                    
            
            
                | 234 |  |  | 		 * Auto-enable SSO module for new Jetpack Start connections | 
            
                                                                        
                            
            
                                    
            
            
                | 235 |  |  | 		 * | 
            
                                                                        
                            
            
                                    
            
            
                | 236 |  |  | 		 * @since 5.0.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 237 |  |  | 		 * | 
            
                                                                        
                            
            
                                    
            
            
                | 238 |  |  | 		 * @param bool $enable_sso Whether to enable the SSO module. Default to true. | 
            
                                                                        
                            
            
                                    
            
            
                | 239 |  |  | 		 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 240 |  |  | 		$other_modules = apply_filters( 'jetpack_start_enable_sso', true ) | 
            
                                                                        
                            
            
                                    
            
            
                | 241 |  |  | 			? array( 'sso' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 242 |  |  | 			: array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 243 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 244 |  | View Code Duplication | 		if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 245 |  |  | 			Jetpack::delete_active_modules(); | 
            
                                                                        
                            
            
                                    
            
            
                | 246 |  |  | 			Jetpack::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), false ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 247 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 248 |  |  | 			Jetpack::activate_default_modules( false, false, $other_modules, false ); | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 	private static function verify_token( $access_token ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  | 		$request = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 			'headers' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 				'Authorization' => "Bearer " . $access_token, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 				'Host'          => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 			'timeout' => 10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 			'method'  => 'POST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 			'body'    => '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  | 		$url = sprintf( 'https://%s/rest/v1.3/jpphp/partner-keys/verify', self::get_api_host() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 		$result = Jetpack_Client::_wp_remote_request( $url, $request ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 		if ( is_wp_error( $result ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 			return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 		$response_code = wp_remote_retrieve_response_code( $result ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 		$body_json     = json_decode( wp_remote_retrieve_body( $result ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 273 |  | View Code Duplication | 		if( 200 !== $response_code ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 			if ( isset( $body_json->error ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 				return new WP_Error( $body_json->error, $body_json->message ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 				return new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 	private static function get_api_host() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 		$env_api_host = getenv( 'JETPACK_START_API_HOST', true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 		return $env_api_host ? $env_api_host : JETPACK__WPCOM_JSON_API_HOST; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 288 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 289 |  |  |  | 
            
                        
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.