Completed
Push — master-stable ( 2cb821...605f06 )
by Jeremy
184:40 queued 169:16
created

_inc/lib/class.core-rest-api-endpoints.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Register WP REST API endpoints for Jetpack.
4
 *
5
 * @author Automattic
6
 */
7
8
/**
9
 * Disable direct access.
10
 */
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
// Load WP_Error for error messages.
16
require_once ABSPATH . '/wp-includes/class-wp-error.php';
17
18
// Register endpoints when WP REST API is initialized.
19
add_action( 'rest_api_init', array( 'Jetpack_Core_Json_Api_Endpoints', 'register_endpoints' ) );
20
21
/**
22
 * Class Jetpack_Core_Json_Api_Endpoints
23
 *
24
 * @since 4.1.0
25
 */
26
class Jetpack_Core_Json_Api_Endpoints {
27
28
	/**
29
	 * @var string Generic error message when user is not allowed to perform an action.
30
	 */
31
	public static $user_permissions_error_msg;
32
33
	/**
34
	 * @var array Roles that can access Stats once they're granted access.
35
	 */
36
	public static $stats_roles;
37
38
	/**
39
	 * Declare the Jetpack REST API endpoints.
40
	 *
41
	 * @since 4.1.0
42
	 */
43
	public static function register_endpoints() {
44
45
		// Load API endpoint base classes
46
		require_once JETPACK__PLUGIN_DIR . '/_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php';
47
48
		// Load API endpoints
49
		require_once JETPACK__PLUGIN_DIR . '/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
50
51
		self::$user_permissions_error_msg = esc_html__(
52
			'You do not have the correct user permissions to perform this action.
53
			Please contact your site admin if you think this is a mistake.',
54
			'jetpack'
55
		);
56
57
		self::$stats_roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
58
59
		// Get current connection status of Jetpack
60
		register_rest_route( 'jetpack/v4', '/connection-status', array(
61
			'methods' => WP_REST_Server::READABLE,
62
			'callback' => __CLASS__ . '::jetpack_connection_status',
63
		) );
64
65
		// Fetches a fresh connect URL
66
		register_rest_route( 'jetpack/v4', '/connect-url', array(
67
			'methods' => WP_REST_Server::READABLE,
68
			'callback' => __CLASS__ . '::build_connect_url',
69
			'permission_callback' => __CLASS__ . '::connect_url_permission_callback',
70
		) );
71
72
		// Get current user connection data
73
		register_rest_route( 'jetpack/v4', '/user-connection-data', array(
74
			'methods' => WP_REST_Server::READABLE,
75
			'callback' => __CLASS__ . '::get_user_connection_data',
76
			'permission_callback' => __CLASS__ . '::get_user_connection_data_permission_callback',
77
		) );
78
79
		// Disconnect site from WordPress.com servers
80
		register_rest_route( 'jetpack/v4', '/disconnect/site', array(
81
			'methods' => WP_REST_Server::EDITABLE,
82
			'callback' => __CLASS__ . '::disconnect_site',
83
			'permission_callback' => __CLASS__ . '::disconnect_site_permission_callback',
84
		) );
85
86
		// Disconnect/unlink user from WordPress.com servers
87
		register_rest_route( 'jetpack/v4', '/unlink', array(
88
			'methods' => WP_REST_Server::EDITABLE,
89
			'callback' => __CLASS__ . '::unlink_user',
90
			'permission_callback' => __CLASS__ . '::link_user_permission_callback',
91
			'args' => array(
92
				'id' => array(
93
					'default' => get_current_user_id(),
94
					'validate_callback' => __CLASS__  . '::validate_posint',
95
				),
96
			),
97
		) );
98
99
		// Get current site data
100
		register_rest_route( 'jetpack/v4', '/site', array(
101
			'methods' => WP_REST_Server::READABLE,
102
			'callback' => __CLASS__ . '::get_site_data',
103
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
104
		) );
105
106
		// Return all modules
107
		register_rest_route( 'jetpack/v4', '/modules', array(
108
			'methods' => WP_REST_Server::READABLE,
109
			'callback' => __CLASS__ . '::get_modules',
110
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
111
		) );
112
113
		// Return a single module
114
		register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)', array(
115
			'methods' => WP_REST_Server::READABLE,
116
			'callback' => __CLASS__ . '::get_module',
117
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
118
		) );
119
120
		// Activate a module
121
		Jetpack::load_xml_rpc_client();
122
		$xmlrpc = new Jetpack_IXR_Client();
123
		$module_activate = new Jetpack_Core_API_Module_Activate_Endpoint( $xmlrpc );
124
		register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)/activate', array(
125
			'methods' => WP_REST_Server::EDITABLE,
126
			'callback' => array( $module_activate, 'process' ),
127
			'permission_callback' => array( $module_activate, 'can_write' ),
128
		) );
129
130
		// Deactivate a module
131
		register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)/deactivate', array(
132
			'methods' => WP_REST_Server::EDITABLE,
133
			'callback' => __CLASS__ . '::deactivate_module',
134
			'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
135
		) );
136
137
		// Update a module
138
		register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)/update', array(
139
			'methods' => WP_REST_Server::EDITABLE,
140
			'callback' => __CLASS__ . '::update_module',
141
			'permission_callback' => __CLASS__ . '::configure_modules_permission_check',
142
			'args' => self::get_module_updating_parameters(),
143
		) );
144
145
		// Activate many modules
146
		register_rest_route( 'jetpack/v4', '/modules/activate', array(
147
			'methods' => WP_REST_Server::EDITABLE,
148
			'callback' => __CLASS__ . '::activate_modules',
149
			'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
150
			'args' => array(
151
				'modules' => array(
152
					'default'           => '',
153
					'type'              => 'array',
154
					'required'          => true,
155
					'validate_callback' => __CLASS__ . '::validate_module_list',
156
				),
157
			),
158
		) );
159
160
		// Reset all Jetpack options
161
		register_rest_route( 'jetpack/v4', '/reset/(?P<options>[a-z\-]+)', array(
162
			'methods' => WP_REST_Server::EDITABLE,
163
			'callback' => __CLASS__ . '::reset_jetpack_options',
164
			'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
165
		) );
166
167
		// Return miscellaneous settings
168
		register_rest_route( 'jetpack/v4', '/settings', array(
169
			'methods' => WP_REST_Server::READABLE,
170
			'callback' => __CLASS__ . '::get_settings',
171
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
172
		) );
173
174
		// Update miscellaneous setting
175
		register_rest_route( 'jetpack/v4', '/setting/update', array(
176
			'methods' => WP_REST_Server::EDITABLE,
177
			'callback' => __CLASS__ . '::update_setting',
178
			'permission_callback' => __CLASS__ . '::update_settings_permission_check',
179
		) );
180
181
		// Jumpstart
182
		register_rest_route( 'jetpack/v4', '/jumpstart/activate', array(
183
			'methods' => WP_REST_Server::EDITABLE,
184
			'callback' => __CLASS__ . '::jumpstart_activate',
185
			'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
186
		) );
187
188
		register_rest_route( 'jetpack/v4', '/jumpstart/deactivate', array(
189
			'methods' => WP_REST_Server::EDITABLE,
190
			'callback' => __CLASS__ . '::jumpstart_deactivate',
191
			'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
192
		) );
193
194
		// Protect: get blocked count
195
		register_rest_route( 'jetpack/v4', '/module/protect/count/get', array(
196
			'methods' => WP_REST_Server::READABLE,
197
			'callback' => __CLASS__ . '::protect_get_blocked_count',
198
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
199
		) );
200
201
		// Stats: get stats from WPCOM
202
		register_rest_route( 'jetpack/v4', '/module/stats/get', array(
203
			'methods'  => WP_REST_Server::EDITABLE,
204
			'callback' => __CLASS__ . '::site_get_stats_data',
205
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
206
			'args' => array(
207
				'range' => array(
208
					'default'           => 'day',
209
					'type'              => 'string',
210
					'required'          => true,
211
					'validate_callback' => __CLASS__ . '::validate_string',
212
				),
213
			),
214
		) );
215
216
		// Akismet: get spam count
217
		register_rest_route( 'jetpack/v4', '/akismet/stats/get', array(
218
			'methods'  => WP_REST_Server::READABLE,
219
			'callback' => __CLASS__ . '::akismet_get_stats_data',
220
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
221
		) );
222
223
		// Monitor: get last downtime
224
		register_rest_route( 'jetpack/v4', '/module/monitor/downtime/last', array(
225
			'methods' => WP_REST_Server::READABLE,
226
			'callback' => __CLASS__ . '::monitor_get_last_downtime',
227
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
228
		) );
229
230
		// Updates: get number of plugin updates available
231
		register_rest_route( 'jetpack/v4', '/updates/plugins', array(
232
			'methods' => WP_REST_Server::READABLE,
233
			'callback' => __CLASS__ . '::get_plugin_update_count',
234
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
235
		) );
236
237
		// Verification: get services that this site is verified with
238
		register_rest_route( 'jetpack/v4', '/module/verification-tools/services', array(
239
			'methods' => WP_REST_Server::READABLE,
240
			'callback' => __CLASS__ . '::get_verified_services',
241
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
242
		) );
243
244
		// VaultPress: get date last backup or status and actions for user to take
245
		register_rest_route( 'jetpack/v4', '/module/vaultpress/data', array(
246
			'methods' => WP_REST_Server::READABLE,
247
			'callback' => __CLASS__ . '::vaultpress_get_site_data',
248
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
249
		) );
250
251
		// Dismiss Jetpack Notices
252
		register_rest_route( 'jetpack/v4', '/notice/(?P<notice>[a-z\-_]+)/dismiss', array(
253
			'methods' => WP_REST_Server::EDITABLE,
254
			'callback' => __CLASS__ . '::dismiss_notice',
255
			'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
256
		) );
257
258
		// Plugins: get list of all plugins.
259
		register_rest_route( 'jetpack/v4', '/plugins', array(
260
			'methods' => WP_REST_Server::READABLE,
261
			'callback' => __CLASS__ . '::get_plugins',
262
			'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
263
		) );
264
265
		// Plugins: check if the plugin is active.
266
		register_rest_route( 'jetpack/v4', '/plugin/(?P<plugin>[a-z\/\.\-_]+)', array(
267
			'methods' => WP_REST_Server::READABLE,
268
			'callback' => __CLASS__ . '::get_plugin',
269
			'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
270
		) );
271
	}
272
273
	/**
274
	 * Handles dismissing of Jetpack Notices
275
	 *
276
	 * @since 4.1.0
277
	 *
278
	 * @return array|wp-error
279
	 */
280
	public static function dismiss_notice( $data ) {
281
		$notice = $data['notice'];
282
		if ( isset( $notice ) && ! empty( $notice ) ) {
283
			switch( $notice ) {
284
				case 'feedback_dash_request':
285
				case 'welcome':
286
					$notices = get_option( 'jetpack_dismissed_notices', array() );
287
					$notices[ $notice ] = true;
288
					update_option( 'jetpack_dismissed_notices', $notices );
289
					return rest_ensure_response( get_option( 'jetpack_dismissed_notices', array() ) );
290
291
				default:
292
					return new WP_Error( 'invalid_param', esc_html__( 'Invalid parameter "notice".', 'jetpack' ), array( 'status' => 404 ) );
293
			}
294
		}
295
296
		return new WP_Error( 'required_param', esc_html__( 'Missing parameter "notice".', 'jetpack' ), array( 'status' => 404 ) );
297
	}
298
299
	/**
300
	 * Verify that the user can disconnect the site.
301
	 *
302
	 * @since 4.1.0
303
	 *
304
	 * @return bool|WP_Error True if user is able to disconnect the site.
305
	 */
306
	public static function disconnect_site_permission_callback() {
307
		if ( current_user_can( 'jetpack_disconnect' ) ) {
308
			return true;
309
		}
310
311
		return new WP_Error( 'invalid_user_permission_jetpack_disconnect', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
312
313
	}
314
315
	/**
316
	 * Verify that the user can get a connect/link URL
317
	 *
318
	 * @since 4.1.0
319
	 *
320
	 * @return bool|WP_Error True if user is able to disconnect the site.
321
	 */
322 View Code Duplication
	public static function connect_url_permission_callback() {
323
		if ( current_user_can( 'jetpack_connect_user' ) ) {
324
			return true;
325
		}
326
327
		return new WP_Error( 'invalid_user_permission_jetpack_disconnect', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
328
329
	}
330
331
	/**
332
	 * Verify that a user can use the link endpoint.
333
	 *
334
	 * @since 4.1.0
335
	 *
336
	 * @return bool|WP_Error True if user is able to link to WordPress.com
337
	 */
338 View Code Duplication
	public static function link_user_permission_callback() {
339
		if ( current_user_can( 'jetpack_connect_user' ) ) {
340
			return true;
341
		}
342
343
		return new WP_Error( 'invalid_user_permission_link_user', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
344
	}
345
346
	/**
347
	 * Verify that a user can get the data about the current user.
348
	 * Only those who can connect.
349
	 *
350
	 * @since 4.1.0
351
	 *
352
	 * @uses Jetpack::is_user_connected();
353
	 *
354
	 * @return bool|WP_Error True if user is able to unlink.
355
	 */
356 View Code Duplication
	public static function get_user_connection_data_permission_callback() {
357
		if ( current_user_can( 'jetpack_connect_user' ) ) {
358
			return true;
359
		}
360
361
		return new WP_Error( 'invalid_user_permission_unlink_user', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
362
	}
363
364
	/**
365
	 * Verify that a user can use the unlink endpoint.
366
	 * Either needs to be an admin of the site, or for them to be currently linked.
367
	 *
368
	 * @since 4.1.0
369
	 *
370
	 * @uses Jetpack::is_user_connected();
371
	 *
372
	 * @return bool|WP_Error True if user is able to unlink.
373
	 */
374
	public static function unlink_user_permission_callback() {
375
		if ( current_user_can( 'jetpack_connect' ) || Jetpack::is_user_connected( get_current_user_id() ) ) {
376
			return true;
377
		}
378
379
		return new WP_Error( 'invalid_user_permission_unlink_user', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
380
	}
381
382
	/**
383
	 * Verify that user can manage Jetpack modules.
384
	 *
385
	 * @since 4.1.0
386
	 *
387
	 * @return bool Whether user has the capability 'jetpack_manage_modules'.
388
	 */
389
	public static function manage_modules_permission_check() {
390
		if ( current_user_can( 'jetpack_manage_modules' ) ) {
391
			return true;
392
		}
393
394
		return new WP_Error( 'invalid_user_permission_manage_modules', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
395
	}
396
397
	/**
398
	 * Verify that user can update Jetpack modules.
399
	 *
400
	 * @since 4.1.0
401
	 *
402
	 * @return bool Whether user has the capability 'jetpack_configure_modules'.
403
	 */
404
	public static function configure_modules_permission_check() {
405
		if ( current_user_can( 'jetpack_configure_modules' ) ) {
406
			return true;
407
		}
408
409
		return new WP_Error( 'invalid_user_permission_configure_modules', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
410
	}
411
412
	/**
413
	 * Verify that user can view Jetpack admin page.
414
	 *
415
	 * @since 4.1.0
416
	 *
417
	 * @return bool Whether user has the capability 'jetpack_admin_page'.
418
	 */
419 View Code Duplication
	public static function view_admin_page_permission_check() {
420
		if ( current_user_can( 'jetpack_admin_page' ) ) {
421
			return true;
422
		}
423
424
		return new WP_Error( 'invalid_user_permission_view_admin', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
425
	}
426
427
	/**
428
	 * Verify that user can update Jetpack options.
429
	 *
430
	 * @since 4.1.0
431
	 *
432
	 * @return bool Whether user has the capability 'jetpack_admin_page'.
433
	 */
434
	public static function update_settings_permission_check() {
435
		if ( current_user_can( 'manage_options' ) ) {
436
			return true;
437
		}
438
439
		return new WP_Error( 'invalid_user_permission_manage_settings', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
440
	}
441
442
	/**
443
	 * Verify that user can view Jetpack admin page and can activate plugins.
444
	 *
445
	 * @since 4.1.0
446
	 *
447
	 * @return bool Whether user has the capability 'jetpack_admin_page' and 'activate_plugins'.
448
	 */
449 View Code Duplication
	public static function activate_plugins_permission_check() {
450
		if ( current_user_can( 'jetpack_admin_page', 'activate_plugins' ) ) {
451
			return true;
452
		}
453
454
		return new WP_Error( 'invalid_user_permission_activate_plugins', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
455
	}
456
457
	/**
458
	 * Contextual HTTP error code for authorization failure.
459
	 *
460
	 * Taken from rest_authorization_required_code() in WP-API plugin until is added to core.
461
	 * @see https://github.com/WP-API/WP-API/commit/7ba0ae6fe4f605d5ffe4ee85b1cd5f9fb46900a6
462
	 *
463
	 * @since 4.1.0
464
	 *
465
	 * @return int
466
	 */
467
	public static function rest_authorization_required_code() {
468
		return is_user_logged_in() ? 403 : 401;
469
	}
470
471
	/**
472
	 * Get connection status for this Jetpack site.
473
	 *
474
	 * @since 4.1.0
475
	 *
476
	 * @return bool True if site is connected
477
	 */
478
	public static function jetpack_connection_status() {
479
		return rest_ensure_response( array(
480
				'isActive'  => Jetpack::is_active(),
481
				'isStaging' => Jetpack::is_staging_site(),
482
				'devMode'   => array(
483
					'isActive' => Jetpack::is_development_mode(),
484
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
485
					'url'      => site_url() && false === strpos( site_url(), '.' ),
486
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
487
				),
488
			)
489
		);
490
	}
491
492
	/**
493
	 * Disconnects Jetpack from the WordPress.com Servers
494
	 *
495
	 * @uses Jetpack::disconnect();
496
	 * @since 4.1.0
497
	 * @return bool|WP_Error True if Jetpack successfully disconnected.
498
	 */
499
	public static function disconnect_site() {
500
		if ( Jetpack::is_active() ) {
501
			Jetpack::disconnect();
502
			return rest_ensure_response( array( 'code' => 'success' ) );
503
		}
504
505
		return new WP_Error( 'disconnect_failed', esc_html__( 'Was not able to disconnect the site.  Please try again.', 'jetpack' ), array( 'status' => 400 ) );
506
	}
507
508
	/**
509
	 * Gets a new connect URL with fresh nonce
510
	 *
511
	 * @uses Jetpack::disconnect();
512
	 * @since 4.1.0
513
	 * @return bool|WP_Error True if Jetpack successfully disconnected.
514
	 */
515
	public static function build_connect_url() {
516
		if ( require_once( ABSPATH . 'wp-admin/includes/plugin.php' ) ) {
517
			$url = Jetpack::init()->build_connect_url( true, false, false );
518
			return rest_ensure_response( $url );
519
		}
520
521
		return new WP_Error( 'build_connect_url_failed', esc_html__( 'Unable to build the connect URL.  Please reload the page and try again.', 'jetpack' ), array( 'status' => 400 ) );
522
	}
523
524
	/**
525
	 * Get miscellaneous settings for this Jetpack installation, like Holiday Snow.
526
	 *
527
	 * @since 4.1.0
528
	 *
529
	 * @return object $response {
530
	 *     Array of miscellaneous settings.
531
	 *
532
	 *     @type bool $holiday-snow Did Jack steal Christmas?
533
	 * }
534
	 */
535
	public static function get_settings() {
536
		$response = array(
537
			jetpack_holiday_snow_option_name() => get_option( jetpack_holiday_snow_option_name() ) == 'letitsnow',
538
		);
539
		return rest_ensure_response( $response );
540
	}
541
542
	/**
543
	 * Get miscellaneous user data related to the connection. Similar data available in old "My Jetpack".
544
	 * Information about the master/primary user.
545
	 * Information about the current user.
546
	 *
547
	 * @since 4.1.0
548
	 *
549
	 * @return object
550
	 */
551
	public static function get_user_connection_data() {
552
		require_once( JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php' );
553
554
		$response = array(
555
			'othersLinked' => jetpack_get_other_linked_users(),
556
			'currentUser'  => jetpack_current_user_data(),
557
		);
558
		return rest_ensure_response( $response );
559
	}
560
561
562
563
	/**
564
	 * Update a single miscellaneous setting for this Jetpack installation, like Holiday Snow.
565
	 *
566
	 * @since 4.1.0
567
	 *
568
	 * @param WP_REST_Request $data
569
	 *
570
	 * @return object Jetpack miscellaneous settings.
571
	 */
572
	public static function update_setting( $data ) {
573
		// Get parameters to update the module.
574
		$param = $data->get_json_params();
575
576
		// Exit if no parameters were passed.
577 View Code Duplication
		if ( ! is_array( $param ) ) {
578
			return new WP_Error( 'missing_setting', esc_html__( 'Missing setting.', 'jetpack' ), array( 'status' => 404 ) );
579
		}
580
581
		// Get option name and value.
582
		$option = key( $param );
583
		$value  = current( $param );
584
585
		// Log success or not
586
		$updated = false;
587
588
		switch ( $option ) {
589
			case jetpack_holiday_snow_option_name():
590
				$updated = update_option( $option, ( true == (bool) $value ) ? 'letitsnow' : '' );
591
				break;
592
		}
593
594
		if ( $updated ) {
595
			return rest_ensure_response( array(
596
				'code' 	  => 'success',
597
				'message' => esc_html__( 'Setting updated.', 'jetpack' ),
598
				'value'   => $value,
599
			) );
600
		}
601
602
		return new WP_Error( 'setting_not_updated', esc_html__( 'The setting was not updated.', 'jetpack' ), array( 'status' => 400 ) );
603
	}
604
605
	/**
606
	 * Unlinks a user from the WordPress.com Servers.
607
	 * Default $data['id'] will default to current_user_id if no value is given.
608
	 *
609
	 * Example: '/unlink?id=1234'
610
	 *
611
	 * @since 4.1.0
612
	 * @uses  Jetpack::unlink_user
613
	 *
614
	 * @param WP_REST_Request $data {
615
	 *     Array of parameters received by request.
616
	 *
617
	 *     @type int $id ID of user to unlink.
618
	 * }
619
	 *
620
	 * @return bool|WP_Error True if user successfully unlinked.
621
	 */
622
	public static function unlink_user( $data ) {
623
		if ( isset( $data['id'] ) && Jetpack::unlink_user( $data['id'] ) ) {
624
			return rest_ensure_response(
625
				array(
626
					'code' => 'success'
627
				)
628
			);
629
		}
630
631
		return new WP_Error( 'unlink_user_failed', esc_html__( 'Was not able to unlink the user.  Please try again.', 'jetpack' ), array( 'status' => 400 ) );
632
	}
633
634
	/**
635
	 * Get site data, including for example, the site's current plan.
636
	 *
637
	 * @since 4.1.0
638
	 *
639
	 * @return array Array of Jetpack modules.
640
	 */
641
	public static function get_site_data() {
642
643
		if ( $site_id = Jetpack_Options::get_option( 'id' ) ) {
644
			$response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d', $site_id ), '1.1' );
645
646
			if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
647
				return new WP_Error( 'site_data_fetch_failed', esc_html__( 'Failed fetching site data. Try again later.', 'jetpack' ), array( 'status' => 400 ) );
648
			}
649
650
			return rest_ensure_response( array(
651
					'code' => 'success',
652
					'message' => esc_html__( 'Site data correctly received.', 'jetpack' ),
653
					'data' => wp_remote_retrieve_body( $response ),
654
				)
655
			);
656
		}
657
658
		return new WP_Error( 'site_id_missing', esc_html__( 'The ID of this site does not exist.', 'jetpack' ), array( 'status' => 404 ) );
659
	}
660
661
	/**
662
	 * Is Akismet registered and active?
663
	 *
664
	 * @since 4.1.0
665
	 *
666
	 * @return bool|WP_Error True if Akismet is active and registered. Otherwise, a WP_Error instance with the corresponding error.
667
	 */
668
	public static function akismet_is_active_and_registered() {
669
		if ( ! file_exists( WP_PLUGIN_DIR . '/akismet/class.akismet.php' ) ) {
670
			return new WP_Error( 'not_installed', esc_html__( 'Please install Akismet.', 'jetpack' ), array( 'status' => 400 ) );
671
		}
672
673
		if ( ! class_exists( 'Akismet' ) ) {
674
			return new WP_Error( 'not_active', esc_html__( 'Please activate Akismet.', 'jetpack' ), array( 'status' => 400 ) );
675
		}
676
677
		// What about if Akismet is put in a sub-directory or maybe in mu-plugins?
678
		require_once WP_PLUGIN_DIR . '/akismet/class.akismet.php';
679
		require_once WP_PLUGIN_DIR . '/akismet/class.akismet-admin.php';
680
		$akismet_key = Akismet::verify_key( Akismet::get_api_key() );
681
682
		if ( ! $akismet_key || 'invalid' === $akismet_key || 'failed' === $akismet_key ) {
683
			return new WP_Error( 'invalid_key', esc_html__( 'Invalid Akismet key. Please contact support.', 'jetpack' ), array( 'status' => 400 ) );
684
		}
685
686
		return true;
687
	}
688
689
	/**
690
	 * Get a list of all Jetpack modules and their information.
691
	 *
692
	 * @since 4.1.0
693
	 *
694
	 * @return array Array of Jetpack modules.
695
	 */
696
	public static function get_modules() {
697
		require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' );
698
699
		$modules = Jetpack_Admin::init()->get_modules();
700
		foreach ( $modules as $slug => $properties ) {
701
			$modules[ $slug ]['options'] = self::prepare_options_for_response( $slug );
702
			if ( isset( $modules[ $slug ]['requires_connection'] ) && $modules[ $slug ]['requires_connection'] && Jetpack::is_development_mode() ) {
703
				$modules[ $slug ]['activated'] = false;
704
			}
705
		}
706
707
		return self::prepare_modules_for_response( $modules );
0 ignored issues
show
$modules is of type array, but the function expects a string.

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...
708
	}
709
710
	/**
711
	 * Get information about a specific and valid Jetpack module.
712
	 *
713
	 * @since 4.1.0
714
	 *
715
	 * @param WP_REST_Request $data {
716
	 *     Array of parameters received by request.
717
	 *
718
	 *     @type string $slug Module slug.
719
	 * }
720
	 *
721
	 * @return mixed|void|WP_Error
722
	 */
723
	public static function get_module( $data ) {
724
		if ( Jetpack::is_module( $data['slug'] ) ) {
725
726
			$module = Jetpack::get_module( $data['slug'] );
727
728
			$module['options'] = self::prepare_options_for_response( $data['slug'] );
729
730
			if ( isset( $module['requires_connection'] ) && $module['requires_connection'] && Jetpack::is_development_mode() ) {
731
				$module['activated'] = false;
732
			}
733
734
			return self::prepare_modules_for_response( $module, $data['slug'] );
0 ignored issues
show
$module is of type array<string,?>, but the function expects a string.

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...
735
		}
736
737
		return new WP_Error( 'not_found', esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), array( 'status' => 404 ) );
738
	}
739
740
	/**
741
	 * If it's a valid Jetpack module, activate it.
742
	 *
743
	 * @since 4.1.0
744
	 *
745
	 * @param WP_REST_Request $data {
746
	 *     Array of parameters received by request.
747
	 *
748
	 *     @type string $slug Module slug.
749
	 * }
750
	 *
751
	 * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error.
752
	 */
753
	public static function activate_module( $data ) {
754
		if ( Jetpack::is_module( $data['slug'] ) ) {
755 View Code Duplication
			if ( Jetpack::activate_module( $data['slug'], false, false ) ) {
756
				return rest_ensure_response( array(
757
					'code' 	  => 'success',
758
					'message' => esc_html__( 'The requested Jetpack module was activated.', 'jetpack' ),
759
				) );
760
			}
761
			return new WP_Error( 'activation_failed', esc_html__( 'The requested Jetpack module could not be activated.', 'jetpack' ), array( 'status' => 424 ) );
762
		}
763
764
		return new WP_Error( 'not_found', esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), array( 'status' => 404 ) );
765
	}
766
767
	/**
768
	 * Activate a list of valid Jetpack modules.
769
	 *
770
	 * @since 4.1.0
771
	 *
772
	 * @param WP_REST_Request $data {
773
	 *     Array of parameters received by request.
774
	 *
775
	 *     @type string $slug Module slug.
776
	 * }
777
	 *
778
	 * @return bool|WP_Error True if modules were activated. Otherwise, a WP_Error instance with the corresponding error.
779
	 */
780
	public static function activate_modules( $data ) {
781
		$params = $data->get_json_params();
782
		if ( isset( $params['modules'] ) && is_array( $params['modules'] ) ) {
783
			$activated = array();
784
			$failed = array();
785
786
			foreach ( $params['modules'] as $module ) {
787
				if ( Jetpack::activate_module( $module, false, false ) ) {
788
					$activated[] = $module;
789
				} else {
790
					$failed[] = $module;
791
				}
792
			}
793
794
			if ( empty( $failed ) ) {
795
				return rest_ensure_response( array(
796
					'code' 	  => 'success',
797
					'message' => esc_html__( 'All modules activated.', 'jetpack' ),
798
				) );
799
			} else {
800
				$error = '';
801
802
				$activated_count = count( $activated );
803 View Code Duplication
				if ( $activated_count > 0 ) {
804
					$activated_last = array_pop( $activated );
805
					$activated_text = $activated_count > 1 ? sprintf(
806
						/* Translators: first variable is a list followed by a last item. Example: dog, cat and bird. */
807
						__( '%s and %s', 'jetpack' ),
808
						join( ', ', $activated ), $activated_last ) : $activated_last;
809
810
					$error = sprintf(
811
						/* Translators: the plural variable is a list followed by a last item. Example: dog, cat and bird. */
812
						_n( 'The module %s was activated.', 'The modules %s were activated.', $activated_count, 'jetpack' ),
813
						$activated_text ) . ' ';
814
				}
815
816
				$failed_count = count( $failed );
817 View Code Duplication
				if ( count( $failed ) > 0 ) {
818
					$failed_last = array_pop( $failed );
819
					$failed_text = $failed_count > 1 ? sprintf(
820
						/* Translators: first variable is a list followed by a last item. Example: dog, cat and bird. */
821
						__( '%s and %s', 'jetpack' ),
822
						join( ', ', $failed ), $failed_last ) : $failed_last;
823
824
					$error = sprintf(
825
						/* Translators: the plural variable is a list followed by a last item. Example: dog, cat and bird. */
826
						_n( 'The module %s failed to be activated.', 'The modules %s failed to be activated.', $failed_count, 'jetpack' ),
827
						$failed_text ) . ' ';
828
				}
829
			}
830
			return new WP_Error( 'activation_failed', esc_html( $error ), array( 'status' => 424 ) );
831
		}
832
833
		return new WP_Error( 'not_found', esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), array( 'status' => 404 ) );
834
	}
835
836
	/**
837
	 * Reset Jetpack options
838
	 *
839
	 * @since 4.1.0
840
	 *
841
	 * @param WP_REST_Request $data {
842
	 *     Array of parameters received by request.
843
	 *
844
	 *     @type string $options Available options to reset are options|modules
845
	 * }
846
	 *
847
	 * @return bool|WP_Error True if options were reset. Otherwise, a WP_Error instance with the corresponding error.
848
	 */
849
	public static function reset_jetpack_options( $data ) {
850
		if ( isset( $data['options'] ) ) {
851
			$data = $data['options'];
852
853
			switch( $data ) {
854
				case ( 'options' ) :
855
					$options_to_reset = Jetpack::get_jetpack_options_for_reset();
856
857
					// Reset the Jetpack options
858
					foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
859
						Jetpack_Options::delete_option( $option_to_reset );
860
					}
861
862
					foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
863
						delete_option( $option_to_reset );
864
					}
865
866
					// Reset to default modules
867
					$default_modules = Jetpack::get_default_modules();
868
					Jetpack::update_active_modules( $default_modules );
869
870
					// Jumpstart option is special
871
					Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
872
					return rest_ensure_response( array(
873
						'code' 	  => 'success',
874
						'message' => esc_html__( 'Jetpack options reset.', 'jetpack' ),
875
					) );
876
					break;
877
878
				case 'modules':
879
					$default_modules = Jetpack::get_default_modules();
880
					Jetpack::update_active_modules( $default_modules );
881
					return rest_ensure_response( array(
882
						'code' 	  => 'success',
883
						'message' => esc_html__( 'Modules reset to default.', 'jetpack' ),
884
					) );
885
					break;
886
887
				default:
888
					return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
889
			}
890
		}
891
892
		return new WP_Error( 'required_param', esc_html__( 'Missing parameter "type".', 'jetpack' ), array( 'status' => 404 ) );
893
	}
894
895
	/**
896
	 * Activates a series of valid Jetpack modules and initializes some options.
897
	 *
898
	 * @since 4.1.0
899
	 *
900
	 * @param WP_REST_Request $data {
901
	 *     Array of parameters received by request.
902
	 * }
903
	 *
904
	 * @return bool|WP_Error True if Jumpstart succeeded. Otherwise, a WP_Error instance with the corresponding error.
905
	 */
906
	public static function jumpstart_activate( $data ) {
907
		$modules = Jetpack::get_available_modules();
908
		$activate_modules = array();
909
		foreach ( $modules as $module ) {
910
			$module_info = Jetpack::get_module( $module );
911
			if ( isset( $module_info['feature'] ) && is_array( $module_info['feature'] ) && in_array( 'Jumpstart', $module_info['feature'] ) ) {
912
				$activate_modules[] = $module;
913
			}
914
		}
915
916
		// Collect success/error messages like modules that are properly activated.
917
		$result = array(
918
			'activated_modules' => array(),
919
			'failed_modules'    => array(),
920
		);
921
922
		// Update the jumpstart option
923
		if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
924
			$result['jumpstart_activated'] = Jetpack_Options::update_option( 'jumpstart', 'jumpstart_activated' );
925
		}
926
927
		// Check for possible conflicting plugins
928
		$module_slugs_filtered = Jetpack::init()->filter_default_modules( $activate_modules );
929
930
		foreach ( $module_slugs_filtered as $module_slug ) {
931
			Jetpack::log( 'activate', $module_slug );
932
			if ( Jetpack::activate_module( $module_slug, false, false ) ) {
933
				$result['activated_modules'][] = $module_slug;
934
			} else {
935
				$result['failed_modules'][] = $module_slug;
936
			}
937
		}
938
939
		// Set the default sharing buttons and set to display on posts if none have been set.
940
		$sharing_services = get_option( 'sharing-services' );
941
		$sharing_options  = get_option( 'sharing-options' );
942
		if ( empty( $sharing_services['visible'] ) ) {
943
			// Default buttons to set
944
			$visible = array(
945
				'twitter',
946
				'facebook',
947
				'google-plus-1',
948
			);
949
			$hidden = array();
950
951
			// Set some sharing settings
952
			$sharing = new Sharing_Service();
953
			$sharing_options['global'] = array(
954
				'button_style'  => 'icon',
955
				'sharing_label' => $sharing->default_sharing_label,
956
				'open_links'    => 'same',
957
				'show'          => array( 'post' ),
958
				'custom'        => isset( $sharing_options['global']['custom'] ) ? $sharing_options['global']['custom'] : array()
959
			);
960
961
			$result['sharing_options']  = update_option( 'sharing-options', $sharing_options );
962
			$result['sharing_services'] = update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
963
		}
964
965
		// If all Jumpstart modules were activated
966
		if ( empty( $result['failed_modules'] ) ) {
967
			return rest_ensure_response( array(
968
				'code' 	  => 'success',
969
				'message' => esc_html__( 'Jumpstart done.', 'jetpack' ),
970
				'data'    => $result,
971
			) );
972
		}
973
974
		return new WP_Error( 'jumpstart_failed', esc_html( sprintf( _n( 'Jumpstart failed activating this module: %s.', 'Jumpstart failed activating these modules: %s.', count( $result['failed_modules'] ), 'jetpack' ), join( ', ', $result['failed_modules'] ) ) ), array( 'status' => 400 ) );
975
	}
976
977
	/**
978
	 * Dismisses Jumpstart so user is not prompted to go through it again.
979
	 *
980
	 * @since 4.1.0
981
	 *
982
	 * @param WP_REST_Request $data {
983
	 *     Array of parameters received by request.
984
	 * }
985
	 *
986
	 * @return bool|WP_Error True if Jumpstart was disabled or was nothing to dismiss. Otherwise, a WP_Error instance with a message.
987
	 */
988
	public static function jumpstart_deactivate( $data ) {
989
990
		// If dismissed, flag the jumpstart option as such.
991
		if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
992
			if ( Jetpack_Options::update_option( 'jumpstart', 'jumpstart_dismissed' ) ) {
993
				return rest_ensure_response( array(
994
					'code' 	  => 'success',
995
					'message' => esc_html__( 'Jumpstart dismissed.', 'jetpack' ),
996
				) );
997
			} else {
998
				return new WP_Error( 'jumpstart_failed_dismiss', esc_html__( 'Jumpstart could not be dismissed.', 'jetpack' ), array( 'status' => 400 ) );
999
			}
1000
		}
1001
1002
		// If this was not a new connection and there was nothing to dismiss, don't fail.
1003
		return rest_ensure_response( array(
1004
			'code' 	  => 'success',
1005
			'message' => esc_html__( 'Nothing to dismiss. This was not a new connection.', 'jetpack' ),
1006
		) );
1007
	}
1008
1009
	/**
1010
	 * If it's a valid Jetpack module, deactivate it.
1011
	 *
1012
	 * @since 4.1.0
1013
	 *
1014
	 * @param WP_REST_Request $data {
1015
	 *     Array of parameters received by request.
1016
	 *
1017
	 *     @type string $slug Module slug.
1018
	 * }
1019
	 *
1020
	 * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error.
1021
	 */
1022
	public static function deactivate_module( $data ) {
1023
		if ( Jetpack::is_module( $data['slug'] ) ) {
1024 View Code Duplication
			if ( ! Jetpack::is_module_active( $data['slug'] ) ) {
1025
				return new WP_Error( 'already_inactive', esc_html__( 'The requested Jetpack module was already inactive.', 'jetpack' ), array( 'status' => 409 ) );
1026
			}
1027 View Code Duplication
			if ( Jetpack::deactivate_module( $data['slug'] ) ) {
1028
				return rest_ensure_response( array(
1029
					'code' 	  => 'success',
1030
					'message' => esc_html__( 'The requested Jetpack module was deactivated.', 'jetpack' ),
1031
				) );
1032
			}
1033
			return new WP_Error( 'deactivation_failed', esc_html__( 'The requested Jetpack module could not be deactivated.', 'jetpack' ), array( 'status' => 400 ) );
1034
		}
1035
1036
		return new WP_Error( 'not_found', esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), array( 'status' => 404 ) );
1037
	}
1038
1039
	/**
1040
	 * If it's a valid Jetpack module and configuration parameters have been sent, update it.
1041
	 *
1042
	 * @since 4.1.0
1043
	 *
1044
	 * @param WP_REST_Request $data {
1045
	 *     Array of parameters received by request.
1046
	 *
1047
	 *     @type string $slug Module slug.
1048
	 * }
1049
	 *
1050
	 * @return bool|WP_Error True if module was updated. Otherwise, a WP_Error instance with the corresponding error.
1051
	 */
1052
	public static function update_module( $data ) {
1053
		if ( ! Jetpack::is_module( $data['slug'] ) ) {
1054
			return new WP_Error( 'not_found', esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), array( 'status' => 404 ) );
1055
		}
1056
1057 View Code Duplication
		if ( ! Jetpack::is_module_active( $data['slug'] ) ) {
1058
			return new WP_Error( 'inactive', esc_html__( 'The requested Jetpack module is inactive.', 'jetpack' ), array( 'status' => 409 ) );
1059
		}
1060
1061
		// Get parameters to update the module.
1062
		$params = $data->get_json_params();
1063
1064
		// Exit if no parameters were passed.
1065 View Code Duplication
		if ( ! is_array( $params ) ) {
1066
			return new WP_Error( 'missing_options', esc_html__( 'Missing options.', 'jetpack' ), array( 'status' => 404 ) );
1067
		}
1068
1069
		// Get available module options.
1070
		$options = self::get_module_available_options( $data['slug'] );
1071
1072
		// Options that are invalid or failed to update.
1073
		$invalid = array();
1074
		$not_updated = array();
1075
1076
		// Used if response is successful. The message can be overwritten and additional data can be added here.
1077
		$response = array(
1078
			'code' 	  => 'success',
1079
			'message' => esc_html__( 'The requested Jetpack module was updated.', 'jetpack' ),
1080
		);
1081
1082
		foreach ( $params as $option => $value ) {
1083
			// If option is invalid, don't go any further.
1084
			if ( ! in_array( $option, array_keys( $options ) ) ) {
1085
				$invalid[] = $option;
1086
				continue;
1087
			}
1088
1089
			// Used if there was an error. Can be overwritten with specific error messages.
1090
			$error = '';
1091
1092
			// Set to true if the option update was successful.
1093
			$updated = false;
1094
1095
			// Properly cast value based on its type defined in endpoint accepted args.
1096
			$value = self::cast_value( $value, $options[ $option ] );
1097
1098
			switch ( $option ) {
1099
				case 'monitor_receive_notifications':
1100
					$monitor = new Jetpack_Monitor();
1101
1102
					// If we got true as response, consider it done.
1103
					$updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value );
1104
					break;
1105
1106
				case 'post_by_email_address':
1107
					if ( 'create' == $value ) {
1108
						$result = self::_process_post_by_email(
1109
							'jetpack.createPostByEmailAddress',
1110
							esc_html__( 'Unable to create the Post by Email address. Please try again later.', 'jetpack' )
1111
						);
1112
					} elseif ( 'regenerate' == $value ) {
1113
						$result = self::_process_post_by_email(
1114
							'jetpack.regeneratePostByEmailAddress',
1115
							esc_html__( 'Unable to regenerate the Post by Email address. Please try again later.', 'jetpack' )
1116
						);
1117
					} elseif ( 'delete' == $value ) {
1118
						$result = self::_process_post_by_email(
1119
							'jetpack.deletePostByEmailAddress',
1120
							esc_html__( 'Unable to delete the Post by Email address. Please try again later.', 'jetpack' )
1121
						);
1122
					} else {
1123
						$result = false;
1124
					}
1125
1126
					// If we got an email address (create or regenerate) or 1 (delete), consider it done.
1127
					if ( preg_match( '/[a-z0-9][email protected]/', $result ) ) {
1128
						$response[ $option ] = $result;
1129
						$updated = true;
1130
					} elseif ( 1 == $result ) {
1131
						$updated = true;
1132
					} elseif ( is_array( $result ) && isset( $result['message'] ) ) {
1133
						$error = $result['message'];
1134
					}
1135
					break;
1136
1137
				case 'jetpack_protect_key':
1138
					$protect = Jetpack_Protect_Module::instance();
1139
					if ( 'create' == $value ) {
1140
						$result = $protect->get_protect_key();
1141
					} else {
1142
						$result = false;
1143
					}
1144
1145
					// If we got one of Protect keys, consider it done.
1146
					if ( preg_match( '/[a-z0-9]{40,}/i', $result ) ) {
1147
						$response[ $option ] = $result;
1148
						$updated = true;
1149
					}
1150
					break;
1151
1152
				case 'jetpack_protect_global_whitelist':
1153
					$updated = jetpack_protect_save_whitelist( explode( PHP_EOL, str_replace( array( ' ', ',' ), array( '', "\n" ), $value ) ) );
1154
					if ( is_wp_error( $updated ) ) {
1155
						$error = $updated->get_error_message();
1156
					}
1157
					break;
1158
1159
				case 'show_headline':
1160
				case 'show_thumbnails':
1161
					$grouped_options = $grouped_options_current = (array) Jetpack_Options::get_option( 'relatedposts' );
1162
					$grouped_options[ $option ] = $value;
1163
1164
					// If option value was the same, consider it done.
1165
					$updated = $grouped_options_current != $grouped_options ? Jetpack_Options::update_option( 'relatedposts', $grouped_options ) : true;
1166
					break;
1167
1168
				case 'google':
1169
				case 'bing':
1170 View Code Duplication
				case 'pinterest':
1171
					$grouped_options = $grouped_options_current = (array) get_option( 'verification_services_codes' );
1172
					$grouped_options[ $option ] = $value;
1173
1174
					// If option value was the same, consider it done.
1175
					$updated = $grouped_options_current != $grouped_options ? update_option( 'verification_services_codes', $grouped_options ) : true;
1176
					break;
1177
1178
				case 'sharing_services':
1179
					$sharer = new Sharing_Service();
1180
1181
					// If option value was the same, consider it done.
1182
					$updated = $value != $sharer->get_blog_services() ? $sharer->set_blog_services( $value['visible'], $value['hidden'] ) : true;
1183
					break;
1184
1185
				case 'button_style':
1186
				case 'sharing_label':
1187
				case 'show':
1188
					$sharer = new Sharing_Service();
1189
					$grouped_options = $sharer->get_global_options();
1190
					$grouped_options[ $option ] = $value;
1191
					$updated = $sharer->set_global_options( $grouped_options );
1192
					break;
1193
1194
				case 'custom':
1195
					$sharer = new Sharing_Service();
1196
					$updated = $sharer->new_service( stripslashes( $value['sharing_name'] ), stripslashes( $value['sharing_url'] ), stripslashes( $value['sharing_icon'] ) );
1197
1198
					// Return new custom service
1199
					$response[ $option ] = $updated;
1200
					break;
1201
1202
				case 'sharing_delete_service':
1203
					$sharer = new Sharing_Service();
1204
					$updated = $sharer->delete_service( $value );
1205
					break;
1206
1207
				case 'jetpack-twitter-cards-site-tag':
1208
					$value = trim( ltrim( strip_tags( $value ), '@' ) );
1209
					$updated = get_option( $option ) !== $value ? update_option( $option, $value ) : true;
1210
					break;
1211
1212
				case 'onpublish':
1213
				case 'onupdate':
1214
				case 'Bias Language':
1215
				case 'Cliches':
1216
				case 'Complex Expression':
1217
				case 'Diacritical Marks':
1218
				case 'Double Negative':
1219
				case 'Hidden Verbs':
1220
				case 'Jargon Language':
1221
				case 'Passive voice':
1222
				case 'Phrases to Avoid':
1223
				case 'Redundant Expression':
1224
				case 'guess_lang':
1225
					if ( in_array( $option, array( 'onpublish', 'onupdate' ) ) ) {
1226
						$atd_option = 'AtD_check_when';
1227
					} elseif ( 'guess_lang' == $option ) {
1228
						$atd_option = 'AtD_guess_lang';
1229
						$option = 'true';
1230
					} else {
1231
						$atd_option = 'AtD_options';
1232
					}
1233
					$user_id = get_current_user_id();
1234
					$grouped_options_current = AtD_get_options( $user_id, $atd_option );
1235
					unset( $grouped_options_current['name'] );
1236
					$grouped_options = $grouped_options_current;
1237
					if ( $value && ! isset( $grouped_options [ $option ] ) ) {
1238
						$grouped_options [ $option ] = $value;
1239
					} elseif ( ! $value && isset( $grouped_options [ $option ] ) ) {
1240
						unset( $grouped_options [ $option ] );
1241
					}
1242
					// If option value was the same, consider it done, otherwise try to update it.
1243
					$options_to_save = implode( ',', array_keys( $grouped_options ) );
1244
					$updated = $grouped_options != $grouped_options_current ? AtD_update_setting( $user_id, $atd_option, $options_to_save ) : true;
1245
					break;
1246
1247
				case 'ignored_phrases':
1248
				case 'unignore_phrase':
1249
					$user_id = get_current_user_id();
1250
					$atd_option = 'AtD_ignored_phrases';
1251
					$grouped_options = $grouped_options_current = explode( ',', AtD_get_setting( $user_id, $atd_option ) );
1252
					if ( 'ignored_phrases' == $option ) {
1253
						$grouped_options = explode( ',', $value );
1254
					} else {
1255
						$index = array_search( $value, $grouped_options );
1256
						if ( false !== $index ) {
1257
							unset( $grouped_options[ $index ] );
1258
							$grouped_options = array_values( $grouped_options );
1259
						}
1260
					}
1261
					$ignored_phrases = implode( ',', array_filter( array_map( 'strip_tags', $grouped_options ) ) );
1262
					$updated = $grouped_options != $grouped_options_current ? AtD_update_setting( $user_id, $atd_option, $ignored_phrases ) : true;
1263
					break;
1264
1265
				case 'admin_bar':
1266
				case 'roles':
1267
				case 'count_roles':
1268
				case 'blog_id':
1269
				case 'do_not_track':
1270
				case 'hide_smile':
1271 View Code Duplication
				case 'version':
1272
					$grouped_options = $grouped_options_current = (array) get_option( 'stats_options' );
1273
					$grouped_options[ $option ] = $value;
1274
1275
					// If option value was the same, consider it done.
1276
					$updated = $grouped_options_current != $grouped_options ? update_option( 'stats_options', $grouped_options ) : true;
1277
					break;
1278
1279
				case 'wp_mobile_featured_images':
1280
				case 'wp_mobile_excerpt':
1281
					$value = ( 'enabled' === $value ) ? '1' : '0';
1282
					// break intentionally omitted
1283
				default:
1284
					// If option value was the same, consider it done.
1285
					$updated = get_option( $option ) != $value ? update_option( $option, $value ) : true;
1286
					break;
1287
			}
1288
1289
			// The option was not updated.
1290
			if ( ! $updated ) {
1291
				$not_updated[ $option ] = $error;
1292
			}
1293
		}
1294
1295
		if ( empty( $invalid ) && empty( $not_updated ) ) {
1296
			// The option was updated.
1297
			return rest_ensure_response( $response );
1298
		} else {
1299
			$invalid_count = count( $invalid );
1300
			$not_updated_count = count( $not_updated );
1301
			$error = '';
1302
			if ( $invalid_count > 0 ) {
1303
				$error = sprintf(
1304
					/* Translators: the plural variable is a comma-separated list. Example: dog, cat, bird. */
1305
					_n( 'Invalid option for this module: %s.', 'Invalid options for this module: %s.', $invalid_count, 'jetpack' ),
1306
					join( ', ', $invalid )
1307
				);
1308
			}
1309
			if ( $not_updated_count > 0 ) {
1310
				$not_updated_messages = array();
1311
				foreach ( $not_updated as $not_updated_option => $not_updated_message ) {
1312
					if ( ! empty( $not_updated_message ) ) {
1313
						$not_updated_messages[] = sprintf(
1314
							/* Translators: the first variable is a module option name. The second is the error message . */
1315
							__( 'Extra info for %1$s: %2$s', 'jetpack' ),
1316
							$not_updated_option, $not_updated_message );
1317
					}
1318
				}
1319
				if ( ! empty( $error ) ) {
1320
					$error .= ' ';
1321
				}
1322
				$error .= sprintf(
1323
					/* Translators: the plural variable is a comma-separated list. Example: dog, cat, bird. */
1324
					_n( 'Option not updated: %s.', 'Options not updated: %s.', $not_updated_count, 'jetpack' ),
1325
					join( ', ', array_keys( $not_updated ) ) );
1326
				if ( ! empty( $not_updated_messages ) ) {
1327
					$error .= ' ' . join( '. ', $not_updated_messages );
1328
				}
1329
1330
			}
1331
			// There was an error because some options were updated but others were invalid or failed to update.
1332
			return new WP_Error( 'some_updated', esc_html( $error ), array( 'status' => 400 ) );
1333
		}
1334
1335
	}
1336
1337
	/**
1338
	 * Calls WPCOM through authenticated request to create, regenerate or delete the Post by Email address.
1339
	 * @todo: When all settings are updated to use endpoints, move this to the Post by Email module and replace __process_ajax_proxy_request.
1340
	 *
1341
	 * @since 4.1.0
1342
	 *
1343
	 * @param string $endpoint Process to call on WPCOM to create, regenerate or delete the Post by Email address.
1344
	 * @param string $error	   Error message to return.
1345
	 *
1346
	 * @return array
1347
	 */
1348
	private static function _process_post_by_email( $endpoint, $error ) {
1349
		if ( ! current_user_can( 'edit_posts' ) ) {
1350
			return array( 'message' => $error );
1351
		}
1352
		Jetpack::load_xml_rpc_client();
1353
		$xml = new Jetpack_IXR_Client( array(
1354
			'user_id' => get_current_user_id(),
1355
		) );
1356
		$xml->query( $endpoint );
1357
1358
		if ( $xml->isError() ) {
1359
			return array( 'message' => $error );
1360
		}
1361
1362
		$response = $xml->getResponse();
1363
		if ( empty( $response ) ) {
1364
			return array( 'message' => $error );
1365
		}
1366
1367
		// Used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
1368
		update_option( 'post_by_email_address', $response );
1369
1370
		return $response;
1371
	}
1372
1373
	/**
1374
	 * Get the query parameters for module updating.
1375
	 *
1376
	 * @since 4.1.0
1377
	 *
1378
	 * @return array
1379
	 */
1380
	public static function get_module_updating_parameters() {
1381
		$parameters = array(
1382
			'context'     => array(
1383
				'default' => 'edit',
1384
			),
1385
		);
1386
1387
		return array_merge( $parameters, self::get_module_available_options() );
1388
	}
1389
1390
	/**
1391
	 * Returns a list of module options that can be updated.
1392
	 *
1393
	 * @since 4.1.0
1394
	 *
1395
	 * @param string $module Module slug. If empty, it's assumed we're updating a module and we'll try to get its slug.
1396
	 * @param bool $cache Whether to cache the options or return always fresh.
1397
	 *
1398
	 * @return array
1399
	 */
1400
	public static function get_module_available_options( $module = '', $cache = true ) {
1401
		if ( $cache ) {
1402
			static $options;
1403
		} else {
1404
			$options = null;
1405
		}
1406
1407
		if ( isset( $options ) ) {
1408
			return $options;
1409
		}
1410
1411
		if ( empty( $module ) ) {
1412
			$module = self::get_module_requested( '/module/(?P<slug>[a-z\-]+)/update' );
1413
			if ( empty( $module ) ) {
1414
				return array();
1415
			}
1416
		}
1417
1418
		switch ( $module ) {
1419
1420
			// Carousel
1421
			case 'carousel':
1422
				$options = array(
1423
					'carousel_background_color' => array(
1424
						'description'        => esc_html__( 'Background color.', 'jetpack' ),
1425
						'type'               => 'string',
1426
						'default'            => 'black',
1427
						'enum'				 => array(
1428
							'black' => esc_html__( 'Black', 'jetpack' ),
1429
							'white' => esc_html__( 'White', 'jetpack' ),
1430
						),
1431
						'validate_callback'  => __CLASS__ . '::validate_list_item',
1432
					),
1433
					'carousel_display_exif' => array(
1434
						'description'        => wp_kses( sprintf( __( 'Show photo metadata (<a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format" target="_blank">Exif</a>) in carousel, when available.', 'jetpack' ) ), array( 'a' => array( 'href' => true, 'target' => true ) )  ),
1435
						'type'               => 'boolean',
1436
						'default'            => 0,
1437
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1438
					),
1439
				);
1440
				break;
1441
1442
			// Comments
1443
			case 'comments':
1444
				$options = array(
1445
					'highlander_comment_form_prompt' => array(
1446
						'description'        => esc_html__( 'Greeting Text', 'jetpack' ),
1447
						'type'               => 'string',
1448
						'default'            => esc_html__( 'Leave a Reply', 'jetpack' ),
1449
						'sanitize_callback'  => 'sanitize_text_field',
1450
					),
1451
					'jetpack_comment_form_color_scheme' => array(
1452
						'description'        => esc_html__( "Color Scheme", 'jetpack' ),
1453
						'type'               => 'string',
1454
						'default'            => 'light',
1455
						'enum'				 => array(
1456
							'light'       => esc_html__( 'Light', 'jetpack' ),
1457
							'dark'        => esc_html__( 'Dark', 'jetpack' ),
1458
							'transparent' => esc_html__( 'Transparent', 'jetpack' ),
1459
						),
1460
						'validate_callback'  => __CLASS__ . '::validate_list_item',
1461
					),
1462
				);
1463
				break;
1464
1465
			// Custom Content Types
1466
			case 'custom-content-types':
1467
				$options = array(
1468
					'jetpack_portfolio' => array(
1469
						'description'        => esc_html__( 'Enable or disable Jetpack portfolio post type.', 'jetpack' ),
1470
						'type'               => 'boolean',
1471
						'default'            => 0,
1472
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1473
					),
1474
					'jetpack_portfolio_posts_per_page' => array(
1475
						'description'        => esc_html__( 'Number of entries to show at most in Portfolio pages.', 'jetpack' ),
1476
						'type'               => 'integer',
1477
						'default'            => 10,
1478
						'validate_callback'  => __CLASS__ . '::validate_posint',
1479
					),
1480
					'jetpack_testimonial' => array(
1481
						'description'        => esc_html__( 'Enable or disable Jetpack testimonial post type.', 'jetpack' ),
1482
						'type'               => 'boolean',
1483
						'default'            => 0,
1484
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1485
					),
1486
					'jetpack_testimonial_posts_per_page' => array(
1487
						'description'        => esc_html__( 'Number of entries to show at most in Testimonial pages.', 'jetpack' ),
1488
						'type'               => 'integer',
1489
						'default'            => 10,
1490
						'validate_callback'  => __CLASS__ . '::validate_posint',
1491
					),
1492
				);
1493
				break;
1494
1495
			// Galleries
1496 View Code Duplication
			case 'tiled-gallery':
1497
				$options = array(
1498
					'tiled_galleries' => array(
1499
						'description'        => esc_html__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ),
1500
						'type'               => 'boolean',
1501
						'default'            => 0,
1502
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1503
					),
1504
				);
1505
				break;
1506
1507
			// Gravatar Hovercards
1508
			case 'gravatar-hovercards':
1509
				$options = array(
1510
					'gravatar_disable_hovercards' => array(
1511
						'description'        => esc_html__( "View people's profiles when you mouse over their Gravatars", 'jetpack' ),
1512
						'type'               => 'string',
1513
						'default'            => 'enabled',
1514
						// Not visible. This is used as the checkbox value.
1515
						'enum'				 => array(
1516
							'enabled' => esc_html__( 'Enabled', 'jetpack' ),
1517
							'disabled' => esc_html__( 'Disabled', 'jetpack' ),
1518
						),
1519
						'validate_callback'  => __CLASS__ . '::validate_list_item',
1520
					),
1521
				);
1522
				break;
1523
1524
			// Infinite Scroll
1525
			case 'infinite-scroll':
1526
				$options = array(
1527
					'infinite_scroll' => array(
1528
						'description'        => esc_html__( 'To infinity and beyond', 'jetpack' ),
1529
						'type'               => 'boolean',
1530
						'default'            => 1,
1531
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1532
					),
1533
					'infinite_scroll_google_analytics' => array(
1534
						'description'        => esc_html__( 'Use Google Analytics with Infinite Scroll', 'jetpack' ),
1535
						'type'               => 'boolean',
1536
						'default'            => 0,
1537
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1538
					),
1539
				);
1540
				break;
1541
1542
			// Likes
1543
			case 'likes':
1544
				$options = array(
1545
					'wpl_default' => array(
1546
						'description'        => esc_html__( 'WordPress.com Likes are', 'jetpack' ),
1547
						'type'               => 'string',
1548
						'default'            => 'on',
1549
						'enum'				 => array(
1550
							'on'  => esc_html__( 'On for all posts', 'jetpack' ),
1551
							'off' => esc_html__( 'Turned on per post', 'jetpack' ),
1552
						),
1553
						'validate_callback'  => __CLASS__ . '::validate_list_item',
1554
					),
1555
					'social_notifications_like' => array(
1556
						'description'        => esc_html__( 'Send email notification when someone likes a posts', 'jetpack' ),
1557
						'type'               => 'boolean',
1558
						'default'            => 1,
1559
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1560
					),
1561
				);
1562
				break;
1563
1564
			// Markdown
1565 View Code Duplication
			case 'markdown':
1566
				$options = array(
1567
					'wpcom_publish_comments_with_markdown' => array(
1568
						'description'        => esc_html__( 'Use Markdown for comments.', 'jetpack' ),
1569
						'type'               => 'boolean',
1570
						'default'            => 0,
1571
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1572
					),
1573
				);
1574
				break;
1575
1576
			// Mobile Theme
1577
			case 'minileven':
1578
				$options = array(
1579
					'wp_mobile_excerpt' => array(
1580
						'description'        => esc_html__( 'Excerpts', 'jetpack' ),
1581
						'type'               => 'string',
1582
						'default'            => 'disabled',
1583
						'enum'				 => array(
1584
							'enabled'  => esc_html__( 'Enable excerpts on front page and on archive pages', 'jetpack' ),
1585
							'disabled' => esc_html__( 'Show full posts on front page and on archive pages', 'jetpack' ),
1586
						),
1587
						'validate_callback'  => __CLASS__ . '::validate_list_item',
1588
					),
1589
					'wp_mobile_featured_images' => array(
1590
						'description'        => esc_html__( 'Featured Images', 'jetpack' ),
1591
						'type'               => 'string',
1592
						'default'            => 'disabled',
1593
						'enum'				 => array(
1594
							'enabled' => esc_html__( 'Display featured images', 'jetpack' ),
1595
							'disabled'  => esc_html__( 'Hide all featured images', 'jetpack' ),
1596
						),
1597
						'validate_callback'  => __CLASS__ . '::validate_list_item',
1598
					),
1599
					'wp_mobile_app_promos' => array(
1600
						'description'        => esc_html__( 'Show a promo for the WordPress mobile apps in the footer of the mobile theme.', 'jetpack' ),
1601
						'type'               => 'boolean',
1602
						'default'            => 0,
1603
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1604
					),
1605
				);
1606
				break;
1607
1608
			// Monitor
1609 View Code Duplication
			case 'monitor':
1610
				$options = array(
1611
					'monitor_receive_notifications' => array(
1612
						'description'        => esc_html__( 'Receive Monitor Email Notifications.', 'jetpack' ),
1613
						'type'               => 'boolean',
1614
						'default'            => 0,
1615
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1616
					),
1617
				);
1618
				break;
1619
1620
			// Post by Email
1621 View Code Duplication
			case 'post-by-email':
1622
				$options = array(
1623
					'post_by_email_address' => array(
1624
						'description'       => esc_html__( 'Email Address', 'jetpack' ),
1625
						'type'              => 'string',
1626
						'default'           => '',
1627
						'enum'              => array(
1628
							'create'     => esc_html__( 'Create Post by Email address', 'jetpack' ),
1629
							'regenerate' => esc_html__( 'Regenerate Post by Email address', 'jetpack' ),
1630
							'delete'     => esc_html__( 'Delete Post by Email address', 'jetpack' ),
1631
						),
1632
						'validate_callback' => __CLASS__ . '::validate_list_item',
1633
					),
1634
				);
1635
				break;
1636
1637
			// Protect
1638 View Code Duplication
			case 'protect':
1639
				$options = array(
1640
					'jetpack_protect_key' => array(
1641
						'description'        => esc_html__( 'Protect API key', 'jetpack' ),
1642
						'type'               => 'string',
1643
						'default'            => '',
1644
						'validate_callback'  => __CLASS__ . '::validate_alphanum',
1645
					),
1646
					'jetpack_protect_global_whitelist' => array(
1647
						'description'        => esc_html__( 'Protect global whitelist', 'jetpack' ),
1648
						'type'               => 'string',
1649
						'default'            => '',
1650
						'validate_callback'  => __CLASS__ . '::validate_string',
1651
						'sanitize_callback'  => 'esc_textarea',
1652
					),
1653
				);
1654
				break;
1655
1656
			// Sharing
1657
			case 'sharedaddy':
1658
				$options = array(
1659
					'sharing_services' => array(
1660
						'description'        => esc_html__( 'Enabled Services and those hidden behind a button', 'jetpack' ),
1661
						'type'               => 'array',
1662
						'default'            => array(
1663
							'visible' => array( 'twitter', 'facebook', 'google-plus-1' ),
1664
							'hidden'  => array(),
1665
						),
1666
						'validate_callback'  => __CLASS__ . '::validate_services',
1667
					),
1668
					'button_style' => array(
1669
						'description'       => esc_html__( 'Button Style', 'jetpack' ),
1670
						'type'              => 'string',
1671
						'default'           => 'icon',
1672
						'enum'              => array(
1673
							'icon-text' => esc_html__( 'Icon + text', 'jetpack' ),
1674
							'icon'      => esc_html__( 'Icon only', 'jetpack' ),
1675
							'text'      => esc_html__( 'Text only', 'jetpack' ),
1676
							'official'  => esc_html__( 'Official buttons', 'jetpack' ),
1677
						),
1678
						'validate_callback' => __CLASS__ . '::validate_list_item',
1679
					),
1680
					'sharing_label' => array(
1681
						'description'        => esc_html__( 'Sharing Label', 'jetpack' ),
1682
						'type'               => 'string',
1683
						'default'            => '',
1684
						'validate_callback'  => __CLASS__ . '::validate_string',
1685
						'sanitize_callback'  => 'esc_html',
1686
					),
1687
					'show' => array(
1688
						'description'        => esc_html__( 'Views where buttons are shown', 'jetpack' ),
1689
						'type'               => 'array',
1690
						'default'            => array( 'post' ),
1691
						'validate_callback'  => __CLASS__ . '::validate_sharing_show',
1692
					),
1693
					'jetpack-twitter-cards-site-tag' => array(
1694
						'description'        => esc_html__( "The Twitter username of the owner of this site's domain.", 'jetpack' ),
1695
						'type'               => 'string',
1696
						'default'            => '',
1697
						'validate_callback'  => __CLASS__ . '::validate_twitter_username',
1698
						'sanitize_callback'  => 'esc_html',
1699
					),
1700
					'sharedaddy_disable_resources' => array(
1701
						'description'        => esc_html__( 'Disable CSS and JS', 'jetpack' ),
1702
						'type'               => 'boolean',
1703
						'default'            => 0,
1704
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1705
					),
1706
					'custom' => array(
1707
						'description'        => esc_html__( 'Custom sharing services added by user.', 'jetpack' ),
1708
						'type'               => 'array',
1709
						'default'            => array(
1710
							'sharing_name' => '',
1711
							'sharing_url'  => '',
1712
							'sharing_icon' => '',
1713
						),
1714
						'validate_callback'  => __CLASS__ . '::validate_custom_service',
1715
					),
1716
					// Not an option, but an action that can be perfomed on the list of custom services passing the service ID.
1717
					'sharing_delete_service' => array(
1718
						'description'        => esc_html__( 'Delete custom sharing service.', 'jetpack' ),
1719
						'type'               => 'string',
1720
						'default'            => '',
1721
						'validate_callback'  => __CLASS__ . '::validate_custom_service_id',
1722
					),
1723
				);
1724
				break;
1725
1726
			// SSO
1727 View Code Duplication
			case 'sso':
1728
				$options = array(
1729
					'jetpack_sso_require_two_step' => array(
1730
						'description'        => esc_html__( 'Require Two-Step Authentication', 'jetpack' ),
1731
						'type'               => 'boolean',
1732
						'default'            => 0,
1733
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1734
					),
1735
					'jetpack_sso_match_by_email' => array(
1736
						'description'        => esc_html__( 'Match by Email', 'jetpack' ),
1737
						'type'               => 'boolean',
1738
						'default'            => 0,
1739
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1740
					),
1741
				);
1742
				break;
1743
1744
			// Site Icon
1745 View Code Duplication
			case 'site-icon':
1746
				$options = array(
1747
					'site_icon_id' => array(
1748
						'description'        => esc_html__( 'Site Icon ID', 'jetpack' ),
1749
						'type'               => 'integer',
1750
						'default'            => 0,
1751
						'validate_callback'  => __CLASS__ . '::validate_posint',
1752
					),
1753
					'site_icon_url' => array(
1754
						'description'        => esc_html__( 'Site Icon URL', 'jetpack' ),
1755
						'type'               => 'string',
1756
						'default'            => '',
1757
						'sanitize_callback'  => 'esc_url',
1758
					),
1759
				);
1760
				break;
1761
1762
			// Subscriptions
1763
			case 'subscriptions':
1764
				$options = array(
1765
					'stb_enabled' => array(
1766
						'description'        => esc_html__( "Show a <em>'follow blog'</em> option in the comment form", 'jetpack' ),
1767
						'type'               => 'boolean',
1768
						'default'            => 1,
1769
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1770
					),
1771
					'stc_enabled' => array(
1772
						'description'        => esc_html__( "Show a <em>'follow comments'</em> option in the comment form", 'jetpack' ),
1773
						'type'               => 'boolean',
1774
						'default'            => 1,
1775
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1776
					),
1777
				);
1778
				break;
1779
1780
			// Related Posts
1781
			case 'related-posts':
1782
				$options = array(
1783
					'show_headline' => array(
1784
						'description'        => esc_html__( 'Show a "Related" header to more clearly separate the related section from posts', 'jetpack' ),
1785
						'type'               => 'boolean',
1786
						'default'            => 1,
1787
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1788
					),
1789
					'show_thumbnails' => array(
1790
						'description'        => esc_html__( 'Use a large and visually striking layout', 'jetpack' ),
1791
						'type'               => 'boolean',
1792
						'default'            => 0,
1793
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1794
					),
1795
				);
1796
				break;
1797
1798
			// Spelling and Grammar - After the Deadline
1799
			case 'after-the-deadline':
1800
				$options = array(
1801
					'onpublish' => array(
1802
						'description'        => esc_html__( 'Proofread when a post or page is first published.', 'jetpack' ),
1803
						'type'               => 'boolean',
1804
						'default'            => 0,
1805
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1806
					),
1807
					'onupdate' => array(
1808
						'description'        => esc_html__( 'Proofread when a post or page is updated.', 'jetpack' ),
1809
						'type'               => 'boolean',
1810
						'default'            => 0,
1811
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1812
					),
1813
					'Bias Language' => array(
1814
						'description'        => esc_html__( 'Bias Language', 'jetpack' ),
1815
						'type'               => 'boolean',
1816
						'default'            => 0,
1817
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1818
					),
1819
					'Cliches' => array(
1820
						'description'        => esc_html__( 'Clichés', 'jetpack' ),
1821
						'type'               => 'boolean',
1822
						'default'            => 0,
1823
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1824
					),
1825
					'Complex Expression' => array(
1826
						'description'        => esc_html__( 'Complex Phrases', 'jetpack' ),
1827
						'type'               => 'boolean',
1828
						'default'            => 0,
1829
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1830
					),
1831
					'Diacritical Marks' => array(
1832
						'description'        => esc_html__( 'Diacritical Marks', 'jetpack' ),
1833
						'type'               => 'boolean',
1834
						'default'            => 0,
1835
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1836
					),
1837
					'Double Negative' => array(
1838
						'description'        => esc_html__( 'Double Negatives', 'jetpack' ),
1839
						'type'               => 'boolean',
1840
						'default'            => 0,
1841
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1842
					),
1843
					'Hidden Verbs' => array(
1844
						'description'        => esc_html__( 'Hidden Verbs', 'jetpack' ),
1845
						'type'               => 'boolean',
1846
						'default'            => 0,
1847
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1848
					),
1849
					'Jargon Language' => array(
1850
						'description'        => esc_html__( 'Jargon', 'jetpack' ),
1851
						'type'               => 'boolean',
1852
						'default'            => 0,
1853
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1854
					),
1855
					'Passive voice' => array(
1856
						'description'        => esc_html__( 'Passive Voice', 'jetpack' ),
1857
						'type'               => 'boolean',
1858
						'default'            => 0,
1859
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1860
					),
1861
					'Phrases to Avoid' => array(
1862
						'description'        => esc_html__( 'Phrases to Avoid', 'jetpack' ),
1863
						'type'               => 'boolean',
1864
						'default'            => 0,
1865
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1866
					),
1867
					'Redundant Expression' => array(
1868
						'description'        => esc_html__( 'Redundant Phrases', 'jetpack' ),
1869
						'type'               => 'boolean',
1870
						'default'            => 0,
1871
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1872
					),
1873
					'guess_lang' => array(
1874
						'description'        => esc_html__( 'Use automatically detected language to proofread posts and pages', 'jetpack' ),
1875
						'type'               => 'boolean',
1876
						'default'            => 0,
1877
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1878
					),
1879
					'ignored_phrases' => array(
1880
						'description'        => esc_html__( 'Add Phrase to be ignored', 'jetpack' ),
1881
						'type'               => 'string',
1882
						'default'            => '',
1883
						'sanitize_callback'  => 'esc_html',
1884
					),
1885
					'unignore_phrase' => array(
1886
						'description'        => esc_html__( 'Remove Phrase from being ignored', 'jetpack' ),
1887
						'type'               => 'string',
1888
						'default'            => '',
1889
						'sanitize_callback'  => 'esc_html',
1890
					),
1891
				);
1892
				break;
1893
1894
			// Verification Tools
1895
			case 'verification-tools':
1896
				$options = array(
1897
					'google' => array(
1898
						'description'        => esc_html__( 'Google Search Console', 'jetpack' ),
1899
						'type'               => 'string',
1900
						'default'            => '',
1901
						'validate_callback'  => __CLASS__ . '::validate_alphanum',
1902
					),
1903
					'bing' => array(
1904
						'description'        => esc_html__( 'Bing Webmaster Center', 'jetpack' ),
1905
						'type'               => 'string',
1906
						'default'            => '',
1907
						'validate_callback'  => __CLASS__ . '::validate_alphanum',
1908
					),
1909
					'pinterest' => array(
1910
						'description'        => esc_html__( 'Pinterest Site Verification', 'jetpack' ),
1911
						'type'               => 'string',
1912
						'default'            => '',
1913
						'validate_callback'  => __CLASS__ . '::validate_alphanum',
1914
					),
1915
				);
1916
				break;
1917
1918
			// Stats
1919
			/*
1920
				Example:
1921
				'admin_bar' => true
1922
				'roles' => array ( 'administrator', 'editor' )
1923
				'count_roles' => array ( 'editor' )
1924
				'blog_id' => false
1925
				'do_not_track' => true
1926
				'hide_smile' => true
1927
				'version' => '9'
1928
			*/
1929
			case 'stats':
1930
				$options = array(
1931
					'admin_bar' => array(
1932
						'description'        => esc_html__( 'Put a chart showing 48 hours of views in the admin bar.', 'jetpack' ),
1933
						'type'               => 'boolean',
1934
						'default'            => 1,
1935
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1936
					),
1937
					'roles' => array(
1938
						'description'       => esc_html__( 'Select the roles that will be able to view stats reports.', 'jetpack' ),
1939
						'type'              => 'array',
1940
						'default'           => array( 'administrator' ),
1941
						'validate_callback' => __CLASS__ . '::validate_stats_roles',
1942
						'sanitize_callback' => __CLASS__ . '::sanitize_stats_allowed_roles',
1943
					),
1944
					'count_roles' => array(
1945
						'description'       => esc_html__( 'Count the page views of registered users who are logged in.', 'jetpack' ),
1946
						'type'              => 'array',
1947
						'default'           => array( 'administrator' ),
1948
						'validate_callback' => __CLASS__ . '::validate_stats_roles',
1949
					),
1950
					'blog_id' => array(
1951
						'description'        => esc_html__( 'Blog ID.', 'jetpack' ),
1952
						'type'               => 'boolean',
1953
						'default'            => 0,
1954
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1955
					),
1956
					'do_not_track' => array(
1957
						'description'        => esc_html__( 'Do not track.', 'jetpack' ),
1958
						'type'               => 'boolean',
1959
						'default'            => 1,
1960
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1961
					),
1962
					'hide_smile' => array(
1963
						'description'        => esc_html__( 'Hide the stats smiley face image.', 'jetpack' ),
1964
						'type'               => 'boolean',
1965
						'default'            => 1,
1966
						'validate_callback'  => __CLASS__ . '::validate_boolean',
1967
					),
1968
					'version' => array(
1969
						'description'        => esc_html__( 'Version.', 'jetpack' ),
1970
						'type'               => 'integer',
1971
						'default'            => 9,
1972
						'validate_callback'  => __CLASS__ . '::validate_posint',
1973
					),
1974
				);
1975
				break;
1976
		}
1977
1978
		return $options;
1979
	}
1980
1981
	/**
1982
	 * Validates that the parameter is either a pure boolean or a numeric string that can be mapped to a boolean.
1983
	 *
1984
	 * @since 4.1.0
1985
	 *
1986
	 * @param string|bool $value Value to check.
1987
	 * @param WP_REST_Request $request
1988
	 * @param string $param
1989
	 *
1990
	 * @return bool
1991
	 */
1992
	public static function validate_boolean( $value, $request, $param ) {
1993
		if ( ! is_bool( $value ) && ! ( ( ctype_digit( $value ) || is_numeric( $value ) ) && in_array( $value, array( 0, 1 ) ) ) ) {
1994
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be true, false, 0 or 1.', 'jetpack' ), $param ) );
1995
		}
1996
		return true;
1997
	}
1998
1999
	/**
2000
	 * Validates that the parameter is a positive integer.
2001
	 *
2002
	 * @since 4.1.0
2003
	 *
2004
	 * @param int $value Value to check.
2005
	 * @param WP_REST_Request $request
2006
	 * @param string $param
2007
	 *
2008
	 * @return bool
2009
	 */
2010
	public static function validate_posint( $value = 0, $request, $param ) {
2011 View Code Duplication
		if ( ! is_numeric( $value ) || $value <= 0 ) {
2012
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be a positive integer.', 'jetpack' ), $param ) );
2013
		}
2014
		return true;
2015
	}
2016
2017
	/**
2018
	 * Validates that the parameter belongs to a list of admitted values.
2019
	 *
2020
	 * @since 4.1.0
2021
	 *
2022
	 * @param string $value Value to check.
2023
	 * @param WP_REST_Request $request
2024
	 * @param string $param
2025
	 *
2026
	 * @return bool
2027
	 */
2028
	public static function validate_list_item( $value = '', $request, $param ) {
2029
		$attributes = $request->get_attributes();
2030
		if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
2031
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s not recognized', 'jetpack' ), $param ) );
2032
		}
2033
		$args = $attributes['args'][ $param ];
2034
		if ( ! empty( $args['enum'] ) ) {
2035
2036
			// If it's an associative array, use the keys to check that the value is among those admitted.
2037
			$enum = ( count( array_filter( array_keys( $args['enum'] ), 'is_string' ) ) > 0 ) ? array_keys( $args['enum'] ) : $args['enum'];
2038 View Code Duplication
			if ( ! in_array( $value, $enum ) ) {
2039
				return new WP_Error( 'invalid_param_value', sprintf( esc_html__( '%s must be one of %s', 'jetpack' ), $param, implode( ', ', $enum ) ) );
2040
			}
2041
		}
2042
		return true;
2043
	}
2044
2045
	/**
2046
	 * Validates that the parameter belongs to a list of admitted values.
2047
	 *
2048
	 * @since 4.1.0
2049
	 *
2050
	 * @param string $value Value to check.
2051
	 * @param WP_REST_Request $request
2052
	 * @param string $param
2053
	 *
2054
	 * @return bool
2055
	 */
2056
	public static function validate_module_list( $value = '', $request, $param ) {
2057
		if ( ! is_array( $value ) ) {
2058
			return new WP_Error( 'invalid_param_value', sprintf( esc_html__( '%s must be an array', 'jetpack' ), $param ) );
2059
		}
2060
2061
		$modules = Jetpack::get_available_modules();
2062
2063 View Code Duplication
		if ( count( array_intersect( $value, $modules ) ) != count( $value ) ) {
2064
			return new WP_Error( 'invalid_param_value', sprintf( esc_html__( '%s must be a list of valid modules', 'jetpack' ), $param ) );
2065
		}
2066
2067
		return true;
2068
	}
2069
2070
	/**
2071
	 * Validates that the parameter is an alphanumeric or empty string (to be able to clear the field).
2072
	 *
2073
	 * @since 4.1.0
2074
	 *
2075
	 * @param string $value Value to check.
2076
	 * @param WP_REST_Request $request
2077
	 * @param string $param
2078
	 *
2079
	 * @return bool
2080
	 */
2081
	public static function validate_alphanum( $value = '', $request, $param ) {
2082 View Code Duplication
		if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/[a-z0-9]+/i', $value ) ) ) {
2083
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an alphanumeric string.', 'jetpack' ), $param ) );
2084
		}
2085
		return true;
2086
	}
2087
2088
	/**
2089
	 * Validates that the parameter is among the roles allowed for Stats.
2090
	 *
2091
	 * @since 4.1.0
2092
	 *
2093
	 * @param string|bool $value Value to check.
2094
	 * @param WP_REST_Request $request
2095
	 * @param string $param
2096
	 *
2097
	 * @return bool
2098
	 */
2099
	public static function validate_stats_roles( $value, $request, $param ) {
2100
		if ( ! empty( $value ) && ! array_intersect( self::$stats_roles, $value ) ) {
2101
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be %s.', 'jetpack' ), $param, join( ', ', self::$stats_roles ) ) );
2102
		}
2103
		return true;
2104
	}
2105
2106
	/**
2107
	 * Validates that the parameter is among the views where the Sharing can be displayed.
2108
	 *
2109
	 * @since 4.1.0
2110
	 *
2111
	 * @param string|bool $value Value to check.
2112
	 * @param WP_REST_Request $request
2113
	 * @param string $param
2114
	 *
2115
	 * @return bool
2116
	 */
2117
	public static function validate_sharing_show( $value, $request, $param ) {
2118
		$views = array( 'index', 'post', 'page', 'attachment', 'jetpack-portfolio' );
2119 View Code Duplication
		if ( ! array_intersect( $views, $value ) ) {
2120
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be %s.', 'jetpack' ), $param, join( ', ', $views ) ) );
2121
		}
2122
		return true;
2123
	}
2124
2125
	/**
2126
	 * Validates that the parameter is among the views where the Sharing can be displayed.
2127
	 *
2128
	 * @since 4.1.0
2129
	 *
2130
	 * @param string|bool $value Value to check.
2131
	 * @param WP_REST_Request $request
2132
	 * @param string $param
2133
	 *
2134
	 * @return bool
2135
	 */
2136
	public static function validate_services( $value, $request, $param ) {
2137 View Code Duplication
		if ( ! is_array( $value ) || ! isset( $value['visible'] ) || ! isset( $value['hidden'] ) ) {
2138
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an array with visible and hidden items.', 'jetpack' ), $param ) );
2139
		}
2140
2141
		// Allow to clear everything.
2142
		if ( empty( $value['visible'] ) && empty( $value['hidden'] ) ) {
2143
			return true;
2144
		}
2145
2146 View Code Duplication
		if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
2147
			return new WP_Error( 'invalid_param', esc_html__( 'Failed loading required dependency Sharing_Service.', 'jetpack' ) );
2148
		}
2149
		$sharer = new Sharing_Service();
2150
		$services = array_keys( $sharer->get_all_services() );
2151
2152
		if (
2153
			( ! empty( $value['visible'] ) && ! array_intersect( $value['visible'], $services ) )
2154
			||
2155
			( ! empty( $value['hidden'] ) && ! array_intersect( $value['hidden'], $services ) ) )
2156
		{
2157
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s visible and hidden items must be a list of %s.', 'jetpack' ), $param, join( ', ', $services ) ) );
2158
		}
2159
		return true;
2160
	}
2161
2162
	/**
2163
	 * Validates that the parameter has enough information to build a custom sharing button.
2164
	 *
2165
	 * @since 4.1.0
2166
	 *
2167
	 * @param string|bool $value Value to check.
2168
	 * @param WP_REST_Request $request
2169
	 * @param string $param
2170
	 *
2171
	 * @return bool
2172
	 */
2173
	public static function validate_custom_service( $value, $request, $param ) {
2174 View Code Duplication
		if ( ! is_array( $value ) || ! isset( $value['sharing_name'] ) || ! isset( $value['sharing_url'] ) || ! isset( $value['sharing_icon'] ) ) {
2175
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an array with sharing name, url and icon.', 'jetpack' ), $param ) );
2176
		}
2177
2178
		// Allow to clear everything.
2179
		if ( empty( $value['sharing_name'] ) && empty( $value['sharing_url'] ) && empty( $value['sharing_icon'] ) ) {
2180
			return true;
2181
		}
2182
2183 View Code Duplication
		if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
2184
			return new WP_Error( 'invalid_param', esc_html__( 'Failed loading required dependency Sharing_Service.', 'jetpack' ) );
2185
		}
2186
2187
		if ( ( ! empty( $value['sharing_name'] ) && ! is_string( $value['sharing_name'] ) )
2188
		|| ( ! empty( $value['sharing_url'] ) && ! is_string( $value['sharing_url'] ) )
2189
		|| ( ! empty( $value['sharing_icon'] ) && ! is_string( $value['sharing_icon'] ) ) ) {
2190
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s needs sharing name, url and icon.', 'jetpack' ), $param ) );
2191
		}
2192
		return true;
2193
	}
2194
2195
	/**
2196
	 * Validates that the parameter is a custom sharing service ID like 'custom-1461976264'.
2197
	 *
2198
	 * @since 4.1.0
2199
	 *
2200
	 * @param string $value Value to check.
2201
	 * @param WP_REST_Request $request
2202
	 * @param string $param
2203
	 *
2204
	 * @return bool
2205
	 */
2206
	public static function validate_custom_service_id( $value = '', $request, $param ) {
2207 View Code Duplication
		if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/custom\-[0-1]+/i', $value ) ) ) {
2208
			return new WP_Error( 'invalid_param', sprintf( esc_html__( "%s must be a string prefixed with 'custom-' and followed by a numeric ID.", 'jetpack' ), $param ) );
2209
		}
2210
2211 View Code Duplication
		if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
2212
			return new WP_Error( 'invalid_param', esc_html__( 'Failed loading required dependency Sharing_Service.', 'jetpack' ) );
2213
		}
2214
		$sharer = new Sharing_Service();
2215
		$services = array_keys( $sharer->get_all_services() );
2216
2217 View Code Duplication
		if ( ! empty( $value ) && ! in_array( $value, $services ) ) {
2218
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s is not a registered custom sharing service.', 'jetpack' ), $param ) );
2219
		}
2220
2221
		return true;
2222
	}
2223
2224
	/**
2225
	 * Validates that the parameter is a Twitter username or empty string (to be able to clear the field).
2226
	 *
2227
	 * @since 4.1.0
2228
	 *
2229
	 * @param string $value Value to check.
2230
	 * @param WP_REST_Request $request
2231
	 * @param string $param
2232
	 *
2233
	 * @return bool
2234
	 */
2235
	public static function validate_twitter_username( $value = '', $request, $param ) {
2236 View Code Duplication
		if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/^@?\w{1,15}$/i', $value ) ) ) {
2237
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be a Twitter username.', 'jetpack' ), $param ) );
2238
		}
2239
		return true;
2240
	}
2241
2242
	/**
2243
	 * Validates that the parameter is a string.
2244
	 *
2245
	 * @since 4.1.0
2246
	 *
2247
	 * @param string $value Value to check.
2248
	 * @param WP_REST_Request $request
2249
	 * @param string $param
2250
	 *
2251
	 * @return bool
2252
	 */
2253
	public static function validate_string( $value = '', $request, $param ) {
2254 View Code Duplication
		if ( ! is_string( $value ) ) {
2255
			return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be a string.', 'jetpack' ), $param ) );
2256
		}
2257
		return true;
2258
	}
2259
2260
	/**
2261
	 * If for some reason the roles allowed to see Stats are empty (for example, user tampering with checkboxes),
2262
	 * return an array with only 'administrator' as the allowed role and save it for 'roles' option.
2263
	 *
2264
	 * @since 4.1.0
2265
	 *
2266
	 * @param string|bool $value Value to check.
2267
	 *
2268
	 * @return bool
2269
	 */
2270
	public static function sanitize_stats_allowed_roles( $value ) {
2271
		if ( empty( $value ) ) {
2272
			return array( 'administrator' );
2273
		}
2274
		return $value;
2275
	}
2276
2277
	/**
2278
	 * Get the currently accessed route and return the module slug in it.
2279
	 *
2280
	 * @since 4.1.0
2281
	 *
2282
	 * @param string $route Regular expression for the endpoint with the module slug to return.
2283
	 *
2284
	 * @return array
2285
	 */
2286
	public static function get_module_requested( $route ) {
2287
2288
		if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
2289
			return '';
2290
		}
2291
2292
		preg_match( "#$route#", $GLOBALS['wp']->query_vars['rest_route'], $module );
2293
2294
		if ( empty( $module['slug'] ) ) {
2295
			return '';
2296
		}
2297
2298
		return $module['slug'];
2299
	}
2300
2301
	/**
2302
	 * Adds extra information for modules.
2303
	 *
2304
	 * @since 4.3.0
2305
	 *
2306
	 * @param string      $modules Can be a single module or a list of modules.
2307
	 * @param null|string $slug    Slug of the module in the first parameter.
2308
	 *
2309
	 * @return array
2310
	 */
2311
	public static function prepare_modules_for_response( $modules = '', $slug = null ) {
2312
		if ( get_option( 'permalink_structure' ) ) {
2313
			$sitemap_url = home_url( '/sitemap.xml' );
2314
			$news_sitemap_url = home_url( '/news-sitemap.xml' );
2315
		} else {
2316
			$sitemap_url = home_url( '/?jetpack-sitemap=true' );
2317
			$news_sitemap_url = home_url( '/?jetpack-news-sitemap=true' );
2318
		}
2319
		/** This filter is documented in modules/sitemaps/sitemaps.php */
2320
		$sitemap_url = apply_filters( 'jetpack_sitemap_location', $sitemap_url );
2321
		/** This filter is documented in modules/sitemaps/sitemaps.php */
2322
		$news_sitemap_url = apply_filters( 'jetpack_news_sitemap_location', $news_sitemap_url );
2323
2324
		if ( is_null( $slug ) && isset( $modules['sitemaps'] ) ) {
2325
			// Is a list of modules
2326
			$modules['sitemaps']['extra']['sitemap_url'] = $sitemap_url;
2327
			$modules['sitemaps']['extra']['news_sitemap_url'] = $news_sitemap_url;
2328
		} elseif ( 'sitemaps' == $slug ) {
2329
			// It's a single module
2330
			$modules['extra']['sitemap_url'] = $sitemap_url;
2331
			$modules['extra']['news_sitemap_url'] = $news_sitemap_url;
2332
		}
2333
		return $modules;
2334
	}
2335
2336
	/**
2337
	 * Remove 'validate_callback' item from options available for module.
2338
	 * Fetch current option value and add to array of module options.
2339
	 * Prepare values of module options that need special handling, like those saved in wpcom.
2340
	 *
2341
	 * @since 4.1.0
2342
	 *
2343
	 * @param string $module Module slug.
2344
	 * @return array
2345
	 */
2346
	public static function prepare_options_for_response( $module = '' ) {
2347
		$options = self::get_module_available_options( $module, false );
2348
2349
		if ( ! is_array( $options ) || empty( $options ) ) {
2350
			return $options;
2351
		}
2352
2353
		foreach ( $options as $key => $value ) {
2354
2355
			if ( isset( $options[ $key ]['validate_callback'] ) ) {
2356
				unset( $options[ $key ]['validate_callback'] );
2357
			}
2358
2359
			$default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
2360
2361
			$current_value = get_option( $key, $default_value );
2362
2363
			$options[ $key ]['current_value'] = self::cast_value( $current_value, $options[ $key ] );
2364
		}
2365
2366
		// Some modules need special treatment.
2367
		switch ( $module ) {
2368
2369
			case 'monitor':
2370
				// Status of user notifications
2371
				$options['monitor_receive_notifications']['current_value'] = self::cast_value( self::get_remote_value( 'monitor', 'monitor_receive_notifications' ), $options['monitor_receive_notifications'] );
2372
				break;
2373
2374
			case 'post-by-email':
2375
				// Email address
2376
				$options['post_by_email_address']['current_value'] = self::cast_value( self::get_remote_value( 'post-by-email', 'post_by_email_address' ), $options['post_by_email_address'] );
2377
				break;
2378
2379
			case 'protect':
2380
				// Protect
2381
				$options['jetpack_protect_key']['current_value'] = get_site_option( 'jetpack_protect_key', false );
2382
				if ( ! function_exists( 'jetpack_protect_format_whitelist' ) ) {
2383
					@include( JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php' );
2384
				}
2385
				$options['jetpack_protect_global_whitelist']['current_value'] = jetpack_protect_format_whitelist();
2386
				break;
2387
2388
			case 'related-posts':
2389
				// It's local, but it must be broken apart since it's saved as an array.
2390
				$options = self::split_options( $options, Jetpack_Options::get_option( 'relatedposts' ) );
2391
				break;
2392
2393
			case 'verification-tools':
2394
				// It's local, but it must be broken apart since it's saved as an array.
2395
				$options = self::split_options( $options, get_option( 'verification_services_codes' ) );
2396
				break;
2397
2398
			case 'sharedaddy':
2399
				// It's local, but it must be broken apart since it's saved as an array.
2400
				if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
2401
					break;
2402
				}
2403
				$sharer = new Sharing_Service();
2404
				$options = self::split_options( $options, $sharer->get_global_options() );
2405
				$options['sharing_services']['current_value'] = $sharer->get_blog_services();
2406
				break;
2407
2408
			case 'site-icon':
2409
				// Return site icon ID and URL to make it more complete.
2410
				$options['site_icon_id']['current_value'] = Jetpack_Options::get_option( 'site_icon_id' );
2411
				if ( ! function_exists( 'jetpack_site_icon_url' ) ) {
2412
					@include( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
2413
				}
2414
				$options['site_icon_url']['current_value'] = jetpack_site_icon_url();
2415
				break;
2416
2417
			case 'after-the-deadline':
2418
				if ( ! function_exists( 'AtD_get_options' ) ) {
2419
					@include( JETPACK__PLUGIN_DIR . 'modules/after-the-deadline.php' );
2420
				}
2421
				$atd_options = array_merge( AtD_get_options( get_current_user_id(), 'AtD_options' ), AtD_get_options( get_current_user_id(), 'AtD_check_when' ) );
2422
				unset( $atd_options['name'] );
2423
				foreach ( $atd_options as $key => $value ) {
2424
					$options[ $key ]['current_value'] = self::cast_value( $value, $options[ $key ] );
2425
				}
2426
				$atd_options = AtD_get_options( get_current_user_id(), 'AtD_guess_lang' );
2427
				$options['guess_lang']['current_value'] = self::cast_value( isset( $atd_options['true'] ), $options[ 'guess_lang' ] );
2428
				$options['ignored_phrases']['current_value'] = AtD_get_setting( get_current_user_id(), 'AtD_ignored_phrases' );
2429
				unset( $options['unignore_phrase'] );
2430
				break;
2431
2432
			case 'minileven':
2433
				$options['wp_mobile_excerpt']['current_value'] =
2434
					1 === intval( $options['wp_mobile_excerpt']['current_value'] ) ?
2435
					'enabled' : 'disabled';
2436
2437
				$options['wp_mobile_featured_images']['current_value'] =
2438
					1 === intval( $options['wp_mobile_featured_images']['current_value'] ) ?
2439
					'enabled' : 'disabled';
2440
				break;
2441
2442
			case 'stats':
2443
				// It's local, but it must be broken apart since it's saved as an array.
2444
				if ( ! function_exists( 'stats_get_options' ) ) {
2445
					@include( JETPACK__PLUGIN_DIR . 'modules/stats.php' );
2446
				}
2447
				$options = self::split_options( $options, stats_get_options() );
2448
				break;
2449
		}
2450
2451
		return $options;
2452
	}
2453
2454
	/**
2455
	 * Splits module options saved as arrays like relatedposts or verification_services_codes into separate options to be returned in the response.
2456
	 *
2457
	 * @since 4.1.0
2458
	 *
2459
	 * @param array  $separate_options Array of options admitted by the module.
2460
	 * @param array  $grouped_options Option saved as array to be splitted.
2461
	 * @param string $prefix Optional prefix for the separate option keys.
2462
	 *
2463
	 * @return array
2464
	 */
2465
	public static function split_options( $separate_options, $grouped_options, $prefix = '' ) {
2466
		if ( is_array( $grouped_options ) ) {
2467
			foreach ( $grouped_options as $key => $value ) {
2468
				$option_key = $prefix . $key;
2469
				if ( isset( $separate_options[ $option_key ] ) ) {
2470
					$separate_options[ $option_key ]['current_value'] = self::cast_value( $grouped_options[ $key ], $separate_options[ $option_key ] );
2471
				}
2472
			}
2473
		}
2474
		return $separate_options;
2475
	}
2476
2477
	/**
2478
	 * Perform a casting to the value specified in the option definition.
2479
	 *
2480
	 * @since 4.1.0
2481
	 *
2482
	 * @param mixed $value Value to cast to the proper type.
2483
	 * @param array $definition Type to cast the value to.
2484
	 *
2485
	 * @return bool|float|int|string
2486
	 */
2487
	public static function cast_value( $value, $definition ) {
2488
		if ( $value === 'NULL' ) {
2489
			return null;
2490
		}
2491
2492
		if ( isset( $definition['type'] ) ) {
2493
			switch ( $definition['type'] ) {
2494
				case 'boolean':
2495
					if ( 'true' === $value ) {
2496
						return true;
2497
					} elseif ( 'false' === $value ) {
2498
						return false;
2499
					}
2500
					return (bool) $value;
2501
					break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
2502
2503
				case 'integer':
2504
					return (int) $value;
2505
					break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
2506
2507
				case 'float':
2508
					return (float) $value;
2509
					break;
2510
			}
2511
		}
2512
		return $value;
2513
	}
2514
2515
	/**
2516
	 * Get a value not saved locally.
2517
	 *
2518
	 * @since 4.1.0
2519
	 *
2520
	 * @param string $module Module slug.
2521
	 * @param string $option Option name.
2522
	 *
2523
	 * @return bool Whether user is receiving notifications or not.
2524
	 */
2525
	public static function get_remote_value( $module, $option ) {
2526
2527
		// If option doesn't exist, 'does_not_exist' will be returned.
2528
		$value = get_option( $option, 'does_not_exist' );
2529
2530
		// If option exists, just return it.
2531
		if ( 'does_not_exist' !== $value ) {
2532
			return $value;
2533
		}
2534
2535
		// Only check a remote option if Jetpack is connected.
2536
		if ( ! Jetpack::is_active() ) {
2537
			return false;
2538
		}
2539
2540
		// If the module is inactive, load the class to use the method.
2541
		if ( ! did_action( 'jetpack_module_loaded_' . $module ) ) {
2542
			// Class can't be found so do nothing.
2543
			if ( ! @include( Jetpack::get_module_path( $module ) ) ) {
2544
				return false;
2545
			}
2546
		}
2547
2548
		// Do what is necessary for each module.
2549
		switch ( $module ) {
2550
			case 'monitor':
2551
				$monitor = new Jetpack_Monitor();
2552
				$value = $monitor->user_receives_notifications( false );
2553
				break;
2554
2555
			case 'post-by-email':
2556
				$post_by_email = new Jetpack_Post_By_Email();
2557
				$value = $post_by_email->get_post_by_email_address();
2558
				if ( $value === null ) {
2559
					$value = 'NULL'; // sentinel value so it actually gets set
2560
				}
2561
				break;
2562
		}
2563
2564
		// Normalize value to boolean.
2565
		if ( is_wp_error( $value ) || is_null( $value ) ) {
2566
			$value = false;
2567
		}
2568
2569
		// Save option to use it next time.
2570
		update_option( $option, $value );
2571
2572
		return $value;
2573
	}
2574
2575
	/**
2576
	 * Get number of blocked intrusion attempts.
2577
	 *
2578
	 * @since 4.1.0
2579
	 *
2580
	 * @return mixed|WP_Error Number of blocked attempts if protection is enabled. Otherwise, a WP_Error instance with the corresponding error.
2581
	 */
2582
	public static function protect_get_blocked_count() {
2583
		if ( Jetpack::is_module_active( 'protect' ) ) {
2584
			return get_site_option( 'jetpack_protect_blocked_attempts' );
2585
		}
2586
2587
		return new WP_Error( 'not_active', esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ), array( 'status' => 404 ) );
2588
	}
2589
2590
	/**
2591
	 * Get number of spam messages blocked by Akismet.
2592
	 *
2593
	 * @since 4.1.0
2594
	 *
2595
	 * @param WP_REST_Request $data {
2596
	 *     Array of parameters received by request.
2597
	 *
2598
	 *     @type string $date Date range to restrict results to.
2599
	 * }
2600
	 *
2601
	 * @return int|string Number of spam blocked by Akismet. Otherwise, an error message.
2602
	 */
2603
	public static function akismet_get_stats_data( WP_REST_Request $data ) {
2604
		if ( ! is_wp_error( $status = self::akismet_is_active_and_registered() ) ) {
2605
			return rest_ensure_response( Akismet_Admin::get_stats( Akismet::get_api_key() ) );
2606
		} else {
2607
			return $status->get_error_code();
2608
		}
2609
	}
2610
2611
	/**
2612
	 * Get stats data for this site
2613
	 *
2614
	 * @since 4.1.0
2615
	 *
2616
	 * @param WP_REST_Request $data {
2617
	 *     Array of parameters received by request.
2618
	 *
2619
	 *     @type string $date Date range to restrict results to.
2620
	 * }
2621
	 *
2622
	 * @return int|string Number of spam blocked by Akismet. Otherwise, an error message.
2623
	 */
2624
	public static function site_get_stats_data( WP_REST_Request $data ) {
2625
		// Get parameters to fetch Stats data.
2626
		$params = $data->get_json_params();
2627
2628
		// If no parameters were passed.
2629
		$range = is_array( $params ) && isset( $params['range'] )
2630
				 && in_array( $params['range'], array( 'day', 'week', 'month' ), true ) ? $params['range'] : 'day';
2631
2632
		if ( ! function_exists( 'stats_get_from_restapi' ) ) {
2633
			require_once( JETPACK__PLUGIN_DIR . 'modules/stats.php' );
2634
		}
2635
2636
		$response = array(
2637
			'general' => stats_get_from_restapi(),
2638
		);
2639
2640
		switch ( $range ) {
2641
			case 'day':
2642
				$response['day'] = stats_get_from_restapi( array(), 'visits?unit=day&quantity=30' );
2643
				break;
2644
			case 'week':
2645
				$response['week'] = stats_get_from_restapi( array(), 'visits?unit=week&quantity=14' );
2646
				break;
2647
			case 'month':
2648
				$response['month'] = stats_get_from_restapi( array(), 'visits?unit=month&quantity=12&' );
2649
				break;
2650
		}
2651
2652
		return rest_ensure_response( $response );
2653
	}
2654
2655
	/**
2656
	 * Get date of last downtime.
2657
	 *
2658
	 * @since 4.1.0
2659
	 *
2660
	 * @return mixed|WP_Error Number of days since last downtime. Otherwise, a WP_Error instance with the corresponding error.
2661
	 */
2662
	public static function monitor_get_last_downtime() {
2663
		if ( Jetpack::is_module_active( 'monitor' ) ) {
2664
			$monitor       = new Jetpack_Monitor();
2665
			$last_downtime = $monitor->monitor_get_last_downtime();
2666
			if ( is_wp_error( $last_downtime ) ) {
2667
				return $last_downtime;
2668
			} else if ( false === strtotime( $last_downtime ) ) {
2669
				return rest_ensure_response( array(
2670
					'code' => 'success',
2671
					'date' => null,
2672
				) );
2673
			} else {
2674
				return rest_ensure_response( array(
2675
					'code' => 'success',
2676
					'date' => human_time_diff( strtotime( $last_downtime ), strtotime( 'now' ) ),
2677
				) );
2678
			}
2679
		}
2680
2681
		return new WP_Error( 'not_active', esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ), array( 'status' => 404 ) );
2682
	}
2683
2684
	/**
2685
	 * Get number of plugin updates available.
2686
	 *
2687
	 * @since 4.1.0
2688
	 *
2689
	 * @return mixed|WP_Error Number of plugin updates available. Otherwise, a WP_Error instance with the corresponding error.
2690
	 */
2691
	public static function get_plugin_update_count() {
2692
		$updates = wp_get_update_data();
2693
		if ( isset( $updates['counts'] ) && isset( $updates['counts']['plugins'] ) ) {
2694
			$count = $updates['counts']['plugins'];
2695
			if ( 0 == $count ) {
2696
				$response = array(
2697
					'code'    => 'success',
2698
					'message' => esc_html__( 'All plugins are up-to-date. Keep up the good work!', 'jetpack' ),
2699
					'count'   => 0,
2700
				);
2701
			} else {
2702
				$response = array(
2703
					'code'    => 'updates-available',
2704
					'message' => esc_html( sprintf( _n( '%s plugin need updating.', '%s plugins need updating.', $count, 'jetpack' ), $count ) ),
2705
					'count'   => $count,
2706
				);
2707
			}
2708
			return rest_ensure_response( $response );
2709
		}
2710
2711
		return new WP_Error( 'not_found', esc_html__( 'Could not check updates for plugins on this site.', 'jetpack' ), array( 'status' => 404 ) );
2712
	}
2713
2714
	/**
2715
	 * Get services that this site is verified with.
2716
	 *
2717
	 * @since 4.1.0
2718
	 *
2719
	 * @return mixed|WP_Error List of services that verified this site. Otherwise, a WP_Error instance with the corresponding error.
2720
	 */
2721
	public static function get_verified_services() {
2722
		if ( Jetpack::is_module_active( 'verification-tools' ) ) {
2723
			$verification_services_codes = get_option( 'verification_services_codes' );
2724
			if ( is_array( $verification_services_codes ) && ! empty( $verification_services_codes ) ) {
2725
				$services = array();
2726
				foreach ( jetpack_verification_services() as $name => $service ) {
2727
					if ( is_array( $service ) && ! empty( $verification_services_codes[ $name ] ) ) {
2728
						switch ( $name ) {
2729
							case 'google':
2730
								$services[] = 'Google';
2731
								break;
2732
							case 'bing':
2733
								$services[] = 'Bing';
2734
								break;
2735
							case 'pinterest':
2736
								$services[] = 'Pinterest';
2737
								break;
2738
						}
2739
					}
2740
				}
2741
				if ( ! empty( $services ) ) {
2742
					if ( 2 > count( $services ) ) {
2743
						$message = esc_html( sprintf( __( 'Your site is verified with %s.', 'jetpack' ), $services[0] ) );
2744
					} else {
2745
						$copy_services = $services;
2746
						$last = count( $copy_services ) - 1;
2747
						$last_service = $copy_services[ $last ];
2748
						unset( $copy_services[ $last ] );
2749
						$message = esc_html( sprintf( __( 'Your site is verified with %s and %s.', 'jetpack' ), join( ', ', $copy_services ), $last_service ) );
2750
					}
2751
					return rest_ensure_response( array(
2752
						'code'     => 'success',
2753
						'message'  => $message,
2754
						'services' => $services,
2755
					) );
2756
				}
2757
			}
2758
			return new WP_Error( 'empty', esc_html__( 'Site not verified with any service.', 'jetpack' ), array( 'status' => 404 ) );
2759
		}
2760
2761
		return new WP_Error( 'not_active', esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ), array( 'status' => 404 ) );
2762
	}
2763
2764
	/**
2765
	 * Get VaultPress site data including, among other things, the date of tge last backup if it was completed.
2766
	 *
2767
	 * @since 4.1.0
2768
	 *
2769
	 * @return mixed|WP_Error VaultPress site data. Otherwise, a WP_Error instance with the corresponding error.
2770
	 */
2771
	public static function vaultpress_get_site_data() {
2772
		if ( class_exists( 'VaultPress' ) ) {
2773
			$vaultpress = new VaultPress();
2774
			if ( ! $vaultpress->is_registered() ) {
2775
				return rest_ensure_response( array(
2776
					'code'    => 'not_registered',
2777
					'message' => esc_html( __( 'You need to register for VaultPress.', 'jetpack' ) )
2778
				) );
2779
			}
2780
			$data = json_decode( base64_decode( $vaultpress->contact_service( 'plugin_data' ) ) );
2781
			if ( is_wp_error( $data ) ) {
2782
				return $data;
2783
			} else if ( ! $data->backups->last_backup ) {
2784
				return rest_ensure_response( array(
2785
					'code'    => 'success',
2786
					'message' => esc_html__( 'VaultPress is active and will back up your site soon.', 'jetpack' ),
2787
					'data'    => $data,
2788
				) );
2789
			} else {
2790
				return rest_ensure_response( array(
2791
					'code'    => 'success',
2792
					'message' => esc_html( sprintf( __( 'Your site was successfully backed-up %s ago.', 'jetpack' ), human_time_diff( $data->backups->last_backup, current_time( 'timestamp' ) ) ) ),
2793
					'data'    => $data,
2794
				) );
2795
			}
2796
		}
2797
2798
		return new WP_Error( 'not_active', esc_html__( 'The requested Jetpack module is not active.', 'jetpack' ), array( 'status' => 404 ) );
2799
	}
2800
2801
	/**
2802
	 * Returns a list of all plugins in the site.
2803
	 *
2804
	 * @since 4.2.0
2805
	 * @uses get_plugins()
2806
	 *
2807
	 * @return array
2808
	 */
2809
	private static function core_get_plugins() {
2810
		if ( ! function_exists( 'get_plugins' ) ) {
2811
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
2812
		}
2813
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
2814
		$plugins = apply_filters( 'all_plugins', get_plugins() );
2815
2816
		if ( is_array( $plugins ) && ! empty( $plugins ) ) {
2817
			foreach ( $plugins as $plugin_slug => $plugin_data ) {
2818
				$plugins[ $plugin_slug ]['active'] = self::core_is_plugin_active( $plugin_slug );
2819
			}
2820
			return $plugins;
2821
		}
2822
2823
		return array();
2824
	}
2825
2826
	/**
2827
	 * Checks if the queried plugin is active.
2828
	 *
2829
	 * @since 4.2.0
2830
	 * @uses is_plugin_active()
2831
	 *
2832
	 * @return bool
2833
	 */
2834
	private static function core_is_plugin_active( $plugin ) {
2835
		if ( ! function_exists( 'get_plugins' ) ) {
2836
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
2837
		}
2838
2839
		return is_plugin_active( $plugin );
2840
	}
2841
2842
	/**
2843
	 * Get plugins data in site.
2844
	 *
2845
	 * @since 4.2.0
2846
	 *
2847
	 * @return WP_REST_Response|WP_Error List of plugins in the site. Otherwise, a WP_Error instance with the corresponding error.
2848
	 */
2849
	public static function get_plugins() {
2850
		$plugins = self::core_get_plugins();
2851
2852
		if ( ! empty( $plugins ) ) {
2853
			return rest_ensure_response( $plugins );
2854
		}
2855
2856
		return new WP_Error( 'not_found', esc_html__( 'Unable to list plugins.', 'jetpack' ), array( 'status' => 404 ) );
2857
	}
2858
2859
	/**
2860
	 * Get data about the queried plugin. Currently it only returns whether the plugin is active or not.
2861
	 *
2862
	 * @since 4.2.0
2863
	 *
2864
	 * @param WP_REST_Request $data {
2865
	 *     Array of parameters received by request.
2866
	 *
2867
	 *     @type string $slug Plugin slug with the syntax 'plugin-directory/plugin-main-file.php'.
2868
	 * }
2869
	 *
2870
	 * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error.
2871
	 */
2872
	public static function get_plugin( $data ) {
2873
2874
		$plugins = self::core_get_plugins();
2875
2876
		if ( empty( $plugins ) ) {
2877
			return new WP_Error( 'no_plugins_found', esc_html__( 'This site has no plugins.', 'jetpack' ), array( 'status' => 404 ) );
2878
		}
2879
2880
		$plugin = stripslashes( $data['plugin'] );
2881
2882
		if ( ! in_array( $plugin, array_keys( $plugins ) ) ) {
2883
			return new WP_Error( 'plugin_not_found', esc_html( sprintf( __( 'Plugin %s is not installed.', 'jetpack' ), $plugin ) ), array( 'status' => 404 ) );
2884
		}
2885
2886
		$plugin_data = $plugins[ $plugin ];
2887
2888
		$plugin_data['active'] = self::core_is_plugin_active( $plugin );
2889
2890
		return rest_ensure_response( array(
2891
			'code'    => 'success',
2892
			'message' => esc_html__( 'Plugin found.', 'jetpack' ),
2893
			'data'    => $plugin_data
2894
		) );
2895
	}
2896
2897
} // class end
2898