Completed
Push — update/partner-provision-via-r... ( 9dc788 )
by
unknown
13:13 queued 03:55
created

Jetpack_Provision   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 254
Duplicated Lines 14.96 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 38
loc 254
rs 6.433
c 0
b 0
f 0
wmc 57
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
F register_and_build_request_body() 15 131 35
C partner_provision() 10 60 12
A authorize_user() 6 22 3
B verify_token() 7 31 5
A get_api_host() 0 4 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Provision often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Provision, and based on these observations, apply Extract Interface, too.

1
<?php
2
class Jetpack_Provision {
3
	public static function register_and_build_request_body( $named_args ) {
4
		$url_args = array(
5
			'home_url' => 'WP_HOME',
6
			'site_url' => 'WP_SITEURL',
7
		);
8
9
		foreach ( $url_args as $url_arg => $constant_name ) {
10
			// Anonymous functions were introduced in 5.3.0. So, if we're running on
11
			// >= 5.3.0, use an anonymous function to set the home/siteurl value%s.
12
			//
13
			// Otherwise, fallback to setting the home/siteurl value via the WP_HOME and
14
			// WP_SITEURL constants if the constant hasn't already been defined.
15
			if ( isset( $named_args[ $url_arg ] ) ) {
16
				if ( version_compare( phpversion(), '5.3.0', '>=' ) ) {
17
					add_filter( $url_arg, function( $url ) use ( $url_arg, $named_args ) {
18
						return $named_args[ $url_arg ];
19
					}, 11 );
20
				} else if ( ! defined( $constant_name ) ) {
21
					define( $constant_name, $named_args[ $url_arg ] );
22
				}
23
			}
24
		}
25
26
		// If Jetpack is currently connected, and is not in Safe Mode already, kick off a sync of the current
27
		// functions/callables so that we can test if this site is in IDC.
28
		if ( Jetpack::is_active() && ! Jetpack::validate_sync_error_idc_option() && Jetpack_Sync_Actions::sync_allowed() ) {
29
			Jetpack_Sync_Actions::do_full_sync( array( 'functions' => true ) );
30
			Jetpack_Sync_Actions::$sender->do_full_sync();
0 ignored issues
show
Bug introduced by
The property sender cannot be accessed from this context as it is declared private in class Jetpack_Sync_Actions.

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.

Loading history...
31
		}
32
33
		if ( Jetpack::validate_sync_error_idc_option() ) {
34
			return new WP_Error(
35
				'site_in_safe_mode',
36
				esc_html__( 'Can not provision a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
37
			);
38
		}
39
40
		$blog_id    = Jetpack_Options::get_option( 'id' );
41
		$blog_token = Jetpack_Options::get_option( 'blog_token' );
42
43
		if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) {
44
			// this code mostly copied from Jetpack::admin_page_load
45
			Jetpack::maybe_set_version_option();
46
			$registered = Jetpack::try_registration();
47
			if ( is_wp_error( $registered ) ) {
48
				return $registered;
49
			} elseif ( ! $registered ) {
50
				return new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) );
51
			}
52
53
			$blog_id    = Jetpack_Options::get_option( 'id' );
0 ignored issues
show
Unused Code introduced by
$blog_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
			$blog_token = Jetpack_Options::get_option( 'blog_token' );
0 ignored issues
show
Unused Code introduced by
$blog_token is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
		}
56
57
		// if the user isn't specified, but we have a current master user, then set that to current user
58
		if ( ! get_current_user_id() && $master_user_id = Jetpack_Options::get_option( 'master_user' ) ) {
59
			wp_set_current_user( $master_user_id );
60
		}
61
62
		$site_icon = ( function_exists( 'has_site_icon' ) && has_site_icon() )
63
			? get_site_icon_url()
64
			: false;
65
66
		$auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) );
67
68
		/** This filter is documented in class.jetpack-cli.php */
69
		if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) {
70
			$redirect_uri = add_query_arg(
71
				array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ),
72
				wp_login_url() // TODO: come back to Jetpack dashboard?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
73
			);
74
		} else {
75
			$redirect_uri = admin_url();
76
		}
77
78
		$request_body = array(
79
			'jp_version'   => JETPACK__VERSION,
80
			'redirect_uri' => $redirect_uri,
81
		);
82
83
		if ( $site_icon ) {
84
			$request_body['site_icon'] = $site_icon;
85
		}
86
87
		if ( get_current_user_id() ) {
88
			$user = wp_get_current_user();
89
90
			// role
91
			$role = Jetpack::translate_current_user_to_role();
92
			$signed_role = Jetpack::sign_role( $role );
93
94
			$secrets = Jetpack::init()->generate_secrets( 'authorize' );
95
96
			// Jetpack auth stuff
97
			$request_body['scope']  = $signed_role;
98
			$request_body['secret'] = $secrets['secret_1'];
99
100
			// User stuff
101
			$request_body['user_id']    = $user->ID;
102
			$request_body['user_email'] = $user->user_email;
103
			$request_body['user_login'] = $user->user_login;
104
		}
105
106
		// optional additional params
107 View Code Duplication
		if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) {
108
			$request_body['wpcom_user_id'] = $named_args['wpcom_user_id'];
109
		}
110
111
		// override email of selected user
112 View Code Duplication
		if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) {
113
			$request_body['user_email'] = $named_args['wpcom_user_email'];
114
		}
115
116 View Code Duplication
		if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) {
117
			$request_body['plan'] = $named_args['plan'];
118
		}
119
120 View Code Duplication
		if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) {
121
			$request_body['onboarding'] = intval( $named_args['onboarding'] );
122
		}
123
124 View Code Duplication
		if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) {
125
			$request_body['force_connect'] = intval( $named_args['force_connect'] );
126
		}
127
128
		if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) {
129
			Jetpack::create_onboarding_token();
130
		}
131
132
		return $request_body;
133
	}
134
135
	public static function partner_provision( $access_token, $named_args ) {
136
		// First, verify the token.
137
		$verify_response = self::verify_token( $access_token );
138
139
		if ( is_wp_error( $verify_response ) ) {
140
			return $verify_response;
141
		}
142
143
		$request_body = $this->register_and_build_request_body();
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The call to register_and_build_request_body() misses a required argument $named_args.

This check looks for function calls that miss required arguments.

Loading history...
144
		if ( is_wp_error( $request_body ) ) {
145
			return $request_body;
146
		}
147
148
		$request = array(
149
			'headers' => array(
150
				'Authorization' => "Bearer " . $access_token,
151
				'Host'          => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com',
152
			),
153
			'timeout' => 60,
154
			'method'  => 'POST',
155
			'body'    => json_encode( $request_body )
156
		);
157
158
		$url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-provision', self::get_api_host(), $blog_id );
0 ignored issues
show
Bug introduced by
The variable $blog_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
159 View Code Duplication
		if ( ! empty( $named_args['partner_tracking_id'] ) ) {
160
			$url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
161
		}
162
163
		// add calypso env if set
164
		if ( getenv( 'CALYPSO_ENV' ) ) {
165
			$url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url );
166
		}
167
168
		$result = Jetpack_Client::_wp_remote_request( $url, $request );
169
170
		if ( is_wp_error( $result ) ) {
171
			return $result;
172
		}
173
174
		$response_code = wp_remote_retrieve_response_code( $result );
175
		$body_json     = json_decode( wp_remote_retrieve_body( $result ) );
176
177 View Code Duplication
		if( 200 !== $response_code ) {
178
			if ( isset( $body_json->error ) ) {
179
				return new WP_Error( $body_json->error, $body_json->message );
180
			} else {
181
				return new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) );
182
			}
183
		}
184
185
		if ( isset( $body_json->access_token ) ) {
186
			// check if this matches the existing token before replacing
187
			$existing_token = Jetpack_Data::get_access_token( $user->ID );
0 ignored issues
show
Bug introduced by
The variable $user does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
188
			if ( empty( $existing_token ) || $existing_token->secret !== $body_json->access_token ) {
189
				self::authorize_user( $user->ID, $body_json->access_token );
190
			}
191
		}
192
193
		return $body_json;
194
	}
195
196
	private static function authorize_user( $user_id, $access_token ) {
197
		// authorize user and enable SSO
198
		Jetpack::update_user_token( $user_id, sprintf( '%s.%d', $access_token, $user_id ), true );
199
200
		/**
201
		 * Auto-enable SSO module for new Jetpack Start connections
202
		 *
203
		 * @since 5.0.0
204
		 *
205
		 * @param bool $enable_sso Whether to enable the SSO module. Default to true.
206
		 */
207
		$other_modules = apply_filters( 'jetpack_start_enable_sso', true )
208
			? array( 'sso' )
209
			: array();
210
211 View Code Duplication
		if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) {
212
			Jetpack::delete_active_modules();
213
			Jetpack::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), false );
0 ignored issues
show
Documentation introduced by
999 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
214
		} else {
215
			Jetpack::activate_default_modules( false, false, $other_modules, false );
216
		}
217
	}
218
219
	private static function verify_token( $access_token ) {
220
		$request = array(
221
			'headers' => array(
222
				'Authorization' => "Bearer " . $access_token,
223
				'Host'          => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com',
224
			),
225
			'timeout' => 10,
226
			'method'  => 'POST',
227
			'body'    => ''
228
		);
229
230
		$url = sprintf( 'https://%s/rest/v1.3/jpphp/partner-keys/verify', self::get_api_host() );
231
		$result = Jetpack_Client::_wp_remote_request( $url, $request );
232
233
		if ( is_wp_error( $result ) ) {
234
			return $result;
235
		}
236
237
		$response_code = wp_remote_retrieve_response_code( $result );
238
		$body_json     = json_decode( wp_remote_retrieve_body( $result ) );
239
240 View Code Duplication
		if( 200 !== $response_code ) {
241
			if ( isset( $body_json->error ) ) {
242
				return new WP_Error( $body_json->error, $body_json->message );
243
			} else {
244
				return new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) );
245
			}
246
		}
247
248
		return true;
249
	}
250
251
	private static function get_api_host() {
252
		$env_api_host = getenv( 'JETPACK_START_API_HOST', true );
253
		return $env_api_host ? $env_api_host : JETPACK__WPCOM_JSON_API_HOST;
254
	}
255
}
256