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