-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-gf-constantcontact.php
More file actions
1640 lines (1400 loc) · 51.9 KB
/
class-gf-constantcontact.php
File metadata and controls
1640 lines (1400 loc) · 51.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// Include the Gravity Forms Add-On Framework
GFForms::include_feed_addon_framework();
/**
* Gravity Forms Constant Contact Add-On.
*
* The class name should be GF_Constant_Contact but unfortunately it conflicts with the legacy version.
* So we use GF_ConstantContact instead.
*
* @since 1.0
* @package GravityForms
* @author Rocketgenius
* @copyright Copyright (c) 2019, Rocketgenius
*/
class GF_ConstantContact extends GFFeedAddOn {
/**
* Contains an instance of this class, if available.
*
* @since 1.0
* @access private
* @var GF_ConstantContact $_instance If available, contains an instance of this class
*/
private static $_instance = null;
/**
* Version of this add-on which requires reauthentication with the API.
*
* Anytime updates are made to this class that requires a site to reauthenticate Gravity Forms with Constant Contact, this
* constant should be updated to the value of GF_ConstantContact::$version.
*
* @since 1.6
*
* @see GF_ConstantContact::$version
*/
const LAST_REAUTHENTICATION_VERSION = '1.5.1';
/**
* Defines the version of the Gravity Forms Constant Contact Add-On Add-On.
*
* @since 1.0
* @access protected
* @var string $_version Contains the version.
*/
protected $_version = GF_CONSTANTCONTACT_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 = GF_CONSTANTCONTACT_MIN_GF_VERSION;
/**
* Defines the plugin slug.
*
* @since 1.0
* @access protected
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformsconstantcontact';
/**
* 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 = 'gravityformsconstantcontact/constantcontact.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
*/
protected $_url = 'https://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 Constant Contact 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 = 'Constant Contact';
/**
* 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 capabilities needed for the Constant Contact Add-On
*
* @since 1.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array( 'gravityforms_constantcontact', 'gravityforms_constantcontact_uninstall' );
/**
* 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_constantcontact';
/**
* 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_constantcontact';
/**
* 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_constantcontact_uninstall';
/**
* Contains an instance of the Constant Contact API library, if available.
*
* @since 1.0
* @access protected
* @var GF_ConstantContact_API $api If available, contains an instance of the Constant Contact API library.
*/
protected $api = null;
/**
* Enabling background feed processing to prevent API performance and token refresh issues delaying form submission completion.
*
* @since 1.7
*
* @var bool
*/
protected $_async_feed_processing = true;
/**
* Defines how many times the add-on should retry to refresh the token before disconnecting.
*
* @since 1.7
*/
const CC_REFRESH_RETRIES_BEFORE_DISCONNECT = 6;
/**
* Returns an instance of this class, and stores it in the $_instance property.
*
* @since 1.0
* @access public
* @return GF_ConstantContact $_instance An instance of the GF_ConstantContact class
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GF_ConstantContact();
}
return self::$_instance;
}
private function __clone() {
} /* do nothing */
/**
* Register needed plugin hooks and PayPal delayed payment support.
*
* @access public
* @return void
*/
public function init() {
parent::init();
add_filter( 'gform_settings_header_buttons', array( $this, 'filter_gform_settings_header_buttons' ), 99 );
$this->add_delayed_payment_support(
array(
'option_label' => esc_html__( 'Subscribe contact to Constant Contact only when payment is received.', 'gravityformsconstantcontact' ),
)
);
}
/**
* Add AJAX callbacks.
*
* @since 1.0
*/
public function init_ajax() {
parent::init_ajax();
// Add AJAX callback for de-authorizing with Constant Contact.
add_action( 'wp_ajax_gfconstantcontact_deauthorize', array( $this, 'ajax_deauthorize' ) );
// Add AJAX callback to get auth url.
add_action( 'wp_ajax_gfconstantcontact_get_auth_url', array( $this, 'ajax_get_auth_url' ) );
}
/**
* Admin initial actions.
*
* @since 1.0
*/
public function init_admin() {
parent::init_admin();
add_action( 'admin_init', array( $this, 'request_access_token' ) );
add_action( 'admin_notices', array( $this, 'display_notices' ) );
}
/**
* Enqueue admin scripts.
*
* @since 1.0
*
* @return array
*/
public function scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$scripts = array(
array(
'handle' => 'gform_constantcontact_pluginsettings',
'deps' => array( 'jquery' ),
'src' => $this->get_base_url() . "/js/plugin_settings{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings' ),
'tab' => $this->_slug,
),
),
'strings' => array(
'auth' => esc_html__( "Do you use your Constant Contact account ONLY for this website?\n\nIf not, please set up a custom app.", 'gravityformsconstantcontact' ),
'disconnect' => esc_html__( 'Are you sure you want to disconnect from Constant Contact?', 'gravityformsconstantcontact' ),
'settings_url' => admin_url( 'admin.php?page=gf_settings&subview=' . $this->get_slug() ),
'ajax_nonce' => wp_create_nonce( 'gfconstantcontact_ajax' ),
),
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Enqueue needed stylesheets.
*
* @since 1.0
*
* @return array
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gform_constantcontact_pluginsettings',
'src' => $this->get_base_url() . "/css/plugin_settings{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings' ),
'tab' => $this->_slug,
),
),
),
);
return array_merge( parent::styles(), $styles );
}
/**
* Return the plugin's icon for the plugin/form settings menu.
*
* @since 1.4
*
* @return string
*/
public function get_menu_icon() {
return file_get_contents( $this->get_base_path() . '/images/menu-icon.svg' );
}
/**
* Maybe save access token.
*
* @since 1.0
* @since 1.4 Added CSRF nonce (the state param).
*
* @return void
*/
public function plugin_settings_page() {
// Check the state value.
if ( rgget( 'state' ) && ! wp_verify_nonce( rgget( 'state' ), $this->get_authentication_state_action() ) ) {
GFCommon::add_error_message( esc_html__( 'Unable to connect to Constant Contact due to mismatched state.', 'gravityformsconstantcontact' ) );
}
$auth_payload = rgget( 'auth_payload' );
if ( $auth_payload == 'false' ) {
// Add error message.
GFCommon::add_error_message( esc_html__( 'Unable to connect to Constant Contact.', 'gravityformsconstantcontact' ) );
}
return parent::plugin_settings_page();
}
/**
* Setup plugin settings fields.
*
* @since 1.0
*
* @return array
*/
public function plugin_settings_fields() {
return array(
array(
'description' => sprintf(
'<p>%s</p>',
sprintf(
esc_html__( 'Constant Contact 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 Constant Contact subscriber list. If you don\'t have a Constant Contact account, you can %1$s sign up for one here.%2$s', 'gravityformsconstantcontact' ),
'<a href="https://www.constantcontact.com/?pn=gravityforms" target="_blank">', '</a>' . $this->get_instructions()
)
),
'fields' => array(
array(
'name' => 'auth_token',
'type' => 'auth_token',
'feedback_callback' => array( $this, 'initialize_api' ),
),
),
),
);
}
/**
* Hide submit button on plugin settings page.
*
* @since 1.4
*
* @param string $html
*
* @return string
*/
public function filter_gform_settings_header_buttons( $html = '' ) {
// If this is not the plugin settings page, return.
if ( ! $this->is_plugin_settings( $this->get_slug() ) ) {
return $html;
}
return '';
}
/**
* Prepare settings to be rendered on feed settings tab.
*
* @since 1.0
*
* @return array $fields - The feed settings fields
*/
public function feed_settings_fields() {
$settings = array(
array(
'title' => '',
'fields' => array(
array(
'name' => 'feedName',
'label' => __( 'Name', 'gravityformsconstantcontact' ),
'type' => 'text',
'class' => 'medium',
'required' => true,
'tooltip' => '<h6>' . __( 'Name', 'gravityformsconstantcontact' ) . '</h6>' . __( 'Enter a feed name to uniquely identify this setup.', 'gravityformsconstantcontact' ),
),
array(
'name' => 'list',
'label' => esc_html__( 'Constant Contact List', 'gravityformsconstantcontact' ),
'type' => 'constantcontact_list',
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Constant Contact List', 'gravityformsconstantcontact' ),
esc_html__( 'Select the Constant Contact list you would like to add your contacts to.', 'gravityformsconstantcontact' )
),
),
),
),
array(
'dependency' => 'list',
'fields' => array(
array(
'name' => 'fields',
'label' => __( 'Map Fields', 'gravityformsconstantcontact' ),
'type' => 'field_map',
'field_map' => $this->fields_for_feed_mapping(),
'tooltip' => '<h6>' . __( 'Map Fields', 'gravityformsconstantcontact' ) . '</h6>' . __( 'Select which Gravity Form fields pair with their respective Constant Contact fields.', 'gravityformsconstantcontact' ),
),
array(
'name' => 'feed_condition',
'label' => __( 'Conditional Logic', 'gravityformsconstantcontact' ),
'type' => 'feed_condition',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Conditional Logic', 'gravityformsconstantcontact' ),
esc_html__( 'When conditional logic is enabled, form submissions will only be exported to Constant Contact when the conditions are met. When disabled all form submissions will be exported.', 'gravityformsconstantcontact' )
),
),
),
),
);
if ( $this->initialize_api() ) {
$custom_fields = $this->api->get_custom_fields();
if ( is_wp_error( $custom_fields ) ) {
$this->log_debug( __METHOD__ . '(): No custom fields were set.' );
} elseif ( count( $custom_fields['custom_fields'] ) > 0 ) {
$cf_field = array(
array(
'name' => 'custom_fields',
'label' => __( 'Custom Fields', 'gravityformsconstantcontact' ),
'type' => 'dynamic_field_map',
'disable_custom' => true,
'limit' => 25,
'exclude_field_types' => 'creditcard',
'tooltip' => '<h6>' . __( 'Custom Fields', 'gravityformsconstantcontact' ) . '</h6>' . __( 'Select custom fields in Constant Contact to pair with Gravity Forms fields.', 'gravityformsconstantcontact' ),
'field_map' => $this->custom_fields_for_feed_mapping( $custom_fields['custom_fields'] ),
),
);
$settings = $this->add_field_after( 'fields', $cf_field, $settings );
}
}
return $settings;
}
/**
* Add custom fields for mapping.
*
* @since 1.0
*
* @param array $custom_fields Custom fields.
*
* @return array
*/
private function custom_fields_for_feed_mapping( $custom_fields ) {
$field_map = array(
array(
'name' => esc_html__( 'Select a Custom Field', 'gravityformsconstantcontact' ),
'value' => '',
'label' => esc_html__( 'Select a Custom Field', 'gravityformsconstantcontact' ),
),
);
foreach ( $custom_fields as $custom_field ) {
$field = array(
'name' => $custom_field['custom_field_id'],
'value' => $custom_field['custom_field_id'],
'label' => $custom_field['label'],
);
$field_map[] = $field;
}
return $field_map;
}
/**
* Prepare fields for field mapping feed settings field.
*
* @since 1.0
*
* @return array $field_map
*/
public function fields_for_feed_mapping() {
/* Setup initial field map */
$field_map = array(
array(
'name' => 'email_address',
'label' => __( 'Email Address', 'gravityformsconstantcontact' ),
'required' => true,
'field_type' => array( 'email' ),
),
array(
'name' => 'first_name',
'label' => __( 'First Name', 'gravityformsconstantcontact' ),
'field_type' => array( 'name', 'text' ),
),
array(
'name' => 'last_name',
'label' => __( 'Last Name', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'name', 'text' ),
),
array(
'name' => 'job_title',
'label' => __( 'Job Title', 'gravityformsconstantcontact' ),
'required' => false,
),
array(
'name' => 'company_name',
'label' => __( 'Company Name', 'gravityformsconstantcontact' ),
'required' => false,
),
array(
'name' => 'home_number',
'label' => __( 'Home Phone Number', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'phone', 'text' ),
),
array(
'name' => 'mobile_number',
'label' => __( 'Mobile Phone Number', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'phone', 'text' ),
),
array(
'name' => 'work_number',
'label' => __( 'Work Phone Number', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'phone', 'text' ),
),
array(
'name' => 'address_line_1',
'label' => __( 'Address', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'address', 'text' ),
),
array(
'name' => 'city_name',
'label' => __( 'City', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'address', 'text' ),
),
array(
'name' => 'state_name',
'label' => __( 'State', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'address', 'text' ),
),
array(
'name' => 'zip_code',
'label' => __( 'ZIP Code', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'address', 'text' ),
),
array(
'name' => 'country_name',
'label' => __( 'Country', 'gravityformsconstantcontact' ),
'required' => false,
'field_type' => array( 'address', 'text' ),
),
);
return $field_map;
}
/**
* Configures which columns should be displayed on the feed list page.
*
* @since 1.0
*
* @return array
*/
public function feed_list_columns() {
return array(
'feedName' => __( 'Name', 'gravityformsconstantcontact' ),
'list' => __( 'Constant Contact List', 'gravityformsconstantcontact' ),
);
}
/**
* Set feed creation control.
*
* @since 1.0
*
* @return bool
*/
public function can_create_feed() {
return (bool) $this->initialize_api();
}
/**
* Enable feed duplication.
*
* @since 1.0
*
* @param int $feed_id Feed ID.
*
* @return bool
*/
public function can_duplicate_feed( $feed_id ) {
return true;
}
/**
* Returns the value to be displayed in the list name column.
*
* @since 1.0
*
* @param array $feed The feed being included in the feed list.
*
* @return string
*/
public function get_column_value_list( $feed ) {
/* If Constant Contact instance is not initialized, return campaign ID. */
if ( ! $this->initialize_api() ) {
return rgars( $feed, 'meta/list' );
}
// Get list.
$list = $this->api->get_list( rgars( $feed, 'meta/list' ) );
if ( is_wp_error( $list ) ) {
// Log error.
$this->log_debug( __METHOD__ . '(): Unable to get Constant Contact list for feed list; ' . print_r( $list->get_error_messages(), true ) );
// Return list ID.
return rgars( $feed, 'meta/list' );
}
// Return list name.
return rgar( $list, 'name' );
}
/**
* Create Generate Auth Token settings field.
*
* @since 1.0
*
* @param array $field Field settings.
* @param bool $echo Display field. Defaults to true.
*
* @return string
*/
public function settings_auth_token( $field, $echo = true ) {
// Initialize return HTML.
$html = '';
// If Constant Contact is authenticated, display de-authorize button.
if ( $this->initialize_api() ) {
$html .= '<p>' . esc_html__( 'Connected to Constant Contact.', 'gravityformsconstantcontact' );
$html .= '</p>';
$html .= sprintf(
' <a href="#" class="button" id="gform_constantcontact_deauth_button">%1$s</a>',
esc_html__( 'Disconnect from Constant Contact', 'gravityformsconstantcontact' )
);
} else {
// If SSL is available, display custom app settings.
if ( is_ssl() ) {
$html .= $this->custom_app_settings();
} else {
$headline = esc_html__( 'SSL Certificate Required', 'gravityformsconstantcontact' ) . '</h4>';
$paragraph = sprintf( esc_html__( 'Make sure you have an SSL certificate installed and enabled, then %1$sclick here to continue%2$s.', 'gravityformsconstantcontact' ), '<a href="' . admin_url( 'admin.php?page=gf_settings&subview=gravityformsconstantcontact', 'https' ) . '">', '</a>' );
if ( version_compare( GFForms::$version, '2.5-dev-1', '<' ) ) {
$html .= sprintf( '<div class="alert_red" style="padding:20px; padding-top:5px;"><h4>%s</h4>%s</div>', $headline, $paragraph );
} else {
$html .= sprintf( '<div class="alert gforms_note_error">%s<br />%s</div>', $headline, $paragraph );
}
}
}
if ( $echo ) {
echo $html;
}
return $html;
}
/**
* Renders instructions for creating Constant Contact app.
*
* @since 1.0
*
* @return string
*/
public function get_instructions() {
// Don't display instructions if page is not under SSL or if already connected to Constant Contact.
if ( ! is_ssl() || $this->initialize_api() ) {
return '';
}
$html = '<h4>' . esc_html__( 'An application must be created with Constant Contact to get your API Key and App Secret. The app should be dedicated to this website, please do not use the same app with multiple sites.', 'gravityformsconstantcontact' ) . '</h4>';
$html .= '<ol>';
$html .= '<li>' . sprintf( esc_html__( 'Login to the %1$sConstant Contact V3 Portal%2$s and create a "New Application".', 'gravityformsconstantcontact' ), '<a href="https://app.constantcontact.com/pages/dma/portal" target="_blank">', '</a>' ) . '</li>';
$html .= '<li>' . esc_html__( 'Enter "Gravity Forms" for the application name and click "Save".', 'gravityformsconstantcontact' ) . '</li>';
$html .= '<li>' . esc_html__( 'Copy your Constant Contact API Key and paste it into the API Key field below.', 'gravityformsconstantcontact' ) . '</li>';
$html .= '<li>' . esc_html__( 'Click the "Generate Secret" button, go through the secret generation process and paste the resulted key in the "App Secret" field below.', 'gravityformsconstantcontact' ) . '</li>';
$html .= '<li>' . sprintf( esc_html__( 'Paste the URL %s into the Redirect URI field and click "Save" in the top right corner of the screen.', 'gravityformsconstantcontact' ), '<strong>' . $this->get_redirect_uri() . '</strong>' ) . '</li>';
$html .= '</ol>';
return $html;
}
/**
* Renders settings section for custom Constant Contact app.
*
* @since 1.0
*
* @return string
*/
public function custom_app_settings() {
$html = '';
// Open custom app table.
if ( version_compare( GFForms::$version, '2.5-dev-1', '<' ) ) {
$html .= '<table class="form-table">';
}
ob_start();
// Display custom app key.
$this->single_setting_row(
array(
'name' => 'custom_app_key',
'type' => 'text',
'label' => esc_html__( 'API Key', 'gravityformsconstantcontact' ),
'class' => 'medium',
)
);
// Display custom app secret.
$this->single_setting_row(
array(
'name' => 'custom_app_secret',
'type' => 'text',
'label' => esc_html__( 'App Secret', 'gravityformsconstantcontact' ),
'class' => 'medium',
)
);
$html .= ob_get_contents();
ob_end_clean();
// Display auth button.
$html .= version_compare( GFForms::$version, '2.5-dev-1', '<' ) ? '<tr><td></td><td>' : '';
$html .= sprintf(
'<button type="button" class="primary button large" id="gform_constantcontact_custom_auth_button">%1$s</button>',
esc_html__( 'Connect to Constant Contact', 'gravityformsconstantcontact' )
);
$html .= version_compare( GFForms::$version, '2.5-dev-1', '<' ) ? '</td></tr></table>' : '';
return $html;
}
/**
* Get Constant Contact app key.
*
* @since 1.0
*
* @return string
*/
public function get_app_key() {
// Get plugin settings.
$settings = $this->get_plugin_settings();
return rgar( $settings, 'custom_app_key' ) ? rgar( $settings, 'custom_app_key' ) : null;
}
/**
* Get Constant Contact app secret.
*
* @since 1.0
*
* @return string
*/
public function get_app_secret() {
// Get plugin settings.
$settings = $this->get_plugin_settings();
return rgar( $settings, 'custom_app_secret' ) ? rgar( $settings, 'custom_app_secret' ) : null;
}
/**
* Get OAuth Redirect URI for custom Constant Contact app.
*
* @since 1.0
*
* @return string
*/
public function get_redirect_uri() {
return admin_url( 'admin.php', 'https' );
}
/**
* Get Constant Contact authentication URL.
*
* @since 1.0
* @since 1.4 Used nonce as the state value.
*
* @param string $app_key Constant Contact app key.
*
* @return string
*/
public function get_auth_url( $app_key = null ) {
// If app key is empty, get from setting.
if ( rgblank( $app_key ) ) {
$app_key = $this->get_app_key();
}
// If app key or secret are empty, return null.
if ( rgblank( $app_key ) ) {
return null;
}
// Get base OAuth URL.
$auth_url = 'https://authz.constantcontact.com/oauth2/default/v1/authorize';
// Prepare OAuth URL parameters.
$auth_params = array(
'response_type' => 'code',
'client_id' => $app_key,
'scope' => 'contact_data+offline_access',
'redirect_uri' => urlencode( $this->get_redirect_uri() ),
'state' => wp_create_nonce( $this->get_authentication_state_action() ),
);
// Add parameters to OAuth url.
$auth_url = add_query_arg( $auth_params, $auth_url );
return $auth_url;
}
/**
* Define the markup for the Constant Contact type field.
*
* @since 1.0
*
* @param array $field The field properties.
* @param bool $echo Should the setting markup be echoed. Defaults to true.
*
* @return string
*/
public function settings_constantcontact_list( $field, $echo = true ) {
// Initialize HTML string.
$html = '';
// If API is not initialized, return.
if ( ! $this->initialize_api() ) {
return $html;
}
// Log contact lists request parameters.
$this->log_debug( __METHOD__ . '(): Retrieving contact lists;' );
// Get lists.
$lists = $this->api->get_lists();
if ( is_wp_error( $lists ) ) {
// Log that contact lists could not be obtained.
$this->log_debug( __METHOD__ . '(): Could not retrieve Constant Contact contact lists; ' . print_r( $lists->get_error_messages(), true ) );
// Display error message.
printf( esc_html__( 'Could not load Constant Contact contact lists. %sError: %s', 'gravityformconstantcontact' ), '<br/>', $lists->get_error_message() );
return;
}
// If no lists were found, display error message.
if ( 0 === $lists['lists_count'] ) {
// Log that no lists were found.
$this->log_debug( __METHOD__ . '(): Could not load Constant Contact contact lists; no lists found.' );
// Display error message.
printf( esc_html__( 'Could not load Constant Contact contact lists. %sError: %s', 'gravityformconstantcontact' ), '<br/>', esc_html__( 'No lists found.', 'gravityformconstantcontact' ) );
return;
}
// Log number of lists retrieved.
$this->log_debug( __METHOD__ . '(): Number of lists: ' . count( $lists['lists'] ) );
// Initialize select options.
$options = array(
array(
'label' => esc_html__( 'Select a Constant Contact List', 'gravityformconstantcontact' ),
'value' => '',
),
);
// Loop through Constant Contact lists.
foreach ( $lists['lists'] as $list ) {
// Add list to select options.
$options[] = array(
'label' => esc_html( $list['name'] ),
'value' => esc_attr( $list['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;
}
/**
* Initializes Constant Contact API if credentials are valid.
*
* @since 1.0
*
* @param array $auth_token Authentication token data.
*
* @return bool
*/
public function initialize_api( $auth_token = null ) {
// If API is already initialized and auth token is not provided, return true.
if ( ! is_null( $this->api ) ) {
return true;
}
// Load the API library.
if ( ! class_exists( 'GF_ConstantContact_API' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-gf-constant-contact-api.php';
}
// Get the OAuth tokens.
if ( empty( $auth_token ) ) {
$auth_token = $this->get_plugin_setting( 'auth_token' );
}
// If no OAuth tokens, do not run a validation check.
if ( empty( $auth_token['access_token'] ) || empty( $auth_token['refresh_token'] ) ) {
$this->log_debug( __METHOD__ . '(): API tokens are empty.' );
return false;
}
// Log validation step.
$this->log_debug( __METHOD__ . '(): Validating API Info.' );
// Setup a new Constant Contact object with the API credentials.
$ctct = new GF_ConstantContact_API( $auth_token );
// Assign API library to instance.
$this->api = $ctct;
// If the token has an expiry time from Constant Contact, use that. Legacy tokens prior to March 31, 2022 expire 7200 seconds after being used.
$time_to_expiration = ( ! empty( $auth_token['expires_in'] ) ) ? $auth_token['expires_in'] : 7199;
if ( time() > $auth_token['date_created'] + $time_to_expiration ) {
// Log that authentication test failed.