-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-gf-zapier.php
More file actions
1668 lines (1408 loc) · 46.2 KB
/
class-gf-zapier.php
File metadata and controls
1668 lines (1408 loc) · 46.2 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
// Include the Gravity Forms Add-On Framework
GFForms::include_feed_addon_framework();
use Gravity_Forms\Gravity_Forms_Zapier\REST;
class GF_Zapier extends GFFeedAddOn {
/**
* Holds the cached request bodies for the current submission.
*
* @since 1.9
* @since 4.0 Changed default from null to empty array.
*
* @var array
*/
private static $_current_body = array();
/**
* Contains an instance of this class, if available.
*
* @since 4.0
* @var GF_Zapier $_instance If available, contains an instance of this class
*/
private static $_instance = null;
/**
* Enable background feed processing.
*
* @since 4.3
*
* @var bool
*/
protected $_async_feed_processing = true;
/**
* Defines the version of the Gravity Forms Zapier Add-On Add-On.
*
* @since 4.0
* @var string $_version Contains the version.
*/
protected $_version = GF_ZAPIER_VERSION;
/**
* Defines the minimum Gravity Forms version required.
* @since 4.0
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = GF_ZAPIER_MIN_GF_VERSION;
/**
* Defines the plugin slug.
*
* @since 4.0
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformszapier';
/**
* Defines the main plugin file.
*
* @since 4.0
*
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformszapier/zapier.php';
/**
* Defines the full path to this class file.
*
* @since 4.0
*
* @var string $_full_path The full path.
*/
protected $_full_path = __FILE__;
/**
* Defines the URL where this add-on can be found.
*
* @since 4.0
*
* @var string
*/
protected $_url = 'https://gravityforms.com';
/**
* Defines the title of this add-on.
*
* @since 4.0
*
* @var string $_title The title of the add-on.
*/
protected $_title = 'Gravity Forms Zapier Add-On';
/**
* Defines the short title of the add-on.
*
* @since 4.0
*
* @var string $_short_title The short title.
*/
protected $_short_title = 'Zapier';
/**
* Defines if Add-On should use Gravity Forms server for update data.
*
* @since 4.0
*
* @var bool
*/
protected $_enable_rg_autoupgrade = true;
/* Members plugin integration */
/**
* Capabilities required for this add-on.
*
* @since 4.0
*
* @var array
*/
protected $_capabilities = array( 'gravityforms_zapier', 'gravityforms_zapier_uninstall' );
/**
* Permissions required to uninstall this add-on.
*
* @since 4.0
*
* @var string
*/
protected $_capabilities_uninstall = 'gravityforms_zapier_uninstall';
/**
* Permissions required to access the add-on on the Gravity Forms settings page.
*
* @since 4.0
*
* @var string
*/
protected $_capabilities_settings_page = 'gravityforms_zapier';
/**
* Permissions required to access the add-on on the form settings page.
*
* @since 4.0
*
* @var string
*/
protected $_capabilities_form_settings = 'gravityforms_zapier';
/**
* Returns an instance of this class, and stores it in the $_instance property.
*
* @since 4.0
*
* @return GF_Zapier $_instance An instance of the GF_Zapier class
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GF_Zapier();
}
return self::$_instance;
}
// # INIT ----------------------------------------------------------------------------------------------------------
/**
* Fired in the front end and admin. Subscribes to appropriate actions/filters in order to initialize the add-on.
*
* @since 4.0
*/
public function init() {
parent::init();
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 11 );
add_action( 'gform_pre_validation', array( $this, 'populate_product_inputs_submission' ) );
add_action( 'gform_post_add_entry', array( $this, 'populate_product_inputs_add_entry' ), 10, 2 );
$this->add_delayed_payment_support(
array(
'option_label' => esc_html__( 'Send feed to Zapier only when payment is received.', 'gravityformszapier' ),
)
);
}
/**
* Includes the GF REST API, if not already included.
*
* @since 4.1
*/
public function init_rest_api() {
if ( class_exists( 'GF_REST_Controller' ) ) {
return;
}
if ( is_callable( array( 'GFWebAPI', 'get_instance' ) ) ) {
GFWebAPI::get_instance()->init_v2();
} else {
( new GFWebAPI() )->init_v2();
}
}
/**
* Registers the REST API endpoints.
*
* @since 4.0
*/
public function register_rest_routes() {
$this->init_rest_api();
require_once plugin_dir_path( __FILE__ ) . 'includes/rest/class-zapier-controller.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/rest/class-requirements-controller.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/rest/class-sample-entry-controller.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/rest/class-sample-entries-controller.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/rest/class-transfer-entries-controller.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/rest/class-feeds-controller.php';
$controllers = array(
REST\Requirements_Controller::class,
REST\Sample_Entry_Controller::class,
REST\Sample_Entries_Controller::class,
REST\Transfer_Entries_Controller::class,
REST\Feeds_Controller::class,
);
foreach ( $controllers as $controller ) {
$controller_obj = new $controller();
$controller_obj->register_routes();
}
}
/**
* For form submissions made by Zapier, populates the product name and product price inputs of single product and
* hidden product fields.
*
* @since 4.0
*
* @param array $form Current form object.
*
* @return array
*/
public function populate_product_inputs_submission( $form ) {
// Ignore requests that did not come from Zapier.
if ( ! $this->is_zapier_request() ) {
return $form;
}
$product_inputs = $this->get_product_inputs( $form );
foreach ( $product_inputs as $field_id => $product ) {
$_POST["input_{$field_id}_1"] = $product['name'];
$_POST["input_{$field_id}_2"] = $product['price'];
}
return $form;
}
/**
* For request made by Zapier, populates the product name and product price inputs of single product and hidden product fields, and updates the entry with those new values.
*
* @since 4.0
*
* @param array $entry Current entry object.
* @param array $form Current form object.
*/
public function populate_product_inputs_add_entry( $entry, $form ) {
// Ignore requests that did not come from Zapier.
if ( ! $this->is_zapier_request() ) {
return;
}
$product_inputs = $this->get_product_inputs( $form, $entry );
if ( empty( $product_inputs ) ) {
return;
}
foreach ( $product_inputs as $field_id => $product ) {
$entry["{$field_id}.1"] = $product['name'];
$entry["{$field_id}.2"] = $product['price'];
}
GFAPI::update_entry( $entry );
}
/**
* Determines whether the current request was made by the Zapier App.
*
* @since 4.0
*
* @return bool Returns true if the current request was made by the Gravity Forms Zapier App. Returns false otherwise
*/
public function is_zapier_request() {
return rgar( $_SERVER, 'HTTP_X_APPLICATION_SOURCE' ) == 'Zapier Integration';
}
/**
* Check to see if there are legacy feeds.
* Used to filter the feed list, control menu links.
*
* @since 4.0
*
* @param null|int $form_id The form ID.
*
* @return bool
*/
public function has_legacy_feeds( $form_id = null ) {
$feeds = $this->get_feeds( $form_id );
foreach ( $feeds as $feed ) {
$is_legacy = $this->is_legacy_feed( $feed );
$this->log_debug( 'legacy check ' . $is_legacy );
if ( $is_legacy ) {
$this->log_debug( 'there are legacy feeds for feed id ' . $feed['id'] . ', returning true' );
return true;
}
}
$this->log_debug( 'no legacy found' );
return false;
}
/**
* Check if a feed (or current feed) is legacy.
*
* @since 4.0
*
* @param array $feed GF Feed Array.
*
* @return bool
*/
public function is_legacy_feed( $feed = null ) {
if ( null === $feed ) {
$feed = $this->get_current_feed();
}
return (bool) rgar( $feed['meta'], 'legacy' );
}
/**
* Remove items from Feed Table when Feeds are hidden.
*
* @since 4.0
*
* @param array $form GF Form array.
*
* @return GFAddOnFeedsTable
*/
public function get_feed_table( $form ) {
$feeds = $this->get_feeds( rgar( $form, 'id' ) );
// Disable rendering of feeds unless toggled true.
if ( ! $this->should_display_feeds() ) {
$feeds = array();
}
$columns = $this->feed_list_columns();
$column_value_callback = array( $this, 'get_column_value' );
$bulk_actions = $this->get_bulk_actions();
$action_links = $this->get_action_links();
$no_item_callback = array( $this, 'feed_list_no_item_message' );
$message_callback = '__return_false';
require_once $this->get_base_path() . '/includes/class-feeds-list-table.php';
return new GF_Zapier_Feeds_List_Table( $feeds, $this->_slug, $columns, $bulk_actions, $action_links, $column_value_callback, $no_item_callback, $message_callback, $this );
}
// # FEED SETTINGS -------------------------------------------------------------------------------------------------
/**
* Restores the previous value of the given field.
*
* @since 4.1
*
* @param array $field The current field.
*
* @return string|null
*/
public function restore_previous_value( $field ) {
$name = rgar( $field, 'name' );
$value = rgar( $this->get_previous_settings(), $name );
if ( ! $this->is_gravityforms_supported( '2.5-rc-1' ) ) {
global $_gaddon_posted_settings;
$_gaddon_posted_settings[ $name ] = $value;
}
return $value;
}
/**
* Setup fields for feed settings.
*
* @since 4.0
*
* @return array
*/
public function feed_settings_fields() {
$feed_name = array(
'label' => esc_html__( 'Name', 'gravityformszapier' ),
'name' => 'feedName',
'type' => 'text',
'class' => 'medium',
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Name', 'gravityformszapier' ),
esc_html__( 'This is a friendly name so you know what Zap is run when this form is submitted.', 'gravityformszapier' )
),
);
$zap_url = array(
'label' => esc_html__( 'URL', 'gravityformszapier' ),
'name' => 'zapURL',
'type' => 'text',
'class' => 'large',
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'URL', 'gravityformszapier' ),
esc_html__( 'This is the URL provided by Zapier when you created your Zap on their website. This is the location to which your form data will be submitted to Zapier for additional processing.', 'gravityformszapier' )
),
);
$admin_labels = array(
'label' => esc_html__( 'Use Admin Labels', 'gravityformszapier' ),
'name' => 'adminLabels',
'type' => 'radio',
'choices' => array(
array(
'label' => 'Yes',
'value' => '1',
),
array(
'label' => 'No',
'value' => '0',
),
),
'horizontal' => true,
'default_value' => '0',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Use Admin Labels', 'gravityformszapier' ),
esc_html__( 'By default the field labels will be sent to Zapier. Enable this option to send the field admin labels when available.', 'gravityformszapier' )
),
);
if ( ! $this->is_legacy_feed() ) {
$save_callback = array( $this, 'restore_previous_value' );
$feed_name['readonly'] = 'readonly';
$feed_name['save_callback'] = $save_callback;
$zap_url['readonly'] = 'readonly';
$zap_url['save_callback'] = $save_callback;
$admin_labels['disabled'] = 'disabled';
$admin_labels['save_callback'] = $save_callback;
}
return array(
array(
'fields' => array(
$feed_name,
$zap_url,
$admin_labels,
array(
'name' => 'zapier_conditional_enabled',
'label' => __( 'Conditional Logic', 'gravityformszapier' ),
'type' => 'feed_condition',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Conditional Logic', 'gravityformszapier' ),
esc_html__( 'When Conditional Logic is enabled, submissions for this form will only be sent to Zapier when the condition is met. When disabled, all submissions for this form will be sent to Zapier.', 'gravityformszapier' )
),
),
array(
'name' => 'legacy',
'type' => 'hidden',
),
array(
'name' => 'legacy_id',
'type' => 'hidden',
),
array(
'name' => 'zapID',
'type' => 'hidden',
),
),
),
);
}
/**
* Setup columns for feed list table.
*
* @since 4.0
*
* @return array
*/
public function feed_list_columns() {
return array(
'feedName' => esc_html__( 'Name', 'gravityformszapier' ),
'zapURL' => esc_html__( 'Zap URL', 'gravityformszapier' ),
);
}
// # FEED PROCESSING -------------------------------------------------------------------------------------------------
/**
* Send trigger request to Zapier.
*
* @since 4.0
*
* @param array $feed The current Feed object.
* @param array $entry The current Entry object.
* @param array $form The current Form object.
*
* @return bool
*/
public function process_feed( $feed, $entry, $form ) {
$body = $this->get_body( $entry, $form, $feed );
$headers = array();
if ( empty( $entry ) ) {
$headers['X-Hook-Test'] = 'true';
}
$json_body = json_encode( $body );
if ( empty( $body ) ) {
$this->log_debug( 'There is no field data to send to Zapier.' );
return false;
}
$this->log_debug( 'Posting to url: ' . $feed['meta']['zapURL'] . ' data: ' . print_r( $body, true ) );
$form_data = array( 'sslverify' => false, 'ssl' => true, 'body' => $json_body, 'headers' => $headers );
$response = wp_remote_post( $feed['meta']['zapURL'], $form_data );
if ( is_wp_error( $response ) ) {
$this->log_error( 'The following error occurred: ' . print_r( $response, true ) );
return false;
} else {
$this->log_debug( 'Successful response from Zap: ' . print_r( $response, true ) );
if ( ! empty( $entry ) ) {
$this->log_debug( 'Marking entry #'.$entry['id'].' as fulfilled.' );
gform_update_meta( $entry['id'], $this->_slug.'_is_fulfilled', true );
}
return true;
}
}
/**
* Returns the body of the request to be sent to zapier.
*
* @since 4.0
*
* @param array $entry The current Entry array.
* @param array $form The current Form array.
* @param bool|array $feed The current Feed array.
*
* @return array Returns the request body to be sent to Zapier as an associative array.
*/
public function get_body( $entry, $form, $feed = false ) {
$admin_labels = is_array( $feed ) ? rgars( $feed, 'meta/adminLabels' ) : false;
$cache_key = get_current_blog_id() . '_' . rgar( $form, 'id' ) . '_' . rgar( $entry, 'id', 'sample' ) . '_' . $admin_labels;
/**
* Determines if the Zapier add-on should use the body already stored.
*
* @since 2.1.1
*
* @param bool true If the current body should be used. Defaults to true.
* @param array $entry The Entry array.
* @param array $form The Form array.
* @param array $feed The Feed array.
*/
if ( apply_filters( 'gform_zapier_use_stored_body', true, $entry, $form, $feed ) ) {
$current_body = rgar( self::$_current_body, $cache_key );
if ( ! empty( $current_body ) ) {
$this->log_debug( __METHOD__ . "(): Using cached request body ({$cache_key})." );
return $current_body;
}
}
$use_sample_value = empty( $entry );
$body = array();
$body[ esc_html__( 'Form ID', 'gravityformszapier' ) ] = rgar( $form, 'id' );
$body[ esc_html__( 'Form Title', 'gravityformszapier' ) ] = rgar( $form, 'title' );
$entry_properties = $this->get_entry_properties();
foreach ( $entry_properties as $property_key => $property_config ) {
$key = $this->get_body_key( $body, $property_config['label'] );
if ( $use_sample_value ) {
$value = $property_config['sample_value'];
} else {
$value = rgar( $entry, $property_key );
}
$body[ $key ] = $value;
}
$entry_meta = GFFormsModel::get_entry_meta( $form['id'] );
foreach ( $entry_meta as $meta_key => $meta_config ) {
$key = $this->get_body_key( $body, $meta_config['label'] );
if ( $use_sample_value ) {
$body[ $key ] = rgar( $meta_config, 'is_numeric' ) ? rand( 0, 10 ) : 'Sample value';
} else {
$body[ $key ] = rgar( $entry, $meta_key );
}
}
foreach ( $form['fields'] as $field ) {
$input_type = GFFormsModel::get_input_type( $field );
if ( $input_type == 'honeypot' || $field->displayOnly ) {
// Skip the honeypot and displayOnly fields.
continue;
}
if ( ! $use_sample_value ) {
$field_value = GFFormsModel::get_lead_field_value( $entry, $field );
$field_value = apply_filters( 'gform_zapier_field_value', $field_value, $form['id'], $field->id, $entry );
} else {
$field_value = $this->get_sample_value( $field );
$field_value = apply_filters( 'gform_zapier_sample_field_value', $field_value, $form['id'], $field->id );
}
$field_label = $this->get_body_label( $admin_labels, $field );
$inputs = $field instanceof GF_Field ? $field->get_entry_inputs() : rgar( $field, 'inputs' );
if ( is_array( $inputs ) && ( is_array( $field_value ) || $use_sample_value ) ) {
// Handling multi-input fields.
$non_blank_items = array();
// Field has inputs, complex field like name, address and checkboxes. Get individual inputs.
foreach ( $inputs as $input ) {
$input_label = $this->get_body_label( $admin_labels, $field, $input['id'] );
$key = $this->get_body_key( $body, $input_label );
$field_id = (string) $input['id'];
$input_value = rgar( $field_value, $field_id );
$body[ $key ] = $input_value;
if ( ! rgblank( $input_value ) ) {
$non_blank_items[] = $input_value;
}
}
// Also adding an item for the "whole" field, which will be a concatenation of the individual inputs.
switch ( $input_type ) {
case 'checkbox' :
// Checkboxes will create a comma separated list of values.
$key = $this->get_body_key( $body, $field_label );
$body[ $key ] = implode( ', ', $non_blank_items );
break;
case 'name' :
case 'address' :
// Name and address will separate inputs by a single blank space.
$key = $this->get_body_key( $body, $field_label );
$body[ $key ] = implode( ' ', $non_blank_items );
break;
case 'calculation':
case 'hiddenproduct':
case 'singleproduct':
if ( $use_sample_value ) {
$name = rgar( $field_value, $field->id.'.1' );
$price = rgar( $field_value, $field->id.'.2' );
$quantity = rgar( $field_value, $field->id.'.3' );
$body['Products /'][] = array(
'product_id' => $field->id,
'product_name' => $name,
'product_quantity' => $quantity,
'product_price' => $price,
'product_price_with_options' => $price + 10 + 20,
'product_subtotal' => ( $price + 10 + 20 ) * $quantity,
'product_options' => 'Option 1, Option 2',
);
} else {
// We get all product fields at once, so skipped if products has been set.
if ( isset( $body['Products /'] ) ) {
break;
}
$body['Products /'] = $this->get_products_array( $form, $entry );
}
break;
}
} else {
$key = $this->get_body_key( $body, $field_label );
switch ( $input_type ) {
case 'list' :
if ( $field->enableColumns ) {
// Keep for backwards compatibility.
$body[ $key ] = $field_value;
// Add line-item support to list.
$body[ $key.' /' ] = maybe_unserialize( $field_value );
} else {
$body[ $key ] = maybe_unserialize( $field_value );
}
break;
default :
if ( $field->type == 'product' ) {
// Keep for backwards compatibility.
$body[ $key ] = $field_value;
if ( $use_sample_value ) {
list( $name, $price ) = explode( '|', $field_value );
$quantity = rand( 1, 10 );
$body['Products /'][] = array(
'product_id' => $field->id,
'product_name' => $name,
'product_quantity' => $quantity,
'product_price' => $price,
'product_price_with_options' => $price + 10 + 20,
'product_subtotal' => ( $price + 10 + 20 ) * $quantity,
'product_options' => 'Option 1, Option 2'
);
} else {
// We get all product fields at once, so skipped if products has been set.
if ( isset( $body['Products /'] ) ) {
break;
}
$body['Products /'] = $this->get_products_array( $form, $entry );
}
} elseif ( $field->type == 'shipping' ) {
// Keep old shipping value for backward compatibility.
$body[ $key ] = rgblank( $field_value ) ? '' : $field_value;
// Set shipping as a faux product.
if ( $use_sample_value ) {
if ( $field->get_input_type() !== 'singleshipping' ) {
list( $name, $price ) = explode( '|', $field_value );
$name = 'Shipping ('.$name.')';
} else {
$name = 'Shipping';
$price = $field_value;
}
$body['Products /'][] = array(
'product_id' => $field->id,
'product_name' => $name,
'product_quantity' => 1,
'product_price' => $price,
'product_price_with_options' => $price,
'product_subtotal' => $price,
'product_options' => '',
);
}
} else {
$body[ $key ] = rgblank( $field_value ) ? '' : $field_value;
}
}
}
}
/**
* Allows the request body sent to zapier to be filtered
*
* @param array $body An associative array containing the request body that will be sent to Zapier.
* @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.
*
* @since 3.1.1
*/
self::$_current_body[ $cache_key ] = gf_apply_filters(
array( 'gform_zapier_request_body', rgar( $form, 'id' ) ),
$body,
$feed,
$entry,
$form
);
$this->log_debug( __METHOD__ . "(): Request body cached ({$cache_key})." );
return rgar( self::$_current_body, $cache_key );
}
/**
* Retrieve a sample value for the current field.
*
* @since 4.0
*
* @param GF_Field $field The field properties.
*
* @return array|string
*/
public function get_sample_value( $field ) {
$default_value = 'Sample value';
$always_text = array( 'survey', 'quiz', 'poll' );
$field_id = absint( $field->id );
$choice_type = in_array( $field->type, $always_text ) || ! $field->enableChoiceValue ? 'text' : 'value';
switch ( $field->get_input_type() ) {
case 'address' :
$value[ $field_id.'.1' ] = 'Bag End';
$value[ $field_id.'.2' ] = 'Bagshot Row';
$value[ $field_id.'.3' ] = 'Hobbiton';
$value[ $field_id.'.4' ] = 'Shire';
$value[ $field_id.'.5' ] = '1234';
$value[ $field_id.'.6' ] = 'Middle Earth';
break;
case 'name' :
$value[ $field_id.'.2' ] = 'Mr.';
$value[ $field_id.'.3' ] = 'Bilbo';
$value[ $field_id.'.4' ] = 'L.';
$value[ $field_id.'.6' ] = 'Baggins';
$value[ $field_id.'.8' ] = 'Ring-bearer';
$inputs = $field->get_entry_inputs();
if ( ! is_array( $inputs ) ) {
$value = implode( ' ', $value );
}
break;
case 'calculation' :
$value[ $field_id.'.1' ] = $field->label;
$value[ $field_id.'.2' ] = 10;
$value[ $field_id.'.3' ] = 2;
break;
case 'checkbox' :
$value = array();
if ( is_array( $field->choices ) ) {
$choice_number = 1;
foreach ( $field->choices as $choice ) {
if ( $choice_number % 10 == 0 ) {
$choice_number ++;
}
$choice_value = rgar( $choice, $choice_type );
if ( $field->enablePrice ) {
$price = rgempty( 'price', $choice ) ? 0 : GFCommon::to_number( rgar( $choice, 'price' ) );
$choice_value .= '|'.$price;
}
$input_id = $field_id.'.'.$choice_number ++;
$value[ $input_id ] = $choice_value;
}
}
break;
case 'creditcard' :
$value[ $field_id.'.1' ] = str_repeat( 'X', 16 );
$value[ $field_id.'.4' ] = 'Visa';
break;
case 'date' :
$value = date( 'Y-m-d' );
break;
case 'email' :
$value = 'test@domain.dev';
break;
case 'fileupload' :
case 'signature' :
$value = 'http://domain.dev/some_location/file.png';
break;
case 'list' :
if ( ! $field->enableColumns ) {
$max = 2;
} else {
$max = count( $field->choices ) * 2;
}
$value = array_fill( 0, $max, $default_value );
$value = serialize( $field->create_list_array( $value ) );
break;
case 'multiselect' :
$value = rgars( $field->choices, '0/'.$choice_type );
if ( isset( $field->choices[1] ) ) {
$value .= ','.rgar( $field->choices[1], $choice_type );
}
break;
case 'number' :
case 'total' :
$value = 100;
break;
case 'price' :
$value = $field->label.'|10';
break;
case 'phone' :
$value = '(999) 999-9999';
break;
case 'post_image' :
$title = $field->displayTitle ? 'The title' : '';
$caption = $field->displayCaption ? 'The caption' : '';
$description = $field->displayDescription ? 'The description' : '';
$value = 'http://domain.dev/some_location/image.img|:|'.$title.'|:|'.$caption.'|:|'.$description;
break;
case 'hiddenproduct' :
case 'singleproduct' :
$value[ $field_id.'.1' ] = $field->label;
$value[ $field_id.'.2' ] = empty( $field->basePrice ) ? 10 : GFCommon::to_number( $field->basePrice );
$value[ $field_id.'.3' ] = 2;
break;
case 'singleshipping' :
$value = empty( $field->basePrice ) ? 10 : GFCommon::to_number( $field->basePrice );
break;
case 'time' :
$value = '10:30 am';
break;
case 'website' :
$value = 'http://domain.dev';
break;
case 'likert' :
if ( $field->gsurveyLikertEnableMultipleRows ) {
$value = array();
foreach ( $field->inputs as $input ) {
$value[ $input['id'] ] = $this->get_random_choice( $field->choices, $choice_type );
}
} else {
$value = $this->get_random_choice( $field->choices, $choice_type );
}
break;
case 'rank' :
$c = 1;
$value = array();
$choices = $field->choices;
shuffle( $choices );
foreach ( $choices as $choice ) {
$value[] = $c ++.'. '.rgar( $choice, $choice_type );
}
$value = implode( ', ', $value );
break;
default :
$inputs = $field->get_entry_inputs();
if ( $inputs ) {
$value = array();
foreach ( $inputs as $input ) {
$choices = rgar( $input, 'choices' );
if ( is_array( $choices ) ) {
$value[ $input['id'] ] = $this->get_random_choice( $choices, $choice_type );
} else {
$value[ $input['id'] ] = $default_value;
}
}
} elseif ( is_array( $field->choices ) && count( $field->choices ) > 0 ) {
$value = $this->get_random_choice( $field->choices, $choice_type, $field->enablePrice );
} else {
$value = $default_value;
}
}
return $value;
}
/**
* Return a random choice.
*
* @since 4.0
*
* @param array $choices The choices.