PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.5.2
Jetpack – WP Security, Backup, Speed, & Growth v11.5.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / plugin-search.php

plugin-search.php in Jetpack – WP Security, Backup, Speed, & Growth 11.5.2, at modules/plugin-search.php

614 lines 19.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Adds the PSH functionality to Jetpack.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Constants;
9 use Automattic\Jetpack\Redirect;
10 use Automattic\Jetpack\Tracking;
11
12 /**
13 * Disable direct access and execution.
14 */
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 if (
20 is_admin() &&
21 Jetpack::is_connection_ready() &&
22 /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
23 apply_filters( 'jetpack_show_promotions', true ) &&
24 // Disable feature hints when plugins cannot be installed.
25 ! Constants::is_true( 'DISALLOW_FILE_MODS' ) &&
26 jetpack_is_psh_active()
27 ) {
28 Jetpack_Plugin_Search::init();
29 }
30
31 // Register endpoints when WP REST API is initialized.
32 add_action( 'rest_api_init', array( 'Jetpack_Plugin_Search', 'register_endpoints' ) );
33
34 /**
35 * Class that includes cards in the plugin search results when users enter terms that match some Jetpack feature.
36 * Card can be dismissed and includes a title, description, button to enable the feature and a link for more information.
37 *
38 * @since 7.1.0
39 */
40 class Jetpack_Plugin_Search {
41
42 /**
43 * PSH slug name.
44 *
45 * @var string
46 */
47 public static $slug = 'jetpack-plugin-search';
48
49 /**
50 * Singleton constructor.
51 *
52 * @return Jetpack_Plugin_Search
53 */
54 public static function init() {
55 static $instance = null;
56
57 if ( ! $instance ) {
58 $instance = new Jetpack_Plugin_Search();
59 }
60
61 return $instance;
62 }
63
64 /**
65 * Jetpack_Plugin_Search constructor.
66 */
67 public function __construct() {
68 add_action( 'current_screen', array( $this, 'start' ) );
69 }
70
71 /**
72 * Add actions and filters only if this is the plugin installation screen and it's the first page.
73 *
74 * @param object $screen WP SCreen object.
75 *
76 * @since 7.1.0
77 */
78 public function start( $screen ) {
79 if ( 'plugin-install' === $screen->base && ( ! isset( $_GET['paged'] ) || 1 === intval( $_GET['paged'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
80 add_action( 'admin_enqueue_scripts', array( $this, 'load_plugins_search_script' ) );
81 add_filter( 'plugins_api_result', array( $this, 'inject_jetpack_module_suggestion' ), 10, 3 );
82 add_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
83 add_filter( 'plugin_install_action_links', array( $this, 'insert_module_related_links' ), 10, 2 );
84 }
85 }
86
87 /**
88 * Modify URL used to fetch to plugin information so it pulls Jetpack plugin page.
89 *
90 * @param string $url URL to load in dialog pulling the plugin page from wporg.
91 *
92 * @since 7.1.0
93 *
94 * @return string The URL with 'jetpack' instead of 'jetpack-plugin-search'.
95 */
96 public function plugin_details( $url ) {
97 return false !== stripos( $url, 'tab=plugin-information&amp;plugin=' . self::$slug )
98 ? 'plugin-install.php?tab=plugin-information&amp;plugin=jetpack&amp;TB_iframe=true&amp;width=600&amp;height=550'
99 : $url;
100 }
101
102 /**
103 * Register REST API endpoints.
104 *
105 * @since 7.1.0
106 */
107 public static function register_endpoints() {
108 register_rest_route(
109 'jetpack/v4',
110 '/hints',
111 array(
112 'methods' => WP_REST_Server::EDITABLE,
113 'callback' => __CLASS__ . '::dismiss',
114 'permission_callback' => __CLASS__ . '::can_request',
115 'args' => array(
116 'hint' => array(
117 'default' => '',
118 'type' => 'string',
119 'required' => true,
120 'validate_callback' => __CLASS__ . '::is_hint_id',
121 ),
122 ),
123 )
124 );
125 }
126
127 /**
128 * A WordPress REST API permission callback method that accepts a request object and
129 * decides if the current user has enough privileges to act.
130 *
131 * @since 7.1.0
132 *
133 * @return bool does a current user have enough privileges.
134 */
135 public static function can_request() {
136 return current_user_can( 'jetpack_admin_page' );
137 }
138
139 /**
140 * Validates that the ID of the hint to dismiss is a string.
141 *
142 * @since 7.1.0
143 *
144 * @param string|bool $value Value to check.
145 * @param WP_REST_Request $request The request sent to the WP REST API.
146 * @param string $param Name of the parameter passed to endpoint holding $value.
147 *
148 * @return bool|WP_Error
149 */
150 public static function is_hint_id( $value, $request, $param ) {
151 return in_array( $value, Jetpack::get_available_modules(), true )
152 ? true
153 /* translators: %s is the name of a parameter passed to an endpoint. */
154 : new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an alphanumeric string.', 'jetpack' ), $param ) );
155 }
156
157 /**
158 * A WordPress REST API callback method that accepts a request object and decides what to do with it.
159 *
160 * @param WP_REST_Request $request {
161 * Array of parameters received by request.
162 *
163 * @type string $hint Slug of card to dismiss.
164 * }
165 *
166 * @since 7.1.0
167 *
168 * @return bool|array|WP_Error a resulting value or object, or an error.
169 */
170 public static function dismiss( WP_REST_Request $request ) {
171 return self::add_to_dismissed_hints( $request['hint'] )
172 ? rest_ensure_response( array( 'code' => 'success' ) )
173 : new WP_Error( 'not_dismissed', esc_html__( 'The card could not be dismissed', 'jetpack' ), array( 'status' => 400 ) );
174 }
175
176 /**
177 * Returns a list of previously dismissed hints.
178 *
179 * @since 7.1.0
180 *
181 * @return array List of dismissed hints.
182 */
183 protected static function get_dismissed_hints() {
184 $dismissed_hints = Jetpack_Options::get_option( 'dismissed_hints' );
185 return isset( $dismissed_hints ) && is_array( $dismissed_hints )
186 ? $dismissed_hints
187 : array();
188 }
189
190 /**
191 * Save the hint in the list of dismissed hints.
192 *
193 * @since 7.1.0
194 *
195 * @param string $hint The hint id, which is a Jetpack module slug.
196 *
197 * @return bool Whether the card was added to the list and hence dismissed.
198 */
199 protected static function add_to_dismissed_hints( $hint ) {
200 return Jetpack_Options::update_option( 'dismissed_hints', array_merge( self::get_dismissed_hints(), array( $hint ) ) );
201 }
202
203 /**
204 * Checks that the module slug passed should be displayed.
205 *
206 * A feature hint will be displayed if it has not been dismissed before or if 2 or fewer other hints have been dismissed.
207 *
208 * @since 7.2.1
209 *
210 * @param string $hint The hint id, which is a Jetpack module slug.
211 *
212 * @return bool True if $hint should be displayed.
213 */
214 protected function should_display_hint( $hint ) {
215 $dismissed_hints = $this->get_dismissed_hints();
216 // If more than 2 hints have been dismissed, then show no more.
217 if ( 2 < count( $dismissed_hints ) ) {
218 return false;
219 }
220
221 $plan = Jetpack_Plan::get();
222 if ( isset( $plan['class'] ) && ( 'free' === $plan['class'] || 'personal' === $plan['class'] ) && 'vaultpress' === $hint ) {
223 return false;
224 }
225
226 return ! in_array( $hint, $dismissed_hints, true );
227 }
228
229 /**
230 * Load the search scripts and CSS for PSH.
231 */
232 public function load_plugins_search_script() {
233 wp_enqueue_script( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION, true );
234 wp_localize_script(
235 self::$slug,
236 'jetpackPluginSearch',
237 array(
238 'nonce' => wp_create_nonce( 'wp_rest' ),
239 'base_rest_url' => rest_url( '/jetpack/v4' ),
240 'poweredBy' => esc_html__( 'by Jetpack (installed)', 'jetpack' ),
241 'manageSettings' => esc_html__( 'Configure', 'jetpack' ),
242 'activateModule' => esc_html__( 'Activate Module', 'jetpack' ),
243 'getStarted' => esc_html__( 'Get started', 'jetpack' ),
244 'activated' => esc_html__( 'Activated', 'jetpack' ),
245 'activating' => esc_html__( 'Activating', 'jetpack' ),
246 'logo' => 'https://ps.w.org/jetpack/assets/icon.svg?rev=1791404',
247 'legend' => esc_html__(
248 'This suggestion was made by Jetpack, the security and performance plugin already installed on your site.',
249 'jetpack'
250 ),
251 'supportText' => esc_html__(
252 'Learn more about these suggestions.',
253 'jetpack'
254 ),
255 'supportLink' => Redirect::get_url( 'plugin-hint-learn-support' ),
256 'hideText' => esc_html__( 'Hide this suggestion', 'jetpack' ),
257 )
258 );
259
260 wp_enqueue_style( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
261 }
262
263 /**
264 * Get the plugin repo's data for Jetpack to populate the fields with.
265 *
266 * @return array|mixed|object|WP_Error
267 */
268 public static function get_jetpack_plugin_data() {
269 $data = get_transient( 'jetpack_plugin_data' );
270
271 if ( false === $data || is_wp_error( $data ) ) {
272 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
273 $data = plugins_api(
274 'plugin_information',
275 array(
276 'slug' => 'jetpack',
277 'is_ssl' => is_ssl(),
278 'fields' => array(
279 'banners' => true,
280 'reviews' => true,
281 'active_installs' => true,
282 'versions' => false,
283 'sections' => false,
284 ),
285 )
286 );
287 set_transient( 'jetpack_plugin_data', $data, DAY_IN_SECONDS );
288 }
289
290 return $data;
291 }
292
293 /**
294 * Create a list with additional features for those we don't have a module, like Akismet.
295 *
296 * @since 7.1.0
297 *
298 * @return array List of features.
299 */
300 public function get_extra_features() {
301 return array(
302 'akismet' => array(
303 'name' => 'Akismet',
304 'search_terms' => 'akismet, anti-spam, antispam, comments, spam, spam protection, form spam, captcha, no captcha, nocaptcha, recaptcha, phising, google',
305 'short_description' => esc_html__( 'Keep your visitors and search engines happy by stopping comment and contact form spam with Akismet.', 'jetpack' ),
306 'requires_connection' => true,
307 'module' => 'akismet',
308 'sort' => '16',
309 'learn_more_button' => Redirect::get_url( 'plugin-hint-upgrade-akismet' ),
310 'configure_url' => admin_url( 'admin.php?page=akismet-key-config' ),
311 ),
312 );
313 }
314
315 /**
316 * Intercept the plugins API response and add in an appropriate card for Jetpack
317 *
318 * @param object $result Plugin search results.
319 * @param string $action unused.
320 * @param object $args Search args.
321 */
322 public function inject_jetpack_module_suggestion( $result, $action, $args ) {
323 // Looks like a search query; it's matching time.
324 if ( ! empty( $args->search ) ) {
325 require_once JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php';
326 $tracking = new Tracking();
327 $jetpack_modules_list = array_intersect_key(
328 array_merge( $this->get_extra_features(), Jetpack_Admin::init()->get_modules() ),
329 array_flip(
330 array(
331 'contact-form',
332 'lazy-images',
333 'monitor',
334 'photon',
335 'photon-cdn',
336 'protect',
337 'publicize',
338 'related-posts',
339 'sharedaddy',
340 'akismet',
341 'vaultpress',
342 'videopress',
343 'search',
344 )
345 )
346 );
347 uasort( $jetpack_modules_list, array( $this, 'by_sorting_option' ) );
348
349 // Record event when user searches for a term over 3 chars (less than 3 is not very useful).
350 if ( strlen( $args->search ) >= 3 ) {
351 $tracking->record_user_event( 'wpa_plugin_search_term', array( 'search_term' => $args->search ) );
352 }
353
354 // Lowercase, trim, remove punctuation/special chars, decode url, remove 'jetpack'.
355 $normalized_term = $this->sanitize_search_term( $args->search );
356
357 $matching_module = null;
358
359 // Try to match a passed search term with module's search terms.
360 foreach ( $jetpack_modules_list as $module_slug => $module_opts ) {
361 /*
362 * Does the site's current plan support the feature?
363 * We don't use Jetpack_Plan::supports() here because
364 * that check always returns Akismet as supported,
365 * since Akismet has a free version.
366 */
367 $current_plan = Jetpack_Plan::get();
368 $is_supported_by_plan = in_array( $module_slug, $current_plan['supports'], true );
369
370 if (
371 false !== stripos( $module_opts['search_terms'] . ', ' . $module_opts['name'], $normalized_term )
372 && $is_supported_by_plan
373 ) {
374 $matching_module = $module_slug;
375 break;
376 }
377 }
378
379 if ( isset( $matching_module ) && $this->should_display_hint( $matching_module ) ) {
380 // Record event when a matching feature is found.
381 $tracking->record_user_event( 'wpa_plugin_search_match_found', array( 'feature' => $matching_module ) );
382
383 $inject = (array) self::get_jetpack_plugin_data();
384 $image_url = plugins_url( 'modules/plugin-search/psh', JETPACK__PLUGIN_FILE );
385 $overrides = array(
386 'plugin-search' => true, // Helps to determine if that an injected card.
387 'name' => sprintf( // Supplement name/description so that they clearly indicate this was added.
388 /* translators: Jetpack module name */
389 esc_html_x( 'Jetpack: %s', 'Jetpack: Module Name', 'jetpack' ),
390 $jetpack_modules_list[ $matching_module ]['name']
391 ),
392 'short_description' => $jetpack_modules_list[ $matching_module ]['short_description'],
393 'requires_connection' => (bool) $jetpack_modules_list[ $matching_module ]['requires_connection'],
394 'slug' => self::$slug,
395 'version' => JETPACK__VERSION,
396 'icons' => array(
397 '1x' => "$image_url-128.png",
398 '2x' => "$image_url-256.png",
399 'svg' => "$image_url.svg",
400 ),
401 );
402
403 // Splice in the base module data.
404 $inject = array_merge( $inject, $jetpack_modules_list[ $matching_module ], $overrides );
405
406 // Add it to the top of the list.
407 $result->plugins = array_filter( $result->plugins, array( $this, 'filter_cards' ) );
408 array_unshift( $result->plugins, $inject );
409 }
410 }
411 return $result;
412 }
413
414 /**
415 * Remove cards for Jetpack plugins since we don't want duplicates.
416 *
417 * @since 7.1.0
418 * @since 7.2.0 Only remove Jetpack.
419 * @since 7.4.0 Simplify for WordPress 5.1+.
420 *
421 * @param array|object $plugin WordPress search result card.
422 *
423 * @return bool
424 */
425 public function filter_cards( $plugin ) {
426 /*
427 * $plugin is normally an array.
428 * However, since the response data can be filtered,
429 * we cannot fully trust its format.
430 * Let's handle both arrays and objects, and bail if it's neither.
431 */
432 if ( is_array( $plugin ) && ! empty( $plugin['slug'] ) ) {
433 $slug = $plugin['slug'];
434 } elseif ( is_object( $plugin ) && ! empty( $plugin->slug ) ) {
435 $slug = $plugin->slug;
436 } else {
437 return false;
438 }
439
440 return ! in_array( $slug, array( 'jetpack' ), true );
441 }
442
443 /**
444 * Take a raw search query and return something a bit more standardized and
445 * easy to work with.
446 *
447 * @param string $term The raw search term.
448 * @return string A simplified/sanitized version.
449 */
450 private function sanitize_search_term( $term ) {
451 $term = strtolower( urldecode( $term ) );
452
453 // remove non-alpha/space chars.
454 $term = preg_replace( '/[^a-z ]/', '', $term );
455
456 // remove strings that don't help matches.
457 $term = trim( str_replace( array( 'jetpack', 'jp', 'free', 'wordpress' ), '', $term ) );
458
459 return $term;
460 }
461
462 /**
463 * Callback function to sort the array of modules by the sort option.
464 *
465 * @param array $m1 Array 1 to sort.
466 * @param array $m2 Array 2 to sort.
467 */
468 private function by_sorting_option( $m1, $m2 ) {
469 return $m1['sort'] - $m2['sort'];
470 }
471
472 /**
473 * Modify the URL to the feature settings, for example Publicize.
474 * Sharing is included here because while we still have a page in WP Admin,
475 * we prefer to send users to Calypso.
476 *
477 * @param string $feature Feature.
478 * @param string $configure_url URL to configure feature.
479 *
480 * @return string
481 * @since 7.1.0
482 */
483 private function get_configure_url( $feature, $configure_url ) {
484 switch ( $feature ) {
485 case 'sharing':
486 case 'publicize':
487 $configure_url = Redirect::get_url( 'calypso-marketing-connections' );
488 break;
489 case 'seo-tools':
490 $configure_url = Redirect::get_url(
491 'calypso-marketing-traffic',
492 array(
493 'anchor' => 'seo',
494 )
495 );
496 break;
497 case 'google-analytics':
498 $configure_url = Redirect::get_url(
499 'calypso-marketing-traffic',
500 array(
501 'anchor' => 'analytics',
502 )
503 );
504 break;
505 case 'wordads':
506 $configure_url = Redirect::get_url( 'wpcom-ads-settings' );
507 break;
508 }
509 return $configure_url;
510 }
511
512 /**
513 * Put some more appropriate links on our custom result cards.
514 *
515 * @param array $links Related links.
516 * @param array $plugin Plugin result information.
517 */
518 public function insert_module_related_links( $links, $plugin ) {
519 if ( self::$slug !== $plugin['slug'] ) {
520 return $links;
521 }
522
523 // By the time this filter is applied, self_admin_url was already applied and we don't need it anymore.
524 remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
525
526 $links = array();
527
528 if ( 'akismet' === $plugin['module'] || 'vaultpress' === $plugin['module'] ) {
529 $links['jp_get_started'] = '<a
530 id="plugin-select-settings"
531 class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
532 href="' . esc_url( Redirect::get_url( 'plugin-hint-learn-' . $plugin['module'] ) ) . '"
533 data-module="' . esc_attr( $plugin['module'] ) . '"
534 data-track="get_started"
535 >' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
536 // Jetpack installed, active, feature not enabled; prompt to enable.
537 } elseif (
538 current_user_can( 'jetpack_activate_modules' ) &&
539 ! Jetpack::is_module_active( $plugin['module'] ) &&
540 Jetpack_Plan::supports( $plugin['module'] )
541 ) {
542 $links[] = '<button
543 id="plugin-select-activate"
544 class="jetpack-plugin-search__primary button"
545 data-module="' . esc_attr( $plugin['module'] ) . '"
546 data-configure-url="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
547 > ' . esc_html__( 'Enable', 'jetpack' ) . '</button>';
548
549 // Jetpack installed, active, feature enabled; link to settings.
550 } elseif (
551 ! empty( $plugin['configure_url'] ) &&
552 current_user_can( 'jetpack_configure_modules' ) &&
553 Jetpack::is_module_active( $plugin['module'] ) &&
554 /** This filter is documented in class.jetpack-admin.php */
555 apply_filters( 'jetpack_module_configurable_' . $plugin['module'], false )
556 ) {
557 $links[] = '<a
558 id="plugin-select-settings"
559 class="jetpack-plugin-search__primary button jetpack-plugin-search__configure"
560 href="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
561 data-module="' . esc_attr( $plugin['module'] ) . '"
562 data-track="configure"
563 >' . esc_html__( 'Configure', 'jetpack' ) . '</a>';
564 // Module is active, doesn't have options to configure.
565 } elseif ( Jetpack::is_module_active( $plugin['module'] ) ) {
566 $links['jp_get_started'] = '<a
567 id="plugin-select-settings"
568 class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
569 href="' . esc_url( Redirect::get_url( 'plugin-hint-learn-' . $plugin['module'] ) ) . '"
570 data-module="' . esc_attr( $plugin['module'] ) . '"
571 data-track="get_started"
572 >' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
573 }
574
575 // Add link pointing to a relevant doc page in jetpack.com only if the Get started button isn't displayed.
576 if ( ! empty( $plugin['learn_more_button'] ) && ! isset( $links['jp_get_started'] ) ) {
577 $links[] = '<a
578 class="jetpack-plugin-search__learn-more"
579 href="' . esc_url( $plugin['learn_more_button'] ) . '"
580 target="_blank"
581 data-module="' . esc_attr( $plugin['module'] ) . '"
582 data-track="learn_more"
583 >' . esc_html__( 'Learn more', 'jetpack' ) . '</a>';
584 }
585
586 // Dismiss link.
587 $links[] = '<a
588 class="jetpack-plugin-search__dismiss"
589 data-module="' . esc_attr( $plugin['module'] ) . '"
590 >' . esc_html__( 'Hide this suggestion', 'jetpack' ) . '</a>';
591
592 return $links;
593 }
594
595 }
596
597 /**
598 * Master control that checks if Plugin search hints is active.
599 *
600 * @since 7.1.1
601 *
602 * @return bool True if PSH is active.
603 */
604 function jetpack_is_psh_active() {
605 /**
606 * Disables the Plugin Search Hints feature found when searching the plugins page.
607 *
608 * @since 8.7.0
609 *
610 * @param bool Set false to disable the feature.
611 */
612 return apply_filters( 'jetpack_psh_active', true );
613 }
614