forked from wp-premium/gravityformszapier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zapier.php
2105 lines (1724 loc) · 72.9 KB
/
zapier.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
/*
Plugin Name: Gravity Forms Zapier Add-on
Plugin URI: https://gravityforms.com
Description: Integrates Gravity Forms with Zapier, allowing form submissions to be automatically sent to your configured Zaps.
Version: 3.3
Author: Gravity Forms
Author URI: https://gravityforms.com
License: GPL-2.0+
Text Domain: gravityformszapier
Domain Path: /languages
------------------------------------------------------------------------
Copyright 2009-2020 Rocketgenius, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
defined( 'ABSPATH' ) || die();
add_action( 'init', array( 'GFZapier', 'init' ) );
class GFZapier {
private static $slug = 'gravityformszapier';
private static $path = 'gravityformszapier/zapier.php';
private static $url = 'https://www.gravityforms.com';
private static $version = '3.3';
private static $min_gravityforms_version = '1.9.10';
private static $_current_body = null;
/**
* If true, maybe_delay_feed() checks will be bypassed allowing the feeds to be processed.
*
* @var bool
*/
protected static $_bypass_feed_delay = false;
public static function init() {
//supports logging
add_filter( 'gform_logging_supported', array( 'GFZapier', 'set_logging_supported' ) );
if ( basename( $_SERVER['PHP_SELF'] ) == 'plugins.php' ) {
//loading translations
load_plugin_textdomain( 'gravityformszapier', false, '/gravityformszapier/languages' );
add_action( 'after_plugin_row_' . self::$path, array( 'GFZapier', 'plugin_row' ) );
//force new remote request for version info on the plugin page
self::flush_version_info();
}
if ( ! self::is_gravityforms_supported() ) {
return;
}
//loading data lib
require_once( self::get_base_path() . '/data.php' );
if ( is_admin() ) {
//loading translations
load_plugin_textdomain( 'gravityformszapier', false, '/gravityformszapier/languages' );
add_filter( 'transient_update_plugins', array( 'GFZapier', 'check_update' ) );
add_filter( 'site_transient_update_plugins', array( 'GFZapier', 'check_update' ) );
add_action( 'install_plugins_pre_plugin-information', array( 'GFZapier', 'display_changelog' ) );
//add item to form settings menu in expand list
add_action( 'gform_form_settings_menu', array( 'GFZapier', 'add_form_settings_menu' ), 50 );
//add action so that when form is updated, data fields are sent to Zapier
add_action( 'gform_after_save_form', array( 'GFZapier', 'send_form_updates' ), 10, 2 );
// paypal standard plugin integration hooks
if ( self::is_gravityforms_supported( '2.0-beta-2' ) ) {
add_filter( 'gform_addon_feed_settings_fields', array(
'GFZapier',
'add_post_payment_actions',
), 10, 2 );
} else {
add_action( 'gform_paypal_action_fields', array( 'GFZapier', 'add_paypal_settings' ), 10, 2 );
add_filter( 'gform_paypal_save_config', array( 'GFZapier', 'save_paypal_settings' ) );
}
if ( GFForms::get( 'page' ) == 'gf_settings' ) {
//add Zapier link to settings tabs on GF Main Settings page
if ( self::has_access( 'gravityforms_zapier' ) ) {
GFForms::add_settings_page( array(
'name' => self::$slug,
'title' => 'Zapier Settings',
'tab_label' => 'Zapier',
'handler' => array(
'GFZapier',
'settings_page',
),
'',
), self::get_base_url() . '/images/zapier_wordpress_icon_32.png' );
}
}
if ( RGForms::get( 'subview' ) == 'gravityformszapier' ) {
//add page Zapier link will go to
add_action( 'gform_form_settings_page_gravityformszapier', array( 'GFZapier', 'zapier_page' ) );
//loading upgrade lib
if ( ! class_exists( 'GFZapierUpgrade' ) ) {
require_once( 'plugin-upgrade.php' );
}
//loading Gravity Forms tooltips
require_once( GFCommon::get_base_path() . '/tooltips.php' );
add_filter( 'gform_tooltips', array( 'GFZapier', 'tooltips' ) );
}
// Gravity Forms 2.2+ System Status
add_action( 'gform_post_upgrade', array( 'GFZapierData', 'post_gravityforms_upgrade' ), 10, 3 );
add_filter( 'gform_system_report', array( 'GFZapier', 'system_report' ) );
//runs the setup when version changes
self::setup();
} else {
// ManageWP premium update filters
add_filter( 'mwp_premium_update_notification', array( 'GFZapier', 'premium_update_push' ) );
add_filter( 'mwp_premium_perform_update', array( 'GFZapier', 'premium_update' ) );
}
//integrating with Members plugin
if ( function_exists( 'members_get_capabilities' ) ) {
add_filter( 'members_get_capabilities', array( 'GFZapier', 'members_get_capabilities' ) );
}
add_action( 'gform_after_submission', array( 'GFZapier', 'send_form_data_to_zapier' ), 10, 2 );
// handling paypal fulfillment.
add_action( 'gform_paypal_fulfillment', array( 'GFZapier', 'paypal_fulfillment' ), 10, 4 );
// handling fulfillment in other payment addons.
add_action( 'gform_trigger_payment_delayed_feeds', array( 'GFZapier', 'action_trigger_payment_delayed_feeds' ), 10, 4 );
}
public static function add_form_settings_menu( $tabs ) {
$tabs[] = array(
'name' => self::$slug,
'label' => __( 'Zapier', 'gravityforms' ),
'query' => array( 'zid' => null ),
);
return $tabs;
}
public static function zapier_page() {
//see if there is a form id in the querystring
$form_id = RGForms::get( 'id' );
$zapier_id = rgempty( 'gform_zap_id' ) ? rgget( 'zid' ) : rgpost( 'gform_zap_id' );
if ( ! empty( $zapier_id ) ) {
$zapier_id = absint( $zapier_id );
}
if ( ! rgblank( $zapier_id ) ) {
self::zapier_edit_page( $form_id, $zapier_id );
} else {
self::zapier_list_page( $form_id );
}
GFFormSettings::page_footer();
}
private static function zapier_list_page( $form_id ) {
if ( rgpost( 'action' ) == 'delete' && check_admin_referer( 'gform_zapier_list_action', 'gform_zapier_list_action' ) ) {
$zid = $_POST['action_argument'];
if ( ! empty( $zid ) ) {
GFZapierData::delete_feed( $zid );
GFCommon::add_message( __( 'Zap deleted.', 'gravityformszapier' ) );
}
}
GFFormSettings::page_header( __( 'Zapier', 'gravityformszapier' ) );
?>
<script type='text/javascript'>
function DeleteZap(zid) {
//set hidden fields
jQuery('#action').val('delete');
jQuery('#action_argument').val(zid);
jQuery('#zapier_list_form')[0].submit();
}
</script>
<style type='text/css'>
a.limit-text {
display: block;
height: 18px;
line-height: 18px;
overflow: hidden;
padding-right: 5px;
color: #555;
text-overflow: ellipsis;
white-space: nowrap;
}
a.limit-text:hover {
color: #555;
}
th.column-name {
width: 30%;
}
th.column-type {
width: 20%;
}
</style>
<?php
$add_new_url = add_query_arg( array( 'zid' => 0 ) );
?>
<h3>
<span>
<?php esc_html_e( 'Zapier Feeds', 'gravityforms' ) ?>
<a id="add-new-zapier" class="add-new-h2"
href="<?php echo esc_url( $add_new_url ); ?>"><?php esc_html_e( 'Add New', 'gravityformszapier' ) ?></a>
</span>
</h3>
<?php
$zapier_table = new GFZapierTable( $form_id );
$zapier_table->prepare_items();
?>
<form id="zapier_list_form" method="post">
<?php $zapier_table->display(); ?>
<input type="hidden" id="action" name="action" value="">
<input id="action_argument" name="action_argument" type="hidden"/>
<?php wp_nonce_field( 'gform_zapier_list_action', 'gform_zapier_list_action' ) ?>
</form>
<?php
}
private static function zapier_edit_page( $form_id, $zap_id ) {
$zap = empty( $zap_id ) ? array() : GFZapierData::get_feed( $zap_id );
$is_new_zap = empty( $zap_id ) || empty( $zap );
$is_valid = true;
$is_update = false;
$form = RGFormsModel::get_form_meta( $form_id );
if ( rgpost( 'save' ) ) {
check_admin_referer( 'gforms_save_zap', 'gforms_save_zap' );
if ( rgar( $zap, 'url' ) != rgpost( 'gform_zapier_url' ) ) {
$is_update = true;
}
$zap['name'] = sanitize_text_field( rgpost( 'gform_zapier_name' ) );
$zap['url'] = esc_url_raw( rgpost( 'gform_zapier_url' ) );
$zap['is_active'] = rgpost( 'gform_zapier_active' ) ? '1' : '0';
//conditional
$zap['meta']['zapier_conditional_enabled'] = rgpost( 'gf_zapier_conditional_enabled' ) ? '1' : '0';
$zap['meta']['zapier_conditional_field_id'] = absint( rgpost( 'gf_zapier_conditional_field_id' ) );
$posted_logic_operator = rgpost( 'gf_zapier_conditional_operator' );
if ( ! in_array( $posted_logic_operator, array( 'is', 'isnot', '>', '<', 'contains', 'starts_with', 'ends_with' ) ) ) {
$posted_logic_operator = 'is';
}
$zap['meta']['zapier_conditional_operator'] = $posted_logic_operator;
$zap['meta']['zapier_conditional_value'] = wp_strip_all_tags( rgpost( 'gf_zapier_conditional_value' ) );
$zap['meta']['adminLabels'] = rgpost( 'gform_zapier_admin_labels' ) ? '1' : '0';
if ( empty( $zap['url'] ) || empty( $zap['name'] ) ) {
$is_valid = false;
}
if ( $is_valid ) {
$zap = apply_filters( 'gform_zap_before_save', apply_filters( "gform_zap_before_save_{$form['id']}", $zap, $form ), $form );
$zap_id = GFZapierData::update_feed( $zap_id, $form_id, $zap['is_active'], $zap['name'], $zap['url'], $zap['meta'] );
GFCommon::add_message( sprintf( __( 'Zap saved successfully. %sBack to list.%s', 'gravityformszapier' ), '<a href="' . esc_url( remove_query_arg( 'zid' ) ) . '">', '</a>' ) );
if ( $is_new_zap || $is_update ) {
//send field info to zap when new or url has changed
$sent = self::send_form_data_to_zapier( '', $form );
$is_new_zap = false;
}
} else {
GFCommon::add_error_message( __( 'Zap could not be updated. Please enter all required information below.', 'gravityformszapier' ) );
}
}
GFFormSettings::page_header( __( 'Zapier', 'gravityformszapier' ) );
$feed_active = $is_new_zap ? 1 : rgar( $zap, 'is_active' );
$admin_labels = $is_new_zap ? 0 : rgars( $zap, 'meta/adminLabels' );
$logic_operator = rgars( $zap, 'meta/zapier_conditional_operator' );
?>
<style type="text/css">
a.limit-text {
display: block;
height: 18px;
line-height: 18px;
overflow: hidden;
padding-right: 5px;
color: #555;
text-overflow: ellipsis;
white-space: nowrap;
}
a.limit-text:hover {
color: #555;
}
th.column-name {
width: 30%;
}
th.column-type {
width: 20%;
}
</style>
<div style="<?php echo $is_new_zap ? 'display:block' : 'display:none' ?>">
<?php echo sprintf( __( 'To create a new zap, you must have the Webhook URL. The Webhook URL may be found when you go to your %sZapier dashboard%s and create a new zap, or when you edit an existing zap. Once you have saved your new feed the form fields will be available for mapping on the Zapier site.', 'gravityformszapier' ), "<a href='https://zapier.com/app/dashboard' target='_blank'>", '</a>' ); ?>
</div>
<form method="post" id="gform_zapier_form">
<?php wp_nonce_field( 'gforms_save_zap', 'gforms_save_zap' ) ?>
<input type="hidden" id="gform_zap_id" name="gform_zap_id" value="<?php echo absint( $zap_id ) ?>"/>
<table class="form-table">
<tr valign="top">
<th scope="row">
<label for="gform_zapier_name">
<?php esc_html_e( 'Zap Name', 'gravityformszapier' ); ?><span class="gfield_required">*</span>
<?php gform_tooltip( 'zapier_name' ) ?>
</label>
</th>
<td>
<input type="text" class="fieldwidth-2" name="gform_zapier_name" id="gform_zapier_name"
value="<?php echo esc_attr( rgar( $zap, 'name' ) ) ?>"/>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="gform_zapier_url">
<?php esc_html_e( 'Webhook URL', 'gravityformszapier' ); ?><span class="gfield_required">*</span>
<?php gform_tooltip( 'zapier_url' ) ?>
</label>
</th>
<td>
<input type="text" class="fieldwidth-2" name="gform_zapier_url" id="gform_zapier_url"
value="<?php echo esc_url( rgar( $zap, 'url' ) ) ?>"/>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="gform_zapier_active">
<?php esc_html_e( 'Active', 'gravityformszapier' ); ?>
<?php gform_tooltip( 'zapier_active' ) ?>
</label>
</th>
<td>
<input type="radio" id="form_active_yes"
name="gform_zapier_active" <?php checked( $feed_active, 1 ); ?> value="1"/>
<label for="form_active_yes" class="inline"><?php esc_html_e( 'Yes', 'gravityformszapier' ) ?></label>
<input type="radio" id="form_active_no"
name="gform_zapier_active" <?php checked( $feed_active, 0 ); ?> value="0"/>
<label for="form_active_no" class="inline"><?php esc_html_e( 'No', 'gravityformszapier' ) ?></label>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="gform_zapier_admin_labels">
<?php esc_html_e( 'Use Admin Labels', 'gravityformszapier' ); ?>
<?php gform_tooltip( 'zapier_labels' ) ?>
</label>
</th>
<td>
<input type="radio" id="admin_labels_yes"
name="gform_zapier_admin_labels" <?php checked( $admin_labels, 1 ); ?> value="1"/>
<label for="admin_labels_yes" class="inline"><?php esc_html_e( 'Yes', 'gravityformszapier' ) ?></label>
<input type="radio" id="admin_labels_no"
name="gform_zapier_admin_labels" <?php checked( $admin_labels, 0 ); ?> value="0"/>
<label for="admin_labels_no" class="inline"><?php esc_html_e( 'No', 'gravityformszapier' ) ?></label>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="gform_zapier_conditional_logic">
<?php esc_html_e( 'Conditional Logic', 'gravityforms' ) ?>
<?php gform_tooltip( 'zapier_conditional' ) ?>
</label>
</th>
<td>
<input type="checkbox" id="gf_zapier_conditional_enabled" name="gf_zapier_conditional_enabled"
value="1"
onclick="if(this.checked){jQuery('#gf_zapier_conditional_container').fadeIn('fast');} else{ jQuery('#gf_zapier_conditional_container').fadeOut('fast'); }" <?php checked( rgars( $zap, 'meta/zapier_conditional_enabled' ), 1 ); ?>/>
<label for="gf_zapier_conditional_enable"><?php esc_html_e( 'Enable', 'gravityformszapier' ); ?></label>
<br/>
<div style="height:20px;">
<div
id="gf_zapier_conditional_container" <?php echo ! rgars( $zap, 'meta/zapier_conditional_enabled' ) ? "style='display:none'" : '' ?>>
<div id="gf_zapier_conditional_fields" style="display:none;">
<?php esc_html_e( 'Send to Zapier if ', 'gravityformszapier' ) ?>
<select id="gf_zapier_conditional_field_id" name="gf_zapier_conditional_field_id"
class="optin_select"
onchange='jQuery("#gf_zapier_conditional_value_container").html(GetFieldValues(jQuery(this).val(), "", 20));'></select>
<select id="gf_zapier_conditional_operator" name="gf_zapier_conditional_operator">
<option
value="is" <?php selected( $logic_operator, 'is' ); ?>><?php esc_html_e( 'is', 'gravityformszapier' ) ?></option>
<option
value="isnot" <?php selected( $logic_operator, 'isnot' ); ?>><?php esc_html_e( 'is not', 'gravityformszapier' ) ?></option>
<option
value=">" <?php selected( $logic_operator, '>' ); ?>><?php esc_html_e( 'greater than', 'gravityformszapier' ) ?></option>
<option
value="<" <?php selected( $logic_operator, '<' ); ?>><?php esc_html_e( 'less than', 'gravityformszapier' ) ?></option>
<option
value="contains" <?php selected( $logic_operator, 'contains' ); ?>><?php esc_html_e( 'contains', 'gravityformszapier' ) ?></option>
<option
value="starts_with" <?php selected( $logic_operator, 'starts_with' ); ?>><?php esc_html_e( 'starts with', 'gravityformszapier' ) ?></option>
<option
value="ends_with" <?php selected( $logic_operator, 'ends_with' ); ?>><?php esc_html_e( 'ends with', 'gravityformszapier' ) ?></option>
</select>
<div id="gf_zapier_conditional_value_container"
name="gf_zapier_conditional_value_container" style="display:inline;"></div>
</div>
<div id="gf_zapier_conditional_message" style="display:none">
<?php esc_html_e( 'To create a condition, your form must have a field supported by conditional logic.', 'gravityformzapier' ) ?>
</div>
</div>
</div>
</td>
</tr> <!-- / conditional logic -->
</table>
<p class="submit">
<?php
$button_label = $is_new_zap ? __( 'Save Zapier Feed', 'gravityformszapier' ) : __( 'Update Zapier Feed', 'gravityformszapier' );
$zapier_button = '<input class="button-primary" type="submit" value="' . $button_label . '" name="save"/>';
/**
* A filter allowing for the modification of the save button for saving a Zapier Feed (A conditional).
*
* @param string $zapier_button The HTML rendered for the save button.
*/
echo apply_filters( 'gform_save_zapier_button', $zapier_button );
?>
</p>
</form>
<script type="text/javascript">
// Conditional Functions
// initialize form object
form = <?php echo GFCommon::json_encode( $form )?> ;
// initializing registration condition drop downs
jQuery(document).ready(function () {
var selectedField = <?php echo json_encode( absint( rgars( $zap, 'meta/zapier_conditional_field_id' ) ) ); ?>;
var selectedValue = <?php echo json_encode( rgars( $zap, 'meta/zapier_conditional_value' ) )?>;
SetCondition(selectedField, selectedValue);
});
function SetCondition(selectedField, selectedValue) {
// load form fields
jQuery('#gf_zapier_conditional_field_id').html(GetSelectableFields(selectedField, 20));
var optinConditionField = jQuery('#gf_zapier_conditional_field_id').val();
var checked = jQuery('#gf_zapier_conditional_enabled').attr('checked');
if (optinConditionField) {
jQuery('#gf_zapier_conditional_message').hide();
jQuery('#gf_zapier_conditional_fields').show();
jQuery('#gf_zapier_conditional_value_container').html(GetFieldValues(optinConditionField, selectedValue, 20));
jQuery('#gf_zapier_conditional_value').val(selectedValue);
}
else {
jQuery('#gf_zapier_conditional_message').show();
jQuery('#gf_zapier_conditional_fields').hide();
}
if (!checked) jQuery('#gf_zapier_conditional_container').hide();
}
function GetFieldValues(fieldId, selectedValue, labelMaxCharacters) {
if (!fieldId)
return '';
var str = '';
var field = GetFieldById(fieldId);
if (!field)
return '';
var isAnySelected = false;
if (field['type'] == 'post_category' && field['displayAllCategories']) {
str += '<?php $dd = wp_dropdown_categories( array(
'class' => 'optin_select',
'orderby' => 'name',
'id' => 'gf_zapier_conditional_value',
'name' => 'gf_zapier_conditional_value',
'hierarchical' => true,
'hide_empty' => 0,
'echo' => false
) ); echo str_replace( "\n", '', str_replace( "'", "\\'", $dd ) ); ?>';
}
else if (field.choices) {
str += GetRuleValuesDropDown(field.choices, 'gf_zapier', 0, selectedValue, 'gf_zapier_conditional_value');
}
else {
selectedValue = selectedValue ? selectedValue.replace(/'/g, "'") : "";
//create a text field for fields that don't have choices (i.e text, textarea, number, email, etc...)
str += "<input type='text' placeholder='<?php esc_attr_e( 'Enter value', 'gravityforms' ); ?>' id='gf_zapier_conditional_value' name='gf_zapier_conditional_value' value='" + selectedValue.replace(/'/g, "'") + "'>";
}
return str;
}
function GetFieldById(fieldId) {
for (var i = 0; i < form.fields.length; i++) {
if (form.fields[i].id == fieldId)
return form.fields[i];
}
return null;
}
function TruncateMiddle(text, maxCharacters) {
if (!text)
return '';
if (text.length <= maxCharacters)
return text;
var middle = parseInt(maxCharacters / 2);
return text.substr(0, middle) + '...' + text.substr(text.length - middle, middle);
}
function GetSelectableFields(selectedFieldId, labelMaxCharacters) {
var str = '', fieldLabel;
var inputType;
for (var i = 0; i < form.fields.length; i++) {
fieldLabel = form.fields[i].adminLabel ? form.fields[i].adminLabel : form.fields[i].label;
inputType = form.fields[i].inputType ? form.fields[i].inputType : form.fields[i].type;
if (IsConditionalLogicField(form.fields[i])) {
var selected = form.fields[i].id == selectedFieldId ? "selected='selected'" : '';
str += "<option value='" + form.fields[i].id + "' " + selected + '>' + TruncateMiddle(fieldLabel, labelMaxCharacters) + '</option>';
}
}
return str;
}
function IsConditionalLogicField(field) {
var inputType = field.inputType ? field.inputType : field.type;
var supported_fields = ['checkbox', 'radio', 'select', 'text', 'website', 'textarea', 'email', 'hidden', 'number', 'phone', 'multiselect', 'post_title', 'post_tags', 'post_custom_field', 'post_content', 'post_excerpt'];
var index = jQuery.inArray(inputType, supported_fields);
return index >= 0;
}
</script>
<?php
}
public static function send_form_updates( $form, $is_new ) {
self::send_form_data_to_zapier( '', $form );
}
public static function send_form_data_to_zapier( $entry = null, $form ) {
//if there is an entry, then this is a form submission, get data out of entry to POST to Zapier
//otherwise this is a dummy setup to give Zapier the field data, get the form fields and POST to Zapier with empty data
if ( empty( $form ) && empty( $entry ) ) {
self::log_debug( 'No form or entry was provided to send data to Zapier.' );
return false;
}
//get zaps for form
$form_id = $form['id'];
$zaps = GFZapierData::get_feed_by_form( $form_id, true );
if ( empty( $zaps ) ) {
self::log_debug( "There are no zaps configured for form id {$form_id}" );
return false;
}
$is_entry = ! empty( $entry );
$is_delayed = $is_entry && self::maybe_delay_feed( $entry, $form );
if ( $is_delayed ) {
self::log_debug( 'Zapier Feed processing is delayed, not processing feed for entry #' . $entry['id'] );
return false;
}
//do not send spam entries to zapier
if ( $is_entry && $entry['status'] == 'spam' ) {
self::log_debug( 'The entry is marked as spam, NOT sending to Zapier.' );
return false;
}
$is_entry ? self::log_debug( 'Gathering entry data to send submission.' ) : self::log_debug( 'Gathering field data to send dummy submission.' );
$retval = true;
foreach ( $zaps as $zap ) {
//checking to see if a condition was specified, and if so, met, otherwise don't send to zapier
//only check this when there is an entry, simple form updates should go to zapier regardless of conditions existing
if ( ! $is_entry || ( $is_entry && self::conditions_met( $form, $zap, $entry ) ) ) {
if ( $is_entry ) {
self::log_debug( 'No condition specified or a condition was specified and met, sending to Zapier' );
}
$retval = self::process_feed( $zap, $entry, $form );
} else {
self::log_debug( 'A condition was specified and not met, not sending to Zapier' );
$retval = false;
}
}
return $retval;
}
/**
* Determines if feed processing is delayed by the PayPal Standard Add-On.
*
* Also enables use of the gform_is_delayed_pre_process_feed filter.
*
* @param array $entry The Entry Object currently being processed.
* @param array $form The Form Object currently being processed.
*
* @since 2.0.2
* @since 3.1.5 Updated to support all payment addons.
*
* @return bool
*/
public static function maybe_delay_feed( $entry, $form ) {
if ( self::$_bypass_feed_delay ) {
return false;
}
$is_delayed = false;
$slug = self::$slug;
// Backwards compatibility for GF core < 2.4.13.
if ( ! method_exists( 'GFPaymentAddOn', 'maybe_delay_feed_processing' ) ) {
// See if there is a paypal feed and zapier is set to be delayed until payment is received.
if ( class_exists( 'GFPayPal' ) ) {
$paypal_feeds = self::get_paypal_feeds( $form['id'] );
// Loop through paypal feeds to get active one for this form submission, needed to see if add-on processing should be delayed.
foreach ( $paypal_feeds as $paypal_feed ) {
if ( $paypal_feed['is_active'] && self::is_feed_condition_met( $paypal_feed, $form, $entry ) ) {
$active_paypal_feed = $paypal_feed;
break;
}
}
$is_fulfilled = rgar( $entry, 'is_fulfilled' );
if ( ! empty( $active_paypal_feed ) && self::is_delayed( $active_paypal_feed ) && self::has_paypal_payment( $active_paypal_feed, $form, $entry ) && ! $is_fulfilled ) {
$is_delayed = true;
}
}
}
/**
* Allow feed processing to be delayed.
*
* @param bool $is_delayed Is feed processing delayed?
* @param array $form The Form Object currently being processed.
* @param array $entry The Entry Object currently being processed.
* @param string $slug The Add-On slug e.g. gravityformszapier
*/
$is_delayed = apply_filters( 'gform_is_delayed_pre_process_feed', $is_delayed, $form, $entry, $slug );
$is_delayed = apply_filters( 'gform_is_delayed_pre_process_feed_' . $form['id'], $is_delayed, $form, $entry, $slug );
return $is_delayed;
}
public static function process_feed( $feed, $entry, $form ) {
$body = self::get_body( $entry, $form, $feed );
/**
* 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
*/
$body = gf_apply_filters( array( 'gform_zapier_request_body', $form['id'] ), $body, $feed, $entry, $form );
$headers = array();
if ( empty( $entry ) ) {
$headers['X-Hook-Test'] = 'true';
}
$json_body = json_encode( $body );
if ( empty( $body ) ) {
self::log_debug( 'There is no field data to send to Zapier.' );
return false;
}
self::log_debug( 'Posting to url: ' . $feed['url'] . ' data: ' . print_r( $body, true ) );
$form_data = array( 'sslverify' => false, 'ssl' => true, 'body' => $json_body, 'headers' => $headers );
$response = wp_remote_post( $feed['url'], $form_data );
if ( is_wp_error( $response ) ) {
self::log_error( 'The following error occurred: ' . print_r( $response, true ) );
return false;
} else {
self::log_debug( 'Successful response from Zap: ' . print_r( $response, true ) );
if ( ! empty( $entry ) ) {
self::log_debug( 'Marking entry #' . $entry['id'] . ' as fulfilled.' );
gform_update_meta( $entry['id'], self::$slug . '_is_fulfilled', true );
}
return true;
}
}
public static function get_body( $entry, $form, $feed = false ) {
/**
* 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 Object.
* @param array $form The Form Object.
* @param array $feed The Feed Object.
*/
if ( apply_filters( 'gform_zapier_use_stored_body', true, $entry, $form, $feed ) ) {
$current_body = self::$_current_body;
if ( is_array( $current_body ) ) {
return $current_body;
}
}
$adminLabels = is_array( $feed ) ? rgars( $feed, 'meta/adminLabels' ) : false;
$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 = self::get_entry_properties();
foreach ( $entry_properties as $property_key => $property_config ) {
$key = self::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 = self::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 = self::get_sample_value( $field );
$field_value = apply_filters( 'gform_zapier_sample_field_value', $field_value, $form['id'], $field->id );
}
$field_label = self::get_body_label( $adminLabels, $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 = self::get_body_label( $adminLabels, $field, $input['id'] );
$key = self::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 = self::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 = self::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 /'] = self::get_products_array( $form, $entry );
}
break;
}
} else {
$key = self::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 /'] = self::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;
}
}
}
}
self::$_current_body = $body;
return $body;
}
/**
* Retrieve a sample value for the current field.
*
* @param GF_Field $field The field properties.
*
* @return array|string
*/
public static 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 );