-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfunctions.php
More file actions
1359 lines (1183 loc) · 37.5 KB
/
Copy pathfunctions.php
File metadata and controls
1359 lines (1183 loc) · 37.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Get a single form
*
* @since 1.0.3
*
* @param int $form_id
*
* @return false|WP_Post
*/
function weform_get_form( $form_id ) {
return weforms()->form->get( $form_id );
}
/**
* Get contact form templates
*
* @return array
*/
function weforms_get_form_template_categories() {
$categories = [
'default' => [
'name' => 'Default Templates',
'icon' => 'fa fa-bars',
],
'registration' => [
'name' => 'Registration Templates',
'icon' => 'fa fa-user-plus',
],
'application' => [
'name' => 'Application Templates',
'icon' => 'fa fa-address-card-o',
],
'request' => [
'name' => 'Request Templates',
'icon' => 'fa fa-hand-paper-o',
],
'event' => [
'name' => 'Event Templates',
'icon' => 'fa fa-calendar',
],
'feedback' => [
'name' => 'Feedback Templates',
'icon' => 'fa fa-comments',
],
'employment' => [
'name' => 'Employment Templates',
'icon' => 'fa fa-suitcase',
],
'payment' => [
'name' => 'Payment Templates',
'icon' => 'fa fa-money',
],
'reservation' => [
'name' => 'Reservation Templates',
'icon' => 'fa fa-bandcamp',
],
'others' => [
'name' => 'Others Templates',
'icon' => 'fa fa-info-circle',
],
];
return apply_filters( 'weforms_form_template_categories', $categories );
}
/**
* Get entries by a form_id
*
* @param int $form_id
* @param array $args
*
* @return object
*/
function weforms_get_form_entries( $form_id, $args = [] ) {
global $wpdb;
$defaults = [
'number' => 10,
'offset' => 0,
'orderby' => 'created_at',
'status' => 'publish',
'order' => 'DESC',
];
$r = wp_parse_args( $args, $defaults );
$query = $wpdb->prepare(
SELECT id, form_id, user_id, INET_NTOA( user_ip ) as ip_address, created_at
FROM $wpdb->weforms_entries
WHERE form_id = %d AND status = %s
ORDER BY %s %s
LIMIT %d, %d
",
array(
$form_id,
$r['status'],
$r['orderby'],
$r['order'],
$r['offset'],
$r['number'],
)
);
$results = $wpdb->get_results( $query );
return $results;
}
/**
* Count all entries from weForms_entries table
*
* @param array $args
*
* @return int $number
*/
function weforms_count_entries( $args = [] ) {
global $wpdb;
$defaults = [
'orderby' => 'created_at',
'status' => 'publish',
'order' => 'DESC',
];
$r = wp_parse_args( $args, $defaults );
$query = $wpdb->prepare(
SELECT id, form_id, user_id, INET_NTOA( user_ip ) as ip_address, created_at
FROM $wpdb->weforms_entries
WHERE status = %s
ORDER BY %s %s
",
array(
$r['status'],
$r['orderby'],
$r['order'],
)
);
$results = $wpdb->get_results( $query );
return count( $results );
}
/**
* Get payments by a form_id
*
* @param int $form_id
* @param array $args
*
* @return object
*/
function weforms_get_form_payments( $form_id, $args = [] ) {
global $wpdb;
$defaults = [
'number' => 10,
'offset' => 0,
'orderby' => 'created_at',
'order' => 'DESC',
];
$r = wp_parse_args( $args, $defaults );
$query = $wpdb->prepare(
SELECT *
FROM wp_weforms_payments
WHERE form_id = %d
ORDER BY %s %s
LIMIT %d, %d
",
array(
$form_id,
$r['orderby'],
$r['order'],
$r['offset'],
$r['number'],
)
);
$results = $wpdb->get_results( $query );
return $results;
}
/**
* Get payments by a entry_id
*
* @param int $entry_id
*
* @return object
*/
function weforms_get_entry_payment( $entry_id ) {
global $wpdb;
$query = $wpdb->prepare(
SELECT transaction_id
FROM $wpdb->prefix 'weforms_payments'
WHERE entry_id = %d
",
array(
$entry_id
)
);
$payment = $wpdb->get_row( $query );
return $payment;
}
/**
* Get an entry by id
*
* @param int $entry_id
*
* @return object
*/
function weforms_get_entry( $entry_id ) {
global $wpdb;
$cache_key = 'weforms-entry-' . $entry_id;
$entry = wp_cache_get( $cache_key, 'weforms' );
if ( false === $entry ) {
$query = 'SELECT id, form_id, user_id, user_device, referer, INET_NTOA( user_ip ) as ip_address, created_at
FROM ' . $wpdb->weforms_entries .
WHERE id = %d';
$entry = $wpdb->get_row( $wpdb->prepare( $query, $entry_id ) );
wp_cache_set( $cache_key, $entry );
}
return $entry;
}
/**
* Insert a new entry
*
* @param array $args
* @param array $fields
*
* @return WP_Error|int
*/
function weforms_insert_entry( $args, $fields = [] ) {
global $wpdb;
$browser = weforms_get_browser();
$defaults = [
'form_id' => 0,
'user_id' => get_current_user_id(),
'user_ip' => ip2long( weforms_get_client_ip() ),
'user_device' => $browser['name'] . '/' . $browser['platform'],
'referer' => isset( $_SERVER['HTTP_REFERER'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '',
'created_at' => current_time( 'mysql' )
];
$r = wp_parse_args( $args, $defaults );
if ( !$r['form_id'] ) {
return new WP_Error( 'no-form-id', __( 'No form ID was found.', 'weforms' ) );
}
if ( !$fields ) {
return new WP_Error( 'no-fields', __( 'No form fields were found.', 'weforms' ) );
}
$success = $wpdb->insert( $wpdb->weforms_entries, $r );
if ( is_wp_error( $success ) || !$success ) {
return new WP_Error( 'could-not-create', __( 'Could not create an entry', 'weforms' ) );
}
$entry_id = $wpdb->insert_id;
foreach ( $fields as $key => $value ) {
weforms_add_entry_meta( $entry_id, $key, $value );
}
return $entry_id;
}
/**
* Change an entry status
*
* @param int $entry_id
* @param string $status
*
* @return int|bool
*/
function weforms_change_entry_status( $entry_id, $status ) {
global $wpdb;
return $wpdb->update(
$wpdb->weforms_entries,
[
'status' => $status,
],
[
'id' => $entry_id,
],
[ '%s' ],
[ '%d' ]
);
}
/**
* Delete an entry
*
* @param int $entry_id
*
* @return int|bool
*/
function weforms_delete_entry( $entry_id ) {
global $wpdb;
weforms_delete_entry_attachments( $entry_id );
$deleted = $wpdb->delete(
$wpdb->weforms_entries, [
'id' => $entry_id,
], [ '%d' ]
);
if ( $deleted ) {
$wpdb->delete(
$wpdb->weforms_entrymeta, [
'weforms_entry_id' => $entry_id,
], [ '%d' ]
);
}
return $deleted;
}
/**
* Delete attachments of an entry
*
* @param int $entry_id
*
* @since 1.3.5
*/
function weforms_delete_entry_attachments( $entry_id ) {
$entry = weforms_get_entry( $entry_id );
$fields = weforms_get_form_field_labels( $entry->form_id );
if ( !$fields ) {
return false;
}
foreach ( $fields as $meta_key => $field ) {
$value = weforms_get_entry_meta( $entry_id, $meta_key, true );
if ( in_array( $field['type'], [ 'image_upload', 'file_upload' ] ) ) {
if ( is_array( $value ) && $value ) {
foreach ( $value as $attachment_id ) {
wp_delete_attachment( $attachment_id, true );
}
}
}
}
}
/**
* Add meta data field to an entry.
*
* @param int $entry_id entry ID
* @param string $meta_key metadata name
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* Default false.
*
* @return int|false meta ID on success, false on failure
*/
function weforms_add_entry_meta( $entry_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'weforms_entry', $entry_id, $meta_key, $meta_value, $unique );
}
/**
* Retrieve entry meta field for a entry.
*
* @param int $entry_id entry ID
* @param string $key Optional. The meta key to retrieve. By default, returns
* data for all keys. Default empty.
* @param bool $single Optional. Whether to return a single value. Default false.
*
* @return mixed Will be an array if $single is false. Will be value of meta data
* field if $single is true.
*/
function weforms_get_entry_meta( $entry_id, $key = '', $single = false ) {
return get_metadata( 'weforms_entry', $entry_id, $key, $single );
}
/**
* Update entry meta field based on entry ID.
*
* Use the $prev_value parameter to differentiate between meta fields with the
* same key and entry ID.
*
* If the meta field for the entry does not exist, it will be added.
*
* @param int $entry_id entry ID
* @param string $meta_key metadata key
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. Previous value to check before removing.
* Default empty.
*
* @return int|bool meta ID if the key didn't exist, true on successful update,
* false on failure
*/
function weforms_update_entry_meta( $entry_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'weforms_entry', $entry_id, $meta_key, $meta_value, $prev_value );
}
/**
* Delete everything from entry meta matching meta key.
*
* @param string $entry_meta_key key to search for when deleting
*
* @return bool whether the entry meta key was deleted from the database
*/
function weforms_delete_entry_meta_by_key( $meta_key ) {
return delete_metadata( 'weforms_entry', null, $meta_key, '', true );
}
/**
* Retrieve entry meta fields, based on entry ID.
*
* The entry meta fields are retrieved from the cache where possible,
* so the function is optimized to be called more than once.
*
* @since 1.2.0
*
* @param int $entry_id optional
*
* @return array entry meta for the given entry
*/
function weforms_get_entry_custom( $entry_id = 0 ) {
$entry_id = absint( $entry_id );
return weforms_get_entry_meta( $entry_id );
}
/**
* Retrieve meta field names for a entry.
*
* If there are no meta fields, then nothing (null) will be returned.
*
* @param int $entry_id optional
*
* @return array|void array of the keys, if retrieved
*/
function weforms_get_entry_custom_keys( $entry_id = 0 ) {
$custom = weforms_get_entry_custom( $entry_id );
if ( !is_array( $custom ) ) {
return false;
}
if ( $keys = array_keys( $custom ) ) {
return $keys;
}
}
/**
* Get the number of entries count on a form
*
* @param int $form_id
*
* @return int
*/
function weforms_count_form_entries( $form_id, $status = 'publish' ) {
global $wpdb;
return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count(id) FROM ' . $wpdb->weforms_entries . ' WHERE form_id = %d AND status = %s', $form_id, $status ) );
}
/**
* Get the number of entries count on a form
*
* @param int $form_id
*
* @return int
*/
function weforms_count_form_payments( $form_id ) {
global $wpdb;
if ( !class_exists( 'WeForms_Payment' ) ) {
return 0;
}
return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count(id) FROM ' . $wpdb->prefix . 'weforms_payments' . ' WHERE form_id = %d', $form_id ) );
}
/**
* Get the form author details
*
* @param int $form_id
*
* @return array
*/
function weforms_get_form_author_details( $form_id ) {
if ( empty( $form_id ) ) {
return;
}
$author_details = [];
$form = get_post( $form_id );
$author_id = $form->post_author;
$author_details['username'] = get_the_author_meta( 'user_login', $author_id );
$author_details['avatar'] = get_avatar_url( $author_id );
return $author_details;
}
/**
* Get table column heads for a form
*
* For now, return only text type fields
*
* @param int $form_id
*
* @return array
*/
function weforms_get_entry_columns( $form_id, $limit = 6 ) {
$fields = weforms()->form->get( $form_id )->get_fields();
$columns = [];
// filter by input types
if ( $limit ) {
$fields = array_filter( $fields, function ( $item ) {
return in_array( $item['template'], [ 'text_field', 'name_field', 'dropdown_field', 'radio_field', 'email_address', 'url_field', 'column_field' ] );
} );
}
if ( $fields ) {
foreach ( $fields as $field ) {
if ( $field['template'] == 'column_field' ) {
foreach ( $field['inner_fields'] as $c_field ) {
$columns[ $c_field[0]['name'] ] = $c_field[0]['label'];
}
continue;
}
$columns[ $field['name'] ] = $field['label'];
}
}
// if passed 0/false, return all columns
if ( $limit && sizeof( $columns ) > $limit ) {
$columns = array_slice( $columns, 0, $limit ); // max 6 columns
}
return apply_filters( 'weforms_get_entry_columns', $columns, $form_id );
}
/**
* Get table column heads for a form
*
* For now, return only text type fields
*
* @param int $form_id
*
* @return array
*/
function weforms_get_payment_columns( $form_id, $limit = 6 ) {
$fields = weforms()->form->get( $form_id )->get_fields();
$columns = [];
// filter by input types
if ( $limit ) {
$fields = array_filter( $fields, function ( $item ) {
return in_array( $item['template'], [ 'text_field', 'name_field', 'dropdown_field', 'radio_field', 'email_address', 'url_field' ] );
} );
}
if ( $fields ) {
foreach ( $fields as $field ) {
$columns[ $field['name'] ] = $field['label'];
}
}
// if passed 0/false, return all collumns
if ( $limit && sizeof( $columns ) > $limit ) {
$columns = array_slice( $columns, 0, $limit ); // max 6 columns
}
return apply_filters( 'weforms_get_entry_columns', $columns, $form_id );
}
/**
* Get fields from a form by it's meta key
*
* @param int $form_id
*
* @return array
*/
function weforms_get_form_field_labels( $form_id ) {
$fields = weforms()->form->get( $form_id )->get_fields();
$exclude = [ 'step_start', 'section_break', 'recaptcha', 'shortcode', 'action_hook' ];
if ( !$fields ) {
return false;
}
$data = [];
foreach ( $fields as $field ) {
if ( empty( $field['name'] ) ) {
continue;
}
// exclude the fields
if ( in_array( $field['template'], $exclude ) ) {
continue;
}
$data[ $field['name'] ] = [
'label' => $field['label'] ?? '',
'type' => $field['template'],
];
}
return $data;
}
/**
* Get data from an entry
*
* @param int $entry_id
* @param int $form_id
*
* @return false|array
*/
function weforms_get_entry_data( $entry_id ) {
$data = [];
$entry = weforms_get_entry( $entry_id );
$fields = weforms_get_form_field_labels( $entry->form_id );
if ( !$fields ) {
return false;
}
foreach ( $fields as $meta_key => $field ) {
$value = weforms_get_entry_meta( $entry_id, $meta_key, true );
if ( $field['type'] == 'textarea' ) {
$data[ $meta_key ] = weforms_format_text( $value );
} elseif ( $field['type'] == 'name' ) {
$data[ $meta_key ] = implode( ' ', explode( WeForms::$field_separator, $value ) );
} elseif ( in_array( $field['type'], [ 'image_upload', 'file_upload' ] ) ) {
$data[ $meta_key ] = '';
if ( is_array( $value ) && $value ) {
foreach ( $value as $attachment_id ) {
if ( $field['type'] == 'image_upload' ) {
$thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
} else {
$thumb = get_post_field( 'post_title', $attachment_id );
}
$full_size = wp_get_attachment_url( $attachment_id );
$data[ $meta_key ] .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb );
}
}
} elseif ( in_array( $field['type'], [ 'checkbox', 'multiselect' ] ) ) {
$data[ $meta_key ] = explode( WeForms::$field_separator, $value );
} elseif ( $field['type'] == 'map' ) {
list( $lat, $long ) = explode( ',', $value );
$data[ $meta_key ] = [
'lat' => $lat,
'long' => $long,
];
} else {
$data[ $meta_key ] = $value;
}
}
return [
'fields' => $fields,
'data' => $data,
];
}
/**
* Format a text and apply WP function callbacks
*
* @param string $content
*
* @return string
*/
function weforms_format_text( $content ) {
$content = wptexturize( $content );
$content = convert_smilies( $content );
$content = wpautop( $content );
$content = make_clickable( $content );
return $content;
}
/**
* Get User Agent browser and OS type
*
* @return array
*/
function weforms_get_browser() {
$u_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
$bname = 'Unknown';
$platform = 'Unknown';
$version = '';
$ub = '';
// first get the platform
if ( preg_match( '/linux/i', $u_agent ) ) {
$platform = 'Linux';
} elseif ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
$platform = 'MAC OS';
} elseif ( preg_match( '/windows|win32/i', $u_agent ) ) {
$platform = 'Windows';
}
// next get the name of the useragent yes seperately and for good reason
if ( preg_match( '/MSIE/i', $u_agent ) && !preg_match( '/Opera/i', $u_agent ) ) {
$bname = 'Internet Explorer';
$ub = 'MSIE';
} elseif ( preg_match( '/Trident/i', $u_agent ) ) {
// this condition is for IE11
$bname = 'Internet Explorer';
$ub = 'rv';
} elseif ( preg_match( '/Firefox/i', $u_agent ) ) {
$bname = 'Mozilla Firefox';
$ub = 'Firefox';
} elseif ( preg_match( '/Chrome/i', $u_agent ) ) {
$bname = 'Google Chrome';
$ub = 'Chrome';
} elseif ( preg_match( '/Safari/i', $u_agent ) ) {
$bname = 'Apple Safari';
$ub = 'Safari';
} elseif ( preg_match( '/Opera/i', $u_agent ) ) {
$bname = 'Opera';
$ub = 'Opera';
} elseif ( preg_match( '/Netscape/i', $u_agent ) ) {
$bname = 'Netscape';
$ub = 'Netscape';
}
// finally get the correct version number
// Added "|:"
$known = [ 'Version', $ub, 'other' ];
$pattern = '#(?<browser>' . join( '|', $known ) .
')[/|: ]+(?<version>[0-9.|a-zA-Z.]*)#';
if ( !preg_match_all( $pattern, $u_agent, $matches ) ) {
// we have no matching number just continue
}
// see how many we have
$i = count( $matches['browser'] );
if ( $i != 1 ) {
// we will have two since we are not using 'other' argument yet
// see if version is before or after the name
if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
$version = $matches['version'][0];
} else {
$version = $matches['version'][1];
}
} else {
$version = $matches['version'][0];
}
// check if we have a number
if ( $version == null || $version == '' ) {
$version = '';
}
return [
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern,
];
}
/**
* Get form notification merge tags
*
* @since 1.0
*
* @return array
*/
function weforms_get_merge_tags() {
$tags = [
'form' => [
'title' => __( 'Form', 'weforms' ),
'tags' => [
'entry_id' => __( 'Entry ID', 'weforms' ),
'form_id' => __( 'Form ID', 'weforms' ),
'form_name' => __( 'Form Name', 'weforms' ),
],
],
'system' => [
'title' => __( 'System', 'weforms' ),
'tags' => [
'admin_email' => __( 'Site Administrator Email', 'weforms' ),
'date' => __( 'Date', 'weforms' ),
'site_name' => __( 'Site Title', 'weforms' ),
'site_url' => __( 'Site URL', 'weforms' ),
'page_title' => __( 'Embedded Page Title', 'weforms' ),
],
],
'user' => [
'title' => __( 'User', 'weforms' ),
'tags' => [
'ip_address' => __( 'IP Address', 'weforms' ),
'user_id' => __( 'User ID', 'weforms' ),
'first_name' => __( 'First Name', 'weforms' ),
'last_name' => __( 'Last Name', 'weforms' ),
'display_name' => __( 'Display Name', 'weforms' ),
'user_email' => __( 'Email', 'weforms' ),
],
],
'urls' => [
'title' => __( 'URL\'s', 'weforms' ),
'tags' => [
'url_page' => __( 'Embeded Page URL', 'weforms' ),
'url_referer' => __( 'Referer URL', 'weforms' ),
'url_login' => __( 'Login URL', 'weforms' ),
'url_logout' => __( 'Logout URL', 'weforms' ),
'url_register' => __( 'Register URL', 'weforms' ),
'url_lost_password' => __( 'Lost Password URL', 'weforms' ),
'personal_data_erase_confirm_url' => __( 'Personal Data Erase Confirmation URL', 'weforms' ),
'personal_data_export_confirm_url' => __( 'Personal Data Export Confirmation URL', 'weforms' ),
],
],
];
return apply_filters( 'wpuf_cf_merge_tags', $tags );
}
/**
* Record a form view count
*
* @param int $form_id
*
* @return void
*/
function weforms_track_form_view( $form_id ) {
// don't track administrators
if ( current_user_can( 'administrator' ) ) {
return;
}
// ability to turn this off if someone doesn't like this tracking
$is_enabled = apply_filters( 'weforms_track_form_view', true );
if ( !$is_enabled ) {
return;
}
// increase the count
$meta_key = '_weforms_view_count';
$number = (int) get_post_meta( $form_id, $meta_key, true );
update_post_meta( $form_id, $meta_key, ( $number + 1 ) );
}
/**
* Get form view count of a form
*
* @param int $form_id
*
* @return int
*/
function weforms_get_form_views( $form_id ) {
return (int) get_post_meta( $form_id, '_weforms_view_count', true );
}
/**
* Get the weForms global settings
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
function weforms_get_settings( $key = '', $default = '' ) {
$settings = get_option( 'weforms_settings', [] );
$settings = apply_filters( 'weforms_get_settings', $settings );
if ( empty( $key ) ) {
return $settings;
}
if ( isset( $settings[ $key ] ) ) {
return $settings[ $key ];
}
return $default;
}
/**
* Update Settings
*
* @param array $updated_settings
*
* @return array
*/
function weforms_update_settings( $updated_settings = [] ) {
$previuos_settings = weforms_get_settings();
$settings = array_merge( $previuos_settings, $updated_settings );
update_option( 'weforms_settings', $settings );
return $settings;
}
/**
* Form access capability for forms
*
* @since 1.0.5
*
* @return string
*/
function weforms_form_access_capability() {
return apply_filters( 'weforms_form_access_capability', 'manage_options' );
}
/**
* Form access capability for forms
*
* @since 1.2.4
*
* @return string
*/
function weforms_log_file_path() {
return apply_filters( 'weforms_log_file_path', WP_CONTENT_DIR . '/weforms.log' );
}
/**
* Clear the buffer
*
* prevents ajax breakage and endless loading icon. A LIFE SAVER!!!
*
* @since 1.1.0
*
* @return void
*/
function weforms_clear_buffer() {
if ( ob_get_length() > 0 ) {
ob_clean();
}
}
/**
* Get the client IP address
*
* @since 1.1.0
*
* @return string
*/
function weforms_get_client_ip() {
$ipaddress = '';
if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ipaddress = sanitize_text_field( wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ) );
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ipaddress = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) {
$ipaddress = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED'] ) );
} elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) {
$ipaddress = sanitize_text_field( wp_unslash( $_SERVER['HTTP_FORWARDED_FOR'] ) );
} elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
$ipaddress = sanitize_text_field( wp_unslash( $_SERVER['HTTP_FORWARDED'] ) );
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
$ipaddress = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
} else {
$ipaddress = 'UNKNOWN';
}
return $ipaddress;
}
/**
* Save form fields
*
* @since 1.1.0
*
* @param int $form_id
* @param array $field
* @param int $field_id