-
Notifications
You must be signed in to change notification settings - Fork 22
/
form_display.php
5499 lines (4534 loc) · 210 KB
/
form_display.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();
}
class GFFormDisplay {
public static $submission = array();
public static $init_scripts = array();
public static $hooks_js_printed = false;
public static $sidebar_has_widget = false;
public static $submission_initiated_by = '';
public static $processed = array();
const ON_PAGE_RENDER = 1;
const ON_CONDITIONAL_LOGIC = 2;
const SUBMISSION_INITIATED_BY_WEBFORM = 1;
const SUBMISSION_INITIATED_BY_API = 2;
const SUBMISSION_INITIATED_BY_API_VALIDATION = 3;
const SUBMISSION_METHOD_POSTBACK = 'postback';
const SUBMISSION_METHOD_AJAX = 'ajax';
const SUBMISSION_METHOD_IFRAME = 'iframe';
const SUBMISSION_METHOD_CUSTOM = 'custom';
/**
* Starting point for the form submission process. Handles the following tasks: Form validation, save for later logic, entry creation, notification and confirmation.
*
* @since unknown
* @since 2.6.4 Added the $initiated_by param.
*
* @param int $form_id The form ID being submitted.
* @param int $initiated_by What process initiated the form submission. Possible options are self::SUBMISSION_INITIATED_BY_WEBFORM = 1 or self::SUBMISSION_INITIATED_BY_API = 2.
*/
public static function process_form( $form_id, $initiated_by = self::SUBMISSION_INITIATED_BY_API ) {
GFCommon::timer_start( __METHOD__ );
GFCommon::log_debug( "GFFormDisplay::process_form(): Starting to process form (#{$form_id}) submission." );
self::$submission_initiated_by = $initiated_by;
$form = GFAPI::get_form( $form_id );
$gform_pre_process_args = array( 'gform_pre_process', $form_id );
if ( gf_has_filter( $gform_pre_process_args ) ) {
GFCommon::log_debug( __METHOD__ . '(): Executing functions hooked to gform_pre_process.' );
/**
* Filter the form before GF begins to process the submission.
*
* @param array $form The Form Object
*/
$filtered_form = gf_apply_filters( array( 'gform_pre_process', $form['id'] ), $form );
if ( $filtered_form !== null ) {
$form = $filtered_form;
}
GFCommon::log_debug( __METHOD__ . '(): Completed gform_pre_process.' );
}
// Set files that have been uploaded to temp folder
$files = GFFormsModel::set_uploaded_files( $form_id );
//reading form metadata
$form = self::maybe_add_review_page( $form );
if ( ! rgar( $form, 'is_active' ) ) {
GFCommon::log_debug( __METHOD__ . '(): Aborting. Form is inactive.' );
return;
}
if ( rgar( $form, 'is_trash' ) ) {
GFCommon::log_debug( __METHOD__ . '(): Aborting. Form has been trashed.' );
return;
}
if ( GFCommon::form_requires_login( $form ) ) {
if ( ! is_user_logged_in() ) {
GFCommon::log_debug( __METHOD__ . '(): Aborting. User is not logged in.' );
return;
}
// Bypass nonce check for requests coming from the REST API
$is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
if ( ! $is_rest_request ) {
check_admin_referer( 'gform_submit_' . $form_id, '_gform_submit_nonce_' . $form_id );
}
}
$lead = array();
$field_values = RGForms::post( 'gform_field_values' );
$confirmation_message = '';
$source_page_number = self::get_source_page( $form_id );
$page_number = $source_page_number;
$target_page = self::get_target_page( $form, $page_number, $field_values );
GFCommon::log_debug( "GFFormDisplay::process_form(): Source page number: {$source_page_number}. Target page number: {$target_page}." );
$saving_for_later = rgpost( 'gform_save' ) ? true : false;
$is_valid = true;
$failed_validation_page = $page_number;
//don't validate when going to previous page or saving for later
if ( ! $saving_for_later && ( empty( $target_page ) || $target_page >= $page_number ) ) {
$is_valid = self::validate( $form, $field_values, $page_number, $failed_validation_page );
}
$log_is_valid = $is_valid ? 'Yes' : 'No';
GFCommon::log_debug( __METHOD__ . "(): After validation. Is submission valid? {$log_is_valid}." );
// Upload files to temp folder when going to the next page or when submitting the form and it failed validation
if ( $target_page > $page_number || $target_page == 0 ) {
if ( ! empty( $_FILES ) && ! $saving_for_later ) {
// When saving, ignore files with single file upload fields as they have not been validated.
GFCommon::log_debug( 'GFFormDisplay::process_form(): Uploading files...' );
// Uploading files to temporary folder.
$files = self::upload_files( $form, $files );
RGFormsModel::$uploaded_files[ $form_id ] = $files;
}
}
// Load target page if it did not fail validation or if going to the previous page
if ( ! $saving_for_later && $is_valid ) {
$page_number = $target_page;
} else {
$page_number = $failed_validation_page;
}
$confirmation = '';
if ( ( $is_valid && $page_number == 0 ) || $saving_for_later ) {
// Make sure submit button isn't hidden by conditional logic.
if ( GFFormsModel::is_submit_button_hidden( $form ) && ! $saving_for_later ) {
// Ignore submission.
return;
}
$ajax = isset( $_POST['gform_ajax'] );
/**
* Adds support for aborting submission, displaying the confirmation page/text to the user. This filter is useful for Spam Filters that want to abort submissions that flagged as spam.
*
* @since 2.7
*
* @see https://docs.gravityforms.com/gform_abort_submission_with_confirmation/
*
* @param bool $do_abort The value being filtered. True to abort submission and display the confirmation. False to continue with submission. Defaults to false.
* @param array $form The current form object.
*/
$abort_with_confirmation = gf_apply_filters( array( 'gform_abort_submission_with_confirmation', $form['id'] ), false, $form );
if ( $abort_with_confirmation ) {
GFCommon::log_debug( 'GFFormDisplay::process_form(): Aborting early via gform_abort_submission_with_confirmation filter.' );
// Display confirmation but doesn't process the form. Useful for spam filters.
$confirmation = self::handle_confirmation( $form, $lead, $ajax );
$is_valid = false;
} elseif ( ! $saving_for_later ) {
GFCommon::log_debug( 'GFFormDisplay::process_form(): Submission is valid. Moving forward.' );
$gform_pre_submission_args = array( 'gform_pre_submission', $form_id );
if ( gf_has_action( $gform_pre_submission_args ) ) {
GFCommon::log_debug( __METHOD__ . '(): Executing functions hooked to gform_pre_submission.' );
/**
* Fires before form submission is handled
*
* Typically used to modify values before the submission is processed.
*
* @since 1.0
*
* @param array $form The Form object
*/
gf_do_action( $gform_pre_submission_args, $form );
GFCommon::log_debug( __METHOD__ . '(): Completed gform_pre_submission.' );
}
$gform_pre_submission_filter_args = array( 'gform_pre_submission_filter', $form_id );
if ( gf_has_filter( $gform_pre_submission_filter_args ) ) {
GFCommon::log_debug( __METHOD__ . '(): Executing functions hooked to gform_pre_submission_filter.' );
/**
* Allows the form object to be modified before the entry is saved.
*
* @since Unknown.
*
* @param array $form The form currently being processed.
*/
$form = gf_apply_filters( $gform_pre_submission_filter_args, $form );
GFCommon::log_debug( __METHOD__ . '(): Completed gform_pre_submission_filter.' );
}
$confirmation = self::handle_submission( $form, $lead, $ajax );
$gform_after_submission_args = array( 'gform_after_submission', $form_id );
if ( gf_has_action( $gform_after_submission_args ) ) {
GFCommon::log_debug( __METHOD__ . '(): Executing functions hooked to gform_after_submission.' );
/**
* Allows additional actions to be performed after successful form submission.
*
* @since 1.6
*
* @param array $lead The Entry object.
* @param array $form The Form object.
*/
gf_do_action( $gform_after_submission_args, $lead, $form );
GFCommon::log_debug( __METHOD__ . '(): Completed gform_after_submission.' );
}
} elseif ( $saving_for_later ) {
GFCommon::log_debug( 'GFFormDisplay::process_form(): Saving for later.' );
$lead = GFFormsModel::get_current_lead();
$form = self::update_confirmation( $form, $lead, 'form_saved' );
$confirmation = rgar( $form['confirmation'], 'message' );
$nl2br = rgar( $form['confirmation'], 'disableAutoformat' ) ? false : true;
$confirmation = GFCommon::replace_variables( $confirmation, $form, $lead, false, true, $nl2br );
$form_unique_id = GFFormsModel::get_form_unique_id( $form_id );
$ip = rgars( $form, 'personalData/preventIP' ) ? '' : GFFormsModel::get_ip();
$source_url = GFFormsModel::get_current_page_url();
$source_url = esc_url_raw( $source_url );
$resume_token = rgpost( 'gform_resume_token' );
$resume_token = sanitize_key( $resume_token );
$resume_token = GFFormsModel::save_draft_submission( $form, $lead, $field_values, $page_number, $files, $form_unique_id, $ip, $source_url, $resume_token );
$notifications_to_send = GFCommon::get_notifications_to_send( 'form_saved', $form, $lead );
$log_notification_event = empty( $notifications_to_send ) ? 'No notifications to process' : 'Processing notifications';
GFCommon::log_debug( "GFFormDisplay::process_form(): {$log_notification_event} for form_saved event." );
foreach ( $notifications_to_send as $notification ) {
if ( isset( $notification['isActive'] ) && ! $notification['isActive'] ) {
GFCommon::log_debug( "GFFormDisplay::process_form(): Notification is inactive, not processing notification (#{$notification['id']} - {$notification['name']})." );
continue;
}
$notification['message'] = self::replace_save_variables( $notification['message'], $form, $resume_token );
GFCommon::send_notification( $notification, $form, $lead );
}
self::set_submission_if_null( $form_id, 'saved_for_later', true );
self::set_submission_if_null( $form_id, 'resume_token', $resume_token );
GFCommon::log_debug( 'GFFormDisplay::process_form(): Saved incomplete submission.' );
}
/**
* Allows the confirmation redirect header to be suppressed. Required by GFAPI::submit_form().
*
* @since 2.3
*
* @param bool $suppress_redirect
*/
$suppress_redirect = apply_filters( 'gform_suppress_confirmation_redirect', false );
if ( is_array( $confirmation ) && isset( $confirmation['redirect'] ) && ! $suppress_redirect ) {
header( "Location: {$confirmation["redirect"]}" );
$gform_post_submission_args = array( 'gform_post_submission', $form_id );
if ( gf_has_action( $gform_post_submission_args ) ) {
GFCommon::log_debug( __METHOD__ . '(): Executing functions hooked to gform_post_submission.' );
/**
* Allows additional actions to be performed after form submission when the confirmation is a redirect.
*
* @param array $lead The Entry object.
* @param array $form The Form object.
*/
gf_do_action( $gform_post_submission_args, $lead, $form );
GFCommon::log_debug( __METHOD__ . '(): Completed gform_post_submission.' );
}
GFCommon::log_debug( __METHOD__ . sprintf( '(): Processing completed in %F seconds.', GFCommon::timer_end( __METHOD__ ) ) );
exit;
}
}
if ( ! isset( self::$submission[ $form_id ] ) ) {
self::$submission[ $form_id ] = array();
}
self::set_submission_if_null( $form_id, 'is_valid', $is_valid );
self::set_submission_if_null( $form_id, 'form', $form );
self::set_submission_if_null( $form_id, 'lead', $lead );
self::set_submission_if_null( $form_id, 'confirmation_message', $confirmation );
self::set_submission_if_null( $form_id, 'page_number', $page_number );
self::set_submission_if_null( $form_id, 'source_page_number', $source_page_number );
$gform_post_process_args = array( 'gform_post_process', $form_id );
if ( gf_has_action( $gform_post_process_args ) ) {
GFCommon::log_debug( __METHOD__ . '(): Executing functions hooked to gform_post_process.' );
/**
* Fires after the form processing is completed. Form processing happens when submitting a page on a multi-page form (i.e. going to the "Next" or "Previous" page), or
* when submitting a single page form.
*
* @param array $form The Form Object
* @param int $page_number In a multi-page form, this variable contains the current page number.
* @param int $source_page_number In a multi-page form, this parameters contains the number of the page that the submission came from.
* For example, when clicking "Next" on page 1, this parameter will be set to 1. When clicking "Previous" on page 2, this parameter will be set to 2.
*/
gf_do_action( $gform_post_process_args, $form, $page_number, $source_page_number );
GFCommon::log_debug( __METHOD__ . '(): Completed gform_post_process.' );
}
GFCommon::log_debug( __METHOD__ . sprintf( '(): Processing completed in %F seconds.', GFCommon::timer_end( __METHOD__ ) ) );
}
/**
* Get form object and insert review page, if necessary.
*
* @since 2.1.1.25 Added $partial_entry parameter.
* @since 1.9.15
*
* @param array $form The current Form object.
* @param array $partial_entry The partial entry from the resumed incomplete submission. Defaults to an empty array.
*
* @return array The form object.
*/
public static function maybe_add_review_page( $form, $partial_entry = array() ) {
/* Setup default review page parameters. */
$review_page = array(
'content' => '',
'cssClass' => '',
'is_enabled' => false,
'nextButton' => array(
'type' => 'text',
'text' => __( 'Review Form', 'gravityforms' ),
'imageUrl' => '',
'imageAlt' => '',
),
'previousButton' => array(
'type' => 'text',
'text' => __( 'Previous', 'gravityforms' ),
'imageUrl' => '',
'imageAlt' => '',
),
);
$gform_review_page_args = array( 'gform_review_page', rgar( $form, 'id' ) );
if ( gf_has_filter( $gform_review_page_args ) ) {
if ( empty( $partial_entry ) ) {
// Prepare partial entry for review page.
$partial_entry = GFFormsModel::get_current_lead();
}
/**
* GFFormsModel::create_lead() caches the field value and conditional logic visibility which can create
* issues when 3rd parties use hooks later in the process to modify the form. Let's flush the cache avoid
* any weirdness.
*/
GFCache::flush();
GFCommon::log_debug( __METHOD__ . '(): Executing functions hooked to gform_review_page.' );
/**
* A filter for setting up the review page.
*
* @since 2.4.5
*
* @param array $review_page The review page parameters
* @param array $form The current form object
* @param array|false $partial_entry The partial entry for the form or false on initial form display.
*/
$review_page = gf_apply_filters( $gform_review_page_args, $review_page, $form, $partial_entry );
GFCommon::log_debug( __METHOD__ . '(): Completed gform_review_page.' );
if ( ! rgempty( 'button_text', $review_page ) ) {
$review_page['nextButton']['text'] = $review_page['button_text'];
}
}
if ( rgar( $review_page, 'is_enabled' ) ) {
$form = self::insert_review_page( $form, $review_page );
}
return $form;
}
private static function set_submission_if_null( $form_id, $key, $val ) {
if ( ! isset( self::$submission[ $form_id ][ $key ] ) ) {
self::$submission[ $form_id ][ $key ] = $val;
}
}
private static function upload_files( $form, $files ) {
$form_upload_path = GFFormsModel::get_upload_path( $form['id'] );
GFCommon::log_debug( "GFFormDisplay::upload_files(): Upload path {$form_upload_path}" );
//Creating temp folder if it does not exist
$target_path = $form_upload_path . '/tmp/';
if ( ! is_dir( $target_path ) && wp_mkdir_p( $target_path ) ) {
GFCommon::recursive_add_index_file( $target_path );
}
foreach ( $form['fields'] as $field ) {
$input_name = "input_{$field->id}";
//skip fields that are not file upload fields or that don't have a file to be uploaded or that have failed validation
$input_type = RGFormsModel::get_input_type( $field );
if ( ! in_array( $input_type, array( 'fileupload', 'post_image' ) ) || $field->multipleFiles ) {
continue;
}
/*if ( $field->failed_validation || empty( $_FILES[ $input_name ]['name'] ) ) {
GFCommon::log_debug( "GFFormDisplay::upload_files(): Skipping field: {$field->label}({$field->id} - {$field->type})." );
continue;
}*/
if ( $field->failed_validation ) {
GFCommon::log_debug( "GFFormDisplay::upload_files(): Skipping field because it failed validation: {$field->label}({$field->id} - {$field->type})." );
continue;
}
if ( empty( $_FILES[ $input_name ]['name'] ) ) {
GFCommon::log_debug( "GFFormDisplay::upload_files(): Skipping field because a file could not be found: {$field->label}({$field->id} - {$field->type})." );
continue;
}
$file_name = $_FILES[ $input_name ]['name'];
if ( GFCommon::file_name_has_disallowed_extension( $file_name ) ) {
GFCommon::log_debug( __METHOD__ . "(): Illegal file extension: {$file_name}" );
continue;
}
$allowed_extensions = ! empty( $field->allowedExtensions ) ? GFCommon::clean_extensions( explode( ',', strtolower( $field->allowedExtensions ) ) ) : array();
if ( ! empty( $allowed_extensions ) ) {
if ( ! GFCommon::match_file_extension( $file_name, $allowed_extensions ) ) {
GFCommon::log_debug( __METHOD__ . "(): The uploaded file type is not allowed: {$file_name}" );
continue;
}
}
/**
* Allows the disabling of file upload whitelisting
*
* @param bool false Set to 'true' to disable whitelisting. Defaults to 'false'.
*/
$whitelisting_disabled = apply_filters( 'gform_file_upload_whitelisting_disabled', false );
if ( empty( $allowed_extensions ) && ! $whitelisting_disabled ) {
// Whitelist the file type
$valid_file_name = GFCommon::check_type_and_ext( $_FILES[ $input_name ], $file_name );
if ( is_wp_error( $valid_file_name ) ) {
GFCommon::log_debug( __METHOD__ . "(): The uploaded file type is not allowed: {$file_name}" );
continue;
}
}
$file_info = RGFormsModel::get_temp_filename( $form['id'], $input_name );
GFCommon::log_debug( 'GFFormDisplay::upload_files(): Temp file info: ' . print_r( $file_info, true ) );
if ( $file_info && move_uploaded_file( $_FILES[ $input_name ]['tmp_name'], $target_path . $file_info['temp_filename'] ) ) {
GFFormsModel::set_permissions( $target_path . $file_info['temp_filename'] );
$files[ $input_name ] = $file_info['uploaded_filename'];
GFCommon::log_debug( "GFFormDisplay::upload_files(): File uploaded successfully: {$file_info['uploaded_filename']}" );
} else {
GFCommon::log_error( "GFFormDisplay::upload_files(): File could not be uploaded: tmp_name: {$_FILES[ $input_name ]['tmp_name']} - target location: " . $target_path . $file_info['temp_filename'] );
}
}
return $files;
}
public static function get_state( $form, $field_values ) {
$fields = array();
foreach ( $form['fields'] as $field ) {
/* @var GF_Field $field */
if ( $field->is_state_validation_supported() ) {
$value = RGFormsModel::get_field_value( $field, $field_values, false );
$value = $field->get_value_default_if_empty( $value );
switch ( $field->get_input_type() ) {
case 'calculation' :
case 'singleproduct' :
case 'hiddenproduct' :
$price = ! is_array( $value ) || empty( $value[ $field->id . '.2' ] ) ? $field->basePrice : $value[ $field->id . '.2' ];
if ( empty( $price ) ) {
$price = 0;
}
$price = GFCommon::to_number( $price );
$product_name = ! is_array( $value ) || empty( $value[ $field->id . '.1' ] ) ? $field->label : $value[ $field->id . '.1' ];
$fields[ $field->id . '.1' ] = wp_hash( GFFormsModel::maybe_trim_input( $product_name, $form['id'], $field ) );
$fields[ $field->id . '.2' ] = wp_hash( GFFormsModel::maybe_trim_input( $price, $form['id'], $field ) );
break;
case 'singleshipping' :
$price = ! empty( $value ) ? $value : $field->basePrice;
$price = ! empty( $price ) ? GFCommon::to_number( $price ) : 0;
$fields[ $field->id ] = wp_hash( GFFormsModel::maybe_trim_input( $price, $form['id'], $field ) );
break;
case 'radio' :
case 'select' :
$fields[ $field->id ] = array();
foreach ( $field->choices as $choice ) {
$field_value = ! empty( $choice['value'] ) || $field->enableChoiceValue ? $choice['value'] : $choice['text'];
if ( $field->enablePrice ) {
$price = rgempty( 'price', $choice ) ? 0 : GFCommon::to_number( rgar( $choice, 'price' ) );
$field_value .= '|' . $price;
}
$fields[ $field->id ][] = wp_hash( GFFormsModel::maybe_trim_input( $field_value, $form['id'], $field ) );
}
break;
case 'checkbox' :
$index = 1;
foreach ( $field->choices as $choice ) {
$field_value = ! empty( $choice['value'] ) || $field->enableChoiceValue ? $choice['value'] : $choice['text'];
if ( $field->enablePrice ) {
$price = rgempty( 'price', $choice ) ? 0 : GFCommon::to_number( rgar( $choice, 'price' ) );
$field_value .= '|' . $price;
}
// Checkboxes for Choice fields already have a unique ID.
if ( $field->has_persistent_choices() ) {
$input_id = $field->get_input_id_from_choice_key( $choice['key'] );
$fields[ $input_id ] = wp_hash( GFFormsModel::maybe_trim_input( $field_value, $form['id'], $field ) );
} else {
if ( $index % 10 == 0 ) { //hack to skip numbers ending in 0. so that 5.1 doesn't conflict with 5.10
$index ++;
}
$fields[ $field->id . '.' . $index ++ ] = wp_hash( GFFormsModel::maybe_trim_input( $field_value, $form['id'], $field ) );
}
}
break;
case 'consent':
$text = $field->checkboxLabel;
$description = GFFormsModel::get_latest_form_revisions_id( $form['id'] );
$fields[ $field->id . '.1' ] = wp_hash( 1 );
$fields[ $field->id . '.2' ] = wp_hash( GFFormsModel::maybe_trim_input( $text, $form['id'], $field ) );
$fields[ $field->id . '.3' ] = wp_hash( GFFormsModel::maybe_trim_input( $description, $form['id'], $field ) );
break;
}
}
}
$hash = json_encode( $fields );
$checksum = wp_hash( crc32( $hash ) );
return base64_encode( json_encode( array( $hash, $checksum ) ) );
}
/**
* Determine if form has any pages.
*
* @access private
*
* @param array $form The form object
*
* @return bool If form object has any pages
*/
private static function has_pages( $form ) {
return GFCommon::has_pages( $form );
}
private static function has_character_counter( $form ) {
if ( ! is_array( $form['fields'] ) ) {
return false;
}
foreach ( $form['fields'] as $field ) {
if ( $field->maxLength && ! $field->inputMask ) {
return true;
}
}
return false;
}
private static function has_placeholder( $form ) {
if ( ! is_array( $form['fields'] ) ) {
return false;
}
foreach ( $form['fields'] as $field ) {
if ( $field->placeholder != '' ) {
return true;
}
if ( is_array( $field->inputs ) ) {
foreach ( $field->inputs as $input ) {
if ( rgar( $input, 'placeholder' ) != '' ) {
return true;
}
}
}
}
return false;
}
private static function has_enhanced_dropdown( $form ) {
if ( ! is_array( $form['fields'] ) ) {
return false;
}
foreach ( $form['fields'] as $field ) {
if ( in_array( RGFormsModel::get_input_type( $field ), array( 'select', 'multiselect' ) ) && $field->enableEnhancedUI ) {
return true;
}
}
return false;
}
private static function has_password_strength( $form ) {
if ( ! is_array( $form['fields'] ) ) {
return false;
}
foreach ( $form['fields'] as $field ) {
if ( $field->type == 'password' && $field->passwordStrengthEnabled ) {
return true;
}
}
return false;
}
/**
* Determines if form has a Password field with the Password Visibility Toggle enabled.
*
* @since 2.4.15
*
* @param array $form Form object.
*
* @return bool
*/
private static function has_password_visibility( $form ) {
if ( ! is_array( $form['fields'] ) ) {
return false;
}
foreach ( $form['fields'] as $field ) {
if ( $field->type == 'password' && $field->passwordVisibilityEnabled ) {
return true;
}
}
return false;
}
private static function has_other_choice( $form ) {
if ( ! is_array( $form['fields'] ) ) {
return false;
}
foreach ( $form['fields'] as $field ) {
if ( $field->type == 'radio' && $field->enableOtherChoice ) {
return true;
}
}
return false;
}
public static function get_target_page( $form, $current_page, $field_values ) {
$page_number = RGForms::post( "gform_target_page_number_{$form['id']}" );
$page_number = ! is_numeric( $page_number ) ? 1 : $page_number;
// cast to an integer since page numbers can only be whole numbers
$page_number = absint( $page_number );
$direction = $page_number >= $current_page ? 1 : - 1;
//Finding next page that is not hidden by conditional logic
while ( RGFormsModel::is_page_hidden( $form, $page_number, $field_values ) ) {
$page_number += $direction;
}
//If all following pages are hidden, submit the form
if ( $page_number > self::get_max_page_number( $form ) ) {
$page_number = 0;
}
/**
* Modify the target page.
*
* @since 2.1.2.13
*
* @see https://docs.gravityforms.com/gform_target_page/
*
* @param int $page_number The target page number.
* @param array $form The current form object.
* @param int $current_page The page that was submitted.
* @param array $field_values Dynamic population values that were provided when loading the form.
*/
return gf_apply_filters( array( 'gform_target_page', $form['id'] ), $page_number, $form, $current_page, $field_values );
}
public static function get_source_page( $form_id ) {
$page_number = RGForms::post( "gform_source_page_number_{$form_id}" );
return ! is_numeric( $page_number ) ? 1 : $page_number;
}
public static function set_current_page( $form_id, $page_number ) {
self::$submission[ $form_id ]['page_number'] = $page_number;
}
public static function get_current_page( $form_id ) {
$page_number = isset( self::$submission[ $form_id ] ) ? intval( self::$submission[ $form_id ]['page_number'] ) : 1;
return $page_number;
}
private static function is_page_active( $form_id, $page_number ) {
return intval( self::get_current_page( $form_id ) ) == intval( $page_number );
}
/**
* Determine if the last page for the current form object is being submitted or rendered (depending on the provided $mode).
*
* @param array $form A Gravity Forms form object.
* @param string $mode Mode to check for: 'submit' or 'render'
*
* @return boolean
*/
public static function is_last_page( $form, $mode = 'submit' ) {
$page_number = self::get_source_page( $form['id'] );
$field_values = GFForms::post( 'gform_field_values' );
$target_page = self::get_target_page( $form, $page_number, $field_values );
if ( $mode == 'render' ) {
$is_valid = rgars( self::$submission, "{$form['id']}/is_valid" );
$is_last_page = $is_valid && $target_page == self::get_max_page_number( $form );
} else {
$is_last_page = (string) $target_page === '0';
}
return $is_last_page;
}
/**
* Returns the entry limit date range for the given period.
*
* @since unknown
* @since 2.4.15 Updated the day period to use the local time.
*
* @param string $period The eriod for the entry limit.
*
* @return array
*/
private static function get_limit_period_dates( $period ) {
if ( empty( $period ) ) {
return array( 'start_date' => null, 'end_date' => null );
}
switch ( $period ) {
case 'day':
return array(
'start_date' => current_time( 'Y-m-d' ),
'end_date' => current_time( 'Y-m-d 23:59:59' ),
);
break;
case 'week':
return array(
'start_date' => gmdate( 'Y-m-d', strtotime( 'Monday this week' ) ),
'end_date' => gmdate( 'Y-m-d 23:59:59', strtotime( 'Sunday this week' ) ),
);
break;
case 'month':
$month_start = gmdate( 'Y-m-1');
return array(
'start_date' => $month_start,
'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( "{$month_start} +1 month -1 second" ) ),
);
break;
case 'year':
return array(
'start_date' => gmdate( 'Y-1-1' ),
'end_date' => gmdate( 'Y-12-31 23:59:59' ),
);
break;
}
}
/**
* Get the slug for the form's theme
*
* @since 2.7
*
* @param $form
*
* @return string The theme slug
*/
public static function get_form_theme_slug( $form ) {
$form = (array) $form;
$slug = '';
$is_wp_dashboard = is_admin() && ! defined( 'DOING_AJAX' );
// If form is legacy, return that early to avoid calculating orbital styles.
if ( GFCommon::is_legacy_markup_enabled( $form ) ) {
$slug = 'legacy';
} elseif ( $is_wp_dashboard || GFCommon::is_preview() ) {
$slug = 'gravity-theme';
} elseif ( GFCommon::is_form_editor() ) {
$slug = 'orbital';
} elseif ( isset( $form['theme'] ) ) {
$slug = $form['theme'];
} else {
$instance = rgar( $form, 'page_instance', 0 );
$all_blocks = apply_filters( 'gform_form_block_attribute_values', array() );
$block_settings = rgars( $all_blocks, rgar( $form, 'id', 0 ) . '/' . $instance, array() );
// If a theme is selected for this block or shortcode, return that.
if ( isset( $block_settings['theme'] ) ) {
$slug = $block_settings['theme'];
}
}
// allow using the short version in shortcodes.
if ( $slug == 'gravity' ) {
$slug = 'gravity-theme';
}
if ( empty( $slug ) || ! in_array( $slug, array( 'legacy', 'gravity-theme', 'orbital' ) ) ) {
$slug = GFForms::get_default_theme();
}
/**
* Allow users to filter the theme slug returned for a given form.
*
* @since 2.7
*
* @param string $slug The current theme slug for the form.
* @param array $form The form object being processed.
*
* @return string
*/
return apply_filters( 'gform_form_theme_slug', $slug, $form );
}
/**
* Determines which themes to enqueue based on the form context and current page.
*
* @since 2.9
*
* @param array $form Current form object.
* @param string|array $field_types Optional. If specified, only load themes for forms with fields of these types. Can be a string with a single field type or an array of strings with multiple field types.
*
* @return array|string[] Returns an array of theme slugs to enqueue
*/
public static function get_themes_to_enqueue( $form, $field_types = '' ) {
// In the form editor, enqueue the theme framework.
if ( GFCommon::is_form_editor() ) {
return array( 'orbital' );
}
// Enqueues Gravity and Theme Framework themes in the block editor
if ( GFCommon::is_block_editor_page() ) {
return array( 'gravity-theme', 'orbital' );
}
// Enqueues Gravity theme in the entry detail views
if ( GFCommon::is_entry_detail() ) {
return array( 'gravity-theme' );
}
// On pages other than the form editor and block editor, if a field type is specified, only enqueue a theme if the form has the specified field type.
if ( ! empty( $field_types ) && ! GFCommon::get_fields_by_type( $form, $field_types ) ) {
return array();
}
$selected_theme = self::get_form_theme_slug( $form );
return array( $selected_theme );
}
/**
* Fire the post render events for a form instance when the form is visible on the page.
*
* @since 2.8.4
*
* @param $form_id
* @param $current_page
*
* @return string
*/
public static function post_render_script( $form_id, $current_page = 'current_page' ) {
$post_render_script = '
jQuery(document).trigger("gform_pre_post_render", [{ formId: "' . $form_id . '", currentPage: "' . $current_page . '", abort: function() { this.preventDefault(); } }]);
if (event && event.defaultPrevented) {
return;
}
const gformWrapperDiv = document.getElementById( "gform_wrapper_' . $form_id . '" );
if ( gformWrapperDiv ) {
const visibilitySpan = document.createElement( "span" );
visibilitySpan.id = "gform_visibility_test_' . $form_id . '";
gformWrapperDiv.insertAdjacentElement( "afterend", visibilitySpan );
}
const visibilityTestDiv = document.getElementById( "gform_visibility_test_' . $form_id . '" );
let postRenderFired = false;
function triggerPostRender() {
if ( postRenderFired ) {
return;
}
postRenderFired = true;
jQuery( document ).trigger( \'gform_post_render\', [' . $form_id . ', ' . $current_page . '] );
gform.utils.trigger( { event: \'gform/postRender\', native: false, data: { formId: ' . $form_id . ', currentPage: ' . $current_page . ' } } );
gform.utils.trigger( { event: \'gform/post_render\', native: false, data: { formId: ' . $form_id . ', currentPage: ' . $current_page . ' } } );
if ( visibilityTestDiv ) {
visibilityTestDiv.parentNode.removeChild( visibilityTestDiv );
}
}
function debounce( func, wait, immediate ) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if ( !immediate ) func.apply( context, args );
};
var callNow = immediate && !timeout;
clearTimeout( timeout );
timeout = setTimeout( later, wait );
if ( callNow ) func.apply( context, args );
};
}
const debouncedTriggerPostRender = debounce( function() {
triggerPostRender();
}, 200 );
if ( visibilityTestDiv && visibilityTestDiv.offsetParent === null ) {
const observer = new MutationObserver( ( mutations ) => {
mutations.forEach( ( mutation ) => {
if ( mutation.type === \'attributes\' && visibilityTestDiv.offsetParent !== null ) {
debouncedTriggerPostRender();
observer.disconnect();
}
});
});
observer.observe( document.body, {
attributes: true,
childList: false,
subtree: true,
attributeFilter: [ \'style\', \'class\' ],
});
} else {
triggerPostRender();
}
';
$post_render_script = gf_apply_filters( array( 'gform_post_render_script', $form_id ), $post_render_script, $form_id, $current_page );
return str_replace( [ "\t", "\n", "\r" ], '', $post_render_script );
}
/**
* Get a form for display.
*
* @since unknown
* @since 2.7.15 Added the $form_theme and $style_settings parameters.
*
* @param int $form_id The id of the form.
* @param bool $display_title Whether to display the form title.
* @param bool $display_description Whether to display the form description.
* @param bool $force_display Whether to force the form to display even if it is inactive.
* @param array $field_values Array of field values.
* @param bool $ajax Whether ajax is enabled.
* @param int $tabindex Tabindex for the form.
* @param string $form_theme Form theme slug.
* @param string $style_settings JSON-encoded style settings. Passing false will bypass the gform_default_styles filter.
*
* @return mixed|string|WP_Error