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