Completed
Push — update/search/migrate-to-wpes-... ( 9c956e...11a260 )
by Alex
08:29
created

Jetpack_Core_API_Data   D

Complexity

Total Complexity 211

Size/Duplication

Total Lines 844
Duplicated Lines 7.35 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 211
lcom 1
cbo 10
dl 62
loc 844
rs 4
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 11 3
C get_module() 0 33 7
C get_all_options() 0 66 14
F update_data() 52 475 134
F _process_onboarding() 10 127 38
A _process_post_by_email() 0 21 4
C can_request() 0 30 11

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_Core_API_Data 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_Core_API_Data, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This is the base class for every Core API endpoint Jetpack uses.
4
 *
5
 */
6
class Jetpack_Core_API_Module_Toggle_Endpoint
7
	extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint {
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
8
9
	/**
10
	 * Check if the module requires the site to be publicly accessible from WPCOM.
11
	 * If the site meets this requirement, the module is activated. Otherwise an error is returned.
12
	 *
13
	 * @since 4.3.0
14
	 *
15
	 * @param WP_REST_Request $request {
16
	 *     Array of parameters received by request.
17
	 *
18
	 *     @type string $slug Module slug.
19
	 *     @type bool   $active should module be activated.
20
	 * }
21
	 *
22
	 * @return WP_REST_Response|WP_Error A REST response if the request was served successfully, otherwise an error.
23
	 */
24
	public function process( $request ) {
25
		if ( $request['active'] ) {
26
			return $this->activate_module( $request );
27
		} else {
28
			return $this->deactivate_module( $request );
29
		}
30
	}
31
32
	/**
33
	 * If it's a valid Jetpack module, activate it.
34
	 *
35
	 * @since 4.3.0
36
	 *
37
	 * @param string|WP_REST_Request $request It's a WP_REST_Request when called from endpoint /module/<slug>/*
38
	 *                                        and a string when called from Jetpack_Core_API_Data->update_data.
39
	 * {
40
	 *     Array of parameters received by request.
41
	 *
42
	 *     @type string $slug Module slug.
43
	 * }
44
	 *
45
	 * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error.
46
	 */
47
	public function activate_module( $request ) {
48
		$module_slug = '';
0 ignored issues
show
Unused Code introduced by
$module_slug 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...
49
50 View Code Duplication
		if (
51
			(
52
				is_array( $request )
53
				|| is_object( $request )
54
			)
55
			&& isset( $request['slug'] )
56
		) {
57
			$module_slug = $request['slug'];
58
		} else {
59
			$module_slug = $request;
60
		}
61
62 View Code Duplication
		if ( ! Jetpack::is_module( $module_slug ) ) {
63
			return new WP_Error(
64
				'not_found',
65
				esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ),
66
				array( 'status' => 404 )
67
			);
68
		}
69
70 View Code Duplication
		if ( Jetpack::activate_module( $module_slug, false, false ) ) {
71
			return rest_ensure_response( array(
72
				'code' 	  => 'success',
73
				'message' => esc_html__( 'The requested Jetpack module was activated.', 'jetpack' ),
74
			) );
75
		}
76
77
		return new WP_Error(
78
			'activation_failed',
79
			esc_html__( 'The requested Jetpack module could not be activated.', 'jetpack' ),
80
			array( 'status' => 424 )
81
		);
82
	}
83
84
	/**
85
	 * If it's a valid Jetpack module, deactivate it.
86
	 *
87
	 * @since 4.3.0
88
	 *
89
	 * @param string|WP_REST_Request $request It's a WP_REST_Request when called from endpoint /module/<slug>/*
90
	 *                                        and a string when called from Jetpack_Core_API_Data->update_data.
91
	 * {
92
	 *     Array of parameters received by request.
93
	 *
94
	 *     @type string $slug Module slug.
95
	 * }
96
	 *
97
	 * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error.
98
	 */
99
	public function deactivate_module( $request ) {
100
		$module_slug = '';
0 ignored issues
show
Unused Code introduced by
$module_slug 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...
101
102 View Code Duplication
		if (
103
			(
104
				is_array( $request )
105
				|| is_object( $request )
106
			)
107
			&& isset( $request['slug'] )
108
		) {
109
			$module_slug = $request['slug'];
110
		} else {
111
			$module_slug = $request;
112
		}
113
114 View Code Duplication
		if ( ! Jetpack::is_module( $module_slug ) ) {
115
			return new WP_Error(
116
				'not_found',
117
				esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ),
118
				array( 'status' => 404 )
119
			);
120
		}
121
122 View Code Duplication
		if ( ! Jetpack::is_module_active( $module_slug ) ) {
123
			return new WP_Error(
124
				'already_inactive',
125
				esc_html__( 'The requested Jetpack module was already inactive.', 'jetpack' ),
126
				array( 'status' => 409 )
127
			);
128
		}
129
130 View Code Duplication
		if ( Jetpack::deactivate_module( $module_slug ) ) {
131
			return rest_ensure_response( array(
132
				'code' 	  => 'success',
133
				'message' => esc_html__( 'The requested Jetpack module was deactivated.', 'jetpack' ),
134
			) );
135
		}
136
		return new WP_Error(
137
			'deactivation_failed',
138
			esc_html__( 'The requested Jetpack module could not be deactivated.', 'jetpack' ),
139
			array( 'status' => 400 )
140
		);
141
	}
142
143
	/**
144
	 * Check that the current user has permissions to manage Jetpack modules.
145
	 *
146
	 * @since 4.3.0
147
	 *
148
	 * @return bool
149
	 */
150
	public function can_request() {
151
		return current_user_can( 'jetpack_manage_modules' );
152
	}
153
}
154
155
class Jetpack_Core_API_Module_List_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
156
157
	/**
158
	 * A WordPress REST API callback method that accepts a request object and decides what to do with it.
159
	 *
160
	 * @param WP_REST_Request $request The request sent to the WP REST API.
161
	 *
162
	 * @since 4.3.0
163
	 *
164
	 * @return bool|Array|WP_Error a resulting value or object, or an error.
165
	 */
166
	public function process( $request ) {
167
		if ( 'GET' === $request->get_method() ) {
168
			return $this->get_modules( $request );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Core_API_Module_...Endpoint::get_modules() has too many arguments starting with $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...
169
		} else {
170
			return $this->activate_modules( $request );
171
		}
172
	}
173
174
	/**
175
	 * Get a list of all Jetpack modules and their information.
176
	 *
177
	 * @since 4.3.0
178
	 *
179
	 * @return array Array of Jetpack modules.
180
	 */
181
	public function get_modules() {
182
		require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' );
183
184
		$modules = Jetpack_Admin::init()->get_modules();
185
		foreach ( $modules as $slug => $properties ) {
186
			$modules[ $slug ]['options'] =
187
				Jetpack_Core_Json_Api_Endpoints::prepare_options_for_response( $slug );
188
			if (
189
				isset( $modules[ $slug ]['requires_connection'] )
190
				&& $modules[ $slug ]['requires_connection']
191
				&& Jetpack::is_development_mode()
192
			) {
193
				$modules[ $slug ]['activated'] = false;
194
			}
195
		}
196
197
		$modules = Jetpack::get_translated_modules( $modules );
198
199
		return Jetpack_Core_Json_Api_Endpoints::prepare_modules_for_response( $modules );
200
	}
201
202
	/**
203
	 * Activate a list of valid Jetpack modules.
204
	 *
205
	 * @since 4.3.0
206
	 *
207
	 * @param WP_REST_Request $request {
208
	 *     Array of parameters received by request.
209
	 *
210
	 *     @type string $slug Module slug.
211
	 * }
212
	 *
213
	 * @return bool|WP_Error True if modules were activated. Otherwise, a WP_Error instance with the corresponding error.
214
	 */
215
	public static function activate_modules( $request ) {
216
217 View Code Duplication
		if (
218
			! isset( $request['modules'] )
219
			|| ! is_array( $request['modules'] )
220
		) {
221
			return new WP_Error(
222
				'not_found',
223
				esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ),
224
				array( 'status' => 404 )
225
			);
226
		}
227
228
		$activated = array();
229
		$failed = array();
230
231
		foreach ( $request['modules'] as $module ) {
232
			if ( Jetpack::activate_module( $module, false, false ) ) {
233
				$activated[] = $module;
234
			} else {
235
				$failed[] = $module;
236
			}
237
		}
238
239 View Code Duplication
		if ( empty( $failed ) ) {
240
			return rest_ensure_response( array(
241
				'code' 	  => 'success',
242
				'message' => esc_html__( 'All modules activated.', 'jetpack' ),
243
			) );
244
		}
245
246
		$error = '';
247
248
		$activated_count = count( $activated );
249 View Code Duplication
		if ( $activated_count > 0 ) {
250
			$activated_last = array_pop( $activated );
251
			$activated_text = $activated_count > 1 ? sprintf(
252
				/* Translators: first variable is a list followed by the last item, which is the second variable. Example: dog, cat and bird. */
253
				__( '%1$s and %2$s', 'jetpack' ),
254
				join( ', ', $activated ), $activated_last ) : $activated_last;
255
256
			$error = sprintf(
257
				/* Translators: the variable is a module name. */
258
				_n( 'The module %s was activated.', 'The modules %s were activated.', $activated_count, 'jetpack' ),
259
				$activated_text ) . ' ';
260
		}
261
262
		$failed_count = count( $failed );
263 View Code Duplication
		if ( count( $failed ) > 0 ) {
264
			$failed_last = array_pop( $failed );
265
			$failed_text = $failed_count > 1 ? sprintf(
266
				/* Translators: first variable is a list followed by the last item, which is the second variable. Example: dog, cat and bird. */
267
				__( '%1$s and %2$s', 'jetpack' ),
268
				join( ', ', $failed ), $failed_last ) : $failed_last;
269
270
			$error = sprintf(
271
				/* Translators: the variable is a module name. */
272
				_n( 'The module %s failed to be activated.', 'The modules %s failed to be activated.', $failed_count, 'jetpack' ),
273
				$failed_text ) . ' ';
274
		}
275
276
		return new WP_Error(
277
			'activation_failed',
278
			esc_html( $error ),
279
			array( 'status' => 424 )
280
		);
281
	}
282
283
	/**
284
	 * A WordPress REST API permission callback method that accepts a request object and decides
285
	 * if the current user has enough privileges to act.
286
	 *
287
	 * @since 4.3.0
288
	 *
289
	 * @param WP_REST_Request $request The request sent to the WP REST API.
290
	 *
291
	 * @return bool does the current user have enough privilege.
292
	 */
293
	public function can_request( $request ) {
294
		if ( 'GET' === $request->get_method() ) {
295
			return current_user_can( 'jetpack_admin_page' );
296
		} else {
297
			return current_user_can( 'jetpack_manage_modules' );
298
		}
299
	}
300
}
301
302
/**
303
 * Class that manages updating of Jetpack module options and general Jetpack settings or retrieving module data.
304
 * If no module is specified, all module settings are retrieved/updated.
305
 *
306
 * @since 4.3.0
307
 * @since 4.4.0 Renamed Jetpack_Core_API_Module_Endpoint from to Jetpack_Core_API_Data.
308
 *
309
 * @author Automattic
310
 */
311
class Jetpack_Core_API_Data extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
312
313
	/**
314
	 * Process request by returning the module or updating it.
315
	 * If no module is specified, settings for all modules are assumed.
316
	 *
317
	 * @since 4.3.0
318
	 *
319
	 * @param WP_REST_Request $request
320
	 *
321
	 * @return bool|mixed|void|WP_Error
322
	 */
323
	public function process( $request ) {
324
		if ( 'GET' === $request->get_method() ) {
325
			if ( isset( $request['slug'] ) ) {
326
				return $this->get_module( $request );
327
			}
328
329
			return $this->get_all_options();
330
		} else {
331
			return $this->update_data( $request );
332
		}
333
	}
334
335
	/**
336
	 * Get information about a specific and valid Jetpack module.
337
	 *
338
	 * @since 4.3.0
339
	 *
340
	 * @param WP_REST_Request $request {
341
	 *     Array of parameters received by request.
342
	 *
343
	 *     @type string $slug Module slug.
344
	 * }
345
	 *
346
	 * @return mixed|void|WP_Error
347
	 */
348
	public function get_module( $request ) {
349
		if ( Jetpack::is_module( $request['slug'] ) ) {
350
351
			$module = Jetpack::get_module( $request['slug'] );
352
353
			$module['options'] = Jetpack_Core_Json_Api_Endpoints::prepare_options_for_response( $request['slug'] );
354
355
			if (
356
				isset( $module['requires_connection'] )
357
				&& $module['requires_connection']
358
				&& Jetpack::is_development_mode()
359
			) {
360
				$module['activated'] = false;
361
			}
362
363
			$i18n = jetpack_get_module_i18n( $request['slug'] );
364
			if ( isset( $module['name'] ) ) {
365
				$module['name'] = $i18n['name'];
366
			}
367
			if ( isset( $module['description'] ) ) {
368
				$module['description'] = $i18n['description'];
369
				$module['short_description'] = $i18n['description'];
370
			}
371
372
			return Jetpack_Core_Json_Api_Endpoints::prepare_modules_for_response( $module );
373
		}
374
375
		return new WP_Error(
376
			'not_found',
377
			esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ),
378
			array( 'status' => 404 )
379
		);
380
	}
381
382
	/**
383
	 * Get information about all Jetpack module options and settings.
384
	 *
385
	 * @since 4.6.0
386
	 *
387
	 * @return WP_REST_Response $response
388
	 */
389
	public function get_all_options() {
390
		$response = array();
391
392
		$modules = Jetpack::get_available_modules();
393
		if ( is_array( $modules ) && ! empty( $modules ) ) {
394
			foreach ( $modules as $module ) {
395
				// Add all module options
396
				$options = Jetpack_Core_Json_Api_Endpoints::prepare_options_for_response( $module );
397
				foreach ( $options as $option_name => $option ) {
398
					$response[ $option_name ] = $option['current_value'];
399
				}
400
401
				// Add the module activation state
402
				$response[ $module ] = Jetpack::is_module_active( $module );
403
			}
404
		}
405
406
		$settings = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( 'settings' );
407
		$holiday_snow_option_name = Jetpack_Core_Json_Api_Endpoints::holiday_snow_option_name();
408
409
		if ( ! function_exists( 'is_plugin_active' ) ) {
410
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
411
		}
412
413
		foreach ( $settings as $setting => $properties ) {
414
			switch ( $setting ) {
415
				case $holiday_snow_option_name:
416
					$response[ $setting ] = get_option( $holiday_snow_option_name ) === 'letitsnow';
417
					break;
418
419
				case 'wordpress_api_key':
420
					// When field is clear, return empty. Otherwise it would return "false".
421
					if ( '' === get_option( 'wordpress_api_key', '' ) ) {
422
						$response[ $setting ] = '';
423
					} else {
424
						if ( ! class_exists( 'Akismet' ) ) {
425
							if ( is_readable( WP_PLUGIN_DIR . '/akismet/class.akismet.php' ) ) {
426
								require_once WP_PLUGIN_DIR . '/akismet/class.akismet.php';
427
							}
428
						}
429
						$response[ $setting ] = class_exists( 'Akismet' ) ? Akismet::get_api_key() : '';
430
					}
431
					break;
432
433
				case 'onboarding':
434
					$response[ $setting ] = array(
435
						'siteTitle' => get_option( 'blogname' ),
436
						'siteDescription' => get_option( 'blogdescription' ),
437
						'siteType' => get_option( 'jpo_site_type' ),
438
						'homepageFormat' => get_option( 'jpo_homepage_format' ),
439
						'addContactForm' => intval( get_option( 'jpo_contact_page' ) ),
440
						'businessAddress' => array(), // TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
441
						'installWooCommerce' => is_plugin_active( 'woocommerce/woocommerce.php' ),
442
					);
443
					break;
444
445
				default:
446
					$response[ $setting ] = Jetpack_Core_Json_Api_Endpoints::cast_value( get_option( $setting ), $settings[ $setting ] );
447
					break;
448
			}
449
		}
450
451
		$response['akismet'] = is_plugin_active( 'akismet/akismet.php' );
452
453
		return rest_ensure_response( $response );
454
	}
455
456
	/**
457
	 * If it's a valid Jetpack module and configuration parameters have been sent, update it.
458
	 *
459
	 * @since 4.3.0
460
	 *
461
	 * @param WP_REST_Request $request {
462
	 *     Array of parameters received by request.
463
	 *
464
	 *     @type string $slug Module slug.
465
	 * }
466
	 *
467
	 * @return bool|WP_Error True if module was updated. Otherwise, a WP_Error instance with the corresponding error.
468
	 */
469
	public function update_data( $request ) {
470
471
		// If it's null, we're trying to update many module options from different modules.
472
		if ( is_null( $request['slug'] ) ) {
473
474
			// Value admitted by Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list that will make it return all module options.
475
			// It will not be passed. It's just checked in this method to pass that method a string or array.
476
			$request['slug'] = 'any';
477
		} else {
478 View Code Duplication
			if ( ! Jetpack::is_module( $request['slug'] ) ) {
479
				return new WP_Error( 'not_found', esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), array( 'status' => 404 ) );
480
			}
481
482 View Code Duplication
			if ( ! Jetpack::is_module_active( $request['slug'] ) ) {
483
				return new WP_Error( 'inactive', esc_html__( 'The requested Jetpack module is inactive.', 'jetpack' ), array( 'status' => 409 ) );
484
			}
485
		}
486
487
		// Get parameters to update the module. We can not simply use $request->get_params() because when we registered
488
		// this route, we are adding the entire output of Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list() to
489
		// the current request object's params. We are interested in body of the actual request.
490
		// This may be JSON:
491
		$params = $request->get_json_params();
492
		if ( ! is_array( $params ) ) {
493
			// Or it may be standard POST key-value pairs:
494
			$params = $request->get_body_params();
495
		}
496
497
		// Exit if no parameters were passed.
498
		if ( ! is_array( $params ) ) {
499
			return new WP_Error( 'missing_options', esc_html__( 'Missing options.', 'jetpack' ), array( 'status' => 404 ) );
500
		}
501
502
		// If $params was set via `get_body_params()` there may be some additional variables in the request that can
503
		// cause validation to fail. This method verifies that each param was in fact updated and will throw a `some_updated`
504
		// error if unused variables are included in the request.
505
		foreach ( array_keys( $params ) as $key ) {
506
			if ( is_int( $key ) || 'slug' === $key || 'context' === $key ) {
507
				unset( $params[ $key ] );
508
			}
509
		}
510
511
		// Get available module options.
512
		$options = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( 'any' === $request['slug']
513
			? $params
514
			: $request['slug']
515
		);
516
517
		// Prepare to toggle module if needed
518
		$toggle_module = new Jetpack_Core_API_Module_Toggle_Endpoint( new Jetpack_IXR_Client() );
519
520
		// Options that are invalid or failed to update.
521
		$invalid = array_keys( array_diff_key( $params, $options ) );
522
		$not_updated = array();
523
524
		// Remove invalid options
525
		$params = array_intersect_key( $params, $options );
526
527
		// Used if response is successful. The message can be overwritten and additional data can be added here.
528
		$response = array(
529
			'code'	  => 'success',
530
			'message' => esc_html__( 'The requested Jetpack data updates were successful.', 'jetpack' ),
531
		);
532
533
		// If there are modules to activate, activate them first so they're ready when their options are set.
534
		foreach ( $params as $option => $value ) {
535
			if ( 'modules' === $options[ $option ]['jp_group'] ) {
536
537
				// Used if there was an error. Can be overwritten with specific error messages.
538
				$error = '';
539
540
				// Set to true if the module toggling was successful.
541
				$updated = false;
542
543
				// Check if user can toggle the module.
544
				if ( $toggle_module->can_request() ) {
545
546
					// Activate or deactivate the module according to the value passed.
547
					$toggle_result = $value
548
						? $toggle_module->activate_module( $option )
549
						: $toggle_module->deactivate_module( $option );
550
551
					if (
552
						is_wp_error( $toggle_result )
553
						&& 'already_inactive' === $toggle_result->get_error_code()
554
					) {
555
556
						// If the module is already inactive, we don't fail
557
						$updated = true;
558
					} elseif ( is_wp_error( $toggle_result ) ) {
559
						$error = $toggle_result->get_error_message();
560
					} else {
561
						$updated = true;
562
					}
563
				} else {
564
					$error = Jetpack_Core_Json_Api_Endpoints::$user_permissions_error_msg;
565
				}
566
567
				// The module was not toggled.
568
				if ( ! $updated ) {
569
					$not_updated[ $option ] = $error;
570
				}
571
572
				// Remove module from list so we don't go through it again.
573
				unset( $params[ $option ] );
574
			}
575
		}
576
577
		foreach ( $params as $option => $value ) {
578
579
			// Used if there was an error. Can be overwritten with specific error messages.
580
			$error = '';
581
582
			// Set to true if the option update was successful.
583
			$updated = false;
584
585
			// Get option attributes, including the group it belongs to.
586
			$option_attrs = $options[ $option ];
587
588
			// If this is a module option and the related module isn't active for any reason, continue with the next one.
589
			if ( 'settings' !== $option_attrs['jp_group'] ) {
590 View Code Duplication
				if ( ! Jetpack::is_module( $option_attrs['jp_group'] ) ) {
591
					$not_updated[ $option ] = esc_html__( 'The requested Jetpack module was not found.', 'jetpack' );
592
					continue;
593
				}
594
595 View Code Duplication
				if (
596
					'any' !== $request['slug']
597
					&& ! Jetpack::is_module_active( $option_attrs['jp_group'] )
598
				) {
599
600
					// We only take note of skipped options when updating one module
601
					$not_updated[ $option ] = esc_html__( 'The requested Jetpack module is inactive.', 'jetpack' );
602
					continue;
603
				}
604
			}
605
606
			// Properly cast value based on its type defined in endpoint accepted args.
607
			$value = Jetpack_Core_Json_Api_Endpoints::cast_value( $value, $option_attrs );
608
609
			switch ( $option ) {
610
				case 'monitor_receive_notifications':
611
					$monitor = new Jetpack_Monitor();
612
613
					// If we got true as response, consider it done.
614
					$updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value );
615
					break;
616
617
				case 'post_by_email_address':
618
					if ( 'create' == $value ) {
619
						$result = $this->_process_post_by_email(
620
							'jetpack.createPostByEmailAddress',
621
							esc_html__( 'Unable to create the Post by Email address. Please try again later.', 'jetpack' )
622
						);
623
					} elseif ( 'regenerate' == $value ) {
624
						$result = $this->_process_post_by_email(
625
							'jetpack.regeneratePostByEmailAddress',
626
							esc_html__( 'Unable to regenerate the Post by Email address. Please try again later.', 'jetpack' )
627
						);
628
					} elseif ( 'delete' == $value ) {
629
						$result = $this->_process_post_by_email(
630
							'jetpack.deletePostByEmailAddress',
631
							esc_html__( 'Unable to delete the Post by Email address. Please try again later.', 'jetpack' )
632
						);
633
					} else {
634
						$result = false;
635
					}
636
637
					// If we got an email address (create or regenerate) or 1 (delete), consider it done.
638
					if ( is_string( $result ) && preg_match( '/[a-z0-9][email protected]/', $result ) ) {
639
						$response[$option] = $result;
640
						$updated           = true;
641
					} elseif ( 1 == $result ) {
642
						$updated = true;
643
					} elseif ( is_array( $result ) && isset( $result['message'] ) ) {
644
						$error = $result['message'];
645
					}
646
					break;
647
648
				case 'jetpack_protect_key':
649
					$protect = Jetpack_Protect_Module::instance();
650
					if ( 'create' == $value ) {
651
						$result = $protect->get_protect_key();
652
					} else {
653
						$result = false;
654
					}
655
656
					// If we got one of Protect keys, consider it done.
657
					if ( preg_match( '/[a-z0-9]{40,}/i', $result ) ) {
658
						$response[$option] = $result;
659
						$updated           = true;
660
					}
661
					break;
662
663
				case 'jetpack_protect_global_whitelist':
664
					$updated = jetpack_protect_save_whitelist( explode( PHP_EOL, str_replace( array( ' ', ',' ), array( '', "\n" ), $value ) ) );
665
					if ( is_wp_error( $updated ) ) {
666
						$error = $updated->get_error_message();
667
					}
668
					break;
669
670
				case 'show_headline':
671
				case 'show_thumbnails':
672
					$grouped_options          = $grouped_options_current = (array) Jetpack_Options::get_option( 'relatedposts' );
673
					$grouped_options[$option] = $value;
674
675
					// If option value was the same, consider it done.
676
					$updated = $grouped_options_current != $grouped_options ? Jetpack_Options::update_option( 'relatedposts', $grouped_options ) : true;
677
					break;
678
679
				case 'google':
680
				case 'bing':
681
				case 'pinterest':
682 View Code Duplication
				case 'yandex':
683
					$grouped_options          = $grouped_options_current = (array) get_option( 'verification_services_codes' );
684
					$grouped_options[$option] = $value;
685
686
					// If option value was the same, consider it done.
687
					$updated = $grouped_options_current != $grouped_options ? update_option( 'verification_services_codes', $grouped_options ) : true;
688
					break;
689
690
				case 'sharing_services':
691
					if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
692
						break;
693
					}
694
695
					$sharer = new Sharing_Service();
696
697
					// If option value was the same, consider it done.
698
					$updated = $value != $sharer->get_blog_services() ? $sharer->set_blog_services( $value['visible'], $value['hidden'] ) : true;
699
					break;
700
701
				case 'button_style':
702
				case 'sharing_label':
703
				case 'show':
704
					if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
705
						break;
706
					}
707
708
					$sharer = new Sharing_Service();
709
					$grouped_options = $sharer->get_global_options();
710
					$grouped_options[ $option ] = $value;
711
					$updated = $sharer->set_global_options( $grouped_options );
712
					break;
713
714
				case 'custom':
715
					if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
716
						break;
717
					}
718
719
					$sharer = new Sharing_Service();
720
					$updated = $sharer->new_service( stripslashes( $value['sharing_name'] ), stripslashes( $value['sharing_url'] ), stripslashes( $value['sharing_icon'] ) );
721
722
					// Return new custom service
723
					$response[$option] = $updated;
724
					break;
725
726
				case 'sharing_delete_service':
727
					if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
728
						break;
729
					}
730
731
					$sharer = new Sharing_Service();
732
					$updated = $sharer->delete_service( $value );
733
					break;
734
735
				case 'jetpack-twitter-cards-site-tag':
736
					$value   = trim( ltrim( strip_tags( $value ), '@' ) );
737
					$updated = get_option( $option ) !== $value ? update_option( $option, $value ) : true;
738
					break;
739
740
				case 'onpublish':
741
				case 'onupdate':
742
				case 'Bias Language':
743
				case 'Cliches':
744
				case 'Complex Expression':
745
				case 'Diacritical Marks':
746
				case 'Double Negative':
747
				case 'Hidden Verbs':
748
				case 'Jargon Language':
749
				case 'Passive voice':
750
				case 'Phrases to Avoid':
751
				case 'Redundant Expression':
752
				case 'guess_lang':
753
					if ( in_array( $option, array( 'onpublish', 'onupdate' ) ) ) {
754
						$atd_option = 'AtD_check_when';
755
					} elseif ( 'guess_lang' == $option ) {
756
						$atd_option = 'AtD_guess_lang';
757
						$option     = 'true';
758
					} else {
759
						$atd_option = 'AtD_options';
760
					}
761
					$user_id                 = get_current_user_id();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 17 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
762
					if ( ! function_exists( 'AtD_get_options' ) ) {
763
						include_once( JETPACK__PLUGIN_DIR . 'modules/after-the-deadline.php' );
764
					}
765
					$grouped_options_current = AtD_get_options( $user_id, $atd_option );
766
					unset( $grouped_options_current['name'] );
767
					$grouped_options = $grouped_options_current;
768
					if ( $value && ! isset( $grouped_options [$option] ) ) {
769
						$grouped_options [$option] = $value;
770
					} elseif ( ! $value && isset( $grouped_options [$option] ) ) {
771
						unset( $grouped_options [$option] );
772
					}
773
					// If option value was the same, consider it done, otherwise try to update it.
774
					$options_to_save = implode( ',', array_keys( $grouped_options ) );
775
					$updated         = $grouped_options != $grouped_options_current ? AtD_update_setting( $user_id, $atd_option, $options_to_save ) : true;
776
					break;
777
778
				case 'ignored_phrases':
779
				case 'unignore_phrase':
780
					$user_id         = get_current_user_id();
781
					$atd_option      = 'AtD_ignored_phrases';
782
					$grouped_options = $grouped_options_current = explode( ',', AtD_get_setting( $user_id, $atd_option ) );
783
					if ( 'ignored_phrases' == $option ) {
784
						$grouped_options = explode( ',', $value );
785
					} else {
786
						$index = array_search( $value, $grouped_options );
787
						if ( false !== $index ) {
788
							unset( $grouped_options[$index] );
789
							$grouped_options = array_values( $grouped_options );
790
						}
791
					}
792
					$ignored_phrases = implode( ',', array_filter( array_map( 'strip_tags', $grouped_options ) ) );
793
					$updated         = $grouped_options != $grouped_options_current ? AtD_update_setting( $user_id, $atd_option, $ignored_phrases ) : true;
794
					break;
795
796
				case 'admin_bar':
797
				case 'roles':
798
				case 'count_roles':
799
				case 'blog_id':
800
				case 'do_not_track':
801
				case 'hide_smile':
802 View Code Duplication
				case 'version':
803
					$grouped_options          = $grouped_options_current = (array) get_option( 'stats_options' );
804
					$grouped_options[$option] = $value;
805
806
					// If option value was the same, consider it done.
807
					$updated = $grouped_options_current != $grouped_options ? update_option( 'stats_options', $grouped_options ) : true;
808
					break;
809
810
				case Jetpack_Core_Json_Api_Endpoints::holiday_snow_option_name():
811
					$updated = get_option( $option ) != $value ? update_option( $option, (bool) $value ? 'letitsnow' : '' ) : true;
812
					break;
813
814
				case 'akismet_show_user_comments_approved':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
815
816
					// Save Akismet option '1' or '0' like it's done in akismet/class.akismet-admin.php
817
					$updated = get_option( $option ) != $value ? update_option( $option, (bool) $value ? '1' : '0' ) : true;
818
					break;
819
820
				case 'wordpress_api_key':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
821
822
					if ( ! file_exists( WP_PLUGIN_DIR . '/akismet/class.akismet.php' ) ) {
823
						$error = esc_html__( 'Please install Akismet.', 'jetpack' );
824
						$updated = false;
825
						break;
826
					}
827
828
					if ( ! defined( 'AKISMET_VERSION' ) ) {
829
						$error = esc_html__( 'Please activate Akismet.', 'jetpack' );
830
						$updated = false;
831
						break;
832
					}
833
834
					// Allow to clear the API key field
835
					if ( '' === $value ) {
836
						$updated = get_option( $option ) != $value ? update_option( $option, $value ) : true;
837
						break;
838
					}
839
840
					require_once WP_PLUGIN_DIR . '/akismet/class.akismet.php';
841
					require_once WP_PLUGIN_DIR . '/akismet/class.akismet-admin.php';
842
843
					if ( class_exists( 'Akismet_Admin' ) && method_exists( 'Akismet_Admin', 'save_key' ) ) {
844
						if ( Akismet::verify_key( $value ) === 'valid' ) {
845
							$akismet_user = Akismet_Admin::get_akismet_user( $value );
846
							if ( $akismet_user ) {
847
								if ( in_array( $akismet_user->status, array( 'active', 'active-dunning', 'no-sub' ) ) ) {
848
									$updated = get_option( $option ) != $value ? update_option( $option, $value ) : true;
849
									break;
850
								} else {
851
									$error = esc_html__( "Akismet user status doesn't allow to update the key", 'jetpack' );
852
								}
853
							} else {
854
								$error = esc_html__( 'Invalid Akismet user', 'jetpack' );
855
							}
856
						} else {
857
							$error = esc_html__( 'Invalid Akismet key', 'jetpack' );
858
						}
859
					} else {
860
						$error = esc_html__( 'Akismet is not installed or active', 'jetpack' );
861
					}
862
					$updated = false;
863
					break;
864
865 View Code Duplication
				case 'google_analytics_tracking_id':
866
					$grouped_options = $grouped_options_current = (array) get_option( 'jetpack_wga' );
867
					$grouped_options[ 'code' ] = $value;
868
869
					// If option value was the same, consider it done.
870
					$updated = $grouped_options_current != $grouped_options ? update_option( 'jetpack_wga', $grouped_options ) : true;
871
					break;
872
873
				case 'dismiss_dash_app_card':
874 View Code Duplication
				case 'dismiss_empty_stats_card':
875
					// If option value was the same, consider it done.
876
					$updated = get_option( $option ) != $value ? update_option( $option, (bool) $value ) : true;
877
					break;
878
879
				case 'onboarding':
880
					// Break apart and set Jetpack onboarding options.
881
					$result = $this->_process_onboarding( (array) $value );
882
					if ( empty( $result ) ) {
883
						$updated = true;
884
					} else {
885
						$error = sprintf( esc_html__( 'Onboarding failed to process: %s', 'jetpack' ), $result );
886
						$updated = false;
887
					}
888
					break;
889
890 View Code Duplication
				case 'show_welcome_for_new_plan':
891
					// If option value was the same, consider it done.
892
					$updated = get_option( $option ) !== $value ? update_option( $option, (bool) $value ) : true;
893
					break;
894
895 View Code Duplication
				default:
896
					// If option value was the same, consider it done.
897
					$updated = get_option( $option ) != $value ? update_option( $option, $value ) : true;
898
					break;
899
			}
900
901
			// The option was not updated.
902
			if ( ! $updated ) {
903
				$not_updated[ $option ] = $error;
904
			}
905
		}
906
907
		if ( empty( $invalid ) && empty( $not_updated ) ) {
908
			// The option was updated.
909
			return rest_ensure_response( $response );
910
		} else {
911
			$invalid_count = count( $invalid );
912
			$not_updated_count = count( $not_updated );
913
			$error = '';
914
			if ( $invalid_count > 0 ) {
915
				$error = sprintf(
916
				/* Translators: the plural variable is a comma-separated list. Example: dog, cat, bird. */
917
					_n( 'Invalid option: %s.', 'Invalid options: %s.', $invalid_count, 'jetpack' ),
918
					join( ', ', $invalid )
919
				);
920
			}
921
			if ( $not_updated_count > 0 ) {
922
				$not_updated_messages = array();
923
				foreach ( $not_updated as $not_updated_option => $not_updated_message ) {
924
					if ( ! empty( $not_updated_message ) ) {
925
						$not_updated_messages[] = sprintf(
926
							/* Translators: the first variable is a module option or slug, or setting. The second is the error message . */
927
							__( '%1$s: %2$s', 'jetpack' ),
928
							$not_updated_option, $not_updated_message );
929
					}
930
				}
931
				if ( ! empty( $error ) ) {
932
					$error .= ' ';
933
				}
934
				if ( ! empty( $not_updated_messages ) ) {
935
					$error .= ' ' . join( '. ', $not_updated_messages );
936
				}
937
938
			}
939
			// There was an error because some options were updated but others were invalid or failed to update.
940
			return new WP_Error( 'some_updated', esc_html( $error ), array( 'status' => 400 ) );
941
		}
942
943
	}
944
945
	/**
946
	 * Perform tasks in the site based on onboarding choices.
947
	 *
948
	 * @since 5.4.0
949
	 *
950
	 * @param array $data Onboarding choices made by user.
951
	 *
952
	 * @return string Result of onboarding processing and, if there is one, an error message.
953
	 */
954
	private function _process_onboarding( $data ) {
955
		if ( isset( $data['end'] ) && $data['end'] ) {
956
			return Jetpack::invalidate_onboarding_token()
957
				? ''
958
				: esc_html__( "The onboarding token couldn't be deleted.", 'jetpack' );
959
		}
960
961
		$error = array();
962
963
		if ( ! empty( $data['siteTitle'] ) ) {
964
			// If option value was the same, consider it done.
965
			if ( ! ( update_option( 'blogname', $data['siteTitle'] ) || get_option( 'blogname' ) == $data['siteTitle'] ) ) {
966
				$error[] = 'siteTitle';
967
			}
968
		}
969
970
		if ( ! empty( $data['siteDescription'] ) ) {
971
			// If option value was the same, consider it done.
972
			if ( ! ( update_option( 'blogdescription', $data['siteDescription'] ) || get_option( 'blogdescription' ) == $data['siteDescription'] ) ) {
973
				$error[] = 'siteDescription';
974
			}
975
		}
976
977
		$site_title = get_option( 'blogname' );
978
		$author = get_current_user_id() || 1;
979
980
		if ( ! empty( $data['siteType'] ) ) {
981
			if ( ! ( update_option( 'jpo_site_type', $data['siteType'] ) || get_option( 'jpo_site_type' ) == $data['siteType'] ) ) {
982
				$error[] = 'siteType';
983
			}
984
		}
985
986
		if ( isset( $data['homepageFormat'] ) ) {
987
			// If $data['homepageFormat'] is 'posts', we have nothing to do since it's WordPress' default
988
			if ( 'page' === $data['homepageFormat'] ) {
989
				if ( ! ( update_option( 'show_on_front', 'page' ) || get_option( 'show_on_front' ) == 'page' ) ) {
990
					$error[] = 'homepageFormat';
991
				}
992
993
				$home = wp_insert_post( array(
994
					'post_type'     => 'page',
995
					/* translators: this references the home page of a site, also called front page. */
996
					'post_title'    => esc_html_x( 'Home Page', 'The home page of a website.', 'jetpack' ),
997
					'post_content'  => sprintf( esc_html__( 'Welcome to %s.', 'jetpack' ), $site_title ),
998
					'post_status'   => 'publish',
999
					'post_author'   => $author,
1000
				) );
1001 View Code Duplication
				if ( 0 == $home ) {
1002
					$error[] = 'home insert: 0';
1003
				} elseif ( is_wp_error( $home ) ) {
1004
					$error[] = 'home creation: '. $home->get_error_message();
1005
				}
1006
				if ( ! ( update_option( 'page_on_front', $home ) || get_option( 'page_on_front' ) == $home ) ) {
1007
					$error[] = 'home set';
1008
				}
1009
1010
				$blog = wp_insert_post( array(
1011
					'post_type'     => 'page',
1012
					/* translators: this references the page where blog posts are listed. */
1013
					'post_title'    => esc_html_x( 'Blog', 'The blog of a website.', 'jetpack' ),
1014
					'post_content'  => sprintf( esc_html__( 'These are the latest posts in %s.', 'jetpack' ), $site_title ),
1015
					'post_status'   => 'publish',
1016
					'post_author'   => $author,
1017
				) );
1018 View Code Duplication
				if ( 0 == $blog ) {
1019
					$error[] = 'blog insert: 0';
1020
				} elseif ( is_wp_error( $blog ) ) {
1021
					$error[] = 'blog creation: '. $blog->get_error_message();
1022
				}
1023
				if ( ! ( update_option( 'page_for_posts', $blog ) || get_option( 'page_for_posts' ) == $blog ) ) {
1024
					$error[] = 'blog set';
1025
				}
1026
			}
1027
1028
			update_option( 'jpo_homepage_format', $data['homepageFormat'] );
1029
		}
1030
1031
		// Setup contact page and add a form and/or business info
1032
		$contact_page = '';
1033
1034
		if ( isset( $data['addContactForm'] ) && $data['addContactForm'] ) {
1035
			$contact_form_module_active = Jetpack::is_module_active( 'contact-form' );
1036
			if ( ! $contact_form_module_active ) {
1037
				$contact_form_module_active = Jetpack::activate_module( 'contact-form', false, false );
1038
			}
1039
1040
			if ( $contact_form_module_active ) {
1041
				$contact_page = '[contact-form][contact-field label="' . esc_html__( 'Name', 'jetpack' ) . '" type="name" required="true" /][contact-field label="' . esc_html__( 'Email', 'jetpack' ) . '" type="email" required="true" /][contact-field label="' . esc_html__( 'Website', 'jetpack' ) . '" type="url" /][contact-field label="' . esc_html__( 'Message', 'jetpack' ) . '" type="textarea" /][/contact-form]';
1042
			} else {
1043
				$error[] = 'contact-form activate';
1044
			}
1045
		}
1046
1047
		if ( isset( $data['businessPersonal'] ) && 'business' === $data['businessPersonal'] ) {
1048
			$contact_page .= "\n" . join( "\n", $data['businessInfo'] );
1049
		}
1050
1051
		if ( ! empty( $contact_page ) ) {
1052
			$form = wp_insert_post( array(
1053
				'post_type'     => 'page',
1054
				/* translators: this references a page with contact details and possibly a form. */
1055
				'post_title'    => esc_html_x( 'Contact us', 'Contact page for your website.', 'jetpack' ),
1056
				'post_content'  => esc_html__( 'Send us a message!', 'jetpack' ) . "\n" . $contact_page,
1057
				'post_status'   => 'publish',
1058
				'post_author'   => $author,
1059
			) );
1060
			if ( 0 == $form ) {
1061
				$error[] = 'form insert: 0';
1062
			} elseif ( is_wp_error( $form ) ) {
1063
				$error[] = 'form creation: '. $form->get_error_message();
1064
			} else {
1065
				update_option( 'jpo_contact_page', $form );
1066
			}
1067
		}
1068
1069
		if ( ! empty( $data['installWooCommerce'] ) ) {
1070
			jetpack_require_lib( 'plugins' );
1071
			$wc_install_result = Jetpack_Plugins::install_and_activate_plugin( 'woocommerce' );
1072
			if ( is_wp_error( $wc_install_result ) ) {
1073
				$error[] = 'woocommerce installation';
1074
			}
1075
		}
1076
1077
		return empty( $error )
1078
			? ''
1079
			: join( ', ', $error );
1080
	}
1081
1082
	/**
1083
	 * Calls WPCOM through authenticated request to create, regenerate or delete the Post by Email address.
1084
	 * @todo: When all settings are updated to use endpoints, move this to the Post by Email module and replace __process_ajax_proxy_request.
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
1085
	 *
1086
	 * @since 4.3.0
1087
	 *
1088
	 * @param string $endpoint Process to call on WPCOM to create, regenerate or delete the Post by Email address.
1089
	 * @param string $error	   Error message to return.
1090
	 *
1091
	 * @return array
1092
	 */
1093
	private function _process_post_by_email( $endpoint, $error ) {
1094
		if ( ! current_user_can( 'edit_posts' ) ) {
1095
			return array( 'message' => $error );
1096
		}
1097
1098
		$this->xmlrpc->query( $endpoint );
1099
1100
		if ( $this->xmlrpc->isError() ) {
1101
			return array( 'message' => $error );
1102
		}
1103
1104
		$response = $this->xmlrpc->getResponse();
1105
		if ( empty( $response ) ) {
1106
			return array( 'message' => $error );
1107
		}
1108
1109
		// Used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
1110
		update_option( 'post_by_email_address' . get_current_user_id(), $response );
1111
1112
		return $response;
1113
	}
1114
1115
	/**
1116
	 * Check if user is allowed to perform the update.
1117
	 *
1118
	 * @since 4.3.0
1119
	 *
1120
	 * @param WP_REST_Request $request The request sent to the WP REST API.
1121
	 *
1122
	 * @return bool
1123
	 */
1124
	public function can_request( $request ) {
1125
		$req_params = $request->get_params();
1126
		if ( ! empty( $req_params['onboarding']['token'] ) && isset( $req_params['rest_route'] ) ) {
1127
			return Jetpack::validate_onboarding_token_action( $req_params['onboarding']['token'], $req_params['rest_route'] );
1128
		}
1129
1130
		if ( 'GET' === $request->get_method() ) {
1131
			return current_user_can( 'jetpack_admin_page' );
1132
		} else {
1133
			$module = Jetpack_Core_Json_Api_Endpoints::get_module_requested();
1134
			if ( empty( $module ) ) {
1135
				$params = $request->get_json_params();
1136
				if ( ! is_array( $params ) ) {
1137
					$params = $request->get_body_params();
1138
				}
1139
				$options = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( $params );
1140
				foreach ( $options as $option => $definition ) {
1141
					if ( in_array( $options[ $option ]['jp_group'], array( 'after-the-deadline', 'post-by-email' ) ) ) {
1142
						$module = $options[ $option ]['jp_group'];
1143
						break;
1144
					}
1145
				}
1146
			}
1147
			// User is trying to create, regenerate or delete its PbE || ATD settings.
1148
			if ( 'post-by-email' === $module || 'after-the-deadline' === $module ) {
1149
				return current_user_can( 'edit_posts' ) && current_user_can( 'jetpack_admin_page' );
1150
			}
1151
			return current_user_can( 'jetpack_configure_modules' );
1152
		}
1153
	}
1154
}
1155
1156
class Jetpack_Core_API_Module_Data_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1157
1158
	public function process( $request ) {
1159
		switch( $request['slug'] ) {
1160
			case 'protect':
1161
				return $this->get_protect_data();
1162
			case 'stats':
1163
				return $this->get_stats_data( $request );
1164
			case 'akismet':
1165
				return $this->get_akismet_data();
1166
			case 'monitor':
1167
				return $this->get_monitor_data();
1168
			case 'verification-tools':
1169
				return $this->get_verification_tools_data();
1170
			case 'vaultpress':
1171
				return $this->get_vaultpress_data();
1172
		}
1173
	}
1174
1175
	/**
1176
	 * Decide against which service to check the key.
1177
	 *
1178
	 * @since 4.8.0
1179
	 *
1180
	 * @param WP_REST_Request $request
1181
	 *
1182
	 * @return bool
1183
	 */
1184
	public function key_check( $request ) {
1185
		switch( $request['service'] ) {
1186
			case 'akismet':
1187
				$params = $request->get_json_params();
1188
				if ( isset( $params['api_key'] ) && ! empty( $params['api_key'] ) ) {
1189
					return $this->check_akismet_key( $params['api_key'] );
1190
				}
1191
				return $this->check_akismet_key();
1192
		}
1193
		return false;
1194
	}
1195
1196
	/**
1197
	 * Get number of blocked intrusion attempts.
1198
	 *
1199
	 * @since 4.3.0
1200
	 *
1201
	 * @return mixed|WP_Error Number of blocked attempts if protection is enabled. Otherwise, a WP_Error instance with the corresponding error.
1202
	 */
1203
	public function get_protect_data() {
1204
		if ( Jetpack::is_module_active( 'protect' ) ) {
1205
			return get_site_option( 'jetpack_protect_blocked_attempts' );
1206
		}
1207
1208
		return new WP_Error(
1209
			'not_active',
1210
			esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ),
1211
			array( 'status' => 404 )
1212
		);
1213
	}
1214
1215
	/**
1216
	 * Get number of spam messages blocked by Akismet.
1217
	 *
1218
	 * @since 4.3.0
1219
	 *
1220
	 * @return int|string Number of spam blocked by Akismet. Otherwise, an error message.
1221
	 */
1222
	public function get_akismet_data() {
1223
		if ( ! is_wp_error( $status = $this->akismet_is_active_and_registered() ) ) {
1224
			return rest_ensure_response( Akismet_Admin::get_stats( Akismet::get_api_key() ) );
1225
		} else {
1226
			return $status->get_error_code();
1227
		}
1228
	}
1229
1230
	/**
1231
	 * Verify the Akismet API key.
1232
	 *
1233
	 * @since 4.8.0
1234
	 *
1235
	 * @param string $api_key Optional API key to check.
1236
	 *
1237
	 * @return array Information about the key. 'validKey' is true if key is valid, false otherwise.
1238
	 */
1239
	public function check_akismet_key( $api_key = '' ) {
1240
		$akismet_status = $this->akismet_class_exists();
1241
		if ( is_wp_error( $akismet_status ) ) {
1242
			return rest_ensure_response( array(
1243
				'validKey'          => false,
1244
				'invalidKeyCode'    => $akismet_status->get_error_code(),
1245
				'invalidKeyMessage' => $akismet_status->get_error_message(),
1246
			) );
1247
		}
1248
1249
		$key_status = Akismet::check_key_status( empty( $api_key ) ? Akismet::get_api_key() : $api_key );
1250
1251 View Code Duplication
		if ( ! $key_status || 'invalid' === $key_status || 'failed' === $key_status ) {
1252
			return rest_ensure_response( array(
1253
				'validKey'          => false,
1254
				'invalidKeyCode'    => 'invalid_key',
1255
				'invalidKeyMessage' => esc_html__( 'Invalid Akismet key. Please contact support.', 'jetpack' ),
1256
			) );
1257
		}
1258
1259
		return rest_ensure_response( array(
1260
			'validKey' => isset( $key_status[1] ) && 'valid' === $key_status[1]
1261
		) );
1262
	}
1263
1264
	/**
1265
	 * Check if Akismet class file exists and if class is loaded.
1266
	 *
1267
	 * @since 4.8.0
1268
	 *
1269
	 * @return bool|WP_Error Returns true if class file exists and class is loaded, WP_Error otherwise.
1270
	 */
1271
	private function akismet_class_exists() {
1272
		if ( ! file_exists( WP_PLUGIN_DIR . '/akismet/class.akismet.php' ) ) {
1273
			return new WP_Error( 'not_installed', esc_html__( 'Please install Akismet.', 'jetpack' ), array( 'status' => 400 ) );
1274
		}
1275
1276 View Code Duplication
		if ( ! class_exists( 'Akismet' ) ) {
1277
			return new WP_Error( 'not_active', esc_html__( 'Please activate Akismet.', 'jetpack' ), array( 'status' => 400 ) );
1278
		}
1279
1280
		return true;
1281
	}
1282
1283
	/**
1284
	 * Is Akismet registered and active?
1285
	 *
1286
	 * @since 4.3.0
1287
	 *
1288
	 * @return bool|WP_Error True if Akismet is active and registered. Otherwise, a WP_Error instance with the corresponding error.
1289
	 */
1290
	private function akismet_is_active_and_registered() {
1291
		if ( is_wp_error( $akismet_exists = $this->akismet_class_exists() ) ) {
1292
			return $akismet_exists;
1293
		}
1294
1295
		// What about if Akismet is put in a sub-directory or maybe in mu-plugins?
1296
		require_once WP_PLUGIN_DIR . '/akismet/class.akismet.php';
1297
		require_once WP_PLUGIN_DIR . '/akismet/class.akismet-admin.php';
1298
		$akismet_key = Akismet::verify_key( Akismet::get_api_key() );
1299
1300 View Code Duplication
		if ( ! $akismet_key || 'invalid' === $akismet_key || 'failed' === $akismet_key ) {
1301
			return new WP_Error( 'invalid_key', esc_html__( 'Invalid Akismet key. Please contact support.', 'jetpack' ), array( 'status' => 400 ) );
1302
		}
1303
1304
		return true;
1305
	}
1306
1307
	/**
1308
	 * Get stats data for this site
1309
	 *
1310
	 * @since 4.1.0
1311
	 *
1312
	 * @param WP_REST_Request $request {
1313
	 *     Array of parameters received by request.
1314
	 *
1315
	 *     @type string $date Date range to restrict results to.
1316
	 * }
1317
	 *
1318
	 * @return int|string Number of spam blocked by Akismet. Otherwise, an error message.
1319
	 */
1320
	public function get_stats_data( WP_REST_Request $request ) {
1321
		// Get parameters to fetch Stats data.
1322
		$range = $request->get_param( 'range' );
1323
1324
		// If no parameters were passed.
1325
		if (
1326
			empty ( $range )
1327
			|| ! in_array( $range, array( 'day', 'week', 'month' ), true )
1328
		) {
1329
			$range = 'day';
1330
		}
1331
1332
		if ( ! function_exists( 'stats_get_from_restapi' ) ) {
1333
			require_once( JETPACK__PLUGIN_DIR . 'modules/stats.php' );
1334
		}
1335
1336
		switch ( $range ) {
1337
1338
			// This is always called first on page load
1339
			case 'day':
1340
				$initial_stats = stats_get_from_restapi();
1341
				return rest_ensure_response( array(
1342
					'general' => $initial_stats,
1343
1344
					// Build data for 'day' as if it was stats_get_from_restapi( array(), 'visits?unit=day&quantity=30' );
1345
					'day' => isset( $initial_stats->visits )
1346
						? $initial_stats->visits
1347
						: array(),
1348
				) );
1349
			case 'week':
1350
				return rest_ensure_response( array(
1351
					'week' => stats_get_from_restapi( array(), 'visits?unit=week&quantity=14' ),
1352
				) );
1353
			case 'month':
1354
				return rest_ensure_response( array(
1355
					'month' => stats_get_from_restapi( array(), 'visits?unit=month&quantity=12&' ),
1356
				) );
1357
		}
1358
	}
1359
1360
	/**
1361
	 * Get date of last downtime.
1362
	 *
1363
	 * @since 4.3.0
1364
	 *
1365
	 * @return mixed|WP_Error Number of days since last downtime. Otherwise, a WP_Error instance with the corresponding error.
1366
	 */
1367
	public function get_monitor_data() {
1368 View Code Duplication
		if ( ! Jetpack::is_module_active( 'monitor' ) ) {
1369
			return new WP_Error(
1370
				'not_active',
1371
				esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ),
1372
				array( 'status' => 404 )
1373
			);
1374
		}
1375
1376
		$monitor       = new Jetpack_Monitor();
1377
		$last_downtime = $monitor->monitor_get_last_downtime();
1378
		if ( is_wp_error( $last_downtime ) ) {
1379
			return $last_downtime;
1380
		} else if ( false === strtotime( $last_downtime ) ) {
1381
			return rest_ensure_response( array(
1382
				'code' => 'success',
1383
				'date' => null,
1384
			) );
1385
		} else {
1386
			return rest_ensure_response( array(
1387
				'code' => 'success',
1388
				'date' => human_time_diff( strtotime( $last_downtime ), strtotime( 'now' ) ),
1389
			) );
1390
		}
1391
	}
1392
1393
	/**
1394
	 * Get services that this site is verified with.
1395
	 *
1396
	 * @since 4.3.0
1397
	 *
1398
	 * @return mixed|WP_Error List of services that verified this site. Otherwise, a WP_Error instance with the corresponding error.
1399
	 */
1400
	public function get_verification_tools_data() {
1401 View Code Duplication
		if ( ! Jetpack::is_module_active( 'verification-tools' ) ) {
1402
			return new WP_Error(
1403
				'not_active',
1404
				esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ),
1405
				array( 'status' => 404 )
1406
			);
1407
		}
1408
1409
		$verification_services_codes = get_option( 'verification_services_codes' );
1410 View Code Duplication
		if (
1411
			! is_array( $verification_services_codes )
1412
			|| empty( $verification_services_codes )
1413
		) {
1414
			return new WP_Error(
1415
				'empty',
1416
				esc_html__( 'Site not verified with any service.', 'jetpack' ),
1417
				array( 'status' => 404 )
1418
			);
1419
		}
1420
1421
		$services = array();
1422
		foreach ( jetpack_verification_services() as $name => $service ) {
1423
			if ( is_array( $service ) && ! empty( $verification_services_codes[ $name ] ) ) {
1424
				switch ( $name ) {
1425
					case 'google':
1426
						$services[] = 'Google';
1427
						break;
1428
					case 'bing':
1429
						$services[] = 'Bing';
1430
						break;
1431
					case 'pinterest':
1432
						$services[] = 'Pinterest';
1433
						break;
1434
					case 'yandex':
1435
						$services[] = 'Yandex';
1436
						break;
1437
				}
1438
			}
1439
		}
1440
1441
		if ( empty( $services ) ) {
1442
			return new WP_Error(
1443
				'empty',
1444
				esc_html__( 'Site not verified with any service.', 'jetpack' ),
1445
				array( 'status' => 404 )
1446
			);
1447
		}
1448
1449
		if ( 2 > count( $services ) ) {
1450
			$message = esc_html(
1451
				sprintf(
1452
					/* translators: %s is a service name like Google, Bing, Pinterest, etc. */
1453
					__( 'Your site is verified with %s.', 'jetpack' ),
1454
					$services[0]
1455
				)
1456
			);
1457
		} else {
1458
			$copy_services = $services;
1459
			$last = count( $copy_services ) - 1;
1460
			$last_service = $copy_services[ $last ];
1461
			unset( $copy_services[ $last ] );
1462
			$message = esc_html(
1463
				sprintf(
1464
					/* translators: %1$s is a comma separated list of services, and %2$s is a single service name like Google, Bing, Pinterest, etc. */
1465
					__( 'Your site is verified with %1$s and %2$s.', 'jetpack' ),
1466
					join( ', ', $copy_services ),
1467
					$last_service
1468
				)
1469
			);
1470
		}
1471
1472
		return rest_ensure_response( array(
1473
			'code'     => 'success',
1474
			'message'  => $message,
1475
			'services' => $services,
1476
		) );
1477
	}
1478
1479
	/**
1480
	 * Get VaultPress site data including, among other things, the date of the last backup if it was completed.
1481
	 *
1482
	 * @since 4.3.0
1483
	 *
1484
	 * @return mixed|WP_Error VaultPress site data. Otherwise, a WP_Error instance with the corresponding error.
1485
	 */
1486
	public function get_vaultpress_data() {
1487 View Code Duplication
		if ( ! class_exists( 'VaultPress' ) ) {
1488
			return new WP_Error(
1489
				'not_active',
1490
				esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ),
1491
				array( 'status' => 404 )
1492
			);
1493
		}
1494
1495
		$vaultpress = new VaultPress();
1496
		if ( ! $vaultpress->is_registered() ) {
1497
			return rest_ensure_response( array(
1498
				'code'    => 'not_registered',
1499
				'message' => esc_html__( 'You need to register for VaultPress.', 'jetpack' )
1500
			) );
1501
		}
1502
1503
		$data = json_decode( base64_decode( $vaultpress->contact_service( 'plugin_data' ) ) );
1504
		if ( false == $data ) {
1505
			return rest_ensure_response( array(
1506
				'code'    => 'not_registered',
1507
				'message' => esc_html__( 'Could not connect to VaultPress.', 'jetpack' )
1508
			) );
1509
		} else if ( is_wp_error( $data ) || ! isset( $data->backups->last_backup ) ) {
1510
			return $data;
1511
		} else if ( empty( $data->backups->last_backup ) ) {
1512
			return rest_ensure_response( array(
1513
				'code'    => 'success',
1514
				'message' => esc_html__( 'VaultPress is active and will back up your site soon.', 'jetpack' ),
1515
				'data'    => $data,
1516
			) );
1517
		} else {
1518
			return rest_ensure_response( array(
1519
				'code'    => 'success',
1520
				'message' => esc_html(
1521
					sprintf(
1522
						__( 'Your site was successfully backed-up %s ago.', 'jetpack' ),
1523
						human_time_diff(
1524
							$data->backups->last_backup,
1525
							current_time( 'timestamp' )
1526
						)
1527
					)
1528
				),
1529
				'data'    => $data,
1530
			) );
1531
		}
1532
	}
1533
1534
	/**
1535
	 * A WordPress REST API permission callback method that accepts a request object and
1536
	 * decides if the current user has enough privileges to act.
1537
	 *
1538
	 * @since 4.3.0
1539
	 *
1540
	 * @return bool does a current user have enough privileges.
1541
	 */
1542
	public function can_request() {
1543
		return current_user_can( 'jetpack_admin_page' );
1544
	}
1545
}
1546
1547
/**
1548
 * Actions performed only when Gravatar Hovercards is activated through the endpoint call.
1549
 *
1550
 * @since 4.3.1
1551
 */
1552
function jetpack_do_after_gravatar_hovercards_activation() {
1553
1554
	// When Gravatar Hovercards is activated, enable them automatically.
1555
	update_option( 'gravatar_disable_hovercards', 'enabled' );
1556
}
1557
add_action( 'jetpack_activate_module_gravatar-hovercards', 'jetpack_do_after_gravatar_hovercards_activation' );
1558
1559
/**
1560
 * Actions performed only when Gravatar Hovercards is activated through the endpoint call.
1561
 *
1562
 * @since 4.3.1
1563
 */
1564
function jetpack_do_after_gravatar_hovercards_deactivation() {
1565
1566
	// When Gravatar Hovercards is deactivated, disable them automatically.
1567
	update_option( 'gravatar_disable_hovercards', 'disabled' );
1568
}
1569
add_action( 'jetpack_deactivate_module_gravatar-hovercards', 'jetpack_do_after_gravatar_hovercards_deactivation' );
1570
1571
/**
1572
 * Actions performed only when Markdown is activated through the endpoint call.
1573
 *
1574
 * @since 4.7.0
1575
 */
1576
function jetpack_do_after_markdown_activation() {
1577
1578
	// When Markdown is activated, enable support for post editing automatically.
1579
	update_option( 'wpcom_publish_posts_with_markdown', true );
1580
}
1581
add_action( 'jetpack_activate_module_markdown', 'jetpack_do_after_markdown_activation' );
1582