PluginProbe ʕ •ᴥ•ʔ
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More / 1.3.6.1
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More v1.3.6.1
1.10.1.1 1.10.1 1.10.0.5 trunk 1.1.4 1.1.4.2 1.1.5 1.1.5.1 1.1.6 1.1.6.1 1.1.7 1.1.7.1 1.1.7.2 1.1.8 1.1.8.1 1.1.8.2 1.1.8.3 1.1.8.4 1.10.0.1 1.10.0.2 1.10.0.3 1.10.0.4 1.2.0 1.2.0.1 1.2.1 1.2.2 1.2.2.1 1.2.2.2 1.2.3 1.2.3.1 1.2.3.2 1.2.4 1.2.4.1 1.2.5 1.2.5.1 1.2.6 1.2.7 1.2.8 1.2.8.1 1.2.9 1.3.0 1.3.1 1.3.1.1 1.3.1.2 1.3.2 1.3.3 1.3.5 1.3.6 1.3.6.1 1.3.6.2 1.3.7.2 1.3.7.3 1.3.7.4 1.3.8 1.3.9.1 1.4.0.1 1.4.1.1 1.4.2 1.4.2.1 1.4.2.2 1.4.3 1.4.4 1.4.4.1 1.4.5 1.4.5.1 1.4.5.2 1.4.5.3 1.4.6 1.4.7.1 1.4.7.2 1.4.8.1 1.4.9 1.5.0.1 1.5.0.3 1.5.0.4 1.5.1 1.5.1.1 1.5.1.3 1.5.2.1 1.5.2.2 1.5.2.3 1.5.3 1.5.3.1 1.5.4.1 1.5.4.2 1.5.5 1.5.5.1 1.5.6 1.5.6.2 1.5.7 1.5.8.2 1.5.9.1 1.5.9.4 1.5.9.5 1.6.0.1 1.6.0.2 1.6.1 1.6.2.2 1.6.2.3 1.6.3.1 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.7 1.6.7.1 1.6.7.2 1.6.7.3 1.6.8 1.6.8.1 1.6.9 1.7.0 1.7.1.1 1.7.1.2 1.7.2 1.7.2.1 1.7.3 1.7.4 1.7.4.1 1.7.4.2 1.7.5.1 1.7.5.2 1.7.5.3 1.7.5.5 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.7.9.1 1.8.0.1 1.8.0.2 1.8.1.1 1.8.1.2 1.8.1.3 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.3.1 1.8.4 1.8.4.1 1.8.5.2 1.8.5.3 1.8.5.4 1.8.6.2 1.8.6.3 1.8.6.4 1.8.7.2 1.8.8.2 1.8.8.3 1.8.9.1 1.8.9.2 1.8.9.4 1.8.9.5 1.8.9.6 1.9.0.1 1.9.0.2 1.9.0.3 1.9.0.4 1.9.1.1 1.9.1.2 1.9.1.3 1.9.1.4 1.9.1.5 1.9.1.6 1.9.2.1 1.9.2.2 1.9.2.3 1.9.3.1 1.9.3.2 1.9.4.1 1.9.4.2 1.9.5 1.9.5.1 1.9.5.2 1.9.6 1.9.6.1 1.9.6.2 1.9.7.1 1.9.7.2 1.9.7.3 1.9.8.1 1.9.8.2 1.9.8.4 1.9.8.7 1.9.9.2 1.9.9.3 1.9.9.4
wpforms-lite / includes / functions.php
wpforms-lite / includes Last commit date
admin 9 years ago emails 9 years ago fields 9 years ago providers 9 years ago templates 9 years ago class-fields.php 9 years ago class-form.php 9 years ago class-frontend.php 9 years ago class-install.php 9 years ago class-logging.php 9 years ago class-preview.php 9 years ago class-process.php 9 years ago class-providers.php 9 years ago class-smart-tags.php 9 years ago class-templates.php 9 years ago class-widget.php 10 years ago functions.php 9 years ago integrations.php 9 years ago
functions.php
1002 lines
1 <?php
2 /**
3 * Contains various functions that may be potentially used throughout
4 * the WPForms plugin.
5 *
6 * @package WPForms
7 * @author WPForms
8 * @since 1.0.0
9 * @license GPL-2.0+
10 * @copyright Copyright (c) 2016, WPForms LLC
11 */
12
13 /**
14 * Helper function to trigger displaying a form.
15 *
16 * @since 1.0.2
17 * @param int $form_id
18 * @param bool $title
19 * @param bool $desc
20 */
21 function wpforms_display( $form_id = false, $title = false, $desc = false ) {
22
23 wpforms()->frontend->output( $form_id, $title, $desc );
24 }
25
26 /**
27 * Performs json_decode and unslash.
28 *
29 * @since 1.0.0
30 * @param string $data
31 * @return array
32 */
33 function wpforms_decode( $data ) {
34
35 if ( ! $data || empty( $data ) ) {
36 return false;
37 }
38
39 return wp_unslash( json_decode( $data, true ) );
40 }
41
42 /**
43 * Performs json_encode and wp_slash.
44 *
45 * @since 1.3.1.3
46 * @param array $data
47 * @return string
48 */
49 function wpforms_encode( $data = false ) {
50
51 if( empty( $data ) )
52 return false;
53
54 return wp_slash( json_encode( $data ) );
55 }
56
57 /**
58 * Check if a string is a valid URL.
59 *
60 * @since 1.0.0
61 * @param $url
62 * @return bool
63 */
64 function wpforms_is_url( $url ) {
65
66 if ( preg_match( '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS', trim( $url ) ) ) {
67 return true;
68 } else {
69 return false;
70 }
71 }
72
73 /**
74 * Get current URL.
75 *
76 * @since 1.0.0
77 * @return string
78 */
79 function wpforms_current_url() {
80 $url = ( !empty( $_SERVER['HTTPS'] ) ) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
81 return esc_url_raw( $url );
82 }
83
84 /**
85 * Object to array.
86 *
87 * @since 1.1.7
88 * @param object $object
89 * @return array
90 */
91 function wpforms_object_to_array( $object ) {
92
93 if ( !is_object( $object ) && !is_array( $object ) ) {
94 return $object;
95 }
96
97 if ( is_object( $object ) ) {
98 $object = get_object_vars( $object );
99 }
100 return array_map( 'wpforms_object_to_array', $object );
101 }
102
103 /**
104 * Get the value of a specific WPForms setting.
105 *
106 * @since 1.0.0
107 * @return mixed
108 */
109 function wpforms_setting( $key, $default = false, $option = 'wpforms_settings' ) {
110
111 $options = get_option( $option, false );
112
113 $value = is_array( $options ) && ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
114
115 return $value;
116 }
117
118 /**
119 * Check if form provided contains the specified field type.
120 *
121 * @since 1.0.5
122 * @param string $type
123 * @param mixed $form
124 * @return bool
125 */
126 function wpforms_has_field_type( $type, $form, $multiple = false ) {
127
128 $form_data = '';
129 $field = false;
130 $type = (array) $type;
131
132 if ( $multiple ) {
133 foreach( $form as $single_form ) {
134 $field = wpforms_has_field_type( $type, $single_form );
135 if ( $field ) {
136 break;
137 }
138 }
139 return $field;
140 } else {
141
142 if ( is_object( $form ) && !empty( $form->post_content ) ) {
143 $form_data = wpforms_decode( $form->post_content );
144 } elseif ( is_array( $form ) ) {
145 $form_data = $form;
146 }
147
148 if ( empty( $form_data['fields'] ) )
149 return false;
150
151 foreach ( $form_data['fields'] as $single_field ) {
152 if ( in_array( $single_field['type'], $type ) ) {
153 $field = true;
154 break;
155 }
156 }
157
158 return $field;
159 }
160 }
161
162 /**
163 * Checks if form provided contains page breaks, if so give details.
164 *
165 * @since 1.0.0
166 * @return mixed
167 */
168 function wpforms_has_pagebreak( $form = false ) {
169
170 $form_data = '';
171 $pagebreak = false;
172 $pages = 1;
173
174 if ( is_object( $form ) && !empty( $form->post_content ) ) {
175 $form_data = wpforms_decode( $form->post_content );
176 } elseif ( is_array( $form ) ) {
177 $form_data = $form;
178 }
179
180 if ( empty( $form_data['fields'] ) )
181 return false;
182
183 $fields = $form_data['fields'];
184
185 foreach ( $fields as $field ) {
186 if ( $field['type'] == 'pagebreak' && empty( $field['position'] ) ) {
187 $pagebreak = true;
188 $pages++;
189 }
190 }
191
192 if ( $pagebreak ) {
193 return $pages;
194 } else {
195 return false;
196 }
197 }
198
199 /**
200 * Tries to find and return an top or bottom pagebreak.
201 *
202 * @since 1.2.1
203 * @param boolean $form
204 * @param string $type
205 * @return boolean
206 */
207 function wpforms_get_pagebreak( $form = false, $type = false ) {
208
209 $form_data = '';
210
211 if ( is_object( $form ) && !empty( $form->post_content ) ) {
212 $form_data = wpforms_decode( $form->post_content );
213 } elseif ( is_array( $form ) ) {
214 $form_data = $form;
215 }
216
217 if ( empty( $form_data['fields'] ) )
218 return false;
219
220 $fields = $form_data['fields'];
221 $pages = array();
222 foreach ( $fields as $field ) {
223 if ( $field['type'] == 'pagebreak' ) {
224 $position = !empty( $field['position'] ) ? $field['position'] : false;
225 if ( $type == 'pages' && $position != 'bottom' ) {
226 $pages[] = $field;
227 } elseif ( $position == $type ) {
228 return $field;
229 }
230 }
231 }
232
233 if ( !empty( $pages ) ) {
234 return $pages;
235 }
236 return false;
237 }
238
239 /**
240 * Sanitizes string of CSS classes.
241 *
242 * @since 1.2.1
243 * @param string $classes
244 * @return string
245 */
246 function wpforms_sanitize_classes( $classes ) {
247
248 $css = array();
249 if ( !empty( $classes ) ) {
250 $the_classes = explode( ' ', str_replace('.', '', $classes ) );
251 foreach( $the_classes as $class ) {
252 $css[] = sanitize_html_class( $class );
253 }
254 }
255 return implode( ' ', $css );
256 }
257
258 /**
259 * Convert a file size provided, such as "2M", to bytes.
260 *
261 * @since 1.0.0
262 * @link http://stackoverflow.com/a/22500394
263 * @param string $size
264 * @return int
265 */
266 function wpforms_size_to_bytes( $size ) {
267
268 if ( is_numeric( $size ) ) {
269 return $size;
270 }
271
272 $suffix = substr( $size, -1 );
273 $value = substr( $size, 0, -1 );
274
275 switch( strtoupper( $suffix ) ) {
276 case 'P':
277 $value *= 1024;
278 case 'T':
279 $value *= 1024;
280 case 'G':
281 $value *= 1024;
282 case 'M':
283 $value *= 1024;
284 case 'K':
285 $value *= 1024;
286 break;
287 }
288 return $value;
289 }
290
291 /**
292 * Convert bytes to megabytes (or in some cases KB).
293 *
294 * @since 1.0.0
295 * @param int $bytes
296 * @return string
297 */
298 function wpforms_size_to_megabytes( $bytes ) {
299
300 if ( $bytes < 1048676 ) {
301 return number_format( $bytes/1024, 1 ) . " KB";
302 } else {
303 return round( number_format( $bytes/1048576, 1 ) ) . " MB";
304 }
305 }
306
307 /**
308 * Convert a file size provided, such as "2M", to bytes.
309 *
310 * @since 1.0.0
311 * @link http://stackoverflow.com/a/22500394
312 * @param string $size
313 * @return int
314 */
315 function wpforms_max_upload( $bytes = false ) {
316
317 $max = wp_max_upload_size();
318 if ( $bytes ) {
319 return $max;
320 } else {
321 return wpforms_size_to_megabytes( $max );
322 }
323 }
324
325 /**
326 * Retrieve actual fields from a form.
327 *
328 * Non-posting elements such as section divider, page break, and HTML are
329 * automatically excluded. Optionally a white list can be provided.
330 *
331 * @since 1.0.0
332 * @param mixed $form
333 * @return mixed boolean or array
334 */
335 function wpforms_get_form_fields( $form = false, $whitelist = array() ) {
336
337 // Accept form (post) object or form ID
338 if ( is_object( $form ) ) {
339 $form = wpforms_decode( $form->post_content );
340 } elseif ( is_numeric( $form ) ) {
341 $form = wpforms()->form->get( $form, array( 'content_only' => true ) );
342 }
343
344 if ( !is_array( $form ) || empty( $form['fields'] ) )
345 return false;
346
347 // White list of field types to allow
348 $allowed_form_fields = array(
349 'text',
350 'textarea',
351 'select',
352 'radio',
353 'checkbox',
354 'email',
355 'address',
356 'url',
357 'name',
358 'hidden',
359 'date-time',
360 'phone',
361 'number',
362 'file-upload',
363 'payment-single',
364 'payment-multiple',
365 'payment-select',
366 'payment-total',
367 );
368 $allowed_form_fields = apply_filters( 'wpforms_get_form_fields_allowed', $allowed_form_fields );
369
370 $whitelist = !empty( $whitelist ) ? $whitelist : $allowed_form_fields;
371
372 $form_fields = $form['fields'];
373
374 foreach( $form_fields as $id => $form_field ) {
375 if ( !in_array( $form_field['type'], $whitelist ) ) {
376 unset( $form_fields[$id] );
377 }
378 }
379
380 return $form_fields;
381 }
382
383 /**
384 * Get meta key value for a form field.
385 *
386 * @since 1.1.9
387 * @param int $id Field ID
388 * @param string $key Meta key
389 * @param array $form_data Form data array
390 * @return string
391 */
392 function wpforms_get_form_field_meta( $id = '', $key = '', $form_data = '' ) {
393
394 if ( empty( $id ) || empty( $key ) || empty( $form_data ) ) {
395 return '';
396 }
397
398 if ( !empty( $form_data['fields'][$id]['meta'][$key] ) ) {
399 return $form_data['fields'][$id]['meta'][$key];
400 } else {
401 return '';
402 }
403 }
404
405 /**
406 * Get meta key value for a form field.
407 *
408 * @since 1.3.1
409 * @param string $key Meta key
410 * @param array $form_data Form data array
411 * @return string
412 */
413 function wpforms_get_form_fields_by_meta( $key = '', $value = '', $form_data = '' ) {
414
415 if ( empty( $key ) || empty( $value ) || empty( $form_data['fields'] ) ) {
416 return false;
417 }
418
419 $found = array();
420
421 foreach( $form_data['fields'] as $id => $field ) {
422
423 if ( !empty( $field['meta'][$key] ) && $value == $field['meta'][$key] ) {
424 $found[$id] = $field;
425 }
426 }
427
428 if ( !empty( $found ) ) {
429 return $found;
430 } else {
431 return false;
432 }
433 }
434
435 /**
436 * US States
437 *
438 * @since 1.0.0
439 * @return array
440 */
441 function wpforms_us_states() {
442
443 $states = array(
444 'AL' => 'Alabama',
445 'AK' => 'Alaska',
446 'AZ' => 'Arizona',
447 'AR' => 'Arkansas',
448 'CA' => 'California',
449 'CO' => 'Colorado',
450 'CT' => 'Connecticut',
451 'DE' => 'Delaware',
452 'DC' => 'District of Columbia',
453 'FL' => 'Florida',
454 'GA' => 'Georgia',
455 'HI' => 'Hawaii',
456 'ID' => 'Idaho',
457 'IL' => 'Illinois',
458 'IN' => 'Indiana',
459 'IA' => 'Iowa',
460 'KS' => 'Kansas',
461 'KY' => 'Kentucky',
462 'LA' => 'Louisiana',
463 'ME' => 'Maine',
464 'MD' => 'Maryland',
465 'MA' => 'Massachusetts',
466 'MI' => 'Michigan',
467 'MN' => 'Minnesota',
468 'MS' => 'Mississippi',
469 'MO' => 'Missouri',
470 'MT' => 'Montana',
471 'NE' => 'Nebraska',
472 'NV' => 'Nevada',
473 'NH' => 'New Hampshire',
474 'NJ' => 'New Jersey',
475 'NM' => 'New Mexico',
476 'NY' => 'New York',
477 'NC' => 'North Carolina',
478 'ND' => 'North Dakota',
479 'OH' => 'Ohio',
480 'OK' => 'Oklahoma',
481 'OR' => 'Oregon',
482 'PA' => 'Pennsylvania',
483 'RI' => 'Rhode Island',
484 'SC' => 'South Carolina',
485 'SD' => 'South Dakota',
486 'TN' => 'Tennessee',
487 'TX' => 'Texas',
488 'UT' => 'Utah',
489 'VT' => 'Vermont',
490 'VA' => 'Virginia',
491 'WA' => 'Washington',
492 'WV' => 'West Virginia',
493 'WI' => 'Wisconsin',
494 'WY' => 'Wyoming',
495 );
496 return apply_filters( 'wpforms_us_states', $states );
497 }
498
499 function wpforms_countries() {
500
501 $countries = array(
502 'AF' => 'Afghanistan',
503 'AX' => 'Aland Islands',
504 'AL' => 'Albania',
505 'DZ' => 'Algeria',
506 'AS' => 'American Samoa',
507 'AD' => 'Andorra',
508 'AO' => 'Angola',
509 'AI' => 'Anguilla',
510 'AQ' => 'Antarctica',
511 'AG' => 'Antigua and Barbuda',
512 'AR' => 'Argentina',
513 'AM' => 'Armenia',
514 'AW' => 'Aruba',
515 'AC' => 'Ascension Island',
516 'AU' => 'Australia',
517 'AT' => 'Austria',
518 'AZ' => 'Azerbaijan)',
519 'BS' => 'Bahamas',
520 'BH' => 'Bahrain',
521 'BD' => 'Bangladesh',
522 'BB' => 'Barbados',
523 'BY' => 'Belarus',
524 'BE' => 'Belgium',
525 'BZ' => 'Belize',
526 'BJ' => 'Benin',
527 'BM' => 'Bermuda',
528 'BT' => 'Bhutan',
529 'BO' => 'Bolivia',
530 'BA' => 'Bosnia and Herzegovina',
531 'BW' => 'Botswana',
532 'BV' => 'Bouvet Island',
533 'BR' => 'Brazil',
534 'IO' => 'British Indian Ocean Territory',
535 'VG' => 'British Virgin Islands',
536 'BN' => 'Brunei',
537 'BG' => 'Bulgaria',
538 'BF' => 'Burkina Faso',
539 'BI' => 'Burundi',
540 'KH' => 'Cambodia',
541 'CM' => 'Cameroon',
542 'CA' => 'Canada',
543 'IC' => 'Canary Islands',
544 'CV' => 'Cape Verde',
545 'BQ' => 'Caribbean Netherlands',
546 'KY' => 'Cayman Islands',
547 'CF' => 'Central African Republic',
548 'EA' => 'Ceuta and Melilla',
549 'TD' => 'Chad ',
550 'CL' => 'Chile',
551 'CN' => 'China',
552 'CX' => 'Christmas Island',
553 'CP' => 'Clipperton Island',
554 'CC' => 'Cocos (Keeling) Islands',
555 'CO' => 'Colombia',
556 'KM' => 'Comoros',
557 'CD' => 'Congo (DRC)',
558 'CG' => 'Congo (Republic)',
559 'CK' => 'Cook Islands',
560 'CR' => 'Costa Rica',
561 'CI' => 'Côte d’Ivoire',
562 'HR' => 'Croatia',
563 'CU' => 'Cuba',
564 'CW' => 'Curaçao',
565 'CY' => 'Cyprus',
566 'CZ' => 'Czech Republic',
567 'DK' => 'Denmark (Danmark)',
568 'DG' => 'Diego Garcia',
569 'DJ' => 'Djibouti',
570 'DM' => 'Dominica',
571 'DO' => 'Dominican Republic',
572 'EC' => 'Ecuador',
573 'EG' => 'Egypt',
574 'SV' => 'El Salvador',
575 'GQ' => 'Equatorial Guinea ',
576 'ER' => 'Eritrea',
577 'EE' => 'Estonia',
578 'ET' => 'Ethiopia',
579 'FK' => 'Falkland Islands',
580 'FO' => 'Faroe Islands',
581 'FJ' => 'Fiji',
582 'FI' => 'Finland',
583 'FR' => 'France',
584 'GF' => 'French Guiana',
585 'PF' => 'French Polynesia',
586 'TF' => 'French Southern Territories',
587 'GA' => 'Gabon',
588 'GM' => 'Gambia',
589 'GE' => 'Georgia',
590 'DE' => 'Germany',
591 'GH' => 'Ghana',
592 'GI' => 'Gibraltar',
593 'GR' => 'Greece',
594 'GL' => 'Greenland',
595 'GD' => 'Grenada',
596 'GP' => 'Guadeloupe',
597 'GU' => 'Guam',
598 'GT' => 'Guatemala',
599 'GG' => 'Guernsey',
600 'GN' => 'Guinea',
601 'GW' => 'Guinea-Bissau',
602 'GY' => 'Guyana',
603 'HT' => 'Haiti',
604 'HM' => 'Heard & McDonald Islands',
605 'HN' => 'Honduras',
606 'HK' => 'Hong Kong)',
607 'HU' => 'Hungary',
608 'IS' => 'Iceland',
609 'IN' => 'India',
610 'ID' => 'Indonesia',
611 'IR' => 'Iran',
612 'IQ' => 'Iraq',
613 'IE' => 'Ireland',
614 'IM' => 'Isle of Man',
615 'IL' => 'Israel',
616 'IT' => 'Italy',
617 'JM' => 'Jamaica',
618 'JP' => 'Japan',
619 'JE' => 'Jersey',
620 'JO' => 'Jordan',
621 'KZ' => 'Kazakhstan)',
622 'KE' => 'Kenya',
623 'KI' => 'Kiribati',
624 'XK' => 'Kosovo',
625 'KW' => 'Kuwait',
626 'KG' => 'Kyrgyzstan',
627 'LA' => 'Laos',
628 'LV' => 'Latvia',
629 'LB' => 'Lebanon',
630 'LS' => 'Lesotho',
631 'LR' => 'Liberia',
632 'LY' => 'Libya',
633 'LI' => 'Liechtenstein',
634 'LT' => 'Lithuania',
635 'LU' => 'Luxembourg',
636 'MO' => 'Macau',
637 'MK' => 'Macedonia (FYROM)',
638 'MG' => 'Madagascar',
639 'MW' => 'Malawi',
640 'MY' => 'Malaysia',
641 'MV' => 'Maldives',
642 'ML' => 'Mali',
643 'MT' => 'Malta',
644 'MH' => 'Marshall Islands',
645 'MQ' => 'Martinique',
646 'MR' => 'Mauritania',
647 'MU' => 'Mauritius)',
648 'YT' => 'Mayotte',
649 'MX' => 'Mexico',
650 'FM' => 'Micronesia',
651 'MD' => 'Moldova',
652 'MC' => 'Monaco',
653 'MN' => 'Mongolia',
654 'ME' => 'Montenegro',
655 'MS' => 'Montserrat',
656 'MA' => 'Morocco',
657 'MZ' => 'Mozambique',
658 'MM' => 'Myanmar',
659 'NA' => 'Namibia',
660 'NR' => 'Nauru',
661 'NP' => 'Nepal',
662 'NL' => 'Netherlands',
663 'NC' => 'New Caledonia',
664 'NZ' => 'New Zealand',
665 'NI' => 'Nicaragua',
666 'NE' => 'Niger',
667 'NG' => 'Nigeria',
668 'NU' => 'Niue',
669 'NF' => 'Norfolk Island',
670 'MP' => 'Northern Mariana Islands',
671 'KP' => 'North Korea',
672 'NO' => 'Norway',
673 'OM' => 'Oman',
674 'PK' => 'Pakistan',
675 'PW' => 'Palau',
676 'PS' => 'Palestine',
677 'PA' => 'Panama',
678 'PG' => 'Papua New Guinea',
679 'PY' => 'Paraguay',
680 'PE' => 'Peru',
681 'PH' => 'Philippines',
682 'PN' => 'Pitcairn Islands',
683 'PL' => 'Poland',
684 'PT' => 'Portugal',
685 'PR' => 'Puerto Rico',
686 'QA' => 'Qatar',
687 'RE' => 'Réunion',
688 'RO' => 'Romania',
689 'RU' => 'Russia',
690 'RW' => 'Rwanda',
691 'BL' => 'Saint Barthélemy',
692 'SH' => 'Saint Helena',
693 'KN' => 'Saint Kitts and Nevis',
694 'LC' => 'Saint Lucia',
695 'MF' => 'Saint Martin ',
696 'PM' => 'Saint Pierre and Miquelon',
697 'WS' => 'Samoa',
698 'SM' => 'San Marino',
699 'ST' => 'São Tomé and Príncipe',
700 'SA' => 'Saudi Arabia',
701 'SN' => 'Senegal',
702 'RS' => 'Serbia',
703 'SC' => 'Seychelles',
704 'SL' => 'Sierra Leone',
705 'SG' => 'Singapore',
706 'SX' => 'Sint Maarten',
707 'SK' => 'Slovakia',
708 'SI' => 'Slovenia',
709 'SB' => 'Solomon Islands',
710 'SO' => 'Somalia',
711 'ZA' => 'South Africa',
712 'GS' => 'South Georgia & South Sandwich Islands',
713 'KR' => 'South Korea',
714 'SS' => 'South Sudan',
715 'ES' => 'Spain',
716 'LK' => 'Sri Lanka',
717 'VC' => 'St. Vincent & Grenadines',
718 'SD' => 'Sudan',
719 'SR' => 'Suriname',
720 'SJ' => 'Svalbard and Jan Mayen',
721 'SZ' => 'Swaziland',
722 'SE' => 'Sweden',
723 'CH' => 'Switzerland',
724 'SY' => 'Syria',
725 'TW' => 'Taiwan',
726 'TJ' => 'Tajikistan',
727 'TZ' => 'Tanzania',
728 'TH' => 'Thailand',
729 'TL' => 'Timor-Leste',
730 'TG' => 'Togo',
731 'TK' => 'Tokelau',
732 'TO' => 'Tonga',
733 'TT' => 'Trinidad and Tobago',
734 'TA' => 'Tristan da Cunha',
735 'TN' => 'Tunisia',
736 'TR' => 'Turkey',
737 'TM' => 'Turkmenistan',
738 'TC' => 'Turks and Caicos Islands',
739 'TV' => 'Tuvalu',
740 'UM' => 'U.S. Outlying Islands',
741 'VI' => 'U.S. Virgin Islands',
742 'UG' => 'Uganda',
743 'UA' => 'Ukraine',
744 'AE' => 'United Arab Emirates',
745 'GB' => 'United Kingdom',
746 'US' => 'United States',
747 'UY' => 'Uruguay',
748 'UZ' => 'Uzbekistan',
749 'VU' => 'Vanuatu',
750 'VA' => 'Vatican City',
751 'VE' => 'Venezuela',
752 'VN' => 'Vietnam',
753 'WF' => 'Wallis and Futuna',
754 'EH' => 'Western Sahara',
755 'YE' => 'Yemen',
756 'ZM' => 'Zambia',
757 'ZW' => 'Zimbabwe',
758 );
759 return apply_filters( 'wpforms_countries', $countries );
760 }
761
762 /**
763 * Lookup user IP.
764 *
765 * There are many ways to do this, but we prefer the way EDD does it.
766 * https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/misc-functions.php#L163
767 *
768 * @since 1.2.5
769 * @return string
770 */
771 function wpforms_get_ip() {
772
773 $ip = '127.0.0.1';
774
775 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
776 $ip = $_SERVER['HTTP_CLIENT_IP'];
777 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
778 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
779 } elseif( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
780 $ip = $_SERVER['REMOTE_ADDR'];
781 }
782 // Fix potential CSV returned from $_SERVER variables
783 $ip_array = array_map( 'trim', explode( ',', $ip ) );
784 return $ip_array[0];
785 }
786
787 /**
788 * Sanitizes hex color.
789 *
790 * @since 1.2.1
791 * @param string $color
792 * @return string
793 */
794 function wpforms_sanitize_hex_color( $color ) {
795 if ( '' === $color ) {
796 return '';
797 }
798
799 // 3 or 6 hex digits, or the empty string.
800 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
801 return $color;
802 }
803 }
804
805
806 /**
807 * Detect if we should use a light or dark color based on the color given.
808 *
809 * @since 1.2.5
810 * @link https://docs.woocommerce.com/wc-apidocs/source-function-wc_light_or_dark.html#608-627
811 * @param mixed $color
812 * @param string $dark (default: '#000000')
813 * @param string $light (default: '#FFFFFF')
814 * @return string
815 */
816 function wpforms_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
817
818 $hex = str_replace( '#', '', $color );
819
820 $c_r = hexdec( substr( $hex, 0, 2 ) );
821 $c_g = hexdec( substr( $hex, 2, 2 ) );
822 $c_b = hexdec( substr( $hex, 4, 2 ) );
823
824 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
825
826 return $brightness > 155 ? $dark : $light;
827 }
828
829 /**
830 * Debug mode bool.
831 *
832 * @since 1.2.3
833 */
834 function wpforms_debug() {
835
836 $debug = false;
837
838 if ( ( defined( 'WPFORMS_DEBUG' ) && true === WPFORMS_DEBUG ) && is_super_admin() ) {
839 $debug = true;
840 }
841
842 $debug_option = get_option( 'wpforms_debug' );
843
844 if ( $debug_option ) {
845 $current_user = wp_get_current_user();
846 if ( $current_user->user_login == $debug_option ) {
847 $debug = true;
848 }
849 }
850
851 return apply_filters( 'wpforms_debug', $debug );
852 }
853
854 /**
855 * Helper function to display debug data.
856 *
857 * @since 1.0.0
858 * @param string $data
859 * @return string
860 */
861 function wpforms_debug_data( $data, $echo = true ) {
862
863 if ( wpforms_debug() ) {
864
865 $output = '<textarea style="background:#fff;margin: 20px 0;width:100%;height:500px;font-size:12px;font-family: Consolas,Monaco,monospace;direction: ltr;unicode-bidi: embed;line-height: 1.4;padding: 4px 6px 1px;" readonly>';
866
867 $output .= "=================== WPFORMS DEBUG ===================\n\n";
868
869 if ( is_array( $data ) || is_object( $data ) ) {
870 $output .= ( print_r( $data, true ) );
871 } else {
872 $output .= $data;
873 }
874
875 $output .= '</textarea>';
876
877 if ( $echo ) {
878 echo $output;
879 } else {
880 return $output;
881 }
882 }
883 }
884
885 /**
886 * Log helper.
887 *
888 * @since 1.0.0
889 * @param string $title
890 * @param string $message
891 * @param array $args
892 */
893 function wpforms_log( $title = '', $message = '', $args = array() ) {
894
895 // Require log title
896 if ( empty( $title ) )
897 return;
898
899 // Force logging everything when in debug mode
900 if ( ! wpforms_debug() ) {
901
902 /**
903 * Compare error levels to determine if we should log.
904 * Current supported levels:
905 * - Errors (error)
906 * - Spam (spam)
907 * - Entries (entry)
908 * - Payments (payment)
909 * - Providers (provider)
910 * - Conditional Logic (conditional_logic)
911 */
912 $type = !empty( $args['type'] ) ? (array) $args['type'] : array( 'error' );
913 $levels = get_option( 'wpforms_logging', array() );
914 $lvls = array_intersect( $type, $levels );
915 if ( empty( $lvls ) ) {
916 return;
917 }
918 }
919
920 // Meta
921 if ( !empty( $args['form_id'] ) ) {
922 $meta = array( 'form' => absint( $args['form_id'] ) );
923 } elseif ( !empty( $args['meta'] ) ) {
924 $meta = $args['meta'];
925 } else {
926 $meta = '';
927 }
928
929 // Parent
930 $parent = !empty( $args['parent'] ) ? $args['parent'] : 0;
931
932 // Make arrays and objects look nice
933 if ( is_array( $message ) || is_object( $message ) ) {
934 $message = '<pre>' . print_r( $message, true ) . '</pre>';
935 }
936
937 // Create log entry
938 wpforms()->logs->add( $title, $message, $parent, $parent, $meta );
939 }
940
941 /**
942 * Insert element into an array at a specific point a preserve the key.
943 *
944 * @since 1.3.3
945 * @param array $array
946 * @param array $values
947 * @param int $offset
948 * @return array
949 */
950 function wpforms_array_insert( $array, $values, $offset ) {
951
952 return array_slice( $array, 0, $offset, true ) + $values + array_slice( $array, $offset, NULL, true );
953 }
954
955 /**
956 * Array replace recursive, for PHP 5.2.
957 *
958 */
959 if ( ! function_exists( 'array_replace_recursive' ) ) :
960 /**
961 * PHP-agnostic version of {@link array_replace_recursive()}.
962 *
963 * The array_replace_recursive() function is a PHP 5.3 function. WordPress
964 * currently supports down to PHP 5.2, so this method is a workaround
965 * for PHP 5.2.
966 *
967 * Note: array_replace_recursive() supports infinite arguments, but for our use-
968 * case, we only need to support two arguments.
969 *
970 * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement.
971 *
972 * @since 1.2.3
973 * @see http://php.net/manual/en/function.array-replace-recursive.php#109390
974 * @param array $base Array with keys needing to be replaced.
975 * @param array $replacements Array with the replaced keys.
976 * @return array
977 */
978 function array_replace_recursive( $base = array(), $replacements = array() ) {
979 // PHP 5.2-compatible version
980 // http://php.net/manual/en/function.array-replace-recursive.php#109390.
981 foreach ( array_slice( func_get_args(), 1 ) as $replacements ) {
982 $bref_stack = array( &$base );
983 $head_stack = array( $replacements );
984 do {
985 end( $bref_stack );
986 $bref = &$bref_stack[ key( $bref_stack ) ];
987 $head = array_pop( $head_stack );
988 unset( $bref_stack[ key( $bref_stack ) ] );
989 foreach ( array_keys( $head ) as $key ) {
990 if ( isset( $key, $bref ) && is_array( $bref[ $key ] ) && is_array( $head[ $key ] ) ) {
991 $bref_stack[] = &$bref[ $key ];
992 $head_stack[] = $head[ $key ];
993 } else {
994 $bref[ $key ] = $head[ $key ];
995 }
996 }
997 } while ( count( $head_stack ) );
998 }
999 return $base;
1000 }
1001 endif;
1002