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