PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.5.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.5.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 / helpers / FrmAppHelper.php

FrmAppHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.5.3, at classes/helpers/FrmAppHelper.php

4,004 lines 111.9 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 FrmAppHelper {
7 public static $db_version = 98; // Version of the database we are moving to.
8 public static $pro_db_version = 37; //deprecated
9 public static $font_version = 7;
10
11 /**
12 * @var bool $added_gmt_offset_filter
13 */
14 private static $added_gmt_offset_filter = false;
15
16 /**
17 * @since 2.0
18 *
19 * @var string
20 */
21 public static $plug_version = '6.5.3';
22
23 /**
24 * @var bool
25 */
26 private static $included_svg = false;
27
28 /**
29 * @since 1.07.02
30 *
31 * @param none
32 *
33 * @return string The version of this plugin
34 */
35 public static function plugin_version() {
36 return self::$plug_version;
37 }
38
39 public static function plugin_folder() {
40 return basename( self::plugin_path() );
41 }
42
43 public static function plugin_path() {
44 return dirname( dirname( dirname( __FILE__ ) ) );
45 }
46
47 public static function plugin_url() {
48 // Prevously FRM_URL constant.
49 return plugins_url( '', self::plugin_path() . '/formidable.php' );
50 }
51
52 public static function relative_plugin_url() {
53 return str_replace( array( 'https:', 'http:' ), '', self::plugin_url() );
54 }
55
56 /**
57 * @return string Site URL
58 */
59 public static function site_url() {
60 return site_url();
61 }
62
63 /**
64 * Get the name of this site
65 * Used for [sitename] shortcode
66 *
67 * @since 2.0
68 * @return string
69 */
70 public static function site_name() {
71 return get_option( 'blogname' );
72 }
73
74 public static function make_affiliate_url( $url ) {
75 $affiliate_id = self::get_affiliate();
76 if ( ! empty( $affiliate_id ) ) {
77 $url = str_replace( array( 'http://', 'https://' ), '', $url );
78 $url = 'http://www.shareasale.com/r.cfm?u=' . absint( $affiliate_id ) . '&b=841990&m=64739&afftrack=plugin&urllink=' . urlencode( $url );
79 }
80
81 return $url;
82 }
83
84 public static function get_affiliate() {
85 return absint( apply_filters( 'frm_affiliate_id', 0 ) );
86 }
87
88 /**
89 * @since 3.04.02
90 * @param array|string $args
91 * @param string $page
92 */
93 public static function admin_upgrade_link( $args, $page = '' ) {
94 if ( empty( $page ) ) {
95 $page = 'https://formidableforms.com/lite-upgrade/';
96 } else {
97 $page = str_replace( 'https://formidableforms.com/', '', $page );
98 $page = 'https://formidableforms.com/' . $page;
99 }
100
101 $anchor = '';
102 if ( is_array( $args ) ) {
103 $medium = $args['medium'];
104 if ( isset( $args['content'] ) ) {
105 $content = $args['content'];
106 }
107 if ( isset( $args['anchor'] ) ) {
108 $anchor = '#' . $args['anchor'];
109 }
110 } else {
111 $medium = $args;
112 }
113
114 $query_args = array(
115 'utm_source' => 'WordPress',
116 'utm_medium' => $medium,
117 'utm_campaign' => 'liteplugin',
118 );
119
120 if ( isset( $content ) ) {
121 $query_args['utm_content'] = $content;
122 }
123
124 if ( is_array( $args ) && isset( $args['param'] ) ) {
125 $query_args['f'] = $args['param'];
126 }
127
128 if ( is_array( $args ) && ! empty( $args['plan'] ) ) {
129 $query_args['plan'] = $args['plan'];
130 }
131
132 $link = add_query_arg( $query_args, $page ) . $anchor;
133 return self::make_affiliate_url( $link );
134 }
135
136 /**
137 * Get the Formidable settings
138 *
139 * @since 2.0
140 *
141 * @param array $args - May include the form id when values need translation.
142 * @return FrmSettings $frm_setings
143 */
144 public static function get_settings( $args = array() ) {
145 global $frm_settings;
146 if ( empty( $frm_settings ) ) {
147 $frm_settings = new FrmSettings( $args );
148 } elseif ( isset( $args['current_form'] ) ) {
149 // If the global has already been set, allow strings to be filtered.
150 $frm_settings->maybe_filter_for_form( $args );
151 }
152
153 return $frm_settings;
154 }
155
156 public static function get_menu_name() {
157 $frm_settings = self::get_settings();
158
159 return FrmAddonsController::is_license_expired() ? 'Formidable' : $frm_settings->menu;
160 }
161
162 /**
163 * Determine if the current branding is set to 'formidable'.
164 *
165 * Checks the menu title, retrieved through get_menu_name,
166 * and verifies if it matches the 'formidable' branding.
167 *
168 * @since 6.4.2
169 *
170 * @return bool True if the menu title is 'formidable', false otherwise.
171 */
172 public static function is_formidable_branding() {
173 $menu_title = self::get_menu_name();
174
175 return 'formidable' === strtolower( trim( $menu_title ) );
176 }
177
178 /**
179 * @since 3.05
180 */
181 public static function svg_logo( $atts = array() ) {
182 $defaults = array(
183 'height' => 18,
184 'width' => 18,
185 'fill' => '#4d4d4d',
186 'orange' => '#f05a24',
187 );
188 $atts = array_merge( $defaults, $atts );
189
190 return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 599.68 601.37" width="' . esc_attr( $atts['width'] ) . '" height="' . esc_attr( $atts['height'] ) . '">
191 <path fill="' . esc_attr( $atts['orange'] ) . '" d="M289.6 384h140v76h-140z"/>
192 <path fill="' . esc_attr( $atts['fill'] ) . '" d="M400.2 147h-200c-17 0-30.6 12.2-30.6 29.3V218h260v-71zM397.9 264H169.6v196h75V340H398a32.2 32.2 0 0 0 30.1-21.4 24.3 24.3 0 0 0 1.7-8.7V264zM299.8 601.4A300.3 300.3 0 0 1 0 300.7a299.8 299.8 0 1 1 511.9 212.6 297.4 297.4 0 0 1-212 88zm0-563A262 262 0 0 0 38.3 300.7a261.6 261.6 0 1 0 446.5-185.5 259.5 259.5 0 0 0-185-76.8z"/>
193 </svg>';
194 }
195
196 /**
197 * @since 4.0
198 */
199 public static function show_logo( $atts = array() ) {
200 echo self::kses( self::svg_logo( $atts ), 'all' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
201 }
202
203 /**
204 * @since 4.03.02
205 */
206 public static function show_header_logo() {
207 $icon = self::svg_logo(
208 array(
209 'height' => 35,
210 'width' => 35,
211 )
212 );
213
214 $new_icon = apply_filters( 'frm_icon', $icon, true );
215 if ( $new_icon !== $icon ) {
216 if ( strpos( $new_icon, '<svg' ) === 0 ) {
217 $icon = str_replace( 'viewBox="0 0 20', 'width="30" height="35" style="color:#929699" viewBox="0 0 20', $new_icon );
218 } else {
219 // Show nothing if it isn't an SVG.
220 $icon = '<div style="height:39px"></div>';
221 }
222 }
223 echo self::kses( $icon, 'all' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
224 }
225
226 /**
227 * @since 2.02.04
228 */
229 public static function ips_saved() {
230 $frm_settings = self::get_settings();
231
232 return ! $frm_settings->no_ips;
233 }
234
235 public static function pro_is_installed() {
236 return apply_filters( 'frm_pro_installed', false );
237 }
238
239 /**
240 * @since 4.06.02
241 */
242 public static function pro_is_connected() {
243 global $frm_vars;
244 return self::pro_is_installed() && $frm_vars['pro_is_authorized'];
245 }
246
247 /**
248 * @since 4.06
249 */
250 public static function is_form_builder_page() {
251 $action = self::simple_get( 'frm_action', 'sanitize_title' );
252 return self::is_admin_page( 'formidable' ) && ( $action === 'edit' || $action === 'settings' || $action === 'duplicate' );
253 }
254
255 public static function is_formidable_admin() {
256 $page = self::simple_get( 'page', 'sanitize_title' );
257 $is_formidable = strpos( $page, 'formidable' ) !== false;
258 if ( empty( $page ) ) {
259 $is_formidable = self::is_view_builder_page();
260 }
261
262 return $is_formidable;
263 }
264
265 /**
266 * Check for certain page in Formidable settings
267 *
268 * @since 2.0
269 *
270 * @param string $page The name of the page to check
271 *
272 * @return boolean
273 */
274 public static function is_admin_page( $page = 'formidable' ) {
275 global $pagenow;
276 $get_page = self::simple_get( 'page', 'sanitize_title' );
277 if ( $pagenow ) {
278 // allow this to be true during ajax load i.e. ajax form builder loading
279 $is_page = ( $pagenow === 'admin.php' || $pagenow === 'admin-ajax.php' ) && $get_page === $page;
280 if ( $is_page ) {
281 return true;
282 }
283 }
284
285 return is_admin() && $get_page === $page;
286 }
287
288 /**
289 * If the current page is for editing or creating a view.
290 * Returns false for the views listing page.
291 *
292 * @since 4.0
293 */
294 public static function is_view_builder_page() {
295 global $pagenow;
296
297 if ( $pagenow !== 'post.php' && $pagenow !== 'post-new.php' && $pagenow !== 'edit.php' ) {
298 return false;
299 }
300
301 $post_type = self::simple_get( 'post_type', 'sanitize_title' );
302
303 if ( empty( $post_type ) ) {
304 $post_id = self::simple_get( 'post', 'absint' );
305 $post = get_post( $post_id );
306 $post_type = $post ? $post->post_type : '';
307 }
308
309 return $post_type === 'frm_display';
310 }
311
312 /**
313 * Check for the form preview page
314 *
315 * @since 2.0
316 *
317 * @param None
318 *
319 * @return boolean
320 */
321 public static function is_preview_page() {
322 global $pagenow;
323 $action = self::simple_get( 'action', 'sanitize_title' );
324
325 return $pagenow && $pagenow == 'admin-ajax.php' && $action == 'frm_forms_preview';
326 }
327
328 /**
329 * Check for ajax except the form preview page
330 *
331 * @since 2.0
332 *
333 * @param None
334 *
335 * @return boolean
336 */
337 public static function doing_ajax() {
338 return wp_doing_ajax() && ! self::is_preview_page();
339 }
340
341 public static function js_suffix() {
342 return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
343 }
344
345 /**
346 * @since 2.0.8
347 */
348 public static function prevent_caching() {
349 global $frm_vars;
350
351 return isset( $frm_vars['prevent_caching'] ) && $frm_vars['prevent_caching'];
352 }
353
354 /**
355 * Check if on an admin page
356 *
357 * @since 2.0
358 *
359 * @return boolean
360 */
361 public static function is_admin() {
362 $is_admin = is_admin() && ! wp_doing_ajax();
363
364 /**
365 * @since 6.0
366 * @param bool $is_admin
367 */
368 return apply_filters( 'frm_is_admin', $is_admin );
369 }
370
371 /**
372 * Check if value contains blank value or empty array
373 *
374 * @since 2.0
375 *
376 * @param mixed $value - value to check
377 * @param string
378 *
379 * @return boolean
380 */
381 public static function is_empty_value( $value, $empty = '' ) {
382 return ( is_array( $value ) && empty( $value ) ) || $value === $empty;
383 }
384
385 public static function is_not_empty_value( $value, $empty = '' ) {
386 return ! self::is_empty_value( $value, $empty );
387 }
388
389 /**
390 * Get any value from the $_SERVER
391 *
392 * @since 2.0
393 *
394 * @param string $value
395 *
396 * @return string
397 */
398 public static function get_server_value( $value ) {
399 return isset( $_SERVER[ $value ] ) ? wp_strip_all_tags( wp_unslash( $_SERVER[ $value ] ) ) : '';
400 }
401
402 /**
403 * Get the server OS
404 *
405 * @since 6.4.2
406 *
407 * @return string
408 */
409 public static function get_server_os() {
410 if ( function_exists( 'php_uname' ) ) {
411 return php_uname( 's' );
412 }
413
414 if ( ! defined( 'PHP_OS' ) ) {
415 return '';
416 }
417
418 // match the same response for Windows server as php_uname('s')
419 return in_array( PHP_OS, array( 'WIN32', 'WINNT', 'Windows_NT' ), true ) ? 'Windows NT' : PHP_OS;
420 }
421
422 /**
423 * Check for the IP address in several places (when custom headers are enabled).
424 * Used by [ip] shortcode.
425 *
426 * @return string The IP address of the current user
427 */
428 public static function get_ip_address() {
429 $ip_options = self::should_use_custom_header_ip() ? self::get_custom_header_keys_for_ip() : array( 'REMOTE_ADDR' );
430 $ip = '';
431
432 foreach ( $ip_options as $key ) {
433 if ( ! isset( $_SERVER[ $key ] ) ) {
434 continue;
435 }
436
437 $key = self::get_server_value( $key );
438 foreach ( explode( ',', $key ) as $ip ) {
439 $ip = trim( $ip ); // Just to be safe.
440
441 if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) !== false ) {
442 return sanitize_text_field( $ip );
443 }
444 }
445 }
446
447 return sanitize_text_field( $ip );
448 }
449
450 /**
451 * @since 6.1
452 *
453 * @return array
454 */
455 public static function get_custom_header_keys_for_ip() {
456 return array(
457 'HTTP_CLIENT_IP',
458 'HTTP_CF_CONNECTING_IP',
459 'HTTP_X_FORWARDED_FOR',
460 'HTTP_X_FORWARDED',
461 'HTTP_X_CLUSTER_CLIENT_IP',
462 'HTTP_X_REAL_IP',
463 'HTTP_FORWARDED_FOR',
464 'HTTP_FORWARDED',
465 'REMOTE_ADDR',
466 );
467 }
468
469 /**
470 * Check if we should check every HTTP header or just $_SERVER['REMOTE_ADDR'].
471 * The other HTTP headers can be spoofed so this isn't recommended.
472 * But in some cases (like reverse proxies), the IP may be empty if you use $_SERVER['REMOTE_ADDR'].
473 *
474 * @since 6.1
475 *
476 * @return bool
477 */
478 private static function should_use_custom_header_ip() {
479 $settings = self::get_settings();
480 $should_use_custom_header_ip = ! $settings->no_ips && $settings->custom_header_ip;
481
482 /**
483 * Filter whether to check spoofable HTTP headers.
484 * This uses the custom_header_ip setting, but it is hidden if the GDPR option is also on.
485 * As the IP is still checked for blacklist checks, someone with the GDPR option may still want to enable this when behind a reverse proxy.
486 *
487 * @since 6.1
488 *
489 * @param bool $should_use_custom_header_ip
490 */
491 return apply_filters( 'frm_use_custom_header_ip', $should_use_custom_header_ip );
492 }
493
494 public static function get_param( $param, $default = '', $src = 'get', $sanitize = '' ) {
495 if ( strpos( $param, '[' ) ) {
496 $params = explode( '[', $param );
497 $param = $params[0];
498 }
499
500 if ( $src === 'get' ) {
501 $value = isset( $_POST[ $param ] ) ? wp_unslash( $_POST[ $param ] ) : ( isset( $_GET[ $param ] ) ? wp_unslash( $_GET[ $param ] ) : $default ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
502 if ( ! isset( $_POST[ $param ] ) && isset( $_GET[ $param ] ) && ! is_array( $value ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
503 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
504 $value = htmlspecialchars_decode( wp_unslash( $_GET[ $param ] ) );
505 }
506 self::sanitize_value( $sanitize, $value );
507 } else {
508 $value = self::get_simple_request(
509 array(
510 'type' => $src,
511 'param' => $param,
512 'default' => $default,
513 'sanitize' => $sanitize,
514 )
515 );
516 }
517
518 if ( isset( $params ) && is_array( $value ) && ! empty( $value ) ) {
519 foreach ( $params as $k => $p ) {
520 if ( ! $k || ! is_array( $value ) ) {
521 continue;
522 }
523
524 $p = trim( $p, ']' );
525 $value = isset( $value[ $p ] ) ? $value[ $p ] : $default;
526 }
527 }
528
529 return $value;
530 }
531
532 public static function get_post_param( $param, $default = '', $sanitize = '', $serialized = false ) {
533 return self::get_simple_request(
534 array(
535 'type' => 'post',
536 'param' => $param,
537 'default' => $default,
538 'sanitize' => $sanitize,
539 'serialized' => $serialized,
540 )
541 );
542 }
543
544 /**
545 * @since 2.0
546 *
547 * @param string $param
548 * @param string $sanitize
549 * @param string $default
550 *
551 * @return string|array
552 */
553 public static function simple_get( $param, $sanitize = 'sanitize_text_field', $default = '' ) {
554 return self::get_simple_request(
555 array(
556 'type' => 'get',
557 'param' => $param,
558 'default' => $default,
559 'sanitize' => $sanitize,
560 )
561 );
562 }
563
564 /**
565 * Get a GET/POST/REQUEST value and sanitize it
566 *
567 * @since 2.0.6
568 *
569 * @param array $args
570 *
571 * @return string|array
572 */
573 public static function get_simple_request( $args ) {
574 $defaults = array(
575 'param' => '',
576 'default' => '',
577 'type' => 'get',
578 'sanitize' => 'sanitize_text_field',
579 'serialized' => false,
580 );
581 $args = wp_parse_args( $args, $defaults );
582
583 $value = $args['default'];
584 if ( $args['type'] === 'get' ) {
585 if ( $_GET && isset( $_GET[ $args['param'] ] ) ) {
586 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
587 $value = wp_unslash( $_GET[ $args['param'] ] );
588 }
589 } elseif ( $args['type'] === 'post' ) {
590 if ( isset( $_POST[ $args['param'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
591 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
592 $value = wp_unslash( $_POST[ $args['param'] ] );
593 if ( $args['serialized'] === true && is_serialized_string( $value ) && is_serialized( $value ) ) {
594 self::unserialize_or_decode( $value );
595 }
596 }
597 } else {
598 if ( isset( $_REQUEST[ $args['param'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
599 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
600 $value = wp_unslash( $_REQUEST[ $args['param'] ] );
601 }
602 }
603
604 self::sanitize_value( $args['sanitize'], $value );
605
606 return $value;
607 }
608
609 /**
610 * Preserve backslashes in a value, but make sure value doesn't get compounding slashes
611 *
612 * @since 2.0.8
613 *
614 * @param string $value
615 *
616 * @return string $value
617 */
618 public static function preserve_backslashes( $value ) {
619 // If backslashes have already been added, don't add them again
620 if ( strpos( $value, '\\\\' ) === false ) {
621 $value = addslashes( $value );
622 }
623
624 return $value;
625 }
626
627 public static function sanitize_value( $sanitize, &$value ) {
628 if ( ! empty( $sanitize ) ) {
629 if ( is_array( $value ) ) {
630 $temp_values = $value;
631 foreach ( $temp_values as $k => $v ) {
632 self::sanitize_value( $sanitize, $value[ $k ] );
633 }
634 } else {
635 $value = call_user_func( $sanitize, $value );
636 }
637 }
638 }
639
640 public static function sanitize_request( $sanitize_method, &$values ) {
641 $temp_values = $values;
642 foreach ( $temp_values as $k => $val ) {
643 if ( isset( $sanitize_method[ $k ] ) ) {
644 $values[ $k ] = call_user_func( $sanitize_method[ $k ], $val );
645 }
646 }
647 }
648
649 /**
650 * @since 4.0.04
651 */
652 public static function sanitize_with_html( &$value ) {
653 self::sanitize_value( 'wp_kses_post', $value );
654 self::decode_specialchars( $value );
655 }
656
657 /**
658 * Do wp_specialchars_decode to get back '&' that wp_kses_post might have turned to '&amp;'
659 * this MUST be done, else we'll be back to the '& entity' problem.
660 *
661 * @since 4.0.04
662 */
663 public static function decode_specialchars( &$value ) {
664 if ( is_array( $value ) ) {
665 $temp_values = $value;
666 foreach ( $temp_values as $k => $v ) {
667 self::decode_specialchars( $value[ $k ] );
668 }
669 } else {
670 self::decode_amp( $value );
671 }
672 }
673
674 /**
675 * The wp_specialchars_decode function changes too much.
676 * This will leave HTML as is, but still convert &.
677 * Adapted from wp_specialchars_decode().
678 *
679 * @since 4.03.01
680 *
681 * @param string $string The string to prep.
682 */
683 private static function decode_amp( &$string ) {
684 // Don't bother if there are no entities - saves a lot of processing
685 if ( empty( $string ) || strpos( $string, '&' ) === false ) {
686 return;
687 }
688
689 $translation = array(
690 '&quot;' => '"',
691 '&#034;' => '"',
692 '&#x22;' => '"',
693 '&lt; ' => '< ', // The space preserves the HTML.
694 '&#060; ' => '< ', // The space preserves the HTML.
695 '&gt;' => '>',
696 '&#062;' => '>',
697 '&amp;' => '&',
698 '&#038;' => '&',
699 '&#x26;' => '&',
700 );
701
702 $translation_preg = array(
703 '/&#0*34;/' => '&#034;',
704 '/&#x0*22;/i' => '&#x22;',
705 '/&#0*60;/' => '&#060;',
706 '/&#0*62;/' => '&#062;',
707 '/&#0*38;/' => '&#038;',
708 '/&#x0*26;/i' => '&#x26;',
709 );
710
711 // Remove zero padding on numeric entities
712 $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
713
714 // Replace characters according to translation table
715 $string = strtr( $string, $translation );
716 }
717
718 /**
719 * Sanitize the value, and allow some HTML
720 *
721 * @since 2.0
722 *
723 * @param string $value
724 * @param array|string $allowed 'all' for everything included as defaults
725 *
726 * @return string
727 */
728 public static function kses( $value, $allowed = array() ) {
729 $allowed_html = self::allowed_html( $allowed );
730
731 return wp_kses( $value, $allowed_html );
732 }
733
734 /**
735 * The regular kses function strips [button_action] from submit button HTML.
736 *
737 * @since 5.0.13
738 *
739 * @param string $html
740 * @return string
741 */
742 public static function kses_submit_button( $html ) {
743 $included_button_action = false !== strpos( $html, '[button_action]' );
744 $included_back_hook = false !== strpos( $html, '[back_hook]' );
745 $included_draft_hook = false !== strpos( $html, '[draft_hook]' );
746 add_filter( 'safe_style_css', 'FrmAppHelper::allow_visibility_style' );
747 add_filter( 'frm_striphtml_allowed_tags', 'FrmAppHelper::add_allowed_submit_button_tags' );
748 $html = self::kses( $html, 'all' );
749 remove_filter( 'safe_style_css', 'FrmAppHelper::allow_visibility_style' );
750 remove_filter( 'frm_striphtml_allowed_tags', 'FrmAppHelper::add_allowed_submit_button_tags' );
751 if ( $included_button_action ) {
752 if ( false !== strpos( $html, '<input type="submit"' ) ) {
753 $pattern = '/(<input type="submit")([^>]*)(\/>)/';
754 $html = preg_replace( $pattern, '$1$2[button_action] $3', $html, 1 );
755 } else {
756 $pattern = '/(<button)(.*)(class=")(.*)(frm_button_submit)(.*)(")(.*)([^>]+)(>)/';
757 $html = preg_replace( $pattern, '$1$2$3$4$5$6$7 [button_action]$8$9$10', $html, 1 );
758 }
759 }
760 if ( $included_back_hook ) {
761 $html = str_replace( 'class="frm_prev_page"', 'class="frm_prev_page" [back_hook]', $html );
762 }
763 if ( $included_draft_hook ) {
764 $html = str_replace( 'class="frm_save_draft"', 'class="frm_save_draft" [draft_hook]', $html );
765 }
766 return $html;
767 }
768
769 /**
770 * @since 5.0.13
771 *
772 * @param array $allowed_attr
773 * @return array
774 */
775 public static function allow_visibility_style( $allowed_attr ) {
776 $allowed_attr[] = 'visibility';
777 return $allowed_attr;
778 }
779
780 /**
781 * @since 5.0.13
782 *
783 * @param array $allowed_html
784 * @return array
785 */
786 public static function add_allowed_submit_button_tags( $allowed_html ) {
787 $allowed_html['input'] = array(
788 'type' => true,
789 'value' => true,
790 'formnovalidate' => true,
791 'name' => true,
792 'class' => true,
793 );
794 $allowed_html['button']['formnovalidate'] = true;
795 $allowed_html['button']['name'] = true;
796 $allowed_html['img']['style'] = true;
797 return $allowed_html;
798 }
799
800 /**
801 * @since 2.05.03
802 */
803 private static function allowed_html( $allowed ) {
804 $html = self::safe_html();
805 $allowed_html = array();
806 if ( $allowed === 'all' ) {
807 $allowed_html = $html;
808 } elseif ( ! empty( $allowed ) ) {
809 foreach ( (array) $allowed as $a ) {
810 $allowed_html[ $a ] = isset( $html[ $a ] ) ? $html[ $a ] : array();
811 }
812 }
813
814 return apply_filters( 'frm_striphtml_allowed_tags', $allowed_html );
815 }
816
817 /**
818 * @since 2.05.03
819 */
820 private static function safe_html() {
821 $allow_class = array(
822 'class' => true,
823 'id' => true,
824 );
825
826 return array(
827 'a' => array(
828 'class' => true,
829 'href' => true,
830 'id' => true,
831 'rel' => true,
832 'target' => true,
833 'title' => true,
834 'tabindex' => true,
835 ),
836 'abbr' => array(
837 'title' => true,
838 ),
839 'aside' => $allow_class,
840 'b' => array(),
841 'blockquote' => array(
842 'cite' => true,
843 ),
844 'br' => array(),
845 'cite' => array(
846 'title' => true,
847 ),
848 'code' => array(),
849 'defs' => array(),
850 'del' => array(
851 'datetime' => true,
852 'title' => true,
853 ),
854 'dd' => array(),
855 'div' => array(
856 'class' => true,
857 'id' => true,
858 'title' => true,
859 'style' => true,
860 'role' => true,
861 ),
862 'dl' => array(),
863 'dt' => array(),
864 'em' => array(),
865 'h1' => $allow_class,
866 'h2' => $allow_class,
867 'h3' => $allow_class,
868 'h4' => $allow_class,
869 'h5' => $allow_class,
870 'h6' => $allow_class,
871 'i' => array(
872 'class' => true,
873 'id' => true,
874 'icon' => true,
875 'style' => true,
876 ),
877 'img' => array(
878 'alt' => true,
879 'class' => true,
880 'height' => true,
881 'id' => true,
882 'src' => true,
883 'width' => true,
884 ),
885 'li' => $allow_class,
886 'ol' => $allow_class,
887 'p' => $allow_class,
888 'path' => array(
889 'd' => true,
890 'fill' => true,
891 ),
892 'pre' => array(),
893 'q' => array(
894 'cite' => true,
895 'title' => true,
896 ),
897 'rect' => array(
898 'class' => true,
899 'fill' => true,
900 'height' => true,
901 'width' => true,
902 'x' => true,
903 'y' => true,
904 'rx' => true,
905 'stroke' => true,
906 'stroke-opacity' => true,
907 'stroke-width' => true,
908 ),
909 'section' => $allow_class,
910 'span' => array(
911 'class' => true,
912 'id' => true,
913 'title' => true,
914 'style' => true,
915 'aria-hidden' => true,
916 ),
917 'strike' => array(),
918 'strong' => array(),
919 'symbol' => array(
920 'class' => true,
921 'id' => true,
922 'viewbox' => true,
923 ),
924 'svg' => array(
925 'class' => true,
926 'id' => true,
927 'xmlns' => true,
928 'viewbox' => true,
929 'width' => true,
930 'height' => true,
931 'style' => true,
932 'fill' => true,
933 'aria-label' => true,
934 'aria-hidden' => true,
935 ),
936 'use' => array(
937 'href' => true,
938 'xlink:href' => true,
939 ),
940 'ul' => $allow_class,
941 'label' => array(
942 'for' => true,
943 'class' => true,
944 'id' => true,
945 ),
946 'button' => array(
947 'class' => true,
948 'type' => true,
949 ),
950 'legend' => array(
951 'class' => true,
952 ),
953 );
954 }
955
956 /**
957 * Used when switching the action for a bulk action
958 *
959 * @since 2.0
960 */
961 public static function remove_get_action() {
962 if ( ! isset( $_GET ) ) {
963 return;
964 }
965
966 $action_name = isset( $_GET['action'] ) ? 'action' : ( isset( $_GET['action2'] ) ? 'action2' : '' );
967 if ( empty( $action_name ) ) {
968 return;
969 }
970
971 $new_action = self::get_param( $action_name, '', 'get', 'sanitize_text_field' );
972 if ( ! empty( $new_action ) ) {
973 $_SERVER['REQUEST_URI'] = str_replace( '&action=' . $new_action, '', self::get_server_value( 'REQUEST_URI' ) );
974 }
975 }
976
977 /**
978 * Check the WP query for a parameter
979 *
980 * @since 2.0
981 * @return string|array
982 */
983 public static function get_query_var( $value, $param ) {
984 if ( $value != '' ) {
985 return $value;
986 }
987
988 global $wp_query;
989 if ( isset( $wp_query->query_vars[ $param ] ) ) {
990 $value = $wp_query->query_vars[ $param ];
991 }
992
993 return $value;
994 }
995
996 /**
997 * Try to show the SVG if possible. Otherwise, use the font icon.
998 *
999 * @since 4.0.02
1000 * @param string $class
1001 * @param array $atts
1002 */
1003 public static function icon_by_class( $class, $atts = array() ) {
1004 $echo = ! isset( $atts['echo'] ) || $atts['echo'];
1005 if ( isset( $atts['echo'] ) ) {
1006 unset( $atts['echo'] );
1007 }
1008
1009 $html_atts = self::array_to_html_params( $atts );
1010
1011 $icon = trim( str_replace( array( 'frm_icon_font', 'frmfont ' ), '', $class ) );
1012
1013 // Replace icons that have been removed.
1014 $deprecated = array(
1015 'frm_clone_solid_icon' => 'frm_clone_icon',
1016 );
1017 if ( isset( $deprecated[ $icon ] ) ) {
1018 $icon = $deprecated[ $icon ];
1019 $class = str_replace( $icon, $deprecated[ $icon ], $class );
1020 }
1021
1022 if ( $icon === $class ) {
1023 $icon = '<i class="' . esc_attr( $class ) . '"' . $html_atts . '></i>';
1024 } else {
1025 $class = strpos( $icon, ' ' ) === false ? '' : ' ' . $icon;
1026 if ( strpos( $icon, ' ' ) ) {
1027 $icon = explode( ' ', $icon );
1028 $icon = reset( $icon );
1029 }
1030 $icon = '<svg class="frmsvg' . esc_attr( $class ) . '"' . $html_atts . '>
1031 <use xlink:href="#' . esc_attr( $icon ) . '" />
1032 </svg>';
1033 }
1034
1035 if ( $echo ) {
1036 echo self::kses_icon( $icon ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1037 } else {
1038 return $icon;
1039 }
1040 }
1041
1042 /**
1043 * Run kses for icons. It needs to add a few filters first in order to preserve some custom style values.
1044 *
1045 * @since 5.0.13
1046 *
1047 * @param string $icon
1048 * @return string
1049 */
1050 public static function kses_icon( $icon ) {
1051 add_filter( 'safe_style_css', 'FrmAppHelper::allow_vars_in_styles' );
1052 add_filter( 'safecss_filter_attr_allow_css', 'FrmAppHelper::allow_style', 10, 2 );
1053 add_filter( 'frm_striphtml_allowed_tags', 'FrmAppHelper::add_allowed_icon_tags' );
1054 $icon = self::kses( $icon, 'all' );
1055 remove_filter( 'safe_style_css', 'FrmAppHelper::allow_vars_in_styles' );
1056 remove_filter( 'safecss_filter_attr_allow_css', 'FrmAppHelper::allow_style' );
1057 remove_filter( 'frm_striphtml_allowed_tags', 'FrmAppHelper::add_allowed_icon_tags' );
1058 return $icon;
1059 }
1060
1061 /**
1062 * @since 5.0.13.1
1063 *
1064 * @param array $allowed_html
1065 * @return array
1066 */
1067 public static function add_allowed_icon_tags( $allowed_html ) {
1068 $allowed_html['svg']['data-open'] = true;
1069 $allowed_html['svg']['title'] = true;
1070 return $allowed_html;
1071 }
1072
1073 /**
1074 * @since 5.0.13
1075 *
1076 * @param array $allowed_attr
1077 * @return array
1078 */
1079 public static function allow_vars_in_styles( $allowed_attr ) {
1080 $allowed_attr[] = '--primary-700';
1081 return $allowed_attr;
1082 }
1083
1084 /**
1085 * @since 5.0.13
1086 *
1087 * @param bool $allow_css
1088 * @param string $css_string
1089 */
1090 public static function allow_style( $allow_css, $css_string ) {
1091 if ( ! $allow_css && 0 === strpos( $css_string, '--primary-700:' ) ) {
1092 $split = explode( ':', $css_string, 2 );
1093 $allow_css = 2 === count( $split ) && self::is_a_valid_color( $split[1] );
1094 }
1095 return $allow_css;
1096 }
1097
1098 /**
1099 * @since 5.0.13
1100 *
1101 * @param string $value
1102 * @return bool
1103 */
1104 private static function is_a_valid_color( $value ) {
1105 $match = 0;
1106 if ( 0 === strpos( $value, 'rgba(' ) ) {
1107 $match = preg_match( '/^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d*(?:\.\d+)?)\)$/', $value );
1108 } elseif ( 0 === strpos( $value, 'rgb(' ) ) {
1109 $match = preg_match( '/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/', $value );
1110 } elseif ( 0 === strpos( $value, '#' ) ) {
1111 $match = preg_match( '/^#([a-f0-9]{6}|[a-f0-9]{3})\b$/', $value );
1112 }
1113 return (bool) $match;
1114 }
1115
1116 /**
1117 * Include svg images.
1118 *
1119 * @since 4.0.02
1120 * @return void
1121 */
1122 public static function include_svg() {
1123 if ( self::$included_svg ) {
1124 return;
1125 }
1126
1127 // Use readfile instead of include_once because of a default security rule in Snuffleupagus.
1128 readfile( self::plugin_path() . '/images/icons.svg' );
1129 self::$included_svg = true;
1130 }
1131
1132 /**
1133 * Convert an associative array to HTML values.
1134 *
1135 * @since 4.0.02
1136 * @since 5.0.13 added $echo parameter.
1137 *
1138 * @param array $atts
1139 * @param bool $echo
1140 * @return string|void
1141 */
1142 public static function array_to_html_params( $atts, $echo = false ) {
1143 $callback = function() use ( $atts ) {
1144 if ( $atts ) {
1145 foreach ( $atts as $key => $value ) {
1146 echo ' ' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
1147 }
1148 }
1149 };
1150 return self::clip( $callback, $echo );
1151 }
1152
1153 /**
1154 * Call an echo function and either echo it or return the result as a string.
1155 *
1156 * @since 5.0.13
1157 *
1158 * @param Closure $echo_function
1159 * @param bool $echo
1160 * @return string|void
1161 */
1162 public static function clip( $echo_function, $echo = false ) {
1163 if ( ! $echo ) {
1164 ob_start();
1165 }
1166
1167 if ( is_callable( $echo_function ) ) {
1168 $echo_function();
1169 }
1170
1171 if ( ! $echo ) {
1172 $return = ob_get_contents();
1173 ob_end_clean();
1174 return $return;
1175 }
1176 }
1177
1178 /**
1179 * @since 3.0
1180 */
1181 public static function get_admin_header( $atts ) {
1182 $has_nav = ( isset( $atts['form'] ) && ! empty( $atts['form'] ) && ( ! isset( $atts['is_template'] ) || ! $atts['is_template'] ) );
1183 if ( ! isset( $atts['close'] ) || empty( $atts['close'] ) ) {
1184 $atts['close'] = admin_url( 'admin.php?page=formidable' );
1185 }
1186 if ( ! isset( $atts['import_link'] ) ) {
1187 $atts['import_link'] = false;
1188 }
1189
1190 include( self::plugin_path() . '/classes/views/shared/admin-header.php' );
1191 }
1192
1193 /**
1194 * @since 6.0
1195 *
1196 * @param string $type
1197 * @return void
1198 */
1199 public static function import_link( $type = 'secondary' ) {
1200 ?>
1201 <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-import' ) ); ?>" class="button frm-button-<?php echo esc_attr( $type ); ?> frm_animate_bg">
1202 <?php esc_html_e( 'Import', 'formidable' ); ?>
1203 </a>
1204 <?php
1205 }
1206
1207 /**
1208 * Print applicable admin banner.
1209 *
1210 * @since 5.4.2
1211 *
1212 * @param bool $should_show_lite_upgrade
1213 * @return void
1214 */
1215 public static function print_admin_banner( $should_show_lite_upgrade ) {
1216 if ( ! current_user_can( 'administrator' ) ) {
1217 FrmInbox::maybe_show_banner();
1218 return;
1219 }
1220
1221 if ( self::maybe_show_license_warning() || FrmInbox::maybe_show_banner() || ! $should_show_lite_upgrade || self::pro_is_installed() ) {
1222 // Print license warning or inbox banner and exit if either prints.
1223 // And exit before printing the upgrade bar if it shouldn't be shown.
1224 return;
1225 }
1226 ?>
1227 <div class="frm-upgrade-bar">
1228 <span>You're using Formidable Forms Lite. To unlock more features consider</span>
1229 <a href="<?php echo esc_url( self::admin_upgrade_link( 'top-bar' ) ); ?>" target="_blank" rel="noopener">upgrading to Pro</a>.
1230 </div>
1231 <?php
1232 }
1233
1234 /**
1235 * @since 5.4.2
1236 *
1237 * @return bool True if a banner is available and shown.
1238 */
1239 private static function maybe_show_license_warning() {
1240 return is_callable( 'FrmProAddonsController::admin_banner' ) && FrmProAddonsController::admin_banner();
1241 }
1242
1243 /**
1244 * Render a button for a new item (Form, Application, etc).
1245 *
1246 * @since 3.0
1247 * @param array $atts {
1248 * @type array $link_hook Custom link hook, calls do_action and exits early.
1249 * @type string $new_link Href value, default #.
1250 * @type string $class Custom class names, space separated.
1251 * @type string $button_text Button text. Default "Add New".
1252 * }
1253 * @return void
1254 */
1255 public static function add_new_item_link( $atts ) {
1256 if ( isset( $atts['link_hook'] ) ) {
1257 do_action( $atts['link_hook']['hook'], $atts['link_hook']['param'] );
1258 return;
1259 }
1260
1261 if ( empty( $atts['new_link'] ) && empty( $atts['trigger_new_form_modal'] ) && empty( $atts['class'] ) ) {
1262 // Do not render a button if none of these attributes are set.
1263 return;
1264 }
1265
1266 $href = ! empty( $atts['new_link'] ) ? esc_url( $atts['new_link'] ) : '#';
1267 $class = 'button button-primary frm-button-primary';
1268
1269 if ( ! empty( $atts['trigger_new_form_modal'] ) ) {
1270 $class .= ' frm-trigger-new-form-modal';
1271 }
1272
1273 if ( ! empty( $atts['class'] ) ) {
1274 $class .= ' ' . $atts['class'];
1275 }
1276
1277 $button_text = ! empty( $atts['button_text'] ) ? $atts['button_text'] : __( 'Add New', 'formidable' );
1278
1279 require self::plugin_path() . '/classes/views/shared/add-button.php';
1280 }
1281
1282 /**
1283 * @since 3.06
1284 */
1285 public static function show_search_box( $atts ) {
1286 $defaults = array(
1287 'placeholder' => '',
1288 'tosearch' => '',
1289 'text' => __( 'Search', 'formidable' ),
1290 'input_id' => '',
1291 );
1292 $atts = array_merge( $defaults, $atts );
1293
1294 if ( $atts['input_id'] === 'template' && empty( $atts['tosearch'] ) ) {
1295 $atts['tosearch'] = 'frm-card';
1296 }
1297
1298 $class = 'frm-search-input';
1299 if ( ! empty( $atts['tosearch'] ) ) {
1300 $class .= ' frm-auto-search';
1301 }
1302
1303 $input_id = $atts['input_id'] . '-search-input';
1304
1305 ?>
1306 <p class="frm-search">
1307 <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>">
1308 <?php echo esc_html( $atts['text'] ); ?>:
1309 </label>
1310 <span class="frmfont frm_search_icon"></span>
1311 <input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s"
1312 value="<?php _admin_search_query(); ?>" placeholder="<?php echo esc_attr( $atts['placeholder'] ); ?>"
1313 class="<?php echo esc_attr( $class ); ?>" data-tosearch="<?php echo esc_attr( $atts['tosearch'] ); ?>"
1314 <?php if ( ! empty( $atts['tosearch'] ) ) { ?>
1315 autocomplete="off"
1316 <?php } ?>
1317 />
1318 <?php
1319 if ( empty( $atts['tosearch'] ) ) {
1320 submit_button( $atts['text'], 'button-secondary', '', false, array( 'id' => 'search-submit' ) );
1321 }
1322 ?>
1323 </p>
1324 <?php
1325 }
1326
1327 /**
1328 * @param string $type
1329 */
1330 public static function trigger_hook_load( $type, $object = null ) {
1331 // Only load the form hooks once.
1332 $hooks_loaded = apply_filters( 'frm_' . $type . '_hooks_loaded', false, $object );
1333 if ( ! $hooks_loaded ) {
1334 do_action( 'frm_load_' . $type . '_hooks' );
1335 }
1336 }
1337
1338 /**
1339 * Save all front-end js scripts into a single file.
1340 * And save an additional single file of all front-end Stripe JS scripts.
1341 *
1342 * @since 3.0
1343 *
1344 * @return void
1345 */
1346 public static function save_combined_js() {
1347 $file_atts = apply_filters(
1348 'frm_js_location',
1349 array(
1350 'file_name' => 'frm.min.js',
1351 'new_file_path' => self::plugin_path() . '/js',
1352 )
1353 );
1354 $new_file = new FrmCreateFile( $file_atts );
1355
1356 $files = array(
1357 self::plugin_path() . '/js/formidable.min.js',
1358 );
1359 /**
1360 * @param array $files
1361 */
1362 $files = apply_filters( 'frm_combined_js_files', $files );
1363 $new_file->combine_files( $files );
1364
1365 // Create the minified Stripe Script.
1366 $file_atts = apply_filters(
1367 'frm_stripe_js_location',
1368 array(
1369 'file_name' => 'frmstrp.min.js',
1370 'new_file_path' => self::plugin_path() . '/js',
1371 )
1372 );
1373 $new_file = new FrmCreateFile( $file_atts );
1374 $files = array(
1375 FrmStrpLiteAppHelper::plugin_path() . 'js/frmstrp.min.js',
1376 );
1377
1378 /**
1379 * @since 6.5
1380 *
1381 * @param array $files
1382 */
1383 $files = apply_filters( 'frm_stripe_combined_js_files', $files );
1384 $new_file->combine_files( $files );
1385 }
1386
1387 /**
1388 * Check a value from a shortcode to see if true or false.
1389 * True when value is 1, true, 'true', 'yes'
1390 *
1391 * @since 1.07.10
1392 *
1393 * @param string $value The value to compare
1394 *
1395 * @return boolean True or False
1396 */
1397 public static function is_true( $value ) {
1398 return ( true === $value || 1 == $value || 'true' == $value || 'yes' == $value );
1399 }
1400
1401 /**
1402 * Gets all post from a specific post type.
1403 * This gets the entire WP_Post object so it can require a lot of memory. When only id and title are needed, consider using FrmAppHelper::get_post_ids_and_titles instead.
1404 *
1405 * @since 4.10.01 Add `$post_type` argument.
1406 *
1407 * @param string $post_type Post type to query. Default is `page`.
1408 * @return WP_Post[]
1409 */
1410 public static function get_pages( $post_type = 'page' ) {
1411 $query = array(
1412 'post_type' => $post_type,
1413 'post_status' => array( 'publish', 'private' ),
1414 'numberposts' => - 1,
1415 'orderby' => 'title',
1416 'order' => 'ASC',
1417 );
1418
1419 return get_posts( $query );
1420 }
1421
1422 /**
1423 * Gets post ids and titles for a specific post type.
1424 *
1425 * @since 5.0.09
1426 *
1427 * @param string $post_type Post type to query. Default is `page`.
1428 * @return array
1429 */
1430 public static function get_post_ids_and_titles( $post_type = 'page' ) {
1431 return FrmDb::get_results(
1432 'posts',
1433 array(
1434 'post_type' => $post_type,
1435 'post_status' => array( 'publish', 'private' ),
1436 ),
1437 'ID, post_title',
1438 array(
1439 'order_by' => 'post_title ASC',
1440 )
1441 );
1442 }
1443
1444 /**
1445 * Renders an autocomplete page selection or a regular dropdown depending on
1446 * the total page count
1447 *
1448 * @since 4.03.06
1449 * @since 4.10.01 Added `post_type` and `autocomplete_placeholder` to the arguments array.
1450 *
1451 * @param array $args Selection arguments.
1452 */
1453 public static function maybe_autocomplete_pages_options( $args ) {
1454 $args = self::preformat_selection_args( $args );
1455
1456 $pages_count = wp_count_posts( $args['post_type'] );
1457
1458 if ( $pages_count->publish <= 50 ) {
1459 self::wp_pages_dropdown( $args );
1460 return;
1461 }
1462
1463 wp_enqueue_script( 'jquery-ui-autocomplete' );
1464
1465 $selected = self::get_post_param( $args['field_name'], $args['page_id'], 'absint' );
1466 $title = '';
1467
1468 if ( $selected ) {
1469 $title = get_the_title( $selected );
1470 }
1471
1472 ?>
1473 <input type="text" class="frm-page-search"
1474 data-post-type="<?php echo esc_attr( $args['post_type'] ); ?>"
1475 placeholder="<?php echo esc_attr( $args['autocomplete_placeholder'] ); ?>"
1476 value="<?php echo esc_attr( $title ); ?>" />
1477 <input type="hidden" name="<?php echo esc_attr( $args['field_name'] ); ?>"
1478 class="frm_autocomplete_value_input"
1479 value="<?php echo esc_attr( $selected ); ?>" />
1480 <?php
1481 }
1482
1483 /**
1484 * @param array $args
1485 * @param string $page_id Deprecated.
1486 * @param boolean $truncate Deprecated.
1487 */
1488 public static function wp_pages_dropdown( $args = array(), $page_id = '', $truncate = false ) {
1489 self::prep_page_dropdown_params( $page_id, $truncate, $args );
1490
1491 $pages = self::get_post_ids_and_titles( $args['post_type'] );
1492 $selected = self::get_post_param( $args['field_name'], $args['page_id'], 'absint' );
1493
1494 ?>
1495 <select name="<?php echo esc_attr( $args['field_name'] ); ?>" id="<?php echo esc_attr( $args['field_name'] ); ?>" class="frm-pages-dropdown">
1496 <option value=""><?php echo esc_html( $args['placeholder'] ); ?></option>
1497 <?php foreach ( $pages as $page ) { ?>
1498 <option value="<?php echo esc_attr( $page->ID ); ?>" <?php selected( $selected, $page->ID ); ?>>
1499 <?php echo esc_html( $args['truncate'] ? self::truncate( $page->post_title, $args['truncate'] ) : $page->post_title ); ?>
1500 </option>
1501 <?php } ?>
1502 </select>
1503 <?php
1504 }
1505
1506 /**
1507 * Fill in missing parameters passed to wp_pages_dropdown().
1508 * This is for reverse compatibility with switching 3 params to 1.
1509 *
1510 * @since 4.03.06
1511 */
1512 private static function prep_page_dropdown_params( $page_id, $truncate, &$args ) {
1513 if ( ! is_array( $args ) ) {
1514 $args = array(
1515 'field_name' => $args,
1516 'page_id' => $page_id,
1517 'truncate' => $truncate,
1518 );
1519 }
1520
1521 $args = self::preformat_selection_args( $args );
1522 }
1523
1524 /**
1525 * Filter to format args for page dropdown or autocomplete
1526 *
1527 * @since 4.03.06
1528 * @since 4.10.01 Added `post_type` and `autocomplete_placeholder` to the arguments array.
1529 */
1530 private static function preformat_selection_args( $args ) {
1531 $defaults = array(
1532 'truncate' => false,
1533 'placeholder' => ' ',
1534 'field_name' => '',
1535 'page_id' => '',
1536 'post_type' => 'page',
1537 'autocomplete_placeholder' => __( 'Select a Page', 'formidable' ),
1538 );
1539
1540 return array_merge( $defaults, $args );
1541 }
1542
1543 public static function post_edit_link( $post_id ) {
1544 $post = get_post( $post_id );
1545 if ( $post ) {
1546 $post_url = admin_url( 'post.php?post=' . $post_id . '&action=edit' );
1547
1548 return '<a href="' . esc_url( $post_url ) . '">' . self::truncate( $post->post_title, 50 ) . '</a>';
1549 }
1550
1551 return '';
1552 }
1553
1554 /**
1555 * Hide the WordPress menus on some pages.
1556 *
1557 * @since 4.0
1558 *
1559 * @return bool
1560 */
1561 public static function is_full_screen() {
1562 return self::is_form_builder_page() ||
1563 self::is_style_editor_page() ||
1564 self::is_full_screen_view_builder_page();
1565 }
1566
1567 /**
1568 * Check if user is on the style editor or its alternative URL.
1569 * The first URL is a submenu "Styles" in the Formidable menu /wp-admin/admin.php?page=formidable-styles.
1570 * The alternative URL is linked as a submenu "Forms" item of the Appearance menu /wp-admin/themes.php?page=formidable-styles2.
1571 *
1572 * @since 5.5.3
1573 * @since 6.0 Added the $view parameter. Previously there was only a 'edit' view.
1574 *
1575 * @param string $view Supports 'edit', 'list', and ''. If '', both 'edit' and 'list' will match.
1576 * @return bool
1577 */
1578 public static function is_style_editor_page( $view = '' ) {
1579 if ( ! self::is_admin_page( 'formidable-styles' ) && ! self::is_admin_page( 'formidable-styles2' ) ) {
1580 return false;
1581 }
1582
1583 if ( ! in_array( $view, array( 'list', 'edit' ), true ) ) {
1584 return true;
1585 }
1586
1587 $action = self::simple_get( 'frm_action' );
1588 $is_edit_mode = 'edit' === $action || ( ! $action && ! self::simple_get( 'id' ) && ! self::simple_get( 'form' ) );
1589
1590 if ( ! $is_edit_mode && class_exists( 'FrmProStylesController' ) && in_array( $action, array( 'new_style', 'duplicate' ), true ) ) {
1591 $is_edit_mode = true;
1592 }
1593
1594 $checking_for_edit_mode = 'edit' === $view;
1595
1596 return $is_edit_mode === $checking_for_edit_mode;
1597 }
1598
1599 /**
1600 * @since 5.5.3
1601 *
1602 * @return bool
1603 */
1604 private static function is_full_screen_view_builder_page() {
1605 return self::is_admin_page( 'formidable-views-editor' );
1606 }
1607
1608 /**
1609 * @since 4.0
1610 * @deprecated 5.5.3
1611 */
1612 public static function maybe_full_screen_link( $link ) {
1613 _deprecated_function( __METHOD__, '5.5.3' );
1614 return $link;
1615 }
1616
1617 /**
1618 * @param string $field_name
1619 * @param string|array $capability
1620 * @param string $multiple 'single' and 'multiple'
1621 */
1622 public static function wp_roles_dropdown( $field_name, $capability, $multiple = 'single' ) {
1623 ?>
1624 <select name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $field_name ); ?>"
1625 <?php echo ( 'multiple' === $multiple ) ? 'multiple="multiple"' : ''; ?>
1626 class="frm_multiselect">
1627 <?php self::roles_options( $capability ); ?>
1628 </select>
1629 <?php
1630 }
1631
1632 /**
1633 * @since 4.07
1634 * @param array|string $selected
1635 * @param string $current
1636 */
1637 private static function selected( $selected, $current ) {
1638 if ( is_callable( 'FrmProAppHelper::selected' ) ) {
1639 FrmProAppHelper::selected( $selected, $current );
1640 } else {
1641 selected( in_array( $current, (array) $selected, true ) );
1642 }
1643 }
1644
1645 /**
1646 * @param string|array $capability
1647 */
1648 public static function roles_options( $capability ) {
1649 global $frm_vars;
1650 if ( isset( $frm_vars['editable_roles'] ) ) {
1651 $editable_roles = $frm_vars['editable_roles'];
1652 } else {
1653 $editable_roles = get_editable_roles();
1654 $frm_vars['editable_roles'] = $editable_roles;
1655 }
1656
1657 foreach ( $editable_roles as $role => $details ) {
1658 $name = translate_user_role( $details['name'] );
1659 ?>
1660 <option value="<?php echo esc_attr( $role ); ?>" <?php self::selected( $capability, $role ); ?>><?php echo esc_attr( $name ); ?> </option>
1661 <?php
1662 unset( $role, $details );
1663 }
1664 }
1665
1666 /**
1667 * Gets the list of capabilities.
1668 *
1669 * @since 5.0 Parameter `$type` supports `pro_only` value.
1670 *
1671 * @param string $type Supports `auto`, `pro`, or `pro_only`.
1672 * @return array
1673 */
1674 public static function frm_capabilities( $type = 'auto' ) {
1675 if ( ! self::pro_is_installed() && ! in_array( $type, array( 'pro', 'pro_only' ), true ) ) {
1676 return self::get_lite_capabilities();
1677 }
1678
1679 $pro_cap = array(
1680 'frm_create_entries' => __( 'Add Entries from Admin Area', 'formidable' ),
1681 'frm_edit_entries' => __( 'Edit Entries from Admin Area', 'formidable' ),
1682 'frm_view_reports' => __( 'View Reports', 'formidable' ),
1683 'frm_edit_displays' => __( 'Add/Edit Views', 'formidable' ),
1684 );
1685 /**
1686 * @since 5.3.1
1687 *
1688 * @param array<string,string> $pro_cap
1689 */
1690 $pro_cap = apply_filters( 'frm_pro_capabilities', $pro_cap );
1691
1692 if ( 'pro_only' === $type ) {
1693 return $pro_cap;
1694 }
1695
1696 return self::get_lite_capabilities() + $pro_cap;
1697 }
1698
1699 /**
1700 * Get the list of lite plugin capabilities.
1701 *
1702 * @since 5.3.1
1703 *
1704 * @return array<string,string>
1705 */
1706 private static function get_lite_capabilities() {
1707 return array(
1708 'frm_view_forms' => __( 'View Forms', 'formidable' ),
1709 'frm_edit_forms' => __( 'Add and Edit Forms', 'formidable' ),
1710 'frm_delete_forms' => __( 'Delete Forms', 'formidable' ),
1711 'frm_change_settings' => __( 'Access this Settings Page', 'formidable' ),
1712 'frm_view_entries' => __( 'View Entries from Admin Area', 'formidable' ),
1713 'frm_delete_entries' => __( 'Delete Entries from Admin Area', 'formidable' ),
1714 );
1715 }
1716
1717 /**
1718 * Call the WordPress current_user_can but also validate empty strings as true for any logged in user
1719 *
1720 * @since 4.06.03
1721 *
1722 * @param string $role
1723 *
1724 * @return bool
1725 */
1726 public static function current_user_can( $role ) {
1727 if ( $role === '-1' ) {
1728 return false;
1729 }
1730
1731 if ( $role === 'loggedout' ) {
1732 return ! is_user_logged_in();
1733 }
1734
1735 if ( $role === 'loggedin' || ! $role ) {
1736 return is_user_logged_in();
1737 }
1738
1739 if ( $role == 1 ) {
1740 $role = 'administrator';
1741 }
1742
1743 if ( ! is_user_logged_in() ) {
1744 return false;
1745 }
1746
1747 return current_user_can( $role );
1748 }
1749
1750 /**
1751 * @param string|array $needed_role
1752 * @return bool
1753 */
1754 public static function user_has_permission( $needed_role ) {
1755 if ( is_array( $needed_role ) ) {
1756 foreach ( $needed_role as $role ) {
1757 if ( self::current_user_can( $role ) ) {
1758 return true;
1759 }
1760 }
1761
1762 return false;
1763 }
1764
1765 $can = self::current_user_can( $needed_role );
1766
1767 if ( $can || in_array( $needed_role, array( '-1', 'loggedout' ) ) ) {
1768 return $can;
1769 }
1770
1771 $roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
1772 foreach ( $roles as $role ) {
1773 if ( current_user_can( $role ) ) {
1774 return true;
1775 }
1776 if ( $role == $needed_role ) {
1777 break;
1778 }
1779 }
1780
1781 return false;
1782 }
1783
1784 /**
1785 * Make sure administrators can see Formidable menu
1786 *
1787 * @since 2.0
1788 */
1789 public static function maybe_add_permissions() {
1790 self::force_capability( 'frm_view_entries' );
1791
1792 if ( ! current_user_can( 'administrator' ) || current_user_can( 'frm_view_forms' ) ) {
1793 return;
1794 }
1795
1796 $user_id = get_current_user_id();
1797 $user = new WP_User( $user_id );
1798 $frm_roles = self::frm_capabilities();
1799 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
1800 $user->add_cap( $frm_role );
1801 unset( $frm_role, $frm_role_description );
1802 }
1803 }
1804
1805 /**
1806 * Make sure admins have permission to see the menu items
1807 *
1808 * @since 2.0.6
1809 */
1810 public static function force_capability( $cap = 'frm_change_settings' ) {
1811 if ( current_user_can( 'administrator' ) && ! current_user_can( $cap ) ) {
1812 $role = get_role( 'administrator' );
1813 $frm_roles = self::frm_capabilities();
1814 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
1815 $role->add_cap( $frm_role );
1816 }
1817 }
1818 }
1819
1820 /**
1821 * Check if the user has permision for action.
1822 * Return permission message and stop the action if no permission
1823 *
1824 * @since 2.0
1825 *
1826 * @param string $permission
1827 */
1828 public static function permission_check( $permission, $show_message = 'show' ) {
1829 $permission_error = self::permission_nonce_error( $permission );
1830 if ( $permission_error !== false ) {
1831 if ( 'hide' == $show_message ) {
1832 $permission_error = '';
1833 }
1834 wp_die( esc_html( $permission_error ) );
1835 }
1836 }
1837
1838 /**
1839 * Check user permission and nonce
1840 *
1841 * @since 2.0
1842 *
1843 * @param string $permission
1844 *
1845 * @return false|string The permission message or false if allowed
1846 */
1847 public static function permission_nonce_error( $permission, $nonce_name = '', $nonce = '' ) {
1848 if ( ! empty( $permission ) && ! current_user_can( $permission ) && ! current_user_can( 'administrator' ) ) {
1849 $frm_settings = self::get_settings();
1850
1851 return $frm_settings->admin_permission;
1852 }
1853
1854 $error = false;
1855 if ( empty( $nonce_name ) ) {
1856 return $error;
1857 }
1858
1859 $nonce_value = ( $_REQUEST && isset( $_REQUEST[ $nonce_name ] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $nonce_name ] ) ) : '';
1860 if ( $_REQUEST && ( ! isset( $_REQUEST[ $nonce_name ] ) || ! wp_verify_nonce( $nonce_value, $nonce ) ) ) {
1861 $frm_settings = self::get_settings();
1862 $error = $frm_settings->admin_permission;
1863 }
1864
1865 return $error;
1866 }
1867
1868 public static function checked( $values, $current ) {
1869 if ( self::check_selected( $values, $current ) ) {
1870 echo ' checked="checked"';
1871 }
1872 }
1873
1874 public static function check_selected( $values, $current ) {
1875 $values = self::recursive_function_map( $values, 'trim' );
1876 $values = self::recursive_function_map( $values, 'htmlspecialchars_decode' );
1877 $current = htmlspecialchars_decode( trim( $current ) );
1878
1879 return ( is_array( $values ) && in_array( $current, $values ) ) || ( ! is_array( $values ) && $values == $current );
1880 }
1881
1882 public static function recursive_function_map( $value, $function ) {
1883 if ( is_array( $value ) ) {
1884 $original_function = $function;
1885 if ( count( $value ) ) {
1886 $function = explode( ', ', FrmDb::prepare_array_values( $value, $function ) );
1887 } else {
1888 $function = array( $function );
1889 }
1890 if ( ! self::is_assoc( $value ) ) {
1891 $value = array_map( array( 'FrmAppHelper', 'recursive_function_map' ), $value, $function );
1892 } else {
1893 foreach ( $value as $k => $v ) {
1894 if ( ! is_array( $v ) ) {
1895 $value[ $k ] = call_user_func( $original_function, $v );
1896 }
1897 }
1898 }
1899 } else {
1900 $value = call_user_func( $function, $value );
1901 }
1902
1903 return $value;
1904 }
1905
1906 public static function is_assoc( $array ) {
1907 return (bool) count( array_filter( array_keys( $array ), 'is_string' ) );
1908 }
1909
1910 /**
1911 * Flatten a multi-dimensional array
1912 */
1913 public static function array_flatten( $array, $keys = 'keep' ) {
1914 $return = array();
1915 foreach ( $array as $key => $value ) {
1916 if ( is_array( $value ) ) {
1917 $return = array_merge( $return, self::array_flatten( $value, $keys ) );
1918 } else {
1919 if ( $keys === 'keep' ) {
1920 $return[ $key ] = $value;
1921 } else {
1922 $return[] = $value;
1923 }
1924 }
1925 }
1926
1927 return $return;
1928 }
1929
1930 public static function esc_textarea( $text, $is_rich_text = false ) {
1931 $safe_text = str_replace( '&quot;', '"', $text );
1932 if ( ! $is_rich_text ) {
1933 $safe_text = htmlspecialchars( $safe_text, ENT_NOQUOTES );
1934 }
1935 $safe_text = str_replace( '&amp; ', '& ', $safe_text );
1936
1937 return apply_filters( 'esc_textarea', $safe_text, $text );
1938 }
1939
1940 /**
1941 * Add auto paragraphs to text areas
1942 *
1943 * @since 2.0
1944 */
1945 public static function use_wpautop( $content ) {
1946 if ( apply_filters( 'frm_use_wpautop', true ) && ! is_array( $content ) ) {
1947 $content = wpautop( str_replace( '<br>', '<br />', $content ) );
1948 }
1949
1950 return $content;
1951 }
1952
1953 public static function replace_quotes( $val ) {
1954 // Replace double quotes.
1955 $val = str_replace( array( '&#8220;', '&#8221;', '&#8243;' ), '"', $val );
1956
1957 // Replace single quotes.
1958 $val = str_replace( array( '&#8216;', '&#8217;', '&#8242;', '&prime;', '&rsquo;', '&lsquo;' ), "'", $val );
1959
1960 return $val;
1961 }
1962
1963 /**
1964 * @param string $handle
1965 */
1966 public static function script_version( $handle, $default = 0 ) {
1967 global $wp_scripts;
1968 if ( ! $wp_scripts ) {
1969 return $default;
1970 }
1971
1972 $ver = $default;
1973 if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
1974 return $ver;
1975 }
1976
1977 $query = $wp_scripts->registered[ $handle ];
1978 if ( is_object( $query ) && ! empty( $query->ver ) ) {
1979 $ver = $query->ver;
1980 }
1981
1982 return $ver;
1983 }
1984
1985 /**
1986 * @since 5.0.13 added $echo param.
1987 *
1988 * @param string $url
1989 * @param bool $echo
1990 * @return string|void
1991 */
1992 public static function js_redirect( $url, $echo = false ) {
1993 $callback = function() use ( $url ) {
1994 echo '<script type="text/javascript">window.location="' . esc_url_raw( $url ) . '"</script>';
1995 };
1996 return self::clip( $callback, $echo );
1997 }
1998
1999 public static function get_user_id_param( $user_id ) {
2000 if ( ! $user_id || is_numeric( $user_id ) ) {
2001 return $user_id;
2002 }
2003
2004 $user_id = sanitize_text_field( $user_id );
2005 if ( $user_id === 'current' ) {
2006 $user_id = get_current_user_id();
2007 } else {
2008 if ( is_email( $user_id ) ) {
2009 $user = get_user_by( 'email', $user_id );
2010 } else {
2011 $user = get_user_by( 'login', $user_id );
2012 }
2013
2014 if ( $user ) {
2015 $user_id = $user->ID;
2016 }
2017 unset( $user );
2018 }
2019
2020 return $user_id;
2021 }
2022
2023 public static function get_file_contents( $filename, $atts = array() ) {
2024 if ( ! is_file( $filename ) ) {
2025 return false;
2026 }
2027
2028 extract( $atts ); // phpcs:ignore WordPress.PHP.DontExtract
2029 ob_start();
2030 include( $filename );
2031 $contents = ob_get_contents();
2032 ob_end_clean();
2033
2034 return $contents;
2035 }
2036
2037 /**
2038 * @param string $name
2039 * @param string $table_name
2040 * @param string $column
2041 * @param int $id
2042 * @param int $num_chars
2043 */
2044 public static function get_unique_key( $name, $table_name, $column, $id = 0, $num_chars = 5 ) {
2045 $key = '';
2046 if ( $name ) {
2047 $key = sanitize_key( $name );
2048 $key = self::maybe_clear_long_key( $key, $column );
2049 }
2050
2051 if ( ! $key ) {
2052 $key = self::generate_new_key( $num_chars );
2053 }
2054
2055 $key = self::prevent_numeric_and_reserved_keys( $key );
2056
2057 $similar_keys = FrmDb::get_col(
2058 $table_name,
2059 array(
2060 $column . ' like%' => $key,
2061 'ID !' => $id,
2062 ),
2063 $column
2064 );
2065
2066 // Create a unique field id if it has already been used.
2067 if ( in_array( $key, $similar_keys, true ) ) {
2068 $key = self::maybe_truncate_key_before_appending( $column, $key );
2069
2070 /**
2071 * Allow for a custom separator between the attempted key and the generated suffix.
2072 *
2073 * @since 5.2.03
2074 *
2075 * @param string $separator. Default empty.
2076 * @param string $key the key without the added suffix.
2077 */
2078 $separator = apply_filters( 'frm_unique_' . $column . '_separator', '', $key );
2079
2080 $suffix = 2;
2081 do {
2082 $key_check = $key . $separator . $suffix;
2083 ++$suffix;
2084 } while ( in_array( $key_check, $similar_keys, true ) );
2085
2086 $key = $key_check;
2087 }
2088
2089 return $key;
2090 }
2091
2092 /**
2093 * Avoid trying to append to a really long key,
2094 * The database limit is 100 for form and field keys so we want to avoid getting too close.
2095 *
2096 * @param string $column
2097 * @param string $key
2098 * @return string
2099 */
2100 private static function maybe_truncate_key_before_appending( $column, $key ) {
2101 if ( in_array( $column, array( 'form_key', 'field_key' ), true ) ) {
2102 $max_key_length_before_truncating = 60;
2103 if ( strlen( $key ) > $max_key_length_before_truncating ) {
2104 $key = substr( $key, 0, $max_key_length_before_truncating );
2105 if ( is_numeric( $key ) ) {
2106 $key .= 'a';
2107 }
2108 }
2109 }
2110 return $key;
2111 }
2112
2113 /**
2114 * Possibly reset a key to avoid conflicts with column size limits.
2115 *
2116 * @param string $key
2117 * @param string $column
2118 * @return string either the original key value, or an empty string if the key was too long.
2119 */
2120 private static function maybe_clear_long_key( $key, $column ) {
2121 if ( 'field_key' === $column && strlen( $key ) >= 70 ) {
2122 $key = '';
2123 }
2124 return $key;
2125 }
2126
2127 /**
2128 * @param int $num_chars
2129 * @return string
2130 */
2131 private static function generate_new_key( $num_chars ) {
2132 $max_slug_value = pow( 36, $num_chars );
2133 $min_slug_value = 37; // we want to have at least 2 characters in the slug
2134 return base_convert( rand( $min_slug_value, $max_slug_value ), 10, 36 );
2135 }
2136
2137 /**
2138 * @param string $key
2139 * @return string
2140 */
2141 private static function prevent_numeric_and_reserved_keys( $key ) {
2142 if ( is_numeric( $key ) ) {
2143 $key .= 'a';
2144 } else {
2145 $not_allowed = array(
2146 'id',
2147 'key',
2148 'created-at',
2149 'detaillink',
2150 'editlink',
2151 'siteurl',
2152 'evenodd',
2153 );
2154 if ( in_array( $key, $not_allowed, true ) ) {
2155 $key .= 'a';
2156 }
2157 }
2158 return $key;
2159 }
2160
2161 /**
2162 * Editing a Form or Entry
2163 *
2164 * @param string $table
2165 *
2166 * @return bool|array
2167 */
2168 public static function setup_edit_vars( $record, $table, $fields = '', $default = false, $post_values = array(), $args = array() ) {
2169 if ( ! $record ) {
2170 return false;
2171 }
2172
2173 if ( empty( $post_values ) ) {
2174 $post_values = wp_unslash( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
2175 }
2176
2177 $values = array(
2178 'id' => $record->id,
2179 'fields' => array(),
2180 );
2181
2182 foreach ( array( 'name', 'description' ) as $var ) {
2183 $default_val = isset( $record->{$var} ) ? $record->{$var} : '';
2184 $values[ $var ] = self::get_param( $var, $default_val, 'get', 'wp_kses_post' );
2185 unset( $var, $default_val );
2186 }
2187
2188 $values['description'] = self::use_wpautop( $values['description'] );
2189
2190 self::fill_form_opts( $record, $table, $post_values, $values );
2191
2192 self::prepare_field_arrays( $fields, $record, $values, array_merge( $args, compact( 'default', 'post_values' ) ) );
2193
2194 if ( $table === 'entries' ) {
2195 $values = FrmEntriesHelper::setup_edit_vars( $values, $record );
2196 } elseif ( $table === 'forms' ) {
2197 $values = FrmFormsHelper::setup_edit_vars( $values, $record, $post_values );
2198 }
2199
2200 return $values;
2201 }
2202
2203 private static function prepare_field_arrays( $fields, $record, array &$values, $args ) {
2204 if ( ! empty( $fields ) ) {
2205 foreach ( (array) $fields as $field ) {
2206 if ( ! self::is_admin_page() ) {
2207 // Don't prep default values on the form settings page.
2208 $field->default_value = apply_filters( 'frm_get_default_value', $field->default_value, $field, true );
2209 }
2210 $args['parent_form_id'] = isset( $args['parent_form_id'] ) ? $args['parent_form_id'] : $field->form_id;
2211 self::fill_field_defaults( $field, $record, $values, $args );
2212 }
2213 }
2214 }
2215
2216 private static function fill_field_defaults( $field, $record, array &$values, $args ) {
2217 $post_values = $args['post_values'];
2218
2219 if ( $args['default'] ) {
2220 $meta_value = $field->default_value;
2221 } else {
2222 if ( $record->post_id && self::pro_is_installed() && isset( $field->field_options['post_field'] ) && $field->field_options['post_field'] ) {
2223 if ( ! isset( $field->field_options['custom_field'] ) ) {
2224 $field->field_options['custom_field'] = '';
2225 }
2226 $meta_value = FrmProEntryMetaHelper::get_post_value(
2227 $record->post_id,
2228 $field->field_options['post_field'],
2229 $field->field_options['custom_field'],
2230 array(
2231 'truncate' => false,
2232 'type' => $field->type,
2233 'form_id' => $field->form_id,
2234 'field' => $field,
2235 )
2236 );
2237 } else {
2238 $meta_value = FrmEntryMeta::get_meta_value( $record, $field->id );
2239 }
2240 }
2241
2242 $field_type = isset( $post_values['field_options'][ 'type_' . $field->id ] ) ? $post_values['field_options'][ 'type_' . $field->id ] : $field->type;
2243 if ( isset( $post_values['item_meta'][ $field->id ] ) ) {
2244 $new_value = $post_values['item_meta'][ $field->id ];
2245 self::unserialize_or_decode( $new_value );
2246 } else {
2247 $new_value = $meta_value;
2248 }
2249
2250 $field_array = self::start_field_array( $field );
2251 $field_array['value'] = $new_value;
2252 $field_array['type'] = apply_filters( 'frm_field_type', $field_type, $field, $new_value );
2253 $field_array['parent_form_id'] = $args['parent_form_id'];
2254
2255 $args['field_type'] = $field_type;
2256
2257 FrmFieldsHelper::prepare_edit_front_field( $field_array, $field, $values['id'], $args );
2258
2259 if ( ! isset( $field_array['unique'] ) || ! $field_array['unique'] ) {
2260 $field_array['unique_msg'] = '';
2261 }
2262
2263 $field_array = array_merge( (array) $field->field_options, $field_array );
2264
2265 $values['fields'][ $field->id ] = $field_array;
2266 }
2267
2268 /**
2269 * @since 3.0
2270 *
2271 * @param object $field
2272 *
2273 * @return array
2274 */
2275 public static function start_field_array( $field ) {
2276 return array(
2277 'id' => $field->id,
2278 'default_value' => $field->default_value,
2279 'name' => $field->name,
2280 'description' => $field->description,
2281 'options' => $field->options,
2282 'required' => $field->required,
2283 'field_key' => $field->field_key,
2284 'field_order' => $field->field_order,
2285 'form_id' => $field->form_id,
2286 );
2287 }
2288
2289 /**
2290 * @param string $table
2291 */
2292 private static function fill_form_opts( $record, $table, $post_values, array &$values ) {
2293 if ( $table == 'entries' ) {
2294 $form = $record->form_id;
2295 FrmForm::maybe_get_form( $form );
2296 } else {
2297 $form = $record;
2298 }
2299
2300 if ( ! $form ) {
2301 return;
2302 }
2303
2304 $values['form_name'] = isset( $record->form_id ) ? $form->name : '';
2305 $values['parent_form_id'] = isset( $record->form_id ) ? $form->parent_form_id : 0;
2306
2307 if ( ! is_array( $form->options ) ) {
2308 return;
2309 }
2310
2311 foreach ( $form->options as $opt => $value ) {
2312 if ( isset( $post_values[ $opt ] ) ) {
2313 $values[ $opt ] = $post_values[ $opt ];
2314 self::unserialize_or_decode( $values[ $opt ] );
2315 } else {
2316 $values[ $opt ] = $value;
2317 }
2318 }
2319
2320 self::fill_form_defaults( $post_values, $values );
2321 }
2322
2323 /**
2324 * Set to POST value or default
2325 */
2326 private static function fill_form_defaults( $post_values, array &$values ) {
2327 $form_defaults = FrmFormsHelper::get_default_opts();
2328
2329 foreach ( $form_defaults as $opt => $default ) {
2330 if ( ! isset( $values[ $opt ] ) || $values[ $opt ] == '' ) {
2331 $values[ $opt ] = ( $post_values && isset( $post_values['options'][ $opt ] ) ) ? $post_values['options'][ $opt ] : $default;
2332 }
2333
2334 unset( $opt, $default );
2335 }
2336
2337 if ( ! isset( $values['custom_style'] ) ) {
2338 $values['custom_style'] = self::custom_style_value( $post_values );
2339 }
2340
2341 foreach ( array( 'before', 'after', 'submit' ) as $h ) {
2342 if ( ! isset( $values[ $h . '_html' ] ) ) {
2343 $values[ $h . '_html' ] = ( isset( $post_values['options'][ $h . '_html' ] ) ? $post_values['options'][ $h . '_html' ] : FrmFormsHelper::get_default_html( $h ) );
2344 }
2345 unset( $h );
2346 }
2347 }
2348
2349 /**
2350 * @since 2.2.10
2351 *
2352 * @param array $post_values
2353 *
2354 * @return boolean|int
2355 */
2356 public static function custom_style_value( $post_values ) {
2357 if ( ! empty( $post_values ) && isset( $post_values['options']['custom_style'] ) ) {
2358 $custom_style = absint( $post_values['options']['custom_style'] );
2359 } else {
2360 $frm_settings = self::get_settings();
2361 $custom_style = ( $frm_settings->load_style != 'none' );
2362 }
2363
2364 return $custom_style;
2365 }
2366
2367 public static function truncate( $str, $length, $minword = 3, $continue = '...' ) {
2368 if ( is_array( $str ) ) {
2369 return '';
2370 }
2371
2372 $length = (int) $length;
2373 $str = wp_strip_all_tags( $str );
2374 $original_len = self::mb_function( array( 'mb_strlen', 'strlen' ), array( $str ) );
2375
2376 if ( $length == 0 ) {
2377 return '';
2378 } elseif ( $length <= 10 ) {
2379 $sub = self::mb_function( array( 'mb_substr', 'substr' ), array( $str, 0, $length ) );
2380
2381 return $sub . ( ( $length < $original_len ) ? $continue : '' );
2382 }
2383
2384 $sub = '';
2385 $len = 0;
2386
2387 $words = self::mb_function( array( 'mb_split', 'explode' ), array( ' ', $str ) );
2388
2389 foreach ( $words as $word ) {
2390 $part = ( ( $sub != '' ) ? ' ' : '' ) . $word;
2391 $total_len = self::mb_function( array( 'mb_strlen', 'strlen' ), array( $sub . $part ) );
2392 if ( $total_len > $length && substr_count( $sub, ' ' ) ) {
2393 break;
2394 }
2395
2396 $sub .= $part;
2397 $len += self::mb_function( array( 'mb_strlen', 'strlen' ), array( $part ) );
2398
2399 if ( substr_count( $sub, ' ' ) > $minword && $total_len >= $length ) {
2400 break;
2401 }
2402
2403 unset( $total_len, $word );
2404 }
2405
2406 return $sub . ( ( $len < $original_len ) ? $continue : '' );
2407 }
2408
2409 public static function mb_function( $function_names, $args ) {
2410 $mb_function_name = $function_names[0];
2411 $function_name = $function_names[1];
2412 if ( function_exists( $mb_function_name ) ) {
2413 $function_name = $mb_function_name;
2414 }
2415
2416 return call_user_func_array( $function_name, $args );
2417 }
2418
2419 public static function get_formatted_time( $date, $date_format = '', $time_format = '' ) {
2420 if ( empty( $date ) ) {
2421 return $date;
2422 }
2423
2424 if ( empty( $date_format ) ) {
2425 $date_format = get_option( 'date_format' );
2426 }
2427
2428 if ( preg_match( '/^\d{1-2}\/\d{1-2}\/\d{4}$/', $date ) && self::pro_is_installed() ) {
2429 $frmpro_settings = new FrmProSettings();
2430 $date = FrmProAppHelper::convert_date( $date, $frmpro_settings->date_format, 'Y-m-d' );
2431 }
2432
2433 $formatted = self::get_localized_date( $date_format, $date );
2434
2435 $do_time = ( gmdate( 'H:i:s', strtotime( $date ) ) != '00:00:00' );
2436 if ( $do_time ) {
2437 $formatted .= self::add_time_to_date( $time_format, $date );
2438 }
2439
2440 return $formatted;
2441 }
2442
2443 private static function add_time_to_date( $time_format, $date ) {
2444 if ( empty( $time_format ) ) {
2445 $time_format = get_option( 'time_format' );
2446 }
2447
2448 $trimmed_format = trim( $time_format );
2449 $time = '';
2450 if ( $time_format && ! empty( $trimmed_format ) ) {
2451 $time = ' ' . __( 'at', 'formidable' ) . ' ' . self::get_localized_date( $time_format, $date );
2452 }
2453
2454 return $time;
2455 }
2456
2457 /**
2458 * @since 2.0.8
2459 */
2460 public static function get_localized_date( $date_format, $date ) {
2461 $date = get_date_from_gmt( $date );
2462
2463 return date_i18n( $date_format, strtotime( $date ) );
2464 }
2465
2466 /**
2467 * Gets the time ago in words
2468 *
2469 * @param int $from in seconds
2470 * @param int|string $to in seconds
2471 *
2472 * @return string $time_ago
2473 */
2474 public static function human_time_diff( $from, $to = '', $levels = 1 ) {
2475 if ( empty( $to ) && 0 !== $to ) {
2476 $now = new DateTime();
2477 } else {
2478 $now = new DateTime( '@' . $to );
2479 }
2480 $ago = new DateTime( '@' . $from );
2481
2482 // Get the time difference
2483 $diff_object = $now->diff( $ago );
2484 $diff = get_object_vars( $diff_object );
2485
2486 // Add week amount and update day amount
2487 $diff['w'] = floor( $diff['d'] / 7 );
2488 $diff['d'] -= $diff['w'] * 7;
2489
2490 $time_strings = self::get_time_strings();
2491
2492 if ( ! is_numeric( $levels ) ) {
2493 // Show time in specified unit.
2494 $levels = self::get_unit( $levels );
2495 if ( isset( $time_strings[ $levels ] ) ) {
2496 $diff = array(
2497 $levels => self::time_format( $levels, $diff ),
2498 );
2499 $time_strings = array(
2500 $levels => $time_strings[ $levels ],
2501 );
2502 }
2503 $levels = 1;
2504 }
2505
2506 foreach ( $time_strings as $k => $v ) {
2507 if ( isset( $diff[ $k ] ) && $diff[ $k ] ) {
2508 $time_strings[ $k ] = $diff[ $k ] . ' ' . ( $diff[ $k ] > 1 ? $v[1] : $v[0] );
2509 } elseif ( isset( $diff[ $k ] ) && count( $time_strings ) === 1 ) {
2510 // Account for 0.
2511 $time_strings[ $k ] = $diff[ $k ] . ' ' . $v[1];
2512 } else {
2513 unset( $time_strings[ $k ] );
2514 }
2515 }
2516
2517 $levels_deep = apply_filters( 'frm_time_ago_levels', $levels, compact( 'time_strings', 'from', 'to' ) );
2518 $time_strings = array_slice( $time_strings, 0, absint( $levels_deep ) );
2519 $time_ago_string = implode( ' ', $time_strings );
2520
2521 return $time_ago_string;
2522 }
2523
2524 /**
2525 * @since 4.05.01
2526 */
2527 private static function time_format( $unit, $diff ) {
2528 $return = array(
2529 'y' => 'y',
2530 'd' => 'days',
2531 );
2532 if ( isset( $return[ $unit ] ) ) {
2533 return $diff[ $return[ $unit ] ];
2534 }
2535
2536 $total = $diff['days'] * self::convert_time( 'd', $unit );
2537
2538 $times = array( 'h', 'i', 's' );
2539
2540 foreach ( $times as $time ) {
2541 if ( ! isset( $diff[ $time ] ) ) {
2542 continue;
2543 }
2544
2545 $total += $diff[ $time ] * self::convert_time( $time, $unit );
2546 }
2547
2548 return floor( $total );
2549 }
2550
2551 /**
2552 * @since 4.05.01
2553 */
2554 private static function convert_time( $from, $to ) {
2555 $convert = array(
2556 's' => 1,
2557 'i' => MINUTE_IN_SECONDS,
2558 'h' => HOUR_IN_SECONDS,
2559 'd' => DAY_IN_SECONDS,
2560 'w' => WEEK_IN_SECONDS,
2561 'm' => DAY_IN_SECONDS * 30.42,
2562 'y' => DAY_IN_SECONDS * 365.25,
2563 );
2564
2565 return $convert[ $from ] / $convert[ $to ];
2566 }
2567
2568 /**
2569 * @since 4.05.01
2570 */
2571 private static function get_unit( $unit ) {
2572 $units = self::get_time_strings();
2573 if ( isset( $units[ $unit ] ) || is_numeric( $unit ) ) {
2574 return $unit;
2575 }
2576
2577 foreach ( $units as $u => $strings ) {
2578 if ( in_array( $unit, $strings ) ) {
2579 return $u;
2580 }
2581 }
2582 return 1;
2583 }
2584
2585 /**
2586 * Get the translatable time strings. The untranslated version is a failsafe
2587 * in case langauges are changing for the unit set in the shortcode.
2588 *
2589 * @since 2.0.20
2590 * @return array
2591 */
2592 private static function get_time_strings() {
2593 return array(
2594 'y' => array(
2595 __( 'year', 'formidable' ),
2596 __( 'years', 'formidable' ),
2597 'year',
2598 ),
2599 'm' => array(
2600 __( 'month', 'formidable' ),
2601 __( 'months', 'formidable' ),
2602 'month',
2603 ),
2604 'w' => array(
2605 __( 'week', 'formidable' ),
2606 __( 'weeks', 'formidable' ),
2607 'week',
2608 ),
2609 'd' => array(
2610 __( 'day', 'formidable' ),
2611 __( 'days', 'formidable' ),
2612 'day',
2613 ),
2614 'h' => array(
2615 __( 'hour', 'formidable' ),
2616 __( 'hours', 'formidable' ),
2617 'hour',
2618 ),
2619 'i' => array(
2620 __( 'minute', 'formidable' ),
2621 __( 'minutes', 'formidable' ),
2622 'minute',
2623 ),
2624 's' => array(
2625 __( 'second', 'formidable' ),
2626 __( 'seconds', 'formidable' ),
2627 'second',
2628 ),
2629 );
2630 }
2631
2632 // Pagination Methods.
2633
2634 /**
2635 * @param integer $current_p
2636 */
2637 public static function get_last_record_num( $r_count, $current_p, $p_size ) {
2638 return ( ( $r_count < ( $current_p * $p_size ) ) ? $r_count : ( $current_p * $p_size ) );
2639 }
2640
2641 /**
2642 * @param integer $current_p
2643 */
2644 public static function get_first_record_num( $r_count, $current_p, $p_size ) {
2645 if ( $current_p == 1 ) {
2646 return 1;
2647 } else {
2648 return ( self::get_last_record_num( $r_count, ( $current_p - 1 ), $p_size ) + 1 );
2649 }
2650 }
2651
2652 /**
2653 * @return array
2654 */
2655 public static function json_to_array( $json_vars ) {
2656 $vars = array();
2657 foreach ( $json_vars as $jv ) {
2658 $jv_name = explode( '[', $jv['name'] );
2659 $last = count( $jv_name ) - 1;
2660 foreach ( $jv_name as $p => $n ) {
2661 $name = trim( $n, ']' );
2662 if ( ! isset( $l1 ) ) {
2663 $l1 = $name;
2664 }
2665
2666 if ( ! isset( $l2 ) ) {
2667 $l2 = $name;
2668 }
2669
2670 if ( ! isset( $l3 ) ) {
2671 $l3 = $name;
2672 }
2673
2674 $this_val = ( $p == $last ) ? $jv['value'] : array();
2675
2676 switch ( $p ) {
2677 case 0:
2678 $l1 = $name;
2679 self::add_value_to_array( $name, $l1, $this_val, $vars );
2680 break;
2681
2682 case 1:
2683 $l2 = $name;
2684 self::add_value_to_array( $name, $l2, $this_val, $vars[ $l1 ] );
2685 break;
2686
2687 case 2:
2688 $l3 = $name;
2689 self::add_value_to_array( $name, $l3, $this_val, $vars[ $l1 ][ $l2 ] );
2690 break;
2691
2692 case 3:
2693 $l4 = $name;
2694 self::add_value_to_array( $name, $l4, $this_val, $vars[ $l1 ][ $l2 ][ $l3 ] );
2695 }
2696
2697 unset( $this_val, $n );
2698 }
2699
2700 unset( $last, $jv );
2701 }
2702
2703 return $vars;
2704 }
2705
2706 /**
2707 * @param string $name
2708 * @param string $l1
2709 */
2710 public static function add_value_to_array( $name, $l1, $val, &$vars ) {
2711 if ( $name == '' ) {
2712 $vars[] = $val;
2713 } elseif ( ! isset( $vars[ $l1 ] ) ) {
2714 $vars[ $l1 ] = $val;
2715 }
2716 }
2717
2718 public static function maybe_add_tooltip( $name, $class = 'closed', $form_name = '' ) {
2719 $tooltips = array(
2720 'action_title' => __( 'Give this action a label for easy reference.', 'formidable' ),
2721 'email_to' => __( 'Add one or more recipient addresses separated by a ",". FORMAT: Name <name@email.com> or name@email.com. [admin_email] is the address set in WP General Settings.', 'formidable' ),
2722 'cc' => __( 'Add CC addresses separated by a ",". FORMAT: Name <name@email.com> or name@email.com.', 'formidable' ),
2723 'bcc' => __( 'Add BCC addresses separated by a ",". FORMAT: Name <name@email.com> or name@email.com.', 'formidable' ),
2724 'reply_to' => __( 'If you would like a different reply to address than the "from" address, add a single address here. FORMAT: Name <name@email.com> or name@email.com.', 'formidable' ),
2725 'from' => __( 'Enter the name and/or email address of the sender. FORMAT: John Bates <john@example.com> or john@example.com.', 'formidable' ),
2726 /* translators: %1$s: Form name, %2$s: Date */
2727 'email_subject' => esc_attr( sprintf( __( 'If you leave the subject blank, the default will be used: %1$s Form submitted on %2$s', 'formidable' ), $form_name, self::site_name() ) ),
2728 'new_tab' => __( 'This option will open the link in a new browser tab. Please note that some popup blockers may prevent this from happening, in which case the link will be displayed.', 'formidable' ),
2729 );
2730
2731 if ( ! isset( $tooltips[ $name ] ) ) {
2732 return;
2733 }
2734
2735 if ( 'open' == $class ) {
2736 echo ' frm_help"';
2737 } else {
2738 echo ' class="frm_help"';
2739 }
2740
2741 echo ' title="' . esc_attr( $tooltips[ $name ] );
2742
2743 if ( 'open' != $class ) {
2744 echo '"';
2745 }
2746 }
2747
2748 /**
2749 * Add the current_page class to that page in the form nav
2750 */
2751 public static function select_current_page( $page, $current_page, $action = array() ) {
2752 if ( $current_page != $page ) {
2753 return;
2754 }
2755
2756 $frm_action = self::simple_get( 'frm_action', 'sanitize_title' );
2757 if ( 'lite-reports' === $frm_action ) {
2758 $frm_action = 'reports';
2759 }
2760
2761 if ( empty( $action ) || ( ! empty( $frm_action ) && in_array( $frm_action, $action ) ) ) {
2762 echo ' class="current_page"';
2763 }
2764 }
2765
2766 /**
2767 * Prepare and json_encode post content
2768 *
2769 * @since 2.0
2770 *
2771 * @param array $post_content
2772 *
2773 * @return string $post_content ( json encoded array )
2774 */
2775 public static function prepare_and_encode( $post_content ) {
2776 // Loop through array to strip slashes and add only the needed ones.
2777 foreach ( $post_content as $key => $val ) {
2778 // Replace problematic characters (like &quot;)
2779 if ( is_string( $val ) ) {
2780 $val = str_replace( '&quot;', '"', $val );
2781 }
2782
2783 self::prepare_action_slashes( $val, $key, $post_content );
2784 unset( $key, $val );
2785 }
2786
2787 // json_encode the array.
2788 $post_content = json_encode( $post_content );
2789
2790 // Add extra slashes for \r\n since WP strips them.
2791 $post_content = str_replace( array( '\\r', '\\n', '\\u', '\\t' ), array( '\\\\r', '\\\\n', '\\\\u', '\\\\t' ), $post_content );
2792
2793 // allow for &quot
2794 $post_content = str_replace( '&quot;', '\\"', $post_content );
2795
2796 return $post_content;
2797 }
2798
2799 private static function prepare_action_slashes( $val, $key, &$post_content ) {
2800 if ( ! isset( $post_content[ $key ] ) || is_numeric( $val ) ) {
2801 return;
2802 }
2803
2804 if ( is_array( $val ) ) {
2805 foreach ( $val as $k1 => $v1 ) {
2806 self::prepare_action_slashes( $v1, $k1, $post_content[ $key ] );
2807 unset( $k1, $v1 );
2808 }
2809 } else {
2810 // Strip all slashes so everything is the same, no matter where the value is coming from
2811 $val = stripslashes( $val );
2812
2813 // Add backslashes before double quotes and forward slashes only
2814 $post_content[ $key ] = addcslashes( $val, '"\\/' );
2815 }
2816 }
2817
2818 /**
2819 * Check for either json or serialized data. This is temporary while transitioning
2820 * all data to json.
2821 *
2822 * @since 4.02.03
2823 *
2824 * @param array|string $value
2825 * @return void
2826 */
2827 public static function unserialize_or_decode( &$value ) {
2828 if ( is_array( $value ) ) {
2829 return;
2830 }
2831
2832 if ( is_serialized( $value ) ) {
2833 $value = self::maybe_unserialize_array( $value );
2834 } else {
2835 $value = self::maybe_json_decode( $value, false );
2836 }
2837 }
2838
2839 /**
2840 * Safely unserialize an array if necessary.
2841 * This function doesn't actually use unserialize. The string is parsed instead.
2842 *
2843 * @since 6.2
2844 *
2845 * @param mixed $value
2846 * @return mixed
2847 */
2848 public static function maybe_unserialize_array( $value ) {
2849 if ( ! is_string( $value ) ) {
2850 return $value;
2851 }
2852
2853 // Since we only expect an array, skip anything that doesn't start with a:.
2854 if ( ! is_serialized( $value ) || 'a:' !== substr( $value, 0, 2 ) ) {
2855 return $value;
2856 }
2857
2858 $parsed = FrmSerializedStringParserHelper::get()->parse( $value );
2859 if ( is_array( $parsed ) ) {
2860 $value = $parsed;
2861 }
2862
2863 return $value;
2864 }
2865
2866 /**
2867 * Decode a JSON string.
2868 * Do not switch shortcodes like [24] to array unless intentional ie XML values.
2869 *
2870 * @param mixed $string
2871 * @param bool $single_to_array
2872 * @return mixed
2873 */
2874 public static function maybe_json_decode( $string, $single_to_array = true ) {
2875 if ( is_array( $string ) || is_null( $string ) ) {
2876 return $string;
2877 }
2878
2879 $new_string = json_decode( $string, true );
2880 if ( function_exists( 'json_last_error' ) ) {
2881 // php 5.3+
2882 $single_value = false;
2883 if ( ! $single_to_array ) {
2884 $single_value = is_array( $new_string ) && count( $new_string ) === 1 && isset( $new_string[0] );
2885 }
2886 if ( json_last_error() == JSON_ERROR_NONE && is_array( $new_string ) && ! $single_value ) {
2887 $string = $new_string;
2888 }
2889 }
2890
2891 return $string;
2892 }
2893
2894 /**
2895 * @since 6.2.3
2896 *
2897 * @param string $value
2898 * @return string
2899 */
2900 public static function maybe_utf8_encode( $value ) {
2901 $from_format = 'ISO-8859-1';
2902 $to_format = 'UTF-8';
2903
2904 if ( function_exists( 'mb_check_encoding' ) && function_exists( 'mb_convert_encoding' ) ) {
2905 if ( mb_check_encoding( $value, $from_format ) ) {
2906 return mb_convert_encoding( $value, $to_format, $from_format );
2907 }
2908 return $value;
2909 }
2910
2911 if ( function_exists( 'iconv' ) ) {
2912 $converted = iconv( $from_format, $to_format, $value );
2913 // Value is false if $value is not ISO-8859-1.
2914 if ( false !== $converted ) {
2915 return $converted;
2916 }
2917 }
2918
2919 return $value;
2920 }
2921
2922 /**
2923 * Reformat the json serialized array in name => value array.
2924 *
2925 * @since 4.02.03
2926 */
2927 public static function format_form_data( &$form ) {
2928 $formatted = array();
2929
2930 foreach ( $form as $input ) {
2931 if ( ! isset( $input['name'] ) ) {
2932 continue;
2933 }
2934 $key = $input['name'];
2935 if ( isset( $formatted[ $key ] ) ) {
2936 if ( is_array( $formatted[ $key ] ) ) {
2937 $formatted[ $key ][] = $input['value'];
2938 } else {
2939 $formatted[ $key ] = array( $formatted[ $key ], $input['value'] );
2940 }
2941 } else {
2942 $formatted[ $key ] = $input['value'];
2943 }
2944 }
2945
2946 parse_str( http_build_query( $formatted ), $form );
2947 }
2948
2949 /**
2950 * @since 4.02.03
2951 */
2952 public static function maybe_json_encode( $value ) {
2953 if ( is_array( $value ) ) {
2954 $value = wp_json_encode( $value );
2955 }
2956 return $value;
2957 }
2958
2959 /**
2960 * @since 1.07.10
2961 *
2962 * @param string $post_type The name of the post type that may need to be highlighted
2963 * echo The javascript to open and highlight the Formidable menu
2964 */
2965 public static function maybe_highlight_menu( $post_type ) {
2966 global $post;
2967
2968 if ( isset( $_REQUEST['post_type'] ) && $_REQUEST['post_type'] != $post_type ) {
2969 return;
2970 }
2971
2972 if ( is_object( $post ) && $post->post_type != $post_type ) {
2973 return;
2974 }
2975
2976 self::load_admin_wide_js();
2977 echo '<script type="text/javascript">jQuery(document).ready(function(){frmSelectSubnav();});</script>';
2978 }
2979
2980 /**
2981 * Load the JS file on non-Formidable pages in the admin area
2982 *
2983 * @since 2.0
2984 */
2985 public static function load_admin_wide_js( $load = true ) {
2986 $version = self::plugin_version();
2987 wp_register_script( 'formidable_admin_global', self::plugin_url() . '/js/formidable_admin_global.js', array( 'jquery' ), $version );
2988
2989 $global_strings = array(
2990 'updating_msg' => __( 'Please wait while your site updates.', 'formidable' ),
2991 'deauthorize' => __( 'Are you sure you want to deauthorize Formidable Forms on this site?', 'formidable' ),
2992 'url' => self::plugin_url(),
2993 'app_url' => 'https://formidableforms.com/',
2994 'applicationsUrl' => admin_url( 'admin.php?page=formidable-applications' ),
2995 'canAccessApplicationDashboard' => current_user_can( is_callable( 'FrmProApplicationsHelper::get_required_templates_capability' ) ? FrmProApplicationsHelper::get_required_templates_capability() : 'frm_edit_forms' ),
2996 'loading' => __( 'Loading&hellip;', 'formidable' ),
2997 'nonce' => wp_create_nonce( 'frm_ajax' ),
2998 'proIncludesSliderJs' => is_callable( 'FrmProFormsHelper::prepare_custom_currency' ),
2999 );
3000 wp_localize_script( 'formidable_admin_global', 'frmGlobal', $global_strings );
3001
3002 if ( $load ) {
3003 wp_enqueue_script( 'formidable_admin_global' );
3004 }
3005 }
3006
3007 /**
3008 * @since 2.0.9
3009 */
3010 public static function load_font_style() {
3011 wp_enqueue_style( 'frm_fonts', self::plugin_url() . '/css/frm_fonts.css', array(), self::plugin_version() );
3012 }
3013
3014 /**
3015 * @param string $location
3016 */
3017 public static function localize_script( $location ) {
3018 global $wp_scripts;
3019
3020 $script_strings = array(
3021 'ajax_url' => esc_url_raw( self::get_ajax_url() ),
3022 'images_url' => self::plugin_url() . '/images',
3023 'loading' => __( 'Loading&hellip;', 'formidable' ),
3024 'remove' => __( 'Remove', 'formidable' ),
3025 'offset' => apply_filters( 'frm_scroll_offset', 4 ),
3026 'nonce' => wp_create_nonce( 'frm_ajax' ),
3027 'id' => __( 'ID', 'formidable' ),
3028 'no_results' => __( 'No results match', 'formidable' ),
3029 'file_spam' => __( 'That file looks like Spam.', 'formidable' ),
3030 'calc_error' => __( 'There is an error in the calculation in the field with key', 'formidable' ),
3031 'empty_fields' => __( 'Please complete the preceding required fields before uploading a file.', 'formidable' ),
3032 'focus_first_error' => self::should_focus_first_error(),
3033 'include_alert_role' => self::should_include_alert_role_on_field_errors(),
3034 );
3035
3036 $data = $wp_scripts->get_data( 'formidable', 'data' );
3037 if ( ! $data ) {
3038 wp_localize_script( 'formidable', 'frm_js', $script_strings );
3039 }
3040
3041 if ( $location === 'admin' ) {
3042 $frm_settings = self::get_settings();
3043 $admin_script_strings = array(
3044 'desc' => __( '(Click to add description)', 'formidable' ),
3045 'blank' => __( '(Blank)', 'formidable' ),
3046 'no_label' => __( '(no label)', 'formidable' ),
3047 'ok' => __( 'OK', 'formidable' ),
3048 'cancel' => __( 'Cancel', 'formidable' ),
3049 'default_label' => __( 'Default', 'formidable' ),
3050 'clear_default' => __( 'Clear default value when typing', 'formidable' ),
3051 'no_clear_default' => __( 'Do not clear default value when typing', 'formidable' ),
3052 'valid_default' => __( 'Default value will pass form validation', 'formidable' ),
3053 'no_valid_default' => __( 'Default value will NOT pass form validation', 'formidable' ),
3054 'confirm' => __( 'Are you sure?', 'formidable' ),
3055 'conf_delete' => __( 'Are you sure you want to delete this field and all data associated with it?', 'formidable' ),
3056 'conf_delete_sec' => __( 'All fields inside this Section will be deleted along with their data. Are you sure you want to delete this group of fields?', 'formidable' ),
3057 'conf_no_repeat' => __( 'Warning: If you have entries with multiple rows, all but the first row will be lost.', 'formidable' ),
3058 'default_unique' => $frm_settings->unique_msg,
3059 'default_conf' => __( 'The entered values do not match', 'formidable' ),
3060 'enter_email' => __( 'Enter Email', 'formidable' ),
3061 'confirm_email' => __( 'Confirm Email', 'formidable' ),
3062 'conditional_text' => __( 'Conditional content here', 'formidable' ),
3063 'new_option' => __( 'New Option', 'formidable' ),
3064 'css_invalid_size' => __( 'In certain browsers (e.g. Firefox) text will not display correctly if the field height is too small relative to the field padding and text size. Please increase your field height or decrease your field padding.', 'formidable' ),
3065 'enter_password' => __( 'Enter Password', 'formidable' ),
3066 'confirm_password' => __( 'Confirm Password', 'formidable' ),
3067 'import_complete' => __( 'Import Complete', 'formidable' ),
3068 'updating' => __( 'Please wait while your site updates.', 'formidable' ),
3069 'no_save_warning' => __( 'Warning: There is no way to retrieve unsaved entries.', 'formidable' ),
3070 'private_label' => __( 'Private', 'formidable' ),
3071 'jquery_ui_url' => '',
3072 'pro_url' => is_callable( 'FrmProAppHelper::plugin_url' ) ? FrmProAppHelper::plugin_url() : '',
3073 'no_licenses' => __( 'No new licenses were found', 'formidable' ),
3074 'unmatched_parens' => __( 'This calculation has at least one unmatched ( ) { } [ ].', 'formidable' ),
3075 'view_shortcodes' => __( 'This calculation may have shortcodes that work in Views but not forms.', 'formidable' ),
3076 'text_shortcodes' => __( 'This calculation may have shortcodes that work in text calculations but not numeric calculations.', 'formidable' ),
3077 'only_one_action' => __( 'This form action is limited to one per form. Please edit the existing form action.', 'formidable' ),
3078 'unsafe_params' => FrmFormsHelper::reserved_words(),
3079 /* Translators: %s is the name of a Detail Page Slug that is a reserved word.*/
3080 'slug_is_reserved' => sprintf( __( 'The Detail Page Slug "%s" is reserved by WordPress. This may cause problems. Is this intentional?', 'formidable' ), '****' ),
3081 /* Translators: %s is the name of a parameter that is a reserved word. More than one word could be listed here, though that would not be common. */
3082 'param_is_reserved' => sprintf( __( 'The parameter "%s" is reserved by WordPress. This may cause problems when included in the URL. Is this intentional? ', 'formidable' ), '****' ),
3083 'reserved_words' => __( 'See the list of reserved words in WordPress.', 'formidable' ),
3084 'repeat_limit_min' => __( 'Please enter a Repeat Limit that is greater than 1.', 'formidable' ),
3085 'checkbox_limit' => __( 'Please select a limit between 0 and 200.', 'formidable' ),
3086 'install' => __( 'Install', 'formidable' ),
3087 'active' => __( 'Active', 'formidable' ),
3088 'select_a_field' => __( 'Select a Field', 'formidable' ),
3089 'no_items_found' => __( 'No items found.', 'formidable' ),
3090 'field_already_used' => __( 'Oops. You have already used that field.', 'formidable' ),
3091 'saving' => '', // Deprecated in 6.0.
3092 'saved' => '', // Deprecated in 6.0.
3093 );
3094 /**
3095 * @param array $admin_script_strings
3096 */
3097 $admin_script_strings = apply_filters( 'frm_admin_script_strings', $admin_script_strings );
3098
3099 $data = $wp_scripts->get_data( 'formidable_admin', 'data' );
3100 if ( ! $data ) {
3101 wp_localize_script( 'formidable_admin', 'frm_admin_js', $admin_script_strings );
3102 }
3103 }
3104 }
3105
3106 /**
3107 * @since 6.5
3108 *
3109 * @return string
3110 */
3111 public static function get_ajax_url() {
3112 $ajax_url = admin_url( 'admin-ajax.php', is_ssl() ? 'admin' : 'http' );
3113
3114 /**
3115 * @since 2.0.13
3116 *
3117 * @param string $ajax_url
3118 */
3119 return apply_filters( 'frm_ajax_url', $ajax_url );
3120 }
3121
3122 /**
3123 * Returns whether or not the first errored input should be auto-focused (default true).
3124 *
3125 * @since 5.2.05
3126 *
3127 * @return bool
3128 */
3129 private static function should_focus_first_error() {
3130 return (bool) apply_filters( 'frm_focus_first_error', true );
3131 }
3132
3133 /**
3134 * Returns whether or not field errors should include role="alert" (default true).
3135 *
3136 * @since 5.2.05
3137 *
3138 * @return bool
3139 */
3140 public static function should_include_alert_role_on_field_errors() {
3141 return (bool) apply_filters( 'frm_include_alert_role_on_field_errors', true );
3142 }
3143
3144 /**
3145 * Echo the message on the plugins listing page
3146 *
3147 * @since 1.07.10
3148 *
3149 * @param float $min_version The version the add-on requires
3150 */
3151 public static function min_version_notice( $min_version ) {
3152 $frm_version = self::plugin_version();
3153
3154 // Check if Formidable meets minimum requirements.
3155 if ( version_compare( $frm_version, $min_version, '>=' ) ) {
3156 return;
3157 }
3158
3159 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
3160 echo '<tr class="plugin-update-tr active"><th colspan="' . absint( $wp_list_table->get_column_count() ) . '" class="check-column plugin-update colspanchange"><div class="update-message">' .
3161 esc_html__( 'You are running an outdated version of Formidable. This plugin may not work correctly if you do not update Formidable.', 'formidable' ) .
3162 '</div></td></tr>';
3163 }
3164
3165 /**
3166 * If Pro is far outdated, show a message.
3167 *
3168 * @since 4.0.01
3169 *
3170 * @return void
3171 */
3172 public static function min_pro_version_notice( $min_version ) {
3173 if ( ! self::is_formidable_admin() ) {
3174 // Don't show admin-wide.
3175 return;
3176 }
3177
3178 self::php_version_notice();
3179
3180 $is_pro = self::pro_is_installed() && class_exists( 'FrmProDb' );
3181 if ( ! $is_pro || self::meets_min_pro_version( $min_version ) ) {
3182 return;
3183 }
3184
3185 $pro_version = FrmProDb::$plug_version;
3186 $expired = FrmAddonsController::is_license_expired();
3187 ?>
3188 <div class="frm-banner-alert frm_error_style frm_previous_install">
3189 <?php
3190 esc_html_e( 'You are running a version of Formidable Forms that may not be compatible with your version of Formidable Forms Pro.', 'formidable' );
3191 if ( empty( $expired ) ) {
3192 echo ' Please <a href="' . esc_url( admin_url( 'plugins.php?s=formidable%20forms%20pro' ) ) . '">update now</a>.';
3193 } else {
3194 echo '<br/>Please <a href="https://formidableforms.com/account/downloads/?utm_source=WordPress&utm_medium=outdated">renew now</a> to get the latest version.';
3195 }
3196 ?>
3197 </div>
3198 <?php
3199 }
3200
3201 /**
3202 * If Pro is installed, check the version number.
3203 *
3204 * @since 4.0.01
3205 */
3206 public static function meets_min_pro_version( $min_version ) {
3207 return ! class_exists( 'FrmProDb' ) || version_compare( FrmProDb::$plug_version, $min_version, '>=' );
3208 }
3209
3210 /**
3211 * Show a message if the browser or PHP version is below the recommendations.
3212 *
3213 * @since 4.0.02
3214 */
3215 private static function php_version_notice() {
3216 $message = array();
3217 if ( version_compare( phpversion(), '5.6', '<' ) ) {
3218 $message[] = __( 'The version of PHP on your server is too low. If this is not corrected, you may see issues with Formidable Forms. Please contact your web host and ask to be updated to PHP 7.0+.', 'formidable' );
3219 }
3220
3221 $browser = self::get_server_value( 'HTTP_USER_AGENT' );
3222 $is_ie = strpos( $browser, 'MSIE' ) !== false;
3223 if ( $is_ie ) {
3224 $message[] = __( 'You are using an outdated browser that is not compatible with Formidable Forms. Please update to a more current browser (we recommend Chrome).', 'formidable' );
3225 }
3226
3227 foreach ( $message as $m ) {
3228 ?>
3229 <div class="frm-banner-alert frm_error_style frm_previous_install">
3230 <?php echo esc_html( $m ); ?>
3231 </div>
3232 <?php
3233 }
3234 }
3235
3236 /**
3237 * @param string $type
3238 * @return array<string,string>
3239 */
3240 public static function locales( $type = 'date' ) {
3241 $locales = array(
3242 'en' => __( 'English', 'formidable' ),
3243 'af' => __( 'Afrikaans', 'formidable' ),
3244 'sq' => __( 'Albanian', 'formidable' ),
3245 'ar-DZ' => __( 'Algerian Arabic', 'formidable' ),
3246 'am' => __( 'Amharic', 'formidable' ),
3247 'ar' => __( 'Arabic', 'formidable' ),
3248 'hy' => __( 'Armenian', 'formidable' ),
3249 'az' => __( 'Azerbaijani', 'formidable' ),
3250 'eu' => __( 'Basque', 'formidable' ),
3251 'be' => __( 'Belarusian', 'formidable' ),
3252 'bn' => __( 'Bengali', 'formidable' ),
3253 'bs' => __( 'Bosnian', 'formidable' ),
3254 'bg' => __( 'Bulgarian', 'formidable' ),
3255 'ca' => __( 'Catalan', 'formidable' ),
3256 'zh-HK' => __( 'Chinese Hong Kong', 'formidable' ),
3257 'zh-CN' => __( 'Chinese Simplified', 'formidable' ),
3258 'zh-TW' => __( 'Chinese Traditional', 'formidable' ),
3259 'hr' => __( 'Croatian', 'formidable' ),
3260 'cs' => __( 'Czech', 'formidable' ),
3261 'da' => __( 'Danish', 'formidable' ),
3262 'nl' => __( 'Dutch', 'formidable' ),
3263 'en-GB' => __( 'English/UK', 'formidable' ),
3264 'eo' => __( 'Esperanto', 'formidable' ),
3265 'et' => __( 'Estonian', 'formidable' ),
3266 'fo' => __( 'Faroese', 'formidable' ),
3267 'fa' => __( 'Farsi/Persian', 'formidable' ),
3268 'fil' => __( 'Filipino', 'formidable' ),
3269 'fi' => __( 'Finnish', 'formidable' ),
3270 'fr' => __( 'French', 'formidable' ),
3271 'fr-CA' => __( 'French/Canadian', 'formidable' ),
3272 'fr-CH' => __( 'French/Swiss', 'formidable' ),
3273 'gl' => __( 'Galician', 'formidable' ),
3274 'ka' => __( 'Georgian', 'formidable' ),
3275 'de' => __( 'German', 'formidable' ),
3276 'de-AT' => __( 'German/Austria', 'formidable' ),
3277 'de-CH' => __( 'German/Switzerland', 'formidable' ),
3278 'el' => __( 'Greek', 'formidable' ),
3279 'gu' => __( 'Gujarati', 'formidable' ),
3280 'he' => __( 'Hebrew', 'formidable' ),
3281 'iw' => __( 'Hebrew', 'formidable' ),
3282 'hi' => __( 'Hindi', 'formidable' ),
3283 'hu' => __( 'Hungarian', 'formidable' ),
3284 'is' => __( 'Icelandic', 'formidable' ),
3285 'id' => __( 'Indonesian', 'formidable' ),
3286 'it' => __( 'Italian', 'formidable' ),
3287 'ja' => __( 'Japanese', 'formidable' ),
3288 'kn' => __( 'Kannada', 'formidable' ),
3289 'kk' => __( 'Kazakh', 'formidable' ),
3290 'km' => __( 'Khmer', 'formidable' ),
3291 'ko' => __( 'Korean', 'formidable' ),
3292 'ky' => __( 'Kyrgyz', 'formidable' ),
3293 'lo' => __( 'Laothian', 'formidable' ),
3294 'lv' => __( 'Latvian', 'formidable' ),
3295 'lt' => __( 'Lithuanian', 'formidable' ),
3296 'lb' => __( 'Luxembourgish', 'formidable' ),
3297 'mk' => __( 'Macedonian', 'formidable' ),
3298 'ml' => __( 'Malayalam', 'formidable' ),
3299 'ms' => __( 'Malaysian', 'formidable' ),
3300 'mr' => __( 'Marathi', 'formidable' ),
3301 'no' => __( 'Norwegian', 'formidable' ),
3302 'nb' => __( 'Norwegian Bokmål', 'formidable' ),
3303 'nn' => __( 'Norwegian Nynorsk', 'formidable' ),
3304 'pl' => __( 'Polish', 'formidable' ),
3305 'pt' => __( 'Portuguese', 'formidable' ),
3306 'pt-BR' => __( 'Portuguese/Brazilian', 'formidable' ),
3307 'pt-PT' => __( 'Portuguese/Portugal', 'formidable' ),
3308 'rm' => __( 'Romansh', 'formidable' ),
3309 'ro' => __( 'Romanian', 'formidable' ),
3310 'ru' => __( 'Russian', 'formidable' ),
3311 'sr' => __( 'Serbian', 'formidable' ),
3312 'sr-SR' => __( 'Serbian', 'formidable' ),
3313 'si' => __( 'Sinhalese', 'formidable' ),
3314 'sk' => __( 'Slovak', 'formidable' ),
3315 'sl' => __( 'Slovenian', 'formidable' ),
3316 'es' => __( 'Spanish', 'formidable' ),
3317 'es-419' => __( 'Spanish/Latin America', 'formidable' ),
3318 'sw' => __( 'Swahili', 'formidable' ),
3319 'sv' => __( 'Swedish', 'formidable' ),
3320 'ta' => __( 'Tamil', 'formidable' ),
3321 'te' => __( 'Telugu', 'formidable' ),
3322 'th' => __( 'Thai', 'formidable' ),
3323 'tj' => __( 'Tajiki', 'formidable' ),
3324 'tr' => __( 'Turkish', 'formidable' ),
3325 'uk' => __( 'Ukrainian', 'formidable' ),
3326 'ur' => __( 'Urdu', 'formidable' ),
3327 'vi' => __( 'Vietnamese', 'formidable' ),
3328 'cy-GB' => __( 'Welsh', 'formidable' ),
3329 'zu' => __( 'Zulu', 'formidable' ),
3330 );
3331
3332 if ( $type === 'captcha' ) {
3333 // remove the languages unavailable for the captcha
3334 $unset = array( 'sq', 'bs', 'eo', 'fo', 'fr-CH', 'sr-SR', 'ar-DZ', 'be', 'cy-GB', 'kk', 'km', 'ky', 'lb', 'mk', 'nb', 'nn', 'rm', 'tj' );
3335 } else {
3336 // remove the languages unavailable for the datepicker
3337 $unset = array( 'fil', 'fr-CA', 'de-AT', 'de-CH', 'iw', 'hi', 'pt', 'pt-PT', 'es-419', 'mr', 'lo', 'kn', 'si', 'gu', 'bn', 'zu', 'ur', 'te', 'sw', 'am' );
3338 }
3339
3340 $locales = array_diff_key( $locales, array_flip( $unset ) );
3341
3342 /**
3343 * Filter available locale options.
3344 *
3345 * @since 5.4.5 Added $args parameter with type.
3346 *
3347 * @param array<string,string> $locales
3348 * @param array $args {
3349 * @type string $type
3350 * }
3351 */
3352 $locales = apply_filters( 'frm_locales', $locales, compact( 'type' ) );
3353
3354 return $locales;
3355 }
3356
3357 /**
3358 * Output HTML containing reference text for accessibility
3359 *
3360 * @deprecated 5.1
3361 */
3362 public static function multiselect_accessibility() {
3363 _deprecated_function( __METHOD__, '5.1' );
3364 }
3365
3366 public static function get_menu_icon_class() {
3367 if ( is_callable( 'FrmProAppHelper::get_settings' ) ) {
3368 $settings = FrmProAppHelper::get_settings();
3369 if ( is_object( $settings ) && ! empty( $settings->menu_icon ) ) {
3370 return $settings->menu_icon;
3371 }
3372 }
3373 return 'frmfont frm_logo_icon';
3374 }
3375
3376 /**
3377 * Shows the images dropdown.
3378 *
3379 * @since 5.0.04
3380 *
3381 * @param array $args {
3382 * Arguments.
3383 *
3384 * @type string $selected Selected value.
3385 * @type array[] $options Array of options with keys are option values and values are array.
3386 * The option array contains `text`, `svg` and `custom_atts`.
3387 * @type string $classes Custom CSS classes for the wrapper element.
3388 * @type array $input_attrs Attributes of value input.
3389 * }
3390 */
3391 public static function images_dropdown( $args ) {
3392 $args = self::fill_default_images_dropdown_args( $args );
3393
3394 $input_attrs_str = self::get_images_dropdown_input_attrs( $args );
3395 ob_start();
3396 include self::plugin_path() . '/classes/views/shared/images-dropdown.php';
3397 $output = ob_get_clean();
3398
3399 /**
3400 * Allows modifying the output of FrmAppHelper::images_dropdown() method.
3401 *
3402 * @since 5.0.04
3403 *
3404 * @param string $output The output.
3405 * @param array $args Passed arguments.
3406 */
3407 echo apply_filters( 'frm_images_dropdown_output', $output, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
3408 }
3409
3410 /**
3411 * Fills the default images_dropdown() arguments.
3412 *
3413 * @since 5.0.04
3414 *
3415 * @param array $args The arguments.
3416 * @return array
3417 */
3418 private static function fill_default_images_dropdown_args( $args ) {
3419 $defaults = array(
3420 'selected' => '',
3421 'options' => array(),
3422 'classes' => '',
3423 'input_attrs' => array(),
3424 );
3425 $new_args = wp_parse_args( $args, $defaults );
3426
3427 $new_args['options'] = (array) $new_args['options'];
3428 $new_args['input_attrs'] = (array) $new_args['input_attrs'];
3429
3430 // Set the number of columns.
3431 $new_args['col_class'] = ceil( 12 / count( $new_args['options'] ) );
3432 if ( $new_args['col_class'] > 6 ) {
3433 $new_args['col_class'] = ceil( $new_args['col_class'] / 2 );
3434 }
3435
3436 /**
3437 * Allows modifying the arguments of images_dropdown() method.
3438 *
3439 * @since 5.0.04
3440 *
3441 * @param array $new_args Arguments after filling the defaults.
3442 * @param array $args Arguments passed to the method, before filling the defaults.
3443 */
3444 return apply_filters( 'frm_images_dropdown_args', $new_args, $args );
3445 }
3446
3447 /**
3448 * Gets HTML attributes of the input in images_dropdown() method.
3449 *
3450 * @since 5.0.04
3451 *
3452 * @param array $args The arguments.
3453 * @return string
3454 */
3455 private static function get_images_dropdown_input_attrs( $args ) {
3456 $input_attrs = $args['input_attrs'];
3457 $input_attrs['type'] = 'radio';
3458 $input_attrs['name'] = $args['name'];
3459
3460 $input_attrs_str = '';
3461 foreach ( $input_attrs as $key => $input_attr ) {
3462 $input_attrs_str .= ' ' . sprintf( '%s="%s"', esc_attr( $key ), esc_attr( $input_attr ) );
3463 }
3464
3465 /**
3466 * Allows modifying the HTML attributes of the input in images_dropdown() method.
3467 *
3468 * @since 5.0.04
3469 *
3470 * @param string $input_attrs_str HTML attributes string.
3471 * @param array $args The arguments of images_dropdown() method.
3472 */
3473 return apply_filters( 'frm_images_dropdown_input_attrs', $input_attrs_str, $args );
3474 }
3475
3476 /**
3477 * Gets the image of each option in images_dropdown() method.
3478 *
3479 * @since 5.0.04
3480 *
3481 * @param array $option Option data.
3482 * @param array $args The arguments of images_dropdown() method.
3483 * @return string
3484 */
3485 private static function get_images_dropdown_option_image( $option, $args ) {
3486 $image = self::icon_by_class(
3487 'frmfont ' . $option['svg'],
3488 array(
3489 'echo' => false,
3490 )
3491 );
3492
3493 $args['option'] = $option;
3494
3495 /**
3496 * Allows modifying the image of each option in images_dropdown() method.
3497 *
3498 * @since 5.0.04
3499 *
3500 * @param string $image The image HTML.
3501 * @param array $args The arguments of images_dropdown() method, with `option` array is added.
3502 */
3503 return apply_filters( 'frm_images_dropdown_option_image', $image, $args );
3504 }
3505
3506 /**
3507 * Gets the HTML classes of each option in images_dropdown() method.
3508 *
3509 * @since 5.0.04
3510 *
3511 * @param array $option Option data.
3512 * @param array $args The arguments of images_dropdown() method.
3513 * @return string
3514 */
3515 private static function get_images_dropdown_option_classes( $option, $args ) {
3516 $classes = '';
3517
3518 if ( ! empty( $option['custom_attrs']['class'] ) ) {
3519 $classes .= ' ' . $option['custom_attrs']['class'];
3520 }
3521
3522 $args['option'] = $option;
3523
3524 /**
3525 * Allows modifying the CSS classes of each option in images_dropdown() method.
3526 *
3527 * @since 5.0.04
3528 *
3529 * @param string $classes CSS classes.
3530 * @param array $args The arguments of images_dropdown() method, with `option` array is added.
3531 */
3532 return apply_filters( 'frm_images_dropdown_option_classes', $classes, $args );
3533 }
3534
3535 /**
3536 * Gets the custom HTML attributes of each option in images_dropdown() method.
3537 *
3538 * @since 5.0.04
3539 *
3540 * @param array $option Option data.
3541 * @param array $args The arguments of images_dropdown() method.
3542 * @return string
3543 */
3544 private static function get_images_dropdown_option_html_attrs( $option, $args ) {
3545 $html_attrs = '';
3546 if ( ! empty( $option['custom_attrs'] ) && is_array( $option['custom_attrs'] ) ) {
3547 $html_attrs_arr = array();
3548
3549 foreach ( $option['custom_attrs'] as $key => $value ) {
3550 if ( in_array( $key, array( 'type', 'class', 'data-value' ) ) ) {
3551 continue;
3552 }
3553
3554 $html_attrs_arr[] = sprintf( '%s="%s"', esc_attr( $key ), esc_attr( $value ) );
3555 }
3556
3557 $html_attrs = implode( ' ', $html_attrs_arr );
3558 }
3559
3560 $args['option'] = $option;
3561
3562 /**
3563 * Allows modifying the custom HTML attributes of each option in images_dropdown() method.
3564 *
3565 * @since 5.0.04
3566 *
3567 * @param string $html_attrs The HTML attributes string.
3568 * @param array $args The arguments of images_dropdown() method, with `option` array is added.
3569 */
3570 return apply_filters( 'frm_images_dropdown_option_html_attrs', $html_attrs, $args );
3571 }
3572
3573 /**
3574 * @since 5.0.07
3575 *
3576 * @return bool true if the current user is allowed to save unfiltered HTML.
3577 */
3578 public static function allow_unfiltered_html() {
3579 if ( self::should_never_allow_unfiltered_html() ) {
3580 return false;
3581 }
3582 return current_user_can( 'unfiltered_html' );
3583 }
3584
3585 /**
3586 * @since 5.0.13
3587 *
3588 * @return bool
3589 */
3590 public static function should_never_allow_unfiltered_html() {
3591 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) {
3592 return true;
3593 }
3594
3595 /**
3596 * Formidable will check DISALLOW_UNFILTERED_HTML to determine if some form HTML should be filtered or not.
3597 * In many cases, scripts are added intentionally to forms and will not be stripped if DISALLOW_UNFILTERED_HTML is not set.
3598 * It is also possible to filter Formidable without defining DISALLOW_UNFILTERED_HTML, with add_filter( 'frm_disallow_unfiltered_html', '__return_true' );
3599 *
3600 * @since 5.0.13
3601 */
3602 return apply_filters( 'frm_disallow_unfiltered_html', false );
3603 }
3604
3605 /**
3606 * @since 5.0.07
3607 *
3608 * @param array $values
3609 * @param array $keys
3610 * @return array
3611 */
3612 public static function maybe_filter_array( $values, $keys ) {
3613 $allow_unfiltered_html = self::allow_unfiltered_html();
3614
3615 if ( $allow_unfiltered_html ) {
3616 return $values;
3617 }
3618
3619 foreach ( $keys as $key ) {
3620 if ( isset( $values[ $key ] ) ) {
3621 $values[ $key ] = self::kses( $values[ $key ], 'all' );
3622 }
3623 }
3624
3625 return $values;
3626 }
3627
3628 /**
3629 * Some back end fields allow privleged users to add scripts.
3630 * A site that uses the DISALLOW_UNFILTERED_HTML always remove scripts on echo.
3631 *
3632 * @since 5.0.13
3633 *
3634 * @param string $value
3635 * @param array|string $allowed 'all' for everything included as defaults
3636 * @return string
3637 */
3638 public static function maybe_kses( $value, $allowed = 'all' ) {
3639 if ( self::should_never_allow_unfiltered_html() ) {
3640 $value = self::kses( $value, $allowed );
3641 }
3642 return $value;
3643 }
3644
3645 /**
3646 * @since 5.0.16
3647 *
3648 * @return bool
3649 */
3650 public static function show_landing_pages() {
3651 return self::show_new_feature( 'landing' );
3652 }
3653
3654 /**
3655 * @since 5.0.16
3656 *
3657 * @return array
3658 */
3659 public static function get_landing_page_upgrade_data_params( $medium = 'landing' ) {
3660 $params = array(
3661 'medium' => $medium,
3662 'upgrade' => __( 'Form Landing Pages', 'formidable' ),
3663 'message' => __( 'Easily manage a landing page for your form. Upgrade to get form landing pages.', 'formidable' ),
3664 'screenshot' => 'landing.png',
3665 );
3666 return self::get_upgrade_data_params( 'landing', $params );
3667 }
3668
3669 /**
3670 * @since 5.0.17
3671 *
3672 * @param string $plugin
3673 * @return string|false
3674 */
3675 private static function get_plan_required( $plugin ) {
3676 $link = FrmAddonsController::install_link( $plugin );
3677 return FrmFormsHelper::get_plan_required( $link );
3678 }
3679
3680 /**
3681 * @since 5.0.17
3682 *
3683 * @param string
3684 * @return bool
3685 */
3686 public static function show_new_feature( $feature ) {
3687 $link = FrmAddonsController::install_link( $feature );
3688 return array_key_exists( 'status', $link ) || array_key_exists( 'class', $link );
3689 }
3690
3691 /**
3692 * @since 5.0.17
3693 *
3694 * @param string $plugin
3695 * @param array $params
3696 * @return array
3697 */
3698 public static function get_upgrade_data_params( $plugin, $params ) {
3699 $link = FrmAddonsController::install_link( $plugin );
3700 if ( ! $link ) {
3701 return $params;
3702 }
3703
3704 if ( ! empty( $link['url'] ) && self::pro_is_installed() ) {
3705 $params['oneclick'] = json_encode( $link );
3706 unset( $params['message'] );
3707 if ( ! isset( $params['medium'] ) ) {
3708 $params['medium'] = $plugin;
3709 }
3710 } else {
3711 $params['requires'] = FrmFormsHelper::get_plan_required( $link );
3712 }
3713
3714 return $params;
3715 }
3716
3717 /**
3718 * Returns true if every character in text is a hexadecimal 'digit', that is a decimal digit or a character from [A-Fa-f], false otherwise.
3719 * Not every server installs the ctype extension, so use a fallback if the function does not exist.
3720 *
3721 * @since 5.0.17
3722 *
3723 * @param string $text
3724 * @return bool
3725 */
3726 public static function ctype_xdigit( $text ) {
3727 if ( function_exists( 'ctype_xdigit' ) ) {
3728 return ctype_xdigit( $text );
3729 }
3730 return is_string( $text ) && '' !== $text && ! preg_match( '/[^A-Fa-f0-9]/', $text );
3731 }
3732
3733 /**
3734 * Set the current screen to avoid undefined notices.
3735 *
3736 * @since 5.2.01
3737 */
3738 public static function set_current_screen_and_hook_suffix() {
3739 global $hook_suffix;
3740 if ( is_null( $hook_suffix ) ) {
3741 // $hook_suffix gets used in substr so make sure it's not null. PHP 8.1 deprecates null in substr.
3742 $hook_suffix = ''; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
3743 }
3744 set_current_screen();
3745 }
3746
3747 /**
3748 * Shows pill text.
3749 *
3750 * @since 5.2.02
3751 *
3752 * @param string $text Text in the pill. Default is NEW.
3753 */
3754 public static function show_pill_text( $text = null ) {
3755 if ( null === $text ) {
3756 $text = __( 'NEW', 'formidable' );
3757 }
3758 echo '<span class="frm-new-pill">' . esc_html( $text ) . '</span>';
3759 }
3760
3761 /**
3762 * Count the number of decimals digits.
3763 *
3764 * @since 5.2.07
3765 *
3766 * @param mixed $num Number.
3767 * @return int|false Returns `false` if the passed parameter is not number.
3768 */
3769 public static function count_decimals( $num ) {
3770 if ( ! is_numeric( $num ) ) {
3771 return false;
3772 }
3773
3774 $num = (string) $num;
3775 $parts = explode( '.', $num );
3776 if ( 1 === count( $parts ) ) {
3777 return 0;
3778 }
3779
3780 return strlen( $parts[ count( $parts ) - 1 ] );
3781 }
3782
3783 /**
3784 * Prevent a fatal error in PHP8 if gmt_offset happens to be set an empty string.
3785 * This is a bug in WordPress. It isn't safe to call current_time( 'timestamp' ) without this with an empty string offset.
3786 * In the future this might be safe to remove. Keep an eye on the current_time function in functions.php.
3787 *
3788 * @since 5.3.1
3789 *
3790 * @return void
3791 */
3792 public static function filter_gmt_offset() {
3793 if ( self::$added_gmt_offset_filter ) {
3794 // Avoid adding twice.
3795 return;
3796 }
3797
3798 add_filter(
3799 'option_gmt_offset',
3800 function( $offset ) {
3801 if ( ! is_string( $offset ) || is_numeric( $offset ) ) {
3802 // Leave a valid value alone.
3803 return $offset;
3804 }
3805
3806 return 0;
3807 }
3808 );
3809 self::$added_gmt_offset_filter = true;
3810 }
3811
3812 /**
3813 * @since 5.3.1
3814 *
3815 * @return bool
3816 */
3817 public static function on_form_listing_page() {
3818 if ( ! self::is_admin_page( 'formidable' ) ) {
3819 return false;
3820 }
3821
3822 $action = self::simple_get( 'frm_action', 'sanitize_title' );
3823 return ! $action || in_array( $action, self::get_form_listing_page_actions(), true );
3824 }
3825
3826 /**
3827 * Get all actions that also display the forms list.
3828 *
3829 * @since 5.3.1
3830 *
3831 * @return array<string>
3832 */
3833 private static function get_form_listing_page_actions() {
3834 return array( 'list', 'trash', 'untrash', 'destroy' );
3835 }
3836
3837 /**
3838 * Safely call get_plugins, importing the required files if they are not yet loaded.
3839 *
3840 * @since 5.5
3841 *
3842 * @return array
3843 */
3844 public static function get_plugins() {
3845 if ( ! function_exists( 'get_plugins' ) ) {
3846 require_once ABSPATH . 'wp-admin/includes/plugin.php';
3847 }
3848 return get_plugins();
3849 }
3850
3851 /**
3852 * Make sure that the file we're trying to load is in fact the expected file type, and that it's coming from our S3 bucket.
3853 * This is to make sure that the URL can't be exploited for a SSRF attack.
3854 *
3855 * @since 5.5.5
3856 *
3857 * @param string $url
3858 * @param string $expected_extension
3859 * @return bool
3860 */
3861 public static function validate_url_is_in_s3_bucket( $url, $expected_extension ) {
3862 $file_is_in_expected_s3_bucket = 0 === strpos( $url, 'https://s3.amazonaws.com/fp.strategy11.com' );
3863 if ( ! $file_is_in_expected_s3_bucket ) {
3864 return false;
3865 }
3866
3867 $parsed = parse_url( $url );
3868 if ( ! is_array( $parsed ) ) {
3869 // URL is malformed.
3870 return false;
3871 }
3872
3873 $path = $parsed['path'];
3874 $ext = pathinfo( $path, PATHINFO_EXTENSION );
3875 if ( $expected_extension !== $ext ) {
3876 // The URL isn't to an XML file.
3877 return false;
3878 }
3879
3880 return true;
3881 }
3882
3883 /**
3884 * @since 4.08
3885 * @deprecated 4.09.01
3886 */
3887 public static function expiring_message() {
3888 _deprecated_function( __METHOD__, '4.09.01', 'FrmProAddonsController::expiring_message' );
3889 if ( is_callable( 'FrmProAddonsController::expiring_message' ) ) {
3890 FrmProAddonsController::expiring_message();
3891 }
3892 }
3893
3894 /**
3895 * Use the WP 4.7 wp_doing_ajax function
3896 *
3897 * @since 2.05.07
3898 * @deprecated 4.04.04
3899 */
3900 public static function wp_doing_ajax() {
3901 _deprecated_function( __METHOD__, '4.04.04', 'wp_doing_ajax' );
3902 return wp_doing_ajax();
3903 }
3904
3905 /**
3906 * @deprecated 4.0
3907 */
3908 public static function insert_opt_html( $args ) {
3909 _deprecated_function( __METHOD__, '4.0', 'FrmFormsHelper::insert_opt_html' );
3910 FrmFormsHelper::insert_opt_html( $args );
3911 }
3912
3913 /**
3914 * @deprecated 3.01
3915 * @codeCoverageIgnore
3916 */
3917 public static function sanitize_array( &$values ) {
3918 FrmDeprecated::sanitize_array( $values );
3919 }
3920
3921 /**
3922 * @since 2.0
3923 * @deprecated 5.0.13
3924 *
3925 * @return string The base Google APIS url for the current version of jQuery UI
3926 */
3927 public static function jquery_ui_base_url() {
3928 _deprecated_function( __FUNCTION__, '5.0.13', 'FrmProAppHelper::jquery_ui_base_url' );
3929 return is_callable( 'FrmProAppHelper::jquery_ui_base_url' ) ? FrmProAppHelper::jquery_ui_base_url() : '';
3930 }
3931
3932 /**
3933 * @since 4.07
3934 * @deprecated 6.0
3935 */
3936 public static function renewal_message() {
3937 _deprecated_function( __METHOD__, '6.0', 'FrmProAddonsController::renewal_message' );
3938 }
3939
3940 /**
3941 * Display a dismissable warning message and save its dismissal state.
3942 *
3943 * @since 6.3
3944 *
3945 * @param string $message The warning message to display.
3946 * @param string $option The unique identifier for the dismissal state of the message and the WP Ajax action.
3947 * @return void
3948 */
3949 public static function add_dismissable_warning_message( $message = '', $option = '' ) {
3950 if ( ! $message || ! $option ) {
3951 return;
3952 }
3953
3954 $ajax_callback = function() use ( $option ) {
3955 self::dismiss_warning_message( $option );
3956 };
3957
3958 // We're handling JS codes with `doJsonPost` and it adds 'frm_' to the beginning of the action.
3959 // To prevent any issues, we add 'frm_' from the beginning of the action.
3960 add_action( 'wp_ajax_frm_' . $option, $ajax_callback );
3961
3962 add_filter(
3963 'frm_message_list',
3964 function( $show_messages ) use ( $message, $option ) {
3965 if ( get_option( $option, false ) ) {
3966 return $show_messages;
3967 }
3968
3969 $dismiss_icon = self::icon_by_class(
3970 'frmfont frm_close_icon',
3971 array(
3972 'aria-label' => _x( 'Dismiss', 'warning message: close icon label', 'formidable' ),
3973 'echo' => false,
3974 )
3975 );
3976
3977 $show_messages[] = $message;
3978 $show_messages[] = '<span class="frm-warning-dismiss frmsvg" data-action="' . esc_attr( $option ) . '">' . $dismiss_icon . '</span>';
3979
3980 return $show_messages;
3981 }
3982 );
3983 }
3984
3985 /**
3986 * Dismiss a warning message and update the dismissal state.
3987 *
3988 * @since 6.3
3989 *
3990 * @param string $option The unique identifier for the dismissal state of the message.
3991 * @return void
3992 */
3993 public static function dismiss_warning_message( $option = '' ) {
3994 self::permission_check( 'frm_change_settings' );
3995 check_ajax_referer( 'frm_ajax', 'nonce' );
3996
3997 if ( $option ) {
3998 update_option( $option, true, 'no' );
3999 }
4000
4001 wp_send_json_success();
4002 }
4003 }
4004