PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 2.0.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v2.0.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 2 years ago admin 2 years ago elementor 4 years ago export 3 years ago fields 2 years ago interfaces 8 years ago libraries 2 years ago log-handlers 4 years ago shortcodes 2 years ago stats 3 years ago templates 5 years ago class-everest-forms.php 2 years ago class-evf-ajax.php 2 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-cron.php 3 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 5 years ago class-evf-emails.php 2 years ago class-evf-fields.php 2 years ago class-evf-form-block.php 4 years ago class-evf-form-handler.php 3 years ago class-evf-form-task.php 2 years ago class-evf-forms-features.php 2 years ago class-evf-frontend-scripts.php 2 years ago class-evf-install.php 2 years ago class-evf-integrations.php 7 years ago class-evf-log-levels.php 8 years ago class-evf-logger.php 5 years ago class-evf-post-types.php 5 years ago class-evf-privacy.php 6 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 4 years ago class-evf-smart-tags.php 2 years ago class-evf-template-loader.php 2 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 2 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 3 years ago evf-formatting-functions.php 4 years ago evf-notice-functions.php 4 years ago evf-template-functions.php 4 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 5 years ago
evf-formatting-functions.php
641 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 * Callback which can flatten post meta (gets the first value if it's an array).
268 *
269 * @param array $value Value to flatten.
270 * @return mixed
271 */
272 function evf_flatten_meta_callback( $value ) {
273 return is_array( $value ) ? current( $value ) : $value;
274 }
275
276 if ( ! function_exists( 'evf_rgb_from_hex' ) ) {
277
278 /**
279 * Convert RGB to HEX.
280 *
281 * @param mixed $color Color.
282 *
283 * @return array
284 */
285 function evf_rgb_from_hex( $color ) {
286 $color = str_replace( '#', '', $color );
287 // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF".
288 $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
289
290 $rgb = array();
291 $rgb['R'] = hexdec( $color[0] . $color[1] );
292 $rgb['G'] = hexdec( $color[2] . $color[3] );
293 $rgb['B'] = hexdec( $color[4] . $color[5] );
294
295 return $rgb;
296 }
297 }
298
299 if ( ! function_exists( 'evf_hex_darker' ) ) {
300
301 /**
302 * Make HEX color darker.
303 *
304 * @param mixed $color Color.
305 * @param int $factor Darker factor.
306 * Defaults to 30.
307 * @return string
308 */
309 function evf_hex_darker( $color, $factor = 30 ) {
310 $base = evf_rgb_from_hex( $color );
311 $color = '#';
312
313 foreach ( $base as $k => $v ) {
314 $amount = $v / 100;
315 $amount = round( $amount * $factor );
316 $new_decimal = $v - $amount;
317
318 $new_hex_component = dechex( $new_decimal );
319 if ( strlen( $new_hex_component ) < 2 ) {
320 $new_hex_component = '0' . $new_hex_component;
321 }
322 $color .= $new_hex_component;
323 }
324
325 return $color;
326 }
327 }
328
329 if ( ! function_exists( 'evf_hex_lighter' ) ) {
330
331 /**
332 * Make HEX color lighter.
333 *
334 * @param mixed $color Color.
335 * @param int $factor Lighter factor.
336 * Defaults to 30.
337 * @return string
338 */
339 function evf_hex_lighter( $color, $factor = 30 ) {
340 $base = evf_rgb_from_hex( $color );
341 $color = '#';
342
343 foreach ( $base as $k => $v ) {
344 $amount = 255 - $v;
345 $amount = $amount / 100;
346 $amount = round( $amount * $factor );
347 $new_decimal = $v + $amount;
348
349 $new_hex_component = dechex( $new_decimal );
350 if ( strlen( $new_hex_component ) < 2 ) {
351 $new_hex_component = '0' . $new_hex_component;
352 }
353 $color .= $new_hex_component;
354 }
355
356 return $color;
357 }
358 }
359
360 if ( ! function_exists( 'evf_is_light' ) ) {
361
362 /**
363 * Determine whether a hex color is light.
364 *
365 * @param mixed $color Color.
366 * @return bool True if a light color.
367 */
368 function evf_hex_is_light( $color ) {
369 $hex = str_replace( '#', '', $color );
370
371 $c_r = hexdec( substr( $hex, 0, 2 ) );
372 $c_g = hexdec( substr( $hex, 2, 2 ) );
373 $c_b = hexdec( substr( $hex, 4, 2 ) );
374
375 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
376
377 return $brightness > 155;
378 }
379 }
380
381 if ( ! function_exists( 'evf_light_or_dark' ) ) {
382
383 /**
384 * Detect if we should use a light or dark color on a background color.
385 *
386 * @param mixed $color Color.
387 * @param string $dark Darkest reference.
388 * Defaults to '#000000'.
389 * @param string $light Lightest reference.
390 * Defaults to '#FFFFFF'.
391 * @return string
392 */
393 function evf_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
394 return evf_hex_is_light( $color ) ? $dark : $light;
395 }
396 }
397
398 if ( ! function_exists( 'evf_format_hex' ) ) {
399
400 /**
401 * Format string as hex.
402 *
403 * @param string $hex HEX color.
404 * @return string|null
405 */
406 function evf_format_hex( $hex ) {
407 $hex = trim( str_replace( '#', '', $hex ) );
408
409 if ( strlen( $hex ) === 3 ) {
410 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
411 }
412
413 return $hex ? '#' . $hex : null;
414 }
415 }
416
417 /**
418 * Format phone numbers.
419 *
420 * @param string $phone Phone number.
421 * @return string
422 */
423 function evf_format_phone_number( $phone ) {
424 return str_replace( '.', '-', $phone );
425 }
426
427 /**
428 * Wrapper for mb_strtoupper which see's if supported first.
429 *
430 * @param string $string String to format.
431 * @return string
432 */
433 function evf_strtoupper( $string ) {
434 return function_exists( 'mb_strtoupper' ) ? mb_strtoupper( $string, 'UTF-8' ) : strtoupper( $string );
435 }
436
437 /**
438 * Make a string lowercase.
439 * Try to use mb_strtolower() when available.
440 *
441 * @param string $string String to format.
442 * @return string
443 */
444 function evf_strtolower( $string ) {
445 return function_exists( 'mb_strtolower' ) ? mb_strtolower( $string, 'UTF-8' ) : strtolower( $string );
446 }
447
448 /**
449 * Trim a string and append a suffix.
450 *
451 * @param string $string String to trim.
452 * @param integer $chars Amount of characters.
453 * Defaults to 200.
454 * @param string $suffix Suffix.
455 * Defaults to '...'.
456 * @return string
457 */
458 function evf_trim_string( $string, $chars = 200, $suffix = '...' ) {
459 if ( strlen( $string ) > $chars ) {
460 if ( function_exists( 'mb_substr' ) ) {
461 $string = mb_substr( $string, 0, ( $chars - mb_strlen( $suffix, 'UTF-8' ) ), 'UTF-8' ) . $suffix;
462 } else {
463 $string = substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;
464 }
465 }
466 return $string;
467 }
468
469 /**
470 * Format content to display shortcodes.
471 *
472 * @param string $raw_string Raw string.
473 * @return string
474 */
475 function evf_format_content( $raw_string ) {
476 return apply_filters( 'everest_forms_format_content', apply_filters( 'everest_forms_short_description', $raw_string ), $raw_string );
477 }
478
479 /**
480 * Process oEmbeds.
481 *
482 * @param string $content Content.
483 * @return string
484 */
485 function evf_do_oembeds( $content ) {
486 global $wp_embed;
487
488 $content = $wp_embed->autoembed( $content );
489
490 return $content;
491 }
492
493 /**
494 * Array merge and sum function.
495 *
496 * Source: https://gist.github.com/Nickology/f700e319cbafab5eaedc
497 *
498 * @since 1.0.0
499 * @return array
500 */
501 function evf_array_merge_recursive_numeric() {
502 $arrays = func_get_args();
503
504 // If there's only one array, it's already merged.
505 if ( 1 === count( $arrays ) ) {
506 return $arrays[0];
507 }
508
509 // Remove any items in $arrays that are NOT arrays.
510 foreach ( $arrays as $key => $array ) {
511 if ( ! is_array( $array ) ) {
512 unset( $arrays[ $key ] );
513 }
514 }
515
516 // We start by setting the first array as our final array.
517 // We will merge all other arrays with this one.
518 $final = array_shift( $arrays );
519
520 foreach ( $arrays as $b ) {
521 foreach ( $final as $key => $value ) {
522 // If $key does not exist in $b, then it is unique and can be safely merged.
523 if ( ! isset( $b[ $key ] ) ) {
524 $final[ $key ] = $value;
525 } else {
526 // If $key is present in $b, then we need to merge and sum numeric values in both.
527 if ( is_numeric( $value ) && is_numeric( $b[ $key ] ) ) {
528 // If both values for these keys are numeric, we sum them.
529 $final[ $key ] = $value + $b[ $key ];
530 } elseif ( is_array( $value ) && is_array( $b[ $key ] ) ) {
531 // If both values are arrays, we recursively call ourself.
532 $final[ $key ] = evf_array_merge_recursive_numeric( $value, $b[ $key ] );
533 } else {
534 // If both keys exist but differ in type, then we cannot merge them.
535 // In this scenario, we will $b's value for $key is used.
536 $final[ $key ] = $b[ $key ];
537 }
538 }
539 }
540
541 // Finally, we need to merge any keys that exist only in $b.
542 foreach ( $b as $key => $value ) {
543 if ( ! isset( $final[ $key ] ) ) {
544 $final[ $key ] = $value;
545 }
546 }
547 }
548
549 return $final;
550 }
551
552 /**
553 * Implode and escape HTML attributes for output.
554 *
555 * @since 1.2.0
556 * @param array $raw_attributes Attribute name value pairs.
557 * @return string
558 */
559 function evf_implode_html_attributes( $raw_attributes ) {
560 $attributes = array();
561 foreach ( $raw_attributes as $name => $value ) {
562 $attributes[] = esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
563 }
564 return implode( ' ', $attributes );
565 }
566
567 /**
568 * Parse a relative date option from the settings API into a standard format.
569 *
570 * @since 1.2.0
571 * @param mixed $raw_value Value stored in DB.
572 * @return array Nicely formatted array with number and unit values.
573 */
574 function evf_parse_relative_date_option( $raw_value ) {
575 $periods = array(
576 'days' => __( 'Day(s)', 'everest-forms' ),
577 'weeks' => __( 'Week(s)', 'everest-forms' ),
578 'months' => __( 'Month(s)', 'everest-forms' ),
579 'years' => __( 'Year(s)', 'everest-forms' ),
580 );
581
582 $value = wp_parse_args(
583 (array) $raw_value,
584 array(
585 'number' => '',
586 'unit' => 'days',
587 )
588 );
589
590 $value['number'] = ! empty( $value['number'] ) ? absint( $value['number'] ) : '';
591
592 if ( ! in_array( $value['unit'], array_keys( $periods ), true ) ) {
593 $value['unit'] = 'days';
594 }
595
596 return $value;
597 }
598
599 /**
600 * Callback which can flatten structure data (gets the value if it's a multidimensional array).
601 *
602 * @since 1.3.0
603 * @param array $value Value to flatten.
604 * @return array
605 */
606 function evf_flatten_array( $value = array() ) {
607 $return = array();
608 array_walk_recursive( $value, function( $a ) use ( &$return ) { $return[] = $a; } ); // @codingStandardsIgnoreLine.
609 return $return;
610 }
611
612 /**
613 * An `array_splice` which does preverse the keys of the replacement array
614 *
615 * The argument list is identical to `array_splice`
616 *
617 * @since 1.6.5
618 *
619 * @link https://github.com/lode/gaps/blob/master/src/gaps.php
620 *
621 * @param array $input The input array.
622 * @param int $offset The offeset to start.
623 * @param int $length Optional length.
624 * @param array $replacement The replacement array.
625 *
626 * @return array the array consisting of the extracted elements.
627 */
628 function evf_array_splice_preserve_keys( &$input, $offset, $length = null, $replacement = array() ) {
629 if ( empty( $replacement ) ) {
630 return array_splice( $input, $offset, $length );
631 }
632
633 $part_before = array_slice( $input, 0, $offset, true );
634 $part_removed = array_slice( $input, $offset, $length, true );
635 $part_after = array_slice( $input, $offset + $length, null, true );
636
637 $input = $part_before + $replacement + $part_after;
638
639 return $part_removed;
640 }
641