class.jetpack-provision.php 9.4 KB

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