PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.5
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.5
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / functions.php

functions.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.2.5, at includes/functions.php

1,118 lines 30.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Get a single form
5 *
6 * @since 1.0.3
7 *
8 * @param int $form_id
9 *
10 * @return false|WP_Post
11 */
12 function weform_get_form( $form_id ) {
13 return weforms()->form->get( $form_id );
14 }
15
16 /**
17 * Get contact form templates
18 *
19 * @return array
20 */
21 function weforms_get_form_template_categories() {
22
23 $categories = array(
24 'default' => array(
25 'name' => 'Default Templates',
26 'icon' => 'fa fa-bars',
27 ),
28 'registration' => array(
29 'name' => 'Registration Templates',
30 'icon' => 'fa fa-user-plus',
31 ),
32 'application' => array(
33 'name' => 'Application Templates',
34 'icon' => 'fa fa-address-card-o',
35 ),
36 'request' => array(
37 'name' => 'Request Templates',
38 'icon' => 'fa fa-hand-paper-o',
39 ),
40 'event' => array(
41 'name' => 'Event Templates',
42 'icon' => 'fa fa-calendar',
43 ),
44 'feedback' => array(
45 'name' => 'Feedback Templates',
46 'icon' => 'fa fa-comments',
47 ),
48 'employment' => array(
49 'name' => 'Employment Templates',
50 'icon' => 'fa fa-suitcase',
51 ),
52 'payment' => array(
53 'name' => 'Payment Templates',
54 'icon' => 'fa fa-money',
55 ),
56 'reservation' => array(
57 'name' => 'Reservation Templates',
58 'icon' => 'fa fa-bandcamp',
59 ),
60 'others' => array(
61 'name' => 'Others Templates',
62 'icon' => 'fa fa-info-circle',
63 ),
64 );
65
66 return apply_filters( 'weforms_form_template_categories', $categories );
67 }
68
69 /**
70 * Get entries by a form_id
71 *
72 * @param int $form_id
73 * @param array $args
74 *
75 * @return Object
76 */
77 function weforms_get_form_entries( $form_id, $args = array() ) {
78 global $wpdb;
79
80 $defaults = array(
81 'number' => 10,
82 'offset' => 0,
83 'orderby' => 'created_at',
84 'status' => 'publish',
85 'order' => 'DESC',
86 );
87
88 $r = wp_parse_args( $args, $defaults );
89
90 $query = 'SELECT id, form_id, user_id, INET_NTOA( user_ip ) as ip_address, created_at
91 FROM ' . $wpdb->weforms_entries .
92 ' WHERE form_id = ' . $form_id . ' AND status = \'' . $r['status'] . '\'' .
93 ' ORDER BY ' . $r['orderby'] . ' ' . $r['order'];
94
95 if ( ! empty( $r['offset'] ) && ! empty( $r['number'] ) ) {
96 $query .= ' LIMIT ' . $r['offset'] . ', ' . $r['number'];
97 }
98
99 $results = $wpdb->get_results( $query );
100
101 return $results;
102 }
103
104 /**
105 * Get payments by a form_id
106 *
107 * @param int $form_id
108 * @param array $args
109 *
110 * @return Object
111 */
112 function weforms_get_form_payments( $form_id, $args = array() ) {
113 global $wpdb;
114
115 $defaults = array(
116 'number' => 10,
117 'offset' => 0,
118 'orderby' => 'created_at',
119 'order' => 'DESC',
120 );
121
122 $r = wp_parse_args( $args, $defaults );
123
124 $query = 'SELECT * FROM ' . $wpdb->prefix . 'weforms_payments' .
125 ' WHERE form_id = ' . $form_id .
126 ' ORDER BY ' . $r['orderby'] . ' ' . $r['order'] .
127 ' LIMIT ' . $r['offset'] . ', ' . $r['number'];
128
129 $results = $wpdb->get_results( $query );
130
131 return $results;
132 }
133
134 /**
135 * Get an entry by id
136 *
137 * @param int $entry_id
138 *
139 * @return Object
140 */
141 function weforms_get_entry( $entry_id ) {
142 global $wpdb;
143
144 $cache_key = 'weforms-entry-' . $entry_id;
145 $entry = wp_cache_get( $cache_key, 'weforms' );
146
147 if ( false === $entry ) {
148 $query = 'SELECT id, form_id, user_id, user_device, referer, INET_NTOA( user_ip ) as ip_address, created_at
149 FROM ' . $wpdb->weforms_entries . '
150 WHERE id = %d';
151
152 $entry = $wpdb->get_row( $wpdb->prepare( $query, $entry_id ) );
153 wp_cache_set( $cache_key, $entry );
154 }
155
156 return $entry;
157 }
158
159 /**
160 * Insert a new entry
161 *
162 * @param array $args
163 * @param array $fields
164 *
165 * @return WP_Error|integer
166 */
167 function weforms_insert_entry( $args, $fields = array() ) {
168 global $wpdb;
169
170 $browser = weforms_get_browser();
171
172 $defaults = array(
173 'form_id' => 0,
174 'user_id' => get_current_user_id(),
175 'user_ip' => ip2long( weforms_get_client_ip() ),
176 'user_device' => $browser['name'] . '/' . $browser['platform'],
177 'referer' => $_SERVER['HTTP_REFERER'],
178 'created_at' => current_time( 'mysql' )
179 );
180
181 $r = wp_parse_args( $args, $defaults );
182
183 if ( ! $r['form_id'] ) {
184 return new WP_Error( 'no-form-id', __( 'No form ID was found.', 'weforms' ) );
185 }
186
187 if ( ! $fields ) {
188 return new WP_Error( 'no-fields', __( 'No form fields were found.', 'weforms' ) );
189 }
190
191 $success = $wpdb->insert( $wpdb->weforms_entries, $r );
192
193 if ( is_wp_error( $success ) || ! $success ) {
194 return new WP_Error( 'could-not-create', __( 'Could not create an entry', 'weforms' ) );
195 }
196
197 $entry_id = $wpdb->insert_id;
198
199 foreach ( $fields as $key => $value ) {
200 weforms_add_entry_meta( $entry_id, $key, $value );
201 }
202
203 return $entry_id;
204 }
205
206 /**
207 * Change an entry status
208 *
209 * @param int $entry_id
210 * @param string $status
211 *
212 * @return int|boolean
213 */
214 function weforms_change_entry_status( $entry_id, $status ) {
215 global $wpdb;
216
217 return $wpdb->update(
218 $wpdb->weforms_entries,
219 array(
220 'status' => $status
221 ),
222 array(
223 'id' => $entry_id
224 ),
225 array( '%s' ),
226 array( '%d' )
227 );
228 }
229
230 /**
231 * Delete an entry
232 *
233 * @param int $entry_id
234 *
235 * @return int|boolean
236 */
237 function weforms_delete_entry( $entry_id ) {
238 global $wpdb;
239
240 return $wpdb->delete(
241 $wpdb->weforms_entries, array(
242 'id' => $entry_id
243 ), array( '%d' )
244 );
245 }
246
247 /**
248 * Add meta data field to an entry.
249 *
250 * @param int $entry_id Entry ID.
251 * @param string $meta_key Metadata name.
252 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
253 * @param bool $unique Optional. Whether the same key should not be added.
254 * Default false.
255 * @return int|false Meta ID on success, false on failure.
256 */
257 function weforms_add_entry_meta( $entry_id, $meta_key, $meta_value, $unique = false ) {
258 return add_metadata( 'weforms_entry', $entry_id, $meta_key, $meta_value, $unique );
259 }
260
261 /**
262 * Retrieve entry meta field for a entry.
263 *
264 * @param int $entry_id Entry ID.
265 * @param string $key Optional. The meta key to retrieve. By default, returns
266 * data for all keys. Default empty.
267 * @param bool $single Optional. Whether to return a single value. Default false.
268 * @return mixed Will be an array if $single is false. Will be value of meta data
269 * field if $single is true.
270 */
271 function weforms_get_entry_meta( $entry_id, $key = '', $single = false ) {
272 return get_metadata( 'weforms_entry', $entry_id, $key, $single );
273 }
274
275 /**
276 * Update entry meta field based on entry ID.
277 *
278 * Use the $prev_value parameter to differentiate between meta fields with the
279 * same key and entry ID.
280 *
281 * If the meta field for the entry does not exist, it will be added.
282 *
283 * @param int $entry_id entry ID.
284 * @param string $meta_key Metadata key.
285 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
286 * @param mixed $prev_value Optional. Previous value to check before removing.
287 * Default empty.
288 * @return int|bool Meta ID if the key didn't exist, true on successful update,
289 * false on failure.
290 */
291 function weforms_update_entry_meta( $entry_id, $meta_key, $meta_value, $prev_value = '' ) {
292 return update_metadata( 'weforms_entry', $entry_id, $meta_key, $meta_value, $prev_value );
293 }
294
295 /**
296 * Delete everything from entry meta matching meta key.
297 *
298 * @param string $entry_meta_key Key to search for when deleting.
299 * @return bool Whether the entry meta key was deleted from the database.
300 */
301 function weforms_delete_entry_meta_by_key( $meta_key ) {
302 return delete_metadata( 'weforms_entry', null, $meta_key, '', true );
303 }
304
305 /**
306 * Retrieve entry meta fields, based on entry ID.
307 *
308 * The entry meta fields are retrieved from the cache where possible,
309 * so the function is optimized to be called more than once.
310 *
311 * @since 1.2.0
312 *
313 * @param int $entry_id Optional.
314 * @return array entry meta for the given entry.
315 */
316 function weforms_get_entry_custom( $entry_id = 0 ) {
317 $entry_id = absint( $entry_id );
318
319 return weforms_get_entry_meta( $entry_id );
320 }
321
322 /**
323 * Retrieve meta field names for a entry.
324 *
325 * If there are no meta fields, then nothing (null) will be returned.
326 *
327 * @param int $entry_id Optional.
328 * @return array|void Array of the keys, if retrieved.
329 */
330 function weforms_get_entry_custom_keys( $entry_id = 0 ) {
331 $custom = weforms_get_entry_custom( $entry_id );
332
333 if ( ! is_array( $custom ) ) {
334 return false;
335 }
336
337 if ( $keys = array_keys( $custom ) ) {
338 return $keys;
339 }
340 }
341
342 /**
343 * Get the number of entries count on a form
344 *
345 * @param int $form_id
346 *
347 * @return int
348 */
349 function weforms_count_form_entries( $form_id, $status = 'publish' ) {
350 global $wpdb;
351
352 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count(id) FROM ' . $wpdb->weforms_entries . ' WHERE form_id = %d AND status = %s', $form_id, $status ) );
353 }
354
355 /**
356 * Get the number of entries count on a form
357 *
358 * @param int $form_id
359 *
360 * @return int
361 */
362 function weforms_count_form_payments( $form_id ) {
363 global $wpdb;
364
365 if ( ! class_exists( 'WeForms_Payment' ) ) {
366 return 0;
367 }
368
369 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count(id) FROM ' . $wpdb->prefix . 'weforms_payments' . ' WHERE form_id = %d', $form_id ) );
370 }
371
372 /**
373 * Get table column heads for a form
374 *
375 * For now, return only text type fields
376 *
377 * @param int $form_id
378 *
379 * @return array
380 */
381 function weforms_get_entry_columns( $form_id, $limit = 6 ) {
382 $fields = weforms()->form->get( $form_id )->get_fields();
383 $columns = array();
384
385 // filter by input types
386 if ( $limit ) {
387
388 $fields = array_filter( $fields, function( $item ) {
389 return in_array( $item['template'], array( 'text_field', 'name_field', 'dropdown_field', 'radio_field', 'email_address', 'url_field' ) );
390 });
391 }
392
393 if ( $fields ) {
394 foreach ( $fields as $field ) {
395 $columns[ $field['name'] ] = $field['label'];
396 }
397 }
398
399 // if passed 0/false, return all collumns
400 if ( $limit && sizeof( $columns ) > $limit ) {
401 $columns = array_slice( $columns, 0, $limit ); // max 6 columns
402 }
403
404 return apply_filters( 'weforms_get_entry_columns', $columns, $form_id );
405 }
406
407 /**
408 * Get table column heads for a form
409 *
410 * For now, return only text type fields
411 *
412 * @param int $form_id
413 *
414 * @return array
415 */
416 function weforms_get_payment_columns( $form_id, $limit = 6 ) {
417 $fields = weforms()->form->get( $form_id )->get_fields();
418 $columns = array();
419
420 // filter by input types
421 if ( $limit ) {
422
423 $fields = array_filter( $fields, function( $item ) {
424 return in_array( $item['template'], array( 'text_field', 'name_field', 'dropdown_field', 'radio_field', 'email_address', 'url_field' ) );
425 });
426 }
427
428 if ( $fields ) {
429 foreach ( $fields as $field ) {
430 $columns[ $field['name'] ] = $field['label'];
431 }
432 }
433
434 // if passed 0/false, return all collumns
435 if ( $limit && sizeof( $columns ) > $limit ) {
436 $columns = array_slice( $columns, 0, $limit ); // max 6 columns
437 }
438
439 return apply_filters( 'weforms_get_entry_columns', $columns, $form_id );
440 }
441
442 /**
443 * Get fields from a form by it's meta key
444 *
445 * @param int $form_id
446 *
447 * @return array
448 */
449 function weforms_get_form_field_labels( $form_id ) {
450 $fields = weforms()->form->get( $form_id )->get_fields();
451 $exclude = array( 'step_start', 'section_break', 'recaptcha', 'shortcode', 'action_hook' );
452
453 if ( ! $fields ) {
454 return false;
455 }
456
457 $data = array();
458 foreach ( $fields as $field ) {
459 if ( empty( $field['name'] ) ) {
460 continue;
461 }
462
463 // exclude the fields
464 if ( in_array( $field['template'], $exclude ) ) {
465 continue;
466 }
467
468 $data[ $field['name'] ] = array(
469 'label' => $field['label'],
470 'type' => $field['template']
471 );
472 }
473
474 return $data;
475 }
476
477 /**
478 * Get data from an entry
479 *
480 * @param int $entry_id
481 * @param int $form_id
482 *
483 * @return false|array
484 */
485 function weforms_get_entry_data( $entry_id ) {
486 $data = array();
487 $entry = weforms_get_entry( $entry_id );
488 $fields = weforms_get_form_field_labels( $entry->form_id );
489
490 if ( ! $fields ) {
491 return false;
492 }
493
494 foreach ( $fields as $meta_key => $field ) {
495 $value = weforms_get_entry_meta( $entry_id, $meta_key, true );
496
497 if ( $field['type'] == 'textarea' ) {
498
499 $data[ $meta_key ] = weforms_format_text( $value );
500
501 } elseif ( $field['type'] == 'name' ) {
502
503 $data[ $meta_key ] = implode( ' ', explode( WeForms::$field_separator, $value ) );
504
505 } elseif ( in_array( $field['type'], array( 'image_upload', 'file_upload' ) ) ) {
506
507 $data[ $meta_key ] = '';
508
509 if ( is_array( $value ) && $value ) {
510
511 foreach ( $value as $attachment_id ) {
512
513 if ( $field['type'] == 'image_upload' ) {
514 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
515 } else {
516 $thumb = get_post_field( 'post_title', $attachment_id );
517 }
518
519 $full_size = wp_get_attachment_url( $attachment_id );
520
521 $data[ $meta_key ] .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb );
522 }
523 }
524 } elseif ( in_array( $field['type'], array( 'checkbox', 'multiselect' ) ) ) {
525
526 $data[ $meta_key ] = explode( WeForms::$field_separator, $value );
527
528 } elseif ( $field['type'] == 'map' ) {
529
530 list( $lat, $long ) = explode( ',', $value );
531
532 $data[ $meta_key ] = array(
533 'lat' => $lat,
534 'long' => $long
535 );
536
537 } else {
538 $data[ $meta_key ] = $value;
539 }
540 }
541
542 return array(
543 'fields' => $fields,
544 'data' => $data
545 );
546 }
547
548 /**
549 * Format a text and apply WP function callbacks
550 *
551 * @param string $content
552 *
553 * @return string
554 */
555 function weforms_format_text( $content ) {
556 $content = wptexturize( $content );
557 $content = convert_smilies( $content );
558 $content = wpautop( $content );
559 $content = make_clickable( $content );
560
561 return $content;
562 }
563
564 /**
565 * Get User Agent browser and OS type
566 *
567 * @return array
568 */
569 function weforms_get_browser() {
570 $u_agent = $_SERVER['HTTP_USER_AGENT'];
571 $bname = 'Unknown';
572 $platform = 'Unknown';
573 $version = '';
574
575 // first get the platform
576 if ( preg_match( '/linux/i', $u_agent ) ) {
577 $platform = 'Linux';
578 } elseif ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
579 $platform = 'MAC OS';
580 } elseif ( preg_match( '/windows|win32/i', $u_agent ) ) {
581 $platform = 'Windows';
582 }
583
584 // next get the name of the useragent yes seperately and for good reason
585 if ( preg_match( '/MSIE/i',$u_agent ) && ! preg_match( '/Opera/i',$u_agent ) ) {
586 $bname = 'Internet Explorer';
587 $ub = 'MSIE';
588 } elseif ( preg_match( '/Trident/i',$u_agent ) ) {
589 // this condition is for IE11
590 $bname = 'Internet Explorer';
591 $ub = 'rv';
592 } elseif ( preg_match( '/Firefox/i',$u_agent ) ) {
593 $bname = 'Mozilla Firefox';
594 $ub = 'Firefox';
595 } elseif ( preg_match( '/Chrome/i',$u_agent ) ) {
596 $bname = 'Google Chrome';
597 $ub = 'Chrome';
598 } elseif ( preg_match( '/Safari/i',$u_agent ) ) {
599 $bname = 'Apple Safari';
600 $ub = 'Safari';
601 } elseif ( preg_match( '/Opera/i',$u_agent ) ) {
602 $bname = 'Opera';
603 $ub = 'Opera';
604 } elseif ( preg_match( '/Netscape/i',$u_agent ) ) {
605 $bname = 'Netscape';
606 $ub = 'Netscape';
607 }
608
609 // finally get the correct version number
610 // Added "|:"
611 $known = array( 'Version', $ub, 'other' );
612 $pattern = '#(?<browser>' . join( '|', $known ) .
613 ')[/|: ]+(?<version>[0-9.|a-zA-Z.]*)#';
614 if ( ! preg_match_all( $pattern, $u_agent, $matches ) ) {
615 // we have no matching number just continue
616 }
617
618 // see how many we have
619 $i = count( $matches['browser'] );
620
621 if ( $i != 1 ) {
622 // we will have two since we are not using 'other' argument yet
623 // see if version is before or after the name
624 if ( strripos( $u_agent,'Version' ) < strripos( $u_agent,$ub ) ) {
625 $version = $matches['version'][0];
626 } else {
627 $version = $matches['version'][1];
628 }
629 } else {
630 $version = $matches['version'][0];
631 }
632
633 // check if we have a number
634 if ( $version == null || $version == '' ) {
635 $version = '';
636 }
637
638 return array(
639 'userAgent' => $u_agent,
640 'name' => $bname,
641 'version' => $version,
642 'platform' => $platform,
643 'pattern' => $pattern
644 );
645 }
646
647 /**
648 * Get form notification merge tags
649 *
650 * @since 1.0
651 *
652 * @return array
653 */
654 function weforms_get_merge_tags() {
655 $tags = array(
656 'form' => array(
657 'title' => __( 'Form', 'weforms' ),
658 'tags' => array(
659 'entry_id' => __( 'Entry ID', 'weforms' ),
660 'form_id' => __( 'Form ID', 'weforms' ),
661 'form_name' => __( 'Form Name', 'weforms' )
662 )
663 ),
664 'system' => array(
665 'title' => __( 'System', 'weforms' ),
666 'tags' => array(
667 'admin_email' => __( 'Site Administrator Email', 'weforms' ),
668 'date' => __( 'Date', 'weforms' ),
669 'site_name' => __( 'Site Title', 'weforms' ),
670 'site_url' => __( 'Site URL', 'weforms' ),
671 'page_title' => __( 'Embedded Page Title', 'weforms' ),
672 )
673 ),
674 'user' => array(
675 'title' => __( 'User', 'weforms' ),
676 'tags' => array(
677 'ip_address' => __( 'IP Address', 'weforms' ),
678 'user_id' => __( 'User ID', 'weforms' ),
679 'first_name' => __( 'First Name', 'weforms' ),
680 'last_name' => __( 'Last Name', 'weforms' ),
681 'display_name' => __( 'Display Name', 'weforms' ),
682 'user_email' => __( 'Email', 'weforms' ),
683 )
684 ),
685 'urls' => array(
686 'title' => __( 'URL\'s', 'weforms' ),
687 'tags' => array(
688 'url_page' => __( 'Embeded Page URL', 'weforms' ),
689 'url_referer' => __( 'Referer URL', 'weforms' ),
690 'url_login' => __( 'Login URL', 'weforms' ),
691 'url_logout' => __( 'Logout URL', 'weforms' ),
692 'url_register' => __( 'Register URL', 'weforms' ),
693 'url_lost_password' => __( 'Lost Password URL', 'weforms' ),
694 )
695 ),
696 );
697
698 return apply_filters( 'wpuf_cf_merge_tags', $tags );
699 }
700
701 /**
702 * Record a form view count
703 *
704 * @param int $form_id
705 *
706 * @return void
707 */
708 function weforms_track_form_view( $form_id ) {
709 // don't track administrators
710 if ( current_user_can( 'administrator' ) ) {
711 return;
712 }
713
714 // ability to turn this off if someone doesn't like this tracking
715 $is_enabled = apply_filters( 'weforms_track_form_view', true );
716
717 if ( ! $is_enabled ) {
718 return;
719 }
720
721 // increase the count
722 $meta_key = '_weforms_view_count';
723 $number = (int) get_post_meta( $form_id, $meta_key, true );
724
725 update_post_meta( $form_id, $meta_key, ( $number + 1 ) );
726 }
727
728 /**
729 * Get form view count of a form
730 *
731 * @param int $form_id
732 *
733 * @return int
734 */
735 function weforms_get_form_views( $form_id ) {
736 return (int) get_post_meta( $form_id, '_weforms_view_count', true );
737 }
738
739 /**
740 * Get the weForms global settings
741 *
742 * @param string $key
743 * @param mixed $default
744 *
745 * @return mixed
746 */
747 function weforms_get_settings( $key = '', $default = '' ) {
748
749 $settings = get_option( 'weforms_settings', array() );
750 $settings = apply_filters( 'weforms_get_settings', $settings );
751
752 if ( empty( $key ) ) {
753 return $settings;
754 }
755
756 if ( isset( $settings[ $key ] ) ) {
757 return $settings[ $key ];
758 }
759
760 return $default;
761 }
762
763
764 /**
765 * Update Settings
766 *
767 * @param array $updated_settings
768 *
769 * @return array
770 */
771 function weforms_update_settings( $updated_settings = array() ) {
772
773 $previuos_settings = weforms_get_settings();
774
775 $settings = array_merge( $previuos_settings, $updated_settings );
776
777 update_option( 'weforms_settings', $settings );
778
779 return $settings;
780 }
781
782 /**
783 * Form access capability for forms
784 *
785 * @since 1.0.5
786 *
787 * @return string
788 */
789 function weforms_form_access_capability() {
790 return apply_filters( 'weforms_form_access_capability', 'manage_options' );
791 }
792
793 /**
794 * Form access capability for forms
795 *
796 * @since 1.2.4
797 *
798 * @return string
799 */
800 function weforms_log_file_path() {
801 return apply_filters( 'weforms_log_file_path', WP_CONTENT_DIR . '/weforms.log' );
802 }
803
804 /**
805 * Clear the buffer
806 *
807 * prevents ajax breakage and endless loading icon. A LIFE SAVER!!!
808 *
809 * @since 1.1.0
810 *
811 * @return void
812 */
813 function weforms_clear_buffer() {
814 ob_clean();
815 }
816
817 /**
818 * Get the client IP address
819 *
820 * @since 1.1.0
821 *
822 * @return string
823 */
824 function weforms_get_client_ip() {
825 $ipaddress = '';
826
827 if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
828 $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
829 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
830 $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
831 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) {
832 $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
833 } elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) {
834 $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
835 } elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
836 $ipaddress = $_SERVER['HTTP_FORWARDED'];
837 } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
838 $ipaddress = $_SERVER['REMOTE_ADDR'];
839 } else {
840 $ipaddress = 'UNKNOWN';
841 }
842
843 return $ipaddress;
844 }
845
846 /**
847 * Save form fields
848 *
849 * @since 1.1.0
850 *
851 * @param int $form_id
852 * @param array $field
853 * @param int $field_id
854 * @param int $order
855 *
856 * @return int ID of updated or inserted post
857 */
858 function weforms_insert_form_field( $form_id, $field = array(), $field_id = null, $order = 0 ) {
859
860 $args = array(
861 'post_type' => 'wpuf_input',
862 'post_parent' => $form_id,
863 'post_status' => 'publish',
864 'post_content' => maybe_serialize( wp_unslash( $field ) ),
865 'menu_order' => $order
866 );
867
868 if ( $field_id ) {
869 $args['ID'] = $field_id;
870 }
871
872 if ( $field_id ) {
873 return wp_update_post( $args );
874 } else {
875 return wp_insert_post( $args );
876 }
877 }
878
879 /**
880 * Allowed upload extensions
881 *
882 * @return array
883 */
884 function weforms_allowed_extensions() {
885 $extesions = array(
886 'images' => array(
887 'ext' => 'jpg,jpeg,gif,png,bmp',
888 'label' => __( 'Images', 'weforms' )
889 ),
890 'audio' => array(
891 'ext' => 'mp3,wav,ogg,wma,mka,m4a,ra,mid,midi',
892 'label' => __( 'Audio', 'weforms' )
893 ),
894 'video' => array(
895 'ext' => 'avi,divx,flv,mov,ogv,mkv,mp4,m4v,divx,mpg,mpeg,mpe',
896 'label' => __( 'Videos', 'weforms' )
897 ),
898 'pdf' => array(
899 'ext' => 'pdf',
900 'label' => __( 'PDF', 'weforms' )
901 ),
902 'office' => array(
903 'ext' => 'doc,ppt,pps,xls,mdb,docx,xlsx,pptx,odt,odp,ods,odg,odc,odb,odf,rtf,txt',
904 'label' => __( 'Office Documents', 'weforms' )
905 ),
906 'zip' => array(
907 'ext' => 'zip,gz,gzip,rar,7z',
908 'label' => __( 'Zip Archives', 'weforms' )
909 ),
910 'exe' => array(
911 'ext' => 'exe',
912 'label' => __( 'Executable Files', 'weforms' )
913 ),
914 'csv' => array(
915 'ext' => 'csv',
916 'label' => __( 'CSV', 'weforms' )
917 )
918 );
919
920 return apply_filters( 'weforms_allowed_extensions', $extesions );
921 }
922
923 /**
924 * Get form integration settings
925 *
926 * @since 1.1.0
927 *
928 * @param int $form_id
929 *
930 * @return array
931 */
932 function weforms_get_form_integrations( $form_id ) {
933 $integrations = get_post_meta( $form_id, 'integrations', true );
934
935 if ( ! $integrations ) {
936 return array();
937 }
938
939 return $integrations;
940 }
941
942
943 /**
944 * Check if an integration is active
945 *
946 * @since 1.1.0
947 *
948 * @param int $form_id
949 * @param string $integration_id
950 *
951 * @return boolean
952 */
953 function weforms_is_integration_active( $form_id, $integration_id ) {
954 $integrations = weforms_get_form_integrations( $form_id );
955
956 if ( ! $integrations ) {
957 return false;
958 }
959
960 foreach ( $integrations as $id => $integration ) {
961 if ( $integration_id == $id && $integration->enabled == true ) {
962 return $integration;
963 }
964 }
965
966 return false;
967 }
968
969
970 /**
971 * Get Flat UI Colors
972 *
973 * @return array
974 */
975 function weforms_get_flat_ui_colors() {
976 return array( '#1abc9c', '#2ecc71', '#3498db', '#9b59b6', '#34495e' );
977 }
978
979 /**
980 * Get default form settings
981 *
982 * @return array
983 */
984 function weforms_get_default_form_settings() {
985 return apply_filters(
986 'weforms_get_default_form_settings', array(
987 'redirect_to' => 'same',
988 'message' => __( 'Thanks for contacting us! We will get in touch with you shortly.', 'weforms' ),
989 'page_id' => '',
990 'url' => '',
991 'submit_text' => __( 'Submit Query', 'weforms' ),
992 'schedule_form' => 'false',
993 'schedule_start' => '',
994 'schedule_end' => '',
995 'sc_pending_message' => __( 'Form submission hasn\'t been started yet', 'weforms' ),
996 'sc_expired_message' => __( 'Form submission is now closed.', 'weforms' ),
997 'require_login' => 'false',
998 'req_login_message' => __( 'You need to login to submit a query.', 'weforms' ),
999 'limit_entries' => 'false',
1000 'limit_number' => '100',
1001 'limit_message' => __( 'Sorry, we have reached the maximum number of submissions.', 'weforms' ),
1002 'label_position' => 'above',
1003 'enable_multistep' => false,
1004 'multistep_progressbar_type' => 'progressive',
1005
1006 // payment
1007 'payment_paypal_images' => 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg',
1008
1009 'payment_paypal_label' => __( 'PayPal', 'weforms' ),
1010 'payment_stripe_label' => __( 'Credit Card', 'weforms' ),
1011 'payment_stripe_images' => array( 'visa','mastercard','amex','discover' ),
1012
1013 'payment_stripe_deactivate' => '',
1014 'stripe_mode' => 'live',
1015 'stripe_page_id' => '',
1016
1017 'stripe_override_keys' => '',
1018 'stripe_email' => '',
1019 'stripe_key' => '',
1020 'stripe_secret_key' => '',
1021 'stripe_key_test' => '',
1022 'stripe_secret_key_test' => '',
1023
1024 'stripe_prefill_email' => '',
1025 'stripe_user_email_field' => '',
1026
1027 'payment_paypal_deactivate' => '',
1028 'paypal_mode' => 'live',
1029 'paypal_type' => '_cart',
1030
1031 'paypal_override' => '',
1032 'paypal_email' => '',
1033
1034 'paypal_page_id' => '',
1035
1036 'paypal_prefill_email' => '',
1037 'paypal_user_email_field' => '',
1038 )
1039 );
1040 }
1041
1042 /**
1043 * Get default form settings
1044 *
1045 * @return array
1046 */
1047 function weforms_get_default_form_notification() {
1048 return apply_filters(
1049 'weforms_get_default_form_notification', array(
1050 'active' => 'true',
1051
1052 'type' => 'email',
1053 'smsTo' => '',
1054 'smsText' => '[{form_name}] ' . __( 'New Form Submission', 'weforms' ) . ' #{entry_id}',
1055
1056 'name' => __( 'Admin Notification', 'weforms' ),
1057 'subject' => '[{form_name}] ' . __( 'New Form Submission', 'weforms' ) . ' #{entry_id}',
1058 'to' => '{admin_email}',
1059 'replyTo' => '{field:email}',
1060 'message' => '{all_fields}',
1061 'fromName' => '{site_name}',
1062 'fromAddress' => '{admin_email}',
1063 'cc' => '',
1064 'bcc' => '',
1065 'weforms_cond' => array(
1066 'condition_status' => 'no',
1067 'cond_logic' => 'any',
1068 'conditions' => array(
1069 array(
1070 'name' => '',
1071 'operator' => '=',
1072 'option' => ''
1073 )
1074 )
1075 )
1076 )
1077 );
1078 }
1079
1080 /**
1081 * weforms_get_pain_text
1082 *
1083 * @param $value mixed
1084 *
1085 * @return string
1086 **/
1087 function weforms_get_pain_text( $value ) {
1088
1089 if ( is_serialized( $value ) ) {
1090 $value = unserialize( $value );
1091 }
1092
1093 if ( is_array( $value ) ) {
1094
1095 $string_value = array();
1096
1097 if ( is_array( $value ) ) {
1098
1099 foreach ( $value as $key => $single_value ) {
1100
1101 if ( is_array( $single_value ) || is_serialized( $single_value ) ) {
1102 $single_value = weforms_get_pain_text( $single_value );
1103 }
1104
1105 $single_value = ucwords( str_replace( array( '_', '-' ), ' ', $key ) ) . ': ' . ucwords( $single_value );
1106
1107 $string_value[] = $single_value;
1108 }
1109
1110 $value = implode( WeForms::$field_separator , $string_value );
1111 }
1112 }
1113
1114 $value = trim( strip_tags( $value ) );
1115
1116 return $value;
1117 }
1118