-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Pods_GF_Addon.php
executable file
·1731 lines (1384 loc) · 44.1 KB
/
Pods_GF_Addon.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
require_once( PODS_GF_DIR . 'includes/Pods_GF.php' );
/**
* Class Pods_GF_Addon
*/
class Pods_GF_Addon extends GFFeedAddOn {
/**
* @var string
*/
protected $_version = PODS_GF_VERSION;
/**
* @var string
*/
protected $_min_gravityforms_version = '1.9';
/**
* @var string
*/
protected $_path = PODS_GF_ADDON_FILE;
/**
* @var string
*/
protected $_full_path = PODS_GF_FILE;
/**
* @var string
*/
protected $_url = 'http://pods.io/';
/**
* @var string
*/
protected $_slug = 'pods-gravity-forms';
/**
* @var string
*/
protected $_title = 'Pods Gravity Forms Add-On';
/**
* @var string
*/
protected $_short_title = 'Pods';
/**
* @var array
*/
protected $_capabilities = array( 'pods_gravityforms', 'pods_gravityforms_uninstall' );
/**
* @var array
*/
protected $_capabilities_form_settings = array( 'pods_gravityforms', 'pods' );
/**
* @var Pods_GF[]
*/
public $pods_gf = array();
/**
* Contains an instance of this class, if available.
*
* @since 1.0
* @access private
*
* @used-by Pods_GF_Addon::get_instance()
*
* @var object $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Get an instance of this class.
*
* @since 1.0
* @access public
*
* @uses Pods_GF_Addon
* @uses Pods_GF_Addon::$_instance
*
* @return object Pods_GF_Addon
*/
public static function get_instance() {
if ( null === self::$_instance ) {
self::$_instance = new Pods_GF_Addon;
}
return self::$_instance;
}
/**
* Override this function to allow the feed to being duplicated.
*
* @param int|array $id The ID of the feed to be duplicated or the feed object when duplicating a form.
*
* @return boolean|true
*/
public function can_duplicate_feed( $id ) {
return true;
}
/**
* @return array
*/
public function scripts() {
$scripts = array(
array(
'handle' => 'pods_gf_admin',
'enqueue' => array( array( 'admin_page' => array( 'form_settings' ) ) ),
'src' => PODS_GF_URL . '/ui/pods-gf-admin.js',
'version' => $this->_version,
'deps' => array( 'jquery' ),
),
);
return array_merge( parent::scripts(), $scripts );
}
/*public function plugin_page() {
?>
This page appears in the Forms menu
<?php
}*/
/**
* @return array
*/
public function feed_settings_fields() {
$feed_field_name = array(
'label' => __( 'Name', 'pods-gravity-forms' ),
'type' => 'text',
'name' => 'feedName',
'tooltip' => __( 'Name for this feed', 'pods-gravity-forms' ),
'class' => 'medium',
);
$gf_fields = array();
$gf_form = GFAPI::get_form( pods_v( 'id' ) );
if ( ! empty( $gf_form ) && ! empty( $gf_form['fields'] ) ) {
$gf_fields = $gf_form['fields'];
}
$pods_api = pods_api();
$all_pods = $pods_api->load_pods( array( 'names' => true ) );
$pod_choice_list = array();
$pod_choice_list[] = array(
'label' => __( 'Select a Pod', 'pods-gravity-forms' ),
'value' => '',
);
foreach ( $all_pods as $name => $label ) {
$pod_choice_list[] = array(
'label' => $label . ' (' . $name . ')',
'value' => $name,
);
}
$feed_field_pod = array(
'label' => __( 'Pod', 'pods-gravity-forms' ),
'type' => 'select',
'name' => 'pod',
'tooltip' => __( 'Select the pod', 'pods-gravity-forms' ),
'choices' => $pod_choice_list,
'onchange' => "jQuery(this).parents('form').submit();",
'required' => true,
);
$selected_pod = $this->get_setting( 'pod' );
$enable_current_post = (int) $this->get_setting( 'enable_current_post' );
$enable_current_user = (int) $this->get_setting( 'enable_current_user' );
$posted_settings = $this->get_posted_settings();
if ( isset( $posted_settings['enable_current_post'] ) ) {
$enable_current_post = (int) $posted_settings['enable_current_post'];
}
if ( isset( $posted_settings['enable_current_user'] ) ) {
$enable_current_user = (int) $posted_settings['enable_current_user'];
}
$pod_fields = array();
$pod_type = '';
if ( ! empty( $selected_pod ) ) {
$pod_object = $pods_api->load_pod( array( 'name' => $selected_pod ) );
if ( ! empty( $pod_object ) ) {
$pod_type = $pod_object['type'];
foreach ( $pod_object['fields'] as $name => $field ) {
$pod_fields[] = array(
'needs_process' => true,
'name' => $name,
'field' => $field,
);
}
}
}
$feed_field_pod_fields = array(
'name' => 'pod_fields',
'label' => __( 'Pod Fields', 'pods-gravity-forms' ),
'type' => 'field_map',
'dependency' => 'pod',
'field_map' => $pod_fields,
);
$ignore_object_fields = array(
'ID',
'post_type',
'comment_type',
'taxonomy',
'guid',
'menu_order',
'post_mime_type',
'comment_count',
'comment_status',
'ping_status',
'post_date_gmt',
'post_modified_gmt',
'post_password',
'post_status',
'post_content_filtered',
'pinged',
'to_ping',
'comments',
);
$wp_object_fields = array();
if ( ! empty( $pod_object ) ) {
foreach ( $pod_object['object_fields'] as $name => $field ) {
if ( in_array( $name, $ignore_object_fields, true ) ) {
continue;
}
if ( in_array( $pod_type, array( 'post_type', 'media' ), true ) ) {
if ( 1 === $enable_current_post ) {
$field['options']['required'] = 0;
} elseif ( in_array( $name, array( 'post_title', 'post_content' ), true ) ) {
$field['options']['required'] = 1;
}
} elseif ( 'taxonomy' === $pod_type ) {
if ( 'name' === $name ) {
$field['options']['required'] = 1;
}
} elseif ( 'user' === $pod_type ) {
if ( 1 === $enable_current_user ) {
$field['options']['required'] = 0;
} elseif ( 'user_login' === $name ) {
$field['options']['required'] = 1;
}
}
$wp_object_fields[ $name ] = array(
'needs_process' => true,
'name' => $name,
'field' => $field,
);
}
}
if ( 'post_type' === $pod_type ) {
$wp_object_fields['_thumbnail_id'] = array(
'name' => '_thumbnail_id',
'label' => __( 'Featured Image', 'pods-gravity-forms' ),
);
}
$feed_field_wp_object_fields = array(
'name' => 'wp_object_fields',
'label' => __( 'WP Object Fields', 'pods-gravity-forms' ),
'type' => 'field_map',
'dependency' => 'pod',
'field_map' => array_values( $wp_object_fields ),
);
$settings = array();
///////////////////
// Pod feed mapping
///////////////////
$settings['pod_mapping'] = array(
'title' => __( 'Pod Feed Mapping', 'pods-gravity-forms' ),
'fields' => array(
$feed_field_name,
$feed_field_pod,
$feed_field_pod_fields,
),
);
if ( ! empty( $feed_field_wp_object_fields['field_map'] ) ) {
$settings['pod_mapping']['fields'][] = $feed_field_wp_object_fields;
}
$blacklisted_keys = array();
// Build field mapping data arrays
foreach ( $settings['pod_mapping']['fields'] as $k => $field_set ) {
if ( empty( $field_set['field_map'] ) ) {
continue;
}
foreach ( $field_set['field_map'] as $kf => $field_map ) {
$blacklisted_keys[] = $field_map['name'];
if ( ! empty( $field_map['needs_process'] ) ) {
$name = $field_map['name'];
$field = $field_map['field'];
$field_required = false;
if ( isset( $field['options']['required'] ) && 1 === (int) $field['options']['required'] ) {
$field_required = true;
if ( isset( $wp_object_fields[ $name ] ) ) {
if ( in_array( $pod_type, array( 'post_type', 'media' ), true ) && 1 === $enable_current_post ) {
$field_required = false;
} elseif ( 'user' === $pod_type && 1 === $enable_current_user ) {
$field_required = false;
}
}
}
$field_map = array(
'name' => $name,
'label' => $field['label'],
'required' => $field_required,
);
if ( 0 === (int) pods_v( 'fid' ) ) {
foreach ( $gf_fields as $gf_field ) {
if ( strtolower( $field['label'] ) === strtolower( $gf_field['label'] ) ) {
$field_map['default_value'] = $gf_field['id'];
}
}
}
}
// Add field names to labels
$field_map['label'] = sprintf(
'%s (%s)',
esc_html( $field_map['label'] ),
esc_html( $field_map['name'] )
);
$settings['pod_mapping']['fields'][ $k ]['field_map'][ $kf ] = $field_map;
}
}
///////////////////
// Custom fields
///////////////////
if ( in_array( $pod_type, array( 'post_type', 'taxonomy', 'user', 'media', 'comment' ), true ) ) {
$settings['custom_fields'] = array(
'title' => esc_html__( 'Custom Fields', 'pods-gravity-forms' ),
'fields' => array(
array(
'name' => 'custom_fields',
'label' => esc_html__( 'Custom Fields', 'pods-gravity-forms' ),
'type' => 'generic_map',
'key_field' => array(
'choices' => $this->get_meta_field_map( $selected_pod, $pod_type, $blacklisted_keys ),
'placeholder' => esc_html__( 'Custom Field Name', 'pods-gravity-forms' ),
'title' => esc_html__( 'Name', 'pods-gravity-forms' ),
),
'value_field' => array(
'choices' => 'form_fields',
'custom_value' => false,
'merge_tags' => true,
'placeholder' => esc_html__( 'Custom Field Value', 'pods-gravity-forms' ),
),
),
),
);
}
///////////////////
// Advanced
///////////////////
$settings['advanced'] = array(
'title' => __( 'Advanced', 'pods-gravity-forms' ),
'fields' => array(),
);
$settings['advanced']['fields'][] = array(
'name' => 'update_pod_item',
'label' => __( 'Support entry updates', 'pods-gravity-forms' ),
'type' => 'checkbox',
'choices' => array(
array(
'value' => 1,
'label' => __( 'Update pod item if the entry is updated', 'pods-gravity-forms' ),
'name' => 'update_pod_item',
),
),
);
$settings['advanced']['fields'][] = array(
'name' => 'enable_markdown',
'label' => __( 'Enable Markdown', 'pods-gravity-forms' ),
'type' => 'checkbox',
'choices' => array(
array(
'value' => 1,
'label' => __( 'Enable Markdown in HTML Fields', 'pods-gravity-forms' ),
'name' => 'enable_markdown',
),
),
);
if ( 'user' === $pod_type ) {
$settings['advanced']['fields'][] = array(
'name' => 'enable_current_user',
'label' => __( 'Enable editing with this form using logged in user', 'pods-gravity-forms' ),
'type' => 'checkbox',
'choices' => array(
array(
'value' => 1,
'label' => __( 'Enable editing with this form using the logged in user data', 'pods-gravity-forms' ),
'name' => 'enable_current_user',
),
),
);
$settings['advanced']['fields'][] = array(
'name' => 'enable_prepopulate',
'label' => __( 'Enable populating field values for this form using logged in user', 'pods-gravity-forms' ),
'type' => 'checkbox',
'choices' => array(
array(
'value' => 1,
'label' => __( 'Enable populating field values for this form using the logged in user data', 'pods-gravity-forms' ),
'name' => 'enable_prepopulate',
),
),
);
} elseif ( in_array( $pod_type, array( 'post_type', 'media' ), true ) ) {
$settings['advanced']['fields'][] = array(
'name' => 'enable_current_post',
'label' => __( 'Enable editing with this form using current post', 'pods-gravity-forms' ),
'type' => 'checkbox',
'choices' => array(
array(
'value' => 1,
'label' => __( 'Enable editing with this form using the current post ID (only works on singular template)', 'pods-gravity-forms' ),
'name' => 'enable_current_post',
),
),
);
$settings['advanced']['fields'][] = array(
'name' => 'enable_prepopulate',
'label' => __( 'Enable populating field values for this form using current post', 'pods-gravity-forms' ),
'type' => 'checkbox',
'choices' => array(
array(
'value' => 1,
'label' => __( 'Enable populating field values for this form using the current post ID (only works on singular template)', 'pods-gravity-forms' ),
'name' => 'enable_prepopulate',
),
),
);
}
$settings['advanced']['fields'][] = array(
'name' => 'delete_entry',
'label' => __( 'Delete Gravity Form Entry on submission', 'pods-gravity-forms' ),
'type' => 'checkbox',
'choices' => array(
array(
'value' => 1,
'label' => __( 'Delete entry after processing', 'pods-gravity-forms' ),
'name' => 'delete_entry',
),
),
);
$addon_slug = $this->get_slug();
add_filter( "gform_{$addon_slug}_field_map_choices", array( $this, 'add_field_map_choices' ) );
$settings['advanced']['fields'][] = array(
'name' => 'feed_condition',
'label' => __( 'Conditional Logic', 'pods-gravity-forms' ),
'checkbox_label' => __( 'Enable', 'pods-gravity-forms' ),
'type' => 'feed_condition',
);
return $settings;
}
/**
* Prepare fields for meta field mapping.
*
* Props go to GF Post Creation add-on for the initial source of this method.
*
* @param string $pod_name Current pod name
* @param string $pod_type Current pod type
* @param string[] $blacklisted_keys Meta keys to exclude
*
* @uses GFFormsModel::get_custom_field_names()
*
* @return array
*/
public function get_meta_field_map( $pod_name = '', $pod_type = '', $blacklisted_keys = array() ) {
// Setup meta fields array.
$meta_fields = array(
array(
'label' => esc_html__( 'Select a Custom Field Name', 'pods-gravity-forms' ),
'value' => '',
),
);
///////////////////
// Custom fields
///////////////////
// Get most used post meta keys
$meta_keys = $this->get_custom_field_names( $pod_name, $pod_type );
// If no meta keys exist, return an empty array.
if ( empty( $meta_keys ) ) {
return array();
}
// Add post meta keys to the meta fields array.
foreach ( $meta_keys as $meta_key ) {
$meta_fields[] = array(
'label' => $meta_key,
'value' => $meta_key,
);
}
///////////////////
// Custom key
///////////////////
$meta_fields[] = array(
'label' => esc_html__( 'Add New Custom Field Name', 'pods-gravity-forms' ),
'value' => 'gf_custom',
);
return $meta_fields;
}
/**
* Get most common custom field names from DB.
*
* @param string $pod_name Current pod name
* @param string $pod_type Current pod type
* @param string[] $blacklisted_keys Meta keys to exclude
*
* @return string[]
*/
public function get_custom_field_names( $pod_name = '', $pod_type = '', $blacklisted_keys = array() ) {
global $wpdb;
$object_table = $wpdb->posts;
$meta_table = $wpdb->postmeta;
$id_col = 'ID';
$meta_id_col = 'post_id';
$blacklist_where = "
AND `object`.`post_type` LIKE '_pods_%'
";
if ( 'taxonomy' === $pod_type ) {
$object_table = $wpdb->terms;
$meta_table = $wpdb->termmeta;
$id_col = 'term_id';
$meta_id_col = 'term_id';
$blacklist_where = '';
} elseif ( 'user' === $pod_type ) {
$object_table = $wpdb->users;
$meta_table = $wpdb->usermeta;
$id_col = 'ID';
$meta_id_col = 'user_id';
$blacklist_where = '';
} elseif ( 'comment' === $pod_type ) {
$object_table = $wpdb->users;
$meta_table = $wpdb->usermeta;
$id_col = 'comment_ID';
$meta_id_col = 'comment_id';
$blacklist_where = '';
}
$where = '';
$pods_blacklist_keys = array();
if ( $blacklist_where ) {
$sql = "
SELECT `meta`.`meta_key`
FROM `{$meta_table}` AS `meta`
LEFT JOIN `{$object_table}` AS `object` ON `object`.`{$id_col}` = `meta`.`{$meta_id_col}`
WHERE
`meta`.`meta_key` NOT LIKE '\_%' {$blacklist_where}
GROUP BY `meta`.`meta_key`
";
$pods_blacklist_keys = $wpdb->get_col( $sql );
}
$pods_blacklist_keys = array_merge( $pods_blacklist_keys, $blacklisted_keys );
$pods_blacklist_keys = array_unique( $pods_blacklist_keys );
$pods_blacklist_keys = array_filter( $pods_blacklist_keys );
if ( $pods_blacklist_keys ) {
$placeholders = array_fill( 0, count( $pods_blacklist_keys ), '%s' );
$where = "
AND `meta`.`meta_key` NOT IN ( " . implode( ", ", $placeholders ) . " )
";
$where = $wpdb->prepare( $where, $pods_blacklist_keys );
}
$sql = "
SELECT `meta`.`meta_key`, COUNT(*) AS `total_count`
FROM `{$meta_table}` AS `meta`
WHERE
`meta`.`meta_key` NOT LIKE '\_%' {$where}
GROUP BY `meta`.`meta_key`
ORDER BY `total_count` DESC
LIMIT 50
";
$meta_keys = $wpdb->get_col( $sql );
if ( $meta_keys ) {
natcasesort( $meta_keys );
}
return $meta_keys;
}
/**
* @param $choices
*
* @return array
*/
public function add_field_map_choices( $choices ) {
// Remove first choice
array_shift( $choices );
$choices = array_merge(
array(
// Add first choice back
array(
'value' => '',
'label' => __( 'Select a Field', 'gravityforms' ), // Use gravtiyforms text domain here
),
// Make custom override first option
array(
'value' => 'gf_custom',
'label' => __( 'Custom override value', 'pods-gravity-forms' ),
),
),
$choices,
array(
array(
'value' => 'transaction_id',
'label' => 'Transaction ID',
),
array(
'value' => 'payment_amount',
'label' => 'Payment Amount',
),
array(
'value' => 'payment_date',
'label' => 'Payment Date',
),
array(
'value' => 'payment_status',
'label' => 'Payment Status',
),
)
);
foreach ( $choices as $k => $choice ) {
if ( '_pods_item_id' === $choice['value'] ) {
unset( $choices[ $k ] );
$choices = array_values( $choices );
break;
}
}
return $choices;
}
/***
* Renders and initializes a drop down field based on the $field array
*
* @param array $field - Field array containing the configuration options of this field
* @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
*
* @return string The HTML for the field
*/
public function settings_select( $field, $echo = true ) {
$has_gf_custom = false;
if ( ! empty( $field['choices'] ) ) {
foreach ( $field['choices'] as $choice ) {
if ( isset( $choice['value'] ) && 'gf_custom' === $choice['value'] ) {
$has_gf_custom = true;
break;
}
}
}
// Select has no custom choice or we already took over the first select.
if ( empty( $field['choices'] ) || ! $has_gf_custom || ! empty( $field['_pods_custom_select'] ) ) {
return parent::settings_select( $field, $echo );
}
// Already doing custom select.
if ( ! empty( $field['type'] ) && 'generic_map' === $field['type'] ) {
return parent::settings_select( $field, $echo );
}
$field['_pods_custom_select'] = true;
return parent::settings_select_custom( $field, $echo );
}
/**
* @return string|void
*/
public function field_map_title() {
return __( 'Pod Field', 'pods-gravity-forms' );
}
/**
* @return array
*/
public function feed_list_columns() {
return array(
'feedName' => __( 'Name', 'pods-gravity-forms' ),
'pod' => __( 'Pod', 'pods-gravity-forms' )
);
}
/**
* @param $feed
*
* @return string
*/
public function get_column_value_pod( $feed ) {
return '<strong>' . $feed['meta']['pod'] . '</strong>';
}
/**
*
*/
public function init_admin() {
parent::init_admin();
add_action( 'gform_field_standard_settings', array( $this, 'populate_related_items_settings' ), 10, 2 );
add_filter( 'gform_tooltips', array( $this, 'populate_related_items_tooltip' ) );
add_action( 'gform_editor_js', array( $this, 'populate_related_items_editor_script' ) );
}
/**
* @param $position
* @param $form_id
*/
public function populate_related_items_settings( $position, $form_id ) {
if ( -1 === $position ) {
?>
<li class="pods_populate_related_items_setting field_setting">
<?php _e( 'Pods', 'pods-gravity-forms' ); ?><br />
<input type="checkbox" id="pods_populate_related_items_value" onclick="SetFieldProperty('pods_populate_related_items', this.checked);" />
<label for="pods_populate_related_items_value" class="inline">
<?php _e( 'Populate Related Items (requires a feed configured)', 'pods-gravity-forms' ); ?>
<?php gform_tooltip( 'form_pods_populate_related_items_value' ) ?>
</label>
</li>
<?php
}
}
/**
*
*/
public function populate_related_items_editor_script() {
?>
<script type='text/javascript'>
fieldSettings['select'] += ', .pods_populate_related_items_setting';
fieldSettings['multiselect'] += ', .pods_populate_related_items_setting';
fieldSettings['checkbox'] += ', .pods_populate_related_items_setting';
fieldSettings['radio'] += ', .pods_populate_related_items_setting';
jQuery( document ).bind( 'gform_load_field_settings', function ( event, field, form ) {
jQuery( '#pods_populate_related_items_value' ).attr( 'checked', field['pods_populate_related_items'] == true );
} );
</script>
<?php
}
/**
* @param $tooltips
*
* @return mixed
*/
public function populate_related_items_tooltip( $tooltips ) {
$tooltips['form_pods_populate_related_items_value'] = sprintf( '<h6>%s</h6> %s', __( 'Populate Related Items from Pods', 'pods-gravity-forms' ), __( 'Check this box to populate the related items from Pods instead of keeping the list up-to-date manually.' ) );
return $tooltips;
}
/**
* Init integration.
*/
public function init() {
parent::init();
if ( ! $this->is_gravityforms_supported() ) {
return;
}
// Handle normal forms.
add_filter( 'gform_pre_render', array( $this, '_gf_pre_render' ), 9, 3 );
add_filter( 'gform_admin_pre_render', array( $this, '_gf_pre_render' ), 9, 1 );
add_filter( 'gform_pre_process', array( $this, '_gf_pre_process' ) );
// Handle merge tags
add_filter( 'gform_custom_merge_tags', array( $this, '_gf_custom_merge_tags' ), 10, 2 );
add_filter( 'gform_merge_tag_data', array( $this, '_gf_add_merge_tags' ), 10, 3 );
add_filter( 'gform_replace_merge_tags', array( $this, '_gf_replace_merge_tags' ), 10, 2 );
// Handle entry detail edits.
add_action( 'gform_pre_entry_detail', array( $this, '_gf_pre_entry_detail' ), 10, 2 );
add_action( 'check_admin_referer', array( $this, '_check_admin_referer' ), 10, 2 );
add_action( 'gform_entry_detail_content_before', array( $this, '_gf_entry_detail_content_before' ), 10, 2 );
// Handle entry updates.
add_action( 'gform_post_update_entry', array( $this, '_gf_post_update_entry' ), 9, 2 );
add_action( 'gform_after_update_entry', array( $this, '_gf_after_update_entry' ), 9, 3 );
// Handle Payment Add-on callbacks.
add_action( 'gform_action_pre_payment_callback', array( $this, '_gf_action_pre_payment_callback' ), 10, 2 );
}
/**
* Processes feed action.
*
* @since 1.4.2
* @access public
*
* @param array $feed The Feed Object currently being processed.
* @param array $entry The Entry Object currently being processed.
* @param array $form The Form Object currently being processed.
*
* @return array|null Returns a modified entry object or null.
*/
public function process_feed( $feed, $entry, $form ) {
if ( empty( $this->pods_gf[ $feed['id'] ] ) ) {
return null;
}
$form = $this->_gf_pre_render( $form, false, $entry );
/** @var Pods_GF $pods_gf */
$pods_gf = $this->pods_gf[ $feed['id'] ];
try {
$pods_gf->options['entry'] = $entry;
// Ensure other custom Pods GF submission handling does not duplicate.
remove_action( 'gform_after_submission_' . $form['id'], [ $pods_gf, '_gf_after_submission' ] );
$id = $pods_gf->_gf_to_pods_handler( $form, $entry );
// Set post_id if we have it.
if ( 'post_type' === $pods_gf->pod->pod_data['type'] ) {
$entry['post_id'] = $id;
return $entry;
}
}
catch ( Exception $e ) {
// @todo Log something to the form entry
if ( defined( 'WP_CLI' ) ) {
\WP_CLI::warning( 'Feed processing error: ' . $e->getMessage() );
}
}
return null;
}
/**
* Action handler for Gravity Forms: gform_action_pre_payment_callback.
*
* @param array $action Action data being saved.
* @param array $entry GF Entry array.
*/
public function _gf_action_pre_payment_callback( $action, $entry ) {
$form = GFAPI::get_form( $entry['form_id'] );
$this->_gf_pre_process( $form );
}
/**
* Action handler for Gravity Forms: gform_post_update_entry.
*
* @param array $entry GF Entry array
* @param array $original_entry Original GF Entry array
*/
public function _gf_post_update_entry( $entry, $original_entry ) {
$form = GFAPI::get_form( $entry['form_id'] );
$this->_gf_pre_process( $form );
}
/**
* Action handler for Gravity Forms: gform_after_update_entry.
*
* @param array $form GF Form array
* @param array $entry GF Entry array
* @param array $original_entry Original GF Entry array
*/
public function _gf_after_update_entry( $form, $entry, $original_entry ) {
$this->_gf_pre_process( $form );
}
/**
* Hook into action to setup Pods GF add-on hooks before form entry edit form is shown.
*
* @param array|object $form GF form data.
* @param array|object $entry GF entry data.
*/
public function _gf_pre_entry_detail( $form, $entry ) {
// Remove other hooks for workarounds we don't need if this hook now exists. Not in GF when this was written.
remove_action( 'check_admin_referer', array( $this, '_check_admin_referer' ) );
remove_action( 'gform_entry_detail_content_before', array( $this, '_gf_entry_detail_content_before' ) );
$this->_gf_pre_render( $form, false, $entry, true );
}
/**
* Hook into check_admin_referer to setup Pods GF add-on hooks before updating form entry.
*
* @param string $action The nonce action.
* @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
* 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
*/
public function _check_admin_referer( $action, $result ) {
// So hacky, we need GF to add a better hook than this.
if ( $result && 'gforms_save_entry' === $action && class_exists( 'GFEntryDetail' ) ) {
$form = GFEntryDetail::get_current_form();
if ( $form ) {
$this->_gf_pre_process( $form );
}