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