PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 12.2.3
Jetpack – WP Security, Backup, Speed, & Growth v12.2.3
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 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / plugin-search.php
jetpack / modules Last commit date
calypsoify 3 years ago carousel 3 years ago cloudflare-analytics 4 years ago comment-likes 5 years ago comments 3 years ago contact-form 1 year ago custom-css 3 years ago custom-post-types 3 years ago geo-location 4 years ago google-analytics 3 years ago gravatar 6 years ago infinite-scroll 3 years ago likes 3 years ago markdown 3 years ago masterbar 3 years ago memberships 3 years ago photon-cdn 3 years ago plugin-search 4 years ago post-by-email 3 years ago related-posts 3 years ago scan 3 years ago seo-tools 3 years ago sharedaddy 3 years ago shortcodes 3 years ago simple-payments 3 years ago site-icon 4 years ago sitemaps 3 years ago sso 3 years ago stats 3 years ago subscriptions 3 years ago theme-tools 3 years ago tiled-gallery 3 years ago verification-tools 4 years ago videopress 3 years ago widget-visibility 3 years ago widgets 3 years ago woocommerce-analytics 3 years ago wordads 3 years ago wpcom-block-editor 3 years ago wpcom-tos 5 years ago action-bar.php 3 years ago carousel.php 4 years ago comment-likes.php 3 years ago comments.php 4 years ago contact-form.php 3 years ago copy-post.php 3 years ago custom-content-types.php 4 years ago custom-css.php 3 years ago enhanced-distribution.php 4 years ago geo-location.php 4 years ago google-analytics.php 4 years ago google-fonts.php 3 years ago gravatar-hovercards.php 3 years ago infinite-scroll.php 3 years ago json-api.php 5 years ago latex.php 4 years ago lazy-images.php 4 years ago likes.php 3 years ago markdown.php 4 years ago masterbar.php 4 years ago module-extras.php 4 years ago module-headings.php 3 years ago module-info.php 3 years ago monitor.php 4 years ago notes.php 3 years ago photon-cdn.php 3 years ago photon.php 3 years ago plugin-search.php 3 years ago post-by-email.php 5 years ago post-list.php 4 years ago protect.php 3 years ago publicize.php 3 years ago related-posts.php 4 years ago search.php 4 years ago seo-tools.php 4 years ago sharedaddy.php 4 years ago shortcodes.php 3 years ago shortlinks.php 4 years ago sitemaps.php 4 years ago sso.php 3 years ago stats.php 3 years ago subscriptions.php 3 years ago theme-tools.php 3 years ago tiled-gallery.php 4 years ago vaultpress.php 4 years ago verification-tools.php 5 years ago videopress.php 4 years ago waf.php 3 years ago widget-visibility.php 4 years ago widgets.php 3 years ago woocommerce-analytics.php 5 years ago wordads.php 5 years ago wpgroho.js 6 years ago
plugin-search.php
624 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Adds the PSH functionality to Jetpack.
4 *
5 * @package automattic/jetpack
6 */
7
8 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
9
10 use Automattic\Jetpack\Constants;
11 use Automattic\Jetpack\Redirect;
12 use Automattic\Jetpack\Tracking;
13
14 /**
15 * Disable direct access and execution.
16 */
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 if (
22 is_admin() &&
23 Jetpack::is_connection_ready() &&
24 /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
25 apply_filters( 'jetpack_show_promotions', true ) &&
26 // Disable feature hints when plugins cannot be installed.
27 ! Constants::is_true( 'DISALLOW_FILE_MODS' ) &&
28 jetpack_is_psh_active()
29 ) {
30 Jetpack_Plugin_Search::init();
31 }
32
33 // Register endpoints when WP REST API is initialized.
34 add_action( 'rest_api_init', array( 'Jetpack_Plugin_Search', 'register_endpoints' ) );
35
36 /**
37 * Class that includes cards in the plugin search results when users enter terms that match some Jetpack feature.
38 * Card can be dismissed and includes a title, description, button to enable the feature and a link for more information.
39 *
40 * @since 7.1.0
41 */
42 class Jetpack_Plugin_Search {
43
44 /**
45 * PSH slug name.
46 *
47 * @var string
48 */
49 public static $slug = 'jetpack-plugin-search';
50
51 /**
52 * Singleton constructor.
53 *
54 * @return Jetpack_Plugin_Search
55 */
56 public static function init() {
57 static $instance = null;
58
59 if ( ! $instance ) {
60 $instance = new Jetpack_Plugin_Search();
61 }
62
63 return $instance;
64 }
65
66 /**
67 * Jetpack_Plugin_Search constructor.
68 */
69 public function __construct() {
70 add_action( 'current_screen', array( $this, 'start' ) );
71 }
72
73 /**
74 * Add actions and filters only if this is the plugin installation screen and it's the first page.
75 *
76 * @param object $screen WP SCreen object.
77 *
78 * @since 7.1.0
79 */
80 public function start( $screen ) {
81 if ( 'plugin-install' === $screen->base && ( ! isset( $_GET['paged'] ) || 1 === intval( $_GET['paged'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
82 add_action( 'admin_enqueue_scripts', array( $this, 'load_plugins_search_script' ) );
83 add_filter( 'plugins_api_result', array( $this, 'inject_jetpack_module_suggestion' ), 10, 3 );
84 add_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
85 add_filter( 'plugin_install_action_links', array( $this, 'insert_module_related_links' ), 10, 2 );
86 }
87 }
88
89 /**
90 * Modify URL used to fetch to plugin information so it pulls Jetpack plugin page.
91 *
92 * @param string $url URL to load in dialog pulling the plugin page from wporg.
93 *
94 * @since 7.1.0
95 *
96 * @return string The URL with 'jetpack' instead of 'jetpack-plugin-search'.
97 */
98 public function plugin_details( $url ) {
99 return false !== stripos( $url, 'tab=plugin-information&amp;plugin=' . self::$slug )
100 ? 'plugin-install.php?tab=plugin-information&amp;plugin=jetpack&amp;TB_iframe=true&amp;width=600&amp;height=550'
101 : $url;
102 }
103
104 /**
105 * Register REST API endpoints.
106 *
107 * @since 7.1.0
108 */
109 public static function register_endpoints() {
110 register_rest_route(
111 'jetpack/v4',
112 '/hints',
113 array(
114 'methods' => WP_REST_Server::EDITABLE,
115 'callback' => __CLASS__ . '::dismiss',
116 'permission_callback' => __CLASS__ . '::can_request',
117 'args' => array(
118 'hint' => array(
119 'default' => '',
120 'type' => 'string',
121 'required' => true,
122 'validate_callback' => __CLASS__ . '::is_hint_id',
123 ),
124 ),
125 )
126 );
127 }
128
129 /**
130 * A WordPress REST API permission callback method that accepts a request object and
131 * decides if the current user has enough privileges to act.
132 *
133 * @since 7.1.0
134 *
135 * @return bool does a current user have enough privileges.
136 */
137 public static function can_request() {
138 return current_user_can( 'jetpack_admin_page' );
139 }
140
141 /**
142 * Validates that the ID of the hint to dismiss is a string.
143 *
144 * @since 7.1.0
145 *
146 * @param string|bool $value Value to check.
147 * @param WP_REST_Request $request The request sent to the WP REST API.
148 * @param string $param Name of the parameter passed to endpoint holding $value.
149 *
150 * @return bool|WP_Error
151 */
152 public static function is_hint_id( $value, $request, $param ) {
153 return in_array( $value, Jetpack::get_available_modules(), true )
154 ? true
155 /* translators: %s is the name of a parameter passed to an endpoint. */
156 : new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an alphanumeric string.', 'jetpack' ), $param ) );
157 }
158
159 /**
160 * A WordPress REST API callback method that accepts a request object and decides what to do with it.
161 *
162 * @param WP_REST_Request $request {
163 * Array of parameters received by request.
164 *
165 * @type string $hint Slug of card to dismiss.
166 * }
167 *
168 * @since 7.1.0
169 *
170 * @return bool|array|WP_Error a resulting value or object, or an error.
171 */
172 public static function dismiss( WP_REST_Request $request ) {
173 return self::add_to_dismissed_hints( $request['hint'] )
174 ? rest_ensure_response( array( 'code' => 'success' ) )
175 : new WP_Error( 'not_dismissed', esc_html__( 'The card could not be dismissed', 'jetpack' ), array( 'status' => 400 ) );
176 }
177
178 /**
179 * Returns a list of previously dismissed hints.
180 *
181 * @since 7.1.0
182 *
183 * @return array List of dismissed hints.
184 */
185 protected static function get_dismissed_hints() {
186 $dismissed_hints = Jetpack_Options::get_option( 'dismissed_hints' );
187 return isset( $dismissed_hints ) && is_array( $dismissed_hints )
188 ? $dismissed_hints
189 : array();
190 }
191
192 /**
193 * Save the hint in the list of dismissed hints.
194 *
195 * @since 7.1.0
196 *
197 * @param string $hint The hint id, which is a Jetpack module slug.
198 *
199 * @return bool Whether the card was added to the list and hence dismissed.
200 */
201 protected static function add_to_dismissed_hints( $hint ) {
202 return Jetpack_Options::update_option( 'dismissed_hints', array_merge( self::get_dismissed_hints(), array( $hint ) ) );
203 }
204
205 /**
206 * Checks that the module slug passed should be displayed.
207 *
208 * A feature hint will be displayed if it has not been dismissed before or if 2 or fewer other hints have been dismissed.
209 *
210 * @since 7.2.1
211 *
212 * @param string $hint The hint id, which is a Jetpack module slug.
213 *
214 * @return bool True if $hint should be displayed.
215 */
216 protected function should_display_hint( $hint ) {
217 $dismissed_hints = static::get_dismissed_hints();
218 // If more than 2 hints have been dismissed, then show no more.
219 if ( 2 < count( $dismissed_hints ) ) {
220 return false;
221 }
222
223 $plan = Jetpack_Plan::get();
224 if ( isset( $plan['class'] ) && ( 'free' === $plan['class'] || 'personal' === $plan['class'] ) && 'vaultpress' === $hint ) {
225 return false;
226 }
227
228 return ! in_array( $hint, $dismissed_hints, true );
229 }
230
231 /**
232 * Load the search scripts and CSS for PSH.
233 */
234 public function load_plugins_search_script() {
235 wp_enqueue_script( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION, true );
236 wp_localize_script(
237 self::$slug,
238 'jetpackPluginSearch',
239 array(
240 'nonce' => wp_create_nonce( 'wp_rest' ),
241 'base_rest_url' => rest_url( '/jetpack/v4' ),
242 'poweredBy' => esc_html__( 'by Jetpack (installed)', 'jetpack' ),
243 'manageSettings' => esc_html__( 'Configure', 'jetpack' ),
244 'activateModule' => esc_html__( 'Activate Module', 'jetpack' ),
245 'getStarted' => esc_html__( 'Get started', 'jetpack' ),
246 'activated' => esc_html__( 'Activated', 'jetpack' ),
247 'activating' => esc_html__( 'Activating', 'jetpack' ),
248 'logo' => 'https://ps.w.org/jetpack/assets/icon.svg?rev=1791404',
249 'legend' => esc_html__(
250 'This suggestion was made by Jetpack, the security and performance plugin already installed on your site.',
251 'jetpack'
252 ),
253 'supportText' => esc_html__(
254 'Learn more about these suggestions.',
255 'jetpack'
256 ),
257 'supportLink' => Redirect::get_url( 'plugin-hint-learn-support' ),
258 'hideText' => esc_html__( 'Hide this suggestion', 'jetpack' ),
259 )
260 );
261
262 wp_enqueue_style( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
263 }
264
265 /**
266 * Get the plugin repo's data for Jetpack to populate the fields with.
267 *
268 * @return array|mixed|object|WP_Error
269 */
270 public static function get_jetpack_plugin_data() {
271 $data = get_transient( 'jetpack_plugin_data' );
272
273 if ( false === $data || is_wp_error( $data ) ) {
274 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
275 $data = plugins_api(
276 'plugin_information',
277 array(
278 'slug' => 'jetpack',
279 'is_ssl' => is_ssl(),
280 'fields' => array(
281 'banners' => true,
282 'reviews' => true,
283 'active_installs' => true,
284 'versions' => false,
285 'sections' => false,
286 ),
287 )
288 );
289 set_transient( 'jetpack_plugin_data', $data, DAY_IN_SECONDS );
290 }
291
292 return $data;
293 }
294
295 /**
296 * Create a list with additional features for those we don't have a module, like Akismet.
297 *
298 * @since 7.1.0
299 *
300 * @return array List of features.
301 */
302 public function get_extra_features() {
303 return array(
304 'akismet' => array(
305 'name' => 'Akismet',
306 'search_terms' => 'akismet, anti-spam, antispam, comments, spam, spam protection, form spam, captcha, no captcha, nocaptcha, recaptcha, phising, google',
307 'short_description' => esc_html__( 'Keep your visitors and search engines happy by stopping comment and contact form spam with Akismet.', 'jetpack' ),
308 'requires_connection' => true,
309 'module' => 'akismet',
310 'sort' => '16',
311 'learn_more_button' => Redirect::get_url( 'plugin-hint-upgrade-akismet' ),
312 'configure_url' => admin_url( 'admin.php?page=akismet-key-config' ),
313 ),
314 );
315 }
316
317 /**
318 * Intercept the plugins API response and add in an appropriate card for Jetpack
319 *
320 * @param object $result Plugin search results.
321 * @param string $action unused.
322 * @param object $args Search args.
323 */
324 public function inject_jetpack_module_suggestion( $result, $action, $args ) {
325 /*
326 * Bail if something else hooks into the Plugins' API response
327 * and does not return results.
328 */
329 if ( empty( $result->plugins ) || is_wp_error( $result ) ) {
330 return $result;
331 }
332
333 // Looks like a search query; it's matching time.
334 if ( ! empty( $args->search ) ) {
335 require_once JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php';
336 $tracking = new Tracking();
337 $jetpack_modules_list = array_intersect_key(
338 array_merge( $this->get_extra_features(), Jetpack_Admin::init()->get_modules() ),
339 array_flip(
340 array(
341 'contact-form',
342 'lazy-images',
343 'monitor',
344 'photon',
345 'photon-cdn',
346 'protect',
347 'publicize',
348 'related-posts',
349 'sharedaddy',
350 'akismet',
351 'vaultpress',
352 'videopress',
353 'search',
354 )
355 )
356 );
357 uasort( $jetpack_modules_list, array( $this, 'by_sorting_option' ) );
358
359 // Record event when user searches for a term over 3 chars (less than 3 is not very useful).
360 if ( strlen( $args->search ) >= 3 ) {
361 $tracking->record_user_event( 'wpa_plugin_search_term', array( 'search_term' => $args->search ) );
362 }
363
364 // Lowercase, trim, remove punctuation/special chars, decode url, remove 'jetpack'.
365 $normalized_term = $this->sanitize_search_term( $args->search );
366
367 $matching_module = null;
368
369 // Try to match a passed search term with module's search terms.
370 foreach ( $jetpack_modules_list as $module_slug => $module_opts ) {
371 /*
372 * Does the site's current plan support the feature?
373 * We don't use Jetpack_Plan::supports() here because
374 * that check always returns Akismet as supported,
375 * since Akismet has a free version.
376 */
377 $current_plan = Jetpack_Plan::get();
378 $is_supported_by_plan = in_array( $module_slug, $current_plan['supports'], true );
379
380 if (
381 false !== stripos( $module_opts['search_terms'] . ', ' . $module_opts['name'], $normalized_term )
382 && $is_supported_by_plan
383 ) {
384 $matching_module = $module_slug;
385 break;
386 }
387 }
388
389 if ( isset( $matching_module ) && $this->should_display_hint( $matching_module ) ) {
390 // Record event when a matching feature is found.
391 $tracking->record_user_event( 'wpa_plugin_search_match_found', array( 'feature' => $matching_module ) );
392
393 $inject = (array) self::get_jetpack_plugin_data();
394 $image_url = plugins_url( 'modules/plugin-search/psh', JETPACK__PLUGIN_FILE );
395 $overrides = array(
396 'plugin-search' => true, // Helps to determine if that an injected card.
397 'name' => sprintf( // Supplement name/description so that they clearly indicate this was added.
398 /* translators: Jetpack module name */
399 esc_html_x( 'Jetpack: %s', 'Jetpack: Module Name', 'jetpack' ),
400 $jetpack_modules_list[ $matching_module ]['name']
401 ),
402 'short_description' => $jetpack_modules_list[ $matching_module ]['short_description'],
403 'requires_connection' => (bool) $jetpack_modules_list[ $matching_module ]['requires_connection'],
404 'slug' => self::$slug,
405 'version' => JETPACK__VERSION,
406 'icons' => array(
407 '1x' => "$image_url-128.png",
408 '2x' => "$image_url-256.png",
409 'svg' => "$image_url.svg",
410 ),
411 );
412
413 // Splice in the base module data.
414 $inject = array_merge( $inject, $jetpack_modules_list[ $matching_module ], $overrides );
415
416 // Add it to the top of the list.
417 $result->plugins = array_filter( $result->plugins, array( $this, 'filter_cards' ) );
418 array_unshift( $result->plugins, $inject );
419 }
420 }
421 return $result;
422 }
423
424 /**
425 * Remove cards for Jetpack plugins since we don't want duplicates.
426 *
427 * @since 7.1.0
428 * @since 7.2.0 Only remove Jetpack.
429 * @since 7.4.0 Simplify for WordPress 5.1+.
430 *
431 * @param array|object $plugin WordPress search result card.
432 *
433 * @return bool
434 */
435 public function filter_cards( $plugin ) {
436 /*
437 * $plugin is normally an array.
438 * However, since the response data can be filtered,
439 * we cannot fully trust its format.
440 * Let's handle both arrays and objects, and bail if it's neither.
441 */
442 if ( is_array( $plugin ) && ! empty( $plugin['slug'] ) ) {
443 $slug = $plugin['slug'];
444 } elseif ( is_object( $plugin ) && ! empty( $plugin->slug ) ) {
445 $slug = $plugin->slug;
446 } else {
447 return false;
448 }
449
450 return ! in_array( $slug, array( 'jetpack' ), true );
451 }
452
453 /**
454 * Take a raw search query and return something a bit more standardized and
455 * easy to work with.
456 *
457 * @param string $term The raw search term.
458 * @return string A simplified/sanitized version.
459 */
460 private function sanitize_search_term( $term ) {
461 $term = strtolower( urldecode( $term ) );
462
463 // remove non-alpha/space chars.
464 $term = preg_replace( '/[^a-z ]/', '', $term );
465
466 // remove strings that don't help matches.
467 $term = trim( str_replace( array( 'jetpack', 'jp', 'free', 'wordpress' ), '', $term ) );
468
469 return $term;
470 }
471
472 /**
473 * Callback function to sort the array of modules by the sort option.
474 *
475 * @param array $m1 Array 1 to sort.
476 * @param array $m2 Array 2 to sort.
477 */
478 private function by_sorting_option( $m1, $m2 ) {
479 return $m1['sort'] - $m2['sort'];
480 }
481
482 /**
483 * Modify the URL to the feature settings, for example Publicize.
484 * Sharing is included here because while we still have a page in WP Admin,
485 * we prefer to send users to Calypso.
486 *
487 * @param string $feature Feature.
488 * @param string $configure_url URL to configure feature.
489 *
490 * @return string
491 * @since 7.1.0
492 */
493 private function get_configure_url( $feature, $configure_url ) {
494 switch ( $feature ) {
495 case 'sharing':
496 case 'publicize':
497 $configure_url = Redirect::get_url( 'calypso-marketing-connections' );
498 break;
499 case 'seo-tools':
500 $configure_url = Redirect::get_url(
501 'calypso-marketing-traffic',
502 array(
503 'anchor' => 'seo',
504 )
505 );
506 break;
507 case 'google-analytics':
508 $configure_url = Redirect::get_url(
509 'calypso-marketing-traffic',
510 array(
511 'anchor' => 'analytics',
512 )
513 );
514 break;
515 case 'wordads':
516 $configure_url = Redirect::get_url( 'wpcom-ads-settings' );
517 break;
518 }
519 return $configure_url;
520 }
521
522 /**
523 * Put some more appropriate links on our custom result cards.
524 *
525 * @param array $links Related links.
526 * @param array $plugin Plugin result information.
527 */
528 public function insert_module_related_links( $links, $plugin ) {
529 if ( self::$slug !== $plugin['slug'] ) {
530 return $links;
531 }
532
533 // By the time this filter is applied, self_admin_url was already applied and we don't need it anymore.
534 remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
535
536 $links = array();
537
538 if ( 'akismet' === $plugin['module'] || 'vaultpress' === $plugin['module'] ) {
539 $links['jp_get_started'] = '<a
540 id="plugin-select-settings"
541 class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
542 href="' . esc_url( Redirect::get_url( 'plugin-hint-learn-' . $plugin['module'] ) ) . '"
543 data-module="' . esc_attr( $plugin['module'] ) . '"
544 data-track="get_started"
545 >' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
546 // Jetpack installed, active, feature not enabled; prompt to enable.
547 } elseif (
548 current_user_can( 'jetpack_activate_modules' ) &&
549 ! Jetpack::is_module_active( $plugin['module'] ) &&
550 Jetpack_Plan::supports( $plugin['module'] )
551 ) {
552 $links[] = '<button
553 id="plugin-select-activate"
554 class="jetpack-plugin-search__primary button"
555 data-module="' . esc_attr( $plugin['module'] ) . '"
556 data-configure-url="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
557 > ' . esc_html__( 'Enable', 'jetpack' ) . '</button>';
558
559 // Jetpack installed, active, feature enabled; link to settings.
560 } elseif (
561 ! empty( $plugin['configure_url'] ) &&
562 current_user_can( 'jetpack_configure_modules' ) &&
563 Jetpack::is_module_active( $plugin['module'] ) &&
564 /** This filter is documented in class.jetpack-admin.php */
565 apply_filters( 'jetpack_module_configurable_' . $plugin['module'], false )
566 ) {
567 $links[] = '<a
568 id="plugin-select-settings"
569 class="jetpack-plugin-search__primary button jetpack-plugin-search__configure"
570 href="' . esc_url( $this->get_configure_url( $plugin['module'], $plugin['configure_url'] ) ) . '"
571 data-module="' . esc_attr( $plugin['module'] ) . '"
572 data-track="configure"
573 >' . esc_html__( 'Configure', 'jetpack' ) . '</a>';
574 // Module is active, doesn't have options to configure.
575 } elseif ( Jetpack::is_module_active( $plugin['module'] ) ) {
576 $links['jp_get_started'] = '<a
577 id="plugin-select-settings"
578 class="jetpack-plugin-search__primary jetpack-plugin-search__get-started button"
579 href="' . esc_url( Redirect::get_url( 'plugin-hint-learn-' . $plugin['module'] ) ) . '"
580 data-module="' . esc_attr( $plugin['module'] ) . '"
581 data-track="get_started"
582 >' . esc_html__( 'Get started', 'jetpack' ) . '</a>';
583 }
584
585 // Add link pointing to a relevant doc page in jetpack.com only if the Get started button isn't displayed.
586 if ( ! empty( $plugin['learn_more_button'] ) && ! isset( $links['jp_get_started'] ) ) {
587 $links[] = '<a
588 class="jetpack-plugin-search__learn-more"
589 href="' . esc_url( $plugin['learn_more_button'] ) . '"
590 target="_blank"
591 data-module="' . esc_attr( $plugin['module'] ) . '"
592 data-track="learn_more"
593 >' . esc_html__( 'Learn more', 'jetpack' ) . '</a>';
594 }
595
596 // Dismiss link.
597 $links[] = '<a
598 class="jetpack-plugin-search__dismiss"
599 data-module="' . esc_attr( $plugin['module'] ) . '"
600 >' . esc_html__( 'Hide this suggestion', 'jetpack' ) . '</a>';
601
602 return $links;
603 }
604
605 }
606
607 /**
608 * Master control that checks if Plugin search hints is active.
609 *
610 * @since 7.1.1
611 *
612 * @return bool True if PSH is active.
613 */
614 function jetpack_is_psh_active() {
615 /**
616 * Disables the Plugin Search Hints feature found when searching the plugins page.
617 *
618 * @since 8.7.0
619 *
620 * @param bool Set false to disable the feature.
621 */
622 return apply_filters( 'jetpack_psh_active', true );
623 }
624