Completed
Push — add/user-authentication ( 783ce9...34ed8f )
by
unknown
349:58 queued 341:49
created

Jetpack_XMLRPC_Server::unlink_user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Connection\Client;
4
use Automattic\Jetpack\Tracking;
5
6
/**
7
 * Just a sack of functions.  Not actually an IXR_Server
8
 */
9
class Jetpack_XMLRPC_Server {
10
	/**
11
	 * The current error object
12
	 */
13
	public $error = null;
14
15
	/**
16
	 * The current user
17
	 */
18
	public $user = null;
19
	
20
	private $tracking;
21
	
22
	function __construct() {
23
		$this->tracking = new Tracking();
24
	}
25
26
	/**
27
	 * Whitelist of the XML-RPC methods available to the Jetpack Server. If the
28
	 * user is not authenticated (->login()) then the methods are never added,
29
	 * so they will get a "does not exist" error.
30
	 */
31
	function xmlrpc_methods( $core_methods ) {
32
		$jetpack_methods = array(
33
			'jetpack.jsonAPI'           => array( $this, 'json_api' ),
34
			'jetpack.verifyAction'      => array( $this, 'verify_action' ),
35
			'jetpack.getUser'           => array( $this, 'get_user' ),
36
			'jetpack.remoteRegister'    => array( $this, 'remote_register' ),
37
			'jetpack.remoteProvision'   => array( $this, 'remote_provision' ),
38
		);
39
40
		$this->user = $this->login();
41
42
		if ( $this->user ) {
43
			$jetpack_methods = array_merge( $jetpack_methods, array(
44
				'jetpack.testConnection'    => array( $this, 'test_connection' ),
45
				'jetpack.testAPIUserCode'   => array( $this, 'test_api_user_code' ),
46
				'jetpack.featuresAvailable' => array( $this, 'features_available' ),
47
				'jetpack.featuresEnabled'   => array( $this, 'features_enabled' ),
48
				'jetpack.disconnectBlog'    => array( $this, 'disconnect_blog' ),
49
				'jetpack.unlinkUser'        => array( $this, 'unlink_user' ),
50
				'jetpack.syncObject'        => array( $this, 'sync_object' ),
51
				'jetpack.idcUrlValidation'  => array( $this, 'validate_urls_for_idc_mitigation' ),
52
			) );
53
54
			if ( isset( $core_methods['metaWeblog.editPost'] ) ) {
55
				$jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject'];
56
				$jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' );
57
			}
58
59
			/**
60
			 * Filters the XML-RPC methods available to Jetpack for authenticated users.
61
			 *
62
			 * @since 1.1.0
63
			 *
64
			 * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server.
65
			 * @param array $core_methods Available core XML-RPC methods.
66
			 * @param WP_User $user Information about a given WordPress user.
67
			 */
68
			$jetpack_methods = apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $this->user );
69
		}
70
71
		/**
72
		 * Filters the XML-RPC methods available to Jetpack for unauthenticated users.
73
		 *
74
		 * @since 3.0.0
75
		 *
76
		 * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server.
77
		 * @param array $core_methods Available core XML-RPC methods.
78
		 */
79
		return apply_filters( 'jetpack_xmlrpc_unauthenticated_methods', $jetpack_methods, $core_methods );
80
	}
81
82
	/**
83
	 * Whitelist of the bootstrap XML-RPC methods
84
	 */
85
	function bootstrap_xmlrpc_methods() {
86
		return array(
87
			'jetpack.remoteAuthorize' => array( $this, 'remote_authorize' ),
88
			'jetpack.remoteRegister' => array( $this, 'remote_register' ),
89
		);
90
	}
91
92
	function authorize_xmlrpc_methods() {
93
		return array(
94
			'jetpack.remoteAuthorize' => array( $this, 'remote_authorize' ),
95
		);
96
	}
97
98
	function provision_xmlrpc_methods() {
99
		return array(
100
			'jetpack.remoteRegister'  => array( $this, 'remote_register' ),
101
			'jetpack.remoteProvision' => array( $this, 'remote_provision' ),
102
			'jetpack.remoteConnect'   => array( $this, 'remote_connect' ),
103
			'jetpack.getUser'         => array( $this, 'get_user' ),
104
		);
105
	}
106
107
	/**
108
	 * Used to verify whether a local user exists and what role they have.
109
	 *
110
	 * @param int|string|array $request One of:
111
	 *                         int|string The local User's ID, username, or email address.
112
	 *                         array      A request array containing:
113
	 *                                    0: int|string The local User's ID, username, or email address.
114
	 *
115
	 * @return array|IXR_Error Information about the user, or error if no such user found:
116
	 *                         roles:     string[] The user's rols.
117
	 *                         login:     string   The user's username.
118
	 *                         email_hash string[] The MD5 hash of the user's normalized email address.
119
	 *                         caps       string[] The user's capabilities.
120
	 *                         allcaps    string[] The user's granular capabilities, merged from role capabilities.
121
	 *                         token_key  string   The Token Key of the user's Jetpack token. Empty string if none.
122
	 */
123
	function get_user( $request ) {
124
		$user_id = is_array( $request ) ? $request[0] : $request;
125
126
		if ( ! $user_id ) {
127
			return $this->error(
128
				new Jetpack_Error(
129
					'invalid_user',
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'invalid_user'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
130
					__( 'Invalid user identifier.', 'jetpack' ),
131
					400
132
				),
133
				'jpc_get_user_fail'
134
			);
135
		}
136
137
		$user = $this->get_user_by_anything( $user_id );
138
139
		if ( ! $user ) {
140
			return $this->error(
141
				new Jetpack_Error(
142
					'user_unknown',
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'user_unknown'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
143
					__( 'User not found.', 'jetpack' ),
144
					404
145
				),
146
				'jpc_get_user_fail'
147
			);
148
		}
149
150
		$connection = Jetpack::connection();
151
		$user_token = $connection->get_access_token( $user->ID );
152
153
		if ( $user_token ) {
154
			list( $user_token_key, $user_token_private ) = explode( '.', $user_token->secret );
0 ignored issues
show
Unused Code introduced by
The assignment to $user_token_private is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
155
			if ( $user_token_key === $user_token->secret ) {
156
				$user_token_key = '';
157
			}
158
		} else {
159
			$user_token_key = '';
160
		}
161
162
		return array(
163
			'id'         => $user->ID,
164
			'login'      => $user->user_login,
165
			'email_hash' => md5( strtolower( trim( $user->user_email ) ) ),
166
			'roles'      => $user->roles,
167
			'caps'       => $user->caps,
168
			'allcaps'    => $user->allcaps,
169
			'token_key'  => $user_token_key,
170
		);
171
	}
172
173
	function remote_authorize( $request ) {
174
		$user = get_user_by( 'id', $request['state'] );
175
		$this->tracking->record_user_event( 'jpc_remote_authorize_begin', array(), $user );
176
177
		foreach( array( 'secret', 'state', 'redirect_uri', 'code' ) as $required ) {
178
			if ( ! isset( $request[ $required ] ) || empty( $request[ $required ] ) ) {
179
				return $this->error( new Jetpack_Error( 'missing_parameter', 'One or more parameters is missing from the request.', 400 ), 'jpc_remote_authorize_fail' );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'missing_parameter'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
180
			}
181
		}
182
183
		if ( ! $user ) {
184
			return $this->error( new Jetpack_Error( 'user_unknown', 'User not found.', 404 ), 'jpc_remote_authorize_fail' );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'user_unknown'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
185
		}
186
187
		if ( Jetpack::is_active() && Jetpack::is_user_connected( $request['state'] ) ) {
188
			return $this->error( new Jetpack_Error( 'already_connected', 'User already connected.', 400 ), 'jpc_remote_authorize_fail' );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'already_connected'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
189
		}
190
191
		$verified = $this->verify_action( array( 'authorize', $request['secret'], $request['state'] ) );
192
193
		if ( is_a( $verified, 'IXR_Error' ) ) {
194
			return $this->error( $verified, 'jpc_remote_authorize_fail' );
195
		}
196
197
		wp_set_current_user( $request['state'] );
198
199
		$client_server = new Jetpack_Client_Server;
200
		$result = $client_server->authorize( $request );
201
202
		if ( is_wp_error( $result ) ) {
203
			return $this->error( $result, 'jpc_remote_authorize_fail' );
204
		}
205
206
		$this->tracking->record_user_event( 'jpc_remote_authorize_success' );
207
208
		return array(
209
			'result' => $result,
210
		);
211
	}
212
213
	/**
214
	 * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to
215
	 * register this site so that a plan can be provisioned.
216
	 *
217
	 * @param array $request An array containing at minimum nonce and local_user keys.
218
	 *
219
	 * @return WP_Error|array
220
	 */
221
	public function remote_register( $request ) {
222
		$this->tracking->record_user_event( 'jpc_remote_register_begin', array() );
223
224
		$user = $this->fetch_and_verify_local_user( $request );
225
226
		if ( ! $user ) {
227
			return $this->error( new WP_Error( 'input_error', __( 'Valid user is required', 'jetpack' ), 400 ), 'jpc_remote_register_fail' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'input_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
228
		}
229
230
		if ( is_wp_error( $user ) || is_a( $user, 'IXR_Error' ) ) {
231
			return $this->error( $user, 'jpc_remote_register_fail' );
232
		}
233
234
		if ( empty( $request['nonce'] ) ) {
235
			return $this->error(
236
				new Jetpack_Error(
237
					'nonce_missing',
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'nonce_missing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
238
					__( 'The required "nonce" parameter is missing.', 'jetpack' ),
239
					400
240
				),
241
				'jpc_remote_register_fail'
242
			);
243
		}
244
245
		$nonce = sanitize_text_field( $request['nonce'] );
246
		unset( $request['nonce'] );
247
248
		$api_url  = Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'partner_provision_nonce_check' ) );
249
		$response = Client::_wp_remote_request(
250
			esc_url_raw( add_query_arg( 'nonce', $nonce, $api_url ) ),
251
			array( 'method' => 'GET' ),
252
			true
253
		);
254
255
		if (
256
			200 !== wp_remote_retrieve_response_code( $response ) ||
257
			'OK' !== trim( wp_remote_retrieve_body( $response ) )
258
		) {
259
			return $this->error(
260
				new Jetpack_Error(
261
					'invalid_nonce',
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'invalid_nonce'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
262
					__( 'There was an issue validating this request.', 'jetpack' ),
263
					400
264
				),
265
				'jpc_remote_register_fail'
266
			);
267
		}
268
269
		if ( ! Jetpack_Options::get_option( 'id' ) || ! Jetpack_Data::get_access_token() || ! empty( $request['force'] ) ) {
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Data::get_access_token() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
270
			wp_set_current_user( $user->ID );
271
272
			// This code mostly copied from Jetpack::admin_page_load.
273
			Jetpack::maybe_set_version_option();
274
			$registered = Jetpack::try_registration();
275
			if ( is_wp_error( $registered ) ) {
276
				return $this->error( $registered, 'jpc_remote_register_fail' );
277
			} elseif ( ! $registered ) {
278
				return $this->error(
279
					new Jetpack_Error(
280
						'registration_error',
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'registration_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
281
						__( 'There was an unspecified error registering the site', 'jetpack' ),
282
						400
283
					),
284
					'jpc_remote_register_fail'
285
				);
286
			}
287
		}
288
289
		$this->tracking->record_user_event( 'jpc_remote_register_success' );
290
291
		return array(
292
			'client_id' => Jetpack_Options::get_option( 'id' )
293
		);
294
	}
295
296
	/**
297
	 * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to
298
	 * register this site so that a plan can be provisioned.
299
	 *
300
	 * @param array $request An array containing at minimum a nonce key and a local_username key.
301
	 *
302
	 * @return WP_Error|array
303
	 */
304
	public function remote_provision( $request ) {
305
		$user = $this->fetch_and_verify_local_user( $request );
306
307
		if ( ! $user ) {
308
			return $this->error( new WP_Error( 'input_error', __( 'Valid user is required', 'jetpack' ), 400 ), 'jpc_remote_provision_fail' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'input_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
309
		}
310
311
		if ( is_wp_error( $user ) || is_a( $user, 'IXR_Error' ) ) {
312
			return $this->error( $user, 'jpc_remote_provision_fail' );
313
		}
314
315
		$site_icon = get_site_icon_url();
316
317
		$auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) );
318
319
		/** This filter is documented in class.jetpack-cli.php */
320 View Code Duplication
		if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) {
321
			$redirect_uri = add_query_arg(
322
				array(
323
					'action'      => 'jetpack-sso',
324
					'redirect_to' => rawurlencode( admin_url() ),
325
				),
326
				wp_login_url() // TODO: come back to Jetpack dashboard?
327
			);
328
		} else {
329
			$redirect_uri = admin_url();
330
		}
331
332
		// Generate secrets.
333
		$role    = Jetpack::translate_user_to_role( $user );
334
		$secrets = Jetpack::init()->generate_secrets( 'authorize', $user->ID );
335
336
		$response = array(
337
			'jp_version'   => JETPACK__VERSION,
338
			'redirect_uri' => $redirect_uri,
339
			'user_id'      => $user->ID,
340
			'user_email'   => $user->user_email,
341
			'user_login'   => $user->user_login,
342
			'scope'        => Jetpack::sign_role( $role, $user->ID ),
343
			'secret'       => $secrets['secret_1'],
344
			'is_active'    => Jetpack::is_active(),
345
		);
346
347
		if ( $site_icon ) {
348
			$response['site_icon'] = $site_icon;
349
		}
350
351
		if ( ! empty( $request['onboarding'] ) ) {
352
			Jetpack::create_onboarding_token();
353
			$response['onboarding_token'] = Jetpack_Options::get_option( 'onboarding' );
354
		}
355
356
		return $response;
357
	}
358
359
	/**
360
	 * Given an array containing a local user identifier and a nonce, will attempt to fetch and set
361
	 * an access token for the given user.
362
	 *
363
	 * @param array $request An array containing local_user and nonce keys at minimum.
364
	 * @return mixed
365
	 */
366
	public function remote_connect( $request, $ixr_client = false ) {
367
		if ( Jetpack::is_active() ) {
368
			return $this->error(
369
				new WP_Error(
370
					'already_connected',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'already_connected'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
371
					__( 'Jetpack is already connected.', 'jetpack' ),
372
					400
373
				),
374
				'jpc_remote_connect_fail'
375
			);
376
		}
377
378
		$user = $this->fetch_and_verify_local_user( $request );
379
380
		if ( ! $user || is_wp_error( $user ) || is_a( $user, 'IXR_Error' ) ) {
381
			return $this->error(
382
				new WP_Error(
383
					'input_error',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'input_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
384
					__( 'Valid user is required.', 'jetpack' ),
385
					400
386
				),
387
				'jpc_remote_connect_fail'
388
			);
389
		}
390
391
		if ( empty( $request['nonce'] ) ) {
392
			return $this->error(
393
				new WP_Error(
394
					'input_error',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'input_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
395
					__( 'A non-empty nonce must be supplied.', 'jetpack' ),
396
					400
397
				),
398
				'jpc_remote_connect_fail'
399
			);
400
		}
401
402
		if ( ! $ixr_client ) {
403
			Jetpack::load_xml_rpc_client();
404
			$ixr_client = new Jetpack_IXR_Client();
405
		}
406
		$ixr_client->query( 'jetpack.getUserAccessToken', array(
0 ignored issues
show
Bug introduced by
It seems like $ixr_client is not always an object, but can also be of type boolean. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
407
			'nonce'            => sanitize_text_field( $request['nonce'] ),
408
			'external_user_id' => $user->ID,
409
		) );
410
411
		$token = $ixr_client->isError() ? false : $ixr_client->getResponse();
412
		if ( empty( $token ) ) {
413
			return $this->error(
414
				new WP_Error(
415
					'token_fetch_failed',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'token_fetch_failed'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
416
					__( 'Failed to fetch user token from WordPress.com.', 'jetpack' ),
417
					400
418
				),
419
				'jpc_remote_connect_fail'
420
			);
421
		}
422
		$token = sanitize_text_field( $token );
423
424
		Jetpack::update_user_token( $user->ID, sprintf( '%s.%d', $token, $user->ID ), true );
425
426
		$this->do_post_authorization();
427
428
		return Jetpack::is_active();
429
	}
430
431
	private function fetch_and_verify_local_user( $request ) {
432
		if ( empty( $request['local_user'] ) ) {
433
			return $this->error(
434
				new Jetpack_Error(
435
					'local_user_missing',
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'local_user_missing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
436
					__( 'The required "local_user" parameter is missing.', 'jetpack' ),
437
					400
438
				),
439
				'jpc_remote_provision_fail'
440
			);
441
		}
442
443
		// local user is used to look up by login, email or ID
444
		$local_user_info = $request['local_user'];
445
446
		return $this->get_user_by_anything( $local_user_info );
447
	}
448
449
	private function get_user_by_anything( $user_id ) {
450
		$user = get_user_by( 'login', $user_id );
451
452
		if ( ! $user ) {
453
			$user = get_user_by( 'email', $user_id );
454
		}
455
456
		if ( ! $user ) {
457
			$user = get_user_by( 'ID', $user_id );
458
		}
459
460
		return $user;
461
	}
462
463
	private function tracks_record_error( $name, $error, $user = null ) {
464
		if ( is_wp_error( $error ) ) {
465
			$this->tracking->record_user_event( $name, array(
466
				'error_code' => $error->get_error_code(),
467
				'error_message' => $error->get_error_message()
468
			), $user );
469
		} elseif( is_a( $error, 'IXR_Error' ) ) {
470
			$this->tracking->record_user_event( $name, array(
471
				'error_code' => $error->code,
472
				'error_message' => $error->message
473
			), $user );
474
		}
475
476
		return $error;
477
	}
478
479
	/**
480
	 * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure
481
	 *
482
	 * Possible error_codes:
483
	 *
484
	 * verify_secret_1_missing
485
	 * verify_secret_1_malformed
486
	 * verify_secrets_missing: verification secrets are not found in database
487
	 * verify_secrets_incomplete: verification secrets are only partially found in database
488
	 * verify_secrets_expired: verification secrets have expired
489
	 * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com
490
	 * state_missing: required parameter of state not found
491
	 * state_malformed: state is not a digit
492
	 * invalid_state: state in request does not match the stored state
493
	 *
494
	 * The 'authorize' and 'register' actions have additional error codes
495
	 *
496
	 * Possible values for action are `authorize`, `publicize` and `register`.
497
	 *
498
	 * state_missing: a state ( user id ) was not supplied
499
	 * state_malformed: state is not the correct data type
500
	 * invalid_state: supplied state does not match the stored state
501
	 */
502
	function verify_action( $params ) {
503
		$action = $params[0];
504
		$verify_secret = $params[1];
505
		$state = isset( $params[2] ) ? $params[2] : '';
506
		$user = get_user_by( 'id', $state );
507
		$tracks_failure_event_name = '';
508
509
		if ( 'authorize' === $action ) {
510
			$tracks_failure_event_name = 'jpc_verify_authorize_fail';
511
			$this->tracking->record_user_event( 'jpc_verify_authorize_begin', array(), $user );
512
		}
513
		if ( 'publicize' === $action ) {
514
			// This action is used on a response from a direct XML-RPC done from WordPress.com
515
			$tracks_failure_event_name = 'jpc_verify_publicize_fail';
516
			$this->tracking->record_user_event( 'jpc_verify_publicize_begin', array(), $user );
517
		}
518
		if ( 'register' === $action ) {
519
			$tracks_failure_event_name = 'jpc_verify_register_fail';
520
			$this->tracking->record_user_event( 'jpc_verify_register_begin', array(), $user );
521
		}
522
523
		if ( empty( $verify_secret ) ) {
524
			return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'verify_secret_1_missing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
525
		} else if ( ! is_string( $verify_secret ) ) {
526
			return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'verify_secret_1_malformed'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
527
		} else if ( empty( $state ) ) {
528
			return $this->error( new Jetpack_Error( 'state_missing', sprintf( 'The required "%s" parameter is missing.', 'state' ), 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'state_missing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
529
		} else if ( ! ctype_digit( $state ) ) {
530
			return $this->error( new Jetpack_Error( 'state_malformed', sprintf( 'The required "%s" parameter is malformed.', 'state' ), 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'state_malformed'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
531
		}
532
533
		$secrets = Jetpack::get_secrets( $action, $state );
534
535
		if ( ! $secrets ) {
536
			Jetpack::delete_secrets( $action, $state );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack::delete_secrets() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
537
			return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification secrets not found', 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'verify_secrets_missing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
538
		}
539
540
		if ( is_wp_error( $secrets ) ) {
541
			Jetpack::delete_secrets( $action, $state );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack::delete_secrets() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
542
			return $this->error( new Jetpack_Error( $secrets->get_error_code(), $secrets->get_error_message(), 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with $secrets->get_error_code().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
543
		}
544
545
		if ( empty( $secrets['secret_1'] ) || empty( $secrets['secret_2'] ) || empty( $secrets['exp'] ) ) {
546
			Jetpack::delete_secrets( $action, $state );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack::delete_secrets() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
547
			return $this->error( new Jetpack_Error( 'verify_secrets_incomplete', 'Verification secrets are incomplete', 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'verify_secrets_incomplete'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
548
		}
549
550
		if ( ! hash_equals( $verify_secret, $secrets['secret_1'] ) ) {
551
			Jetpack::delete_secrets( $action, $state );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack::delete_secrets() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
552
			return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ), $tracks_failure_event_name, $user );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'verify_secrets_mismatch'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
553
		}
554
555
		Jetpack::delete_secrets( $action, $state );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack::delete_secrets() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
556
557
		if ( 'authorize' === $action ) {
558
			$this->tracking->record_user_event( 'jpc_verify_authorize_success', array(), $user );
559
		}
560
		if ( 'publicize' === $action ) {
561
			$this->tracking->record_user_event( 'jpc_verify_publicize_success', array(), $user );
562
		}
563
		if ( 'register' === $action ) {
564
			$this->tracking->record_user_event( 'jpc_verify_register_success', array(), $user );
565
		}
566
567
		return $secrets['secret_2'];
568
	}
569
570
	/**
571
	 * Wrapper for wp_authenticate( $username, $password );
572
	 *
573
	 * @return WP_User|bool
574
	 */
575
	function login() {
576
		Jetpack::init()->require_jetpack_authentication();
577
		$user = wp_authenticate( 'username', 'password' );
578
		if ( is_wp_error( $user ) ) {
579
			if ( 'authentication_failed' == $user->get_error_code() ) { // Generic error could mean most anything.
580
				$this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'invalid_request'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
581
			} else {
582
				$this->error = $user;
583
			}
584
			return false;
585
		} else if ( !$user ) { // Shouldn't happen.
586
			$this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'invalid_request'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
587
			return false;
588
		}
589
590
		return $user;
591
	}
592
593
	/**
594
	 * Returns the current error as an IXR_Error
595
	 *
596
	 * @return bool|IXR_Error
597
	 */
598
	function error( $error = null, $tracks_event_name = null, $user = null ) {
599
		// record using Tracks
600
		if ( null !== $tracks_event_name ) {
601
			$this->tracks_record_error( $tracks_event_name, $error, $user );
602
		}
603
604
		if ( !is_null( $error ) ) {
605
			$this->error = $error;
606
		}
607
608
		if ( is_wp_error( $this->error ) ) {
609
			$code = $this->error->get_error_data();
610
			if ( !$code ) {
611
				$code = -10520;
612
			}
613
			$message = sprintf( 'Jetpack: [%s] %s', $this->error->get_error_code(), $this->error->get_error_message() );
614
			return new IXR_Error( $code, $message );
615
		} else if ( is_a( $this->error, 'IXR_Error' ) ) {
616
			return $this->error;
617
		}
618
619
		return false;
620
	}
621
622
/* API Methods */
623
624
	/**
625
	 * Just authenticates with the given Jetpack credentials.
626
	 *
627
	 * @return string The current Jetpack version number
628
	 */
629
	function test_connection() {
630
		return JETPACK__VERSION;
631
	}
632
633
	function test_api_user_code( $args ) {
634
		$client_id = (int) $args[0];
635
		$user_id   = (int) $args[1];
636
		$nonce     = (string) $args[2];
637
		$verify    = (string) $args[3];
638
639
		if ( !$client_id || !$user_id || !strlen( $nonce ) || 32 !== strlen( $verify ) ) {
640
			return false;
641
		}
642
643
		$user = get_user_by( 'id', $user_id );
644
		if ( !$user || is_wp_error( $user ) ) {
645
			return false;
646
		}
647
648
		/* debugging
649
		error_log( "CLIENT: $client_id" );
650
		error_log( "USER:   $user_id" );
651
		error_log( "NONCE:  $nonce" );
652
		error_log( "VERIFY: $verify" );
653
		*/
654
655
		$jetpack_token = Jetpack_Data::get_access_token( $user_id );
0 ignored issues
show
Documentation introduced by
$user_id 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...
Deprecated Code introduced by
The method Jetpack_Data::get_access_token() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
656
657
		$api_user_code = get_user_meta( $user_id, "jetpack_json_api_$client_id", true );
658
		if ( !$api_user_code ) {
659
			return false;
660
		}
661
662
		$hmac = hash_hmac( 'md5', json_encode( (object) array(
663
			'client_id' => (int) $client_id,
664
			'user_id'   => (int) $user_id,
665
			'nonce'     => (string) $nonce,
666
			'code'      => (string) $api_user_code,
667
		) ), $jetpack_token->secret );
668
669
		if ( ! hash_equals( $hmac, $verify ) ) {
670
			return false;
671
		}
672
673
		return $user_id;
674
	}
675
676
	/**
677
	* Disconnect this blog from the connected wordpress.com account
678
	* @return boolean
679
	*/
680
	function disconnect_blog() {
681
682
		// For tracking
683
		if ( ! empty( $this->user->ID ) ) {
684
			wp_set_current_user( $this->user->ID );
685
		}
686
687
		Jetpack::log( 'disconnect' );
688
		Jetpack::disconnect();
689
690
		return true;
691
	}
692
693
	/**
694
	 * Unlink a user from WordPress.com
695
	 *
696
	 * This will fail if called by the Master User.
697
	 */
698
	function unlink_user() {
699
		Jetpack::log( 'unlink' );
700
		return Jetpack::unlink_user();
701
	}
702
703
	/**
704
	 * Returns any object that is able to be synced
705
	 */
706
	function sync_object( $args ) {
707
		// e.g. posts, post, 5
708
		list( $module_name, $object_type, $id ) = $args;
709
710
		$sync_module = Jetpack_Sync_Modules::get_module( $module_name );
711
		$codec = Jetpack_Sync_Sender::get_instance()->get_codec();
712
713
		return $codec->encode( $sync_module->get_object_by_id( $object_type, $id ) );
714
	}
715
716
	/**
717
	 * Returns the home URL and site URL for the current site which can be used on the WPCOM side for
718
	 * IDC mitigation to decide whether sync should be allowed if the home and siteurl values differ between WPCOM
719
	 * and the remote Jetpack site.
720
	 *
721
	 * @return array
722
	 */
723
	function validate_urls_for_idc_mitigation() {
724
		return array(
725
			'home'    => Jetpack_Sync_Functions::home_url(),
726
			'siteurl' => Jetpack_Sync_Functions::site_url(),
727
		);
728
	}
729
730
	/**
731
	 * Returns what features are available. Uses the slug of the module files.
732
	 *
733
	 * @return array
734
	 */
735 View Code Duplication
	function features_available() {
736
		$raw_modules = Jetpack::get_available_modules();
737
		$modules = array();
738
		foreach ( $raw_modules as $module ) {
739
			$modules[] = Jetpack::get_module_slug( $module );
740
		}
741
742
		return $modules;
743
	}
744
745
	/**
746
	 * Returns what features are enabled. Uses the slug of the modules files.
747
	 *
748
	 * @return array
749
	 */
750 View Code Duplication
	function features_enabled() {
751
		$raw_modules = Jetpack::get_active_modules();
752
		$modules = array();
753
		foreach ( $raw_modules as $module ) {
754
			$modules[] = Jetpack::get_module_slug( $module );
755
		}
756
757
		return $modules;
758
	}
759
760
	function update_attachment_parent( $args ) {
761
		$attachment_id = (int) $args[0];
762
		$parent_id     = (int) $args[1];
763
764
		return wp_update_post( array(
765
			'ID'          => $attachment_id,
766
			'post_parent' => $parent_id,
767
		) );
768
	}
769
770
	function json_api( $args = array() ) {
771
		$json_api_args = $args[0];
772
		$verify_api_user_args = $args[1];
773
774
		$method       = (string) $json_api_args[0];
775
		$url          = (string) $json_api_args[1];
776
		$post_body    = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2];
777
		$user_details = (array) $json_api_args[4];
778
		$locale       = (string) $json_api_args[5];
779
780
		if ( !$verify_api_user_args ) {
781
			$user_id = 0;
782
		} elseif ( 'internal' === $verify_api_user_args[0] ) {
783
			$user_id = (int) $verify_api_user_args[1];
784
			if ( $user_id ) {
785
				$user = get_user_by( 'id', $user_id );
786
				if ( !$user || is_wp_error( $user ) ) {
787
					return false;
788
				}
789
			}
790
		} else {
791
			$user_id = call_user_func( array( $this, 'test_api_user_code' ), $verify_api_user_args );
792
			if ( !$user_id ) {
793
				return false;
794
			}
795
		}
796
797
		/* debugging
798
		error_log( "-- begin json api via jetpack debugging -- " );
799
		error_log( "METHOD: $method" );
800
		error_log( "URL: $url" );
801
		error_log( "POST BODY: $post_body" );
802
		error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) );
803
		error_log( "VERIFIED USER_ID: " . (int) $user_id );
804
		error_log( "-- end json api via jetpack debugging -- " );
805
		*/
806
807
		if ( 'en' !== $locale ) {
808
			// .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them.
809
			$new_locale = $locale;
810
			if ( strpos( $locale, '-' ) !== false ) {
811
				$locale_pieces = explode( '-', $locale );
812
				$new_locale = $locale_pieces[0];
813
				$new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : '';
814
			} else {
815
				// .com might pass 'fr' because thats what our language files are named as, where core seems
816
				// to do fr_FR - so try that if we don't think we can load the file.
817
				if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) {
818
					$new_locale =  $locale . '_' . strtoupper( $locale );
819
				}
820
			}
821
822
			if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) {
823
				unload_textdomain( 'default' );
824
				load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' );
825
			}
826
		}
827
828
		$old_user = wp_get_current_user();
829
		wp_set_current_user( $user_id );
830
831
		if ( $user_id ) {
832
			$token_key = false;
833
		} else {
834
			$jetpack   = Jetpack::init();
835
			$verified  = $jetpack->verify_xml_rpc_signature();
836
			$token_key = $verified['token_key'];
837
		}
838
839
		$token = Jetpack_Data::get_access_token( $user_id, $token_key );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Data::get_access_token() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
840
		if ( !$token || is_wp_error( $token ) ) {
841
			return false;
842
		}
843
844
		define( 'REST_API_REQUEST', true );
845
		define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
846
847
		// needed?
848
		require_once ABSPATH . 'wp-admin/includes/admin.php';
849
850
		require_once JETPACK__PLUGIN_DIR . 'class.json-api.php';
851
		$api = WPCOM_JSON_API::init( $method, $url, $post_body );
852
		$api->token_details['user'] = $user_details;
853
		require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
854
855
		$display_errors = ini_set( 'display_errors', 0 );
856
		ob_start();
857
		$content_type = $api->serve( false );
0 ignored issues
show
Unused Code introduced by
$content_type 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...
858
		$output = ob_get_clean();
859
		ini_set( 'display_errors', $display_errors );
860
861
		$nonce = wp_generate_password( 10, false );
862
		$hmac  = hash_hmac( 'md5', $nonce . $output, $token->secret );
863
864
		wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 );
865
866
		return array(
867
			(string) $output,
868
			(string) $nonce,
869
			(string) $hmac,
870
		);
871
	}
872
873
	/**
874
	 * Handles authorization actions after connecting a site, such as enabling modules.
875
	 *
876
	 * This do_post_authorization() is used in this class, as opposed to calling
877
	 * Jetpack::handle_post_authorization_actions() directly so that we can mock this method as necessary.
878
	 *
879
	 * @return void
880
	 */
881
	public function do_post_authorization() {
882
		/** This filter is documented in class.jetpack-cli.php */
883
		$enable_sso = apply_filters( 'jetpack_start_enable_sso', true );
884
		Jetpack::handle_post_authorization_actions( $enable_sso, false, false );
885
	}
886
}
887