Completed
Push — master ( b0b171...1c7210 )
by Stephanie
02:34
created

FrmAppHelper::recursive_function_map()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 5
nop 2
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
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 = 46; //version of the database we are moving to
8
	public static $pro_db_version = 37; //deprecated
9
10
	/**
11
	 * @since 2.0
12
	 */
13
	public static $plug_version = '2.05.02';
14
15
    /**
16
     * @since 1.07.02
17
     *
18
     * @param none
19
     * @return string The version of this plugin
20
     */
21
    public static function plugin_version() {
22
        return self::$plug_version;
23
    }
24
25
    public static function plugin_folder() {
26
        return basename(self::plugin_path());
27
    }
28
29
    public static function plugin_path() {
30
        return dirname(dirname(dirname(__FILE__)));
31
    }
32
33
    public static function plugin_url() {
34
        //prevously FRM_URL constant
35
		return plugins_url( '', self::plugin_path() . '/formidable.php' );
36
    }
37
38
	public static function relative_plugin_url() {
39
		return str_replace( array( 'https:', 'http:' ), '', self::plugin_url() );
40
	}
41
42
    /**
43
     * @return string Site URL
44
     */
45
    public static function site_url() {
46
        return site_url();
47
    }
48
49
    /**
50
     * Get the name of this site
51
     * Used for [sitename] shortcode
52
     *
53
     * @since 2.0
54
     * @return string
55
     */
56
    public static function site_name() {
57
        return get_option('blogname');
58
    }
59
60
	public static function make_affiliate_url( $url ) {
61
		$affiliate_id = self::get_affiliate();
62
		if ( ! empty( $affiliate_id ) ) {
63
			$url = str_replace( array( 'http://', 'https://' ), '', $url );
64
			$url = 'http://www.shareasale.com/r.cfm?u='. absint( $affiliate_id ) .'&b=841990&m=64739&afftrack=plugin&urllink=' . urlencode( $url );
65
		}
66
		return $url;
67
	}
68
69
	public static function get_affiliate() {
70
		return absint( apply_filters( 'frm_affiliate_id', 0 ) );
71
	}
72
73
    /**
74
     * Get the Formidable settings
75
     *
76
     * @since 2.0
77
     *
78
     * @param None
79
     * @return FrmSettings $frm_setings
80
     */
81
    public static function get_settings() {
82
        global $frm_settings;
83
        if ( empty($frm_settings) ) {
84
            $frm_settings = new FrmSettings();
85
        }
86
        return $frm_settings;
87
    }
88
89
	public static function get_menu_name() {
90
		$frm_settings = FrmAppHelper::get_settings();
91
		return $frm_settings->menu;
92
	}
93
94
	/**
95
	 * @since 2.02.04
96
	 */
97
	public static function ips_saved() {
98
		$frm_settings = self::get_settings();
99
		return ! $frm_settings->no_ips;
100
	}
101
102
    /**
103
     * Show a message in place of pro features
104
     *
105
     * @since 2.0
106
     */
107
	public static function update_message() {
108
		_deprecated_function( __FUNCTION__, '2.0.19' );
109
    }
110
111
    public static function pro_is_installed() {
112
        return apply_filters('frm_pro_installed', false);
113
    }
114
115
    /**
116
     * Check for certain page in Formidable settings
117
     *
118
     * @since 2.0
119
     *
120
     * @param string $page The name of the page to check
121
     * @return boolean
122
     */
123
	public static function is_admin_page( $page = 'formidable' ) {
124
        global $pagenow;
125
		$get_page = self::simple_get( 'page', 'sanitize_title' );
126
        if ( $pagenow ) {
127
			return $pagenow == 'admin.php' && $get_page == $page;
128
        }
129
130
		return is_admin() && $get_page == $page;
131
    }
132
133
    /**
134
     * Check for the form preview page
135
     *
136
     * @since 2.0
137
     *
138
     * @param None
139
     * @return boolean
140
     */
141
    public static function is_preview_page() {
142
        global $pagenow;
143
		$action = FrmAppHelper::simple_get( 'action', 'sanitize_title' );
144
		return $pagenow && $pagenow == 'admin-ajax.php' && $action == 'frm_forms_preview';
145
    }
146
147
    /**
148
     * Check for ajax except the form preview page
149
     *
150
     * @since 2.0
151
     *
152
     * @param None
153
     * @return boolean
154
     */
155
    public static function doing_ajax() {
156
        return defined('DOING_AJAX') && DOING_AJAX && ! self::is_preview_page();
157
    }
158
159
	/**
160
	 * @since 2.0.8
161
	 */
162
	public static function prevent_caching() {
163
		global $frm_vars;
164
		return isset( $frm_vars['prevent_caching'] ) && $frm_vars['prevent_caching'];
165
	}
166
167
    /**
168
     * Check if on an admin page
169
     *
170
     * @since 2.0
171
     *
172
     * @param None
173
     * @return boolean
174
     */
175
    public static function is_admin() {
176
        return is_admin() && ( ! defined('DOING_AJAX') || ! DOING_AJAX );
177
    }
178
179
    /**
180
     * Check if value contains blank value or empty array
181
     *
182
     * @since 2.0
183
     * @param mixed $value - value to check
184
	 * @param string
185
     * @return boolean
186
     */
187
    public static function is_empty_value( $value, $empty = '' ) {
188
        return ( is_array( $value ) && empty( $value ) ) || $value == $empty;
189
    }
190
191
    public static function is_not_empty_value( $value, $empty = '' ) {
192
        return ! self::is_empty_value( $value, $empty );
193
    }
194
195
    /**
196
     * Get any value from the $_SERVER
197
     *
198
     * @since 2.0
199
     * @param string $value
200
     * @return string
201
     */
202
	public static function get_server_value( $value ) {
203
        return isset( $_SERVER[ $value ] ) ? wp_strip_all_tags( $_SERVER[ $value ] ) : '';
204
    }
205
206
    /**
207
     * Check for the IP address in several places
208
     * Used by [ip] shortcode
209
     *
210
     * @return string The IP address of the current user
211
     */
212
    public static function get_ip_address() {
213
		$ip = '';
214
        foreach ( array(
215
            'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP',
216
            'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR',
217
        ) as $key ) {
218
            if ( ! isset( $_SERVER[ $key ] ) ) {
219
                continue;
220
            }
221
222
            foreach ( explode( ',', $_SERVER[ $key ] ) as $ip ) {
223
                $ip = trim($ip); // just to be safe
224
225
                if ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) {
226
                    return $ip;
227
                }
228
            }
229
        }
230
231
		return sanitize_text_field( $ip );
232
    }
233
234
    public static function get_param( $param, $default = '', $src = 'get', $sanitize = '' ) {
235
        if ( strpos($param, '[') ) {
236
            $params = explode('[', $param);
237
            $param = $params[0];
238
        }
239
240
		if ( $src == 'get' ) {
241
            $value = isset( $_POST[ $param ] ) ? stripslashes_deep( $_POST[ $param ] ) : ( isset( $_GET[ $param ] ) ? stripslashes_deep( $_GET[ $param ] ) : $default );
242
            if ( ! isset( $_POST[ $param ] ) && isset( $_GET[ $param ] ) && ! is_array( $value ) ) {
243
                $value = stripslashes_deep( htmlspecialchars_decode( $_GET[ $param ] ) );
244
            }
245
			self::sanitize_value( $sanitize, $value );
246
		} else {
247
            $value = self::get_simple_request( array( 'type' => $src, 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) );
248
        }
249
250
		if ( isset( $params ) && is_array( $value ) && ! empty( $value ) ) {
251
            foreach ( $params as $k => $p ) {
252
                if ( ! $k || ! is_array($value) ) {
253
                    continue;
254
                }
255
256
                $p = trim($p, ']');
257
                $value = isset( $value[ $p ] ) ? $value[ $p ] : $default;
258
            }
259
        }
260
261
        return $value;
262
    }
263
264
	public static function get_post_param( $param, $default = '', $sanitize = '' ) {
265
		return self::get_simple_request( array( 'type' => 'post', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) );
266
	}
267
268
	/**
269
	 * @since 2.0
270
	 *
271
	 * @param string $param
272
	 * @param string $sanitize
273
	 * @param string $default
274
	 * @return string|array
275
	 */
276
	public static function simple_get( $param, $sanitize = 'sanitize_text_field', $default = '' ) {
277
		return self::get_simple_request( array( 'type' => 'get', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) );
278
    }
279
280
	/**
281
	 * Get a GET/POST/REQUEST value and sanitize it
282
	 *
283
	 * @since 2.0.6
284
	 * @param array $args
285
	 * @return string|array
286
	 */
287
	public static function get_simple_request( $args ) {
288
		$defaults = array(
289
			'param' => '', 'default' => '',
290
			'type' => 'get', 'sanitize' => 'sanitize_text_field',
291
		);
292
		$args = wp_parse_args( $args, $defaults );
293
294
		$value = $args['default'];
295
		if ( $args['type'] == 'get' ) {
296
			if ( $_GET && isset( $_GET[ $args['param'] ] ) ) {
297
				$value = $_GET[ $args['param'] ];
298
			}
299
		} else if ( $args['type'] == 'post' ) {
300
			if ( isset( $_POST[ $args['param'] ] ) ) {
301
				$value = stripslashes_deep( maybe_unserialize( $_POST[ $args['param'] ] ) );
302
			}
303
		} else {
304
			if ( isset( $_REQUEST[ $args['param'] ] ) ) {
305
				$value = $_REQUEST[ $args['param'] ];
306
			}
307
		}
308
309
		self::sanitize_value( $args['sanitize'], $value );
310
		return $value;
311
	}
312
313
	/**
314
	* Preserve backslashes in a value, but make sure value doesn't get compounding slashes
315
	*
316
	* @since 2.0.8
317
	* @param string $value
318
	* @return string $value
319
	*/
320
	public static function preserve_backslashes( $value ) {
321
		// If backslashes have already been added, don't add them again
322
		if ( strpos( $value, '\\\\' ) === false ) {
323
			$value = addslashes( $value );
324
		}
325
		return $value;
326
	}
327
328
	public static function sanitize_value( $sanitize, &$value ) {
329
		if ( ! empty( $sanitize ) ) {
330
			if ( is_array( $value ) ) {
331
				$temp_values = $value;
332
				foreach ( $temp_values as $k => $v ) {
333
					FrmAppHelper::sanitize_value( $sanitize, $value[ $k ] );
334
				}
335
			} else {
336
				$value = call_user_func( $sanitize, $value );
337
			}
338
		}
339
	}
340
341
    public static function sanitize_request( $sanitize_method, &$values ) {
342
        $temp_values = $values;
343
        foreach ( $temp_values as $k => $val ) {
344
            if ( isset( $sanitize_method[ $k ] ) ) {
345
				$values[ $k ] = call_user_func( $sanitize_method[ $k ], $val );
346
            }
347
        }
348
    }
349
350
	public static function sanitize_array( &$values ) {
351
		$temp_values = $values;
352
		foreach ( $temp_values as $k => $val ) {
353
			$values[ $k ] = wp_kses_post( $val );
354
		}
355
	}
356
357
	/**
358
	 * Sanitize the value, and allow some HTML
359
	 * @since 2.0
360
	 * @param string $value
361
	 * @param array|string $allowed 'all' for everything included as defaults
362
	 * @return string
363
	 */
364
	public static function kses( $value, $allowed = array() ) {
365
		$allowed_html = self::allowed_html( $allowed );
366
367
		return wp_kses( $value, $allowed_html );
368
	}
369
370
	/**
371
	 * @since 2.05.03
372
	 */
373
	private static function allowed_html( $allowed ) {
374
		$allowed_html = array();
375
		if ( $allowed == 'all' ) {
376
			$allowed_html = $html;
0 ignored issues
show
Bug introduced by
The variable $html does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
377
		} else {
378
			foreach ( $allowed as $a ) {
379
				$allowed_html[ $a ] = isset( $html[ $a ] ) ? $html[ $a ] : array();
380
			}
381
		}
382
383
		return apply_filters( 'frm_striphtml_allowed_tags', $allowed_html );
384
	}
385
386
	/**
387
	 * @since 2.05.03
388
	 */
389
	private static function safe_html() {
390
		return array(
391
			'a' => array(
392
				'class' => array(),
393
				'href'  => array(),
394
				'id'    => array(),
395
				'rel'   => array(),
396
				'title' => array(),
397
			),
398
			'abbr' => array(
399
				'title' => array(),
400
			),
401
			'b' => array(),
402
			'blockquote' => array(
403
				'cite'  => array(),
404
			),
405
			'br'   => array(),
406
			'cite' => array(
407
				'title' => array(),
408
			),
409
			'code' => array(),
410
			'del'  => array(
411
				'datetime' => array(),
412
				'title' => array(),
413
			),
414
			'dd'  => array(),
415
			'div' => array(
416
				'class' => array(),
417
				'id'    => array(),
418
				'title' => array(),
419
				'style' => array(),
420
			),
421
			'dl'  => array(),
422
			'dt'  => array(),
423
			'em'  => array(),
424
			'h1'  => array(),
425
			'h2'  => array(),
426
			'h3'  => array(),
427
			'h4'  => array(),
428
			'h5'  => array(),
429
			'h6'  => array(),
430
			'i'   => array(),
431
			'img' => array(
432
				'alt'    => array(),
433
				'class'  => array(),
434
				'height' => array(),
435
				'id'     => array(),
436
				'src'    => array(),
437
				'width'  => array(),
438
			),
439
			'li' => array(
440
				'class' => array(),
441
				'id'    => array(),
442
			),
443
			'ol' => array(
444
				'class' => array(),
445
				'id'    => array(),
446
			),
447
			'p'   => array(
448
				'class' => array(),
449
				'id'    => array(),
450
			),
451
			'pre' => array(),
452
			'q'   => array(
453
				'cite' => array(),
454
				'title' => array(),
455
			),
456
			'span' => array(
457
				'class' => array(),
458
				'id'    => array(),
459
				'title' => array(),
460
				'style' => array(),
461
			),
462
			'strike' => array(),
463
			'strong' => array(),
464
			'ul' => array(
465
				'class' => array(),
466
				'id'    => array(),
467
			),
468
		);
469
	}
470
471
    /**
472
     * Used when switching the action for a bulk action
473
     * @since 2.0
474
     */
475
    public static function remove_get_action() {
476
        if ( ! isset($_GET) ) {
477
            return;
478
        }
479
480
        $new_action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : ( isset( $_GET['action2'] ) ? sanitize_text_field( $_GET['action2'] ) : '' );
481
        if ( ! empty( $new_action ) ) {
482
			$_SERVER['REQUEST_URI'] = str_replace( '&action=' . $new_action, '', FrmAppHelper::get_server_value( 'REQUEST_URI' ) );
483
        }
484
    }
485
486
    /**
487
     * Check the WP query for a parameter
488
     *
489
     * @since 2.0
490
     * @return string|array
491
     */
492
    public static function get_query_var( $value, $param ) {
493
        if ( $value != '' ) {
494
            return $value;
495
        }
496
497
        global $wp_query;
498
        if ( isset( $wp_query->query_vars[ $param ] ) ) {
499
            $value = $wp_query->query_vars[ $param ];
500
        }
501
502
        return $value;
503
    }
504
505
    /**
506
     * @param string $type
507
     */
508
    public static function trigger_hook_load( $type, $object = null ) {
509
        // only load the form hooks once
510
		$hooks_loaded = apply_filters( 'frm_' . $type . '_hooks_loaded', false, $object );
511
        if ( ! $hooks_loaded ) {
512
			do_action( 'frm_load_' . $type . '_hooks' );
513
        }
514
    }
515
516
    /**
517
     * Check cache before fetching values and saving to cache
518
     *
519
     * @since 2.0
520
     *
521
     * @param string $cache_key The unique name for this cache
522
     * @param string $group The name of the cache group
523
     * @param string $query If blank, don't run a db call
524
     * @param string $type The wpdb function to use with this query
525
     * @return mixed $results The cache or query results
526
     */
527
    public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
528
        $results = wp_cache_get($cache_key, $group);
529
        if ( ! self::is_empty_value( $results, false ) || empty($query) ) {
530
            return $results;
531
        }
532
533
        if ( 'get_posts' == $type ) {
534
            $results = get_posts($query);
535
		} else if ( 'get_associative_results' == $type ) {
536
			global $wpdb;
537
			$results = $wpdb->get_results( $query, OBJECT_K );
538
        } else {
539
            global $wpdb;
540
            $results = $wpdb->{$type}($query);
541
        }
542
543
		self::set_cache( $cache_key, $results, $group, $time );
544
545
		return $results;
546
	}
547
548
	public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
549
		if ( ! self::prevent_caching() ) {
550
			self::add_key_to_group_cache( $cache_key, $group );
551
			wp_cache_set( $cache_key, $results, $group, $time );
552
		}
553
	}
554
555
	/**
556
	 * Keep track of the keys cached in each group so they can be deleted
557
	 * in Redis and Memcache
558
	 */
559
	public static function add_key_to_group_cache( $key, $group ) {
560
		$cached = self::get_group_cached_keys( $group );
561
		$cached[ $key ] = $key;
562
		wp_cache_set( 'cached_keys', $cached, $group, 300 );
563
	}
564
565
	public static function get_group_cached_keys( $group ) {
566
		$cached = wp_cache_get( 'cached_keys', $group );
567
		if ( ! $cached || ! is_array( $cached ) ) {
568
			$cached = array();
569
		}
570
571
		return $cached;
572
	}
573
574
    /**
575
     * Data that should be stored for a long time can be stored in a transient.
576
     * First check the cache, then check the transient
577
     * @since 2.0
578
     * @return mixed The cached value or false
579
     */
580
	public static function check_cache_and_transient( $cache_key ) {
581
        // check caching layer first
582
        $results = self::check_cache( $cache_key );
583
        if ( $results ) {
584
            return $results;
585
        }
586
587
        // then check the transient
588
        $results = get_transient($cache_key);
589
        if ( $results ) {
590
            wp_cache_set($cache_key, $results);
591
        }
592
593
        return $results;
594
    }
595
596
    /**
597
     * @since 2.0
598
     * @param string $cache_key
599
     */
600
	public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
601
		delete_transient($cache_key);
602
		wp_cache_delete( $cache_key, $group );
603
	}
604
605
    /**
606
     * Delete all caching in a single group
607
     *
608
     * @since 2.0
609
     *
610
     * @param string $group The name of the cache group
611
     */
612
	public static function cache_delete_group( $group ) {
613
		$cached_keys = self::get_group_cached_keys( $group );
614
615
		if ( ! empty( $cached_keys ) ) {
616
			foreach ( $cached_keys as $key ) {
617
				wp_cache_delete( $key, $group );
618
			}
619
620
			wp_cache_delete( 'cached_keys', $group );
621
		}
622
	}
623
624
    /**
625
     * Check a value from a shortcode to see if true or false.
626
     * True when value is 1, true, 'true', 'yes'
627
     *
628
     * @since 1.07.10
629
     *
630
     * @param string $value The value to compare
631
     * @return boolean True or False
632
     */
633
	public static function is_true( $value ) {
634
        return ( true === $value || 1 == $value || 'true' == $value || 'yes' == $value );
635
    }
636
637
    /**
638
     * Used to filter shortcode in text widgets
639
     */
640
    public static function widget_text_filter_callback( $matches ) {
641
        return do_shortcode( $matches[0] );
642
    }
643
644
    public static function get_pages() {
645
		return get_posts( array( 'post_type' => 'page', 'post_status' => array( 'publish', 'private' ), 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ) );
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
646
    }
647
648
    public static function wp_pages_dropdown( $field_name, $page_id, $truncate = false ) {
649
        $pages = self::get_pages();
650
		$selected = self::get_post_param( $field_name, $page_id, 'absint' );
651
    ?>
652
        <select name="<?php echo esc_attr($field_name); ?>" id="<?php echo esc_attr($field_name); ?>" class="frm-pages-dropdown">
653
            <option value=""> </option>
654
            <?php foreach ( $pages as $page ) { ?>
655
				<option value="<?php echo esc_attr($page->ID); ?>" <?php selected( $selected, $page->ID ) ?>>
656
					<?php echo esc_html( $truncate ? self::truncate( $page->post_title, $truncate ) : $page->post_title ); ?>
657
				</option>
658
            <?php } ?>
659
        </select>
660
    <?php
661
    }
662
663
	public static function post_edit_link( $post_id ) {
664
        $post = get_post($post_id);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
665
        if ( $post ) {
666
			$post_url = admin_url( 'post.php?post=' . $post_id . '&action=edit' );
667
			return '<a href="' . esc_url( $post_url ) . '">' . self::truncate( $post->post_title, 50 ) . '</a>';
668
        }
669
        return '';
670
    }
671
672
	public static function wp_roles_dropdown( $field_name, $capability, $multiple = 'single' ) {
673
    ?>
674
        <select name="<?php echo esc_attr($field_name); ?>" id="<?php echo esc_attr($field_name); ?>" <?php
675
            echo ( 'multiple' == $multiple ) ? 'multiple="multiple"' : '';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
676
            ?> class="frm_multiselect">
677
            <?php self::roles_options($capability); ?>
678
        </select>
679
    <?php
680
    }
681
682
	public static function roles_options( $capability ) {
683
        global $frm_vars;
684
        if ( isset($frm_vars['editable_roles']) ) {
685
            $editable_roles = $frm_vars['editable_roles'];
686
        } else {
687
            $editable_roles = get_editable_roles();
688
            $frm_vars['editable_roles'] = $editable_roles;
689
        }
690
691
        foreach ( $editable_roles as $role => $details ) {
692
            $name = translate_user_role($details['name'] ); ?>
693
        <option value="<?php echo esc_attr($role) ?>" <?php echo in_array($role, (array) $capability) ? ' selected="selected"' : ''; ?>><?php echo esc_attr($name) ?> </option>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'in_array'
Loading history...
694
<?php
695
            unset($role, $details);
696
        }
697
    }
698
699
	public static function frm_capabilities( $type = 'auto' ) {
700
        $cap = array(
701
            'frm_view_forms'        => __( 'View Forms and Templates', 'formidable' ),
702
            'frm_edit_forms'        => __( 'Add/Edit Forms and Templates', 'formidable' ),
703
            'frm_delete_forms'      => __( 'Delete Forms and Templates', 'formidable' ),
704
            'frm_change_settings'   => __( 'Access this Settings Page', 'formidable' ),
705
            'frm_view_entries'      => __( 'View Entries from Admin Area', 'formidable' ),
706
            'frm_delete_entries'    => __( 'Delete Entries from Admin Area', 'formidable' ),
707
        );
708
709
		if ( ! self::pro_is_installed() && 'pro' != $type ) {
710
            return $cap;
711
        }
712
713
        $cap['frm_create_entries'] = __( 'Add Entries from Admin Area', 'formidable' );
714
        $cap['frm_edit_entries'] = __( 'Edit Entries from Admin Area', 'formidable' );
715
        $cap['frm_view_reports'] = __( 'View Reports', 'formidable' );
716
        $cap['frm_edit_displays'] = __( 'Add/Edit Views', 'formidable' );
717
718
        return $cap;
719
    }
720
721
	public static function user_has_permission( $needed_role ) {
722
        if ( $needed_role == '-1' ) {
723
            return false;
724
		}
725
726
        // $needed_role will be equal to blank if "Logged-in users" is selected
727
        if ( ( $needed_role == '' && is_user_logged_in() ) || current_user_can( $needed_role ) ) {
728
            return true;
729
        }
730
731
        $roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
732
        foreach ( $roles as $role ) {
733
			if ( current_user_can( $role ) ) {
734
        		return true;
735
			}
736
        	if ( $role == $needed_role ) {
737
        		break;
738
			}
739
        }
740
        return false;
741
    }
742
743
    /**
744
     * Make sure administrators can see Formidable menu
745
     *
746
     * @since 2.0
747
     */
748
    public static function maybe_add_permissions() {
749
		self::force_capability( 'frm_view_entries' );
750
751
        if ( ! current_user_can('administrator') || current_user_can('frm_view_forms') ) {
752
            return;
753
        }
754
755
		$user_id = get_current_user_id();
756
		$user = new WP_User( $user_id );
757
        $frm_roles = self::frm_capabilities();
758
        foreach ( $frm_roles as $frm_role => $frm_role_description ) {
759
			$user->add_cap( $frm_role );
760
            unset($frm_role, $frm_role_description);
761
        }
762
    }
763
764
	/**
765
	 * Make sure admins have permission to see the menu items
766
	 * @since 2.0.6
767
	 */
768
	public static function force_capability( $cap = 'frm_change_settings' ) {
769
		if ( current_user_can( 'administrator' ) && ! current_user_can( $cap ) ) {
770
			$role = get_role( 'administrator' );
771
			$frm_roles = self::frm_capabilities();
772
			foreach ( $frm_roles as $frm_role => $frm_role_description ) {
773
				$role->add_cap( $frm_role );
774
			}
775
		}
776
	}
777
778
    /**
779
     * Check if the user has permision for action.
780
     * Return permission message and stop the action if no permission
781
     * @since 2.0
782
     * @param string $permission
783
     */
784
	public static function permission_check( $permission, $show_message = 'show' ) {
785
        $permission_error = self::permission_nonce_error($permission);
786
        if ( $permission_error !== false ) {
787
            if ( 'hide' == $show_message ) {
788
                $permission_error = '';
789
            }
790
            wp_die($permission_error);
791
        }
792
    }
793
794
    /**
795
     * Check user permission and nonce
796
     * @since 2.0
797
     * @param string $permission
798
     * @return false|string The permission message or false if allowed
799
     */
800
	public static function permission_nonce_error( $permission, $nonce_name = '', $nonce = '' ) {
801
		if ( ! empty( $permission ) && ! current_user_can( $permission ) && ! current_user_can( 'administrator' ) ) {
802
			$frm_settings = self::get_settings();
803
			return $frm_settings->admin_permission;
804
		}
805
806
		$error = false;
807
        if ( empty($nonce_name) ) {
808
            return $error;
809
        }
810
811
        if ( $_REQUEST && ( ! isset( $_REQUEST[ $nonce_name ] ) || ! wp_verify_nonce( $_REQUEST[ $nonce_name ], $nonce ) ) ) {
812
            $frm_settings = self::get_settings();
813
            $error = $frm_settings->admin_permission;
814
        }
815
816
        return $error;
817
    }
818
819
    public static function checked( $values, $current ) {
820
		if ( self::check_selected( $values, $current ) ) {
821
            echo ' checked="checked"';
822
		}
823
    }
824
825
	public static function check_selected( $values, $current ) {
826
		$values = self::recursive_function_map( $values, 'trim' );
827
		$values = self::recursive_function_map( $values, 'htmlspecialchars_decode' );
828
		$current = htmlspecialchars_decode( trim( $current ) );
829
830
		return ( is_array( $values ) && in_array( $current, $values ) ) || ( ! is_array( $values ) && $values == $current );
831
	}
832
833
    /**
834
    * Check if current field option is an "other" option
835
    *
836
    * @since 2.0
837
    *
838
    * @param string $opt_key
839
    * @return boolean Returns true if current field option is an "Other" option
840
    */
841
    public static function is_other_opt( $opt_key ) {
842
        _deprecated_function( __FUNCTION__, '2.0.6', 'FrmFieldsHelper::is_other_opt' );
843
        return FrmFieldsHelper::is_other_opt( $opt_key );
844
    }
845
846
    /**
847
    * Get value that belongs in "Other" text box
848
    *
849
    * @since 2.0
850
    *
851
    * @param string $opt_key
852
    * @param array $field
853
    * @return string $other_val
854
    */
855
    public static function get_other_val( $opt_key, $field, $parent = false, $pointer = false ) {
856
		_deprecated_function( __FUNCTION__, '2.0.6', 'FrmFieldsHelper::get_other_val' );
857
		return FrmFieldsHelper::get_other_val( compact( 'opt_key', 'field', 'parent', 'pointer' ) );
858
    }
859
860
    /**
861
    * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val.
862
    * Intended for front-end use
863
    *
864
    * @since 2.0
865
    *
866
    * @param array $field
867
    * @param boolean $other_opt
868
    * @param string $checked
869
    * @param array $args should include opt_key and field name
870
    * @return string $other_val
871
    */
872
    public static function prepare_other_input( $field, &$other_opt, &$checked, $args = array() ) {
873
		_deprecated_function( __FUNCTION__, '2.0.6', 'FrmFieldsHelper::prepare_other_input' );
874
		$args['field'] = $field;
875
		return FrmFieldsHelper::prepare_other_input( $args, $other_opt, $checked );
876
    }
877
878
	public static function recursive_function_map( $value, $function ) {
879
		if ( is_array( $value ) ) {
880
			$original_function = $function;
881
			if ( count( $value ) ) {
882
				$function = explode( ', ', self::prepare_array_values( $value, $function ) );
883
			} else {
884
				$function = array( $function );
885
			}
886
			if ( ! self::is_assoc( $value ) ) {
887
				$value = array_map( array( 'FrmAppHelper', 'recursive_function_map' ), $value, $function );
888
			} else {
889
				foreach ( $value as $k => $v ) {
890
					if ( ! is_array( $v ) ) {
891
						$value[ $k ] = call_user_func( $original_function, $v );
892
					}
893
				}
894
			}
895
		} else {
896
			$value = call_user_func( $function, $value );
897
		}
898
899
		return $value;
900
	}
901
902
	public static function is_assoc( $array ) {
903
		return (bool) count( array_filter( array_keys( $array ), 'is_string' ) );
904
	}
905
906
    /**
907
     * Flatten a multi-dimensional array
908
     */
909
	public static function array_flatten( $array, $keys = 'keep' ) {
910
        $return = array();
911
        foreach ( $array as $key => $value ) {
912
            if ( is_array($value) ) {
913
				$return = array_merge( $return, self::array_flatten( $value, $keys ) );
914
            } else {
915
				if ( $keys == 'keep' ) {
916
					$return[ $key ] = $value;
917
				} else {
918
					$return[] = $value;
919
				}
920
            }
921
        }
922
        return $return;
923
    }
924
925
	public static function esc_textarea( $text, $is_rich_text = false ) {
926
		$safe_text = str_replace( '&quot;', '"', $text );
927
		if ( ! $is_rich_text ) {
928
			$safe_text = htmlspecialchars( $safe_text, ENT_NOQUOTES );
929
		}
930
		$safe_text = str_replace( '&amp;', '&', $safe_text );
931
		return apply_filters( 'esc_textarea', $safe_text, $text );
932
	}
933
934
    /**
935
     * Add auto paragraphs to text areas
936
     * @since 2.0
937
     */
938
	public static function use_wpautop( $content ) {
939
        if ( apply_filters('frm_use_wpautop', true) ) {
940
            $content = wpautop(str_replace( '<br>', '<br />', $content));
941
        }
942
        return $content;
943
    }
944
945
	public static function replace_quotes( $val ) {
946
        //Replace double quotes
947
		$val = str_replace( array( '&#8220;', '&#8221;', '&#8243;' ), '"', $val );
948
        //Replace single quotes
949
        $val = str_replace( array( '&#8216;', '&#8217;', '&#8242;', '&prime;', '&rsquo;', '&lsquo;' ), "'", $val );
950
        return $val;
951
    }
952
953
    /**
954
     * @since 2.0
955
     * @return string The base Google APIS url for the current version of jQuery UI
956
     */
957
    public static function jquery_ui_base_url() {
958
		$url = 'http' . ( is_ssl() ? 's' : '' ) . '://ajax.googleapis.com/ajax/libs/jqueryui/' . self::script_version( 'jquery-ui-core', '1.11.4' );
959
        $url = apply_filters('frm_jquery_ui_base_url', $url);
960
        return $url;
961
    }
962
963
    /**
964
     * @param string $handle
965
     */
966
	public static function script_version( $handle, $default = 0 ) {
967
		global $wp_scripts;
968
		if ( ! $wp_scripts ) {
969
			return $default;
970
		}
971
972
		$ver = $default;
973
		if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
974
			return $ver;
975
		}
976
977
		$query = $wp_scripts->registered[ $handle ];
978
		if ( is_object( $query ) && ! empty( $query->ver ) ) {
979
			$ver = $query->ver;
980
		}
981
982
		return $ver;
983
	}
984
985
	public static function js_redirect( $url ) {
986
		return '<script type="text/javascript">window.location="' . esc_url_raw( $url ) . '"</script>';
987
    }
988
989
	public static function get_user_id_param( $user_id ) {
990
        if ( ! $user_id || empty($user_id) || is_numeric($user_id) ) {
991
            return $user_id;
992
        }
993
994
		if ( $user_id == 'current' ) {
995
			$user_id = get_current_user_id();
996
		} else {
997
            if ( is_email($user_id) ) {
998
                $user = get_user_by('email', $user_id);
999
            } else {
1000
                $user = get_user_by('login', $user_id);
1001
            }
1002
1003
            if ( $user ) {
1004
                $user_id = $user->ID;
1005
            }
1006
            unset($user);
1007
        }
1008
1009
        return $user_id;
1010
    }
1011
1012
	public static function get_file_contents( $filename, $atts = array() ) {
1013
        if ( ! is_file($filename) ) {
1014
            return false;
1015
        }
1016
1017
        extract($atts);
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
1018
        ob_start();
1019
        include($filename);
1020
        $contents = ob_get_contents();
1021
        ob_end_clean();
1022
        return $contents;
1023
    }
1024
1025
    /**
1026
     * @param string $table_name
1027
     * @param string $column
1028
	 * @param int $id
1029
	 * @param int $num_chars
1030
     */
1031
    public static function get_unique_key( $name = '', $table_name, $column, $id = 0, $num_chars = 5 ) {
1032
        $key = '';
1033
1034
        if ( ! empty( $name ) ) {
1035
            $key = sanitize_key($name);
1036
        }
1037
1038
		if ( empty( $key ) ) {
1039
            $max_slug_value = pow(36, $num_chars);
1040
            $min_slug_value = 37; // we want to have at least 2 characters in the slug
1041
            $key = base_convert( rand($min_slug_value, $max_slug_value), 10, 36 );
1042
        }
1043
1044
		if ( is_numeric($key) || in_array( $key, array( 'id', 'key', 'created-at', 'detaillink', 'editlink', 'siteurl', 'evenodd' ) ) ) {
1045
			$key = $key . 'a';
1046
        }
1047
1048
		$key_check = FrmDb::get_var( $table_name, array( $column => $key, 'ID !' => $id ), $column );
1049
1050
        if ( $key_check || is_numeric($key_check) ) {
1051
            $suffix = 2;
1052
			do {
1053
				$alt_post_name = substr( $key, 0, 200 - ( strlen( $suffix ) + 1 ) ) . $suffix;
1054
				$key_check = FrmDb::get_var( $table_name, array( $column => $alt_post_name, 'ID !' => $id ), $column );
1055
				$suffix++;
1056
			} while ( $key_check || is_numeric( $key_check ) );
1057
			$key = $alt_post_name;
1058
        }
1059
        return $key;
1060
    }
1061
1062
    /**
1063
     * Editing a Form or Entry
1064
     * @param string $table
1065
     * @return bool|array
1066
     */
1067
    public static function setup_edit_vars( $record, $table, $fields = '', $default = false, $post_values = array(), $args = array() ) {
1068
        if ( ! $record ) {
1069
            return false;
1070
        }
1071
1072
        if ( empty($post_values) ) {
1073
            $post_values = stripslashes_deep($_POST);
1074
        }
1075
1076
		$values = array( 'id' => $record->id, 'fields' => array() );
1077
1078
		foreach ( array( 'name', 'description' ) as $var ) {
1079
            $default_val = isset($record->{$var}) ? $record->{$var} : '';
1080
			$values[ $var ] = self::get_param( $var, $default_val, 'get', 'wp_kses_post' );
1081
            unset($var, $default_val);
1082
        }
1083
1084
        $values['description'] = self::use_wpautop($values['description']);
1085
        $frm_settings = self::get_settings();
1086
        $is_form_builder = self::is_admin_page('formidable' );
1087
1088
        foreach ( (array) $fields as $field ) {
1089
            // Make sure to filter default values (for placeholder text), but not on the form builder page
1090
            if ( ! $is_form_builder ) {
1091
                $field->default_value = apply_filters('frm_get_default_value', $field->default_value, $field, true );
1092
            }
1093
			$parent_form_id = isset( $args['parent_form_id'] ) ? $args['parent_form_id'] : $field->form_id;
1094
			self::fill_field_defaults($field, $record, $values, compact('default', 'post_values', 'frm_settings', 'parent_form_id' ) );
1095
        }
1096
1097
        self::fill_form_opts($record, $table, $post_values, $values);
1098
1099
        if ( $table == 'entries' ) {
1100
            $values = FrmEntriesHelper::setup_edit_vars( $values, $record );
1101
        } else if ( $table == 'forms' ) {
1102
            $values = FrmFormsHelper::setup_edit_vars( $values, $record, $post_values );
1103
        }
1104
1105
        return $values;
1106
    }
1107
1108
	private static function fill_field_defaults( $field, $record, array &$values, $args ) {
1109
        $post_values = $args['post_values'];
1110
1111
        if ( $args['default'] ) {
1112
            $meta_value = $field->default_value;
1113
        } else {
1114
            if ( $record->post_id && self::pro_is_installed() && isset($field->field_options['post_field']) && $field->field_options['post_field'] ) {
1115
                if ( ! isset($field->field_options['custom_field']) ) {
1116
                    $field->field_options['custom_field'] = '';
1117
                }
1118
				$meta_value = FrmProEntryMetaHelper::get_post_value( $record->post_id, $field->field_options['post_field'], $field->field_options['custom_field'], array( 'truncate' => false, 'type' => $field->type, 'form_id' => $field->form_id, 'field' => $field ) );
1119
            } else {
1120
				$meta_value = FrmEntryMeta::get_meta_value( $record, $field->id );
1121
            }
1122
        }
1123
1124
		$field_type = isset( $post_values['field_options'][ 'type_' . $field->id ] ) ? $post_values['field_options'][ 'type_' . $field->id ] : $field->type;
1125
        $new_value = isset( $post_values['item_meta'][ $field->id ] ) ? maybe_unserialize( $post_values['item_meta'][ $field->id ] ) : $meta_value;
1126
1127
        $field_array = array(
1128
            'id'            => $field->id,
1129
            'value'         => $new_value,
1130
            'default_value' => $field->default_value,
1131
            'name'          => $field->name,
1132
            'description'   => $field->description,
1133
            'type'          => apply_filters('frm_field_type', $field_type, $field, $new_value),
1134
            'options'       => $field->options,
1135
            'required'      => $field->required,
1136
            'field_key'     => $field->field_key,
1137
            'field_order'   => $field->field_order,
1138
            'form_id'       => $field->form_id,
1139
			'parent_form_id' => $args['parent_form_id'],
1140
        );
1141
1142
        $args['field_type'] = $field_type;
1143
        self::fill_field_opts($field, $field_array, $args);
1144
		// Track the original field's type
1145
		$field_array['original_type'] = isset( $field->field_options['original_type'] ) ? $field->field_options['original_type'] : $field->type;
1146
1147
        $field_array = apply_filters( 'frm_setup_edit_fields_vars', $field_array, $field, $values['id'], array() );
1148
1149
        if ( ! isset($field_array['unique']) || ! $field_array['unique'] ) {
1150
            $field_array['unique_msg'] = '';
1151
        }
1152
1153
        $field_array = array_merge( $field->field_options, $field_array );
1154
1155
        $values['fields'][ $field->id ] = $field_array;
1156
    }
1157
1158
	private static function fill_field_opts( $field, array &$field_array, $args ) {
1159
        $post_values = $args['post_values'];
1160
        $opt_defaults = FrmFieldsHelper::get_default_field_opts($field_array['type'], $field, true);
1161
1162
        foreach ( $opt_defaults as $opt => $default_opt ) {
1163
			$field_array[ $opt ] = ( $post_values && isset( $post_values['field_options'][ $opt . '_' . $field->id ] ) ) ? maybe_unserialize( $post_values['field_options'][ $opt . '_' . $field->id ] ) : ( isset( $field->field_options[ $opt ] ) ? $field->field_options[ $opt ] : $default_opt );
1164
            if ( $opt == 'blank' && $field_array[ $opt ] == '' ) {
1165
                $field_array[ $opt ] = $args['frm_settings']->blank_msg;
1166
            } else if ( $opt == 'invalid' && $field_array[ $opt ] == '' ) {
1167
                if ( $args['field_type'] == 'captcha' ) {
1168
                    $field_array[ $opt ] = $args['frm_settings']->re_msg;
1169
                } else {
1170
                    $field_array[ $opt ] = sprintf( __( '%s is invalid', 'formidable' ), $field_array['name'] );
1171
                }
1172
            }
1173
        }
1174
1175
        if ( $field_array['custom_html'] == '' ) {
1176
            $field_array['custom_html'] = FrmFieldsHelper::get_default_html($args['field_type']);
1177
        }
1178
    }
1179
1180
    /**
1181
     * @param string $table
1182
     */
1183
	private static function fill_form_opts( $record, $table, $post_values, array &$values ) {
1184
        if ( $table == 'entries' ) {
1185
            $form = $record->form_id;
1186
			FrmForm::maybe_get_form( $form );
1187
        } else {
1188
            $form = $record;
1189
        }
1190
1191
        if ( ! $form ) {
1192
            return;
1193
        }
1194
1195
        $values['form_name'] = isset($record->form_id) ? $form->name : '';
1196
		$values['parent_form_id'] = isset( $record->form_id ) ? $form->parent_form_id : 0;
1197
1198
        if ( ! is_array($form->options) ) {
1199
            return;
1200
        }
1201
1202
        foreach ( $form->options as $opt => $value ) {
1203
            $values[ $opt ] = isset( $post_values[ $opt ] ) ? maybe_unserialize( $post_values[ $opt ] ) : $value;
1204
        }
1205
1206
        self::fill_form_defaults($post_values, $values);
1207
    }
1208
1209
    /**
1210
     * Set to POST value or default
1211
     */
1212
	private static function fill_form_defaults( $post_values, array &$values ) {
1213
        $form_defaults = FrmFormsHelper::get_default_opts();
1214
1215
        foreach ( $form_defaults as $opt => $default ) {
1216
            if ( ! isset( $values[ $opt ] ) || $values[ $opt ] == '' ) {
1217
				$values[ $opt ] = ( $post_values && isset( $post_values['options'][ $opt ] ) ) ? $post_values['options'][ $opt ] : $default;
1218
            }
1219
1220
            unset($opt, $defaut);
1221
        }
1222
1223
		if ( ! isset( $values['custom_style'] ) ) {
1224
			$values['custom_style'] = self::custom_style_value( $post_values );
1225
		}
1226
1227
		foreach ( array( 'before', 'after', 'submit' ) as $h ) {
1228
			if ( ! isset( $values[ $h . '_html' ] ) ) {
1229
				$values[ $h . '_html' ] = ( isset( $post_values['options'][ $h . '_html' ] ) ? $post_values['options'][ $h . '_html' ] : FrmFormsHelper::get_default_html( $h ) );
1230
            }
1231
            unset($h);
1232
        }
1233
    }
1234
1235
	/**
1236
	 * @since 2.2.10
1237
	 * @param array $post_values
1238
	 * @return boolean|int
1239
	 */
1240
	public static function custom_style_value( $post_values ) {
1241
		if ( ! empty( $post_values ) && isset( $post_values['options']['custom_style'] ) ) {
1242
			$custom_style = absint( $post_values['options']['custom_style'] );
1243
		} else {
1244
			$frm_settings = FrmAppHelper::get_settings();
1245
			$custom_style = ( $frm_settings->load_style != 'none' );
1246
		}
1247
		return $custom_style;
1248
	}
1249
1250
	public static function get_meta_value( $field_id, $entry ) {
1251
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryMeta::get_meta_value' );
1252
		return FrmEntryMeta::get_meta_value( $entry, $field_id );
1253
	}
1254
1255
	public static function insert_opt_html( $args ) {
1256
        $class = '';
1257
        if ( in_array( $args['type'], array( 'email', 'user_id', 'hidden', 'select', 'radio', 'checkbox', 'phone', 'text' ) ) ) {
1258
            $class .= 'show_frm_not_email_to';
1259
        }
1260
    ?>
1261
<li>
1262
    <a href="javascript:void(0)" class="frmids frm_insert_code alignright <?php echo esc_attr($class) ?>" data-code="<?php echo esc_attr($args['id']) ?>" >[<?php echo esc_attr( $args['id'] ) ?>]</a>
1263
    <a href="javascript:void(0)" class="frmkeys frm_insert_code alignright <?php echo esc_attr($class) ?>" data-code="<?php echo esc_attr($args['key']) ?>" >[<?php echo esc_attr( self::truncate($args['key'], 10) ) ?>]</a>
1264
    <a href="javascript:void(0)" class="frm_insert_code <?php echo esc_attr( $class ) ?>" data-code="<?php echo esc_attr($args['id']) ?>" ><?php echo esc_attr( self::truncate($args['name'], 60) ) ?></a>
1265
</li>
1266
    <?php
1267
    }
1268
1269
	public static function truncate( $str, $length, $minword = 3, $continue = '...' ) {
1270
        if ( is_array( $str ) ) {
1271
            return '';
1272
		}
1273
1274
        $length = (int) $length;
1275
		$str = wp_strip_all_tags( $str );
1276
		$original_len = self::mb_function( array( 'mb_strlen', 'strlen' ), array( $str ) );
1277
1278
		if ( $length == 0 ) {
1279
            return '';
1280
        } else if ( $length <= 10 ) {
1281
			$sub = self::mb_function( array( 'mb_substr', 'substr' ), array( $str, 0, $length ) );
1282
            return $sub . (($length < $original_len) ? $continue : '');
1283
        }
1284
1285
        $sub = '';
1286
        $len = 0;
1287
1288
		$words = self::mb_function( array( 'mb_split', 'explode' ), array( ' ', $str ) );
1289
1290
		foreach ( $words as $word ) {
1291
            $part = (($sub != '') ? ' ' : '') . $word;
1292
			$total_len = self::mb_function( array( 'mb_strlen', 'strlen' ), array( $sub . $part ) );
1293
            if ( $total_len > $length && str_word_count($sub) ) {
1294
                break;
1295
            }
1296
1297
            $sub .= $part;
1298
			$len += self::mb_function( array( 'mb_strlen', 'strlen' ), array( $part ) );
1299
1300
            if ( str_word_count($sub) > $minword && $total_len >= $length ) {
1301
                break;
1302
            }
1303
1304
            unset($total_len, $word);
1305
        }
1306
1307
        return $sub . (($len < $original_len) ? $continue : '');
1308
    }
1309
1310
	public static function mb_function( $function_names, $args ) {
1311
		$mb_function_name = $function_names[0];
1312
		$function_name = $function_names[1];
1313
		if ( function_exists( $mb_function_name ) ) {
1314
			$function_name = $mb_function_name;
1315
		}
1316
		return call_user_func_array( $function_name, $args );
1317
	}
1318
1319
	public static function get_formatted_time( $date, $date_format = '', $time_format = '' ) {
1320
        if ( empty($date) ) {
1321
            return $date;
1322
        }
1323
1324
        if ( empty($date_format) ) {
1325
            $date_format = get_option('date_format');
1326
        }
1327
1328
        if ( preg_match('/^\d{1-2}\/\d{1-2}\/\d{4}$/', $date) && self::pro_is_installed() ) {
1329
            $frmpro_settings = new FrmProSettings();
1330
            $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
1331
        }
1332
1333
		$formatted = self::get_localized_date( $date_format, $date );
1334
1335
		$do_time = ( date( 'H:i:s', strtotime( $date ) ) != '00:00:00' );
1336
		if ( $do_time ) {
1337
			$formatted .= self::add_time_to_date( $time_format, $date );
1338
		}
1339
1340
        return $formatted;
1341
    }
1342
1343
	private static function add_time_to_date( $time_format, $date ) {
1344
		if ( empty( $time_format ) ) {
1345
			$time_format = get_option('time_format');
1346
		}
1347
1348
		$trimmed_format = trim( $time_format );
1349
		$time = '';
1350
		if ( $time_format && ! empty( $trimmed_format ) ) {
1351
			$time = ' ' . __( 'at', 'formidable' ) . ' ' . self::get_localized_date( $time_format, $date );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
1352
		}
1353
1354
		return $time;
1355
	}
1356
1357
	/**
1358
	 * @since 2.0.8
1359
	 */
1360
	public static function get_localized_date( $date_format, $date ) {
1361
		$date = get_date_from_gmt( $date );
1362
		return date_i18n( $date_format, strtotime( $date ) );
1363
	}
1364
1365
	/**
1366
	 * Gets the time ago in words
1367
	 *
1368
	 * @param int $from in seconds
1369
	 * @param int|string $to in seconds
1370
	 * @return string $time_ago
1371
	 */
1372
	public static function human_time_diff( $from, $to = '', $levels = 1 ) {
1373
		if ( empty( $to ) ) {
1374
			$now = new DateTime;
1375
		} else {
1376
			$now = new DateTime( '@' . $to );
1377
		}
1378
		$ago = new DateTime( '@' . $from );
1379
1380
		// Get the time difference
1381
		$diff_object = $now->diff( $ago );
1382
		$diff = get_object_vars( $diff_object );
1383
1384
		// Add week amount and update day amount
1385
		$diff['w'] = floor( $diff['d'] / 7 );
1386
		$diff['d'] -= $diff['w'] * 7;
1387
1388
		$time_strings = self::get_time_strings();
1389
1390
		foreach ( $time_strings as $k => $v ) {
1391
			if ( $diff[ $k ] ) {
1392
				$time_strings[ $k ] = $diff[ $k ] . ' ' . ( $diff[ $k ] > 1 ? $v[1] : $v[0] );
1393
			} else {
1394
				unset( $time_strings[ $k ] );
1395
			}
1396
		}
1397
1398
		$levels_deep = apply_filters( 'frm_time_ago_levels', $levels, compact( 'time_strings', 'from', 'to' ) );
1399
		$time_strings = array_slice( $time_strings, 0, $levels_deep );
1400
		$time_ago_string = $time_strings ? implode( ' ', $time_strings ) : '0 ' . __( 'seconds', 'formidable' );
1401
1402
		return $time_ago_string;
1403
	}
1404
1405
	/**
1406
	 * Get the translatable time strings
1407
	 *
1408
	 * @since 2.0.20
1409
	 * @return array
1410
	 */
1411
	private static function get_time_strings() {
1412
		return array(
1413
			'y' => array( __( 'year', 'formidable' ), __( 'years', 'formidable' ) ),
1414
			'm' => array( __( 'month', 'formidable' ), __( 'months', 'formidable' ) ),
1415
			'w' => array( __( 'week', 'formidable' ), __( 'weeks', 'formidable' ) ),
1416
			'd' => array( __( 'day', 'formidable' ), __( 'days', 'formidable' ) ),
1417
			'h' => array( __( 'hour', 'formidable' ), __( 'hours', 'formidable' ) ),
1418
			'i' => array( __( 'minute', 'formidable' ), __( 'minutes', 'formidable' ) ),
1419
			's' => array( __( 'second', 'formidable' ), __( 'seconds', 'formidable' ) ),
1420
		);
1421
	}
1422
1423
    /**
1424
     * Added for < WP 4.0 compatability
1425
     *
1426
     * @since 1.07.10
1427
     *
1428
     * @param string $term The value to escape
1429
     * @return string The escaped value
1430
     */
1431
	public static function esc_like( $term ) {
1432
        global $wpdb;
1433
        if ( method_exists($wpdb, 'esc_like') ) {
1434
			// WP 4.0
1435
            $term = $wpdb->esc_like( $term );
1436
        } else {
1437
            $term = like_escape( $term );
1438
        }
1439
1440
        return $term;
1441
    }
1442
1443
    /**
1444
     * @param string $order_query
1445
     */
1446
	public static function esc_order( $order_query ) {
1447
        if ( empty($order_query) ) {
1448
            return '';
1449
        }
1450
1451
        // remove ORDER BY before santizing
1452
        $order_query = strtolower($order_query);
1453
        if ( strpos($order_query, 'order by') !== false ) {
1454
            $order_query = str_replace('order by', '', $order_query);
1455
        }
1456
1457
        $order_query = explode(' ', trim($order_query));
1458
1459
        $order_fields = array(
1460
            'id', 'form_key', 'name', 'description',
1461
            'parent_form_id', 'logged_in', 'is_template',
1462
            'default_template', 'status', 'created_at',
1463
        );
1464
1465
        $order = trim(trim(reset($order_query), ','));
1466
        if ( ! in_array($order, $order_fields) ) {
1467
            return '';
1468
        }
1469
1470
        $order_by = '';
1471
        if ( count($order_query) > 1 ) {
1472
			$order_by = end( $order_query );
1473
			self::esc_order_by( $order_by );
1474
        }
1475
1476
		return ' ORDER BY ' . $order . ' ' . $order_by;
1477
    }
1478
1479
	/**
1480
	 * Make sure this is ordering by either ASC or DESC
1481
	 */
1482
	public static function esc_order_by( &$order_by ) {
1483
		$sort_options = array( 'asc', 'desc' );
1484
		if ( ! in_array( strtolower( $order_by ), $sort_options ) ) {
1485
			$order_by = 'asc';
1486
		}
1487
	}
1488
1489
    /**
1490
     * @param string $limit
1491
     */
1492
	public static function esc_limit( $limit ) {
1493
        if ( empty($limit) ) {
1494
            return '';
1495
        }
1496
1497
        $limit = trim(str_replace(' limit', '', strtolower($limit)));
1498
        if ( is_numeric($limit) ) {
1499
			return ' LIMIT ' . $limit;
1500
        }
1501
1502
        $limit = explode(',', trim($limit));
1503
        foreach ( $limit as $k => $l ) {
1504
            if ( is_numeric( $l ) ) {
1505
                $limit[ $k ] = $l;
1506
            }
1507
        }
1508
1509
        $limit = implode(',', $limit);
1510
		return ' LIMIT ' . $limit;
1511
    }
1512
1513
    /**
1514
     * Get an array of values ready to go through $wpdb->prepare
1515
     * @since 2.0
1516
     */
1517
    public static function prepare_array_values( $array, $type = '%s' ) {
1518
        $placeholders = array_fill(0, count($array), $type);
1519
        return implode(', ', $placeholders);
1520
    }
1521
1522
    public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
1523
        if ( empty($where) ) {
1524
            return '';
1525
        }
1526
1527
		if ( is_array( $where ) ) {
1528
            global $wpdb;
1529
            FrmDb::get_where_clause_and_values( $where, $starts_with );
1530
			$where = $wpdb->prepare( $where['where'], $where['values'] );
1531
		} else {
1532
            $where = $starts_with . $where;
1533
        }
1534
1535
        return $where;
1536
    }
1537
1538
    // Pagination Methods
1539
1540
    /**
1541
     * @param integer $current_p
1542
     */
1543
	public static function get_last_record_num( $r_count, $current_p, $p_size ) {
1544
		return ( ( $r_count < ( $current_p * $p_size ) ) ? $r_count : ( $current_p * $p_size ) );
1545
	}
1546
1547
    /**
1548
     * @param integer $current_p
1549
     */
1550
    public static function get_first_record_num( $r_count, $current_p, $p_size ) {
1551
        if ( $current_p == 1 ) {
1552
            return 1;
1553
        } else {
1554
            return ( self::get_last_record_num( $r_count, ( $current_p - 1 ), $p_size ) + 1 );
1555
        }
1556
    }
1557
1558
	/**
1559
	 * @return array
1560
	 */
1561
	public static function json_to_array( $json_vars ) {
1562
        $vars = array();
1563
        foreach ( $json_vars as $jv ) {
1564
            $jv_name = explode('[', $jv['name']);
1565
            $last = count($jv_name) - 1;
1566
            foreach ( $jv_name as $p => $n ) {
1567
                $name = trim($n, ']');
1568
                if ( ! isset($l1) ) {
1569
                    $l1 = $name;
1570
                }
1571
1572
                if ( ! isset($l2) ) {
1573
                    $l2 = $name;
1574
                }
1575
1576
                if ( ! isset($l3) ) {
1577
                    $l3 = $name;
1578
                }
1579
1580
                $this_val = ( $p == $last ) ? $jv['value'] : array();
1581
1582
                switch ( $p ) {
1583
                    case 0:
1584
                        $l1 = $name;
1585
                        self::add_value_to_array( $name, $l1, $this_val, $vars );
1586
                    break;
1587
1588
                    case 1:
1589
                        $l2 = $name;
1590
                        self::add_value_to_array( $name, $l2, $this_val, $vars[ $l1 ] );
1591
                    break;
1592
1593 View Code Duplication
                    case 2:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1594
                        $l3 = $name;
1595
                        self::add_value_to_array( $name, $l3, $this_val, $vars[ $l1 ][ $l2 ] );
1596
                    break;
1597
1598 View Code Duplication
                    case 3:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1599
                        $l4 = $name;
1600
                        self::add_value_to_array( $name, $l4, $this_val, $vars[ $l1 ][ $l2 ][ $l3 ] );
1601
                    break;
1602
                }
1603
1604
                unset($this_val, $n);
1605
            }
1606
1607
            unset($last, $jv);
1608
        }
1609
1610
        return $vars;
1611
    }
1612
1613
    /**
1614
     * @param string $name
1615
     * @param string $l1
1616
     */
1617
    public static function add_value_to_array( $name, $l1, $val, &$vars ) {
1618
        if ( $name == '' ) {
1619
            $vars[] = $val;
1620
        } else if ( ! isset( $vars[ $l1 ] ) ) {
1621
            $vars[ $l1 ] = $val;
1622
        }
1623
    }
1624
1625
	public static function maybe_add_tooltip( $name, $class = 'closed', $form_name = '' ) {
1626
        $tooltips = array(
1627
            'action_title'  => __( 'Give this action a label for easy reference.', 'formidable' ),
1628
            'email_to'      => __( 'Add one or more recipient addresses separated by a ",".  FORMAT: Name <[email protected]> or [email protected].  [admin_email] is the address set in WP General Settings.', 'formidable' ),
1629
            'cc'            => __( 'Add CC addresses separated by a ",".  FORMAT: Name <[email protected]> or [email protected].', 'formidable' ),
1630
            'bcc'           => __( 'Add BCC addresses separated by a ",".  FORMAT: Name <[email protected]> or [email protected].', 'formidable' ),
1631
            'reply_to'      => __( 'If you would like a different reply to address than the "from" address, add a single address here.  FORMAT: Name <[email protected]> or [email protected].', 'formidable' ),
1632
            'from'          => __( 'Enter the name and/or email address of the sender. FORMAT: John Bates <[email protected]> or [email protected].', 'formidable' ),
1633
            '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() ) ),
1634
        );
1635
1636
        if ( ! isset( $tooltips[ $name ] ) ) {
1637
            return;
1638
        }
1639
1640
        if ( 'open' == $class ) {
1641
            echo ' frm_help"';
1642
        } else {
1643
            echo ' class="frm_help"';
1644
        }
1645
1646
		echo ' title="' . esc_attr( $tooltips[ $name ] );
1647
1648
        if ( 'open' != $class ) {
1649
            echo '"';
1650
        }
1651
    }
1652
1653
	/**
1654
	 * Add the current_page class to that page in the form nav
1655
	 */
1656
	public static function select_current_page( $page, $current_page, $action = array() ) {
1657
		if ( $current_page != $page ) {
1658
			return;
1659
		}
1660
1661
		$frm_action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
1662
		if ( empty( $action ) || ( ! empty( $frm_action ) && in_array( $frm_action, $action ) ) ) {
1663
			echo ' class="current_page"';
1664
		}
1665
	}
1666
1667
    /**
1668
     * Prepare and json_encode post content
1669
     *
1670
     * @since 2.0
1671
     *
1672
     * @param array $post_content
1673
     * @return string $post_content ( json encoded array )
1674
     */
1675
    public static function prepare_and_encode( $post_content ) {
1676
        //Loop through array to strip slashes and add only the needed ones
1677
		foreach ( $post_content as $key => $val ) {
1678
			// Replace problematic characters (like &quot;)
1679
			$val = str_replace( '&quot;', '"', $val );
1680
1681
			self::prepare_action_slashes( $val, $key, $post_content );
1682
            unset( $key, $val );
1683
        }
1684
1685
        // json_encode the array
1686
        $post_content = json_encode( $post_content );
1687
1688
	    // add extra slashes for \r\n since WP strips them
1689
		$post_content = str_replace( array( '\\r', '\\n', '\\u', '\\t' ), array( '\\\\r', '\\\\n', '\\\\u', '\\\\t' ), $post_content );
1690
1691
        // allow for &quot
1692
	    $post_content = str_replace( '&quot;', '\\"', $post_content );
1693
1694
        return $post_content;
1695
    }
1696
1697
	private static function prepare_action_slashes( $val, $key, &$post_content ) {
1698
		if ( ! isset( $post_content[ $key ] ) ) {
1699
			return;
1700
		}
1701
1702
		if ( is_array( $val ) ) {
1703
			foreach ( $val as $k1 => $v1 ) {
1704
				self::prepare_action_slashes( $v1, $k1, $post_content[ $key ] );
1705
				unset( $k1, $v1 );
1706
			}
1707
		} else {
1708
			// Strip all slashes so everything is the same, no matter where the value is coming from
1709
			$val = stripslashes( $val );
1710
1711
			// Add backslashes before double quotes and forward slashes only
1712
			$post_content[ $key ] = addcslashes( $val, '"\\/' );
1713
		}
1714
	}
1715
1716
	/**
1717
	 * Prepare and save settings in styles and actions
1718
	 *
1719
	 * @param array $settings
1720
	 * @param string $group
1721
	 *
1722
	 * @since 2.0.6
1723
	 */
1724
	public static function save_settings( $settings, $group ) {
1725
		$settings = (array) $settings;
1726
		$settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] );
1727
1728
		if ( empty( $settings['ID'] ) ) {
1729
			unset( $settings['ID']);
1730
		}
1731
1732
		// delete all caches for this group
1733
		self::cache_delete_group( $group );
1734
1735
		return self::save_json_post( $settings );
1736
	}
1737
1738
	/**
1739
	 * Since actions are JSON encoded, we don't want any filters messing with it.
1740
	 * Remove the filters and then add them back in case any posts or views are
1741
	 * also being imported.
1742
	 *
1743
	 * Used when saving form actions and styles
1744
	 *
1745
	 * @since 2.0.4
1746
	 */
1747
	public static function save_json_post( $settings ) {
1748
		global $wp_filter;
1749
		$filters = $wp_filter['content_save_pre'];
1750
1751
		// Remove the balanceTags filter in case WordPress is trying to validate the XHTML
1752
		remove_all_filters( 'content_save_pre' );
1753
1754
		$post = wp_insert_post( $settings );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1755
1756
		// add the content filters back for views or posts
1757
		$wp_filter['content_save_pre'] = $filters;
1758
1759
		return $post;
1760
	}
1761
1762
	public static function maybe_json_decode( $string ) {
1763
        if ( is_array($string) ) {
1764
            return $string;
1765
        }
1766
1767
        $new_string = json_decode($string, true);
1768
        if ( function_exists('json_last_error') ) {
1769
			// php 5.3+
1770
            if ( json_last_error() == JSON_ERROR_NONE ) {
1771
                $string = $new_string;
1772
            }
1773
        } else if ( isset($new_string) ) {
1774
			// php < 5.3 fallback
1775
            $string = $new_string;
1776
        }
1777
        return $string;
1778
    }
1779
1780
    /**
1781
     * @since 1.07.10
1782
     *
1783
     * @param string $post_type The name of the post type that may need to be highlighted
1784
     * echo The javascript to open and highlight the Formidable menu
1785
     */
1786
	public static function maybe_highlight_menu( $post_type ) {
1787
        global $post;
1788
1789
        if ( isset($_REQUEST['post_type']) && $_REQUEST['post_type'] != $post_type ) {
1790
            return;
1791
        }
1792
1793
        if ( is_object($post) && $post->post_type != $post_type ) {
1794
            return;
1795
        }
1796
1797
        self::load_admin_wide_js();
1798
        echo '<script type="text/javascript">jQuery(document).ready(function(){frmSelectSubnav();});</script>';
1799
    }
1800
1801
    /**
1802
     * Load the JS file on non-Formidable pages in the admin area
1803
     * @since 2.0
1804
     */
1805
	public static function load_admin_wide_js( $load = true ) {
1806
        $version = FrmAppHelper::plugin_version();
1807
		wp_register_script( 'formidable_admin_global', FrmAppHelper::plugin_url() . '/js/formidable_admin_global.js', array( 'jquery' ), $version );
1808
1809
        wp_localize_script( 'formidable_admin_global', 'frmGlobal', array(
1810
			'updating_msg' => __( 'Please wait while your site updates.', 'formidable' ),
1811
            'deauthorize'  => __( 'Are you sure you want to deauthorize Formidable Forms on this site?', 'formidable' ),
1812
			'url'          => FrmAppHelper::plugin_url(),
1813
			'loading'      => __( 'Loading&hellip;' ),
1814
			'nonce'        => wp_create_nonce( 'frm_ajax' ),
1815
        ) );
1816
1817
		if ( $load ) {
1818
			wp_enqueue_script( 'formidable_admin_global' );
1819
		}
1820
    }
1821
1822
	/**
1823
	 * @since 2.0.9
1824
	 */
1825
	public static function load_font_style() {
1826
		wp_enqueue_style( 'frm_fonts', self::plugin_url() . '/css/frm_fonts.css', array(), self::plugin_version() );
1827
	}
1828
1829
    /**
1830
     * @param string $location
1831
     */
1832
	public static function localize_script( $location ) {
1833
		$ajax_url = admin_url( 'admin-ajax.php', is_ssl() ? 'admin' : 'http' );
1834
		$ajax_url = apply_filters( 'frm_ajax_url', $ajax_url );
1835
1836
		wp_localize_script( 'formidable', 'frm_js', array(
1837
			'ajax_url'  => $ajax_url,
1838
			'images_url' => self::plugin_url() . '/images',
1839
			'loading'   => __( 'Loading&hellip;' ),
1840
			'remove'    => __( 'Remove', 'formidable' ),
1841
			'offset'    => apply_filters( 'frm_scroll_offset', 4 ),
1842
			'nonce'     => wp_create_nonce( 'frm_ajax' ),
1843
			'id'        => __( 'ID', 'formidable' ),
1844
			'no_results' => __( 'No results match', 'formidable' ),
1845
			'file_spam' => __( 'That file looks like Spam.', 'formidable' ),
1846
			'empty_fields' => __( 'Please complete the preceding required fields before uploading a file.', 'formidable' ),
1847
		) );
1848
1849
		if ( $location == 'admin' ) {
1850
			$frm_settings = self::get_settings();
1851
			wp_localize_script( 'formidable_admin', 'frm_admin_js', array(
1852
				'confirm_uninstall' => __( 'Are you sure you want to do this? Clicking OK will delete all forms, form data, and all other Formidable data. There is no Undo.', 'formidable' ),
1853
				'desc'              => __( '(Click to add description)', 'formidable' ),
1854
				'blank'             => __( '(Blank)', 'formidable' ),
1855
				'no_label'          => __( '(no label)', 'formidable' ),
1856
				'saving'            => esc_attr( __( 'Saving', 'formidable' ) ),
1857
				'saved'             => esc_attr( __( 'Saved', 'formidable' ) ),
1858
				'ok'                => __( 'OK' ),
1859
				'cancel'            => __( 'Cancel', 'formidable' ),
1860
				'default'           => __( 'Default', 'formidable' ),
1861
				'clear_default'     => __( 'Clear default value when typing', 'formidable' ),
1862
				'no_clear_default'  => __( 'Do not clear default value when typing', 'formidable' ),
1863
				'valid_default'     => __( 'Default value will pass form validation', 'formidable' ),
1864
				'no_valid_default'  => __( 'Default value will NOT pass form validation', 'formidable' ),
1865
				'confirm'           => __( 'Are you sure?', 'formidable' ),
1866
				'conf_delete'       => __( 'Are you sure you want to delete this field and all data associated with it?', 'formidable' ),
1867
				'conf_delete_sec'   => __( 'WARNING: This will delete all fields inside of the section as well.', 'formidable' ),
1868
				'conf_no_repeat'    => __( 'Warning: If you have entries with multiple rows, all but the first row will be lost.', 'formidable' ),
1869
				'default_unique'    => $frm_settings->unique_msg,
1870
				'default_conf'      => __( 'The entered values do not match', 'formidable' ),
1871
				'enter_email'       => __( 'Enter Email', 'formidable' ),
1872
				'confirm_email'     => __( 'Confirm Email', 'formidable' ),
1873
				'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' ),
1874
				'enter_password'    => __( 'Enter Password', 'formidable' ),
1875
				'confirm_password'  => __( 'Confirm Password', 'formidable' ),
1876
				'import_complete'   => __( 'Import Complete', 'formidable' ),
1877
				'updating'          => __( 'Please wait while your site updates.', 'formidable' ),
1878
				'no_save_warning'   => __( 'Warning: There is no way to retrieve unsaved entries.', 'formidable' ),
1879
				'private'           => __( 'Private' ),
1880
				'jquery_ui_url'     => self::jquery_ui_base_url(),
1881
				'no_licenses'       => __( 'No new licenses were found', 'formidable' ),
1882
				'repeat_limit_min'  => __( 'Please enter a Repeat Limit that is greater than 1.', 'formidable' ),
1883
			) );
1884
		}
1885
	}
1886
1887
    /**
1888
	 * echo the message on the plugins listing page
1889
     * @since 1.07.10
1890
     *
1891
     * @param float $min_version The version the add-on requires
1892
     */
1893
	public static function min_version_notice( $min_version ) {
1894
        $frm_version = self::plugin_version();
1895
1896
        // check if Formidable meets minimum requirements
1897
        if ( version_compare($frm_version, $min_version, '>=') ) {
1898
            return;
1899
        }
1900
1901
        $wp_list_table = _get_list_table('WP_Plugins_List_Table');
1902
		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">' .
1903
        __( 'You are running an outdated version of Formidable. This plugin may not work correctly if you do not update Formidable.', 'formidable' ) .
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
1904
        '</div></td></tr>';
1905
    }
1906
1907
    public static function locales( $type = 'date' ) {
1908
        $locales = array(
1909
            'en' => __( 'English', 'formidable' ),    '' => __( 'English/Western', 'formidable' ),
1910
            'af' => __( 'Afrikaans', 'formidable' ),  'sq' => __( 'Albanian', 'formidable' ),
1911
            'ar' => __( 'Arabic', 'formidable' ),     'hy' => __( 'Armenian', 'formidable' ),
1912
            'az' => __( 'Azerbaijani', 'formidable' ), 'eu' => __( 'Basque', 'formidable' ),
1913
            'bs' => __( 'Bosnian', 'formidable' ),    'bg' => __( 'Bulgarian', 'formidable' ),
1914
            'ca' => __( 'Catalan', 'formidable' ),    'zh-HK' => __( 'Chinese Hong Kong', 'formidable' ),
1915
            'zh-CN' => __( 'Chinese Simplified', 'formidable' ), 'zh-TW' => __( 'Chinese Traditional', 'formidable' ),
1916
            'hr' => __( 'Croatian', 'formidable' ),   'cs' => __( 'Czech', 'formidable' ),
1917
            'da' => __( 'Danish', 'formidable' ),     'nl' => __( 'Dutch', 'formidable' ),
1918
            'en-GB' => __( 'English/UK', 'formidable' ), 'eo' => __( 'Esperanto', 'formidable' ),
1919
            'et' => __( 'Estonian', 'formidable' ),   'fo' => __( 'Faroese', 'formidable' ),
1920
            'fa' => __( 'Farsi/Persian', 'formidable' ), 'fil' => __( 'Filipino', 'formidable' ),
1921
            'fi' => __( 'Finnish', 'formidable' ),    'fr' => __( 'French', 'formidable' ),
1922
            'fr-CA' => __( 'French/Canadian', 'formidable' ), 'fr-CH' => __( 'French/Swiss', 'formidable' ),
1923
            'de' => __( 'German', 'formidable' ),     'de-AT' => __( 'German/Austria', 'formidable' ),
1924
            'de-CH' => __( 'German/Switzerland', 'formidable' ), 'el' => __( 'Greek', 'formidable' ),
1925
            'he' => __( 'Hebrew', 'formidable' ),     'iw' => __( 'Hebrew', 'formidable' ),
1926
            'hi' => __( 'Hindi', 'formidable' ),      'hu' => __( 'Hungarian', 'formidable' ),
1927
            'is' => __( 'Icelandic', 'formidable' ),  'id' => __( 'Indonesian', 'formidable' ),
1928
            'it' => __( 'Italian', 'formidable' ),    'ja' => __( 'Japanese', 'formidable' ),
1929
            'ko' => __( 'Korean', 'formidable' ),     'lv' => __( 'Latvian', 'formidable' ),
1930
            'lt' => __( 'Lithuanian', 'formidable' ), 'ms' => __( 'Malaysian', 'formidable' ),
1931
            'no' => __( 'Norwegian', 'formidable' ),  'pl' => __( 'Polish', 'formidable' ),
1932
            'pt' => __( 'Portuguese', 'formidable' ), 'pt-BR' => __( 'Portuguese/Brazilian', 'formidable' ),
1933
            'pt-PT' => __( 'Portuguese/Portugal', 'formidable' ), 'ro' => __( 'Romanian', 'formidable' ),
1934
            'ru' => __( 'Russian', 'formidable' ),    'sr' => __( 'Serbian', 'formidable' ),
1935
            'sr-SR' => __( 'Serbian', 'formidable' ), 'sk' => __( 'Slovak', 'formidable' ),
1936
            'sl' => __( 'Slovenian', 'formidable' ),  'es' => __( 'Spanish', 'formidable' ),
1937
            'es-419' => __( 'Spanish/Latin America', 'formidable' ), 'sv' => __( 'Swedish', 'formidable' ),
1938
            'ta' => __( 'Tamil', 'formidable' ),      'th' => __( 'Thai', 'formidable' ),
1939
            'tu' => __( 'Turkish', 'formidable' ),    'tr' => __( 'Turkish', 'formidable' ),
1940
            'uk' => __( 'Ukranian', 'formidable' ),   'vi' => __( 'Vietnamese', 'formidable' ),
1941
        );
1942
1943
        if ( $type == 'captcha' ) {
1944
            // remove the languages unavailable for the captcha
1945
            $unset = array(
1946
                '', 'af', 'sq', 'hy', 'az', 'eu', 'bs',
1947
                'zh-HK', 'eo', 'et', 'fo', 'fr-CH',
1948
                'he', 'is', 'ms', 'sr-SR', 'ta', 'tu',
1949
            );
1950
        } else {
1951
            // remove the languages unavailable for the datepicker
1952
            $unset = array(
1953
                'en', 'fil', 'fr-CA', 'de-AT', 'de-AT',
1954
                'de-CH', 'iw', 'hi', 'pt', 'pt-PT',
1955
                'es-419', 'tr',
1956
            );
1957
        }
1958
1959
        $locales = array_diff_key($locales, array_flip($unset));
1960
        $locales = apply_filters('frm_locales', $locales);
1961
1962
        return $locales;
1963
    }
1964
}
1965