Completed
Push — add/redirect-everything ( ffbe71...18e8a6 )
by
unknown
108:48 queued 100:57
created

Jetpack_Plugin_Search::sanitize_search_term()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Constants;
4
use Automattic\Jetpack\Tracking;
5
6
/**
7
 * Disable direct access and execution.
8
 */
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
14
if (
15
	is_admin() &&
16
	Jetpack::is_active() &&
17
	/** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
18
	apply_filters( 'jetpack_show_promotions', true ) &&
19
	// Disable feature hints when plugins cannot be installed.
20
	! Constants::is_true( 'DISALLOW_FILE_MODS' ) &&
21
	jetpack_is_psh_active()
22
) {
23
	Jetpack_Plugin_Search::init();
24
}
25
26
// Register endpoints when WP REST API is initialized.
27
add_action( 'rest_api_init', array( 'Jetpack_Plugin_Search', 'register_endpoints' ) );
28
29
/**
30
 * Class that includes cards in the plugin search results when users enter terms that match some Jetpack feature.
31
 * Card can be dismissed and includes a title, description, button to enable the feature and a link for more information.
32
 *
33
 * @since 7.1.0
34
 */
35
class Jetpack_Plugin_Search {
36
37
	static $slug = 'jetpack-plugin-search';
38
39
	public static function init() {
40
		static $instance = null;
41
42
		if ( ! $instance ) {
43
			$instance = new Jetpack_Plugin_Search();
44
		}
45
46
		return $instance;
47
	}
48
49
	public function __construct() {
50
		add_action( 'current_screen', array( $this, 'start' ) );
51
	}
52
53
	/**
54
	 * Add actions and filters only if this is the plugin installation screen and it's the first page.
55
	 *
56
	 * @param object $screen
57
	 *
58
	 * @since 7.1.0
59
	 */
60
	public function start( $screen ) {
61
		if ( 'plugin-install' === $screen->base && ( ! isset( $_GET['paged'] ) || 1 == $_GET['paged'] ) ) {
62
			add_action( 'admin_enqueue_scripts', array( $this, 'load_plugins_search_script' ) );
63
			add_filter( 'plugins_api_result', array( $this, 'inject_jetpack_module_suggestion' ), 10, 3 );
64
			add_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
65
			add_filter( 'plugin_install_action_links', array( $this, 'insert_module_related_links' ), 10, 2 );
66
		}
67
	}
68
69
	/**
70
	 * Modify URL used to fetch to plugin information so it pulls Jetpack plugin page.
71
	 *
72
	 * @param string $url URL to load in dialog pulling the plugin page from wporg.
73
	 *
74
	 * @since 7.1.0
75
	 *
76
	 * @return string The URL with 'jetpack' instead of 'jetpack-plugin-search'.
77
	 */
78
	public function plugin_details( $url ) {
79
		return false !== stripos( $url, 'tab=plugin-information&amp;plugin=' . self::$slug )
80
			? 'plugin-install.php?tab=plugin-information&amp;plugin=jetpack&amp;TB_iframe=true&amp;width=600&amp;height=550'
81
			: $url;
82
	}
83
84
	/**
85
	 * Register REST API endpoints.
86
	 *
87
	 * @since 7.1.0
88
	 */
89
	public static function register_endpoints() {
90
		register_rest_route( 'jetpack/v4', '/hints', array(
91
			'methods' => WP_REST_Server::EDITABLE,
92
			'callback' => __CLASS__ . '::dismiss',
93
			'permission_callback' => __CLASS__ . '::can_request',
94
			'args' => array(
95
				'hint' => array(
96
					'default'           => '',
97
					'type'              => 'string',
98
					'required'          => true,
99
					'validate_callback' => __CLASS__ . '::is_hint_id',
100
				),
101
			)
102
		) );
103
	}
104
105
	/**
106
	 * A WordPress REST API permission callback method that accepts a request object and
107
	 * decides if the current user has enough privileges to act.
108
	 *
109
	 * @since 7.1.0
110
	 *
111
	 * @return bool does a current user have enough privileges.
112
	 */
113
	public static function can_request() {
114
		return current_user_can( 'jetpack_admin_page' );
115
	}
116
117
	/**
118
	 * Validates that the ID of the hint to dismiss is a string.
119
	 *
120
	 * @since 7.1.0
121
	 *
122
	 * @param string|bool $value Value to check.
123
	 * @param WP_REST_Request $request The request sent to the WP REST API.
124
	 * @param string $param Name of the parameter passed to endpoint holding $value.
125
	 *
126
	 * @return bool|WP_Error
127
	 */
128
	public static function is_hint_id( $value, $request, $param ) {
129
		return in_array( $value, Jetpack::get_available_modules(), true )
130
			? true
131
			: new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an alphanumeric string.', 'jetpack' ), $param ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_param'.

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

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

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

Loading history...
132
	}
133
134
	/**
135
	 * A WordPress REST API callback method that accepts a request object and decides what to do with it.
136
	 *
137
	 * @param WP_REST_Request $request {
138
	 *     Array of parameters received by request.
139
	 *
140
	 *     @type string $hint Slug of card to dismiss.
141
	 * }
142
	 *
143
	 * @since 7.1.0
144
	 *
145
	 * @return bool|array|WP_Error a resulting value or object, or an error.
146
	 */
147
	public static function dismiss( WP_REST_Request $request ) {
148
		return self::add_to_dismissed_hints( $request['hint'] )
149
			? rest_ensure_response( array( 'code' => 'success' ) )
150
			: new WP_Error( 'not_dismissed', esc_html__( 'The card could not be dismissed', 'jetpack' ), array( 'status' => 400 ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'not_dismissed'.

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

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

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

Loading history...
151
	}
152
153
	/**
154
	 * Returns a list of previously dismissed hints.
155
	 *
156
	 * @since 7.1.0
157
	 *
158
	 * @return array List of dismissed hints.
159
	 */
160
	protected static function get_dismissed_hints() {
161
		$dismissed_hints = Jetpack_Options::get_option( 'dismissed_hints' );
162
		return isset( $dismissed_hints ) && is_array( $dismissed_hints )
163
			? $dismissed_hints
164
			: array();
165
	}
166
167
	/**
168
	 * Save the hint in the list of dismissed hints.
169
	 *
170
	 * @since 7.1.0
171
	 *
172
	 * @param string $hint The hint id, which is a Jetpack module slug.
173
	 *
174
	 * @return bool Whether the card was added to the list and hence dismissed.
175
	 */
176
	protected static function add_to_dismissed_hints( $hint ) {
177
		return Jetpack_Options::update_option( 'dismissed_hints', array_merge( self::get_dismissed_hints(), array( $hint ) ) );
178
	}
179
180
	/**
181
	 * Checks that the module slug passed should be displayed.
182
	 *
183
	 * A feature hint will be displayed if it has not been dismissed before or if 2 or fewer other hints have been dismissed.
184
	 *
185
	 * @since 7.2.1
186
	 *
187
	 * @param string $hint The hint id, which is a Jetpack module slug.
188
	 *
189
	 * @return bool True if $hint should be displayed.
190
	 */
191
	protected function should_display_hint( $hint ) {
192
		$dismissed_hints = $this->get_dismissed_hints();
193
		// If more than 2 hints have been dismissed, then show no more.
194
		if ( 2 < count( $dismissed_hints ) ) {
195
			return false;
196
		}
197
198
		$plan = Jetpack_Plan::get();
199
		if ( isset( $plan['class'] ) && ( 'free' === $plan['class'] || 'personal' === $plan['class'] ) && 'vaultpress' === $hint ) {
200
			return false;
201
		}
202
203
		return ! in_array( $hint, $dismissed_hints, true );
204
	}
205
206
	public function load_plugins_search_script() {
207
		wp_enqueue_script( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION, true );
208
		wp_localize_script(
209
			self::$slug,
210
			'jetpackPluginSearch',
211
			array(
212
				'nonce'          => wp_create_nonce( 'wp_rest' ),
213
				'base_rest_url'  => rest_url( '/jetpack/v4' ),
214
				'poweredBy'      => esc_html__( 'by Jetpack (installed)', 'jetpack' ),
215
				'manageSettings' => esc_html__( 'Configure', 'jetpack' ),
216
				'activateModule' => esc_html__( 'Activate Module', 'jetpack' ),
217
				'getStarted'     => esc_html__( 'Get started', 'jetpack' ),
218
				'activated'      => esc_html__( 'Activated', 'jetpack' ),
219
				'activating'     => esc_html__( 'Activating', 'jetpack' ),
220
				'logo'           => 'https://ps.w.org/jetpack/assets/icon.svg?rev=1791404',
221
				'legend'         => esc_html__(
222
					'This suggestion was made by Jetpack, the security and performance plugin already installed on your site.',
223
					'jetpack'
224
				),
225
				'supportText'    => esc_html__(
226
					'Learn more about these suggestions.',
227
					'jetpack'
228
				),
229
				'supportLink'    => 'https://jetpack.com/redirect/?source=plugin-hint-learn-support',
230
				'hideText'       => esc_html__( 'Hide this suggestion', 'jetpack' ),
231
			)
232
		);
233
234
		wp_enqueue_style( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.css', JETPACK__PLUGIN_FILE ) );
235
	}
236
237
	/**
238
	 * Get the plugin repo's data for Jetpack to populate the fields with.
239
	 *
240
	 * @return array|mixed|object|WP_Error
241
	 */
242
	public static function get_jetpack_plugin_data() {
243
		$data = get_transient( 'jetpack_plugin_data' );
244
245
		if ( false === $data || is_wp_error( $data ) ) {
246
			include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
247
			$data = plugins_api( 'plugin_information', array(
248
				'slug' => 'jetpack',
249
				'is_ssl' => is_ssl(),
250
				'fields' => array(
251
					'banners' => true,
252
					'reviews' => true,
253
					'active_installs' => true,
254
					'versions' => false,
255
					'sections' => false,
256
				),
257
			) );
258
			set_transient( 'jetpack_plugin_data', $data, DAY_IN_SECONDS );
259
		}
260
261
		return $data;
262
	}
263
264
	/**
265
	 * Create a list with additional features for those we don't have a module, like Akismet.
266
	 *
267
	 * @since 7.1.0
268
	 *
269
	 * @return array List of features.
270
	 */
271
	public function get_extra_features() {
272
		return array(
273
			'akismet' => array(
274
				'name' => 'Akismet',
275
				'search_terms' => 'akismet, anti-spam, antispam, comments, spam, spam protection, form spam, captcha, no captcha, nocaptcha, recaptcha, phising, google',
276
				'short_description' => esc_html__( 'Keep your visitors and search engines happy by stopping comment and contact form spam with Akismet.', 'jetpack' ),
277
				'requires_connection' => true,
278
				'module' => 'akismet',
279
				'sort' => '16',
280
				'learn_more_button' => 'https://jetpack.com/features/security/spam-filtering/',
281
				'configure_url' => admin_url( 'admin.php?page=akismet-key-config' ),
282
			),
283
		);
284
	}
285
286
	/**
287
	 * Intercept the plugins API response and add in an appropriate card for Jetpack
288
	 */
289
	public function inject_jetpack_module_suggestion( $result, $action, $args ) {
290
		// Looks like a search query; it's matching time
291
		if ( ! empty( $args->search ) ) {
292
			require_once JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php';
293
			$tracking = new Tracking();
294
			$jetpack_modules_list = array_intersect_key(
295
				array_merge( $this->get_extra_features(), Jetpack_Admin::init()->get_modules() ),
296
				array_flip( array(
297
					'contact-form',
298
					'lazy-images',
299
					'monitor',
300
					'photon',
301
					'photon-cdn',
302
					'protect',
303
					'publicize',
304
					'related-posts',
305
					'sharedaddy',
306
					'akismet',
307
					'vaultpress',
308
					'videopress',
309
					'search',
310
				) )
311
			);
312
			uasort( $jetpack_modules_list, array( $this, 'by_sorting_option' ) );
313
314
			// Record event when user searches for a term over 3 chars (less than 3 is not very useful.)
315
			if ( strlen( $args->search ) >= 3 ) {
316
				$tracking->record_user_event( 'wpa_plugin_search_term', array( 'search_term' => $args->search ) );
317
			}
318
319
			// Lowercase, trim, remove punctuation/special chars, decode url, remove 'jetpack'
320
			$normalized_term = $this->sanitize_search_term( $args->search );
321
322
			$matching_module = null;
323
324
			// Try to match a passed search term with module's search terms
325
			foreach ( $jetpack_modules_list as $module_slug => $module_opts ) {
326
				/*
327
				* Does the site's current plan support the feature?
328
				* We don't use Jetpack_Plan::supports() here because
329
				* that check always returns Akismet as supported,
330
				* since Akismet has a free version.
331
				*/
332
				$current_plan         = Jetpack_Plan::get();
333
				$is_supported_by_plan = in_array( $module_slug, $current_plan['supports'], true );
334
335
				if (
336
					false !== stripos( $module_opts['search_terms'] . ', ' . $module_opts['name'], $normalized_term )
337
					&& $is_supported_by_plan
338
				) {
339
					$matching_module = $module_slug;
340
					break;
341
				}
342
			}
343
344
			if ( isset( $matching_module ) && $this->should_display_hint( $matching_module ) ) {
345
				// Record event when a matching feature is found
346
				$tracking->record_user_event( 'wpa_plugin_search_match_found', array( 'feature' => $matching_module ) );
347
348
				$inject = (array) self::get_jetpack_plugin_data();
349
				$image_url = plugins_url( 'modules/plugin-search/psh', JETPACK__PLUGIN_FILE );
350
				$overrides = array(
351
					'plugin-search' => true, // Helps to determine if that an injected card.
352
					'name' => sprintf(       // Supplement name/description so that they clearly indicate this was added.
353
						esc_html_x( 'Jetpack: %s', 'Jetpack: Module Name', 'jetpack' ),
354
						$jetpack_modules_list[ $matching_module ]['name']
355
					),
356
					'short_description' => $jetpack_modules_list[ $matching_module ]['short_description'],
357
					'requires_connection' => (bool) $jetpack_modules_list[ $matching_module ]['requires_connection'],
358
					'slug'    => self::$slug,
359
					'version' => JETPACK__VERSION,
360
					'icons' => array(
361
						'1x'  => "$image_url-128.png",
362
						'2x'  => "$image_url-256.png",
363
						'svg' => "$image_url.svg",
364
					),
365
				);
366
367
				// Splice in the base module data
368
				$inject = array_merge( $inject, $jetpack_modules_list[ $matching_module ], $overrides );
369
370
				// Add it to the top of the list
371
				$result->plugins = array_filter( $result->plugins, array( $this, 'filter_cards' ) );
372
				array_unshift( $result->plugins, $inject );
373
			}
374
		}
375
		return $result;
376
	}
377
378
	/**
379
	 * Remove cards for Jetpack plugins since we don't want duplicates.
380
	 *
381
	 * @since 7.1.0
382
	 * @since 7.2.0 Only remove Jetpack.
383
	 * @since 7.4.0 Simplify for WordPress 5.1+.
384
	 *
385
	 * @param array|object $plugin
386
	 *
387
	 * @return bool
388
	 */
389
	function filter_cards( $plugin ) {
390
		return ! in_array( $plugin['slug'], array( 'jetpack' ), true );
391
	}
392
393
	/**
394
	 * Take a raw search query and return something a bit more standardized and
395
	 * easy to work with.
396
	 *
397
	 * @param  String $term The raw search term
398
	 * @return String A simplified/sanitized version.
399
	 */
400
	private function sanitize_search_term( $term ) {
401
		$term = strtolower( urldecode( $term ) );
402
403
		// remove non-alpha/space chars.
404
		$term = preg_replace( '/[^a-z ]/', '', $term );
405
406
		// remove strings that don't help matches.
407
		$term = trim( str_replace( array( 'jetpack', 'jp', 'free', 'wordpress' ), '', $term ) );
408
409
		return $term;
410
	}
411
412
	/**
413
	 * Callback function to sort the array of modules by the sort option.
414
	 */
415
	private function by_sorting_option( $m1, $m2 ) {
416
		return $m1['sort'] - $m2['sort'];
417
	}
418
419
	/**
420
	 * Modify the URL to the feature settings, for example Publicize.
421
	 * Sharing is included here because while we still have a page in WP Admin,
422
	 * we prefer to send users to Calypso.
423
	 *
424
	 * @param string $feature
425
	 * @param string $configure_url
426
	 *
427
	 * @return string
428
	 * @since 7.1.0
429
	 *
430
	 */
431
	private function get_configure_url( $feature, $configure_url ) {
432
		$site_fragment = Jetpack::build_raw_urls( get_home_url() );
433
		switch ( $feature ) {
434
			case 'sharing':
435
			case 'publicize':
436
				$configure_url = \Jetpack::build_redirect_url(
437
					'wpcom-marketing-connections',
438
					array(
439
						'site' => rawurlencode( $site_fragment ),
440
					)
441
				);
442
				break;
443
			case 'seo-tools':
444
				$configure_url = \Jetpack::build_redirect_url(
445
					'wpcom-marketing-traffic',
446
					array(
447
						'site'   => rawurlencode( $site_fragment ),
448
						'anchor' => 'seo',
449
					)
450
				);
451
				break;
452
			case 'google-analytics':
453
				$configure_url = \Jetpack::build_redirect_url(
454
					'wpcom-marketing-traffic',
455
					array(
456
						'site'   => rawurlencode( $site_fragment ),
457
						'anchor' => 'analytics',
458
					)
459
				);
460
				break;
461
			case 'wordads':
462
				$configure_url = \Jetpack::build_redirect_url(
463
					'wpcom-ads-settings',
464
					array(
465
						'site' => rawurlencode( $site_fragment ),
466
					)
467
				);
468
				break;
469
		}
470
		return $configure_url;
471
	}
472
473
	/**
474
	 * Put some more appropriate links on our custom result cards.
475
	 */
476
	public function insert_module_related_links( $links, $plugin ) {
477
		if ( self::$slug !== $plugin['slug'] ) {
478
			return $links;
479
		}
480
481
		// By the time this filter is applied, self_admin_url was already applied and we don't need it anymore.
482
		remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
483
484
		$links = array();
485
486
		if ( 'akismet' === $plugin['module'] || 'vaultpress' === $plugin['module'] ) {
487
			$links['jp_get_started'] = '<a
488
				id="plugin-select-settings"
489
				class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
490
				href="https://jetpack.com/redirect/?source=plugin-hint-learn-' . $plugin['module'] . '"
491
				data-module="' . esc_attr( $plugin['module'] ) . '"
492
				data-track="get_started"
493
				>' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
494
			// Jetpack installed, active, feature not enabled; prompt to enable.
495
		} elseif (
496
			current_user_can( 'jetpack_activate_modules' ) &&
497
			! Jetpack::is_module_active( $plugin['module'] ) &&
498
			Jetpack_Plan::supports( $plugin['module'] )
499
		) {
500
			$links[] = '<button
501
					id="plugin-select-activate"
502
					class="jetpack-plugin-search__primary button"
503
					data-module="' . esc_attr( $plugin['module'] ) . '"
504
					data-configure-url="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
505
					> ' . esc_html__( 'Enable', 'jetpack' ) . '</button>';
506
507
			// Jetpack installed, active, feature enabled; link to settings.
508
		} elseif (
509
			! empty( $plugin['configure_url'] ) &&
510
			current_user_can( 'jetpack_configure_modules' ) &&
511
			Jetpack::is_module_active( $plugin['module'] ) &&
512
			/** This filter is documented in class.jetpack-admin.php */
513
			apply_filters( 'jetpack_module_configurable_' . $plugin['module'], false )
514
		) {
515
			$links[] = '<a
516
				id="plugin-select-settings"
517
				class="jetpack-plugin-search__primary button jetpack-plugin-search__configure"
518
				href="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
519
				data-module="' . esc_attr( $plugin['module'] ) . '"
520
				data-track="configure"
521
				>' . esc_html__( 'Configure', 'jetpack' ) . '</a>';
522
			// Module is active, doesn't have options to configure
523
		} elseif ( Jetpack::is_module_active( $plugin['module'] ) ) {
524
			$links['jp_get_started'] = '<a
525
				id="plugin-select-settings"
526
				class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
527
				href="https://jetpack.com/redirect/?source=plugin-hint-learn-' . $plugin['module'] . '"
528
				data-module="' . esc_attr( $plugin['module'] ) . '"
529
				data-track="get_started"
530
				>' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
531
		}
532
533
		// Add link pointing to a relevant doc page in jetpack.com only if the Get started button isn't displayed.
534
		if ( ! empty( $plugin['learn_more_button'] ) && ! isset( $links['jp_get_started'] ) ) {
535
			$links[] = '<a
536
				class="jetpack-plugin-search__learn-more"
537
				href="' . esc_url( $plugin['learn_more_button'] ) . '"
538
				target="_blank"
539
				data-module="' . esc_attr( $plugin['module'] ) . '"
540
				data-track="learn_more"
541
				>' . esc_html__( 'Learn more', 'jetpack' ) . '</a>';
542
		}
543
544
		// Dismiss link
545
		$links[] = '<a
546
			class="jetpack-plugin-search__dismiss"
547
			data-module="' . esc_attr( $plugin['module'] ) . '"
548
			>' . esc_html__( 'Hide this suggestion', 'jetpack' ) . '</a>';
549
550
		return $links;
551
	}
552
553
}
554
555
/**
556
 * Master control that checks if Plugin search hints is active.
557
 *
558
 * @since 7.1.1
559
 *
560
 * @return bool True if PSH is active.
561
 */
562
function jetpack_is_psh_active() {
563
	// false means unset, 1 means active, 0 means inactive.
564
	$status = get_transient( 'jetpack_psh_status' );
565
566
	if ( false === $status ) {
567
		$error = false;
568
		$status = jetpack_get_remote_is_psh_active( $error );
569
		set_transient(
570
			'jetpack_psh_status',
571
			// Cache as int
572
			(int) $status,
573
			// If there was an error, still cache but for a shorter time
574
			( $error ? 5 : 15 ) * MINUTE_IN_SECONDS
575
		);
576
	}
577
578
	return (bool) $status;
579
}
580
581
/**
582
 * Makes remote request to determine if Plugin search hints is active.
583
 *
584
 * @since 7.1.1
585
 * @internal
586
 *
587
 * @param bool &$error Did the remote request result in an error?
588
 * @return bool True if PSH is active.
589
 */
590
function jetpack_get_remote_is_psh_active( &$error ) {
591
	$response = wp_remote_get( 'https://jetpack.com/psh-status/' );
592
	if ( is_wp_error( $response ) ) {
593
		$error = true;
594
		return true;
595
	}
596
597
	$body = wp_remote_retrieve_body( $response );
598
	if ( empty( $body ) ) {
599
		$error = true;
600
		return true;
601
	}
602
603
	$json = json_decode( $body );
604
	if ( ! isset( $json->active ) ) {
605
		$error = true;
606
		return true;
607
	}
608
609
	$error = false;
610
	return (bool) $json->active;
611
}
612