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

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