PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26
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.26, at classes/models/FrmPluginSearch.php

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