summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/_inc/class.jetpack-provision.php')
-rw-r--r--plugins/jetpack/_inc/class.jetpack-provision.php87
1 files changed, 60 insertions, 27 deletions
diff --git a/plugins/jetpack/_inc/class.jetpack-provision.php b/plugins/jetpack/_inc/class.jetpack-provision.php
index e3d95b27..340e564e 100644
--- a/plugins/jetpack/_inc/class.jetpack-provision.php
+++ b/plugins/jetpack/_inc/class.jetpack-provision.php
@@ -1,11 +1,21 @@
-<?php //phpcs:ignore
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
+/**
+ * Class file for provisioning Jetpack.
+ *
+ * @package automattic/jetpack
+ */
use Automattic\Jetpack\Connection\Client;
-use Automattic\Jetpack\Connection\Utils as Connection_Utils;
+use Automattic\Jetpack\Connection\Secrets;
+use Automattic\Jetpack\Connection\Tokens;
+use Automattic\Jetpack\Identity_Crisis;
use Automattic\Jetpack\Roles;
use Automattic\Jetpack\Sync\Actions;
-class Jetpack_Provision { //phpcs:ignore
+/**
+ * Jetpack_Provision class.
+ */
+class Jetpack_Provision {
/**
* Responsible for checking pre-conditions, registering site, and returning an array of details
@@ -25,7 +35,7 @@ class Jetpack_Provision { //phpcs:ignore
if ( isset( $named_args[ $url_arg ] ) ) {
add_filter(
$url_arg,
- function() use ( $url_arg, $named_args ) {
+ function () use ( $url_arg, $named_args ) {
return $named_args[ $url_arg ];
},
11
@@ -35,23 +45,23 @@ class Jetpack_Provision { //phpcs:ignore
// If Jetpack is currently connected, and is not in Safe Mode already, kick off a sync of the current
// functions/callables so that we can test if this site is in IDC.
- if ( Jetpack::is_active() && ! Jetpack::validate_sync_error_idc_option() && Actions::sync_allowed() ) {
+ if ( Jetpack::is_connection_ready() && ! Identity_Crisis::validate_sync_error_idc_option() && Actions::sync_allowed() ) {
Actions::do_full_sync( array( 'functions' => true ) );
Actions::$sender->do_full_sync();
}
- if ( Jetpack::validate_sync_error_idc_option() ) {
+ if ( Identity_Crisis::validate_sync_error_idc_option() ) {
return new WP_Error(
'site_in_safe_mode',
__( 'Can not provision a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
);
}
-
- if ( ! Jetpack::connection()->is_registered() || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) {
+ if ( ! Jetpack::connection()->is_connected() || ( isset( $named_args['force_register'] ) && (int) $named_args['force_register'] ) ) {
// This code mostly copied from Jetpack::admin_page_load.
Jetpack::maybe_set_version_option();
- $registered = Jetpack::try_registration();
+ Jetpack::connection()->add_register_request_param( 'from', 'jetpack-start' );
+ $registered = Jetpack::connection()->try_registration();
if ( is_wp_error( $registered ) ) {
return $registered;
} elseif ( ! $registered ) {
@@ -67,7 +77,7 @@ class Jetpack_Provision { //phpcs:ignore
$site_icon = get_site_icon_url();
- $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) );
+ $auto_enable_sso = ( ! Jetpack::connection()->has_connected_owner() || Jetpack::is_module_active( 'sso' ) );
/** This filter is documented in class.jetpack-cli.php */
if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) {
@@ -99,7 +109,7 @@ class Jetpack_Provision { //phpcs:ignore
$role = $roles->translate_current_user_to_role();
$signed_role = Jetpack::connection()->sign_role( $role );
- $secrets = Jetpack::init()->generate_secrets( 'authorize' );
+ $secrets = ( new Secrets() )->generate( 'authorize' );
// Jetpack auth stuff.
$request_body['scope'] = $signed_role;
@@ -126,11 +136,11 @@ class Jetpack_Provision { //phpcs:ignore
}
if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) {
- $request_body['onboarding'] = intval( $named_args['onboarding'] );
+ $request_body['onboarding'] = (int) $named_args['onboarding'];
}
if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) {
- $request_body['force_connect'] = intval( $named_args['force_connect'] );
+ $request_body['force_connect'] = (int) $named_args['force_connect'];
}
if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) {
@@ -172,11 +182,13 @@ class Jetpack_Provision { //phpcs:ignore
);
$blog_id = Jetpack_Options::get_option( 'id' );
- $url = esc_url_raw( sprintf(
- 'https://%s/rest/v1.3/jpphp/%d/partner-provision',
- self::get_api_host(),
- $blog_id
- ) );
+ $url = esc_url_raw(
+ sprintf(
+ '%s/rest/v1.3/jpphp/%d/partner-provision',
+ self::get_api_host(),
+ $blog_id
+ )
+ );
if ( ! empty( $named_args['partner_tracking_id'] ) ) {
$url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
}
@@ -210,7 +222,7 @@ class Jetpack_Provision { //phpcs:ignore
if ( isset( $body_json->access_token ) && is_user_logged_in() ) {
// Check if this matches the existing token before replacing.
- $existing_token = Jetpack_Data::get_access_token( get_current_user_id() );
+ $existing_token = ( new Tokens() )->get_access_token( get_current_user_id() );
if ( empty( $existing_token ) || $existing_token->secret !== $body_json->access_token ) {
self::authorize_user( get_current_user_id(), $body_json->access_token );
}
@@ -219,9 +231,15 @@ class Jetpack_Provision { //phpcs:ignore
return $body_json;
}
+ /**
+ * Authorizes the passed user.
+ *
+ * @param int $user_id User ID.
+ * @param string $access_token Access token.
+ */
private static function authorize_user( $user_id, $access_token ) {
- // authorize user and enable SSO
- Connection_Utils::update_user_token( $user_id, sprintf( '%s.%d', $access_token, $user_id ), true );
+ // authorize user and enable SSO.
+ ( new Tokens() )->update_user_token( $user_id, sprintf( '%s.%d', $access_token, $user_id ), true );
/**
* Auto-enable SSO module for new Jetpack Start connections
@@ -234,7 +252,9 @@ class Jetpack_Provision { //phpcs:ignore
? array( 'sso' )
: array();
- if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) {
+ $active_modules = Jetpack_Options::get_option( 'active_modules' );
+
+ if ( $active_modules ) {
Jetpack::delete_active_modules();
Jetpack::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), false );
} else {
@@ -242,18 +262,25 @@ class Jetpack_Provision { //phpcs:ignore
}
}
+ /**
+ * Verifies the access token being used.
+ *
+ * @param string $access_token Access token.
+ *
+ * @return array|\Automattic\Jetpack\Connection\WP_Error|bool|WP_Error
+ */
private static function verify_token( $access_token ) {
$request = array(
'headers' => array(
- 'Authorization' => "Bearer " . $access_token,
+ 'Authorization' => 'Bearer ' . $access_token,
'Host' => 'public-api.wordpress.com',
),
'timeout' => 10,
'method' => 'POST',
- 'body' => ''
+ 'body' => '',
);
- $url = sprintf( 'https://%s/rest/v1.3/jpphp/partner-keys/verify', self::get_api_host() );
+ $url = sprintf( '%s/rest/v1.3/jpphp/partner-keys/verify', self::get_api_host() );
$result = Client::_wp_remote_request( $url, $request );
if ( is_wp_error( $result ) ) {
@@ -263,10 +290,11 @@ class Jetpack_Provision { //phpcs:ignore
$response_code = wp_remote_retrieve_response_code( $result );
$body_json = json_decode( wp_remote_retrieve_body( $result ) );
- if( 200 !== $response_code ) {
+ if ( 200 !== $response_code ) {
if ( isset( $body_json->error ) ) {
return new WP_Error( $body_json->error, $body_json->message );
} else {
+ /* translators: %s is HTTP response code (e.g. 500, 401, etc). */
return new WP_Error( 'server_error', sprintf( __( 'Request failed with code %s', 'jetpack' ), $response_code ) );
}
}
@@ -274,8 +302,13 @@ class Jetpack_Provision { //phpcs:ignore
return true;
}
+ /**
+ * Gets the API host as set via env.
+ *
+ * @return string API URL.
+ */
private static function get_api_host() {
$env_api_host = getenv( 'JETPACK_START_API_HOST', true ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.getenv_local_onlyFound
- return $env_api_host ? $env_api_host : JETPACK__WPCOM_JSON_API_HOST;
+ return $env_api_host ? 'https://' . $env_api_host : JETPACK__WPCOM_JSON_API_BASE;
}
}