PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.0
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.0
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.4.0, at includes/functions.php

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