-
Notifications
You must be signed in to change notification settings - Fork 16
/
class-gf-mailchimp.php
executable file
·2696 lines (2061 loc) · 78.5 KB
/
class-gf-mailchimp.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
// don't load directly
if ( ! defined( 'ABSPATH' ) ) {
die();
}
GFForms::include_feed_addon_framework();
/**
* Gravity Forms Mailchimp Add-On.
*
* @since 1.0
* @package GravityForms
* @author Rocketgenius
* @copyright Copyright (c) 2016, Rocketgenius
*/
class GFMailChimp extends GFFeedAddOn {
/**
* Contains an instance of this class, if available.
*
* @since 3.0
* @access private
* @var GFMailChimp $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Defines the version of the Mailchimp Add-On.
*
* @since 3.0
* @access protected
* @var string $_version Contains the version, defined from mailchimp.php
*/
protected $_version = GF_MAILCHIMP_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since 3.0
* @access protected
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = '1.9.12';
/**
* Defines the plugin slug.
*
* @since 3.0
* @access protected
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformsmailchimp';
/**
* Defines the main plugin file.
*
* @since 3.0
* @access protected
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformsmailchimp/mailchimp.php';
/**
* Defines the full path to this class file.
*
* @since 3.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 3.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 3.0
* @access protected
* @var string $_title The title of the Add-On.
*/
protected $_title = 'Gravity Forms Mailchimp Add-On';
/**
* Defines the short title of the Add-On.
*
* @since 3.0
* @access protected
* @var string $_short_title The short title.
*/
protected $_short_title = 'Mailchimp';
/**
* Defines if Add-On should use Gravity Forms servers for update data.
*
* @since 3.0
* @access protected
* @var bool
*/
protected $_enable_rg_autoupgrade = true;
/**
* Defines the capabilities needed for the Mailchimp Add-On
*
* @since 3.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array( 'gravityforms_mailchimp', 'gravityforms_mailchimp_uninstall' );
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since 3.0
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_mailchimp';
/**
* Defines the capability needed to access the Add-On form settings page.
*
* @since 3.0
* @access protected
* @var string $_capabilities_form_settings The capability needed to access the Add-On form settings page.
*/
protected $_capabilities_form_settings = 'gravityforms_mailchimp';
/**
* Defines the capability needed to uninstall the Add-On.
*
* @since 3.0
* @access protected
* @var string $_capabilities_uninstall The capability needed to uninstall the Add-On.
*/
protected $_capabilities_uninstall = 'gravityforms_mailchimp_uninstall';
/**
* Defines the Mailchimp list/audience field tag name.
*
* @since 3.7
* @access protected
* @var string $merge_var_name The Mailchimp list/audience field tag name; used by gform_mailchimp_field_value.
*/
protected $merge_var_name = '';
/**
* Defines the Mailchimp merge fields used in the current request.
*
* @since 4.2.4
* @access protected
* @var array $merge_fields The Mailchimp merge fields used in the current request.
*/
protected $merge_fields = array();
/**
* Contains an instance of the Mailchimp API library, if available.
*
* @since 1.0
* @access protected
* @var GF_MailChimp_API $api If available, contains an instance of the Mailchimp API library.
*/
public $api = null;
/**
* Get an instance of this class.
*
* @since 3.0
* @access public
*
* @return GFMailChimp
*/
public static function get_instance() {
if ( null === self::$_instance ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Autoload the required libraries.
*
* @since 4.0
* @access public
*
* @uses GFAddOn::is_gravityforms_supported()
*/
public function pre_init() {
parent::pre_init();
if ( $this->is_gravityforms_supported() ) {
// Load the Mailchimp API library.
if ( ! class_exists( 'GF_MailChimp_API' ) ) {
require_once( 'includes/class-gf-mailchimp-api.php' );
}
}
}
/**
* Plugin starting point. Handles hooks, loading of language files and PayPal delayed payment support.
*
* @since 3.0
* @access public
*
* @uses GFFeedAddOn::add_delayed_payment_support()
*/
public function init() {
parent::init();
$this->add_delayed_payment_support(
array(
'option_label' => esc_html__( 'Subscribe user to Mailchimp only when payment is received.', 'gravityformsmailchimp' ),
)
);
}
/**
* Remove unneeded settings.
*
* @since 4.0
* @access public
*/
public function uninstall() {
parent::uninstall();
GFCache::delete( 'mailchimp_plugin_settings' );
delete_option( 'gf_mailchimp_settings' );
delete_option( 'gf_mailchimp_version' );
}
/**
* Register needed styles.
*
* @since 4.0
* @access public
*
* @return array
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => $this->_slug . '_form_settings',
'src' => $this->get_base_url() . "/css/form_settings{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
),
),
);
return array_merge( parent::styles(), $styles );
}
/**
* Return the plugin's icon for the plugin/form settings menu.
*
* @since 4.7
*
* @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 3.0
* @access public
*
* @return array
*/
public function plugin_settings_fields() {
return array(
array(
'description' => '<p>' .
sprintf(
esc_html__( 'Mailchimp makes it easy to send email newsletters to your customers, manage your subscriber audiences, and track campaign performance. Use Gravity Forms to collect customer information and automatically add it to your Mailchimp subscriber audience. If you don\'t have a Mailchimp account, you can %1$ssign up for one here.%2$s', 'gravityformsmailchimp' ),
'<a href="http://www.mailchimp.com/" target="_blank">', '</a>'
)
. '</p>',
'fields' => array(
array(
'name' => 'apiKey',
'label' => esc_html__( 'Mailchimp API Key', 'gravityformsmailchimp' ),
'type' => 'text',
'class' => 'medium',
'feedback_callback' => array( $this, 'initialize_api' ),
),
),
),
);
}
// # FEED SETTINGS -------------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the feed edit page.
*
* @since 3.0
* @access public
*
* @return array
*/
public function feed_settings_fields() {
$settings = array(
array(
'title' => esc_html__( 'Mailchimp Feed Settings', 'gravityformsmailchimp' ),
'fields' => array(
array(
'name' => 'feedName',
'label' => esc_html__( 'Name', 'gravityformsmailchimp' ),
'type' => 'text',
'required' => true,
'class' => 'medium',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Name', 'gravityformsmailchimp' ),
esc_html__( 'Enter a feed name to uniquely identify this setup.', 'gravityformsmailchimp' )
),
),
array(
'name' => 'mailchimpList',
'label' => esc_html__( 'Mailchimp Audience', 'gravityformsmailchimp' ),
'type' => 'mailchimp_list',
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Mailchimp Audience', 'gravityformsmailchimp' ),
esc_html__( 'Select the Mailchimp audience you would like to add your contacts to.', 'gravityformsmailchimp' )
),
),
),
),
array(
'dependency' => 'mailchimpList',
'fields' => array(
array(
'name' => 'mappedFields',
'label' => esc_html__( 'Map Fields', 'gravityformsmailchimp' ),
'type' => 'field_map',
'field_map' => $this->merge_vars_field_map(),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Map Fields', 'gravityformsmailchimp' ),
esc_html__( 'Associate your Mailchimp merge tags to the appropriate Gravity Form fields by selecting the appropriate form field from the list.', 'gravityformsmailchimp' )
),
),
array(
'name' => 'interestCategories',
'label' => esc_html__( 'Groups', 'gravityformsmailchimp' ),
'dependency' => array( $this, 'has_interest_categories' ),
'type' => 'interest_categories',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Groups', 'gravityformsmailchimp' ),
esc_html__( 'When one or more groups are enabled, users will be assigned to the groups in addition to being subscribed to the Mailchimp audience. When disabled, users will not be assigned to groups.', 'gravityformsmailchimp' )
),
),
array(
'name' => 'options',
'label' => esc_html__( 'Options', 'gravityformsmailchimp' ),
'type' => 'checkbox',
'choices' => array(
array(
'name' => 'double_optin',
'label' => esc_html__( 'Double Opt-In', 'gravityformsmailchimp' ),
'default_value' => 1,
'onclick' => 'if(this.checked){jQuery("#mailchimp_doubleoptin_warning").hide();} else{jQuery("#mailchimp_doubleoptin_warning").show();}',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Double Opt-In', 'gravityformsmailchimp' ),
esc_html__( 'When the double opt-in option is enabled, Mailchimp will send a confirmation email to the user and will only add them to your Mailchimp audience upon confirmation.', 'gravityformsmailchimp' )
),
),
array(
'name' => 'markAsVIP',
'label' => esc_html__( 'Mark subscriber as VIP', 'gravityformsmailchimp' ),
),
),
),
array(
'name' => 'tags',
'type' => 'text',
'class' => 'medium merge-tag-support mt-position-right mt-hide_all_fields',
'label' => esc_html__( 'Tags', 'gravityformsmailchimp' ),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Tags', 'gravityformsmailchimp' ),
esc_html__( 'Associate tags to your Mailchimp contacts with a comma separated list (e.g. new lead, Gravity Forms, web source). Commas within a merge tag value will be created as a single tag.', 'gravityformsmailchimp' )
),
),
array(
'name' => 'note',
'type' => 'textarea',
'class' => 'medium merge-tag-support mt-position-right mt-hide_all_fields',
'label' => esc_html__( 'Note', 'gravityformsmailchimp' ),
),
array(
'name' => 'optinCondition',
'label' => esc_html__( 'Conditional Logic', 'gravityformsmailchimp' ),
'type' => 'feed_condition',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Conditional Logic', 'gravityformsmailchimp' ),
esc_html__( 'When conditional logic is enabled, form submissions will only be exported to Mailchimp when the conditions are met. When disabled all form submissions will be exported.', 'gravityformsmailchimp' )
),
),
array( 'type' => 'save' ),
),
),
);
// Get currently selected list/audience.
$list = $this->get_setting( 'mailchimpList' );
// If a list/audience is selected, get marketing permissions and add setting.
if ( $list ) {
try {
// Get Mailchimp list/audience,
$list = $this->api->get_list( $list );
// If marketing permissions are enabled for list/audience, add setting.
if ( rgar( $list, 'marketing_permissions' ) ) {
// Prepare setting.
$setting = array(
'name' => 'marketingPermissions',
'label' => esc_html__( 'Marketing Permissions', 'gravityformsmailchimp' ),
'type' => 'marketing_permissions',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Marketing Permissions', 'gravityformsmailchimp' ),
esc_html__( 'When enabled and conditions are met, users will be opted into your Mailchimp audience marketing permissions. If a user is already subscribed to your audience, they will not be opted out of permissions they are already opted into.', 'gravityformsmailchimp' )
),
);
// Add setting.
$settings = $this->add_field_after( 'interestCategories', $setting, $settings );
}
} catch ( Exception $e ) {
// Log that list/audience could not be retrieved.
$this->log_error( __METHOD__ . '(): Unable to add Marketing Permissions field because audience could not be retrieved; ' . $e->getMessage() );
}
}
return $settings;
}
/**
* Define the markup for the mailchimp_list type field.
*
* @since 3.0
* @access public
*
* @param array $field The field properties.
* @param bool $echo Should the setting markup be echoed. Defaults to true.
*
* @return string
*/
public function settings_mailchimp_list( $field, $echo = true ) {
// Initialize HTML string.
$html = '';
// If API is not initialized, return.
if ( ! $this->initialize_api() ) {
return $html;
}
// Prepare list/audience request parameters.
$params = array( 'start' => 0, 'limit' => 100 );
// Filter parameters.
$params = apply_filters( 'gform_mailchimp_lists_params', $params );
// Convert start parameter to 3.0.
if ( isset( $params['start'] ) ) {
$params['offset'] = $params['start'];
unset( $params['start'] );
}
// Convert limit parameter to 3.0.
if ( isset( $params['limit'] ) ) {
$params['count'] = $params['limit'];
unset( $params['limit'] );
}
try {
// Log contact lists/audiences request parameters.
$this->log_debug( __METHOD__ . '(): Retrieving contact audiences; params: ' . print_r( $params, true ) );
// Get lists/audiences.
$lists = $this->api->get_lists( $params );
} catch ( Exception $e ) {
// Log that contact lists/audiences could not be obtained.
$this->log_error( __METHOD__ . '(): Could not retrieve Mailchimp contact audiences; ' . $e->getMessage() );
// Display error message.
printf( esc_html__( 'Could not load Mailchimp contact audiences. %sError: %s', 'gravityformsmailchimp' ), '<br/>', $e->getMessage() );
return;
}
// If no lists/audiences were found, display error message.
if ( 0 === $lists['total_items'] ) {
// Log that no lists/audiences were found.
$this->log_error( __METHOD__ . '(): Could not load Mailchimp contact audiences; no audiences found.' );
// Display error message.
printf( esc_html__( 'Could not load Mailchimp contact audiences. %sError: %s', 'gravityformsmailchimp' ), '<br/>', esc_html__( 'No audiences found.', 'gravityformsmailchimp' ) );
return;
}
// Log number of lists/audiences retrieved.
$this->log_debug( __METHOD__ . '(): Number of audiences: ' . count( $lists['lists'] ) );
// Initialize select options.
$options = array(
array(
'label' => esc_html__( 'Select a Mailchimp Audience', 'gravityformsmailchimp' ),
'value' => '',
),
);
// Loop through Mailchimp lists/audiences.
foreach ( $lists['lists'] as $list ) {
// Add list/audience to select options.
$options[] = array(
'label' => esc_html( $list['name'] ),
'value' => esc_attr( $list['id'] ),
);
}
// Add select field properties.
$field['type'] = 'select';
$field['choices'] = $options;
$field['onchange'] = 'jQuery(this).parents("form").submit();';
// Generate select field.
$html = $this->settings_select( $field, false );
if ( $echo ) {
echo $html;
}
return $html;
}
/**
* Return an array of Mailchimp list/audience fields which can be mapped to the Form fields/entry meta.
*
* @since 3.0
* @access public
*
* @return array
*/
public function merge_vars_field_map() {
// Initialize field map array.
$field_map = array(
'EMAIL' => array(
'name' => 'EMAIL',
'label' => esc_html__( 'Email Address', 'gravityformsmailchimp' ),
'required' => true,
'field_type' => array( 'email', 'hidden' ),
),
);
// If unable to initialize API, return field map.
if ( ! $this->initialize_api() ) {
return $field_map;
}
// Get current list/audience ID.
$list_id = $this->get_setting( 'mailchimpList' );
// Get merge fields.
$merge_fields = $this->get_list_merge_fields( $list_id );
// If merge fields exist, add to field map.
if ( ! empty( $merge_fields['merge_fields'] ) ) {
// Loop through merge fields.
foreach ( $merge_fields['merge_fields'] as $merge_field ) {
// Define required field type.
$field_type = null;
// If this is an email merge field, set field types to "email" or "hidden".
if ( 'EMAIL' === strtoupper( $merge_field['tag'] ) ) {
$field_type = array( 'email', 'hidden' );
}
// If this is an address merge field, set field type to "address".
if ( 'address' === $merge_field['type'] ) {
$field_type = array( 'address' );
}
// Add to field map.
$field_map[ $merge_field['tag'] ] = array(
'name' => $merge_field['tag'],
'label' => $merge_field['name'],
'required' => $merge_field['required'],
'field_type' => $field_type,
);
}
}
return $field_map;
}
/**
* Prevent feeds being listed or created if the API key isn't valid.
*
* @since 3.0
* @access public
*
* @return bool
*/
public function can_create_feed() {
return $this->initialize_api();
}
/**
* Allow the feed to be duplicated.
*
* @since 4.7
*
* @param array|int $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 3.0
* @access public
*
* @return array
*/
public function feed_list_columns() {
return array(
'feedName' => esc_html__( 'Name', 'gravityformsmailchimp' ),
'mailchimp_list_name' => esc_html__( 'Mailchimp List', 'gravityformsmailchimp' ),
);
}
/**
* Returns the value to be displayed in the Mailchimp List/Audience column.
*
* @since 3.0
* @access public
*
* @param array $feed The feed being included in the feed list/audience.
*
* @return string
*/
public function get_column_value_mailchimp_list_name( $feed ) {
// If unable to initialize API, return the list/audience ID.
if ( ! $this->initialize_api() ) {
return rgars( $feed, 'meta/mailchimpList' );
}
try {
// Get list/audience.
$list = $this->api->get_list( rgars( $feed, 'meta/mailchimpList' ) );
// Return list/audience name.
return rgar( $list, 'name' );
} catch ( Exception $e ) {
// Log error.
$this->log_error( __METHOD__ . '(): Unable to get Mailchimp audience for feed list; ' . $e->getMessage() );
// Return list/audience ID.
return rgars( $feed, 'meta/mailchimpList' );
}
}
/**
* Define the markup for the interest categories type field.
*
* @since 4.0
* @access public
*
* @param array $field The field properties.
* @param bool $echo Should the setting markup be echoed.
*
* @return string
*/
public function settings_interest_categories( $field, $echo = true ) {
// Get interest categories.
$categories = $this->get_interest_categories();
// If no categories are found, return.
if ( empty( $categories ) ) {
$this->log_debug( __METHOD__ . '(): No categories found.' );
return '';
}
// Start field markup.
$html = "<div id='gaddon-mailchimp_interest_categories'>";
// Loop through interest categories.
foreach ( $categories as $category ) {
// Open category container.
$html .= '<div class="gaddon-mailchimp-category">';
// Define label.
$label = rgar( $category, 'title' );
// Display category label.
$html .= '<div class="gaddon-mailchimp-categoryname">' . esc_html( $label ) . '</div><div class="gf_animate_sub_settings">';
// Get interests category interests.
$interests = $this->api->get_interest_category_interests( $category['list_id'], $category['id'] );
// Loop through interests.
foreach ( $interests as $interest ) {
// Define interest key.
$interest_key = 'interestCategory_' . $interest['id'];
// Define enabled checkbox key.
$enabled_key = $interest_key . '_enabled';
// Get interest checkbox markup.
$html .= $this->settings_checkbox(
array(
'name' => esc_html( $interest['name'] ),
'type' => 'checkbox',
'onclick' => "if(this.checked){jQuery('#{$interest_key}_condition_container').slideDown();} else{jQuery('#{$interest_key}_condition_container').slideUp();}",
'choices' => array(
array(
'name' => $enabled_key,
'label' => esc_html( $interest['name'] ),
),
),
),
false
);
$html .= $this->interest_category_condition( $interest_key );
}
$html .= '</div></div>';
}
$html .= '</div>';
if ( $echo ) {
echo $html;
}
return $html;
}
/**
* Define the markup for the interest category conditional logic.
*
* @since 4.0
* @access public
*
* @param string $setting_name_root The category setting key.
*
* @return string
*/
public function interest_category_condition( $setting_name_root ) {
$condition_enabled_setting = "{$setting_name_root}_enabled";
$is_enabled = $this->get_setting( $condition_enabled_setting ) == '1';
$container_style = ! $is_enabled ? "style='display:none;'" : '';
$str = "<div id='{$setting_name_root}_condition_container' {$container_style} class='condition_container'>" .
esc_html__( 'Assign to group:', 'gravityformsmailchimp' ) . ' ';
$str .= $this->settings_select(
array(
'name' => "{$setting_name_root}_decision",
'type' => 'select',
'choices' => array(
array(
'value' => 'always',
'label' => esc_html__( 'Always', 'gravityformsmailchimp' )
),
array(
'value' => 'if',
'label' => esc_html__( 'If', 'gravityformsmailchimp' )
),
),
'onchange' => "if(jQuery(this).val() == 'if'){jQuery('#{$setting_name_root}_decision_container').show();}else{jQuery('#{$setting_name_root}_decision_container').hide();}",
), false
);
$decision = $this->get_setting( "{$setting_name_root}_decision" );
if ( empty( $decision ) ) {
$decision = 'always';
}
$conditional_style = $decision == 'always' ? "style='display:none;'" : '';
$str .= ' <span id="' . $setting_name_root . '_decision_container" class="gform-settings-simple-condition" ' . $conditional_style . '><br />' .
$this->simple_condition( $setting_name_root, $is_enabled ) .
' </span>' .
'</div>';
return $str;
}
/**
* Define the markup for the Marketing Permissions feed settings field.
*
* @since 4.6
* @access public
*
* @param array $field The field properties.
* @param bool $echo Should the setting markup be echoed.
*
* @return string
*/
public function settings_marketing_permissions( $field, $echo = true ) {
// Get current list/audience.
$list = $this->get_setting( 'mailchimpList' );
// Get marketing permissions.
$permissions = $this->get_marketing_permissions( $list );
// If permissions are not available, display error message.
if ( ! $permissions ) {
$html = esc_html__( 'You must have at least one audience subscriber to configure Marketing Permissions.', 'gravityformsmailchimp' );
$html .= ' ' . gform_tooltip( esc_html__( "Due to limitations with Mailchimp's API, we are only able to get available Marketing Permissions when the selected audience has at least one subscriber.", 'gravityformsmailchimp' ), '', true );
if ( $echo ) {
echo $html;
}
return $html;
}
// Start field markup.
$html = "<div id='gaddon-mailchimp_marketing_permissions'>";
// Loop through marketing permissions, add conditional logic for each.
foreach ( $permissions as $permission ) {
// Prepare permission key.
$permission_key = $field['name'] . '_' . $permission['marketing_permission_id'];
// Open category container.
$html .= '<div class="gaddon-mailchimp-permission">';
// Display toggle checkbox.
$html .= $this->settings_checkbox(
array(
'name' => esc_html( $permission['marketing_permission_id'] ),
'type' => 'checkbox',
'choices' => array(
array(
'name' => $permission_key . '_enabled',
'label' => esc_html( $permission['text'] ),
'class' => 'gaddon-mailchimp-permission-toggle',
'onclick' => "if(this.checked){jQuery('#{$permission_key}_condition_container').slideDown();} else{jQuery('#{$permission_key}_condition_container').slideUp();}",
),
),
),
false
);
// Display condition field for permission.
$html .= $this->marketing_permissions_condition( $permission_key );
$html .= '</div>';
}
$html .= '</div>';
if ( $echo ) {
echo $html;
}
return $html;
}
/**
* Define the markup for the Marketing Permissions conditional logic.
*
* @since 4.6
* @access public
*
* @param string $setting_name_root The category setting key.
*
* @return string
*/
public function marketing_permissions_condition( $setting_name_root ) {
$condition_enabled_setting = "{$setting_name_root}_enabled";
$is_enabled = '1' === $this->get_setting( $condition_enabled_setting );
$container_style = ! $is_enabled ? "style='display:none;'" : '';
$str = sprintf(
'<div id="%s_condition_container" %s class="condition_container gf_animate_sub_settings"><span id="%s_condition_label" class="condition_label">%s</span>',
$setting_name_root,
$container_style,
$setting_name_root,
esc_html__( 'Enable permission if:', 'gravityformsmailchimp' )
);
$str .= ' <span id="' . $setting_name_root . '_decision_container" class="gform-settings-simple-condition"><br />' .
$this->simple_condition( $setting_name_root, $is_enabled ) .
' </span>' .
'</div>';
return $str;