-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-gf-getresponse.php
1335 lines (1034 loc) · 36.8 KB
/
class-gf-getresponse.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
defined( 'ABSPATH' ) or die();
GFForms::include_feed_addon_framework();
/**
* Gravity Forms GetResponse Add-On.
*
* @since 1.0
* @package GravityForms
* @author Rocketgenius
* @copyright Copyright (c) 2020, Rocketgenius
*/
class GFGetResponse extends GFFeedAddOn {
/**
* Contains an instance of this class, if available.
*
* @since 1.0
* @access private
* @var GFGetResponse $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Defines the version of the GetResponse Add-On.
*
* @since 1.0
* @access protected
* @var string $_version Contains the version, defined from getresponse.php
*/
protected $_version = GF_GETRESPONSE_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since 1.0
* @access protected
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = '1.9.14.26';
/**
* Defines the plugin slug.
*
* @since 1.0
* @access protected
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformsgetresponse';
/**
* Defines the main plugin file.
*
* @since 1.0
* @access protected
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformsgetresponse/getresponse.php';
/**
* Defines the full path to this class file.
*
* @since 1.0
* @access protected
* @var string $_full_path The full path.
*/
protected $_full_path = __FILE__;
/**
* Defines the URL where this Add-On can be found.
*
* @since 1.0
* @access protected
* @var string The URL of the Add-On.
*/
protected $_url = 'http://www.gravityforms.com';
/**
* Defines the title of this Add-On.
*
* @since 1.0
* @access protected
* @var string $_title The title of the Add-On.
*/
protected $_title = 'Gravity Forms GetResponse Add-On';
/**
* Defines the short title of the Add-On.
*
* @since 1.0
* @access protected
* @var string $_short_title The short title.
*/
protected $_short_title = 'GetResponse';
/**
* Defines if Add-On should use Gravity Forms servers for update data.
*
* @since 1.0
* @access protected
* @var bool
*/
protected $_enable_rg_autoupgrade = true;
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since 1.0
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_getresponse';
/**
* Defines the capability needed to access the Add-On form settings page.
*
* @since 1.0
* @access protected
* @var string $_capabilities_form_settings The capability needed to access the Add-On form settings page.
*/
protected $_capabilities_form_settings = 'gravityforms_getresponse';
/**
* Defines the capability needed to uninstall the Add-On.
*
* @since 1.0
* @access protected
* @var string $_capabilities_uninstall The capability needed to uninstall the Add-On.
*/
protected $_capabilities_uninstall = 'gravityforms_getresponse_uninstall';
/**
* Defines the capabilities needed for the GetResponse Add-On
*
* @since 1.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array( 'gravityforms_getresponse', 'gravityforms_getresponse_uninstall' );
/**
* Contains an instance of the GetResponse API library, if available.
*
* @since 1.0
* @access protected
* @var GF_GetResponse_API $api If available, contains an instance of the GetResponse API library.
*/
protected $api = null;
/**
* Get instance of this class.
*
* @since 1.0
* @access public
*
* @return GFGetResponse
*/
public static function get_instance() {
if ( null === self::$_instance ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Plugin starting point. Handles hooks, loading of language files and PayPal delayed payment support.
*
* @since 1.1
* @access public
*/
public function init() {
parent::init();
$this->add_delayed_payment_support(
array(
'option_label' => esc_html__( 'Subscribe contact to GetResponse only when payment is received.', 'gravityformsgetresponse' ),
)
);
}
/**
* Return the plugin's icon for the plugin/form settings menu.
*
* @since 1.3
*
* @return string
*/
public function get_menu_icon() {
return file_get_contents( $this->get_base_path() . '/images/menu-icon.svg' );
}
// # PLUGIN SETTINGS -----------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @since 1.0
*
* @return array
*/
public function plugin_settings_fields() {
return array(
array(
'title' => esc_html__( 'GetResponse Account Information', 'gravityformsgetresponse' ),
'description' => $this->plugin_settings_description(),
'fields' => array(
array(
'name' => 'account_type',
'label' => esc_html__( 'Account Type', 'gravityformsgetresponse' ),
'type' => 'radio',
'default_value' => 'standard',
'onchange' => "jQuery( this ).parents( 'form' ).submit()",
'horizontal' => true,
'choices' => array(
array(
'label' => esc_html__( 'Standard', 'gravityformsgetresponse' ),
'value' => 'standard',
),
array(
'label' => esc_html__( 'MAX', 'gravityformsgetresponse' ),
'value' => '360',
),
),
),
array(
'name' => 'api_key',
'label' => esc_html__( 'API Key', 'gravityformsgetresponse' ),
'type' => 'text',
'class' => 'medium',
'feedback_callback' => array( $this, 'initialize_api' ),
),
array(
'name' => 'domain',
'label' => esc_html__( 'Domain', 'gravityformsgetresponse' ),
'type' => 'text',
'class' => 'medium',
'dependency' => array( 'field' => 'account_type', 'values' => array( '360' ) ),
'feedback_callback' => array( $this, 'initialize_api' ),
),
array(
'name' => 'max_tld',
'label' => esc_html__( 'MAX Endpoint', 'gravityformsgetresponse' ),
'type' => 'radio',
'default_value' => '.com',
'horizontal' => true,
'choices' => array(
array(
'label' => esc_html__( 'Standard (.com)', 'gravityformsgetresponse' ),
'value' => '.com',
),
array(
'label' => esc_html__( 'Europe (.pl)', 'gravityformsgetresponse' ),
'value' => '.pl',
),
),
'dependency' => array( 'field' => 'account_type', 'values' => array( '360' ) ),
),
array(
'type' => 'save',
'messages' => array(
'success' => esc_html__( 'GetResponse settings have been updated.', 'gravityformsgetresponse' ),
),
),
),
),
);
}
/**
* Prepare plugin settings description.
*
* @since 1.0
*
* @return string
*/
public function plugin_settings_description() {
// Prepare plugin description.
$description = sprintf(
'<p>%s</p>',
sprintf(
esc_html__( 'GetResponse makes it easy to send email newsletters to your customers, manage your subscriber lists, and track campaign performance. Use Gravity Forms to collect customer information and automatically add it to your GetResponse subscriber list. If you don\'t have a GetResponse account, you can %1$s sign up for one here.%2$s', 'gravityformsgetresponse' ),
'<a href="https://www.getresponse.com/" target="_blank">', '</a>'
)
);
// If API is not initialized, add instructions on how to retrieve API key.
if ( ! $this->initialize_api() ) {
$description .= sprintf(
'<p>%s</p>',
sprintf(
esc_html__( 'Gravity Forms GetResponse Add-On requires your GetResponse API key, which can be found in the %1$sGetResponse API tab%2$s under your account details.', 'gravityformsgetresponse' ),
'<a href="https://app.getresponse.com/api" target="_blank">', '</a>'
)
);
}
return $description;
}
// # FEED SETTINGS -------------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the feed edit page.
*
* @since 1.0
*
* @return array
*/
public function feed_settings_fields() {
return array(
array(
'fields' => array(
array(
'name' => 'feed_name',
'label' => esc_html__( 'Name', 'gravityformsgetresponse' ),
'type' => 'text',
'class' => 'medium',
'required' => true,
'default_value' => $this->get_default_feed_name(),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Name', 'gravityformsgetresponse' ),
esc_html__( 'Enter a feed name to uniquely identify this setup.', 'gravityformsgetresponse' )
),
),
array(
'name' => 'campaign',
'label' => esc_html__( 'GetResponse Campaign', 'gravityformsgetresponse' ),
'type' => 'select',
'required' => true,
'choices' => $this->get_campaigns_for_feed_setting(),
'onchange' => "jQuery( this ).parents( 'form' ).submit();",
'no_choices' => esc_html__( 'Please create a GetResponse Campaign to continue setup.', 'gravityformsgetresponse' ),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'GetResponse Campaign', 'gravityformsgetresponse' ),
esc_html__( 'Select which GetResponse campaign this feed will add contacts to.', 'gravityformsgetresponse' )
),
),
array(
'name' => 'fields',
'label' => esc_html__( 'Map Fields', 'gravityformsgetresponse' ),
'type' => 'field_map',
'dependency' => 'campaign',
'field_map' => $this->get_standard_fields_for_field_map(),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Map Fields', 'gravityformsgetresponse' ),
esc_html__( 'Select which Gravity Form fields pair with their respective GetResponse field.', 'gravityformsgetresponse' )
),
),
array(
'name' => 'custom_fields',
'label' => esc_html__( 'Custom Fields', 'gravityformsgetresponse' ),
'type' => 'dynamic_field_map',
'dependency' => 'campaign',
'field_map' => $this->get_custom_fields_for_field_map(),
'save_callback' => array( $this, 'save_custom_fields' ),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Custom Fields', 'gravityformsgetresponse' ),
esc_html__( 'Select or create a new custom GetResponse field to pair with Gravity Forms fields. Custom field names can only contain up to 32 lowercase alphanumeric characters and underscores.', 'gravityformsgetresponse' )
),
),
array(
'name' => 'feed_condition',
'label' => esc_html__( 'Conditional Logic', 'gravityformsgetresponse' ),
'type' => 'feed_condition',
'dependency' => 'campaign',
'checkbox_label' => esc_html__( 'Enable', 'gravityformsgetresponse' ),
'instructions' => esc_html__( 'Export to GetResponse if', 'gravityformsgetresponse' ),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Conditional Logic', 'gravityformsgetresponse' ),
esc_html__( 'When conditional logic is enabled, form submissions will only be exported to GetResponse when the condition is met. When disabled, all form submissions will be exported.', 'gravityformsgetresponse' )
),
),
array(
'type' => 'save',
'dependency' => 'campaign',
),
),
),
);
}
/**
* Prepare campaigns for feed field.
*
* @since 1.0
*
* @return array
*/
public function get_campaigns_for_feed_setting() {
// If GetResponse API instance is not initialized, return choices.
if ( ! $this->initialize_api() ) {
return array();
}
// Get GetResponse campaigns.
$campaigns = $this->api->get_campaigns();
// If campaigns could not be retrieved, return.
if ( is_wp_error( $campaigns ) || empty( $campaigns ) ) {
// Log that campaigns could not be retrieved.
$this->log_error( __METHOD__ . '(): Could not retrieve campaigns;' . ( is_wp_error( $campaigns ) ? ' ' . $campaigns->get_error_message() : '' ) );
return array();
}
// Initialize choices array.
$choices = array(
array(
'label' => esc_html__( 'Select a Campaign', 'gravityformsgetresponse' ),
'value' => '',
),
);
// Loop through campaigns.
foreach ( $campaigns as $campaign ) {
// Add campaign as choice.
$choices[] = array(
'label' => esc_html( $campaign['name'] ),
'value' => esc_attr( $campaign['campaignId'] ),
);
}
return $choices;
}
/**
* Prepare fields for feed field mapping.
*
* @since 1.0
*
* @return array
*/
public function get_standard_fields_for_field_map() {
return array(
array(
'name' => 'name',
'label' => esc_html__( 'Name', 'gravityformsgetresponse' ),
'required' => true,
),
array(
'name' => 'email',
'label' => esc_html__( 'Email Address', 'gravityformsgetresponse' ),
'required' => true,
'field_type' => array( 'email' ),
),
);
}
/**
* Renders and initializes a dynamic field map field based on the $field array whose choices are populated by the fields to be mapped.
* (Forked to refresh field map.)
*
* @since 1.3
*
* @param array $field Field array containing the configuration options of this field.
* @param bool $echo Determines if field contents should automatically be displayed. Defaults to true.
*
* @return string
*/
public function settings_dynamic_field_map( $field, $echo = true ) {
// If feed was saved, refresh field map.
if ( $this->is_save_postback() ) {
$field['field_map'] = $this->get_custom_fields_for_field_map();
}
return parent::settings_dynamic_field_map( $field, $echo );
}
/**
* Prepare custom fields for feed field mapping.
*
* @since 1.0
*
* @return array
*/
public function get_custom_fields_for_field_map() {
// Initialize field map array.
$field_map = array(
array(
'label' => esc_html__( 'Select a Custom Field', 'gravityformsgetresponse' ),
'value' => '',
),
);
// If GetResponse API instance is not initialized, return field map.
if ( ! $this->initialize_api() ) {
return $field_map;
}
// Get GetResponse custom fields.
$custom_fields = $this->get_custom_fields();
// If custom fields could not be retrieved, return.
if ( is_wp_error( $custom_fields ) ) {
// Log that custom fields could not be retrieved.
$this->log_error( __METHOD__ . '(): Could not retrieve custom fields; ' . $custom_fields->get_error_message() );
return $field_map;
}
// Mapped field types.
$mapped_field_types = array( 'text', 'textarea' );
// Get mapped fields.
$mapped_fields = $this->get_setting( 'custom_fields' );
// If fields are mapped, add currently mapped field types to array.
if ( $mapped_fields ) {
// Get the mapped field IDs.
$field_ids = wp_list_pluck( $mapped_fields, 'key' );
$field_ids = array_map( function( $key ) { return str_replace( 'custom_', '', $key ); }, $field_ids );
// Loop through custom fields.
foreach ( $custom_fields as $custom_field ) {
// If this is not one of the mapped fields, skip.
if ( ! in_array( $custom_field['customFieldId'], $field_ids ) ) {
continue;
}
// Add field type.
if ( ! in_array( $custom_field['type'], $mapped_field_types ) ) {
$mapped_field_types[] = $custom_field['type'];
}
}
}
// Loop through custom fields.
foreach ( $custom_fields as $custom_field ) {
// Add custom field to field map.
$field_map[] = array(
'label' => esc_html( $custom_field['name'] ),
'value' => 'custom_' . esc_attr( $custom_field['customFieldId'] ),
);
}
return $field_map;
}
/**
* Create new GetResponse custom fields.
*
* @since 1.3
*
* @param array $field Field settings.
* @param array $field_value Field value.
*
* @return array
*/
public function save_custom_fields( $field = array(), $field_value = array() ) {
global $_gaddon_posted_settings;
// If API is not initialized, return.
if ( ! $this->initialize_api() || empty( $field_value ) ) {
return $field_value;
}
// Get existing GetResponse custom fields.
$custom_fields = $this->get_custom_fields();
// If custom fields could not be retrieved, return.
if ( is_wp_error( $custom_fields ) ) {
// Log that existing fields could not be retrieved.
$this->log_error( __METHOD__ . '(): Unable to retrieve existing custom fields, not saving new custom fields; ' . $custom_fields->get_error_message() );
return $field_value;
}
// Get existing custom field names.
$custom_field_names = wp_list_pluck( $custom_fields, 'name' );
// Loop through custom fields; create new field if using custom key.
foreach ( $field_value as $i => $custom_field ) {
// If this is not a new custom field, skip.
if ( 'gf_custom' !== $custom_field['key'] ) {
continue;
}
// Prepare custom field name.
$field_name = trim( $custom_field['custom_key'] ); // Set shortcut name to custom key.
$field_name = str_replace( ' ', '_', $field_name ); // Remove all spaces.
$field_name = preg_replace( '([^\w\d])', '', $field_name ); // Strip all custom characters.
$field_name = strtolower( $field_name ); // Set to lowercase.
$field_name = substr( $field_name, 0, 32 ); // Limit field name to 32 characters.
// Ensure field name is unique.
$field_name_i = 1;
$start_field_name = $field_name;
while ( in_array( $field_name, $custom_field_names ) ) {
$field_name = $start_field_name . $field_name_i;
$field_name_i++;
}
// Prepare custom field object.
$field_object = array(
'name' => $field_name,
'type' => 'textarea',
'hidden' => false,
'values' => array(),
);
// Log field being created.
$this->log_debug( __METHOD__ . '(): Creating field: ' . print_r( $field_object, true ) );
// Create custom field.
$field_object = $this->api->create_custom_field( $field_object );
// If custom field could not be created, remove from field map.
if ( is_wp_error( $field_object ) ) {
// Log that custom field could not be created.
$this->log_error( __METHOD__ . '(): Unable to create custom field; ' . $field_object->get_error_message() );
// Remove field.
unset( $field_value[ $i ], $_gaddon_posted_settings[ $field['name'] ][ $i ] );
continue;
}
// Update field value.
$field_value[ $i ]['key'] = 'custom_' . $field_object['customFieldId'];
$field_value[ $i ]['custom_key'] = '';
// Update posted settings.
$_gaddon_posted_settings[ $field['name'] ][ $i ]['key'] = 'custom_' . $field_object['customFieldId'];
$_gaddon_posted_settings[ $field['name'] ][ $i ]['custom_key'] = '';
}
GFCache::delete( $this->get_slug() . '_custom_fields_' . $this->get_custom_fields_limit() );
return $field_value;
}
// # FEED LIST -----------------------------------------------------------------------------------------------------
/**
* Set feed creation control.
*
* @since 1.0
*
* @return bool
*/
public function can_create_feed() {
return $this->initialize_api();
}
/**
* Enable feed duplication.
*
* @since 1.1
*
* @param int|array $id The ID of the feed to be duplicated or the feed object when duplicating a form.
*
* @return bool
*/
public function can_duplicate_feed( $id ) {
return true;
}
/**
* Configures which columns should be displayed on the feed list page.
*
* @since 1.0
*
* @return array
*/
public function feed_list_columns() {
return array(
'feed_name' => esc_html__( 'Name', 'gravityformsgetresponse' ),
'campaign' => esc_html__( 'GetResponse Campaign', 'gravityformsgetresponse' ),
);
}
/**
* Returns the value to be displayed in the campaign name column.
*
* @since 1.0
*
* @param array $feed Feed being displayed in the feed list.
*
* @return string
*/
public function get_column_value_campaign( $feed ) {
// Get campaign ID.
$campaign_id = rgars( $feed, 'meta/campaign' );
// If GetResponse instance is not initialized, return campaign ID.
if ( ! $this->initialize_api() ) {
return $campaign_id;
}
// Get campaign.
$campaign = $this->api->get_campaign( $campaign_id );
// If campaign could not be retrieved, return campaign ID.
if ( is_wp_error( $campaign ) ) {
// Log that could not be found.
$this->log_error( __METHOD__ . '(): Unable to get campaign for feed list; ' . $campaign->get_error_message() );
return $campaign_id;
} else {
return esc_html( $campaign['name'] );
}
}
// # FEED PROCESSING -----------------------------------------------------------------------------------------------
/**
* Subscribe the user to the campaign.
*
* @since 1.0
*
* @param array $feed Feed object.
* @param array $entry Entry object.
* @param array $form Form object.
*
* @return array
*/
public function process_feed( $feed, $entry, $form ) {
// If API is not initialized, return error.
if ( ! $this->initialize_api() ) {
$this->add_feed_error( esc_html__( 'Unable to subscribe user to campaign because API was not initialized.', 'gravityformsgetresponse' ), $feed, $entry, $form );
return $entry;
}
// Initialize contact object.
$contact = array(
'name' => $this->get_field_value( $form, $entry, $feed['meta']['fields_name'] ),
'email' => $this->get_field_value( $form, $entry, $feed['meta']['fields_email'] ),
'campaign' => array( 'campaignId' => $feed['meta']['campaign'] ),
'customFieldValues' => array(),
'ipAddress' => $this->get_field_value( $form, $entry, 'ip' ),
);
// If email address is invalid, return.
if ( GFCommon::is_invalid_or_empty_email( $contact['email'] ) ) {
$this->add_feed_error( esc_html__( 'Unable to subscribe user to campaign because an invalid or empty email address was provided.', 'gravityformsgetresponse' ), $feed, $entry, $form );
return $entry;
}
// If no name is provided, return.
if ( rgblank( $contact['name'] ) ) {
$this->add_feed_error( esc_html__( 'Unable to subscribe user to campaign because no name was provided.', 'gravityformsgetresponse' ), $feed, $entry, $form );
return $entry;
}
// Set custom field values.
$contact['customFieldValues'] = $this->prepare_custom_field_values( $feed, $entry, $form );
// Log that we are checking to see if contact already exists.
$this->log_debug( __METHOD__ . "(): Checking to see if {$contact['email']} is already on the list." );
// Get contact.
$existing_contact = $this->get_contact_by_email( $contact['email'], $contact['campaign']['campaignId'] );
if ( $existing_contact ) {
$this->log_debug( __METHOD__ . '(): Found existing contact; updating name, email address, custom fields.' );
// Set contact ID, custom fields.
$contact['contactId'] = $existing_contact['contactId'];
$contact['customFieldValues'] = rgar( $existing_contact, 'customFieldValues' ) && is_array( $existing_contact['customFieldValues'] ) ? array_merge( $existing_contact['customFieldValues'], $contact['customFieldValues'] ) : $contact['customFieldValues'];
}
/**
* Allows the contact properties to be overridden before they are sent to GetResponse.
*
* @since 1.4
*
* @param array $contact The contact properties.
* @param bool|array $existing_contact False or the existing contact properties.
* @param array $feed The feed currently being processed.
* @param array $entry The entry currently being processed.
* @param array $form The form currently being processed.
*/
$contact = gf_apply_filters( array(
'gform_getresponse_contact',
$form['id']
), $contact, $existing_contact, $feed, $entry, $form );
// If contact exists, updated it. Otherwise, create it.
if ( $existing_contact ) {
// Log the contact to be updated.
$this->log_debug( __METHOD__ . '(): Contact that will be updated => ' . print_r( $contact, true ) );
// Update contact.
$updated_contact = $this->api->update_contact( $contact['contactId'], $contact );
// If contact could not be created, add feed error.
if ( is_wp_error( $updated_contact ) ) {
// Log that contact could not be created.
$this->add_feed_error( sprintf( esc_html__( 'Unable to update existing contact: %s', 'gravityformsgetresponse' ), $updated_contact->get_error_message() ), $feed, $entry, $form );
} else {
// Log that contact was created.
$this->log_debug( __METHOD__ . '(): Contact was created.' );
}
return $entry;
} else {
// Log the contact to be added.
$this->log_debug( __METHOD__ . '(): Contact to be added => ' . print_r( $contact, true ) );
// Add contact.
$created_contact = $this->api->create_contact( $contact );
// If contact could not be created, add feed error.
if ( is_wp_error( $created_contact ) ) {
// Log that contact could not be created.
$this->add_feed_error( sprintf( esc_html__( 'Unable to create contact: %s', 'gravityformsgetresponse' ), $created_contact->get_error_message() ), $feed, $entry, $form );
} else {
// Log that contact was created.
$this->log_debug( __METHOD__ . '(): Contact was created.' );
}
return $entry;
}
}
/**
* Prepare custom field values for contact object.
*
* @since 1.3
*
* @param array $feed Feed object.
* @param array $entry Entry object.
* @param array $form Form object.
*
* @return array
*/
private function prepare_custom_field_values( $feed, $entry, $form ) {
// Initialize return array.
$values = array();
// Get custom fields map.
$custom_fields_map = self::get_dynamic_field_map_fields( $feed, 'custom_fields' );
// If no custom fields are mapped, return.
if ( empty( $custom_fields_map ) ) {
return $values;
}
// Get custom fields.
$custom_fields = $this->get_custom_fields();
// If custom fields could not be retrieved, return values array.
if ( is_wp_error( $custom_fields ) ) {
// Log that custom fields could not be retrieved.
$this->add_feed_error( sprintf( 'Unable to get custom fields; %s', $custom_fields->get_error_message() ), $feed, $entry, $form );
return $values;
}
// Update array keys.
foreach ( $custom_fields as $i => $custom_field ) {
$custom_fields[ $custom_field['customFieldId'] ] = $custom_field;
unset( $custom_fields[ $i ] );
}
// Loop through custom fields.
foreach ( $custom_fields_map as $field_name => $field_id ) {
// If no field is paired to this key, skip it.
if ( rgblank( $field_name ) || rgblank( $field_id ) ) {
continue;
}
// Get the field value.
$field_value = $this->get_field_value( $form, $entry, $field_id );
// If this field value is empty, skip it.
if ( rgblank( $field_value ) ) {
continue;
}
// Strip "custom_" string from field name, get custom field.
$custom_field_id = substr( $field_name, 7 );
$custom_field = rgar( $custom_fields, $custom_field_id, false );
// If custom field does not exist, skip.
if ( ! $custom_field ) {
continue;
}
// Validate field value based on type.
switch ( $custom_field['type'] ) {
case 'multi_select':
$form_field = GFAPI::get_field( $form, $field_id );
if ( $form_field instanceof GF_Field_MultiSelect ) {
$field_value = $form_field->to_array( rgar( $entry, $field_id ) );
} elseif ( ! is_array( $field_value ) ) {
$field_value = array( $field_value );
}
// If choices are not in list of custom field values, skip.
if ( $invalid = array_diff( $field_value, $custom_field['values'] ) ) {
$this->log_error( __METHOD__ . '(): Excluding field "' . $custom_field['name'] . '" (' . $custom_field_id . ') from contact because choices (' . implode( ', ', $invalid ) . ') are invalid.' );
continue 2;
}
break;
case 'checkbox':
case 'country':
case 'currency':