PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.0
Jetpack – WP Security, Backup, Speed, & Growth v8.0
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 8.0, at modules/plugin-search.php

607 lines 19.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ) );
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 ) );
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 * Builds a URL to purchase and upgrade inserting the site fragment and the affiliate code if it exists.
421 *
422 * @param string $feature Module slug (or forged one for extra features).
423 *
424 * @since 7.1.0
425 *
426 * @return string URL to upgrade.
427 */
428 private function get_upgrade_url( $feature ) {
429 $site_raw_url = Jetpack::build_raw_urls( get_home_url() );
430 $affiliateCode = Jetpack_Affiliate::init()->get_affiliate_code();
431 $user = wp_get_current_user()->ID;
432 return "https://jetpack.com/redirect/?source=plugin-hint-upgrade-$feature&site=$site_raw_url&u=$user" .
433 ( $affiliateCode ? "&aff=$affiliateCode" : '' );
434 }
435
436 /**
437 * Modify the URL to the feature settings, for example Publicize.
438 * Sharing is included here because while we still have a page in WP Admin,
439 * we prefer to send users to Calypso.
440 *
441 * @param string $feature
442 * @param string $configure_url
443 *
444 * @return string
445 * @since 7.1.0
446 *
447 */
448 private function get_configure_url( $feature, $configure_url ) {
449 $siteFragment = Jetpack::build_raw_urls( get_home_url() );
450 switch ( $feature ) {
451 case 'sharing':
452 case 'publicize':
453 $configure_url = "https://wordpress.com/marketing/connections/$siteFragment";
454 break;
455 case 'seo-tools':
456 $configure_url = "https://wordpress.com/marketing/traffic/$siteFragment#seo";
457 break;
458 case 'google-analytics':
459 $configure_url = "https://wordpress.com/marketing/traffic/$siteFragment#analytics";
460 break;
461 case 'wordads':
462 $configure_url = "https://wordpress.com/ads/settings/$siteFragment";
463 break;
464 }
465 return $configure_url;
466 }
467
468 /**
469 * Put some more appropriate links on our custom result cards.
470 */
471 public function insert_module_related_links( $links, $plugin ) {
472 if ( self::$slug !== $plugin['slug'] ) {
473 return $links;
474 }
475
476 // By the time this filter is applied, self_admin_url was already applied and we don't need it anymore.
477 remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
478
479 $links = array();
480
481 if ( 'akismet' === $plugin['module'] || 'vaultpress' === $plugin['module'] ) {
482 $links['jp_get_started'] = '<a
483 id="plugin-select-settings"
484 class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
485 href="https://jetpack.com/redirect/?source=plugin-hint-learn-' . $plugin['module'] . '"
486 data-module="' . esc_attr( $plugin['module'] ) . '"
487 data-track="get_started"
488 >' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
489 // Jetpack installed, active, feature not enabled; prompt to enable.
490 } elseif (
491 current_user_can( 'jetpack_activate_modules' ) &&
492 ! Jetpack::is_module_active( $plugin['module'] ) &&
493 Jetpack_Plan::supports( $plugin['module'] )
494 ) {
495 $links[] = '<button
496 id="plugin-select-activate"
497 class="jetpack-plugin-search__primary button"
498 data-module="' . esc_attr( $plugin['module'] ) . '"
499 data-configure-url="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
500 > ' . esc_html__( 'Enable', 'jetpack' ) . '</button>';
501
502 // Jetpack installed, active, feature enabled; link to settings.
503 } elseif (
504 ! empty( $plugin['configure_url'] ) &&
505 current_user_can( 'jetpack_configure_modules' ) &&
506 Jetpack::is_module_active( $plugin['module'] ) &&
507 /** This filter is documented in class.jetpack-admin.php */
508 apply_filters( 'jetpack_module_configurable_' . $plugin['module'], false )
509 ) {
510 $links[] = '<a
511 id="plugin-select-settings"
512 class="jetpack-plugin-search__primary button jetpack-plugin-search__configure"
513 href="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
514 data-module="' . esc_attr( $plugin['module'] ) . '"
515 data-track="configure"
516 >' . esc_html__( 'Configure', 'jetpack' ) . '</a>';
517 // Module is active, doesn't have options to configure
518 } elseif ( Jetpack::is_module_active( $plugin['module'] ) ) {
519 $links['jp_get_started'] = '<a
520 id="plugin-select-settings"
521 class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
522 href="https://jetpack.com/redirect/?source=plugin-hint-learn-' . $plugin['module'] . '"
523 data-module="' . esc_attr( $plugin['module'] ) . '"
524 data-track="get_started"
525 >' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
526 }
527
528 // Add link pointing to a relevant doc page in jetpack.com only if the Get started button isn't displayed.
529 if ( ! empty( $plugin['learn_more_button'] ) && ! isset( $links['jp_get_started'] ) ) {
530 $links[] = '<a
531 class="jetpack-plugin-search__learn-more"
532 href="' . esc_url( $plugin['learn_more_button'] ) . '"
533 target="_blank"
534 data-module="' . esc_attr( $plugin['module'] ) . '"
535 data-track="learn_more"
536 >' . esc_html__( 'Learn more', 'jetpack' ) . '</a>';
537 }
538
539 // Dismiss link
540 $links[] = '<a
541 class="jetpack-plugin-search__dismiss"
542 data-module="' . esc_attr( $plugin['module'] ) . '"
543 >' . esc_html__( 'Hide this suggestion', 'jetpack' ) . '</a>';
544
545 return $links;
546 }
547
548 }
549
550 /**
551 * Master control that checks if Plugin search hints is active.
552 *
553 * @since 7.1.1
554 *
555 * @return bool True if PSH is active.
556 */
557 function jetpack_is_psh_active() {
558 // false means unset, 1 means active, 0 means inactive.
559 $status = get_transient( 'jetpack_psh_status' );
560
561 if ( false === $status ) {
562 $error = false;
563 $status = jetpack_get_remote_is_psh_active( $error );
564 set_transient(
565 'jetpack_psh_status',
566 // Cache as int
567 (int) $status,
568 // If there was an error, still cache but for a shorter time
569 ( $error ? 5 : 15 ) * MINUTE_IN_SECONDS
570 );
571 }
572
573 return (bool) $status;
574 }
575
576 /**
577 * Makes remote request to determine if Plugin search hints is active.
578 *
579 * @since 7.1.1
580 * @internal
581 *
582 * @param bool &$error Did the remote request result in an error?
583 * @return bool True if PSH is active.
584 */
585 function jetpack_get_remote_is_psh_active( &$error ) {
586 $response = wp_remote_get( 'https://jetpack.com/psh-status/' );
587 if ( is_wp_error( $response ) ) {
588 $error = true;
589 return true;
590 }
591
592 $body = wp_remote_retrieve_body( $response );
593 if ( empty( $body ) ) {
594 $error = true;
595 return true;
596 }
597
598 $json = json_decode( $body );
599 if ( ! isset( $json->active ) ) {
600 $error = true;
601 return true;
602 }
603
604 $error = false;
605 return (bool) $json->active;
606 }
607