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