forked from wp-premium/gravityforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms_model.php
7666 lines (6313 loc) · 238 KB
/
forms_model.php
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
if ( ! class_exists( 'GFForms' ) ) {
die();
}
require_once( ABSPATH . WPINC . '/post.php' );
require_once( 'includes/legacy/forms_model_legacy.php' );
/**
* Class GFFormsModel
*
* Handles database calls and formatting of stored data regarding forms
*/
class GFFormsModel {
/**
* Stores the values containing and uploaded files for later access
*
* @since Unknwon
* @access public
*
* @var array Defaults to an empty array.
*/
public static $uploaded_files = array();
/**
* Stores unique form IDs found.
*
* @since Unknown
* @access public
*
* @var array Defaults to an empty array.
*/
public static $unique_ids = array();
/**
* Stores confirmations found.
*
* @since Unknown
* @access private
*
* @var array Defaults to an empty array.
*/
private static $_confirmations = array();
/**
* An in-memory cache for the form meta for the current blog.
*
* Use "{Blog ID}_{Form ID}" as the key.
*
* @since Unknown
* @access private
* @example $_current_forms['1_2']
*
* @var array $_current_forms
*/
private static $_current_forms = array();
/**
* The entry data for the current site.
*.
* @access private
*
* @var null Defaults to null.
*/
private static $_current_lead = null;
private static $_batch_field_updates = array();
private static $_batch_field_inserts = array();
private static $_batch_field_deletes = array();
/**
* Returns the current database version.
*
* @since 2.2
*
* @return string
*/
public static function get_database_version() {
static $db_version = array();
$blog_id = get_current_blog_id();
if ( empty( $db_version[ $blog_id ] ) ) {
$db_version[ $blog_id ] = get_option( 'gf_db_version' );
}
return $db_version[ $blog_id ];
}
/**
* Flushes the data stored within GFFormsModel::$_current_forms.
*
* @since Unknown
* @access public
*
* @uses GFFormsModel::$_current_forms
*
* @return void
*/
public static function flush_current_forms() {
self::$_current_forms = null;
self::flush_confirmations();
}
/**
* Flushes the data stored within GFFormsModel::$_current_lead.
*
* @since Unknown
* @access public
*
* @uses GFFormsModel::$_current_lead
*
* @return void
*/
public static function flush_current_lead() {
self::$_current_lead = null;
}
/**
* Flushes the data stored within GFFormsModel::$_confirmations
*
* @since Unknown
* @access public
*
* @uses GFFormsModel::$_confirmations
*
* @return void
*/
public static function flush_confirmations() {
self::$_confirmations = null;
}
/**
* Gets the form table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The form table name.
*/
public static function get_form_table_name() {
global $wpdb;
if ( version_compare( self::get_database_version(), '2.3-dev-1', '<' ) ) {
return $wpdb->prefix . 'rg_form';
}
return $wpdb->prefix . 'gf_form';
}
/**
* Gets the form meta table, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The form meta table.
*/
public static function get_meta_table_name() {
global $wpdb;
if ( version_compare( self::get_database_version(), '2.3-dev-1', '<' ) ) {
return $wpdb->prefix . 'rg_form_meta';
}
return $wpdb->prefix . 'gf_form_meta';
}
/**
* Gets the form view table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The form view table name.
*/
public static function get_form_view_table_name() {
global $wpdb;
if ( version_compare( self::get_database_version(), '2.3-dev-1', '<' ) ) {
return $wpdb->prefix . 'rg_form_view';
}
return $wpdb->prefix . 'gf_form_view';
}
/**
* Gets the form revisions table name, including the site's database prefix.
*
* @since 2.4-dev
*
* @return string The form revisions table name.
*/
public static function get_form_revisions_table_name() {
global $wpdb;
return $wpdb->prefix . 'gf_form_revisions';
}
/**
* Gets the lead (entries) table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The lead (entry) table name.
*/
public static function get_lead_table_name() {
return GF_Forms_Model_Legacy::get_lead_table_name();
}
/**
* Gets the lead (entry) meta table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The lead (entry) meta table name.
*/
public static function get_lead_meta_table_name() {
return GF_Forms_Model_Legacy::get_lead_meta_table_name();
}
/**
* Gets the lead (entry) notes table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The lead (entry) notes table name.
*/
public static function get_lead_notes_table_name() {
return GF_Forms_Model_Legacy::get_lead_notes_table_name();
}
/**
* Gets the lead (entry) details table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The lead (entry) details table name.
*/
public static function get_lead_details_table_name() {
return GF_Forms_Model_Legacy::get_lead_details_table_name();
}
/**
* Gets the lead (entry) details long table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The lead (entry) details long table name.
*/
public static function get_lead_details_long_table_name() {
return GF_Forms_Model_Legacy::get_lead_details_long_table_name();
}
/**
* Gets the lead (entry) view table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string The lead (entry) view table name.
*/
public static function get_lead_view_name() {
return GF_Forms_Model_Legacy::get_lead_view_name();
}
/**
* Gets the incomplete submissions table name, including the site's database prefix.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @return string he incomplete submissions table name.
*/
public static function get_incomplete_submissions_table_name() {
return GF_Forms_Model_Legacy::get_incomplete_submissions_table_name();
}
/**
* Gets the entry table name, including the site's database prefix
*
* @access public
* @static
* @global $wpdb
*
* @return string The entry table name
*/
public static function get_entry_table_name() {
global $wpdb;
return $wpdb->prefix . 'gf_entry';
}
/**
* Gets the entry meta table name, including the site's database prefix
*
* @access public
* @static
* @global $wpdb
*
* @return string The entry meta table name
*/
public static function get_entry_meta_table_name() {
global $wpdb;
return $wpdb->prefix . 'gf_entry_meta';
}
/**
* Gets the lead (entry) notes table name, including the site's database prefix
*
* @access public
* @static
* @global $wpdb
*
* @return string The lead (entry) notes table name
*/
public static function get_entry_notes_table_name() {
global $wpdb;
return $wpdb->prefix . 'gf_entry_notes';
}
/**
* Gets the draft submissions table name, including the site's database prefix
*
* @access public
* @static
* @global $wpdb
*
* @return string The draft submissions table name
*/
public static function get_draft_submissions_table_name() {
global $wpdb;
return $wpdb->prefix . 'gf_draft_submissions';
}
/**
* Gets the REST API Key table name, including the site's database prefix
*
* @access public
* @static
* @global $wpdb
*
* @return string The REST API Keys submissions table name
*/
public static function get_rest_api_keys_table_name() {
global $wpdb;
return $wpdb->prefix . 'gf_rest_api_keys';
}
/**
* Gets all forms.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_form_table_name()
* @uses GFFormsModel::get_form_db_columns()
* @uses GFFormsModel::get_entry_count_per_form()
* @uses GFFormsModel::get_view_count_per_form()
*
* @param bool $is_active Optional. Defines if inactive forms should be displayed. Defaults to null.
* @param string $sort_column Optional. The column to be used for sorting the forms. Defaults to 'title'.
* @param string $sort_dir Optional. Defines the direction that sorting should occur. Defaults to 'ASC' (ascending). Use 'DESC' for descending.
* @param bool $is_trash Optional. Defines if forms within the trash should be displayed. Defaults to false.
*
* @return array $forms All forms found.
*/
public static function get_forms( $is_active = null, $sort_column = 'title', $sort_dir = 'ASC', $is_trash = false ) {
global $wpdb;
$form_table_name = self::get_form_table_name();
$where_arr = array();
$where_arr[] = $wpdb->prepare( 'is_trash=%d', $is_trash );
if ( $is_active !== null ) {
$where_arr[] = $wpdb->prepare( 'is_active=%d', $is_active );
}
$where_clause = 'WHERE ' . join( ' AND ', $where_arr );
$sort_keyword = $sort_dir == 'ASC' ? 'ASC' : 'DESC';
$db_columns = self::get_form_db_columns();
if ( ! in_array( strtolower( $sort_column ), $db_columns ) ) {
$sort_column = 'title';
}
$order_by = ! empty( $sort_column ) ? "ORDER BY $sort_column $sort_keyword" : '';
$sql = "SELECT f.id, f.title, f.date_created, f.is_active, 0 as entry_count, 0 view_count
FROM $form_table_name f
$where_clause
$order_by";
//Getting all forms
$forms = $wpdb->get_results( $sql );
//Getting entry count per form
$entry_count = self::get_entry_count_per_form();
//Getting view count per form
$view_count = self::get_view_count_per_form();
//Adding entry counts and to form array
foreach ( $forms as &$form ) {
foreach ( $view_count as $count ) {
if ( $count->form_id == $form->id ) {
$form->view_count = $count->view_count;
break;
}
}
foreach ( $entry_count as $count ) {
if ( $count->form_id == $form->id ) {
$form->entry_count = $count->entry_count;
break;
}
}
}
return $forms;
}
/**
* Searches form titles based on query.
*
* @access public
* @static
* @global $wpdb
* @see GFFormsModel::get_form_table_name
* @see GFFormsModel::get_form_db_columns
* @see GFFormsModel::get_entry_count_per_form
* @see GFFormsModel::get_view_count_per_form
*
* @param string $query Optional. The query to search.
* @param bool $is_active Optional. Defines if inactive forms should be displayed. Defaults to null.
* @param string $sort_column Optional. The column to be used for sorting the forms. Defaults to 'title'.
* @param string $sort_dir Optional. Defines the direction that sorting should occur. Defaults to 'ASC' (ascending). Use 'DESC' for descending.
* @param bool $is_trash Optional. Defines if forms within the trash should be displayed. Defaults to false.
*
* @return array $forms All forms found.
*/
public static function search_forms( $query = '', $is_active = null, $sort_column = 'title', $sort_dir = 'ASC', $is_trash = false ) {
global $wpdb;
$form_table_name = self::get_form_table_name();
$where_arr = array();
$where_arr[] = $wpdb->prepare( 'is_trash=%d', $is_trash );
if ( $is_active !== null ) {
$where_arr[] = $wpdb->prepare( 'is_active=%d', $is_active );
}
if ( ! rgblank( $query ) ) {
$where_arr[] = $wpdb->prepare( 'title LIKE %s', '%' . $query . '%' );
}
$where_clause = 'WHERE ' . join( ' AND ', $where_arr );
$sort_keyword = $sort_dir == 'ASC' ? 'ASC' : 'DESC';
$db_columns = self::get_form_db_columns();
if ( ! in_array( strtolower( $sort_column ), $db_columns ) ) {
$sort_column = 'title';
}
$order_by = ! empty( $sort_column ) ? "ORDER BY $sort_column $sort_keyword" : '';
$sql = "SELECT f.id, f.title, f.date_created, f.is_active, 0 as entry_count, 0 view_count
FROM $form_table_name f
$where_clause
$order_by";
//Getting all forms
$forms = $wpdb->get_results( $sql );
//Getting entry count per form
$entry_count = self::get_entry_count_per_form();
//Getting view count per form
$view_count = self::get_view_count_per_form();
//Adding entry counts and to form array
foreach ( $forms as &$form ) {
foreach ( $view_count as $count ) {
if ( $count->form_id == $form->id ) {
$form->view_count = $count->view_count;
break;
}
}
foreach ( $entry_count as $count ) {
if ( $count->form_id == $form->id ) {
$form->entry_count = $count->entry_count;
break;
}
}
}
return $forms;
}
/**
* Gets the number of entries per form.
*
* First attempts to read from cache. If unavailable, gets the entry count, caches it, and returns it.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_lead_table_name()
* @uses GFCache::get()
* @uses GFCache::set()
*
* @return array $entry_count Array of forms, containing the form ID and the entry count
*/
public static function get_entry_count_per_form() {
if ( version_compare( self::get_database_version(), '2.3-dev-1', '<' ) ) {
return GF_Forms_Model_Legacy::get_entry_count_per_form();
}
global $wpdb;
$entry_table_name = self::get_entry_table_name();
$entry_count = GFCache::get( 'get_entry_count_per_form' );
if ( empty( $entry_count ) ) {
//Getting entry count per form
$sql = "SELECT form_id, count(id) as entry_count FROM $entry_table_name l WHERE status='active' GROUP BY form_id";
$entry_count = $wpdb->get_results( $sql );
GFCache::set( 'get_entry_count_per_form', $entry_count, true, 30 );
}
return $entry_count;
}
/**
* Gets the number of views per form
*
* Checks the cache first. If not there, gets the count from the database, stores it in the cache, and returns it.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_form_view_table_name()
* @uses GFCache::get()
* @uses GFCache::set()
*
* @return array $view_count Array of forms, containing the form ID and the view count
*/
public static function get_view_count_per_form() {
global $wpdb;
$view_table_name = self::get_form_view_table_name();
$view_count = GFCache::get( 'get_view_count_per_form' );
if ( empty( $view_count ) ){
$sql = "SELECT form_id, sum(count) as view_count FROM $view_table_name GROUP BY form_id";
$view_count = $wpdb->get_results( $sql );
GFCache::set( 'get_view_count_per_form', $view_count, true, 30 );
}
return $view_count;
}
/**
* Returns the form database columns.
*
* @since Unknown
* @access public
*
* @return array The column IDs
*/
public static function get_form_db_columns() {
return array( 'id', 'title', 'date_created', 'is_active', 'is_trash' );
}
/**
* Gets the payment totals for a particular form ID.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_lead_table_name()
*
* @param int $form_id The form ID to get payment totals for.
*
* @return array $totals The payment totals found.
*/
public static function get_form_payment_totals( $form_id ) {
global $wpdb;
$entry_table_name = version_compare( self::get_database_version(), '2.3-dev-1', '<' ) ? self::get_lead_table_name() : self::get_entry_table_name();
$sql = $wpdb->prepare(
" SELECT sum(payment_amount) revenue, count(l.id) orders
FROM $entry_table_name l
WHERE form_id=%d AND payment_amount IS NOT null", $form_id
);
$totals = $wpdb->get_row( $sql, ARRAY_A );
$active = $wpdb->get_var(
$wpdb->prepare(
" SELECT count(id) as active
FROM $entry_table_name
WHERE form_id=%d AND payment_status='Active'", $form_id
)
);
if ( empty( $active ) ) {
$active = 0;
}
$totals['active'] = $active;
return $totals;
}
/**
* Gets the total, unread, starred, spam, and trashed entry counts.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_lead_table_name()
* @uses GFFormsModel::get_lead_details_table_name()
*
* @param int $form_id The ID of the form to check.
*
* @return array $results[0] The form counts.
*/
public static function get_form_counts( $form_id ) {
if ( version_compare( self::get_database_version(), '2.3-dev-1', '<' ) ) {
return GF_Forms_Model_Legacy::get_form_counts( $form_id );
}
global $wpdb;
$cache_key = 'form_counts_' . $form_id;
$results = GFCache::get( $cache_key );
if ( ! empty( $results ) ) {
return $results;
}
$entry_table_name = self::get_entry_table_name();
$entry_detail_table_name = self::get_entry_meta_table_name();
$sql = $wpdb->prepare(
"SELECT
(SELECT count(DISTINCT(l.id)) FROM $entry_table_name l INNER JOIN $entry_detail_table_name ld ON l.id=ld.entry_id WHERE l.form_id=%d AND l.status='active') as total,
(SELECT count(DISTINCT(l.id)) FROM $entry_table_name l INNER JOIN $entry_detail_table_name ld ON l.id=ld.entry_id WHERE l.is_read=0 AND l.status='active' AND l.form_id=%d) as unread,
(SELECT count(DISTINCT(l.id)) FROM $entry_table_name l INNER JOIN $entry_detail_table_name ld ON l.id=ld.entry_id WHERE l.is_starred=1 AND l.status='active' AND l.form_id=%d) as starred,
(SELECT count(DISTINCT(l.id)) FROM $entry_table_name l INNER JOIN $entry_detail_table_name ld ON l.id=ld.entry_id WHERE l.status='spam' AND l.form_id=%d) as spam,
(SELECT count(DISTINCT(l.id)) FROM $entry_table_name l INNER JOIN $entry_detail_table_name ld ON l.id=ld.entry_id WHERE l.status='trash' AND l.form_id=%d) as trash",
$form_id, $form_id, $form_id, $form_id, $form_id
);
$wpdb->timer_start();
$results = $wpdb->get_results( $sql, ARRAY_A );
$time_total = $wpdb->timer_stop();
if ( $time_total > 1 ) {
GFCache::set( $cache_key, $results[0], true, 10 * MINUTE_IN_SECONDS );
}
return $results[0];
}
/**
* Gets the form summary for all forms.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_form_table_name()
* @uses GFFormsModel::get_lead_table_name()
*
* @return array $forms Contains the form summary for all forms.
*/
public static function get_form_summary() {
global $wpdb;
$form_table_name = self::get_form_table_name();
$entry_table_name = version_compare( self::get_database_version(), '2.3-dev-1', '<' ) ? self::get_lead_table_name() : self::get_entry_table_name();
$sql = "SELECT l.form_id, count(l.id) as unread_count
FROM $entry_table_name l
WHERE is_read=0 AND status='active'
GROUP BY form_id";
// Getting number of unread and total leads for all forms
$unread_results = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT l.form_id, max(l.date_created) as last_entry_date, count(l.id) as total_entries
FROM $entry_table_name l
WHERE status='active'
GROUP BY form_id";
$lead_date_results = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT id, title, is_trash, '' as last_entry_date, 0 as unread_count
FROM $form_table_name
WHERE is_active=1
ORDER BY title";
$forms = $wpdb->get_results( $sql, ARRAY_A );
for ( $i = 0; $count = sizeof( $forms ), $i < $count; $i ++ ) {
if ( is_array( $unread_results ) ) {
foreach ( $unread_results as $unread_result ) {
if ( $unread_result['form_id'] == $forms[ $i ]['id'] ) {
$forms[ $i ]['unread_count'] = $unread_result['unread_count'];
break;
}
}
}
if ( is_array( $lead_date_results ) ) {
foreach ( $lead_date_results as $entry_date_result ) {
if ( $entry_date_result['form_id'] == $forms[ $i ]['id'] ) {
$forms[ $i ]['last_entry_date'] = $entry_date_result['last_entry_date'];
$forms[ $i ]['total_entries'] = $entry_date_result['total_entries'];
break;
}
}
}
}
/**
* Modifies the summary of all forms, includes unread and total entry counts.
*
* @since 2.4.16
*
* @param array $forms Form summary.
*/
$forms = apply_filters( 'gform_form_summary', $forms );
return $forms;
}
/**
* Gets the total, active, inactive, and trashed form counts.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_form_table_name()
*
* @return array The form counts.
*/
public static function get_form_count() {
global $wpdb;
$form_table_name = self::get_form_table_name();
if ( ! GFCommon::table_exists( $form_table_name ) ) {
return array(
'total' => 0,
'active' => 0,
'inactive' => 0,
'trash' => 0,
);
}
$results = $wpdb->get_results(
"
SELECT
(SELECT count(0) FROM $form_table_name WHERE is_trash = 0) as total,
(SELECT count(0) FROM $form_table_name WHERE is_active=1 AND is_trash = 0 ) as active,
(SELECT count(0) FROM $form_table_name WHERE is_active=0 AND is_trash = 0 ) as inactive,
(SELECT count(0) FROM $form_table_name WHERE is_trash=1) as trash
"
);
return array(
'total' => intval( $results[0]->total ),
'active' => intval( $results[0]->active ),
'inactive' => intval( $results[0]->inactive ),
'trash' => intval( $results[0]->trash ),
);
}
/**
* Gets the form ID based on the form title.
*
* @since Unknown
* @access public
*
* @uses GFFormsModel::get_forms()
*
* @param string $form_title The form title to search for.
*
* @return int The form ID. Returns 0 if not found.
*/
public static function get_form_id( $form_title ) {
$forms = self::get_forms();
foreach ( $forms as $form ) {
$sanitized_name = str_replace( '[', '', str_replace( ']', '', $form->title ) );
if ( $form->title == $form_title || $sanitized_name == $form_title ) {
return $form->id;
}
}
return 0;
}
/**
* Gets a form based on the form ID.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_form_table_name()
*
* @param int $form_id The ID of the form to get.
* @param bool $allow_trash Optional. Set to true to allow trashed results. Defaults to false.
*
* @return bool
*/
public static function get_form( $form_id, $allow_trash = false ) {
global $wpdb;
$table_name = self::get_form_table_name();
$trash_clause = $allow_trash ? '' : 'AND is_trash = 0';
$results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE id=%d {$trash_clause}", $form_id ) );
return isset( $results[0] ) ? $results[0] : false;
}
/**
* Converts a serialized string or JSON for access in PHP.
*
* @since Unknown
* @access public
*
* @param string $string The string to convert.
*
* @return object|array The object that the string was converted to.
*/
public static function unserialize( $string ) {
if ( is_serialized( $string ) ) {
$obj = @unserialize( $string );
} else {
$obj = json_decode( $string, true );
}
return $obj;
}
/**
* Gets the form meta based on the form ID.
*
* @since Unknown
* @access public
* @global $wpdb
*
* @uses GFFormsModel::get_meta_table_name()
* @uses GFFormsModel::unserialize()
* @uses GFFormsModel::convert_field_objects()
* @uses GFFormsModel::load_notifications_from_legacy()
* @uses GFFormsModel::$_current_forms
*
* @param int $form_id The form ID.
*
* @return array|null $form Form object if found. Null if not found.
*/
public static function get_form_meta( $form_id ) {
global $wpdb;
$form_id = absint( $form_id );
$key = get_current_blog_id() . '_' . $form_id;
// Return cached version if form meta has been previously retrieved for this form
if ( isset( self::$_current_forms[ $key ] ) ) {
return self::$_current_forms[ $key ];
}
$table_name = self::get_meta_table_name();
$form_row = $wpdb->get_row( $wpdb->prepare( "SELECT display_meta, notifications FROM {$table_name} WHERE form_id=%d", $form_id ), ARRAY_A );
// Loading main form object (supports serialized strings as well as JSON strings)
$form = self::unserialize( rgar( $form_row, 'display_meta' ) );
if ( ! $form ) {
return null;
}
// Ensure the fields property is in the correct format, an associative array will cause warnings and js errors in the form editor.
if ( isset( $form['fields'] ) && is_array( $form['fields'] ) ) {
$form['fields'] = array_values( $form['fields'] );
}
// Loading notifications
$form['notifications'] = self::unserialize( $form_row['notifications'] );
// Creating field objects and copying some form variables down to fields for easier access
$form = self::convert_field_objects( $form );
// Loading confirmations from legacy structure into new structure
$form = self::load_confirmations( $form );
//only migrate legacy notification if there isn't any notification configured in new structure
if ( ! isset( $form['notifications'] ) ) {
$form = self::load_notifications_from_legacy( $form ); // Moving notification data from legacy structure into new 'notifications' array
}
// Load notifications to legacy structure to maintain backward compatibility with legacy hooks and functions
$form = self::load_notifications_to_legacy( $form );
// Ensure the next field ID is set correctly.
$form['nextFieldId'] = self::get_next_field_id( $form['fields'] );
/**
* Filters the Form object after the form meta is obtained
*
* @param array $form The Form object
*/
$form = gf_apply_filters( array( 'gform_form_post_get_meta', $form_id ), $form );
// Cached form meta for cheaper retrieval on subsequent requests
self::$_current_forms[ $key ] = $form;
return $form;
}
/**
* Recursively checks the highest ID for all the fields in the form and then returns the highest ID + 1.
*
* @since 2.4.6.12
*
* @param GF_Field[] $fields
* @param int $next_field_id
*
* @return int
*/
public static function get_next_field_id( $fields, $next_field_id = 1 ) {
foreach ( $fields as $field ) {
if ( is_array( $field->fields ) ) {
$next_field_id = self::get_next_field_id( $field->fields, $next_field_id );
}
if ( $field->id >= $next_field_id ) {
$next_field_id = $field->id + 1;
}
}
return (int) $next_field_id;
}
/**
* Converts all field objects in a form, based on field type.
*
* @since Unknown
* @access public
*
* @uses GF_Field_CreditCard::maybe_upgrade_inputs()
*
* @param array $form The Form object.
*
* @return array $form The Form object after the field objects are converted.
*/
public static function convert_field_objects( $form ) {
$page_number = 1;
if ( is_array( rgar( $form, 'fields' ) ) ) {
foreach ( $form['fields'] as &$field ) {