Completed
Push — remove/psh-status-request ( 1149b8 )
by
unknown
47:14 queued 39:58
created

plugin-search.php ➔ jetpack_is_psh_active()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Constants;
4
use Automattic\Jetpack\Tracking;
5
use Automattic\Jetpack\Redirect;
6
7
/**
8
 * Disable direct access and execution.
9
 */
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
15
if (
16
	is_admin() &&
17
	Jetpack::is_active() &&
18
	/** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
19
	apply_filters( 'jetpack_show_promotions', true ) &&
20
	// Disable feature hints when plugins cannot be installed.
21
	! Constants::is_true( 'DISALLOW_FILE_MODS' )
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'    => Redirect::get_url( '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' => Redirect::get_url( 'plugin-hint-upgrade-akismet' ),
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
		switch ( $feature ) {
433
			case 'sharing':
434
			case 'publicize':
435
				$configure_url = Redirect::get_url( 'calypso-marketing-connections' );
436
				break;
437
			case 'seo-tools':
438
				$configure_url = Redirect::get_url(
439
					'calypso-marketing-traffic',
440
					array(
441
						'anchor' => 'seo',
442
					)
443
				);
444
				break;
445
			case 'google-analytics':
446
				$configure_url = Redirect::get_url(
447
					'calypso-marketing-traffic',
448
					array(
449
						'anchor' => 'analytics',
450
					)
451
				);
452
				break;
453
			case 'wordads':
454
				$configure_url = Redirect::get_url( 'wpcom-ads-settings' );
455
				break;
456
		}
457
		return $configure_url;
458
	}
459
460
	/**
461
	 * Put some more appropriate links on our custom result cards.
462
	 */
463
	public function insert_module_related_links( $links, $plugin ) {
464
		if ( self::$slug !== $plugin['slug'] ) {
465
			return $links;
466
		}
467
468
		// By the time this filter is applied, self_admin_url was already applied and we don't need it anymore.
469
		remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
470
471
		$links = array();
472
473
		if ( 'akismet' === $plugin['module'] || 'vaultpress' === $plugin['module'] ) {
474
			$links['jp_get_started'] = '<a
475
				id="plugin-select-settings"
476
				class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
477
				href="' . esc_url( Redirect::get_url( 'plugin-hint-learn-' . $plugin['module'] ) ) . '"
478
				data-module="' . esc_attr( $plugin['module'] ) . '"
479
				data-track="get_started"
480
				>' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
481
			// Jetpack installed, active, feature not enabled; prompt to enable.
482
		} elseif (
483
			current_user_can( 'jetpack_activate_modules' ) &&
484
			! Jetpack::is_module_active( $plugin['module'] ) &&
485
			Jetpack_Plan::supports( $plugin['module'] )
486
		) {
487
			$links[] = '<button
488
					id="plugin-select-activate"
489
					class="jetpack-plugin-search__primary button"
490
					data-module="' . esc_attr( $plugin['module'] ) . '"
491
					data-configure-url="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
492
					> ' . esc_html__( 'Enable', 'jetpack' ) . '</button>';
493
494
			// Jetpack installed, active, feature enabled; link to settings.
495
		} elseif (
496
			! empty( $plugin['configure_url'] ) &&
497
			current_user_can( 'jetpack_configure_modules' ) &&
498
			Jetpack::is_module_active( $plugin['module'] ) &&
499
			/** This filter is documented in class.jetpack-admin.php */
500
			apply_filters( 'jetpack_module_configurable_' . $plugin['module'], false )
501
		) {
502
			$links[] = '<a
503
				id="plugin-select-settings"
504
				class="jetpack-plugin-search__primary button jetpack-plugin-search__configure"
505
				href="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
506
				data-module="' . esc_attr( $plugin['module'] ) . '"
507
				data-track="configure"
508
				>' . esc_html__( 'Configure', 'jetpack' ) . '</a>';
509
			// Module is active, doesn't have options to configure
510
		} elseif ( Jetpack::is_module_active( $plugin['module'] ) ) {
511
			$links['jp_get_started'] = '<a
512
				id="plugin-select-settings"
513
				class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
514
				href="' . esc_url( Redirect::get_url( 'plugin-hint-learn-' . $plugin['module'] ) ) . '"
515
				data-module="' . esc_attr( $plugin['module'] ) . '"
516
				data-track="get_started"
517
				>' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
518
		}
519
520
		// Add link pointing to a relevant doc page in jetpack.com only if the Get started button isn't displayed.
521
		if ( ! empty( $plugin['learn_more_button'] ) && ! isset( $links['jp_get_started'] ) ) {
522
			$links[] = '<a
523
				class="jetpack-plugin-search__learn-more"
524
				href="' . esc_url( $plugin['learn_more_button'] ) . '"
525
				target="_blank"
526
				data-module="' . esc_attr( $plugin['module'] ) . '"
527
				data-track="learn_more"
528
				>' . esc_html__( 'Learn more', 'jetpack' ) . '</a>';
529
		}
530
531
		// Dismiss link
532
		$links[] = '<a
533
			class="jetpack-plugin-search__dismiss"
534
			data-module="' . esc_attr( $plugin['module'] ) . '"
535
			>' . esc_html__( 'Hide this suggestion', 'jetpack' ) . '</a>';
536
537
		return $links;
538
	}
539
540
}
541