Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/_inc
File: class.jetpack-provision.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* Class file for provisioning Jetpack.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
use Automattic\Jetpack\Connection\Client;
[7] Fix | Delete
use Automattic\Jetpack\Connection\Secrets;
[8] Fix | Delete
use Automattic\Jetpack\Connection\Tokens;
[9] Fix | Delete
use Automattic\Jetpack\Identity_Crisis;
[10] Fix | Delete
use Automattic\Jetpack\Roles;
[11] Fix | Delete
use Automattic\Jetpack\Sync\Actions;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Jetpack_Provision class.
[15] Fix | Delete
*/
[16] Fix | Delete
class Jetpack_Provision {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Responsible for checking pre-conditions, registering site, and returning an array of details
[20] Fix | Delete
* that can be used to provision a plan for the site.
[21] Fix | Delete
*
[22] Fix | Delete
* @param array $named_args The array of arguments.
[23] Fix | Delete
*
[24] Fix | Delete
* @return WP_Error|array
[25] Fix | Delete
*/
[26] Fix | Delete
public static function register_and_build_request_body( $named_args ) {
[27] Fix | Delete
$url_args = array(
[28] Fix | Delete
'home_url' => 'WP_HOME',
[29] Fix | Delete
'site_url' => 'WP_SITEURL',
[30] Fix | Delete
);
[31] Fix | Delete
[32] Fix | Delete
foreach ( $url_args as $url_arg => $constant_name ) {
[33] Fix | Delete
if ( isset( $named_args[ $url_arg ] ) ) {
[34] Fix | Delete
add_filter(
[35] Fix | Delete
$url_arg,
[36] Fix | Delete
function () use ( $url_arg, $named_args ) {
[37] Fix | Delete
return $named_args[ $url_arg ];
[38] Fix | Delete
},
[39] Fix | Delete
11
[40] Fix | Delete
);
[41] Fix | Delete
}
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
// If Jetpack is currently connected, and is not in Safe Mode already, kick off a sync of the current
[45] Fix | Delete
// functions/callables so that we can test if this site is in IDC.
[46] Fix | Delete
if ( Jetpack::is_connection_ready() && ! Identity_Crisis::validate_sync_error_idc_option() && Actions::sync_allowed() ) {
[47] Fix | Delete
Actions::do_full_sync( array( 'functions' => true ), 'provision' );
[48] Fix | Delete
Actions::$sender->do_full_sync();
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
if ( Identity_Crisis::validate_sync_error_idc_option() ) {
[52] Fix | Delete
return new WP_Error(
[53] Fix | Delete
'site_in_safe_mode',
[54] Fix | Delete
__( 'Cannot provision a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
[55] Fix | Delete
);
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
if ( ! Jetpack::connection()->is_connected() || ( isset( $named_args['force_register'] ) && (int) $named_args['force_register'] ) ) {
[59] Fix | Delete
// This code mostly copied from Jetpack::admin_page_load.
[60] Fix | Delete
Jetpack::maybe_set_version_option();
[61] Fix | Delete
Jetpack::connection()->add_register_request_param( 'from', 'jetpack-start' );
[62] Fix | Delete
$registered = Jetpack::connection()->try_registration();
[63] Fix | Delete
if ( is_wp_error( $registered ) ) {
[64] Fix | Delete
return $registered;
[65] Fix | Delete
} elseif ( ! $registered ) {
[66] Fix | Delete
return new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) );
[67] Fix | Delete
}
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
// If the user isn't specified, but we have a current master user, then set that to current user.
[71] Fix | Delete
$master_user_id = Jetpack_Options::get_option( 'master_user' );
[72] Fix | Delete
if ( ! get_current_user_id() && $master_user_id ) {
[73] Fix | Delete
wp_set_current_user( $master_user_id );
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
$site_icon = get_site_icon_url();
[77] Fix | Delete
[78] Fix | Delete
$auto_enable_sso = ( ! Jetpack::connection()->has_connected_owner() || Jetpack::is_module_active( 'sso' ) );
[79] Fix | Delete
[80] Fix | Delete
/** This filter is documented in class.jetpack-cli.php */
[81] Fix | Delete
if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) {
[82] Fix | Delete
$redirect_uri = add_query_arg(
[83] Fix | Delete
array(
[84] Fix | Delete
'action' => 'jetpack-sso',
[85] Fix | Delete
'redirect_to' => rawurlencode( admin_url() ),
[86] Fix | Delete
),
[87] Fix | Delete
wp_login_url() // TODO: come back to Jetpack dashboard?
[88] Fix | Delete
);
[89] Fix | Delete
} else {
[90] Fix | Delete
$redirect_uri = admin_url();
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
$request_body = array(
[94] Fix | Delete
'jp_version' => JETPACK__VERSION,
[95] Fix | Delete
'redirect_uri' => $redirect_uri,
[96] Fix | Delete
);
[97] Fix | Delete
[98] Fix | Delete
if ( $site_icon ) {
[99] Fix | Delete
$request_body['site_icon'] = $site_icon;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
if ( get_current_user_id() ) {
[103] Fix | Delete
$user = wp_get_current_user();
[104] Fix | Delete
[105] Fix | Delete
// Role.
[106] Fix | Delete
$roles = new Roles();
[107] Fix | Delete
$role = $roles->translate_current_user_to_role();
[108] Fix | Delete
$signed_role = Jetpack::connection()->sign_role( $role );
[109] Fix | Delete
[110] Fix | Delete
$secrets = ( new Secrets() )->generate( 'authorize' );
[111] Fix | Delete
[112] Fix | Delete
// Jetpack auth stuff.
[113] Fix | Delete
$request_body['scope'] = $signed_role;
[114] Fix | Delete
$request_body['secret'] = $secrets['secret_1'];
[115] Fix | Delete
[116] Fix | Delete
// User stuff.
[117] Fix | Delete
$request_body['user_id'] = $user->ID;
[118] Fix | Delete
$request_body['user_email'] = $user->user_email;
[119] Fix | Delete
$request_body['user_login'] = $user->user_login;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
// Optional additional params.
[123] Fix | Delete
if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) {
[124] Fix | Delete
$request_body['wpcom_user_id'] = $named_args['wpcom_user_id'];
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
// Override email of selected user.
[128] Fix | Delete
if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) {
[129] Fix | Delete
$request_body['user_email'] = $named_args['wpcom_user_email'];
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) {
[133] Fix | Delete
$request_body['plan'] = $named_args['plan'];
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) {
[137] Fix | Delete
$request_body['force_connect'] = (int) $named_args['force_connect'];
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
return $request_body;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Given an access token and an array of arguments, will provision a plan for this site.
[145] Fix | Delete
*
[146] Fix | Delete
* @param string $access_token The access token from the partner.
[147] Fix | Delete
* @param array $named_args The arguments used for registering the site and then provisioning a plan.
[148] Fix | Delete
*
[149] Fix | Delete
* @return WP_Error|array
[150] Fix | Delete
*/
[151] Fix | Delete
public static function partner_provision( $access_token, $named_args ) {
[152] Fix | Delete
// First, verify the token.
[153] Fix | Delete
$verify_response = self::verify_token( $access_token );
[154] Fix | Delete
[155] Fix | Delete
if ( is_wp_error( $verify_response ) ) {
[156] Fix | Delete
return $verify_response;
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
$request_body = self::register_and_build_request_body( $named_args );
[160] Fix | Delete
if ( is_wp_error( $request_body ) ) {
[161] Fix | Delete
return $request_body;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$request = array(
[165] Fix | Delete
'headers' => array(
[166] Fix | Delete
'Authorization' => "Bearer $access_token",
[167] Fix | Delete
'Host' => 'public-api.wordpress.com',
[168] Fix | Delete
),
[169] Fix | Delete
'timeout' => 60,
[170] Fix | Delete
'method' => 'POST',
[171] Fix | Delete
'body' => wp_json_encode( $request_body, JSON_UNESCAPED_SLASHES ),
[172] Fix | Delete
);
[173] Fix | Delete
[174] Fix | Delete
$blog_id = Jetpack_Options::get_option( 'id' );
[175] Fix | Delete
$url = esc_url_raw(
[176] Fix | Delete
sprintf(
[177] Fix | Delete
'%s/rest/v1.3/jpphp/%d/partner-provision',
[178] Fix | Delete
self::get_api_host(),
[179] Fix | Delete
$blog_id
[180] Fix | Delete
)
[181] Fix | Delete
);
[182] Fix | Delete
if ( ! empty( $named_args['partner_tracking_id'] ) ) {
[183] Fix | Delete
$url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
// Add calypso env if set.
[187] Fix | Delete
$calypso_env = ( new \Automattic\Jetpack\Status\Host() )->get_calypso_env();
[188] Fix | Delete
if ( ! empty( $calypso_env ) ) {
[189] Fix | Delete
$url = add_query_arg( array( 'calypso_env' => $calypso_env ), $url );
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
// @phan-suppress-next-line PhanAccessMethodInternal -- Phan is correct, but the usage is intentional.
[193] Fix | Delete
$result = Client::_wp_remote_request( $url, $request );
[194] Fix | Delete
[195] Fix | Delete
if ( is_wp_error( $result ) ) {
[196] Fix | Delete
return $result;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
$response_code = wp_remote_retrieve_response_code( $result );
[200] Fix | Delete
$body_json = json_decode( wp_remote_retrieve_body( $result ) );
[201] Fix | Delete
[202] Fix | Delete
if ( 200 !== $response_code ) {
[203] Fix | Delete
if ( isset( $body_json->error ) ) {
[204] Fix | Delete
return new WP_Error( $body_json->error, $body_json->message );
[205] Fix | Delete
} else {
[206] Fix | Delete
return new WP_Error(
[207] Fix | Delete
'server_error',
[208] Fix | Delete
/* translators: %s is an HTTP status code retured from an API request. Ex. – 400 */
[209] Fix | Delete
sprintf( __( 'Request failed with code %s', 'jetpack' ), $response_code )
[210] Fix | Delete
);
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
if ( isset( $body_json->access_token ) && is_user_logged_in() ) {
[215] Fix | Delete
// Check if this matches the existing token before replacing.
[216] Fix | Delete
$existing_token = ( new Tokens() )->get_access_token( get_current_user_id() );
[217] Fix | Delete
if ( empty( $existing_token ) || $existing_token->secret !== $body_json->access_token ) {
[218] Fix | Delete
self::authorize_user( get_current_user_id(), $body_json->access_token );
[219] Fix | Delete
}
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
return $body_json;
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
/**
[226] Fix | Delete
* Authorizes the passed user.
[227] Fix | Delete
*
[228] Fix | Delete
* @param int $user_id User ID.
[229] Fix | Delete
* @param string $access_token Access token.
[230] Fix | Delete
*/
[231] Fix | Delete
private static function authorize_user( $user_id, $access_token ) {
[232] Fix | Delete
// authorize user and enable SSO.
[233] Fix | Delete
( new Tokens() )->update_user_token( $user_id, sprintf( '%s.%d', $access_token, $user_id ), true );
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Auto-enable SSO module for new Jetpack Start connections
[237] Fix | Delete
*
[238] Fix | Delete
* @since 5.0.0
[239] Fix | Delete
*
[240] Fix | Delete
* @param bool $enable_sso Whether to enable the SSO module. Default to true.
[241] Fix | Delete
*/
[242] Fix | Delete
$other_modules = apply_filters( 'jetpack_start_enable_sso', true )
[243] Fix | Delete
? array( 'sso' )
[244] Fix | Delete
: array();
[245] Fix | Delete
[246] Fix | Delete
$active_modules = Jetpack_Options::get_option( 'active_modules' );
[247] Fix | Delete
[248] Fix | Delete
if ( $active_modules ) {
[249] Fix | Delete
Jetpack::delete_active_modules();
[250] Fix | Delete
Jetpack::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), false );
[251] Fix | Delete
} else {
[252] Fix | Delete
Jetpack::activate_default_modules( false, false, $other_modules, false );
[253] Fix | Delete
}
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* Verifies the access token being used.
[258] Fix | Delete
*
[259] Fix | Delete
* @param string $access_token Access token.
[260] Fix | Delete
*
[261] Fix | Delete
* @return array|bool|WP_Error
[262] Fix | Delete
*/
[263] Fix | Delete
private static function verify_token( $access_token ) {
[264] Fix | Delete
$request = array(
[265] Fix | Delete
'headers' => array(
[266] Fix | Delete
'Authorization' => 'Bearer ' . $access_token,
[267] Fix | Delete
'Host' => 'public-api.wordpress.com',
[268] Fix | Delete
),
[269] Fix | Delete
'timeout' => 10,
[270] Fix | Delete
'method' => 'POST',
[271] Fix | Delete
'body' => '',
[272] Fix | Delete
);
[273] Fix | Delete
[274] Fix | Delete
$url = sprintf( '%s/rest/v1.3/jpphp/partner-keys/verify', self::get_api_host() );
[275] Fix | Delete
// @phan-suppress-next-line PhanAccessMethodInternal -- Phan is correct, but the usage is intentional.
[276] Fix | Delete
$result = Client::_wp_remote_request( $url, $request );
[277] Fix | Delete
[278] Fix | Delete
if ( is_wp_error( $result ) ) {
[279] Fix | Delete
return $result;
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
$response_code = wp_remote_retrieve_response_code( $result );
[283] Fix | Delete
$body_json = json_decode( wp_remote_retrieve_body( $result ) );
[284] Fix | Delete
[285] Fix | Delete
if ( 200 !== $response_code ) {
[286] Fix | Delete
if ( isset( $body_json->error ) ) {
[287] Fix | Delete
return new WP_Error( $body_json->error, $body_json->message );
[288] Fix | Delete
} else {
[289] Fix | Delete
/* translators: %s is HTTP response code (e.g. 500, 401, etc). */
[290] Fix | Delete
return new WP_Error( 'server_error', sprintf( __( 'Request failed with code %s', 'jetpack' ), $response_code ) );
[291] Fix | Delete
}
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
return true;
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Gets the API host as set via env.
[299] Fix | Delete
*
[300] Fix | Delete
* @return string API URL.
[301] Fix | Delete
*/
[302] Fix | Delete
private static function get_api_host() {
[303] Fix | Delete
$env_api_host = getenv( 'JETPACK_START_API_HOST', true );
[304] Fix | Delete
return $env_api_host ? 'https://' . $env_api_host : JETPACK__WPCOM_JSON_API_BASE;
[305] Fix | Delete
}
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function