PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.6.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.6.3
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / evf-core-functions.php
everest-forms / includes Last commit date
abstracts 6 years ago admin 6 years ago export 6 years ago fields 6 years ago interfaces 8 years ago libraries 7 years ago log-handlers 6 years ago shortcodes 6 years ago templates 6 years ago class-everest-forms.php 6 years ago class-evf-ajax.php 6 years ago class-evf-autoloader.php 7 years ago class-evf-background-updater.php 7 years ago class-evf-cache-helper.php 6 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 7 years ago class-evf-emails.php 6 years ago class-evf-fields.php 6 years ago class-evf-form-block.php 6 years ago class-evf-form-handler.php 6 years ago class-evf-form-task.php 6 years ago class-evf-forms-features.php 6 years ago class-evf-frontend-scripts.php 6 years ago class-evf-install.php 6 years ago class-evf-integrations.php 7 years ago class-evf-log-levels.php 8 years ago class-evf-logger.php 6 years ago class-evf-post-types.php 6 years ago class-evf-privacy.php 6 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 7 years ago class-evf-smart-tags.php 6 years ago class-evf-template-loader.php 6 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 6 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 6 years ago evf-formatting-functions.php 6 years ago evf-notice-functions.php 6 years ago evf-template-functions.php 6 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 6 years ago
evf-core-functions.php
1993 lines
1 <?php
2 /**
3 * EverestForms Core Functions
4 *
5 * General core functions available on both the front-end and admin.
6 *
7 * @package EverestForms/Functions
8 * @version 1.0.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 // Include core functions (available in both admin and frontend).
14 require EVF_ABSPATH . 'includes/evf-conditional-functions.php';
15 require EVF_ABSPATH . 'includes/evf-deprecated-functions.php';
16 require EVF_ABSPATH . 'includes/evf-formatting-functions.php';
17 require EVF_ABSPATH . 'includes/evf-entry-functions.php';
18
19 /**
20 * Define a constant if it is not already defined.
21 *
22 * @since 1.0.0
23 * @param string $name Constant name.
24 * @param mixed $value Value.
25 */
26 function evf_maybe_define_constant( $name, $value ) {
27 if ( ! defined( $name ) ) {
28 define( $name, $value );
29 }
30 }
31
32 /**
33 * Get template part.
34 *
35 * EVF_TEMPLATE_DEBUG_MODE will prevent overrides in themes from taking priority.
36 *
37 * @param mixed $slug Template slug.
38 * @param string $name Template name (default: '').
39 */
40 function evf_get_template_part( $slug, $name = '' ) {
41 $cache_key = sanitize_key( implode( '-', array( 'template-part', $slug, $name, EVF_VERSION ) ) );
42 $template = (string) wp_cache_get( $cache_key, 'everest-forms' );
43
44 if ( ! $template ) {
45 if ( $name ) {
46 $template = EVF_TEMPLATE_DEBUG_MODE ? '' : locate_template(
47 array(
48 "{$slug}-{$name}.php",
49 evf()->template_path() . "{$slug}-{$name}.php",
50 )
51 );
52
53 if ( ! $template ) {
54 $fallback = evf()->plugin_path() . "/templates/{$slug}-{$name}.php";
55 $template = file_exists( $fallback ) ? $fallback : '';
56 }
57 }
58
59 if ( ! $template ) {
60 // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/everest-forms/slug.php.
61 $template = EVF_TEMPLATE_DEBUG_MODE ? '' : locate_template(
62 array(
63 "{$slug}.php",
64 evf()->template_path() . "{$slug}.php",
65 )
66 );
67 }
68
69 wp_cache_set( $cache_key, $template, 'everest-forms' );
70 }
71
72 // Allow 3rd party plugins to filter template file from their plugin.
73 $template = apply_filters( 'evf_get_template_part', $template, $slug, $name );
74
75 if ( $template ) {
76 load_template( $template, false );
77 }
78 }
79
80 /**
81 * Get other templates passing attributes and including the file.
82 *
83 * @param string $template_name Template name.
84 * @param array $args Arguments. (default: array).
85 * @param string $template_path Template path. (default: '').
86 * @param string $default_path Default path. (default: '').
87 */
88 function evf_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
89 $cache_key = sanitize_key( implode( '-', array( 'template', $template_name, $template_path, $default_path, EVF_VERSION ) ) );
90 $template = (string) wp_cache_get( $cache_key, 'everest-forms' );
91
92 if ( ! $template ) {
93 $template = evf_locate_template( $template_name, $template_path, $default_path );
94 wp_cache_set( $cache_key, $template, 'everest-forms' );
95 }
96
97 // Allow 3rd party plugin filter template file from their plugin.
98 $filter_template = apply_filters( 'evf_get_template', $template, $template_name, $args, $template_path, $default_path );
99
100 if ( $filter_template !== $template ) {
101 if ( ! file_exists( $filter_template ) ) {
102 /* translators: %s template */
103 evf_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'everest-forms' ), '<code>' . $template . '</code>' ), '1.0.0' );
104 return;
105 }
106 $template = $filter_template;
107 }
108
109 $action_args = array(
110 'template_name' => $template_name,
111 'template_path' => $template_path,
112 'located' => $template,
113 'args' => $args,
114 );
115
116 if ( ! empty( $args ) && is_array( $args ) ) {
117 if ( isset( $args['action_args'] ) ) {
118 evf_doing_it_wrong(
119 __FUNCTION__,
120 __( 'action_args should not be overwritten when calling evf_get_template.', 'everest-forms' ),
121 '1.4.9'
122 );
123 unset( $args['action_args'] );
124 }
125 extract( $args ); // @codingStandardsIgnoreLine
126 }
127
128 do_action( 'everest_forms_before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
129
130 include $action_args['located'];
131
132 do_action( 'everest_forms_after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
133 }
134
135 /**
136 * Like evf_get_template, but returns the HTML instead of outputting.
137 *
138 * @see evf_get_template
139 * @since 1.0.0
140 * @param string $template_name Template name.
141 * @param array $args Arguments. (default: array).
142 * @param string $template_path Template path. (default: '').
143 * @param string $default_path Default path. (default: '').
144 * @return string
145 */
146 function evf_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
147 ob_start();
148 evf_get_template( $template_name, $args, $template_path, $default_path );
149 return ob_get_clean();
150 }
151
152 /**
153 * Locate a template and return the path for inclusion.
154 *
155 * This is the load order:
156 *
157 * yourtheme/$template_path/$template_name
158 * yourtheme/$template_name
159 * $default_path/$template_name
160 *
161 * @param string $template_name Template name.
162 * @param string $template_path Template path. (default: '').
163 * @param string $default_path Default path. (default: '').
164 * @return string
165 */
166 function evf_locate_template( $template_name, $template_path = '', $default_path = '' ) {
167 if ( ! $template_path ) {
168 $template_path = evf()->template_path();
169 }
170
171 if ( ! $default_path ) {
172 $default_path = evf()->plugin_path() . '/templates/';
173 }
174
175 // Look within passed path within the theme - this is priority.
176 $template = locate_template(
177 array(
178 trailingslashit( $template_path ) . $template_name,
179 $template_name,
180 )
181 );
182
183 // Get default template/.
184 if ( ! $template || EVF_TEMPLATE_DEBUG_MODE ) {
185 $template = $default_path . $template_name;
186 }
187
188 // Return what we found.
189 return apply_filters( 'everest_forms_locate_template', $template, $template_name, $template_path );
190 }
191
192 /**
193 * Send HTML emails from EverestForms.
194 *
195 * @param mixed $to Receiver.
196 * @param mixed $subject Subject.
197 * @param mixed $message Message.
198 * @param string $headers Headers. (default: "Content-Type: text/html\r\n").
199 * @param string $attachments Attachments. (default: "").
200 */
201 function evf_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = '' ) {
202 $mailer = evf()->mailer();
203
204 $mailer->send( $to, $subject, $message, $headers, $attachments );
205 }
206
207 /**
208 * Queue some JavaScript code to be output in the footer.
209 *
210 * @param string $code Code.
211 */
212 function evf_enqueue_js( $code ) {
213 global $evf_queued_js;
214
215 if ( empty( $evf_queued_js ) ) {
216 $evf_queued_js = '';
217 }
218
219 $evf_queued_js .= "\n" . $code . "\n";
220 }
221
222 /**
223 * Output any queued javascript code in the footer.
224 */
225 function evf_print_js() {
226 global $evf_queued_js;
227
228 if ( ! empty( $evf_queued_js ) ) {
229 // Sanitize.
230 $evf_queued_js = wp_check_invalid_utf8( $evf_queued_js );
231 $evf_queued_js = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", $evf_queued_js );
232 $evf_queued_js = str_replace( "\r", '', $evf_queued_js );
233
234 $js = "<!-- Everest Forms JavaScript -->\n<script type=\"text/javascript\">\njQuery(function($) { $evf_queued_js });\n</script>\n";
235
236 /**
237 * Queued jsfilter.
238 *
239 * @since 1.0.0
240 * @param string $js JavaScript code.
241 */
242 echo apply_filters( 'everest_forms_queued_js', $js ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.EscapeOutput.OutputNotEscaped
243
244 unset( $evf_queued_js );
245 }
246 }
247
248 /**
249 * Set a cookie - wrapper for setcookie using WP constants.
250 *
251 * @param string $name Name of the cookie being set.
252 * @param string $value Value of the cookie.
253 * @param integer $expire Expiry of the cookie.
254 * @param bool $secure Whether the cookie should be served only over https.
255 * @param bool $httponly Whether the cookie is only accessible over HTTP, not scripting languages like JavaScript. @since 1.4.9.
256 */
257 function evf_setcookie( $name, $value, $expire = 0, $secure = false, $httponly = false ) {
258 if ( ! headers_sent() ) {
259 setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, apply_filters( 'everest_forms_cookie_httponly', $httponly, $name, $value, $expire, $secure ) );
260 } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
261 headers_sent( $file, $line );
262 trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); // @codingStandardsIgnoreLine
263 }
264 }
265
266 /**
267 * Get a log file path.
268 *
269 * @since 1.0.0
270 *
271 * @param string $handle name.
272 * @return string the log file path.
273 */
274 function evf_get_log_file_path( $handle ) {
275 return EVF_Log_Handler_File::get_log_file_path( $handle );
276 }
277
278 /**
279 * Get a csv file name.
280 *
281 * File names consist of the handle, followed by the date, followed by a hash, .csv.
282 *
283 * @since 1.3.0
284 *
285 * @param string $handle Name.
286 * @return bool|string The csv file name or false if cannot be determined.
287 */
288 function evf_get_csv_file_name( $handle ) {
289 if ( function_exists( 'wp_hash' ) ) {
290 $date_suffix = date( 'Y-m-d', time() ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
291 $hash_suffix = wp_hash( $handle );
292 return sanitize_file_name( implode( '-', array( 'evf-entry-export', $handle, $date_suffix, $hash_suffix ) ) . '.csv' );
293 } else {
294 evf_doing_it_wrong( __METHOD__, __( 'This method should not be called before plugins_loaded.', 'everest-forms' ), '1.3.0' );
295 return false;
296 }
297 }
298
299 /**
300 * Recursively get page children.
301 *
302 * @param int $page_id Page ID.
303 * @return int[]
304 */
305 function evf_get_page_children( $page_id ) {
306 $page_ids = get_posts(
307 array(
308 'post_parent' => $page_id,
309 'post_type' => 'page',
310 'numberposts' => - 1,
311 'post_status' => 'any',
312 'fields' => 'ids',
313 )
314 );
315
316 if ( ! empty( $page_ids ) ) {
317 foreach ( $page_ids as $page_id ) {
318 $page_ids = array_merge( $page_ids, evf_get_page_children( $page_id ) );
319 }
320 }
321
322 return $page_ids;
323 }
324
325 /**
326 * Get user agent string.
327 *
328 * @since 1.0.0
329 * @return string
330 */
331 function evf_get_user_agent() {
332 return isset( $_SERVER['HTTP_USER_AGENT'] ) ? evf_clean( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; // @codingStandardsIgnoreLine
333 }
334
335 // This function can be removed when WP 3.9.2 or greater is required.
336 if ( ! function_exists( 'hash_equals' ) ) :
337 /**
338 * Compare two strings in constant time.
339 *
340 * This function was added in PHP 5.6.
341 * It can leak the length of a string.
342 *
343 * @since 1.0.0
344 *
345 * @param string $a Expected string.
346 * @param string $b Actual string.
347 * @return bool Whether strings are equal.
348 */
349 function hash_equals( $a, $b ) {
350 $a_length = strlen( $a );
351 if ( strlen( $b ) !== $a_length ) {
352 return false;
353 }
354 $result = 0;
355
356 // Do not attempt to "optimize" this.
357 for ( $i = 0; $i < $a_length; $i ++ ) {
358 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
359 }
360
361 return 0 === $result;
362 }
363 endif;
364
365 /**
366 * Generate a rand hash.
367 *
368 * @since 1.0.0
369 * @return string
370 */
371 function evf_rand_hash() {
372 if ( ! function_exists( 'openssl_random_pseudo_bytes' ) ) {
373 return sha1( wp_rand() );
374 }
375
376 return bin2hex( openssl_random_pseudo_bytes( 20 ) ); // @codingStandardsIgnoreLine
377 }
378
379 /**
380 * Find all possible combinations of values from the input array and return in a logical order.
381 *
382 * @since 1.0.0
383 * @param array $input Input.
384 * @return array
385 */
386 function evf_array_cartesian( $input ) {
387 $input = array_filter( $input );
388 $results = array();
389 $indexes = array();
390 $index = 0;
391
392 // Generate indexes from keys and values so we have a logical sort order.
393 foreach ( $input as $key => $values ) {
394 foreach ( $values as $value ) {
395 $indexes[ $key ][ $value ] = $index++;
396 }
397 }
398
399 // Loop over the 2D array of indexes and generate all combinations.
400 foreach ( $indexes as $key => $values ) {
401 // When result is empty, fill with the values of the first looped array.
402 if ( empty( $results ) ) {
403 foreach ( $values as $value ) {
404 $results[] = array( $key => $value );
405 }
406 } else {
407 // Second and subsequent input sub-array merging.
408 foreach ( $results as $result_key => $result ) {
409 foreach ( $values as $value ) {
410 // If the key is not set, we can set it.
411 if ( ! isset( $results[ $result_key ][ $key ] ) ) {
412 $results[ $result_key ][ $key ] = $value;
413 } else {
414 // If the key is set, we can add a new combination to the results array.
415 $new_combination = $results[ $result_key ];
416 $new_combination[ $key ] = $value;
417 $results[] = $new_combination;
418 }
419 }
420 }
421 }
422 }
423
424 // Sort the indexes.
425 arsort( $results );
426
427 // Convert indexes back to values.
428 foreach ( $results as $result_key => $result ) {
429 $converted_values = array();
430
431 // Sort the values.
432 arsort( $results[ $result_key ] );
433
434 // Convert the values.
435 foreach ( $results[ $result_key ] as $key => $value ) {
436 $converted_values[ $key ] = array_search( $value, $indexes[ $key ], true );
437 }
438
439 $results[ $result_key ] = $converted_values;
440 }
441
442 return $results;
443 }
444
445 /**
446 * Run a MySQL transaction query, if supported.
447 *
448 * @since 1.0.0
449 * @param string $type Types: start (default), commit, rollback.
450 * @param bool $force use of transactions.
451 */
452 function evf_transaction_query( $type = 'start', $force = false ) {
453 global $wpdb;
454
455 $wpdb->hide_errors();
456
457 evf_maybe_define_constant( 'EVF_USE_TRANSACTIONS', true );
458
459 if ( EVF_USE_TRANSACTIONS || $force ) {
460 switch ( $type ) {
461 case 'commit':
462 $wpdb->query( 'COMMIT' );
463 break;
464 case 'rollback':
465 $wpdb->query( 'ROLLBACK' );
466 break;
467 default:
468 $wpdb->query( 'START TRANSACTION' );
469 break;
470 }
471 }
472 }
473
474 /**
475 * Outputs a "back" link so admin screens can easily jump back a page.
476 *
477 * @param string $label Title of the page to return to.
478 * @param string $url URL of the page to return to.
479 */
480 function evf_back_link( $label, $url ) {
481 echo '<small class="evf-admin-breadcrumb"><a href="' . esc_url( $url ) . '" aria-label="' . esc_attr( $label ) . '">&#x2934;</a></small>';
482 }
483
484 /**
485 * Display a EverestForms help tip.
486 *
487 * @since 1.0.0
488 *
489 * @param string $tip Help tip text.
490 * @param bool $allow_html Allow sanitized HTML if true or escape.
491 * @return string
492 */
493 function evf_help_tip( $tip, $allow_html = false ) {
494 if ( $allow_html ) {
495 $tip = evf_sanitize_tooltip( $tip );
496 } else {
497 $tip = esc_attr( $tip );
498 }
499
500 return '<span class="everest-forms-help-tip" data-tip="' . $tip . '"></span>';
501 }
502
503 /**
504 * Wrapper for set_time_limit to see if it is enabled.
505 *
506 * @since 1.0.0
507 * @param int $limit Time limit.
508 */
509 function evf_set_time_limit( $limit = 0 ) {
510 if ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved
511 @set_time_limit( $limit ); // @codingStandardsIgnoreLine
512 }
513 }
514
515 /**
516 * Wrapper for nocache_headers which also disables page caching.
517 *
518 * @since 1.2.0
519 */
520 function evf_nocache_headers() {
521 EVF_Cache_Helper::set_nocache_constants();
522 nocache_headers();
523 }
524
525 /**
526 * Get a shared logger instance.
527 *
528 * Use the everest_forms_logging_class filter to change the logging class. You may provide one of the following:
529 * - a class name which will be instantiated as `new $class` with no arguments
530 * - an instance which will be used directly as the logger
531 * In either case, the class or instance *must* implement EVF_Logger_Interface.
532 *
533 * @see EVF_Logger_Interface
534 *
535 * @return EVF_Logger
536 */
537 function evf_get_logger() {
538 static $logger = null;
539
540 $class = apply_filters( 'everest_forms_logging_class', 'EVF_Logger' );
541
542 if ( null !== $logger && is_string( $class ) && is_a( $logger, $class ) ) {
543 return $logger;
544 }
545
546 $implements = class_implements( $class );
547
548 if ( is_array( $implements ) && in_array( 'EVF_Logger_Interface', $implements, true ) ) {
549 $logger = is_object( $class ) ? $class : new $class();
550 } else {
551 evf_doing_it_wrong(
552 __FUNCTION__,
553 sprintf(
554 /* translators: 1: class name 2: everest_forms_logging_class 3: EVF_Logger_Interface */
555 __( 'The class %1$s provided by %2$s filter must implement %3$s.', 'everest-forms' ),
556 '<code>' . esc_html( is_object( $class ) ? get_class( $class ) : $class ) . '</code>',
557 '<code>everest_forms_logging_class</code>',
558 '<code>EVF_Logger_Interface</code>'
559 ),
560 '1.2'
561 );
562 $logger = is_a( $logger, 'EVF_Logger' ) ? $logger : new EVF_Logger();
563 }
564
565 return $logger;
566 }
567
568 /**
569 * Prints human-readable information about a variable.
570 *
571 * Some server environments blacklist some debugging functions. This function provides a safe way to
572 * turn an expression into a printable, readable form without calling blacklisted functions.
573 *
574 * @since 1.0.0
575 *
576 * @param mixed $expression The expression to be printed.
577 * @param bool $return Optional. Default false. Set to true to return the human-readable string.
578 *
579 * @return string|bool False if expression could not be printed. True if the expression was printed.
580 * If $return is true, a string representation will be returned.
581 */
582 function evf_print_r( $expression, $return = false ) {
583 $alternatives = array(
584 array(
585 'func' => 'print_r',
586 'args' => array( $expression, true ),
587 ),
588 array(
589 'func' => 'var_export',
590 'args' => array( $expression, true ),
591 ),
592 array(
593 'func' => 'json_encode',
594 'args' => array( $expression ),
595 ),
596 array(
597 'func' => 'serialize',
598 'args' => array( $expression ),
599 ),
600 );
601
602 $alternatives = apply_filters( 'everest_forms_print_r_alternatives', $alternatives, $expression );
603
604 foreach ( $alternatives as $alternative ) {
605 if ( function_exists( $alternative['func'] ) ) {
606 $res = call_user_func_array( $alternative['func'], $alternative['args'] );
607 if ( $return ) {
608 return $res;
609 }
610
611 echo $res; // WPCS: XSS ok.
612 return true;
613 }
614 }
615
616 return false;
617 }
618
619 /**
620 * Registers the default log handler.
621 *
622 * @since 1.0.0
623 * @param array $handlers Handlers.
624 * @return array
625 */
626 function evf_register_default_log_handler( $handlers ) {
627 if ( defined( 'EVF_LOG_HANDLER' ) && class_exists( EVF_LOG_HANDLER ) ) {
628 $handler_class = EVF_LOG_HANDLER;
629 $default_handler = new $handler_class();
630 } else {
631 $default_handler = new EVF_Log_Handler_File();
632 }
633
634 array_push( $handlers, $default_handler );
635
636 return $handlers;
637 }
638
639 add_filter( 'everest_forms_register_log_handlers', 'evf_register_default_log_handler' );
640
641 /**
642 * Based on wp_list_pluck, this calls a method instead of returning a property.
643 *
644 * @since 1.0.0
645 * @param array $list List of objects or arrays.
646 * @param int|string $callback_or_field Callback method from the object to place instead of the entire object.
647 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
648 * Default null.
649 * @return array Array of values.
650 */
651 function evf_list_pluck( $list, $callback_or_field, $index_key = null ) {
652 // Use wp_list_pluck if this isn't a callback.
653 $first_el = current( $list );
654 if ( ! is_object( $first_el ) || ! is_callable( array( $first_el, $callback_or_field ) ) ) {
655 return wp_list_pluck( $list, $callback_or_field, $index_key );
656 }
657 if ( ! $index_key ) {
658 /*
659 * This is simple. Could at some point wrap array_column()
660 * if we knew we had an array of arrays.
661 */
662 foreach ( $list as $key => $value ) {
663 $list[ $key ] = $value->{$callback_or_field}();
664 }
665 return $list;
666 }
667
668 /*
669 * When index_key is not set for a particular item, push the value
670 * to the end of the stack. This is how array_column() behaves.
671 */
672 $newlist = array();
673 foreach ( $list as $value ) {
674 // Get index.
675 if ( is_callable( array( $value, $index_key ) ) ) {
676 $newlist[ $value->{$index_key}() ] = $value->{$callback_or_field}();
677 } elseif ( isset( $value->$index_key ) ) {
678 $newlist[ $value->$index_key ] = $value->{$callback_or_field}();
679 } else {
680 $newlist[] = $value->{$callback_or_field}();
681 }
682 }
683 return $newlist;
684 }
685
686 /**
687 * Switch EverestForms to site language.
688 *
689 * @since 1.0.0
690 */
691 function evf_switch_to_site_locale() {
692 if ( function_exists( 'switch_to_locale' ) ) {
693 switch_to_locale( get_locale() );
694
695 // Filter on plugin_locale so load_plugin_textdomain loads the correct locale.
696 add_filter( 'plugin_locale', 'get_locale' );
697
698 // Init EVF locale.
699 evf()->load_plugin_textdomain();
700 }
701 }
702
703 /**
704 * Switch EverestForms language to original.
705 *
706 * @since 1.0.0
707 */
708 function evf_restore_locale() {
709 if ( function_exists( 'restore_previous_locale' ) ) {
710 restore_previous_locale();
711
712 // Remove filter.
713 remove_filter( 'plugin_locale', 'get_locale' );
714
715 // Init EVF locale.
716 evf()->load_plugin_textdomain();
717 }
718 }
719
720 /**
721 * Get an item of post data if set, otherwise return a default value.
722 *
723 * @since 1.0.0
724 * @param string $key Key.
725 * @param string $default Default.
726 * @return mixed value sanitized by evf_clean
727 */
728 function evf_get_post_data_by_key( $key, $default = '' ) {
729 return evf_clean( evf_get_var( $_POST[ $key ], $default ) ); // @codingStandardsIgnoreLine
730 }
731
732 /**
733 * Get data if set, otherwise return a default value or null. Prevents notices when data is not set.
734 *
735 * @since 1.0.0
736 * @param mixed $var Variable.
737 * @param string $default Default value.
738 * @return mixed
739 */
740 function evf_get_var( &$var, $default = null ) {
741 return isset( $var ) ? $var : $default;
742 }
743
744 /**
745 * Read in EverestForms headers when reading plugin headers.
746 *
747 * @since 1.2.0
748 * @param array $headers Headers.
749 * @return array
750 */
751 function evf_enable_evf_plugin_headers( $headers ) {
752 if ( ! class_exists( 'EVF_Plugin_Updates' ) ) {
753 include_once dirname( __FILE__ ) . '/admin/plugin-updates/class-evf-plugin-updates.php';
754 }
755
756 // EVF requires at least - allows developers to define which version of Everest Forms the plugin requires to run.
757 $headers[] = EVF_Plugin_Updates::VERSION_REQUIRED_HEADER;
758
759 // EVF tested up to - allows developers to define which version of Everest Forms they have tested up to.
760 $headers[] = EVF_Plugin_Updates::VERSION_TESTED_HEADER;
761
762 return $headers;
763 }
764 add_filter( 'extra_theme_headers', 'evf_enable_evf_plugin_headers' );
765 add_filter( 'extra_plugin_headers', 'evf_enable_evf_plugin_headers' );
766
767 /**
768 * Delete expired transients.
769 *
770 * Deletes all expired transients. The multi-table delete syntax is used.
771 * to delete the transient record from table a, and the corresponding.
772 * transient_timeout record from table b.
773 *
774 * Based on code inside core's upgrade_network() function.
775 *
776 * @since 1.0.0
777 * @return int Number of transients that were cleared.
778 */
779 function evf_delete_expired_transients() {
780 global $wpdb;
781
782 $sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b
783 WHERE a.option_name LIKE %s
784 AND a.option_name NOT LIKE %s
785 AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
786 AND b.option_value < %d";
787 $rows = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', time() ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
788
789 $sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b
790 WHERE a.option_name LIKE %s
791 AND a.option_name NOT LIKE %s
792 AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
793 AND b.option_value < %d";
794 $rows2 = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', time() ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
795
796 return absint( $rows + $rows2 );
797 }
798 add_action( 'everest_forms_installed', 'evf_delete_expired_transients' );
799
800 /**
801 * Make a URL relative, if possible.
802 *
803 * @since 1.0.0
804 * @param string $url URL to make relative.
805 * @return string
806 */
807 function evf_get_relative_url( $url ) {
808 return evf_is_external_resource( $url ) ? $url : str_replace( array( 'http://', 'https://' ), '//', $url );
809 }
810
811 /**
812 * See if a resource is remote.
813 *
814 * @since 1.0.0
815 * @param string $url URL to check.
816 * @return bool
817 */
818 function evf_is_external_resource( $url ) {
819 $wp_base = str_replace( array( 'http://', 'https://' ), '//', get_home_url( null, '/', 'http' ) );
820 return strstr( $url, '://' ) && strstr( $wp_base, $url );
821 }
822
823 /**
824 * See if theme/s is activate or not.
825 *
826 * @since 1.0.0
827 * @param string|array $theme Theme name or array of theme names to check.
828 * @return boolean
829 */
830 function evf_is_active_theme( $theme ) {
831 return is_array( $theme ) ? in_array( get_template(), $theme, true ) : get_template() === $theme;
832 }
833
834 /**
835 * Cleans up session data - cron callback.
836 *
837 * @since 1.0.0
838 */
839 function evf_cleanup_session_data() {
840 $session_class = apply_filters( 'everest_forms_session_handler', 'EVF_Session_Handler' );
841 $session = new $session_class();
842
843 if ( is_callable( array( $session, 'cleanup_sessions' ) ) ) {
844 $session->cleanup_sessions();
845 }
846 }
847 add_action( 'everest_forms_cleanup_sessions', 'evf_cleanup_session_data' );
848
849 /**
850 * Return the html selected attribute if stringified $value is found in array of stringified $options
851 * or if stringified $value is the same as scalar stringified $options.
852 *
853 * @param string|int $value Value to find within options.
854 * @param string|int|array $options Options to go through when looking for value.
855 * @return string
856 */
857 function evf_selected( $value, $options ) {
858 if ( is_array( $options ) ) {
859 $options = array_map( 'strval', $options );
860 return selected( in_array( (string) $value, $options, true ), true, false );
861 }
862
863 return selected( $value, $options, false );
864 }
865
866 /**
867 * Retrieve actual fields from a form.
868 *
869 * Non-posting elements such as section divider, page break, and HTML are
870 * automatically excluded. Optionally a white list can be provided.
871 *
872 * @since 1.0.0
873 *
874 * @param mixed $form Form data.
875 * @param array $whitelist Whitelist args.
876 *
877 * @return mixed boolean or array
878 */
879 function evf_get_form_fields( $form = false, $whitelist = array() ) {
880 // Accept form (post) object or form ID.
881 if ( is_object( $form ) ) {
882 $form = json_decode( $form->post_content );
883 } elseif ( is_numeric( $form ) ) {
884 $form = evf()->form->get(
885 $form,
886 array(
887 'content_only' => true,
888 )
889 );
890 }
891
892 if ( ! is_array( $form ) || empty( $form['form_fields'] ) ) {
893 return false;
894 }
895
896 // White list of field types to allow.
897 $allowed_form_fields = array(
898 'first-name',
899 'last-name',
900 'text',
901 'textarea',
902 'select',
903 'radio',
904 'checkbox',
905 'email',
906 'address',
907 'country',
908 'url',
909 'name',
910 'hidden',
911 'date',
912 'phone',
913 'number',
914 'file-upload',
915 'image-upload',
916 'payment-single',
917 'payment-multiple',
918 'payment-checkbox',
919 'payment-total',
920 );
921 $allowed_form_fields = apply_filters( 'everest_forms_allowed_form_fields', $allowed_form_fields );
922
923 $whitelist = ! empty( $whitelist ) ? $whitelist : $allowed_form_fields;
924
925 $form_fields = $form['form_fields'];
926
927 foreach ( $form_fields as $id => $form_field ) {
928 if ( ! in_array( $form_field['type'], $whitelist, true ) ) {
929 unset( $form_fields[ $id ] );
930 }
931 }
932
933 return $form_fields;
934 }
935
936 /**
937 * Sanitize a string, that can be a multiline.
938 * If WP core `sanitize_textarea_field()` exists (after 4.7.0) - use it.
939 * Otherwise - split onto separate lines, sanitize each one, merge again.
940 *
941 * @since 1.4.1
942 *
943 * @param string $string Raw string to sanitize.
944 *
945 * @return string If empty var is passed, or not a string - return unmodified. Otherwise - sanitize.
946 */
947 function evf_sanitize_textarea_field( $string ) {
948 if ( empty( $string ) || ! is_string( $string ) ) {
949 return $string;
950 }
951
952 if ( function_exists( 'sanitize_textarea_field' ) ) {
953 $string = sanitize_textarea_field( $string );
954 } else {
955 $string = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $string ) ) );
956 }
957
958 return $string;
959 }
960
961 /**
962 * Formats, sanitizes, and returns/echos HTML element ID, classes, attributes,
963 * and data attributes.
964 *
965 * @param string $id Element ID.
966 * @param array $class Class args.
967 * @param array $datas Data args.
968 * @param array $atts Attributes.
969 * @param bool $echo True to echo else return.
970 *
971 * @return string
972 */
973 function evf_html_attributes( $id = '', $class = array(), $datas = array(), $atts = array(), $echo = false ) {
974 $id = trim( $id );
975 $parts = array();
976
977 if ( ! empty( $id ) ) {
978 $id = sanitize_html_class( $id );
979 if ( ! empty( $id ) ) {
980 $parts[] = 'id="' . $id . '"';
981 }
982 }
983
984 if ( ! empty( $class ) ) {
985 $class = evf_sanitize_classes( $class, true );
986 if ( ! empty( $class ) ) {
987 $parts[] = 'class="' . $class . '"';
988 }
989 }
990
991 if ( ! empty( $datas ) ) {
992 foreach ( $datas as $data => $val ) {
993 $parts[] = 'data-' . sanitize_html_class( $data ) . '="' . esc_attr( $val ) . '"';
994 }
995 }
996
997 if ( ! empty( $atts ) ) {
998 foreach ( $atts as $att => $val ) {
999 if ( '0' === $val || ! empty( $val ) ) {
1000 $parts[] = sanitize_html_class( $att ) . '="' . esc_attr( $val ) . '"';
1001 }
1002 }
1003 }
1004
1005 $output = implode( ' ', $parts );
1006
1007 if ( $echo ) {
1008 echo trim( $output ); // @codingStandardsIgnoreLine
1009 } else {
1010 return trim( $output );
1011 }
1012 }
1013
1014 /**
1015 * Sanitize string of CSS classes.
1016 *
1017 * @param array|string $classes Class names.
1018 * @param bool $convert True will convert strings to array and vice versa.
1019 *
1020 * @return string|array
1021 */
1022 function evf_sanitize_classes( $classes, $convert = false ) {
1023 $css = array();
1024 $array = is_array( $classes );
1025
1026 if ( ! empty( $classes ) ) {
1027 if ( ! $array ) {
1028 $classes = explode( ' ', trim( $classes ) );
1029 }
1030 foreach ( $classes as $class ) {
1031 $css[] = sanitize_html_class( $class );
1032 }
1033 }
1034
1035 if ( $array ) {
1036 return $convert ? implode( ' ', $css ) : $css;
1037 } else {
1038 return $convert ? $css : implode( ' ', $css );
1039 }
1040 }
1041
1042 /**
1043 * Performs json_decode and unslash.
1044 *
1045 * @since 1.0.0
1046 *
1047 * @param string $data Data to decode.
1048 *
1049 * @return array|bool
1050 */
1051 function evf_decode( $data ) {
1052 if ( ! $data || empty( $data ) ) {
1053 return false;
1054 }
1055
1056 return json_decode( $data, true );
1057 }
1058
1059 /**
1060 * Performs json_encode and wp_slash.
1061 *
1062 * @since 1.0.0
1063 *
1064 * @param mixed $data Data to encode.
1065 *
1066 * @return string
1067 */
1068 function evf_encode( $data = false ) {
1069 if ( empty( $data ) ) {
1070 return false;
1071 }
1072
1073 return wp_slash( wp_json_encode( $data ) );
1074 }
1075
1076 /**
1077 * Crypto rand secure.
1078 *
1079 * @param int $min Min value.
1080 * @param int $max Max value.
1081 *
1082 * @return mixed
1083 */
1084 function evf_crypto_rand_secure( $min, $max ) {
1085 $range = $max - $min;
1086 if ( $range < 1 ) {
1087 return $min;
1088 } // not so random...
1089 $log = ceil( log( $range, 2 ) );
1090 $bytes = (int) ( $log / 8 ) + 1; // Length in bytes.
1091 $bits = (int) $log + 1; // Length in bits.
1092 $filter = (int) ( 1 << $bits ) - 1; // Set all lower bits to 1.
1093 do {
1094 $rnd = hexdec( bin2hex( openssl_random_pseudo_bytes( $bytes ) ) );
1095 $rnd = $rnd & $filter; // Discard irrelevant bits.
1096 } while ( $rnd > $range );
1097
1098 return $min + $rnd;
1099 }
1100
1101 /**
1102 * Generate random string.
1103 *
1104 * @param int $length Length of string.
1105 *
1106 * @return string
1107 */
1108 function evf_get_random_string( $length = 10 ) {
1109 $string = '';
1110 $code_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
1111 $code_alphabet .= 'abcdefghijklmnopqrstuvwxyz';
1112 $code_alphabet .= '0123456789';
1113 $max = strlen( $code_alphabet );
1114 for ( $i = 0; $i < $length; $i ++ ) {
1115 $string .= $code_alphabet[ evf_crypto_rand_secure( 0, $max - 1 ) ];
1116 }
1117
1118 return $string;
1119 }
1120
1121 /**
1122 * Get all forms.
1123 *
1124 * @param bool $skip_disabled_entries True to skip disabled entries.
1125 * @return array of form data.
1126 */
1127 function evf_get_all_forms( $skip_disabled_entries = false ) {
1128 $forms = array();
1129 $form_ids = wp_parse_id_list(
1130 get_posts(
1131 array(
1132 'post_type' => 'everest_form',
1133 'numberposts' => -1, // @codingStandardsIgnoreLine
1134 'status' => 'publish',
1135 'fields' => 'ids',
1136 )
1137 )
1138 );
1139
1140 if ( ! empty( $form_ids ) ) {
1141 foreach ( $form_ids as $form_id ) {
1142 $form = evf()->form->get( $form_id );
1143 $entries = evf_get_entries_ids( $form_id );
1144 $form_data = ! empty( $form->post_content ) ? evf_decode( $form->post_content ) : '';
1145
1146 if ( ( $skip_disabled_entries && count( $entries ) < 1 ) && ( isset( $form_data['settings']['disabled_entries'] ) && '1' === $form_data['settings']['disabled_entries'] ) ) {
1147 continue;
1148 }
1149
1150 $forms[ $form_id ] = $form->post_title;
1151 }
1152 }
1153
1154 return $forms;
1155 }
1156
1157 /**
1158 * Get random meta-key for field option.
1159 *
1160 * @param array $field Field data array.
1161 * @return string
1162 */
1163 function evf_get_meta_key_field_option( $field ) {
1164 $random_number = rand( pow( 10, 3 ), pow( 10, 4 ) - 1 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand
1165 return strtolower( str_replace( array( ' ', '/_' ), array( '_', '' ), $field['label'] ) ) . '_' . $random_number;
1166 }
1167
1168 /**
1169 * Get current user IP Address.
1170 *
1171 * @return string
1172 */
1173 function evf_get_ip_address() {
1174 if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) { // WPCS: input var ok, CSRF ok.
1175 return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) ); // WPCS: input var ok, CSRF ok.
1176 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { // WPCS: input var ok, CSRF ok.
1177 // Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2
1178 // Make sure we always only send through the first IP in the list which should always be the client IP.
1179 return (string) rest_is_ip_address( trim( current( preg_split( '/[,:]/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) ); // WPCS: input var ok, CSRF ok.
1180 } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) { // @codingStandardsIgnoreLine
1181 return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); // @codingStandardsIgnoreLine
1182 }
1183 return '';
1184 }
1185
1186 /**
1187 * Get User Agent browser and OS type
1188 *
1189 * @since 1.1.0
1190 * @return array
1191 */
1192 function evf_get_browser() {
1193 $u_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
1194 $bname = 'Unknown';
1195 $platform = 'Unknown';
1196 $version = '';
1197
1198 // First get the platform.
1199 if ( preg_match( '/linux/i', $u_agent ) ) {
1200 $platform = 'Linux';
1201 } elseif ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
1202 $platform = 'MAC OS';
1203 } elseif ( preg_match( '/windows|win32/i', $u_agent ) ) {
1204 $platform = 'Windows';
1205 }
1206
1207 // Next get the name of the useragent yes seperately and for good reason.
1208 if ( preg_match( '/MSIE/i', $u_agent ) && ! preg_match( '/Opera/i', $u_agent ) ) {
1209 $bname = 'Internet Explorer';
1210 $ub = 'MSIE';
1211 } elseif ( preg_match( '/Trident/i', $u_agent ) ) {
1212 // this condition is for IE11.
1213 $bname = 'Internet Explorer';
1214 $ub = 'rv';
1215 } elseif ( preg_match( '/Firefox/i', $u_agent ) ) {
1216 $bname = 'Mozilla Firefox';
1217 $ub = 'Firefox';
1218 } elseif ( preg_match( '/Chrome/i', $u_agent ) ) {
1219 $bname = 'Google Chrome';
1220 $ub = 'Chrome';
1221 } elseif ( preg_match( '/Safari/i', $u_agent ) ) {
1222 $bname = 'Apple Safari';
1223 $ub = 'Safari';
1224 } elseif ( preg_match( '/Opera/i', $u_agent ) ) {
1225 $bname = 'Opera';
1226 $ub = 'Opera';
1227 } elseif ( preg_match( '/Netscape/i', $u_agent ) ) {
1228 $bname = 'Netscape';
1229 $ub = 'Netscape';
1230 }
1231
1232 // Finally get the correct version number.
1233 // Added "|:".
1234 $known = array( 'Version', $ub, 'other' );
1235 $pattern = '#(?<browser>' . join( '|', $known ) . ')[/|: ]+(?<version>[0-9.|a-zA-Z.]*)#';
1236 if ( ! preg_match_all( $pattern, $u_agent, $matches ) ) { // @codingStandardsIgnoreLine
1237 // We have no matching number just continue.
1238 }
1239
1240 // See how many we have.
1241 $i = count( $matches['browser'] );
1242
1243 if ( 1 !== $i ) {
1244 // we will have two since we are not using 'other' argument yet.
1245 // see if version is before or after the name.
1246 if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
1247 $version = $matches['version'][0];
1248 } else {
1249 $version = $matches['version'][1];
1250 }
1251 } else {
1252 $version = $matches['version'][0];
1253 }
1254
1255 // Check if we have a number.
1256 if ( null === $version || '' === $version ) {
1257 $version = '';
1258 }
1259
1260 return array(
1261 'userAgent' => $u_agent,
1262 'name' => $bname,
1263 'version' => $version,
1264 'platform' => $platform,
1265 'pattern' => $pattern,
1266 );
1267 }
1268
1269 /**
1270 * Get the certain date of a specified day in a specified format.
1271 *
1272 * @since 1.1.0
1273 *
1274 * @param string $period Supported values: start, end.
1275 * @param string $timestamp Default is the current timestamp, if left empty.
1276 * @param string $format Default is a MySQL format.
1277 *
1278 * @return string
1279 */
1280 function evf_get_day_period_date( $period, $timestamp = '', $format = 'Y-m-d H:i:s' ) {
1281 $date = '';
1282
1283 if ( empty( $timestamp ) ) {
1284 $timestamp = time();
1285 }
1286
1287 switch ( $period ) {
1288 case 'start_of_day':
1289 $date = date( $format, strtotime( 'today', $timestamp ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
1290 break;
1291
1292 case 'end_of_day':
1293 $date = date( $format, strtotime( 'tomorrow', $timestamp ) - 1 ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
1294 break;
1295
1296 }
1297
1298 return $date;
1299 }
1300
1301 /**
1302 * Get field label by meta key
1303 *
1304 * @param int $form_id Form ID.
1305 * @param string $meta_key Field's meta key.
1306 *
1307 * @return string|false True if field label exists in form.
1308 */
1309 function evf_get_form_data_by_meta_key( $form_id, $meta_key ) {
1310 $get_post = get_post( $form_id );
1311 $post_content = json_decode( $get_post->post_content, true );
1312 $form_fields = isset( $post_content['form_fields'] ) ? $post_content['form_fields'] : array();
1313
1314 if ( ! empty( $form_fields ) ) {
1315 foreach ( $form_fields as $field ) {
1316 if ( isset( $field['meta-key'] ) && $meta_key === $field['meta-key'] ) {
1317 return $field['label'];
1318 }
1319 }
1320 }
1321
1322 return false;
1323 }
1324
1325 /**
1326 * Get field type by meta key
1327 *
1328 * @param int $form_id Form ID.
1329 * @param string $meta_key Field's meta key.
1330 *
1331 * @return string|false True if field type exists in form.
1332 */
1333 function evf_get_field_type_by_meta_key( $form_id, $meta_key ) {
1334 $get_post = get_post( $form_id );
1335 $post_content = json_decode( $get_post->post_content, true );
1336 $form_fields = isset( $post_content['form_fields'] ) ? $post_content['form_fields'] : array();
1337
1338 if ( ! empty( $form_fields ) ) {
1339 foreach ( $form_fields as $field ) {
1340 if ( isset( $field['meta-key'] ) && $meta_key === $field['meta-key'] ) {
1341 return $field['type'];
1342 }
1343 }
1344 }
1345
1346 return false;
1347 }
1348
1349 /**
1350 * Get all the email fields of a Form.
1351 *
1352 * @param int $form_id Form ID.
1353 */
1354 function evf_get_all_email_fields_by_form_id( $form_id ) {
1355 $user_emails = array();
1356 $form_obj = evf()->form->get( $form_id );
1357 $form_data = ! empty( $form_obj->post_content ) ? evf_decode( $form_obj->post_content ) : '';
1358
1359 if ( ! empty( $form_data['form_fields'] ) ) {
1360 foreach ( $form_data['form_fields'] as $form_fields ) {
1361 if ( 'email' === $form_fields['type'] ) {
1362 $user_emails[ $form_fields['meta-key'] ] = $form_fields['label'];
1363 }
1364 }
1365 }
1366
1367 return $user_emails;
1368 }
1369
1370 /**
1371 * Get all the field's meta-key label pair.
1372 *
1373 * @param int $form_id Form ID.
1374 * @return array
1375 */
1376 function evf_get_all_form_fields_by_form_id( $form_id ) {
1377 $data = array();
1378 $form_obj = evf()->form->get( $form_id );
1379 $form_data = ! empty( $form_obj->post_content ) ? evf_decode( $form_obj->post_content ) : '';
1380
1381 if ( ! empty( $form_data['form_fields'] ) ) {
1382 foreach ( $form_data['form_fields'] as $form_fields ) {
1383 if ( isset( $form_fields['meta-key'], $form_fields['label'] ) ) {
1384 $data[ $form_fields['meta-key'] ] = $form_fields['label'];
1385 }
1386 }
1387 }
1388
1389 return $data;
1390 }
1391
1392 /**
1393 * Check if the string JSON.
1394 *
1395 * @param string $string String to check.
1396 * @return bool
1397 */
1398 function evf_isJson( $string ) {
1399 json_decode( $string );
1400 return ( json_last_error() == JSON_ERROR_NONE ); // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
1401 }
1402
1403 /**
1404 * Checks whether the content passed contains a specific short code.
1405 *
1406 * @since 1.1.4
1407 * @param string $tag Shortcode tag to check.
1408 * @return bool
1409 */
1410 function evf_post_content_has_shortcode( $tag = '' ) {
1411 global $post;
1412
1413 return is_singular() && is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $tag );
1414 }
1415
1416 /**
1417 * Convert a file size provided, such as "2M", to bytes.
1418 *
1419 * @since 1.2.0
1420 * @link http://stackoverflow.com/a/22500394
1421 *
1422 * @param string $size Size to convert to bytes.
1423 *
1424 * @return int
1425 */
1426 function evf_size_to_bytes( $size ) {
1427 if ( is_numeric( $size ) ) {
1428 return $size;
1429 }
1430
1431 $suffix = substr( $size, - 1 );
1432 $value = substr( $size, 0, - 1 );
1433
1434 // @codingStandardsIgnoreStart
1435 switch ( strtoupper( $suffix ) ) {
1436 case 'P':
1437 $value *= 1024;
1438 case 'T':
1439 $value *= 1024;
1440 case 'G':
1441 $value *= 1024;
1442 case 'M':
1443 $value *= 1024;
1444 case 'K':
1445 $value *= 1024;
1446 break;
1447 }
1448 // @codingStandardsIgnoreEnd
1449
1450 return $value;
1451 }
1452
1453 /**
1454 * Convert bytes to megabytes (or in some cases KB).
1455 *
1456 * @since 1.2.0
1457 *
1458 * @param int $bytes Bytes to convert to a readable format.
1459 *
1460 * @return string
1461 */
1462 function evf_size_to_megabytes( $bytes ) {
1463 if ( $bytes < 1048676 ) {
1464 return number_format( $bytes / 1024, 1 ) . ' KB';
1465 } else {
1466 return round( number_format( $bytes / 1048576, 1 ) ) . ' MB';
1467 }
1468 }
1469
1470 /**
1471 * Convert a file size provided, such as "2M", to bytes.
1472 *
1473 * @since 1.2.0
1474 * @link http://stackoverflow.com/a/22500394
1475 *
1476 * @param bool $bytes Whether to convert Bytes to a readable format.
1477 * @return mixed
1478 */
1479 function evf_max_upload( $bytes = false ) {
1480 $max = wp_max_upload_size();
1481
1482 if ( $bytes ) {
1483 return $max;
1484 } else {
1485 return evf_size_to_megabytes( $max );
1486 }
1487 }
1488
1489 /**
1490 * Get the required label text, with a filter.
1491 *
1492 * @since 1.2.0
1493 * @return string
1494 */
1495 function evf_get_required_label() {
1496 return apply_filters( 'everest_forms_required_label', esc_html__( 'This field is required.', 'everest-forms' ) );
1497 }
1498
1499 /**
1500 * Get a PRO license plan.
1501 *
1502 * @since 1.2.0
1503 * @return bool|string Plan on success, false on failure.
1504 */
1505 function evf_get_license_plan() {
1506 $license_key = get_option( 'everest-forms-pro_license_key' );
1507
1508 if ( $license_key && is_plugin_active( 'everest-forms-pro/everest-forms-pro.php' ) ) {
1509 $license_data = get_transient( 'evf_pro_license_plan' );
1510
1511 if ( false === $license_data ) {
1512 $license_data = json_decode(
1513 EVF_Updater_Key_API::check(
1514 array(
1515 'license' => $license_key,
1516 )
1517 )
1518 );
1519
1520 if ( ! empty( $license_data->item_plan ) ) {
1521 set_transient( 'evf_pro_license_plan', $license_data, WEEK_IN_SECONDS );
1522 }
1523 }
1524
1525 return isset( $license_data->item_plan ) ? $license_data->item_plan : false;
1526 }
1527
1528 return false;
1529 }
1530
1531 /**
1532 * Decode special characters, both alpha- (<) and numeric-based (').
1533 *
1534 * @since 1.2.0
1535 *
1536 * @param string $string Raw string to decode.
1537 *
1538 * @return string
1539 */
1540 function evf_decode_string( $string ) {
1541 if ( ! is_string( $string ) ) {
1542 return $string;
1543 }
1544
1545 return wp_kses_decode_entities( html_entity_decode( $string, ENT_QUOTES ) );
1546 }
1547 add_filter( 'everest_forms_email_message', 'evf_decode_string' );
1548
1549 /**
1550 * Get Countries.
1551 *
1552 * @since 1.2.0
1553 * @return array
1554 */
1555 function evf_get_countries() {
1556 $countries = array(
1557 'AF' => esc_html__( 'Afghanistan', 'everest-forms' ),
1558 'AX' => esc_html__( '�
1559 land Islands', 'everest-forms' ),
1560 'AL' => esc_html__( 'Albania', 'everest-forms' ),
1561 'DZ' => esc_html__( 'Algeria', 'everest-forms' ),
1562 'AS' => esc_html__( 'American Samoa', 'everest-forms' ),
1563 'AD' => esc_html__( 'Andorra', 'everest-forms' ),
1564 'AO' => esc_html__( 'Angola', 'everest-forms' ),
1565 'AI' => esc_html__( 'Anguilla', 'everest-forms' ),
1566 'AQ' => esc_html__( 'Antarctica', 'everest-forms' ),
1567 'AG' => esc_html__( 'Antigua and Barbuda', 'everest-forms' ),
1568 'AR' => esc_html__( 'Argentina', 'everest-forms' ),
1569 'AM' => esc_html__( 'Armenia', 'everest-forms' ),
1570 'AW' => esc_html__( 'Aruba', 'everest-forms' ),
1571 'AU' => esc_html__( 'Australia', 'everest-forms' ),
1572 'AT' => esc_html__( 'Austria', 'everest-forms' ),
1573 'AZ' => esc_html__( 'Azerbaijan', 'everest-forms' ),
1574 'BS' => esc_html__( 'Bahamas', 'everest-forms' ),
1575 'BH' => esc_html__( 'Bahrain', 'everest-forms' ),
1576 'BD' => esc_html__( 'Bangladesh', 'everest-forms' ),
1577 'BB' => esc_html__( 'Barbados', 'everest-forms' ),
1578 'BY' => esc_html__( 'Belarus', 'everest-forms' ),
1579 'BE' => esc_html__( 'Belgium', 'everest-forms' ),
1580 'BZ' => esc_html__( 'Belize', 'everest-forms' ),
1581 'BJ' => esc_html__( 'Benin', 'everest-forms' ),
1582 'BM' => esc_html__( 'Bermuda', 'everest-forms' ),
1583 'BT' => esc_html__( 'Bhutan', 'everest-forms' ),
1584 'BO' => esc_html__( 'Bolivia (Plurinational State of)', 'everest-forms' ),
1585 'BA' => esc_html__( 'Bosnia and Herzegovina', 'everest-forms' ),
1586 'BW' => esc_html__( 'Botswana', 'everest-forms' ),
1587 'BV' => esc_html__( 'Bouvet Island', 'everest-forms' ),
1588 'BR' => esc_html__( 'Brazil', 'everest-forms' ),
1589 'IO' => esc_html__( 'British Indian Ocean Territory', 'everest-forms' ),
1590 'BN' => esc_html__( 'Brunei Darussalam', 'everest-forms' ),
1591 'BG' => esc_html__( 'Bulgaria', 'everest-forms' ),
1592 'BF' => esc_html__( 'Burkina Faso', 'everest-forms' ),
1593 'BI' => esc_html__( 'Burundi', 'everest-forms' ),
1594 'CV' => esc_html__( 'Cabo Verde', 'everest-forms' ),
1595 'KH' => esc_html__( 'Cambodia', 'everest-forms' ),
1596 'CM' => esc_html__( 'Cameroon', 'everest-forms' ),
1597 'CA' => esc_html__( 'Canada', 'everest-forms' ),
1598 'KY' => esc_html__( 'Cayman Islands', 'everest-forms' ),
1599 'CF' => esc_html__( 'Central African Republic', 'everest-forms' ),
1600 'TD' => esc_html__( 'Chad', 'everest-forms' ),
1601 'CL' => esc_html__( 'Chile', 'everest-forms' ),
1602 'CN' => esc_html__( 'China', 'everest-forms' ),
1603 'CX' => esc_html__( 'Christmas Island', 'everest-forms' ),
1604 'CC' => esc_html__( 'Cocos (Keeling) Islands', 'everest-forms' ),
1605 'CO' => esc_html__( 'Colombia', 'everest-forms' ),
1606 'KM' => esc_html__( 'Comoros', 'everest-forms' ),
1607 'CG' => esc_html__( 'Congo', 'everest-forms' ),
1608 'CD' => esc_html__( 'Congo (Democratic Republic of the)', 'everest-forms' ),
1609 'CK' => esc_html__( 'Cook Islands', 'everest-forms' ),
1610 'CR' => esc_html__( 'Costa Rica', 'everest-forms' ),
1611 'CI' => esc_html__( 'Côte d\'Ivoire', 'everest-forms' ),
1612 'HR' => esc_html__( 'Croatia', 'everest-forms' ),
1613 'CU' => esc_html__( 'Cuba', 'everest-forms' ),
1614 'CW' => esc_html__( 'Curaçao', 'everest-forms' ),
1615 'CY' => esc_html__( 'Cyprus', 'everest-forms' ),
1616 'CZ' => esc_html__( 'Czech Republic', 'everest-forms' ),
1617 'DK' => esc_html__( 'Denmark', 'everest-forms' ),
1618 'DJ' => esc_html__( 'Djibouti', 'everest-forms' ),
1619 'DM' => esc_html__( 'Dominica', 'everest-forms' ),
1620 'DO' => esc_html__( 'Dominican Republic', 'everest-forms' ),
1621 'EC' => esc_html__( 'Ecuador', 'everest-forms' ),
1622 'EG' => esc_html__( 'Egypt', 'everest-forms' ),
1623 'SV' => esc_html__( 'El Salvador', 'everest-forms' ),
1624 'GQ' => esc_html__( 'Equatorial Guinea', 'everest-forms' ),
1625 'ER' => esc_html__( 'Eritrea', 'everest-forms' ),
1626 'EE' => esc_html__( 'Estonia', 'everest-forms' ),
1627 'ET' => esc_html__( 'Ethiopia', 'everest-forms' ),
1628 'FK' => esc_html__( 'Falkland Islands (Malvinas)', 'everest-forms' ),
1629 'FO' => esc_html__( 'Faroe Islands', 'everest-forms' ),
1630 'FJ' => esc_html__( 'Fiji', 'everest-forms' ),
1631 'FI' => esc_html__( 'Finland', 'everest-forms' ),
1632 'FR' => esc_html__( 'France', 'everest-forms' ),
1633 'GF' => esc_html__( 'French Guiana', 'everest-forms' ),
1634 'PF' => esc_html__( 'French Polynesia', 'everest-forms' ),
1635 'TF' => esc_html__( 'French Southern Territories', 'everest-forms' ),
1636 'GA' => esc_html__( 'Gabon', 'everest-forms' ),
1637 'GM' => esc_html__( 'Gambia', 'everest-forms' ),
1638 'GE' => esc_html_x( 'Georgia', 'Country', 'everest-forms' ),
1639 'DE' => esc_html__( 'Germany', 'everest-forms' ),
1640 'GH' => esc_html__( 'Ghana', 'everest-forms' ),
1641 'GI' => esc_html__( 'Gibraltar', 'everest-forms' ),
1642 'GR' => esc_html__( 'Greece', 'everest-forms' ),
1643 'GL' => esc_html__( 'Greenland', 'everest-forms' ),
1644 'GD' => esc_html__( 'Grenada', 'everest-forms' ),
1645 'GP' => esc_html__( 'Guadeloupe', 'everest-forms' ),
1646 'GU' => esc_html__( 'Guam', 'everest-forms' ),
1647 'GT' => esc_html__( 'Guatemala', 'everest-forms' ),
1648 'GG' => esc_html__( 'Guernsey', 'everest-forms' ),
1649 'GN' => esc_html__( 'Guinea', 'everest-forms' ),
1650 'GW' => esc_html__( 'Guinea-Bissau', 'everest-forms' ),
1651 'GY' => esc_html__( 'Guyana', 'everest-forms' ),
1652 'HT' => esc_html__( 'Haiti', 'everest-forms' ),
1653 'HM' => esc_html__( 'Heard Island and McDonald Islands', 'everest-forms' ),
1654 'HN' => esc_html__( 'Honduras', 'everest-forms' ),
1655 'HK' => esc_html__( 'Hong Kong', 'everest-forms' ),
1656 'HU' => esc_html__( 'Hungary', 'everest-forms' ),
1657 'IS' => esc_html__( 'Iceland', 'everest-forms' ),
1658 'IN' => esc_html__( 'India', 'everest-forms' ),
1659 'ID' => esc_html__( 'Indonesia', 'everest-forms' ),
1660 'IR' => esc_html__( 'Iran (Islamic Republic of)', 'everest-forms' ),
1661 'IQ' => esc_html__( 'Iraq', 'everest-forms' ),
1662 'IE' => esc_html__( 'Ireland (Republic of)', 'everest-forms' ),
1663 'IM' => esc_html__( 'Isle of Man', 'everest-forms' ),
1664 'IL' => esc_html__( 'Israel', 'everest-forms' ),
1665 'IT' => esc_html__( 'Italy', 'everest-forms' ),
1666 'JM' => esc_html__( 'Jamaica', 'everest-forms' ),
1667 'JP' => esc_html__( 'Japan', 'everest-forms' ),
1668 'JE' => esc_html__( 'Jersey', 'everest-forms' ),
1669 'JO' => esc_html__( 'Jordan', 'everest-forms' ),
1670 'KZ' => esc_html__( 'Kazakhstan', 'everest-forms' ),
1671 'KE' => esc_html__( 'Kenya', 'everest-forms' ),
1672 'KI' => esc_html__( 'Kiribati', 'everest-forms' ),
1673 'KP' => esc_html__( 'Korea (Democratic People\'s Republic of)', 'everest-forms' ),
1674 'KR' => esc_html__( 'Korea (Republic of)', 'everest-forms' ),
1675 'KW' => esc_html__( 'Kuwait', 'everest-forms' ),
1676 'KG' => esc_html__( 'Kyrgyzstan', 'everest-forms' ),
1677 'LA' => esc_html__( 'Lao People\'s Democratic Republic', 'everest-forms' ),
1678 'LV' => esc_html__( 'Latvia', 'everest-forms' ),
1679 'LB' => esc_html__( 'Lebanon', 'everest-forms' ),
1680 'LS' => esc_html__( 'Lesotho', 'everest-forms' ),
1681 'LR' => esc_html__( 'Liberia', 'everest-forms' ),
1682 'LY' => esc_html__( 'Libya', 'everest-forms' ),
1683 'LI' => esc_html__( 'Liechtenstein', 'everest-forms' ),
1684 'LT' => esc_html__( 'Lithuania', 'everest-forms' ),
1685 'LU' => esc_html__( 'Luxembourg', 'everest-forms' ),
1686 'MO' => esc_html__( 'Macao', 'everest-forms' ),
1687 'MK' => esc_html__( 'Macedonia (Republic of)', 'everest-forms' ),
1688 'MG' => esc_html__( 'Madagascar', 'everest-forms' ),
1689 'MW' => esc_html__( 'Malawi', 'everest-forms' ),
1690 'MY' => esc_html__( 'Malaysia', 'everest-forms' ),
1691 'MV' => esc_html__( 'Maldives', 'everest-forms' ),
1692 'ML' => esc_html__( 'Mali', 'everest-forms' ),
1693 'MT' => esc_html__( 'Malta', 'everest-forms' ),
1694 'MH' => esc_html__( 'Marshall Islands', 'everest-forms' ),
1695 'MQ' => esc_html__( 'Martinique', 'everest-forms' ),
1696 'MR' => esc_html__( 'Mauritania', 'everest-forms' ),
1697 'MU' => esc_html__( 'Mauritius', 'everest-forms' ),
1698 'YT' => esc_html__( 'Mayotte', 'everest-forms' ),
1699 'MX' => esc_html__( 'Mexico', 'everest-forms' ),
1700 'FM' => esc_html__( 'Micronesia (Federated States of)', 'everest-forms' ),
1701 'MD' => esc_html__( 'Moldova (Republic of)', 'everest-forms' ),
1702 'MC' => esc_html__( 'Monaco', 'everest-forms' ),
1703 'MN' => esc_html__( 'Mongolia', 'everest-forms' ),
1704 'ME' => esc_html__( 'Montenegro', 'everest-forms' ),
1705 'MS' => esc_html__( 'Montserrat', 'everest-forms' ),
1706 'MA' => esc_html__( 'Morocco', 'everest-forms' ),
1707 'MZ' => esc_html__( 'Mozambique', 'everest-forms' ),
1708 'MM' => esc_html__( 'Myanmar', 'everest-forms' ),
1709 'NA' => esc_html__( 'Namibia', 'everest-forms' ),
1710 'NR' => esc_html__( 'Nauru', 'everest-forms' ),
1711 'NP' => esc_html__( 'Nepal', 'everest-forms' ),
1712 'NL' => esc_html__( 'Netherlands', 'everest-forms' ),
1713 'NC' => esc_html__( 'New Caledonia', 'everest-forms' ),
1714 'NZ' => esc_html__( 'New Zealand', 'everest-forms' ),
1715 'NI' => esc_html__( 'Nicaragua', 'everest-forms' ),
1716 'NE' => esc_html__( 'Niger', 'everest-forms' ),
1717 'NG' => esc_html__( 'Nigeria', 'everest-forms' ),
1718 'NU' => esc_html__( 'Niue', 'everest-forms' ),
1719 'NF' => esc_html__( 'Norfolk Island', 'everest-forms' ),
1720 'MP' => esc_html__( 'Northern Mariana Islands', 'everest-forms' ),
1721 'NO' => esc_html__( 'Norway', 'everest-forms' ),
1722 'OM' => esc_html__( 'Oman', 'everest-forms' ),
1723 'PK' => esc_html__( 'Pakistan', 'everest-forms' ),
1724 'PW' => esc_html__( 'Palau', 'everest-forms' ),
1725 'PS' => esc_html__( 'Palestine (State of)', 'everest-forms' ),
1726 'PA' => esc_html__( 'Panama', 'everest-forms' ),
1727 'PG' => esc_html__( 'Papua New Guinea', 'everest-forms' ),
1728 'PY' => esc_html__( 'Paraguay', 'everest-forms' ),
1729 'PE' => esc_html__( 'Peru', 'everest-forms' ),
1730 'PH' => esc_html__( 'Philippines', 'everest-forms' ),
1731 'PN' => esc_html__( 'Pitcairn', 'everest-forms' ),
1732 'PL' => esc_html__( 'Poland', 'everest-forms' ),
1733 'PT' => esc_html__( 'Portugal', 'everest-forms' ),
1734 'PR' => esc_html__( 'Puerto Rico', 'everest-forms' ),
1735 'QA' => esc_html__( 'Qatar', 'everest-forms' ),
1736 'RE' => esc_html__( 'Réunion', 'everest-forms' ),
1737 'RO' => esc_html__( 'Romania', 'everest-forms' ),
1738 'RU' => esc_html__( 'Russian Federation', 'everest-forms' ),
1739 'RW' => esc_html__( 'Rwanda', 'everest-forms' ),
1740 'BL' => esc_html__( 'Saint Barthélemy', 'everest-forms' ),
1741 'SH' => esc_html__( 'Saint Helena, Ascension and Tristan da Cunha', 'everest-forms' ),
1742 'KN' => esc_html__( 'Saint Kitts and Nevis', 'everest-forms' ),
1743 'LC' => esc_html__( 'Saint Lucia', 'everest-forms' ),
1744 'MF' => esc_html__( 'Saint Martin (French part)', 'everest-forms' ),
1745 'PM' => esc_html__( 'Saint Pierre and Miquelon', 'everest-forms' ),
1746 'VC' => esc_html__( 'Saint Vincent and the Grenadines', 'everest-forms' ),
1747 'WS' => esc_html__( 'Samoa', 'everest-forms' ),
1748 'SM' => esc_html__( 'San Marino', 'everest-forms' ),
1749 'ST' => esc_html__( 'Sao Tome and Principe', 'everest-forms' ),
1750 'SA' => esc_html__( 'Saudi Arabia', 'everest-forms' ),
1751 'SN' => esc_html__( 'Senegal', 'everest-forms' ),
1752 'RS' => esc_html__( 'Serbia', 'everest-forms' ),
1753 'SC' => esc_html__( 'Seychelles', 'everest-forms' ),
1754 'SL' => esc_html__( 'Sierra Leone', 'everest-forms' ),
1755 'SG' => esc_html__( 'Singapore', 'everest-forms' ),
1756 'SX' => esc_html__( 'Sint Maarten (Dutch part)', 'everest-forms' ),
1757 'SK' => esc_html__( 'Slovakia', 'everest-forms' ),
1758 'SI' => esc_html__( 'Slovenia', 'everest-forms' ),
1759 'SB' => esc_html__( 'Solomon Islands', 'everest-forms' ),
1760 'SO' => esc_html__( 'Somalia', 'everest-forms' ),
1761 'ZA' => esc_html__( 'South Africa', 'everest-forms' ),
1762 'GS' => esc_html__( 'South Georgia and the South Sandwich Islands', 'everest-forms' ),
1763 'SS' => esc_html__( 'South Sudan', 'everest-forms' ),
1764 'ES' => esc_html__( 'Spain', 'everest-forms' ),
1765 'LK' => esc_html__( 'Sri Lanka', 'everest-forms' ),
1766 'SD' => esc_html__( 'Sudan', 'everest-forms' ),
1767 'SR' => esc_html__( 'Suriname', 'everest-forms' ),
1768 'SJ' => esc_html__( 'Svalbard and Jan Mayen', 'everest-forms' ),
1769 'SZ' => esc_html__( 'Swaziland', 'everest-forms' ),
1770 'SE' => esc_html__( 'Sweden', 'everest-forms' ),
1771 'CH' => esc_html__( 'Switzerland', 'everest-forms' ),
1772 'SY' => esc_html__( 'Syrian Arab Republic', 'everest-forms' ),
1773 'TW' => esc_html__( 'Taiwan, Province of China', 'everest-forms' ),
1774 'TJ' => esc_html__( 'Tajikistan', 'everest-forms' ),
1775 'TZ' => esc_html__( 'Tanzania (United Republic of)', 'everest-forms' ),
1776 'TH' => esc_html__( 'Thailand', 'everest-forms' ),
1777 'TL' => esc_html__( 'Timor-Leste', 'everest-forms' ),
1778 'TG' => esc_html__( 'Togo', 'everest-forms' ),
1779 'TK' => esc_html__( 'Tokelau', 'everest-forms' ),
1780 'TO' => esc_html__( 'Tonga', 'everest-forms' ),
1781 'TT' => esc_html__( 'Trinidad and Tobago', 'everest-forms' ),
1782 'TN' => esc_html__( 'Tunisia', 'everest-forms' ),
1783 'TR' => esc_html__( 'Turkey', 'everest-forms' ),
1784 'TM' => esc_html__( 'Turkmenistan', 'everest-forms' ),
1785 'TC' => esc_html__( 'Turks and Caicos Islands', 'everest-forms' ),
1786 'TV' => esc_html__( 'Tuvalu', 'everest-forms' ),
1787 'UG' => esc_html__( 'Uganda', 'everest-forms' ),
1788 'UA' => esc_html__( 'Ukraine', 'everest-forms' ),
1789 'AE' => esc_html__( 'United Arab Emirates', 'everest-forms' ),
1790 'GB' => esc_html__( 'United Kingdom of Great Britain and Northern Ireland', 'everest-forms' ),
1791 'US' => esc_html__( 'United States of America', 'everest-forms' ),
1792 'UM' => esc_html__( 'United States Minor Outlying Islands', 'everest-forms' ),
1793 'UY' => esc_html__( 'Uruguay', 'everest-forms' ),
1794 'UZ' => esc_html__( 'Uzbekistan', 'everest-forms' ),
1795 'VU' => esc_html__( 'Vanuatu', 'everest-forms' ),
1796 'VA' => esc_html__( 'Vatican City State', 'everest-forms' ),
1797 'VE' => esc_html__( 'Venezuela (Bolivarian Republic of)', 'everest-forms' ),
1798 'VN' => esc_html__( 'Viet Nam', 'everest-forms' ),
1799 'VG' => esc_html__( 'Virgin Islands (British)', 'everest-forms' ),
1800 'VI' => esc_html__( 'Virgin Islands (U.S.)', 'everest-forms' ),
1801 'WF' => esc_html__( 'Wallis and Futuna', 'everest-forms' ),
1802 'EH' => esc_html__( 'Western Sahara', 'everest-forms' ),
1803 'YE' => esc_html__( 'Yemen', 'everest-forms' ),
1804 'ZM' => esc_html__( 'Zambia', 'everest-forms' ),
1805 'ZW' => esc_html__( 'Zimbabwe', 'everest-forms' ),
1806 );
1807
1808 return (array) apply_filters( 'everest_forms_countries', $countries );
1809 }
1810
1811 /**
1812 * Get builder fields groups.
1813 *
1814 * @return array
1815 */
1816 function evf_get_fields_groups() {
1817 return (array) apply_filters(
1818 'everest_forms_builder_fields_groups',
1819 array(
1820 'general' => __( 'General Fields', 'everest-forms' ),
1821 'advanced' => __( 'Advanced Fields', 'everest-forms' ),
1822 'payment' => __( 'Payment Fields', 'everest-forms' ),
1823 'survey' => __( 'Survey Fields', 'everest-forms' ),
1824 )
1825 );
1826 }
1827
1828 /**
1829 * Get a builder fields type's name.
1830 *
1831 * @param string $type Coupon type.
1832 * @return string
1833 */
1834 function evf_get_fields_group( $type = '' ) {
1835 $types = evf_get_fields_groups();
1836 return isset( $types[ $type ] ) ? $types[ $type ] : '';
1837 }
1838
1839 /**
1840 * Get all fields settings.
1841 *
1842 * @return array Settings data.
1843 */
1844 function evf_get_all_fields_settings() {
1845 $settings = array(
1846 'label' => array(
1847 'id' => 'label',
1848 'title' => __( 'Label', 'everest-forms' ),
1849 'desc' => __( 'Enter text for the form field label. This is recommended and can be hidden in the Advanced Settings.', 'everest-forms' ),
1850 'default' => '',
1851 'type' => 'text',
1852 'desc_tip' => true,
1853 ),
1854 'meta' => array(
1855 'id' => 'meta-key',
1856 'title' => __( 'Meta Key', 'everest-forms' ),
1857 'desc' => __( 'Enter meta key to be stored in database.', 'everest-forms' ),
1858 'default' => '',
1859 'type' => 'text',
1860 'desc_tip' => true,
1861 ),
1862 'description' => array(
1863 'id' => 'description',
1864 'title' => __( 'Description', 'everest-forms' ),
1865 'type' => 'textarea',
1866 'desc' => __( 'Enter text for the form field description.', 'everest-forms' ),
1867 'default' => '',
1868 'desc_tip' => true,
1869 ),
1870 'required' => array(
1871 'id' => 'require',
1872 'title' => __( 'Required', 'everest-forms' ),
1873 'type' => 'checkbox',
1874 'desc' => __( 'Check this option to mark the field required.', 'everest-forms' ),
1875 'default' => 'no',
1876 'desc_tip' => true,
1877 ),
1878 'choices' => array(
1879 'id' => 'choices',
1880 'title' => __( 'Choices', 'everest-forms' ),
1881 'desc' => __( 'Add choices for the form field.', 'everest-forms' ),
1882 'type' => 'choices',
1883 'desc_tip' => true,
1884 'defaults' => array(
1885 1 => __( 'First Choice', 'everest-forms' ),
1886 2 => __( 'Second Choice', 'everest-forms' ),
1887 3 => __( 'Third Choice', 'everest-forms' ),
1888 ),
1889 ),
1890 'placeholder' => array(
1891 'id' => 'placeholder',
1892 'title' => __( 'Placeholder Text', 'everest-forms' ),
1893 'desc' => __( 'Enter text for the form field placeholder.', 'everest-forms' ),
1894 'default' => '',
1895 'type' => 'text',
1896 'desc_tip' => true,
1897 ),
1898 'css' => array(
1899 'id' => 'css',
1900 'title' => __( 'CSS Classes', 'everest-forms' ),
1901 'desc' => __( 'Enter CSS class for this field container. Class names should be separated with spaces.', 'everest-forms' ),
1902 'default' => '',
1903 'type' => 'text',
1904 'desc_tip' => true,
1905 ),
1906 'label_hide' => array(
1907 'id' => 'label_hide',
1908 'title' => __( 'Hide Label', 'everest-forms' ),
1909 'type' => 'checkbox',
1910 'desc' => __( 'Check this option to hide the form field label.', 'everest-forms' ),
1911 'default' => 'no',
1912 'desc_tip' => true,
1913 ),
1914 'sublabel_hide' => array(
1915 'id' => 'sublabel_hide',
1916 'title' => __( 'Hide Sub-Labels', 'everest-forms' ),
1917 'type' => 'checkbox',
1918 'desc' => __( 'Check this option to hide the form field sub-label.', 'everest-forms' ),
1919 'default' => 'no',
1920 'desc_tip' => true,
1921 ),
1922 );
1923
1924 return apply_filters( 'everest_form_all_fields_settings', $settings );
1925 }
1926
1927 /**
1928 * Helper function to display debug data.
1929 *
1930 * @since 1.3.2
1931 *
1932 * @param mixed $expression The expression to be printed.
1933 * @param bool $return Optional. Default false. Set to true to return the human-readable string.
1934 *
1935 * @return string
1936 */
1937 function evf_debug_data( $expression, $return = false ) {
1938 if ( defined( 'EVF_DEBUG' ) && true === EVF_DEBUG ) {
1939 $output = '<textarea style="color:#666;background:#fff;margin: 20px 0;width:100%;height:500px;font-size:12px;font-family: Consolas,Monaco,Lucida Console,monospace;direction: ltr;unicode-bidi: embed;line-height: 1.4;padding: 4px 6px 1px;" readonly>';
1940
1941 $output .= "==================== Everest Forms Debugging ====================\n\n";
1942
1943 if ( is_array( $expression ) || is_object( $expression ) ) {
1944 $output .= evf_print_r( $expression, true );
1945 } else {
1946 $output .= $expression;
1947 }
1948
1949 $output .= '</textarea>';
1950
1951 if ( $return ) {
1952 return $output;
1953 } else {
1954 echo $output; // phpcs:ignore
1955 }
1956 }
1957 }
1958
1959 /**
1960 * String translation function.
1961 *
1962 * @since 1.4.9
1963 *
1964 * @param int $form_id Form ID.
1965 * @param string $field_id Field ID.
1966 * @param mixed $variable To be translated for WPML compatibility.
1967 */
1968 function evf_string_translation( $form_id, $field_id, $variable ) {
1969 if ( function_exists( 'icl_register_string' ) ) {
1970 icl_register_string( isset( $form_id ) ? 'everest_forms_' . absint( $form_id ) : 0, isset( $field_id ) ? $field_id : '', $variable );
1971 }
1972
1973 if ( function_exists( 'icl_t' ) ) {
1974 $variable = icl_t( isset( $form_id ) ? 'everest_forms_' . absint( $form_id ) : 0, isset( $field_id ) ? $field_id : '', $variable );
1975 }
1976
1977 return $variable;
1978 }
1979
1980 /**
1981 * Trigger logging cleanup using the logging class.
1982 *
1983 * @since 1.6.2
1984 */
1985 function evf_cleanup_logs() {
1986 $logger = evf_get_logger();
1987
1988 if ( is_callable( array( $logger, 'clear_expired_logs' ) ) ) {
1989 $logger->clear_expired_logs();
1990 }
1991 }
1992 add_action( 'everest_forms_cleanup_logs', 'evf_cleanup_logs' );
1993