PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.3
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmPluginSearch.php

FrmPluginSearch.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.3, at classes/models/FrmPluginSearch.php

376 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmPluginSearch {
7
8 /**
9 * PSH slug name.
10 *
11 * @var string
12 */
13 public static $slug = 'frm-plugin-search';
14
15 /**
16 * Option name that holds dismissed suggestions.
17 *
18 * @var string
19 */
20 protected static $dismissed_opt = 'frm_dismissed_hints';
21
22 public function __construct() {
23 add_action( 'current_screen', array( $this, 'start' ) );
24 }
25
26 /**
27 * Add actions and filters only if this is the plugin installation screen and it's the first page.
28 *
29 * @param object $screen WP Screen object.
30 *
31 * @since 4.12
32 *
33 * @return void
34 */
35 public function start( $screen ) {
36 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
37 if ( 'plugin-install' === $screen->base && ( ! isset( $_GET['paged'] ) || 1 === intval( $_GET['paged'] ) ) ) {
38 add_filter( 'plugins_api_result', array( $this, 'inject_suggestion' ), 10, 3 );
39 add_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
40 add_filter( 'plugin_install_action_links', array( $this, 'insert_related_links' ), 10, 2 );
41 add_action( 'admin_enqueue_scripts', array( $this, 'load_plugins_search_script' ) );
42 $this->maybe_dismiss();
43 }
44 }
45
46 /**
47 * Intercept the plugins API response and add in an appropriate card.
48 *
49 * @param object $result Plugin search results.
50 * @param string $action unused.
51 * @param object $args Search args.
52 *
53 * @return object
54 */
55 public function inject_suggestion( $result, $action, $args ) {
56 // Looks like a search query; it's matching time.
57 if ( empty( $args->search ) ) {
58 return $result;
59 }
60
61 $addon_list = $this->get_addons();
62
63 // Lowercase, trim, remove punctuation/special chars, decode url, remove 'formidable'.
64 $normalized_term = $this->search_to_array( $args->search );
65 if ( empty( $normalized_term ) ) {
66 // Don't add anything extra.
67 return $result;
68 }
69
70 $matching_addon = $this->matching_addon( $addon_list, $normalized_term );
71
72 if ( empty( $matching_addon ) || ! $this->should_display_hint( $matching_addon ) ) {
73 return $result;
74 }
75
76 $inject = (array) $this->get_plugin_data();
77 $overrides = array(
78 'plugin-search' => true, // Helps to determine if that an injected card.
79 'name' => sprintf(
80 /* translators: Formidable addon name */
81 esc_html_x( 'Formidable %s', 'Formidable Addon Name', 'formidable' ),
82 $addon_list[ $matching_addon ]['name']
83 ),
84 'addon' => $addon_list[ $matching_addon ]['slug'],
85 'short_description' => $addon_list[ $matching_addon ]['excerpt'],
86 'slug' => self::$slug,
87 'version' => $addon_list[ $matching_addon ]['version'],
88 );
89
90 if ( ! empty( $addon_list[ $matching_addon ]['external'] ) ) {
91 unset( $overrides['name'] );
92 }
93
94 // Splice in the base addon data.
95 $inject = array_merge( $inject, $addon_list[ $matching_addon ], $overrides );
96
97 // Add it to the top of the list.
98 array_unshift( $result->plugins, $inject );
99
100 return $result;
101 }
102
103 /**
104 * Search for any addons that match the searched terms.
105 *
106 * @since 4.12
107 *
108 * @param array $addon_list
109 * @param array $normalized_term
110 *
111 * @return int|null
112 */
113 private function matching_addon( $addon_list, $normalized_term ) {
114 $matching_addon = null;
115
116 // Try to match a passed search term with addon's search terms.
117 foreach ( $addon_list as $addon_id => $addon_opts ) {
118 if ( ! is_array( $addon_opts ) || empty( $addon_opts['excerpt'] ) ) {
119 continue;
120 }
121
122 if ( ! isset( $addon_opts['search_terms'] ) ) {
123 $addon_opts['search_terms'] = '';
124 }
125
126 $addon_terms = $this->search_to_array( $addon_opts['search_terms'] . ' ' . $addon_opts['name'] );
127
128 $matches = ! empty( array_intersect( $addon_terms, $normalized_term ) );
129
130 if ( $matches ) {
131 $matching_addon = $addon_id;
132 break;
133 }
134 }
135
136 return $matching_addon;
137 }
138
139 /**
140 * @since 4.12
141 *
142 * @return array
143 */
144 private function get_addons() {
145 $api = new FrmFormApi();
146 $addons = $api->get_api_info();
147 return apply_filters( 'frm_plugin_search', $addons );
148 }
149
150 /**
151 * Get the plugin repo's data to populate the fields with.
152 *
153 * @return array|mixed|object|WP_Error
154 */
155 private function get_plugin_data() {
156 $data = get_transient( 'formidable_plugin_data' );
157
158 if ( false !== $data && ! is_wp_error( $data ) ) {
159 return $data;
160 }
161
162 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
163 $data = plugins_api(
164 'plugin_information',
165 array(
166 'slug' => 'formidable',
167 'is_ssl' => is_ssl(),
168 'fields' => array(
169 'banners' => true,
170 'reviews' => true,
171 'active_installs' => true,
172 'versions' => false,
173 'sections' => false,
174 ),
175 )
176 );
177 set_transient( 'formidable_plugin_data', $data, DAY_IN_SECONDS );
178
179 return $data;
180 }
181
182 /**
183 * Modify URL used to fetch to plugin information so it pulls Formidable plugin page.
184 *
185 * @param string $url URL to load in dialog pulling the plugin page from wporg.
186 *
187 * @since 4.12
188 *
189 * @return string The URL with 'formidable' instead of 'frm-plugin-search'.
190 */
191 public function plugin_details( $url ) {
192 return false !== stripos( $url, 'tab=plugin-information&amp;plugin=' . self::$slug )
193 ? 'plugin-install.php?tab=plugin-information&amp;plugin=formidable&amp;TB_iframe=true&amp;width=600&amp;height=550'
194 : $url;
195 }
196
197 /**
198 * @since 4.12
199 *
200 * @return void
201 */
202 private function maybe_dismiss() {
203 $addon = FrmAppHelper::get_param( 'frm-dismiss', '', 'get', 'absint' );
204 if ( ! empty( $addon ) ) {
205 $this->add_to_dismissed_hints( $addon );
206 }
207 }
208
209 /**
210 * Returns a list of previously dismissed hints.
211 *
212 * @since 4.12
213 *
214 * @return array List of dismissed hints.
215 */
216 protected function get_dismissed_hints() {
217 $dismissed_hints = get_option( self::$dismissed_opt );
218 return ! empty( $dismissed_hints ) && is_array( $dismissed_hints ) ? $dismissed_hints : array();
219 }
220
221 /**
222 * Save the hint in the list of dismissed hints.
223 *
224 * @since 4.12
225 *
226 * @param string $hint The hint id, which is a Formidable addon slug.
227 *
228 * @return bool Whether the card was added to the list and hence dismissed.
229 */
230 protected function add_to_dismissed_hints( $hint ) {
231 $hints = array_merge( $this->get_dismissed_hints(), array( $hint ) );
232 return update_option( self::$dismissed_opt, $hints, 'no' );
233 }
234
235 /**
236 * Checks that the addon slug passed should be displayed.
237 *
238 * A feature hint will be displayed if it has not been dismissed before or if 2 or fewer other hints have been dismissed.
239 *
240 * @since 7.2.1
241 *
242 * @param string $hint The hint id, which is a Formidable addon slug.
243 *
244 * @return bool True if $hint should be displayed.
245 */
246 protected function should_display_hint( $hint ) {
247 $dismissed_hints = $this->get_dismissed_hints();
248
249 // If more than 3 hints have been dismissed, then show no more.
250 if ( 3 < count( $dismissed_hints ) ) {
251 return false;
252 }
253
254 return ! in_array( $hint, $dismissed_hints, true );
255 }
256
257 /**
258 * Take a raw search query and return something a bit more standardized and
259 * easy to work with.
260 *
261 * @param string $term The raw search term.
262 * @return string A simplified/sanitized version.
263 */
264 private function sanitize_search_term( $term ) {
265 $term = strtolower( urldecode( $term ) );
266
267 // remove non-alpha/space chars.
268 $term = preg_replace( '/[^a-z ]/', '', $term );
269
270 // remove strings that don't help matches.
271 $term = trim( str_replace( array( 'formidable', 'free', 'wordpress', 'wp ', 'plugin' ), '', $term ) );
272
273 return $term;
274 }
275
276 /**
277 * @since 4.12
278 *
279 * @param string $terms
280 * @return array
281 */
282 private function search_to_array( $terms ) {
283 $terms = $this->sanitize_search_term( $terms );
284 return array_filter( explode( ' ', $terms ) );
285 }
286
287 /**
288 * Put some more appropriate links on our custom result cards.
289 *
290 * @param array $links Related links.
291 * @param array $plugin Plugin result information.
292 *
293 * @return array
294 */
295 public function insert_related_links( $links, $plugin ) {
296 if ( self::$slug !== $plugin['slug'] ) {
297 return $links;
298 }
299
300 // By the time this filter is applied, self_admin_url was already applied and we don't need it anymore.
301 remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) );
302
303 $links = array();
304 $is_installed = $this->is_installed( $plugin['plugin'] );
305 $is_active = is_plugin_active( $plugin['plugin'] );
306
307 // Plugin installed, active, feature not enabled; prompt to enable.
308 if ( ! $is_active && $is_installed ) {
309 if ( current_user_can( 'activate_plugins' ) ) {
310 $activate_url = add_query_arg(
311 array(
312 'action' => 'activate',
313 '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin['plugin'] ),
314 'plugin' => $plugin['plugin'],
315 ),
316 admin_url( 'plugins.php' )
317 );
318 $links['frm_get_started'] = '<a href="' . esc_url( $activate_url ) . '" class="button activate-now" aria-label="Activate ' . esc_attr( $plugin['name'] ) . '">' . __( 'Activate', 'formidable' ) . '</a>';
319 }
320 } elseif ( ! $is_active && isset( $plugin['url'] ) ) {
321 // Go to the add-ons page to install.
322 $links[] = '<a
323 class="button-secondary"
324 href="' . esc_url( admin_url( 'admin.php?page=formidable-addons' ) ) . '"
325 >' . __( 'Install Now', 'formidable' ) . '</a>';
326
327 } elseif ( ! empty( $plugin['link'] ) ) {
328 // Add link pointing to a relevant doc page in formidable.com.
329 $links[] = '<a
330 class="button-primary frm-plugin-search__learn-more"
331 href="' . esc_url( FrmAppHelper::admin_upgrade_link( 'plugin-learn-more', $plugin['link'] ) ) . '"
332 target="_blank"
333 data-addon="' . esc_attr( $plugin['addon'] ) . '"
334 >' . esc_html__( 'Learn more', 'formidable' ) . '</a>';
335 }
336
337 // Dismiss link.
338 $dismiss = add_query_arg( array( 'frm-dismiss' => $plugin['id'] ) );
339 $links[] = '<a
340 href="' . $dismiss . '"
341 class="frm-plugin-search__dismiss"
342 data-addon="' . esc_attr( $plugin['addon'] ) . '"
343 >' . esc_html__( 'Hide this suggestion', 'formidable' ) . '</a>';
344
345 return $links;
346 }
347
348 /**
349 * @param string $plugin
350 * @return bool
351 */
352 protected function is_installed( $plugin ) {
353 $all_plugins = FrmAppHelper::get_plugins();
354 return isset( $all_plugins[ $plugin ] );
355 }
356
357 /**
358 * Load the search scripts and CSS for PSH.
359 *
360 * @return void
361 */
362 public function load_plugins_search_script() {
363 wp_enqueue_script( self::$slug, FrmAppHelper::plugin_url() . '/js/plugin-search.js', array(), FrmAppHelper::plugin_version(), true );
364 wp_localize_script(
365 self::$slug,
366 'frmPlugSearch',
367 array(
368 'legend' => esc_html__(
369 'This suggestion was made by Formidable Forms, the form builder and application plugin already installed on your site.',
370 'formidable'
371 ),
372 )
373 );
374 }
375 }
376