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

1,311 lines 35.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 the form author details
448 *
449 * @param int $form_id
450 *
451 * @return array
452 */
453 function weforms_get_form_author_details( $form_id ) {
454 if ( empty( $form_id ) ) {
455 return;
456 }
457
458 $author_details = array();
459 $form = get_post( $form_id );
460 $author_id = $form->post_author;
461
462 $author_details['username'] = get_the_author_meta( 'user_login' , $author_id );
463 $author_details['avatar'] = get_avatar_url( $author_id );
464
465 return $author_details;
466 }
467
468 /**
469 * Get table column heads for a form
470 *
471 * For now, return only text type fields
472 *
473 * @param int $form_id
474 *
475 * @return array
476 */
477 function weforms_get_entry_columns( $form_id, $limit = 6 ) {
478 $fields = weforms()->form->get( $form_id )->get_fields();
479 $columns = array();
480
481 // filter by input types
482 if ( $limit ) {
483
484 $fields = array_filter( $fields, function( $item ) {
485 return in_array( $item['template'], array( 'text_field', 'name_field', 'dropdown_field', 'radio_field', 'email_address', 'url_field', 'column_field' ) );
486 });
487 }
488
489 if ( $fields ) {
490 foreach ( $fields as $field ) {
491 if ( $field['template'] == 'column_field' ) {
492 foreach ( $field['inner_fields'] as $c_field ) {
493 $columns[ $c_field[0]['name'] ] = $c_field[0]['label'];
494 }
495 continue;
496 }
497 $columns[ $field['name'] ] = $field['label'];
498 }
499 }
500
501 // if passed 0/false, return all columns
502 if ( $limit && sizeof( $columns ) > $limit ) {
503 $columns = array_slice( $columns, 0, $limit ); // max 6 columns
504 }
505
506 return apply_filters( 'weforms_get_entry_columns', $columns, $form_id );
507 }
508
509 /**
510 * Get table column heads for a form
511 *
512 * For now, return only text type fields
513 *
514 * @param int $form_id
515 *
516 * @return array
517 */
518 function weforms_get_payment_columns( $form_id, $limit = 6 ) {
519 $fields = weforms()->form->get( $form_id )->get_fields();
520 $columns = array();
521
522 // filter by input types
523 if ( $limit ) {
524
525 $fields = array_filter( $fields, function( $item ) {
526 return in_array( $item['template'], array( 'text_field', 'name_field', 'dropdown_field', 'radio_field', 'email_address', 'url_field' ) );
527 });
528 }
529
530 if ( $fields ) {
531 foreach ( $fields as $field ) {
532 $columns[ $field['name'] ] = $field['label'];
533 }
534 }
535
536 // if passed 0/false, return all collumns
537 if ( $limit && sizeof( $columns ) > $limit ) {
538 $columns = array_slice( $columns, 0, $limit ); // max 6 columns
539 }
540
541 return apply_filters( 'weforms_get_entry_columns', $columns, $form_id );
542 }
543
544 /**
545 * Get fields from a form by it's meta key
546 *
547 * @param int $form_id
548 *
549 * @return array
550 */
551 function weforms_get_form_field_labels( $form_id ) {
552 $fields = weforms()->form->get( $form_id )->get_fields();
553 $exclude = array( 'step_start', 'section_break', 'recaptcha', 'shortcode', 'action_hook' );
554
555 if ( ! $fields ) {
556 return false;
557 }
558
559 $data = array();
560 foreach ( $fields as $field ) {
561 if ( empty( $field['name'] ) ) {
562 continue;
563 }
564
565 // exclude the fields
566 if ( in_array( $field['template'], $exclude ) ) {
567 continue;
568 }
569
570 $data[ $field['name'] ] = array(
571 'label' => $field['label'],
572 'type' => $field['template']
573 );
574 }
575
576 return $data;
577 }
578
579 /**
580 * Get data from an entry
581 *
582 * @param int $entry_id
583 * @param int $form_id
584 *
585 * @return false|array
586 */
587 function weforms_get_entry_data( $entry_id ) {
588 $data = array();
589 $entry = weforms_get_entry( $entry_id );
590 $fields = weforms_get_form_field_labels( $entry->form_id );
591
592 if ( ! $fields ) {
593 return false;
594 }
595
596 foreach ( $fields as $meta_key => $field ) {
597 $value = weforms_get_entry_meta( $entry_id, $meta_key, true );
598
599 if ( $field['type'] == 'textarea' ) {
600
601 $data[ $meta_key ] = weforms_format_text( $value );
602
603 } elseif ( $field['type'] == 'name' ) {
604
605 $data[ $meta_key ] = implode( ' ', explode( WeForms::$field_separator, $value ) );
606
607 } elseif ( in_array( $field['type'], array( 'image_upload', 'file_upload' ) ) ) {
608
609 $data[ $meta_key ] = '';
610
611 if ( is_array( $value ) && $value ) {
612
613 foreach ( $value as $attachment_id ) {
614
615 if ( $field['type'] == 'image_upload' ) {
616 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
617 } else {
618 $thumb = get_post_field( 'post_title', $attachment_id );
619 }
620
621 $full_size = wp_get_attachment_url( $attachment_id );
622
623 $data[ $meta_key ] .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb );
624 }
625 }
626 } elseif ( in_array( $field['type'], array( 'checkbox', 'multiselect' ) ) ) {
627
628 $data[ $meta_key ] = explode( WeForms::$field_separator, $value );
629
630 } elseif ( $field['type'] == 'map' ) {
631
632 list( $lat, $long ) = explode( ',', $value );
633
634 $data[ $meta_key ] = array(
635 'lat' => $lat,
636 'long' => $long
637 );
638
639 } else {
640 $data[ $meta_key ] = $value;
641 }
642 }
643
644 return array(
645 'fields' => $fields,
646 'data' => $data
647 );
648 }
649
650 /**
651 * Format a text and apply WP function callbacks
652 *
653 * @param string $content
654 *
655 * @return string
656 */
657 function weforms_format_text( $content ) {
658 $content = wptexturize( $content );
659 $content = convert_smilies( $content );
660 $content = wpautop( $content );
661 $content = make_clickable( $content );
662
663 return $content;
664 }
665
666 /**
667 * Get User Agent browser and OS type
668 *
669 * @return array
670 */
671 function weforms_get_browser() {
672 $u_agent = $_SERVER['HTTP_USER_AGENT'];
673 $bname = 'Unknown';
674 $platform = 'Unknown';
675 $version = '';
676
677 // first get the platform
678 if ( preg_match( '/linux/i', $u_agent ) ) {
679 $platform = 'Linux';
680 } elseif ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
681 $platform = 'MAC OS';
682 } elseif ( preg_match( '/windows|win32/i', $u_agent ) ) {
683 $platform = 'Windows';
684 }
685
686 // next get the name of the useragent yes seperately and for good reason
687 if ( preg_match( '/MSIE/i',$u_agent ) && ! preg_match( '/Opera/i',$u_agent ) ) {
688 $bname = 'Internet Explorer';
689 $ub = 'MSIE';
690 } elseif ( preg_match( '/Trident/i',$u_agent ) ) {
691 // this condition is for IE11
692 $bname = 'Internet Explorer';
693 $ub = 'rv';
694 } elseif ( preg_match( '/Firefox/i',$u_agent ) ) {
695 $bname = 'Mozilla Firefox';
696 $ub = 'Firefox';
697 } elseif ( preg_match( '/Chrome/i',$u_agent ) ) {
698 $bname = 'Google Chrome';
699 $ub = 'Chrome';
700 } elseif ( preg_match( '/Safari/i',$u_agent ) ) {
701 $bname = 'Apple Safari';
702 $ub = 'Safari';
703 } elseif ( preg_match( '/Opera/i',$u_agent ) ) {
704 $bname = 'Opera';
705 $ub = 'Opera';
706 } elseif ( preg_match( '/Netscape/i',$u_agent ) ) {
707 $bname = 'Netscape';
708 $ub = 'Netscape';
709 }
710
711 // finally get the correct version number
712 // Added "|:"
713 $known = array( 'Version', $ub, 'other' );
714 $pattern = '#(?<browser>' . join( '|', $known ) .
715 ')[/|: ]+(?<version>[0-9.|a-zA-Z.]*)#';
716 if ( ! preg_match_all( $pattern, $u_agent, $matches ) ) {
717 // we have no matching number just continue
718 }
719
720 // see how many we have
721 $i = count( $matches['browser'] );
722
723 if ( $i != 1 ) {
724 // we will have two since we are not using 'other' argument yet
725 // see if version is before or after the name
726 if ( strripos( $u_agent,'Version' ) < strripos( $u_agent,$ub ) ) {
727 $version = $matches['version'][0];
728 } else {
729 $version = $matches['version'][1];
730 }
731 } else {
732 $version = $matches['version'][0];
733 }
734
735 // check if we have a number
736 if ( $version == null || $version == '' ) {
737 $version = '';
738 }
739
740 return array(
741 'userAgent' => $u_agent,
742 'name' => $bname,
743 'version' => $version,
744 'platform' => $platform,
745 'pattern' => $pattern
746 );
747 }
748
749 /**
750 * Get form notification merge tags
751 *
752 * @since 1.0
753 *
754 * @return array
755 */
756 function weforms_get_merge_tags() {
757 $tags = array(
758 'form' => array(
759 'title' => __( 'Form', 'weforms' ),
760 'tags' => array(
761 'entry_id' => __( 'Entry ID', 'weforms' ),
762 'form_id' => __( 'Form ID', 'weforms' ),
763 'form_name' => __( 'Form Name', 'weforms' )
764 )
765 ),
766 'system' => array(
767 'title' => __( 'System', 'weforms' ),
768 'tags' => array(
769 'admin_email' => __( 'Site Administrator Email', 'weforms' ),
770 'date' => __( 'Date', 'weforms' ),
771 'site_name' => __( 'Site Title', 'weforms' ),
772 'site_url' => __( 'Site URL', 'weforms' ),
773 'page_title' => __( 'Embedded Page Title', 'weforms' ),
774 )
775 ),
776 'user' => array(
777 'title' => __( 'User', 'weforms' ),
778 'tags' => array(
779 'ip_address' => __( 'IP Address', 'weforms' ),
780 'user_id' => __( 'User ID', 'weforms' ),
781 'first_name' => __( 'First Name', 'weforms' ),
782 'last_name' => __( 'Last Name', 'weforms' ),
783 'display_name' => __( 'Display Name', 'weforms' ),
784 'user_email' => __( 'Email', 'weforms' ),
785 )
786 ),
787 'urls' => array(
788 'title' => __( 'URL\'s', 'weforms' ),
789 'tags' => array(
790 'url_page' => __( 'Embeded Page URL', 'weforms' ),
791 'url_referer' => __( 'Referer URL', 'weforms' ),
792 'url_login' => __( 'Login URL', 'weforms' ),
793 'url_logout' => __( 'Logout URL', 'weforms' ),
794 'url_register' => __( 'Register URL', 'weforms' ),
795 'url_lost_password' => __( 'Lost Password URL', 'weforms' ),
796 'personal_data_erase_confirm_url' => __( 'Personal Data Erase Confirmation URL', 'weforms' ),
797 'personal_data_export_confirm_url' => __( 'Personal Data Export Confirmation URL', 'weforms' ),
798 )
799 ),
800 );
801
802 return apply_filters( 'wpuf_cf_merge_tags', $tags );
803 }
804
805 /**
806 * Record a form view count
807 *
808 * @param int $form_id
809 *
810 * @return void
811 */
812 function weforms_track_form_view( $form_id ) {
813 // don't track administrators
814 if ( current_user_can( 'administrator' ) ) {
815 return;
816 }
817
818 // ability to turn this off if someone doesn't like this tracking
819 $is_enabled = apply_filters( 'weforms_track_form_view', true );
820
821 if ( ! $is_enabled ) {
822 return;
823 }
824
825 // increase the count
826 $meta_key = '_weforms_view_count';
827 $number = (int) get_post_meta( $form_id, $meta_key, true );
828
829 update_post_meta( $form_id, $meta_key, ( $number + 1 ) );
830 }
831
832 /**
833 * Get form view count of a form
834 *
835 * @param int $form_id
836 *
837 * @return int
838 */
839 function weforms_get_form_views( $form_id ) {
840 return (int) get_post_meta( $form_id, '_weforms_view_count', true );
841 }
842
843 /**
844 * Get the weForms global settings
845 *
846 * @param string $key
847 * @param mixed $default
848 *
849 * @return mixed
850 */
851 function weforms_get_settings( $key = '', $default = '' ) {
852
853 $settings = get_option( 'weforms_settings', array() );
854 $settings = apply_filters( 'weforms_get_settings', $settings );
855
856 if ( empty( $key ) ) {
857 return $settings;
858 }
859
860 if ( isset( $settings[ $key ] ) ) {
861 return $settings[ $key ];
862 }
863
864 return $default;
865 }
866
867
868 /**
869 * Update Settings
870 *
871 * @param array $updated_settings
872 *
873 * @return array
874 */
875 function weforms_update_settings( $updated_settings = array() ) {
876
877 $previuos_settings = weforms_get_settings();
878
879 $settings = array_merge( $previuos_settings, $updated_settings );
880
881 update_option( 'weforms_settings', $settings );
882
883 return $settings;
884 }
885
886 /**
887 * Form access capability for forms
888 *
889 * @since 1.0.5
890 *
891 * @return string
892 */
893 function weforms_form_access_capability() {
894 return apply_filters( 'weforms_form_access_capability', 'manage_options' );
895 }
896
897 /**
898 * Form access capability for forms
899 *
900 * @since 1.2.4
901 *
902 * @return string
903 */
904 function weforms_log_file_path() {
905 return apply_filters( 'weforms_log_file_path', WP_CONTENT_DIR . '/weforms.log' );
906 }
907
908 /**
909 * Clear the buffer
910 *
911 * prevents ajax breakage and endless loading icon. A LIFE SAVER!!!
912 *
913 * @since 1.1.0
914 *
915 * @return void
916 */
917 function weforms_clear_buffer() {
918 if ( ob_get_length() > 0 ) {
919 ob_clean();
920 }
921 }
922
923 /**
924 * Get the client IP address
925 *
926 * @since 1.1.0
927 *
928 * @return string
929 */
930 function weforms_get_client_ip() {
931 $ipaddress = '';
932
933 if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
934 $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
935 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
936 $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
937 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) {
938 $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
939 } elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) {
940 $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
941 } elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
942 $ipaddress = $_SERVER['HTTP_FORWARDED'];
943 } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
944 $ipaddress = $_SERVER['REMOTE_ADDR'];
945 } else {
946 $ipaddress = 'UNKNOWN';
947 }
948
949 return $ipaddress;
950 }
951
952 /**
953 * Save form fields
954 *
955 * @since 1.1.0
956 *
957 * @param int $form_id
958 * @param array $field
959 * @param int $field_id
960 * @param int $order
961 *
962 * @return int ID of updated or inserted post
963 */
964 function weforms_insert_form_field( $form_id, $field = array(), $field_id = null, $order = 0 ) {
965
966 $args = array(
967 'post_type' => 'wpuf_input',
968 'post_parent' => $form_id,
969 'post_status' => 'publish',
970 'post_content' => maybe_serialize( wp_unslash( $field ) ),
971 'menu_order' => $order
972 );
973
974 if ( $field_id ) {
975 $args['ID'] = $field_id;
976 }
977
978 if ( $field_id ) {
979 return wp_update_post( $args );
980 } else {
981 return wp_insert_post( $args );
982 }
983 }
984
985 /**
986 * Allowed upload extensions
987 *
988 * @return array
989 */
990 function weforms_allowed_extensions() {
991 $extesions = array(
992 'images' => array(
993 'ext' => 'jpg,jpeg,gif,png,bmp',
994 'label' => __( 'Images', 'weforms' )
995 ),
996 'audio' => array(
997 'ext' => 'mp3,wav,ogg,wma,mka,m4a,ra,mid,midi',
998 'label' => __( 'Audio', 'weforms' )
999 ),
1000 'video' => array(
1001 'ext' => 'avi,divx,flv,mov,ogv,mkv,mp4,m4v,divx,mpg,mpeg,mpe',
1002 'label' => __( 'Videos', 'weforms' )
1003 ),
1004 'pdf' => array(
1005 'ext' => 'pdf',
1006 'label' => __( 'PDF', 'weforms' )
1007 ),
1008 'office' => array(
1009 'ext' => 'doc,ppt,pps,xls,mdb,docx,xlsx,pptx,odt,odp,ods,odg,odc,odb,odf,rtf,txt',
1010 'label' => __( 'Office Documents', 'weforms' )
1011 ),
1012 'zip' => array(
1013 'ext' => 'zip,gz,gzip,rar,7z',
1014 'label' => __( 'Zip Archives', 'weforms' )
1015 ),
1016 'exe' => array(
1017 'ext' => 'exe',
1018 'label' => __( 'Executable Files', 'weforms' )
1019 ),
1020 'csv' => array(
1021 'ext' => 'csv',
1022 'label' => __( 'CSV', 'weforms' )
1023 )
1024 );
1025
1026 return apply_filters( 'weforms_allowed_extensions', $extesions );
1027 }
1028
1029 /**
1030 * Get form integration settings
1031 *
1032 * @since 1.1.0
1033 *
1034 * @param int $form_id
1035 *
1036 * @return array
1037 */
1038 function weforms_get_form_integrations( $form_id ) {
1039 $integrations = get_post_meta( $form_id, 'integrations', true );
1040
1041 if ( ! $integrations ) {
1042 return array();
1043 }
1044
1045 return $integrations;
1046 }
1047
1048
1049 /**
1050 * Check if an integration is active
1051 *
1052 * @since 1.1.0
1053 *
1054 * @param int $form_id
1055 * @param string $integration_id
1056 *
1057 * @return boolean
1058 */
1059 function weforms_is_integration_active( $form_id, $integration_id ) {
1060 $integrations = weforms_get_form_integrations( $form_id );
1061
1062 if ( ! $integrations ) {
1063 return false;
1064 }
1065
1066 foreach ( $integrations as $id => $integration ) {
1067 if ( $integration_id == $id && $integration->enabled == true ) {
1068 return $integration;
1069 }
1070 }
1071
1072 return false;
1073 }
1074
1075
1076 /**
1077 * Get Flat UI Colors
1078 *
1079 * @return array
1080 */
1081 function weforms_get_flat_ui_colors() {
1082 return array( '#1abc9c', '#2ecc71', '#3498db', '#9b59b6', '#34495e' );
1083 }
1084
1085 /**
1086 * Get default form settings
1087 *
1088 * @return array
1089 */
1090 function weforms_get_default_form_settings() {
1091 return apply_filters(
1092 'weforms_get_default_form_settings', array(
1093 'redirect_to' => 'same',
1094 'message' => __( 'Thanks for contacting us! We will get in touch with you shortly.', 'weforms' ),
1095 'page_id' => '',
1096 'url' => '',
1097
1098 'submit_text' => __( 'Submit Query', 'weforms' ),
1099 'submit_button_cond' => array(
1100 'condition_status' => 'no',
1101 'cond_logic' => 'any',
1102 'conditions' => array(
1103 array(
1104 'name' => '',
1105 'operator' => '=',
1106 'option' => ''
1107 )
1108 )
1109 ),
1110
1111 'schedule_form' => 'false',
1112 'schedule_start' => '',
1113 'schedule_end' => '',
1114 'sc_pending_message' => __( 'Form submission hasn\'t been started yet', 'weforms' ),
1115 'sc_expired_message' => __( 'Form submission is now closed.', 'weforms' ),
1116 'require_login' => 'false',
1117 'req_login_message' => __( 'You need to login to submit a query.', 'weforms' ),
1118 'limit_entries' => 'false',
1119 'limit_number' => '100',
1120 'limit_message' => __( 'Sorry, we have reached the maximum number of submissions.', 'weforms' ),
1121 'label_position' => 'above',
1122 'use_theme_css' => 'wpuf-style',
1123 'quiz_form' => 'no',
1124 'shuffle_question_order' => 'no',
1125 'release_grade' => 'after_submission',
1126 'respondent_can_see' => array( 'missed_questions', 'correct_answers', 'point_values' ),
1127 'total_points' => 0,
1128 'enable_multistep' => false,
1129 'multistep_progressbar_type' => 'progressive',
1130
1131 // payment
1132 'payment_paypal_images' => 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg',
1133
1134 'payment_paypal_label' => __( 'PayPal', 'weforms' ),
1135 'payment_stripe_label' => __( 'Credit Card', 'weforms' ),
1136 'payment_stripe_images' => array( 'visa','mastercard','amex','discover' ),
1137
1138 'payment_stripe_deactivate' => '',
1139 'stripe_mode' => 'live',
1140 'stripe_page_id' => '',
1141
1142 'stripe_override_keys' => '',
1143 'stripe_email' => '',
1144 'stripe_key' => '',
1145 'stripe_secret_key' => '',
1146 'stripe_key_test' => '',
1147 'stripe_secret_key_test' => '',
1148
1149 'stripe_prefill_email' => '',
1150 'stripe_user_email_field' => '',
1151
1152 'payment_paypal_deactivate' => '',
1153 'paypal_mode' => 'live',
1154 'paypal_type' => '_cart',
1155
1156 'paypal_override' => '',
1157 'paypal_email' => '',
1158
1159 'paypal_page_id' => '',
1160
1161 'paypal_prefill_email' => '',
1162 'paypal_user_email_field' => '',
1163 )
1164 );
1165 }
1166
1167 /**
1168 * Get default form settings
1169 *
1170 * @return array
1171 */
1172 function weforms_get_default_form_notification() {
1173 return apply_filters(
1174 'weforms_get_default_form_notification', array(
1175 'active' => 'true',
1176
1177 'type' => 'email',
1178 'smsTo' => '',
1179 'smsText' => '[{form_name}] ' . __( 'New Form Submission', 'weforms' ) . ' #{entry_id}',
1180
1181 'name' => __( 'Admin Notification', 'weforms' ),
1182 'subject' => '[{form_name}] ' . __( 'New Form Submission', 'weforms' ) . ' #{entry_id}',
1183 'to' => '{admin_email}',
1184 'replyTo' => '{field:email}',
1185 'message' => '{all_fields}',
1186 'fromName' => '{site_name}',
1187 'fromAddress' => '{admin_email}',
1188 'cc' => '',
1189 'bcc' => '',
1190 'weforms_cond' => array(
1191 'condition_status' => 'no',
1192 'cond_logic' => 'any',
1193 'conditions' => array(
1194 array(
1195 'name' => '',
1196 'operator' => '=',
1197 'option' => ''
1198 )
1199 )
1200 )
1201 )
1202 );
1203 }
1204
1205 /**
1206 * weforms_get_pain_text
1207 *
1208 * @param $value mixed
1209 *
1210 * @return string
1211 **/
1212 function weforms_get_pain_text( $value ) {
1213
1214 if ( is_serialized( $value ) ) {
1215 $value = unserialize( $value );
1216 }
1217
1218 if ( is_array( $value ) ) {
1219
1220 $string_value = array();
1221
1222 if ( is_array( $value ) ) {
1223
1224 foreach ( $value as $key => $single_value ) {
1225
1226 if ( is_array( $single_value ) || is_serialized( $single_value ) ) {
1227 $single_value = weforms_get_pain_text( $single_value );
1228 }
1229
1230 $single_value = ucwords( str_replace( array( '_', '-' ), ' ', $key ) ) . ': ' . ucwords( $single_value );
1231
1232 $string_value[] = $single_value;
1233 }
1234
1235 $value = implode( WeForms::$field_separator , $string_value );
1236 }
1237 }
1238
1239 $value = trim( strip_tags( $value ) );
1240
1241 return $value;
1242 }
1243
1244 /**
1245 * Get countries
1246 *
1247 * @since 1.1.0
1248 *
1249 * @param string $type (optional)
1250 *
1251 * @return array|string
1252 */
1253 function weforms_get_countries( $type = 'array' ) {
1254 $countries = include WEFORMS_INCLUDES . '/country-list.php';
1255
1256 if ( $type == 'json' ) {
1257 $countries = json_encode( $countries );
1258 }
1259
1260 return $countries;
1261 }
1262
1263
1264
1265 /**
1266 * Get country name
1267 *
1268 * @since 1.3.7
1269 *
1270 * @param string $country_code (required)
1271 *
1272 * @return string
1273 */
1274 function get_country_name( $country_code ) {
1275 if ( empty( $country_code ) ) {
1276 return;
1277 }
1278
1279 $countries = weforms_get_countries();
1280
1281 foreach ($countries as $country ) {
1282 if ( $country['code'] == $country_code ) {
1283 return $country['name'];
1284 }
1285 }
1286 }
1287
1288 /**
1289 * Localize countries
1290 *
1291 * @since 1.3.5
1292 *
1293 * @return array
1294 */
1295 function weforms_localized_countries( $script ) {
1296 $script['countries'] = weforms_get_countries();
1297 return $script;
1298 }
1299 add_filter( "weforms_localized_script", "weforms_localized_countries" );
1300
1301
1302 function is_weforms_api_allowed_guest_submission( ) {
1303 $permission = false;
1304
1305 if( current_user_can( 'read' )){
1306 $permission = true;
1307 }
1308
1309 return apply_filters( 'weforms_anonymous_entry_submit_permission', $permission );
1310 }
1311