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-formatting-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-formatting-functions.php
639 lines
1 <?php
2 /**
3 * EverestForms Formatting
4 *
5 * Functions for formatting data.
6 *
7 * @package WPEverest\Functions
8 * @version 1.0.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Converts a string (e.g. 'yes' or 'no') to a bool.
15 *
16 * @param string $string String to convert.
17 * @return bool
18 */
19 function evf_string_to_bool( $string ) {
20 return is_bool( $string ) ? $string : ( 'yes' === $string || 1 === $string || 'true' === $string || '1' === $string );
21 }
22
23 /**
24 * Converts a bool to a 'yes' or 'no'.
25 *
26 * @param bool $bool String to convert.
27 * @return string
28 */
29 function evf_bool_to_string( $bool ) {
30 if ( ! is_bool( $bool ) ) {
31 $bool = evf_string_to_bool( $bool );
32 }
33 return true === $bool ? 'yes' : 'no';
34 }
35
36 /**
37 * Add a suffix into an array.
38 *
39 * @since 1.4.5
40 * @param array $array Raw array data.
41 * @param string $suffix Suffix to be added.
42 * @return array Modified array with suffix added.
43 */
44 function evf_suffix_array( $array = array(), $suffix = '' ) {
45 return preg_filter( '/$/', $suffix, $array );
46 }
47
48 /**
49 * Implode an array into a string by $glue and remove empty values.
50 *
51 * @since 1.4.5
52 * @param array $array Array to convert.
53 * @param string $glue Glue, defaults to ' '.
54 * @return string
55 */
56 function evf_array_to_string( $array = array(), $glue = ' ' ) {
57 return is_string( $array ) ? $array : implode( $glue, array_filter( $array ) );
58 }
59
60 /**
61 * Explode a string into an array by $delimiter and remove empty values.
62 *
63 * @param string $string String to convert.
64 * @param string $delimiter Delimiter, defaults to ','.
65 * @return array
66 */
67 function evf_string_to_array( $string, $delimiter = ',' ) {
68 return is_array( $string ) ? $string : array_filter( explode( $delimiter, $string ) );
69 }
70
71 /**
72 * Format dimensions for display.
73 *
74 * @since 1.4.5
75 * @param array $dimensions Array of dimensions.
76 * @param array $unit Unit, defaults to 'px'.
77 * @return string
78 */
79 function evf_sanitize_dimension_unit( $dimensions = array(), $unit = 'px' ) {
80 return evf_array_to_string( evf_suffix_array( $dimensions, $unit ) );
81 }
82
83 /**
84 * Sanitize taxonomy names. Slug format (no spaces, lowercase).
85 * Urldecode is used to reverse munging of UTF8 characters.
86 *
87 * @param string $taxonomy Taxonomy name.
88 * @return string
89 */
90 function evf_sanitize_taxonomy_name( $taxonomy ) {
91 return apply_filters( 'sanitize_taxonomy_name', urldecode( sanitize_title( urldecode( $taxonomy ) ) ), $taxonomy );
92 }
93
94 /**
95 * Sanitize permalink values before insertion into DB.
96 *
97 * Cannot use evf_clean because it sometimes strips % chars and breaks the user's setting.
98 *
99 * @param string $value Permalink.
100 * @return string
101 */
102 function evf_sanitize_permalink( $value ) {
103 global $wpdb;
104
105 $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
106
107 if ( is_wp_error( $value ) ) {
108 $value = '';
109 }
110
111 $value = esc_url_raw( trim( $value ) );
112 $value = str_replace( 'http://', '', $value );
113 return untrailingslashit( $value );
114 }
115
116 /**
117 * Gets the filename part of a download URL.
118 *
119 * @param string $file_url File URL.
120 * @return string
121 */
122 function evf_get_filename_from_url( $file_url ) {
123 $parts = wp_parse_url( $file_url );
124 if ( isset( $parts['path'] ) ) {
125 return basename( $parts['path'] );
126 }
127 }
128
129 /**
130 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
131 * Non-scalar values are ignored.
132 *
133 * @param string|array $var Data to sanitize.
134 * @return string|array
135 */
136 function evf_clean( $var ) {
137 if ( is_array( $var ) ) {
138 return array_map( 'evf_clean', $var );
139 } else {
140 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
141 }
142 }
143
144 /**
145 * Run evf_clean over posted textarea but maintain line breaks.
146 *
147 * @param string $var Data to sanitize.
148 * @return string
149 */
150 function evf_sanitize_textarea( $var ) {
151 return implode( "\n", array_map( 'evf_clean', explode( "\n", $var ) ) );
152 }
153
154 /**
155 * Sanitize a string destined to be a tooltip.
156 *
157 * @since 1.0.0 Tooltips are encoded with htmlspecialchars to prevent XSS. Should not be used in conjunction with esc_attr()
158 * @param string $var Data to sanitize.
159 * @return string
160 */
161 function evf_sanitize_tooltip( $var ) {
162 return htmlspecialchars(
163 wp_kses(
164 html_entity_decode( $var ),
165 array(
166 'br' => array(),
167 'em' => array(),
168 'strong' => array(),
169 'small' => array(),
170 'span' => array(),
171 'ul' => array(),
172 'li' => array(),
173 'ol' => array(),
174 'p' => array(),
175 )
176 )
177 );
178 }
179
180 /**
181 * Merge two arrays.
182 *
183 * @param array $a1 First array to merge.
184 * @param array $a2 Second array to merge.
185 * @return array
186 */
187 function evf_array_overlay( $a1, $a2 ) {
188 foreach ( $a1 as $k => $v ) {
189 if ( ! array_key_exists( $k, $a2 ) ) {
190 continue;
191 }
192 if ( is_array( $v ) && is_array( $a2[ $k ] ) ) {
193 $a1[ $k ] = evf_array_overlay( $v, $a2[ $k ] );
194 } else {
195 $a1[ $k ] = $a2[ $k ];
196 }
197 }
198 return $a1;
199 }
200
201 /**
202 * Array combine.
203 *
204 * @param array $array Array of data.
205 * @return array
206 */
207 function evf_sanitize_array_combine( $array ) {
208 if ( empty( $array ) || ! is_array( $array ) ) {
209 return $array;
210 }
211
212 return array_map( 'sanitize_text_field', $array );
213 }
214
215 /**
216 * Notation to numbers.
217 *
218 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
219 *
220 * @param string $size Size value.
221 * @return int
222 */
223 function evf_let_to_num( $size ) {
224 $l = substr( $size, -1 );
225 $ret = substr( $size, 0, -1 );
226 $byte = 1024;
227
228 switch ( strtoupper( $l ) ) {
229 case 'P':
230 $ret *= 1024;
231 // No break.
232 case 'T':
233 $ret *= 1024;
234 // No break.
235 case 'G':
236 $ret *= 1024;
237 // No break.
238 case 'M':
239 $ret *= 1024;
240 // No break.
241 case 'K':
242 $ret *= 1024;
243 // No break.
244 }
245 return $ret;
246 }
247
248 /**
249 * EverestForms Date Format - Allows to change date format for everything EverestForms.
250 *
251 * @return string
252 */
253 function evf_date_format() {
254 return apply_filters( 'everest_forms_date_format', get_option( 'date_format' ) );
255 }
256
257 /**
258 * EverestForms Time Format - Allows to change time format for everything EverestForms.
259 *
260 * @return string
261 */
262 function evf_time_format() {
263 return apply_filters( 'everest_forms_time_format', get_option( 'time_format' ) );
264 }
265
266 /**
267 * Convert mysql datetime to PHP timestamp, forcing UTC. Wrapper for strtotime.
268 *
269 * Based on wcs_strtotime_dark_knight() from WC Subscriptions by Prospress.
270 *
271 * @since 3.0.0
272 * @param string $time_string Time string.
273 * @param int|null $from_timestamp Timestamp to convert from.
274 * @return int
275 */
276 function evf_string_to_timestamp( $time_string, $from_timestamp = null ) {
277 $original_timezone = date_default_timezone_get();
278
279 // @codingStandardsIgnoreStart
280 date_default_timezone_set( 'UTC' );
281
282 if ( null === $from_timestamp ) {
283 $next_timestamp = strtotime( $time_string );
284 } else {
285 $next_timestamp = strtotime( $time_string, $from_timestamp );
286 }
287
288 date_default_timezone_set( $original_timezone );
289 // @codingStandardsIgnoreEnd
290
291 return $next_timestamp;
292 }
293
294 /**
295 * Callback which can flatten post meta (gets the first value if it's an array).
296 *
297 * @param array $value Value to flatten.
298 * @return mixed
299 */
300 function evf_flatten_meta_callback( $value ) {
301 return is_array( $value ) ? current( $value ) : $value;
302 }
303
304 if ( ! function_exists( 'evf_rgb_from_hex' ) ) {
305
306 /**
307 * Convert RGB to HEX.
308 *
309 * @param mixed $color Color.
310 *
311 * @return array
312 */
313 function evf_rgb_from_hex( $color ) {
314 $color = str_replace( '#', '', $color );
315 // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF".
316 $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
317
318 $rgb = array();
319 $rgb['R'] = hexdec( $color[0] . $color[1] );
320 $rgb['G'] = hexdec( $color[2] . $color[3] );
321 $rgb['B'] = hexdec( $color[4] . $color[5] );
322
323 return $rgb;
324 }
325 }
326
327 if ( ! function_exists( 'evf_hex_darker' ) ) {
328
329 /**
330 * Make HEX color darker.
331 *
332 * @param mixed $color Color.
333 * @param int $factor Darker factor.
334 * Defaults to 30.
335 * @return string
336 */
337 function evf_hex_darker( $color, $factor = 30 ) {
338 $base = evf_rgb_from_hex( $color );
339 $color = '#';
340
341 foreach ( $base as $k => $v ) {
342 $amount = $v / 100;
343 $amount = round( $amount * $factor );
344 $new_decimal = $v - $amount;
345
346 $new_hex_component = dechex( $new_decimal );
347 if ( strlen( $new_hex_component ) < 2 ) {
348 $new_hex_component = '0' . $new_hex_component;
349 }
350 $color .= $new_hex_component;
351 }
352
353 return $color;
354 }
355 }
356
357 if ( ! function_exists( 'evf_hex_lighter' ) ) {
358
359 /**
360 * Make HEX color lighter.
361 *
362 * @param mixed $color Color.
363 * @param int $factor Lighter factor.
364 * Defaults to 30.
365 * @return string
366 */
367 function evf_hex_lighter( $color, $factor = 30 ) {
368 $base = evf_rgb_from_hex( $color );
369 $color = '#';
370
371 foreach ( $base as $k => $v ) {
372 $amount = 255 - $v;
373 $amount = $amount / 100;
374 $amount = round( $amount * $factor );
375 $new_decimal = $v + $amount;
376
377 $new_hex_component = dechex( $new_decimal );
378 if ( strlen( $new_hex_component ) < 2 ) {
379 $new_hex_component = '0' . $new_hex_component;
380 }
381 $color .= $new_hex_component;
382 }
383
384 return $color;
385 }
386 }
387
388 if ( ! function_exists( 'evf_is_light' ) ) {
389
390 /**
391 * Determine whether a hex color is light.
392 *
393 * @param mixed $color Color.
394 * @return bool True if a light color.
395 */
396 function evf_hex_is_light( $color ) {
397 $hex = str_replace( '#', '', $color );
398
399 $c_r = hexdec( substr( $hex, 0, 2 ) );
400 $c_g = hexdec( substr( $hex, 2, 2 ) );
401 $c_b = hexdec( substr( $hex, 4, 2 ) );
402
403 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
404
405 return $brightness > 155;
406 }
407 }
408
409 if ( ! function_exists( 'evf_light_or_dark' ) ) {
410
411 /**
412 * Detect if we should use a light or dark color on a background color.
413 *
414 * @param mixed $color Color.
415 * @param string $dark Darkest reference.
416 * Defaults to '#000000'.
417 * @param string $light Lightest reference.
418 * Defaults to '#FFFFFF'.
419 * @return string
420 */
421 function evf_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
422 return evf_hex_is_light( $color ) ? $dark : $light;
423 }
424 }
425
426 if ( ! function_exists( 'evf_format_hex' ) ) {
427
428 /**
429 * Format string as hex.
430 *
431 * @param string $hex HEX color.
432 * @return string|null
433 */
434 function evf_format_hex( $hex ) {
435 $hex = trim( str_replace( '#', '', $hex ) );
436
437 if ( strlen( $hex ) === 3 ) {
438 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
439 }
440
441 return $hex ? '#' . $hex : null;
442 }
443 }
444
445 /**
446 * Format phone numbers.
447 *
448 * @param string $phone Phone number.
449 * @return string
450 */
451 function evf_format_phone_number( $phone ) {
452 return str_replace( '.', '-', $phone );
453 }
454
455 /**
456 * Wrapper for mb_strtoupper which see's if supported first.
457 *
458 * @param string $string String to format.
459 * @return string
460 */
461 function evf_strtoupper( $string ) {
462 return function_exists( 'mb_strtoupper' ) ? mb_strtoupper( $string, 'UTF-8' ) : strtoupper( $string );
463 }
464
465 /**
466 * Make a string lowercase.
467 * Try to use mb_strtolower() when available.
468 *
469 * @param string $string String to format.
470 * @return string
471 */
472 function evf_strtolower( $string ) {
473 return function_exists( 'mb_strtolower' ) ? mb_strtolower( $string, 'UTF-8' ) : strtolower( $string );
474 }
475
476 /**
477 * Trim a string and append a suffix.
478 *
479 * @param string $string String to trim.
480 * @param integer $chars Amount of characters.
481 * Defaults to 200.
482 * @param string $suffix Suffix.
483 * Defaults to '...'.
484 * @return string
485 */
486 function evf_trim_string( $string, $chars = 200, $suffix = '...' ) {
487 if ( strlen( $string ) > $chars ) {
488 if ( function_exists( 'mb_substr' ) ) {
489 $string = mb_substr( $string, 0, ( $chars - mb_strlen( $suffix, 'UTF-8' ) ), 'UTF-8' ) . $suffix;
490 } else {
491 $string = substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;
492 }
493 }
494 return $string;
495 }
496
497 /**
498 * Format content to display shortcodes.
499 *
500 * @param string $raw_string Raw string.
501 * @return string
502 */
503 function evf_format_content( $raw_string ) {
504 return apply_filters( 'everest_forms_format_content', apply_filters( 'everest_forms_short_description', $raw_string ), $raw_string );
505 }
506
507 /**
508 * Process oEmbeds.
509 *
510 * @param string $content Content.
511 * @return string
512 */
513 function evf_do_oembeds( $content ) {
514 global $wp_embed;
515
516 $content = $wp_embed->autoembed( $content );
517
518 return $content;
519 }
520
521 /**
522 * Array merge and sum function.
523 *
524 * Source: https://gist.github.com/Nickology/f700e319cbafab5eaedc
525 *
526 * @since 1.0.0
527 * @return array
528 */
529 function evf_array_merge_recursive_numeric() {
530 $arrays = func_get_args();
531
532 // If there's only one array, it's already merged.
533 if ( 1 === count( $arrays ) ) {
534 return $arrays[0];
535 }
536
537 // Remove any items in $arrays that are NOT arrays.
538 foreach ( $arrays as $key => $array ) {
539 if ( ! is_array( $array ) ) {
540 unset( $arrays[ $key ] );
541 }
542 }
543
544 // We start by setting the first array as our final array.
545 // We will merge all other arrays with this one.
546 $final = array_shift( $arrays );
547
548 foreach ( $arrays as $b ) {
549 foreach ( $final as $key => $value ) {
550 // If $key does not exist in $b, then it is unique and can be safely merged.
551 if ( ! isset( $b[ $key ] ) ) {
552 $final[ $key ] = $value;
553 } else {
554 // If $key is present in $b, then we need to merge and sum numeric values in both.
555 if ( is_numeric( $value ) && is_numeric( $b[ $key ] ) ) {
556 // If both values for these keys are numeric, we sum them.
557 $final[ $key ] = $value + $b[ $key ];
558 } elseif ( is_array( $value ) && is_array( $b[ $key ] ) ) {
559 // If both values are arrays, we recursively call ourself.
560 $final[ $key ] = evf_array_merge_recursive_numeric( $value, $b[ $key ] );
561 } else {
562 // If both keys exist but differ in type, then we cannot merge them.
563 // In this scenario, we will $b's value for $key is used.
564 $final[ $key ] = $b[ $key ];
565 }
566 }
567 }
568
569 // Finally, we need to merge any keys that exist only in $b.
570 foreach ( $b as $key => $value ) {
571 if ( ! isset( $final[ $key ] ) ) {
572 $final[ $key ] = $value;
573 }
574 }
575 }
576
577 return $final;
578 }
579
580 /**
581 * Implode and escape HTML attributes for output.
582 *
583 * @since 1.2.0
584 * @param array $raw_attributes Attribute name value pairs.
585 * @return string
586 */
587 function evf_implode_html_attributes( $raw_attributes ) {
588 $attributes = array();
589 foreach ( $raw_attributes as $name => $value ) {
590 $attributes[] = esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
591 }
592 return implode( ' ', $attributes );
593 }
594
595 /**
596 * Parse a relative date option from the settings API into a standard format.
597 *
598 * @since 1.2.0
599 * @param mixed $raw_value Value stored in DB.
600 * @return array Nicely formatted array with number and unit values.
601 */
602 function evf_parse_relative_date_option( $raw_value ) {
603 $periods = array(
604 'days' => __( 'Day(s)', 'everest-forms' ),
605 'weeks' => __( 'Week(s)', 'everest-forms' ),
606 'months' => __( 'Month(s)', 'everest-forms' ),
607 'years' => __( 'Year(s)', 'everest-forms' ),
608 );
609
610 $value = wp_parse_args(
611 (array) $raw_value,
612 array(
613 'number' => '',
614 'unit' => 'days',
615 )
616 );
617
618 $value['number'] = ! empty( $value['number'] ) ? absint( $value['number'] ) : '';
619
620 if ( ! in_array( $value['unit'], array_keys( $periods ), true ) ) {
621 $value['unit'] = 'days';
622 }
623
624 return $value;
625 }
626
627 /**
628 * Callback which can flatten structure data (gets the value if it's a multidimensional array).
629 *
630 * @since 1.3.0
631 * @param array $value Value to flatten.
632 * @return array
633 */
634 function evf_flatten_array( $value = array() ) {
635 $return = array();
636 array_walk_recursive( $value, function( $a ) use ( &$return ) { $return[] = $a; } ); // @codingStandardsIgnoreLine.
637 return $return;
638 }
639