Completed
Push — master ( 4fad70...ba7486 )
by Stephanie
03:57
created

FrmAppHelper::get_query_var()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
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 = 30; //version of the database we are moving to
8
	public static $pro_db_version = 34;
9
10
	/**
11
	 * @since 2.0
12
	 */
13
	public static $plug_version = '2.01.02b';
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 = add_query_arg( 'aff', $affiliate_id, $url );
64
		}
65
		return $url;
66
	}
67
68
	public static function get_affiliate() {
69
		$affiliate_id = apply_filters( 'frm_affiliate_link', get_option('frm_aff') );
70
		$affiliate_id = strtolower( $affiliate_id );
71
		$allowed_affiliates = array( 'mojo' );
72
		if ( ! in_array( $affiliate_id, $allowed_affiliates ) ) {
73
			$affiliate_id = false;
74
		}
75
		return $affiliate_id;
76
	}
77
78
    /**
79
     * Get the Formidable settings
80
     *
81
     * @since 2.0
82
     *
83
     * @param None
84
     * @return FrmSettings $frm_setings
85
     */
86
    public static function get_settings() {
87
        global $frm_settings;
88
        if ( empty($frm_settings) ) {
89
            $frm_settings = new FrmSettings();
90
        }
91
        return $frm_settings;
92
    }
93
94
	public static function get_menu_name() {
95
		$frm_settings = FrmAppHelper::get_settings();
96
		return $frm_settings->menu;
97
	}
98
99
    /**
100
     * Show a message in place of pro features
101
     *
102
     * @since 2.0
103
     */
104
	public static function update_message() {
105
		_deprecated_function( __FUNCTION__, '2.0.19' );
106
    }
107
108
    public static function pro_is_installed() {
109
        return apply_filters('frm_pro_installed', false);
110
    }
111
112
    /**
113
     * Check for certain page in Formidable settings
114
     *
115
     * @since 2.0
116
     *
117
     * @param string $page The name of the page to check
118
     * @return boolean
119
     */
120
	public static function is_admin_page( $page = 'formidable' ) {
121
        global $pagenow;
122
		$get_page = self::simple_get( 'page', 'sanitize_title' );
123
        if ( $pagenow ) {
124
			return $pagenow == 'admin.php' && $get_page == $page;
125
        }
126
127
		return is_admin() && $get_page == $page;
128
    }
129
130
    /**
131
     * Check for the form preview page
132
     *
133
     * @since 2.0
134
     *
135
     * @param None
136
     * @return boolean
137
     */
138
    public static function is_preview_page() {
139
        global $pagenow;
140
		$action = FrmAppHelper::simple_get( 'action', 'sanitize_title' );
141
		return $pagenow && $pagenow == 'admin-ajax.php' && $action == 'frm_forms_preview';
142
    }
143
144
    /**
145
     * Check for ajax except the form preview page
146
     *
147
     * @since 2.0
148
     *
149
     * @param None
150
     * @return boolean
151
     */
152
    public static function doing_ajax() {
153
        return defined('DOING_AJAX') && DOING_AJAX && ! self::is_preview_page();
154
    }
155
156
	/**
157
	 * @since 2.0.8
158
	 */
159
	public static function prevent_caching() {
160
		global $frm_vars;
161
		return isset( $frm_vars['prevent_caching'] ) && $frm_vars['prevent_caching'];
162
	}
163
164
    /**
165
     * Check if on an admin page
166
     *
167
     * @since 2.0
168
     *
169
     * @param None
170
     * @return boolean
171
     */
172
    public static function is_admin() {
173
        return is_admin() && ( ! defined('DOING_AJAX') || ! DOING_AJAX );
174
    }
175
176
    /**
177
     * Check if value contains blank value or empty array
178
     *
179
     * @since 2.0
180
     * @param $value - value to check
181
     * @return boolean
182
     */
183
    public static function is_empty_value( $value, $empty = '' ) {
184
        return ( is_array( $value ) && empty( $value ) ) || $value == $empty;
185
    }
186
187
    public static function is_not_empty_value( $value, $empty = '' ) {
188
        return ! self::is_empty_value( $value, $empty );
189
    }
190
191
    /**
192
     * Get any value from the $_SERVER
193
     *
194
     * @since 2.0
195
     * @param string $value
196
     * @return string
197
     */
198
	public static function get_server_value( $value ) {
199
        return isset( $_SERVER[ $value ] ) ? wp_strip_all_tags( $_SERVER[ $value ] ) : '';
0 ignored issues
show
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
200
    }
201
202
    /**
203
     * Check for the IP address in several places
204
     * Used by [ip] shortcode
205
     *
206
     * @return string The IP address of the current user
207
     */
208
    public static function get_ip_address() {
209
		$ip = '';
210
        foreach ( array(
211
            'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP',
212
            'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR',
213
        ) as $key ) {
214
            if ( ! isset( $_SERVER[ $key ] ) ) {
215
                continue;
216
            }
217
218
            foreach ( explode( ',', $_SERVER[ $key ] ) as $ip ) {
219
                $ip = trim($ip); // just to be safe
220
221
                if ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) {
222
                    return $ip;
223
                }
224
            }
225
        }
226
227
		return sanitize_text_field( $ip );
228
    }
229
230
    public static function get_param( $param, $default = '', $src = 'get', $sanitize = '' ) {
231
        if ( strpos($param, '[') ) {
232
            $params = explode('[', $param);
233
            $param = $params[0];
234
        }
235
236
		if ( $src == 'get' ) {
237
            $value = isset( $_POST[ $param ] ) ? stripslashes_deep( $_POST[ $param ] ) : ( isset( $_GET[ $param ] ) ? stripslashes_deep( $_GET[ $param ] ) : $default );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
238
            if ( ! isset( $_POST[ $param ] ) && isset( $_GET[ $param ] ) && ! is_array( $value ) ) {
239
                $value = stripslashes_deep( htmlspecialchars_decode( urldecode( $_GET[ $param ] ) ) );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
240
            }
241
			self::sanitize_value( $sanitize, $value );
242
		} else {
243
            $value = self::get_simple_request( array( 'type' => $src, 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) );
244
        }
245
246
		if ( isset( $params ) && is_array( $value ) && ! empty( $value ) ) {
247
            foreach ( $params as $k => $p ) {
248
                if ( ! $k || ! is_array($value) ) {
249
                    continue;
250
                }
251
252
                $p = trim($p, ']');
253
                $value = isset( $value[ $p ] ) ? $value[ $p ] : $default;
254
            }
255
        }
256
257
        return $value;
258
    }
259
260
	/**
261
	 *
262
	 * @param string $param
263
	 * @param mixed $default
264
	 * @param string $sanitize
265
	 */
266
	public static function get_post_param( $param, $default = '', $sanitize = '' ) {
267
		return self::get_simple_request( array( 'type' => 'post', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) );
268
	}
269
270
	/**
271
	 * @since 2.0
272
	 *
273
	 * @param string $param
274
	 * @param string $sanitize
275
	 * @param string $default
276
	 */
277
	public static function simple_get( $param, $sanitize = 'sanitize_text_field', $default = '' ) {
278
		return self::get_simple_request( array( 'type' => 'get', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) );
279
    }
280
281
	/**
282
	 * Get a GET/POST/REQUEST value and sanitize it
283
	 *
284
	 * @since 2.0.6
285
	 */
286
	public static function get_simple_request( $args ) {
287
		$defaults = array(
288
			'param' => '', 'default' => '',
289
			'type' => 'get', 'sanitize' => 'sanitize_text_field',
290
		);
291
		$args = wp_parse_args( $args, $defaults );
292
293
		$value = $args['default'];
294
		if ( $args['type'] == 'get' ) {
295
			if ( $_GET && isset( $_GET[ $args['param'] ] ) ) {
296
				$value = $_GET[ $args['param'] ];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
297
			}
298
		} else if ( $args['type'] == 'post' ) {
299
			if ( isset( $_POST[ $args['param'] ] ) ) {
300
				$value = stripslashes_deep( maybe_unserialize( $_POST[ $args['param'] ] ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
301
			}
302
		} else {
303
			if ( isset( $_REQUEST[ $args['param'] ] ) ) {
304
				$value = $_REQUEST[ $args['param'] ];
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
305
			}
306
		}
307
308
		self::sanitize_value( $args['sanitize'], $value );
309
		return $value;
310
	}
311
312
	/**
313
	* Preserve backslashes in a value, but make sure value doesn't get compounding slashes
314
	*
315
	* @since 2.0.8
316
	* @param string $value
317
	* @return string $value
318
	*/
319
	public static function preserve_backslashes( $value ) {
320
		// If backslashes have already been added, don't add them again
321
		if ( strpos( $value, '\\\\' ) === false ) {
322
			$value = addslashes( $value );
323
		}
324
		return $value;
325
	}
326
327
	public static function sanitize_value( $sanitize, &$value ) {
328
		if ( ! empty( $sanitize ) ) {
329
			if ( is_array( $value ) ) {
330
				$temp_values = $value;
331
				foreach ( $temp_values as $k => $v ) {
332
					FrmAppHelper::sanitize_value( $sanitize, $value[ $k ] );
333
				}
334
			} else {
335
				$value = call_user_func( $sanitize, $value );
336
			}
337
		}
338
	}
339
340
    public static function sanitize_request( $sanitize_method, &$values ) {
341
        $temp_values = $values;
342
        foreach ( $temp_values as $k => $val ) {
343
            if ( isset( $sanitize_method[ $k ] ) ) {
344
				$values[ $k ] = call_user_func( $sanitize_method[ $k ], $val );
345
            }
346
        }
347
    }
348
349
	public static function sanitize_array( &$values ) {
350
		$temp_values = $values;
351
		foreach ( $temp_values as $k => $val ) {
352
			$values[ $k ] = wp_kses_post( $val );
353
		}
354
	}
355
356
	/**
357
	 * Sanitize the value, and allow some HTML
358
	 * @since 2.0
359
	 */
360
	public static function kses( $value, $allowed = array() ) {
361
		$html = array(
362
		    'a' => array(
363
				'href'  => array(),
364
				'title' => array(),
365
				'id'    => array(),
366
				'class' => array(),
367
		    ),
368
		);
369
370
		$allowed_html = array();
371
		foreach ( $allowed as $a ) {
372
			$allowed_html[ $a ] = isset( $html[ $a ] ) ? $html[ $a ] : array();
373
		}
374
375
		return wp_kses( $value, $allowed_html );
376
	}
377
378
    /**
379
     * Used when switching the action for a bulk action
380
     * @since 2.0
381
     */
382
    public static function remove_get_action() {
383
        if ( ! isset($_GET) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
384
            return;
385
        }
386
387
        $new_action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : ( isset( $_GET['action2'] ) ? sanitize_text_field( $_GET['action2'] ) : '' );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
388
        if ( ! empty( $new_action ) ) {
389
			$_SERVER['REQUEST_URI'] = str_replace( '&action=' . $new_action, '', FrmAppHelper::get_server_value( 'REQUEST_URI' ) );
390
        }
391
    }
392
393
    /**
394
     * Check the WP query for a parameter
395
     *
396
     * @since 2.0
397
     * @return string|array
398
     */
399
    public static function get_query_var( $value, $param ) {
400
        if ( $value != '' ) {
401
            return $value;
402
        }
403
404
        global $wp_query;
405
        if ( isset( $wp_query->query_vars[ $param ] ) ) {
406
            $value = $wp_query->query_vars[ $param ];
407
        }
408
409
        return $value;
410
    }
411
412
    /**
413
     * @param string $type
414
     */
415
    public static function trigger_hook_load( $type, $object = null ) {
416
        // only load the form hooks once
417
		$hooks_loaded = apply_filters( 'frm_' . $type . '_hooks_loaded', false, $object );
418
        if ( ! $hooks_loaded ) {
419
			do_action( 'frm_load_' . $type . '_hooks' );
420
        }
421
    }
422
423
    /**
424
     * Check cache before fetching values and saving to cache
425
     *
426
     * @since 2.0
427
     *
428
     * @param string $cache_key The unique name for this cache
429
     * @param string $group The name of the cache group
430
     * @param string $query If blank, don't run a db call
431
     * @param string $type The wpdb function to use with this query
432
     * @return mixed $results The cache or query results
433
     */
434
    public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
435
        $results = wp_cache_get($cache_key, $group);
436
        if ( ! self::is_empty_value( $results, false ) || empty($query) ) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1465
        return $count;
1466
    }
1467
1468
    public static function get_referer_info() {
1469
        _deprecated_function( __FUNCTION__, '2.0', 'FrmAppHelper::get_server_value' );
1470
        return self::get_server_value('HTTP_REFERER');
1471
    }
1472
1473
	/**
1474
	 * @return array
1475
	 */
1476
	public static function json_to_array( $json_vars ) {
1477
        $vars = array();
1478
        foreach ( $json_vars as $jv ) {
1479
            $jv_name = explode('[', $jv['name']);
1480
            $last = count($jv_name) - 1;
1481
            foreach ( $jv_name as $p => $n ) {
1482
                $name = trim($n, ']');
1483
                if ( ! isset($l1) ) {
1484
                    $l1 = $name;
1485
                }
1486
1487
                if ( ! isset($l2) ) {
1488
                    $l2 = $name;
1489
                }
1490
1491
                if ( ! isset($l3) ) {
1492
                    $l3 = $name;
1493
                }
1494
1495
                $this_val = ( $p == $last ) ? $jv['value'] : array();
1496
1497
                switch ( $p ) {
1498
                    case 0:
1499
                        $l1 = $name;
1500
                        self::add_value_to_array( $name, $l1, $this_val, $vars );
1501
                    break;
1502
1503
                    case 1:
1504
                        $l2 = $name;
1505
                        self::add_value_to_array( $name, $l2, $this_val, $vars[ $l1 ] );
1506
                    break;
1507
1508 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...
1509
                        $l3 = $name;
1510
                        self::add_value_to_array( $name, $l3, $this_val, $vars[ $l1 ][ $l2 ] );
1511
                    break;
1512
1513 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...
1514
                        $l4 = $name;
1515
                        self::add_value_to_array( $name, $l4, $this_val, $vars[ $l1 ][ $l2 ][ $l3 ] );
1516
                    break;
1517
                }
1518
1519
                unset($this_val, $n);
1520
            }
1521
1522
            unset($last, $jv);
1523
        }
1524
1525
        return $vars;
1526
    }
1527
1528
    /**
1529
     * @param string $name
1530
     * @param string $l1
1531
     */
1532
    public static function add_value_to_array( $name, $l1, $val, &$vars ) {
1533
        if ( $name == '' ) {
1534
            $vars[] = $val;
1535
        } else if ( ! isset( $vars[ $l1 ] ) ) {
1536
            $vars[ $l1 ] = $val;
1537
        }
1538
    }
1539
1540
	public static function maybe_add_tooltip( $name, $class = 'closed', $form_name = '' ) {
1541
        $tooltips = array(
1542
            'action_title'  => __( 'Give this action a label for easy reference.', 'formidable' ),
1543
            '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' ),
1544
            'cc'            => __( 'Add CC addresses separated by a ",".  FORMAT: Name <[email protected]> or [email protected].', 'formidable' ),
1545
            'bcc'           => __( 'Add BCC addresses separated by a ",".  FORMAT: Name <[email protected]> or [email protected].', 'formidable' ),
1546
            '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' ),
1547
            'from'          => __( 'Enter the name and/or email address of the sender. FORMAT: John Bates <[email protected]> or [email protected].', 'formidable' ),
1548
            '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() ) ),
1549
        );
1550
1551
        if ( ! isset( $tooltips[ $name ] ) ) {
1552
            return;
1553
        }
1554
1555
        if ( 'open' == $class ) {
1556
            echo ' frm_help"';
1557
        } else {
1558
            echo ' class="frm_help"';
1559
        }
1560
1561
		echo ' title="' . esc_attr( $tooltips[ $name ] );
1562
1563
        if ( 'open' != $class ) {
1564
            echo '"';
1565
        }
1566
    }
1567
1568
	/**
1569
	 * Add the current_page class to that page in the form nav
1570
	 */
1571
	public static function select_current_page( $page, $current_page, $action = array() ) {
1572
		if ( $current_page != $page ) {
1573
			return;
1574
		}
1575
1576
		$frm_action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
1577
		if ( empty( $action ) || ( ! empty( $frm_action ) && in_array( $frm_action, $action ) ) ) {
1578
			echo ' class="current_page"';
1579
		}
1580
	}
1581
1582
    /**
1583
     * Prepare and json_encode post content
1584
     *
1585
     * @since 2.0
1586
     *
1587
     * @param array $post_content
1588
     * @return string $post_content ( json encoded array )
1589
     */
1590
    public static function prepare_and_encode( $post_content ) {
1591
        //Loop through array to strip slashes and add only the needed ones
1592
		foreach ( $post_content as $key => $val ) {
1593
			// Replace problematic characters (like &quot;)
1594
			$val = str_replace( '&quot;', '"', $val );
1595
1596
			self::prepare_action_slashes( $val, $key, $post_content );
1597
            unset( $key, $val );
1598
        }
1599
1600
        // json_encode the array
1601
        $post_content = json_encode( $post_content );
1602
1603
	    // add extra slashes for \r\n since WP strips them
1604
		$post_content = str_replace( array( '\\r', '\\n', '\\u', '\\t' ), array( '\\\\r', '\\\\n', '\\\\u', '\\\\t' ), $post_content );
1605
1606
        // allow for &quot
1607
	    $post_content = str_replace( '&quot;', '\\"', $post_content );
1608
1609
        return $post_content;
1610
    }
1611
1612
	private static function prepare_action_slashes( $val, $key, &$post_content ) {
1613
		if ( ! isset( $post_content[ $key ] ) ) {
1614
			return;
1615
		}
1616
1617
		if ( is_array( $val ) ) {
1618
			foreach ( $val as $k1 => $v1 ) {
1619
				self::prepare_action_slashes( $v1, $k1, $post_content[ $key ] );
1620
				unset( $k1, $v1 );
1621
			}
1622
		} else {
1623
			// Strip all slashes so everything is the same, no matter where the value is coming from
1624
			$val = stripslashes( $val );
1625
1626
			// Add backslashes before double quotes and forward slashes only
1627
			$post_content[ $key ] = addcslashes( $val, '"\\/' );
1628
		}
1629
	}
1630
1631
	/**
1632
	 * Prepare and save settings in styles and actions
1633
	 *
1634
	 * @param array $settings
1635
	 * @param string $group
1636
	 *
1637
	 * @since 2.0.6
1638
	 */
1639
	public static function save_settings( $settings, $group ) {
1640
		$settings = (array) $settings;
1641
		$settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] );
1642
1643
		if ( empty( $settings['ID'] ) ) {
1644
			unset( $settings['ID']);
1645
		}
1646
1647
		// delete all caches for this group
1648
		self::cache_delete_group( $group );
1649
1650
		return self::save_json_post( $settings );
1651
	}
1652
1653
	/**
1654
	 * Since actions are JSON encoded, we don't want any filters messing with it.
1655
	 * Remove the filters and then add them back in case any posts or views are
1656
	 * also being imported.
1657
	 *
1658
	 * Used when saving form actions and styles
1659
	 *
1660
	 * @since 2.0.4
1661
	 */
1662
	public static function save_json_post( $settings ) {
1663
		global $wp_filter;
1664
		$filters = $wp_filter['content_save_pre'];
1665
1666
		// Remove the balanceTags filter in case WordPress is trying to validate the XHTML
1667
		remove_all_filters( 'content_save_pre' );
1668
1669
		$post = wp_insert_post( $settings );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1670
1671
		// add the content filters back for views or posts
1672
		$wp_filter['content_save_pre'] = $filters;
1673
1674
		return $post;
1675
	}
1676
1677
	public static function maybe_json_decode( $string ) {
1678
        if ( is_array($string) ) {
1679
            return $string;
1680
        }
1681
1682
        $new_string = json_decode($string, true);
1683
        if ( function_exists('json_last_error') ) {
1684
			// php 5.3+
1685
            if ( json_last_error() == JSON_ERROR_NONE ) {
1686
                $string = $new_string;
1687
            }
1688
        } else if ( isset($new_string) ) {
1689
			// php < 5.3 fallback
1690
            $string = $new_string;
1691
        }
1692
        return $string;
1693
    }
1694
1695
    /**
1696
     * @since 1.07.10
1697
     *
1698
     * @param string $post_type The name of the post type that may need to be highlighted
1699
     * echo The javascript to open and highlight the Formidable menu
1700
     */
1701
	public static function maybe_highlight_menu( $post_type ) {
1702
        global $post;
1703
1704
        if ( isset($_REQUEST['post_type']) && $_REQUEST['post_type'] != $post_type ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
1705
            return;
1706
        }
1707
1708
        if ( is_object($post) && $post->post_type != $post_type ) {
1709
            return;
1710
        }
1711
1712
        self::load_admin_wide_js();
1713
        echo '<script type="text/javascript">jQuery(document).ready(function(){frmSelectSubnav();});</script>';
1714
    }
1715
1716
    /**
1717
     * Load the JS file on non-Formidable pages in the admin area
1718
     * @since 2.0
1719
     */
1720
	public static function load_admin_wide_js( $load = true ) {
1721
        $version = FrmAppHelper::plugin_version();
1722
		wp_register_script( 'formidable_admin_global', FrmAppHelper::plugin_url() . '/js/formidable_admin_global.js', array( 'jquery' ), $version );
1723
1724
        wp_localize_script( 'formidable_admin_global', 'frmGlobal', array(
1725
			'updating_msg' => __( 'Please wait while your site updates.', 'formidable' ),
1726
            'deauthorize'  => __( 'Are you sure you want to deauthorize Formidable Forms on this site?', 'formidable' ),
1727
			'url'          => FrmAppHelper::plugin_url(),
1728
			'loading'      => __( 'Loading&hellip;' ),
1729
			'nonce'        => wp_create_nonce( 'frm_ajax' ),
1730
        ) );
1731
1732
		if ( $load ) {
1733
			wp_enqueue_script( 'formidable_admin_global' );
1734
		}
1735
    }
1736
1737
	/**
1738
	 * @since 2.0.9
1739
	 */
1740
	public static function load_font_style() {
1741
		wp_enqueue_style( 'frm_fonts', self::plugin_url() . '/css/frm_fonts.css', array(), self::plugin_version() );
1742
	}
1743
1744
    /**
1745
     * @param string $location
1746
     */
1747
	public static function localize_script( $location ) {
1748
		$ajax_url = admin_url( 'admin-ajax.php', is_ssl() ? 'admin' : 'http' );
1749
		$ajax_url = apply_filters( 'frm_ajax_url', $ajax_url );
1750
1751
		wp_localize_script( 'formidable', 'frm_js', array(
1752
			'ajax_url'  => $ajax_url,
1753
			'images_url' => self::plugin_url() . '/images',
1754
			'loading'   => __( 'Loading&hellip;' ),
1755
			'remove'    => __( 'Remove', 'formidable' ),
1756
			'offset'    => apply_filters( 'frm_scroll_offset', 4 ),
1757
			'nonce'     => wp_create_nonce( 'frm_ajax' ),
1758
			'id'        => __( 'ID', 'formidable' ),
1759
		) );
1760
1761
		if ( $location == 'admin' ) {
1762
			$frm_settings = self::get_settings();
1763
			wp_localize_script( 'formidable_admin', 'frm_admin_js', array(
1764
				'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' ),
1765
				'desc'              => __( '(Click to add description)', 'formidable' ),
1766
				'blank'             => __( '(Blank)', 'formidable' ),
1767
				'no_label'          => __( '(no label)', 'formidable' ),
1768
				'saving'            => esc_attr( __( 'Saving', 'formidable' ) ),
1769
				'saved'             => esc_attr( __( 'Saved', 'formidable' ) ),
1770
				'ok'                => __( 'OK' ),
1771
				'cancel'            => __( 'Cancel', 'formidable' ),
1772
				'default'           => __( 'Default', 'formidable' ),
1773
				'clear_default'     => __( 'Clear default value when typing', 'formidable' ),
1774
				'no_clear_default'  => __( 'Do not clear default value when typing', 'formidable' ),
1775
				'valid_default'     => __( 'Default value will pass form validation', 'formidable' ),
1776
				'no_valid_default'  => __( 'Default value will NOT pass form validation', 'formidable' ),
1777
				'confirm'           => __( 'Are you sure?', 'formidable' ),
1778
				'conf_delete'       => __( 'Are you sure you want to delete this field and all data associated with it?', 'formidable' ),
1779
				'conf_delete_sec'   => __( 'WARNING: This will delete all fields inside of the section as well.', 'formidable' ),
1780
				'conf_no_repeat'    => __( 'Warning: If you have entries with multiple rows, all but the first row will be lost.', 'formidable' ),
1781
				'default_unique'    => $frm_settings->unique_msg,
1782
				'default_conf'      => __( 'The entered values do not match', 'formidable' ),
1783
				'enter_email'       => __( 'Enter Email', 'formidable' ),
1784
				'confirm_email'     => __( 'Confirm Email', 'formidable' ),
1785
				'enter_password'    => __( 'Enter Password', 'formidable' ),
1786
				'confirm_password'  => __( 'Confirm Password', 'formidable' ),
1787
				'import_complete'   => __( 'Import Complete', 'formidable' ),
1788
				'updating'          => __( 'Please wait while your site updates.', 'formidable' ),
1789
				'no_save_warning'   => __( 'Warning: There is no way to retrieve unsaved entries.', 'formidable' ),
1790
				'private'           => __( 'Private' ),
1791
				'jquery_ui_url'     => self::jquery_ui_base_url(),
1792
				'no_licenses'       => __( 'No new licenses were found', 'formidable' ),
1793
			) );
1794
		}
1795
	}
1796
1797
    /**
1798
	 * echo the message on the plugins listing page
1799
     * @since 1.07.10
1800
     *
1801
     * @param float $min_version The version the add-on requires
1802
     */
1803
	public static function min_version_notice( $min_version ) {
1804
        $frm_version = self::plugin_version();
1805
1806
        // check if Formidable meets minimum requirements
1807
        if ( version_compare($frm_version, $min_version, '>=') ) {
1808
            return;
1809
        }
1810
1811
        $wp_list_table = _get_list_table('WP_Plugins_List_Table');
1812
		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">' .
1813
        __( '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...
1814
        '</div></td></tr>';
1815
    }
1816
1817
    public static function locales( $type = 'date' ) {
1818
        $locales = array(
1819
            'en' => __( 'English', 'formidable' ),    '' => __( 'English/Western', 'formidable' ),
1820
            'af' => __( 'Afrikaans', 'formidable' ),  'sq' => __( 'Albanian', 'formidable' ),
1821
            'ar' => __( 'Arabic', 'formidable' ),     'hy' => __( 'Armenian', 'formidable' ),
1822
            'az' => __( 'Azerbaijani', 'formidable' ), 'eu' => __( 'Basque', 'formidable' ),
1823
            'bs' => __( 'Bosnian', 'formidable' ),    'bg' => __( 'Bulgarian', 'formidable' ),
1824
            'ca' => __( 'Catalan', 'formidable' ),    'zh-HK' => __( 'Chinese Hong Kong', 'formidable' ),
1825
            'zh-CN' => __( 'Chinese Simplified', 'formidable' ), 'zh-TW' => __( 'Chinese Traditional', 'formidable' ),
1826
            'hr' => __( 'Croatian', 'formidable' ),   'cs' => __( 'Czech', 'formidable' ),
1827
            'da' => __( 'Danish', 'formidable' ),     'nl' => __( 'Dutch', 'formidable' ),
1828
            'en-GB' => __( 'English/UK', 'formidable' ), 'eo' => __( 'Esperanto', 'formidable' ),
1829
            'et' => __( 'Estonian', 'formidable' ),   'fo' => __( 'Faroese', 'formidable' ),
1830
            'fa' => __( 'Farsi/Persian', 'formidable' ), 'fil' => __( 'Filipino', 'formidable' ),
1831
            'fi' => __( 'Finnish', 'formidable' ),    'fr' => __( 'French', 'formidable' ),
1832
            'fr-CA' => __( 'French/Canadian', 'formidable' ), 'fr-CH' => __( 'French/Swiss', 'formidable' ),
1833
            'de' => __( 'German', 'formidable' ),     'de-AT' => __( 'German/Austria', 'formidable' ),
1834
            'de-CH' => __( 'German/Switzerland', 'formidable' ), 'el' => __( 'Greek', 'formidable' ),
1835
            'he' => __( 'Hebrew', 'formidable' ),     'iw' => __( 'Hebrew', 'formidable' ),
1836
            'hi' => __( 'Hindi', 'formidable' ),      'hu' => __( 'Hungarian', 'formidable' ),
1837
            'is' => __( 'Icelandic', 'formidable' ),  'id' => __( 'Indonesian', 'formidable' ),
1838
            'it' => __( 'Italian', 'formidable' ),    'ja' => __( 'Japanese', 'formidable' ),
1839
            'ko' => __( 'Korean', 'formidable' ),     'lv' => __( 'Latvian', 'formidable' ),
1840
            'lt' => __( 'Lithuanian', 'formidable' ), 'ms' => __( 'Malaysian', 'formidable' ),
1841
            'no' => __( 'Norwegian', 'formidable' ),  'pl' => __( 'Polish', 'formidable' ),
1842
            'pt' => __( 'Portuguese', 'formidable' ), 'pt-BR' => __( 'Portuguese/Brazilian', 'formidable' ),
1843
            'pt-PT' => __( 'Portuguese/Portugal', 'formidable' ), 'ro' => __( 'Romanian', 'formidable' ),
1844
            'ru' => __( 'Russian', 'formidable' ),    'sr' => __( 'Serbian', 'formidable' ),
1845
            'sr-SR' => __( 'Serbian', 'formidable' ), 'sk' => __( 'Slovak', 'formidable' ),
1846
            'sl' => __( 'Slovenian', 'formidable' ),  'es' => __( 'Spanish', 'formidable' ),
1847
            'es-419' => __( 'Spanish/Latin America', 'formidable' ), 'sv' => __( 'Swedish', 'formidable' ),
1848
            'ta' => __( 'Tamil', 'formidable' ),      'th' => __( 'Thai', 'formidable' ),
1849
            'tu' => __( 'Turkish', 'formidable' ),    'tr' => __( 'Turkish', 'formidable' ),
1850
            'uk' => __( 'Ukranian', 'formidable' ),   'vi' => __( 'Vietnamese', 'formidable' ),
1851
        );
1852
1853
        if ( $type == 'captcha' ) {
1854
            // remove the languages unavailable for the captcha
1855
            $unset = array(
1856
                '', 'af', 'sq', 'hy', 'az', 'eu', 'bs',
1857
                'zh-HK', 'eo', 'et', 'fo', 'fr-CH',
1858
                'he', 'is', 'ms', 'sr-SR', 'ta', 'tu',
1859
            );
1860
        } else {
1861
            // remove the languages unavailable for the datepicker
1862
            $unset = array(
1863
                'en', 'fil', 'fr-CA', 'de-AT', 'de-AT',
1864
                'de-CH', 'iw', 'hi', 'pt', 'pt-PT',
1865
                'es-419', 'tr',
1866
            );
1867
        }
1868
1869
        $locales = array_diff_key($locales, array_flip($unset));
1870
        $locales = apply_filters('frm_locales', $locales);
1871
1872
        return $locales;
1873
    }
1874
}
1875