-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-gp-nested-forms.php
More file actions
4077 lines (3253 loc) · 139 KB
/
class-gp-nested-forms.php
File metadata and controls
4077 lines (3253 loc) · 139 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
if ( ! class_exists( 'GP_Plugin' ) ) {
return;
}
class GP_Nested_Forms extends GP_Plugin {
protected $_version = GP_NESTED_FORMS_VERSION;
protected $_path = 'gp-nested-forms/gp-nested-forms.php';
protected $_full_path = __FILE__;
protected $_slug = 'gp-nested-forms';
protected $_title = 'Gravity Forms Nested Forms';
protected $_short_title = 'Nested Forms';
public $parent_form_id = null;
public $field_type = 'form';
private $shortcode_field_values = array();
public static $nested_forms_markup = array();
private static $instance = null;
/**
* @var array|null $entry_being_edited Stores the entry being edited so we can access the entry prior to it being
* updated.
*/
public $entry_being_edited = null;
public static function get_instance() {
if ( self::$instance === null ) {
self::includes();
self::$instance = isset( self::$perk_class ) ? new self( new self::$perk_class ) : new self();
}
return self::$instance;
}
public static function includes() { }
public function minimum_requirements() {
return array(
'gravityforms' => array(
'version' => '2.4',
),
'wordpress' => array(
'version' => '4.9',
),
);
}
public function pre_init() {
parent::pre_init();
$this->setup_cron();
require_once( 'includes/class-gp-template.php' );
require_once( 'includes/class-gp-field-nested-form.php' );
require_once( 'includes/class-gpnf-feed-processing.php' );
require_once( 'includes/class-gpnf-gravityview.php' );
require_once( 'includes/class-gpnf-gravityflow.php' );
require_once( 'includes/class-gpnf-gfml.php' );
require_once( 'includes/class-gpnf-wc-product-addons.php' );
require_once( 'includes/class-gpnf-merge-tags.php' );
require_once( 'includes/class-gpnf-parent-merge-tag.php' );
require_once( 'includes/class-gpnf-notification-processing.php' );
require_once( 'includes/class-gpnf-zapier.php' );
require_once( 'includes/class-gpnf-entry.php' );
require_once( 'includes/class-gpnf-session.php' );
require_once( 'includes/class-gpnf-export.php' );
require_once( 'includes/class-gpnf-easy-passthrough.php' );
// Nested Form fields have a dynamically retrieved value set via this filter and needs to be in place as early as possible.
add_filter( 'gform_get_input_value', array( $this, 'handle_nested_form_field_value' ), 10, 3 );
// Must happen on pre_init to intercept the 'gform_export_form' filter.
gpnf_export();
}
public function init() {
parent::init();
load_plugin_textdomain( 'gp-nested-forms', false, basename( dirname( __file__ ) ) . '/languages/' );
// Initialize sub classes.
gpnf_gravityview();
gpnf_gravityflow();
gpnf_gfml();
gpnf_feed_processing();
gpnf_parent_merge_tag();
gpnf_notification_processing();
gpnf_zapier();
gpnf_merge_tags();
gpnf_wc_product_addons();
gpnf_easy_passthrough();
// General Hooks
add_action( 'gform_form_args', array( $this, 'stash_shortcode_field_values' ) );
add_action( 'gform_pre_validation', array( $this, 'maybe_load_nested_form_hooks' ) );
add_action( 'gform_pre_render', array( $this, 'maybe_load_nested_form_hooks' ) );
add_filter( 'gform_entry_meta', array( $this, 'register_entry_meta' ) );
add_filter( 'gform_merge_tag_filter', array( $this, 'all_fields_value' ), 11, 6 );
add_action( 'gform_calculation_formula', array( $this, 'process_merge_tags' ), 10, 4 );
add_filter( 'gform_custom_merge_tags', array( $this, 'add_nested_form_field_total_merge_tag' ), 10, 4 );
// Handle parent form.
add_action( 'gform_register_init_scripts', array( $this, 'register_all_form_init_scripts' ) );
add_filter( 'gform_form_tag', array( $this, 'add_session_hash_input' ), 10, 2 );
add_action( 'gform_entry_created', array( $this, 'handle_parent_submission' ), 10, 2 );
add_action( 'gform_after_update_entry', array( $this, 'handle_parent_update_entry' ), 10, 2 );
add_action( 'gform_entry_post_save', array( $this, 'handle_parent_submission_post_save' ), 20 /* should happen well after feeds are processed on 10 */, 2 );
// Handle nested form.
add_action( 'gform_get_form_filter', array( $this, 'handle_nested_forms_markup' ), 10, 2 );
add_filter( 'gform_confirmation', array( $this, 'handle_nested_confirmation' ), 10, 3 );
add_filter( 'gform_confirmation_anchor', array( $this, 'handle_nested_confirmation_anchor' ) );
add_action( 'gform_entry_id_pre_save_lead', array( $this, 'maybe_edit_entry' ), 10, 2 );
add_action( 'gform_entry_post_save', array( $this, 'add_child_entry_meta' ), 10, 2 );
add_filter( 'gform_get_field_value', array( $this, 'row_id_field_value' ), 11, 3 );
add_filter( 'gform_form_args', array( $this, 'force_display_expired_nested_form' ), 10, 1 );
add_filter( 'gform_pre_validation', array( $this, 'skip_expired_nested_form_schedule_validation' ), 10, 1 );
// Administrative hooks.
// Trash child entries when a parent entry is trashed or deleted.
add_action( 'gform_update_status', array( $this, 'child_entry_trash_manage' ), 10, 3 );
// Delete child entries when parent entry is permanently deleted.
add_action( 'gform_delete_entry', array( $this, 'child_entry_delete' ), 10 );
// Filter child entries by parent entry ID in the List View.
add_filter( 'gform_get_entries_args_entry_list', array( $this, 'filter_entry_list' ) );
// Add support for processing nested forms in Gravity Forms preview.
add_action( 'wp', array( $this, 'handle_core_preview_ajax' ), 9 );
// Add support for filtering by Parent Entry ID in Entries List or and plugins like Gravity Flow Form Connector
add_filter( 'gform_field_filters', array( $this, 'add_parent_form_filter' ), 10, 2 );
// Add support for displaying Parent Entry link in Child Entry Info box.
add_action( 'gform_entry_info', array( $this, 'add_parent_entry_link' ), 10, 2 );
add_filter( 'gform_form_theme_slug', array( $this, 'override_gf_theme_in_preview' ), 11, 2 );
// Integrations.
add_filter( 'gform_webhooks_request_data', array( $this, 'add_full_child_entry_data_for_webhooks' ), 10, 4 );
add_filter( 'gform_partialentries_post_entry_saved', array( $this, 'adopt_partial_entry_children' ), 10, 2 );
add_filter( 'gform_partialentries_post_entry_updated', array( $this, 'adopt_partial_entry_children' ), 10, 2 );
// Integration Fixes.
if ( $this->use_jquery_ui_dialog() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'fix_jquery_ui_issue' ), 1000 );
}
add_filter( 'gform_enqueue_scripts', array( $this, 'enqueue_child_form_scripts' ) );
add_filter( 'gform_field_input', array( $this, 'rerender_signature_field_on_edit' ), 10, 5 );
// Make single entry label and plural entry label translatable via WPML.
add_filter( 'gform_multilingual_field_keys', array( $this, 'wpml_translate_entry_labels' ) );
// Allow Gravity Forms 2.9.18+ to retain previously uploaded files when a nested entry is edited.
add_filter( 'gform_submission_files_pre_save_field_value', array( $this, 'prepare_submission_files_for_save' ), 9, 4 );
// Clear form's nested entries if save and continue is used (migrated from snippet library)
add_action( 'gform_post_process', function( $form ) {
if ( rgpost( 'gform_save' ) && class_exists( 'GPNF_Session' ) ) {
$session = new GPNF_Session( $form['id'] );
$session->delete_cookie();
}
} );
add_filter( 'gform_pre_render', array( $this, 'maybe_disable_honeypot_on_pre_render' ), 10, 3 );
add_filter( 'gform_pre_validation', array( $this, 'maybe_disable_honeypot_on_validation' ), 10, 1 );
}
public function maybe_disable_honeypot_on_pre_render( $form, $ajax, $field_values ) {
return $this->maybe_disable_honeypot( $form );
}
public function maybe_disable_honeypot_on_validation( $form ) {
return $this->maybe_disable_honeypot( $form );
}
public function maybe_disable_honeypot( $form ) {
// Honeypot validation started causing issues when on child forms starting with Gravity Forms 2.7.0.
// This disables Honeypot validtion for child forms in this scenario so that the form can be successfully submitted.
if ( $this->is_nested_form_submission() && version_compare( GFForms::$version, '2.7', '>=' ) ) {
$form['enableHoneypot'] = false;
}
return $form;
}
public function init_admin() {
parent::init_admin();
add_filter( 'gform_admin_pre_render', array( $this, 'cleanup_form_meta' ) );
// Field Settings
add_action( 'gform_field_standard_settings_1430', array( $this, 'editor_field_standard_settings' ) );
add_action( 'gform_field_appearance_settings_500', array( $this, 'editor_field_appearance_settings' ) );
add_action( 'gform_field_advanced_settings_400', array( $this, 'editor_field_advanced_settings' ) );
}
public function init_ajax() {
parent::init_ajax();
// AJAX
add_action( 'wp_ajax_gpnf_get_form_fields', array( $this, 'ajax_get_form_fields' ) );
add_action( 'wp_ajax_nopriv_gpnf_get_form_fields', array( $this, 'ajax_get_form_fields' ) );
add_action( 'wp_ajax_gpnf_delete_entry', array( $this, 'ajax_delete_entry' ) );
add_action( 'wp_ajax_nopriv_gpnf_delete_entry', array( $this, 'ajax_delete_entry' ) );
add_action( 'wp_ajax_gpnf_edit_entry', array( $this, 'ajax_edit_entry' ) );
add_action( 'wp_ajax_nopriv_gpnf_edit_entry', array( $this, 'ajax_edit_entry' ) );
add_action( 'wp_ajax_gpnf_refresh_markup', array( $this, 'ajax_refresh_markup' ) );
add_action( 'wp_ajax_nopriv_gpnf_refresh_markup', array( $this, 'ajax_refresh_markup' ) );
add_action( 'wp_ajax_gpnf_duplicate_entry', array( $this, 'ajax_duplicate_entry' ) );
add_action( 'wp_ajax_nopriv_gpnf_duplicate_entry', array( $this, 'ajax_duplicate_entry' ) );
add_action( 'wp_ajax_gpnf_session', array( $this, 'ajax_session' ) );
add_action( 'wp_ajax_nopriv_gpnf_session', array( $this, 'ajax_session' ) );
}
public function upgrade( $previous_version ) {
global $wpdb;
if ( ! $previous_version ) {
return;
}
if ( version_compare( $previous_version, '1.0-beta-8', '<' ) ) {
add_option( 'gpnf_use_jquery_ui', true );
}
if ( version_compare( $previous_version, '1.0-beta-5', '<' ) ) {
// Delete expiration meta key from entries that have a valid parent entry ID.
$sql = $wpdb->prepare(
"
DELETE em1 FROM {$wpdb->prefix}gf_entry_meta em1
INNER JOIN {$wpdb->prefix}gf_entry_meta em2 ON em2.entry_id = em1.entry_id
WHERE em1.meta_key = %s
AND em2.meta_key = %s
AND concat( '', em2.meta_value * 1 ) = em2.meta_value",
GPNF_Entry::ENTRY_EXP_KEY,
GPNF_Entry::ENTRY_PARENT_KEY
);
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( $sql );
}
}
public function tooltips( $tooltips ) {
$template = '<h6>%s</h6> %s';
$learn_more_link = sprintf(
'<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
esc_url( 'https://gravitywiz.com/documentation/gravity-forms-nested-forms/#what-happens-to-child-entries-when-the-parent-entry-is-never-submitted' ),
esc_html__( 'Learn more', 'gp-nested-forms' )
);
$tooltips['gpnf_form'] = sprintf( $template, __( 'Nested Form', 'gp-nested-forms' ), __( 'Select the form that should be used to create nested entries for this form.', 'gp-nested-forms' ) );
$tooltips['gpnf_fields'] = sprintf( $template, __( 'Summary Fields', 'gp-nested-forms' ), __( 'Select which fields from the nested entry will display in table on the current form. This does not affect which fields will appear in the modal.', 'gp-nested-forms' ) );
$tooltips['gpnf_entry_labels'] = sprintf( $template, __( 'Entry Labels', 'gp-nested-forms' ), __( 'Specify a singular and plural label with which entries submitted via this field will be labeled (i.e. "employee", "employees").', 'gp-nested-forms' ) );
$tooltips['gpnf_entry_limits'] = sprintf( $template, __( 'Entry Limits', 'gp-nested-forms' ), __( 'Specify the minimum and maximum number of entries that can be submitted for this field.', 'gp-nested-forms' ) );
$tooltips['gpnf_feed_processing'] = sprintf( $template, __( 'Feed Processing', 'gp-nested-forms' ), __( 'By default, any Gravity Forms add-on feeds will be processed immediately when the nested form is submitted. Use this option to delay feed processing for entries submitted via the nested form until after the parent form is submitted. <br><br>For example, if you have a User Registration feed configured for the nested form, you may not want the users to actually be registered until the parent form is submitted.', 'gp-nested-forms' ) );
$tooltips['gpnf_modal_header_color'] = sprintf( $template, __( 'Modal Color', 'gp-nested-forms' ), __( 'Select a color which will be used to set the background color of the nested form modal header and navigational buttons.', 'gp-nested-forms' ) );
$tooltips['gpnf_orphaned_child_entry'] = sprintf(
$template,
__( 'Orphaned Child Entry', 'gp-nested-forms' ),
sprintf(
__( 'This child entry does not have a numeric parent entry ID yet because the parent form has not been submitted. %s', 'gp-nested-forms' ),
$learn_more_link
)
);
return $tooltips;
}
public function scripts() {
$scripts = array(
array(
'handle' => 'knockout',
'src' => $this->get_base_url() . '/js/built/knockout.js',
'version' => $this->_version,
'enqueue' => null,
),
);
if ( GFForms::is_gravity_page() ) {
$scripts[] = array(
'handle' => 'gp-nested-forms-admin',
'src' => $this->get_base_url() . '/js/built/gp-nested-forms-admin.js',
'version' => $this->_version,
'deps' => array( 'jquery', 'gwp-asmselect' ),
'enqueue' => array(
array( 'admin_page' => array( 'form_editor' ) ),
),
'callback' => array( $this, 'localize_scripts' ),
);
}
$deps = array( 'jquery', 'gform_gravityforms', 'knockout' );
if ( $this->use_jquery_ui_dialog() ) {
$deps[] = 'jquery-ui-dialog';
}
$src = $this->use_jquery_ui_dialog() ? '/js/built/gp-nested-forms-jquery-ui.js' : '/js/built/gp-nested-forms.js';
$scripts[] = array(
'handle' => 'gp-nested-forms',
'src' => $this->get_base_url() . $src,
'version' => $this->_version,
'deps' => $deps,
'enqueue' => array(
array( $this, 'should_enqueue_frontend_script' ),
),
'callback' => array( $this, 'localize_scripts' ),
);
return array_merge( parent::scripts(), $scripts );
}
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gwp-asmselect',
'enqueue' => array(
array( 'admin_page' => array( 'form_editor' ) ),
),
),
array(
'handle' => 'gp-nested-forms-admin',
'src' => $this->get_base_url() . "/css/gp-nested-forms-admin{$min}.css",
'version' => $this->_version,
'deps' => array(),
'enqueue' => array(
array( 'admin_page' => array( 'form_editor', 'entry_view', 'entry_edit' ) ),
),
),
);
$deps = array();
if ( ! $this->use_jquery_ui_dialog() ) {
$styles[] = array(
'handle' => 'tingle',
'src' => $this->get_base_url() . "/css/tingle{$min}.css",
'version' => $this->_version,
'enqueue' => null,
);
$deps[] = 'tingle';
}
$styles[] = array(
'handle' => 'gp-nested-forms',
'src' => $this->get_base_url() . "/css/gp-nested-forms{$min}.css",
'version' => $this->_version,
'deps' => $deps,
'enqueue' => array(
array( $this, 'should_enqueue_frontend_script' ),
),
);
return array_merge( parent::styles(), $styles );
}
public function should_enqueue_frontend_script( $form ) {
// Do not enqueue if we're inside the Elementor editor. For some reason with the add-on framework, our scripts
// are getting enqueued while gform_gravityforms is not.
if (
class_exists( '\Elementor\Plugin' )
// @phpstan-ignore-next-line
&& method_exists( '\Elementor\Plugin', 'instance' )
&& \Elementor\Plugin::$instance->editor->is_edit_mode()
) {
return false;
}
return ! GFForms::get_page() && ! rgempty( GFFormsModel::get_fields_by_type( $form, array( 'form' ) ) );
}
public function use_jquery_ui_dialog() {
$raw = (bool) get_option( 'gpnf_use_jquery_ui' );
/**
* Filter whether jQuery UI Dialog should be used to power the Nested Forms modal experience.
*
* @since 1.0-beta-8
*
* @param bool $use_jquery_ui Should jQuery UI Dialog be used to power the modal experience?
*/
$filtered = (bool) apply_filters( 'gpnf_use_jquery_ui', $raw );
$use_jquery_ui = $filtered;
if ( $filtered !== $raw ) {
update_option( 'gpnf_use_jquery_ui', $filtered );
}
return $use_jquery_ui;
}
public function localize_scripts( $form ) {
wp_localize_script(
'gp-nested-forms-admin',
'GPNFAdminData',
array(
'nonces' => array(
'getFormFields' => wp_create_nonce( 'gpnf_get_form_fields' ),
),
'strings' => array(
'getFormFieldsError' => esc_html__( 'There was an error retrieving the fields for this form. Please try again or contact support.', 'gp-nested-forms' ),
),
)
);
wp_localize_script(
'gp-nested-forms',
'GPNFData',
array(
'nonces' => array(
'editEntry' => wp_create_nonce( 'gpnf_edit_entry' ),
'refreshMarkup' => wp_create_nonce( 'gpnf_refresh_markup' ),
'deleteEntry' => wp_create_nonce( 'gpnf_delete_entry' ),
'duplicateEntry' => wp_create_nonce( 'gpnf_duplicate_entry' ),
),
'strings' => array(),
)
);
if ( ! is_array( rgar( $form, 'fields' ) ) ) {
return;
}
foreach ( $form['fields'] as $field ) {
if ( $field->get_input_type() === 'form' ) {
$nested_form = $this->get_nested_form( $field->gpnfForm );
if ( ! $nested_form ) {
continue;
}
foreach ( $nested_form['fields'] as $_field ) {
if ( $_field->get_input_type() === 'fileupload' && $_field->multipleFiles ) {
GFCommon::localize_gform_gravityforms_multifile();
}
}
}
}
}
/**
* If certain scripts are loaded *after* jQuery UI it hides the close button in the modal. If one of these scripts
* is enqueued, let's add it as a dependency to jQuery UI so that script will be loaded first.
*
* @deprecated 1.0-beta-8
*
* @see https://stackoverflow.com/questions/17367736/jquery-ui-dialog-missing-close-icon
*/
public function fix_jquery_ui_issue() {
$deps = array(
/**
* Beaver Builder Theme
* https://www.wpbeaverbuilder.com/
*/
'bootstrap',
/**
* Understrap Theme
* https://github.com/understrap/understrap
*/
'understrap-scripts',
);
/**
* Filter the dependencies for jQuery UI.
*
* Allows 3rd parties to avoid the issue where the modal close button is not present if certain scripts they
* load are loaded - after - jQuery UI.
*
* @since 1.0-beta-5.5
*
* @param array $deps An array of script handles.
*/
$deps = apply_filters( 'gpnf_jquery_ui_dependencies', $deps );
foreach ( $deps as $dep ) {
if ( wp_script_is( $dep ) ) {
$this->add_script_dependency( 'jquery-ui-core', $dep );
}
}
}
/**
* Add a dependency to an existing script.
*
* @deprecated 1.0-beta-8
*
* @see https://wordpress.stackexchange.com/questions/100709/add-a-script-as-a-dependency-to-a-registered-script
*
* @param $handle
* @param $dep
*
* @return bool
*/
public function add_script_dependency( $handle, $dep ) {
global $wp_scripts;
$script = $wp_scripts->query( $handle, 'registered' );
if ( ! $script ) {
return false;
}
if ( ! in_array( $dep, $script->deps ) ) {
$script->deps[] = $dep;
}
return true;
}
public function setup_cron() {
if ( ! wp_next_scheduled( 'gpnf_daily_cron' ) ) {
wp_schedule_event( time(), 'daily', 'gpnf_daily_cron' );
}
add_action( 'gpnf_daily_cron', array( $this, 'daily_cron' ) );
}
public function daily_cron() {
$expired = $this->get_expired_entries();
$this->log( 'Running daily cron.' );
$this->log( sprintf( 'Expired entry IDs: %s', implode( ', ', $expired ) ) );
foreach ( $expired as $entry_id ) {
// Move expired entries to the trash. Gravity Forms will handle deleting them from there.
GFAPI::update_entry_property( $entry_id, 'status', 'trash' );
// Remove expiration meta so this entry will never "expire" again.
$entry = new GPNF_Entry( $entry_id );
$entry->delete_expiration();
}
}
public function get_expired_entries() {
global $wpdb;
// Orphaned entries have an expiration timestamp. If it is before the current time, it is expired.
$expiration = time();
$this->log( sprintf( 'Expiration Timestamp: %d', $expiration ) );
$results = $wpdb->get_results( $wpdb->prepare( "SELECT entry_id FROM {$wpdb->prefix}gf_entry_meta WHERE meta_key = %s and meta_value < %d", GPNF_Entry::ENTRY_EXP_KEY, $expiration ) );
$entry_ids = wp_list_pluck( $results, 'entry_id' );
return $entry_ids;
}
public function handle_core_preview_ajax() {
if ( rgget( 'gf_page' ) == 'preview' && $this->is_nested_form_submission() && class_exists( 'GFFormDisplay' ) && ! empty( GFFormDisplay::$submission ) ) {
echo GFForms::get_form( rgpost( 'gform_submit' ), true, true, true, null, true );
exit;
}
}
public function add_parent_form_filter( $field_filters, $form ) {
$field_filters[] = array(
'key' => GPNF_Entry::ENTRY_PARENT_KEY,
'text' => __( 'Parent Entry ID', 'gp-nested-forms' ),
'preventMultiple' => false,
'operators' => array(
'is',
'isnot',
),
);
return $field_filters;
}
function add_parent_entry_link( $form_id, $entry ) {
$parent_entry_id = rgar( $entry, 'gpnf_entry_parent' );
$parent_form_id = rgar( $entry, 'gpnf_entry_parent_form' );
if ( $parent_entry_id && $parent_form_id ) {
$parent_entry_url = '';
$default_template = '%1$s: %2$s';
$orphan_note = '';
if ( is_numeric( $parent_entry_id ) ) {
$parent_entry_url = add_query_arg(
array(
'page' => 'gf_entries',
'view' => 'entry',
'id' => $parent_form_id,
'lid' => $parent_entry_id,
),
admin_url( 'admin.php' )
);
$default_template = '%1$s: <a href="%3$s">%2$s</a>';
} else {
$orphan_note = gform_tooltip( 'gpnf_orphaned_child_entry', null, true );
}
/**
* Filters the template used to render the parent entry link.
*
* @param string $template The format string used to generate the parent entry link.
* @param string $label The label for the parent entry (default: "Parent Entry").
* @param int|string $parent_entry_id The ID of the parent entry or temporary parent hash.
* @param string $parent_entry_url The URL of the parent entry detail view.
* @since 1.2.4
*/
$gpnf_parent_entry_link_template = apply_filters(
'gpnf_parent_entry_link_template',
$default_template,
'Parent Entry',
$parent_entry_id,
$parent_entry_url
);
echo sprintf(
$gpnf_parent_entry_link_template,
esc_html( 'Parent Entry' ), // Label
esc_html( $parent_entry_id ), // Parent entry ID
esc_url( $parent_entry_url ) // URL
);
if ( $orphan_note ) {
echo $orphan_note;
}
}
}
public function filter_entry_list( $args ) {
$parent_entry_id = rgget( GPNF_Entry::ENTRY_PARENT_KEY );
$nested_form_field_id = rgget( GPNF_Entry::ENTRY_NESTED_FORM_FIELD_KEY );
if ( ! $parent_entry_id || ! $nested_form_field_id ) {
return $args;
}
// set field filters if not already set
if ( ! isset( $args['search_criteria']['field_filters'] ) ) {
$args['search_criteria']['field_filters'] = array();
}
$args['search_criteria']['field_filters'][] = array(
'key' => GPNF_Entry::ENTRY_PARENT_KEY,
'value' => $parent_entry_id,
);
$args['search_criteria']['field_filters'][] = array(
'key' => GPNF_Entry::ENTRY_NESTED_FORM_FIELD_KEY,
'value' => $nested_form_field_id,
);
return $args;
}
public function child_entry_trash_manage( $entry_id, $new_status, $old_status ) {
$entry = new GPNF_Entry( $entry_id );
if ( ! $entry->has_children() ) {
return;
}
// Entry is trashed, send children to trash.
if ( $new_status == 'trash' ) {
$entry->trash_children();
}
// Entry is untrashed, set children to active.
if ( $old_status == 'trash' && $new_status == 'active' ) {
$entry->untrash_children();
}
}
public function child_entry_delete( $entry_id ) {
$entry = new GPNF_Entry( $entry_id );
if ( $entry->has_children() ) {
$entry->delete_children();
}
}
// # ADMIN
public function editor_field_standard_settings( $form_id ) {
$forms = GFFormsModel::get_forms();
?>
<li class="gpnf-setting field_setting gp-field-setting">
<div class="gp-row">
<label for="gpnf-form" class="section_label">
<?php _e( 'Nested Form', 'gp-nested-forms' ); ?>
<?php gform_tooltip( 'gpnf_form' ); ?>
<a href="" id="gpnf-edit-child-form" target="_blank">Edit Nested Form</a>
</label>
<select id="gpnf-form" onchange="SetFieldProperty( 'gpnfForm', this.value ); window.gpGlobals.GPNFAdmin.toggleNestedFormFields();" class="fieldwidth-3">
<option value=""><?php _e( 'Select a Form', 'gp-nested-forms' ); ?></option>
<?php
foreach ( $forms as $form ) :
if ( $form->id == $form_id ) {
continue;
}
?>
<option value="<?php echo $form->id; ?>">
<?php
echo $form->title;
if ( ! $form->is_active ) {
echo ' (' . __( 'Inactive', 'gp-nested-forms' ) . ')';
}
?>
</option>
<?php endforeach; ?>
</select>
</div>
<div id="gpnf-form-settings" class="gp-row">
<label for="gpnf-fields" class="section_label">
<?php _e( 'Summary Fields', 'gp-nested-forms' ); ?>
<?php gform_tooltip( 'gpnf_fields' ); ?>
</label>
<div id="gpnf-fields-container" class="gp-group">
<select id="gpnf-fields" title="<?php esc_html_e( 'Select your fields', 'gp-nested-forms' ); ?>" class="fieldwidth-3" multiple disabled>
<!-- dynamically populated based on selection in 'form' select -->
</select>
<img class="gpnf-static-spinner" src="<?php echo GFCommon::get_base_url(); ?>/images/<?php echo $this->is_gf_version_gte( '2.5-beta-1' ) ? 'spinner.svg' : 'spinner.gif'; ?>">
</div>
</div>
<!-- Entry Labels -->
<div id="gpnf-entry-labels" class="gp-row">
<label for="gpnf-entry-label-singular" class="section_label">
<?php esc_html_e( 'Entry Labels', 'gp-nested-forms' ); ?>
<?php gform_tooltip( 'gpnf_entry_labels' ); ?>
</label>
<div class="gp-group">
<label for="gpnf-entry-label-singular">
<?php _e( 'Singular', 'gp-nested-forms' ); ?>
</label>
<input type="text" id="gpnf-entry-label-singular" placeholder="<?php esc_html_e( 'e.g. Entry', 'gp-nested-forms' ); ?>" onchange="SetFieldProperty( 'gpnfEntryLabelSingular', jQuery( this ).val() );" />
</div>
<div class="gp-group">
<label for="gpnf-entry-label-plural">
<?php _e( 'Plural', 'gp-nested-forms' ); ?>
</label>
<input type="text" id="gpnf-entry-label-plural" placeholder="<?php esc_html_e( 'e.g. Entries', 'gp-nested-forms' ); ?>" onchange="SetFieldProperty( 'gpnfEntryLabelPlural', jQuery( this ).val() );" />
</div>
</div>
</li>
<?php
}
public function editor_field_appearance_settings() {
?>
<li class="gpnf-modal-header-color-setting field_setting" style="display:none;">
<label for="gpnf-modal-header-color" class="section_label">
<?php _e( 'Modal Color', 'gp-nested-forms' ); ?>
<?php gform_tooltip( 'gpnf_modal_header_color' ); ?>
</label>
<div class="gp-group">
<input type="text" class="iColorPicker" onchange="SetFieldProperty( 'gpnfModalHeaderColor', this.value );" id="gpnf-modal-header-color" />
<img id="chip_gpnf-modal-header-color" height="24" width="24" src="<?php echo GFCommon::get_base_url(); ?>/images/blankspace.png" />
<img id="chooser_gpnf-modal-header-color" height="16" width="16" src="<?php echo GFCommon::get_base_url(); ?>/images/color.png" />
</div>
</li>
<?php
}
public function editor_field_advanced_settings() {
?>
<li class="gpnf-entry-limits-setting field_setting gp-field-setting" id="gpnf-entry-limits" style="display:none;">
<label for="gpnf-entry-limit-min" class="section_label">
<?php esc_html_e( 'Entry Limits', 'gp-nested-forms' ); ?>
<?php gform_tooltip( 'gpnf_entry_limits' ); ?>
</label>
<div class="gp-group">
<label for="gpnf-entry-limit-min">
<?php _e( 'Minimum', 'gp-nested-forms' ); ?>
</label>
<input type="number" id="gpnf-entry-limit-min" placeholder="<?php esc_html_e( 'e.g. 2', 'gp-nested-forms' ); ?>" onchange="SetFieldProperty( 'gpnfEntryLimitMin', jQuery( this ).val() );" />
</div>
<div class="gp-group">
<label for="gpnf-entry-limit-max">
<?php _e( 'Maximum', 'gp-nested-forms' ); ?>
</label>
<input type="number" id="gpnf-entry-limit-max" placeholder="<?php esc_html_e( 'e.g. 5', 'gp-nested-forms' ); ?>" onchange="SetFieldProperty( 'gpnfEntryLimitMax', jQuery( this ).val() );" />
</div>
</li>
<?php if ( apply_filters( 'gpnf_enable_feed_processing_setting', false ) ) : ?>
<li class="gpnf-feed-processing-setting field_setting" id="gpnf-feed-processing-setting" style="display:none;">
<label for="gpnf-feed-processing" class="section_label">
<?php esc_html_e( 'Feed Processing', 'gp-nested-forms' ); ?>
<?php gform_tooltip( 'gpnf_feed_processing' ); ?>
</label>
<span><?php esc_html_e( 'Process nested feeds when the', 'gp-nested-forms' ); ?></span>
<select id="gpnf-feed-processing" onchange="SetFieldProperty( 'gpnfFeedProcessing', jQuery( this ).val() );">
<option value="parent"><?php esc_html_e( 'parent form', 'gp-nested-forms' ); ?></option>
<option value="child"><?php esc_html_e( 'nested form', 'gp-nested-forms' ); ?></option>
</select>
<span><?php esc_html_e( 'is submitted.', 'gp-nested-forms' ); ?></span>
</li>
<?php endif; ?>
<?php
}
// # GENERAL FUNCTIONALITY
/**
* Enqueue child scripts/styles at the same time as the parent.
*
* This resolves an issue with GF Tooltip where there inline styles were being output *before* the child form's
* inline styles had been enqueued. The child form styles were thus enqueued by not never output.
*
* @param $form
*/
public function enqueue_child_form_scripts( $form ) {
if ( empty( $form['fields'] ) || ! is_array( $form['fields'] ) ) {
return;
}
foreach ( $form['fields'] as $field ) {
if ( $field->type !== 'form' ) {
continue;
}
$nested_form_id = rgar( $field, 'gpnfForm' );
$nested_form = $this->get_nested_form( $nested_form_id );
if ( rgar( $nested_form, 'fields' ) ) {
GFFormDisplay::enqueue_form_scripts( $nested_form, true );
}
}
}
public function get_nested_forms_markup( $form ) {
global $wp_filter;
do_action( 'gpnf_pre_nested_forms_markup', $form );
// I'm not a huge fan of this... but Gravity Forms is promoting a snippet that wraps all GF inline scripts in a
// DOMContentLoaded event listener. This prevents child form markup from being properly initialized. As a stop-gap
// solution, let's unbind (and later rebind) all functions on the 'gform_cdata_open' filter.
$cdata_open_filters = rgar( $wp_filter, 'gform_cdata_open' );
$cdata_close_filters = rgar( $wp_filter, 'gform_cdata_close' );
if ( $cdata_open_filters ) {
unset( $wp_filter['gform_cdata_open'] );
unset( $wp_filter['gform_cdata_close'] );
}
ob_start();
foreach ( $form['fields'] as $field ) :
if ( $field->type != 'form' ) {
continue;
}
$nested_form_id = rgar( $field, 'gpnfForm' );
$nested_form = $this->get_nested_form( $nested_form_id );
if ( ! $nested_form ) {
$data = array(
'nested_field_id' => $field->id,
'nested_form_id' => $nested_form_id,
);
$this->log( sprintf( $nested_form_id ? 'No nested form ID is configured for this field: %s' : 'Nested form does not exist: %s', print_r( $data, true ) ) );
continue;
}
?>
<div class="gpnf-nested-form gpnf-nested-form-<?php echo $form['id']; ?>-<?php echo $field['id']; ?>" style="display:none;">
<?php
if ( $this->use_jquery_ui_dialog() ) {
$this->load_nested_form_hooks( $nested_form_id, $form['id'] );
gravity_form( $nested_form_id, false, true, $this->is_preview(), $this->get_stashed_shortcode_field_values( $form['id'] ), true, $this->get_tabindex() );
$this->unload_nested_form_hooks( $nested_form_id, $form );
} else {
/**
* Preload the form but do not echo it out. This is important for making sure all CSS/JS gets
* enqueued if enqueueing logic during the form.
*
* This addition was necessary for compatibility with GP Populate Anything's Live Merge Tags.
*
* @param boolean $value Whether or not to pre-load (but not echo to document) the form.
* @param array $form The current form.
* @since 1.0-beta-9.4
*/
if ( gf_apply_filters( array( 'gpnf_preload_form', $form['id'] ), true, $form ) ) {
// Store hooks_js_printed, so we can know if this child form impacts the state of the variable.
// Needed after GF 2.8.9.1
$hooks_js_printed = GFFormDisplay::$hooks_js_printed;
add_filter( 'gform_init_scripts_footer', '__return_false', 123 );
/* Ensure that the last param ($echo) is false so it does not get rendered out. */
gravity_form( $nested_form_id, false, true, $this->is_preview(), $this->get_stashed_shortcode_field_values( $form['id'] ), true, $this->get_tabindex(), false );
remove_filter( 'gform_init_scripts_footer', '__return_false', 123 );
if ( $hooks_js_printed !== GFFormDisplay::$hooks_js_printed ) {
GFFormDisplay::$hooks_js_printed = $hooks_js_printed;
}
}
echo '<!-- Loaded dynamically via AJAX -->';
}
?>
</div>
<div class="gpnf-edit-form gpnf-edit-form-<?php echo $form['id']; ?>-<?php echo $field['id']; ?>" style="display:none;">
<!-- Loaded dynamically via AJAX -->
</div>
<?php
endforeach;
if ( $cdata_open_filters ) {
$wp_filter['gform_cdata_open'] = $cdata_open_filters;
$wp_filter['gform_cdata_close'] = $cdata_close_filters;
}
do_action( 'gpnf_nested_forms_markup', $form );
return ob_get_clean();
}
public function get_tabindex() {
return GFCommon::$tab_index > 1 ? GFCommon::$tab_index++ : 0;
}
/**
* Output all queued nested forms markup.
*/
public static function output_nested_forms_markup() {
foreach ( self::$nested_forms_markup as $markup ) {
echo $markup;
}
}
public function register_entry_meta( $meta ) {