-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-gf-aweber.php
More file actions
2151 lines (1721 loc) · 59.1 KB
/
class-gf-aweber.php
File metadata and controls
2151 lines (1721 loc) · 59.1 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
// don't load directly
if ( ! defined( 'ABSPATH' ) ) {
die();
}
GFForms::include_feed_addon_framework();
/**
* Gravity Forms AWeber Add-On.
*
* @since 1.0
* @package GravityForms
* @author Rocketgenius
* @copyright Copyright (c) 2017, Rocketgenius
*/
class GFAWeber extends GFFeedAddOn {
/**
* Contains an instance of this class, if available.
*
* @since Unknown
* @access private
* @var GFAWeber|null $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Defines the version of the AWeber Add-On.
*
* @since Unknown
* @access protected
* @var string $_version Contains the version, defined from aweber.php
*/
protected $_version = GF_AWEBER_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since Unknown
* @access protected
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = '1.9.11';
/**
* Defines the plugin slug.
*
* @since Unknown
* @access protected
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformsaweber';
/**
* Defines the main plugin file.
*
* @since Unknown
* @access protected
* @var string $_path The path of the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformsaweber/aweber.php';
/**
* Defines the full path to the class file.
*
* @since Unknown
* @access protected
* @var string $_full_path The full path to this file.
*/
protected $_full_path = __FILE__;
/**
* Defines the URL where this Add-On can be found.
*
* @since Unknown
* @access protected
* @var string $_url The URL of the Add-On.
*/
protected $_url = 'https://www.gravityforms.com';
/**
* Defines the title of this Add-On.
*
* @since Unknown
* @access protected
* @var string $_title The title of the Add-On.
*/
protected $_title = 'AWeber Add-On';
/**
* Defines the short title of this Add-On.
*
* @since Unknown
* @access protected
* @var string $_short_title The short title of the Add-On.
*/
protected $_short_title = 'AWeber';
/**
* Defines if Add-On should use Gravity Forms server for update data.
*
* @since Unknown
* @access protected
* @var bool
*/
protected $_enable_rg_autoupgrade = true;
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since Unknown
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_aweber';
/**
* Defines the capability needed to access the Add-On form settings page.
*
* @since Unknown
* @access protected
* @var string $_capabilities_form_settings The capability needed to access the Add-On form settings page.
*/
protected $_capabilities_form_settings = 'gravityforms_aweber';
/**
* Defines the capability needed to uninstall the Add-On.
*
* @since Unknown
* @access protected
* @var string $_capabilities_uninstall The capability needed to uninstall the Add-On.
*/
protected $_capabilities_uninstall = 'gravityforms_aweber_uninstall';
/**
* Defines the capabilities needed for the AWeber Add-On.
*
* @since Unknown
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On.
*/
protected $_capabilities = array( 'gravityforms_aweber', 'gravityforms_aweber_uninstall' );
/**
* Enabling background feed processing to prevent performance issues delaying form submission completion.
*
* @since 2.12
*
* @var bool
*/
protected $_async_feed_processing = true;
/**
* Version of this add-on which requires reauthentication with the API.
*
* Anytime updates are made that requires a site to reauthenticate with AWeber, this
* constant should be updated to match the release value of GF_AWEBER_VERSION.
*
* @since 4.0.0
*/
const LAST_REAUTHENTICATION_VERSION = '4.0.0';
/**
* An instance of the Aweber API object.
*
* @since 4.0
*
* @var null|false|GF_Aweber_API An instance of the API object.
*/
private $api = null;
/**
* Get an instance of this class.
*
* @since Unknown
* @access public
* @static
*
* @return GFAWeber
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Plugin starting point.
* Handles hooks, loading of language files and PayPal delayed payment support.
*
* @since Unknown
* @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 AWeber only when payment is received.', 'gravityformsaweber' ),
) );
}
/**
* Updates the auth token before the renderer is initialized with the settings from the db.
*
* @since 4.0
*/
public function plugin_settings_init() {
$this->maybe_update_auth_tokens();
parent::plugin_settings_init();
}
/**
* Register admin initialization hooks.
*
* @since 4.0
*/
public function init_admin() {
parent::init_admin();
add_action( 'admin_notices', array( $this, 'maybe_show_authentication_warning' ) );
}
/**
* Add Ajax callback.
*
* @since 4.0
*/
public function init_ajax() {
parent::init_ajax();
// Ajax callback to disconnect Aweber account.
add_action( 'wp_ajax_gfaweber_deauthorize', array( $this, 'ajax_deauthorize' ) );
}
/**
* Return the plugin's icon for the plugin/form settings menu.
*
* @since 2.11
*
* @return string
*/
public function get_menu_icon() {
return file_get_contents( $this->get_base_path() . '/images/menu-icon.svg' );
}
/**
* Return the scripts which should be enqueued.
*
* @since 4.0
*
* @return array Scripts to be enqueued.
*/
public function scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$scripts = array(
array(
'handle' => 'gform_aweber_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->get_slug(),
),
),
'strings' => array(
/* translators: Confirmation question displayed when user clicks button to disconnect Aweber */
'disconnect' => wp_strip_all_tags( __( 'Are you sure you want to disconnect AWeber?', 'gravityformsaweber' ) ),
'ajax_nonce' => wp_create_nonce( 'gf_aweber_ajax' ),
'is_legacy' => ! $this->is_gravityforms_supported( '2.5-beta' ) ? 'true' : 'false',
),
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Register needed styles.
*
* @since 4.0
*
* @return array $styles
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gform_aweber_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 );
}
// # PLUGIN SETTINGS -----------------------------------------------------------------------------------------------
/**
* Define the settings which should appear on the Forms > Settings > AWeber tab.
*
* @since Unknown
* @access public
*
* @return array
*/
public function plugin_settings_fields() {
$auth_url = 'https://auth.aweber.com/1.0/oauth/authorize_app/' . $this->get_app_id();
$auth_a_tag = sprintf( '<a onclick="window.open(this.href,\'\',\'resizable=yes,location=no,width=750,height=525,status\'); return false" href="%s">', esc_url( $auth_url ) );
return array(
array(
'title' => esc_html__( 'AWeber Account Information', 'gravityformsaweber' ),
'description' => sprintf(
'<p>%s</p>',
sprintf(
/* translators: 1: Open link tag, 2: Close link tag. */
esc_html__( 'AWeber is an email marketing software for designers and their clients. Use Gravity Forms to collect customer information and automatically add it to your client\'s AWeber subscription list. If you don\'t have an AWeber account, you can %1$ssign up for one here%2$s', 'gravityformsaweber' ),
'<a href="http://www.aweber.com" target="_blank">',
'</a>.'
)
),
'fields' => array(
array(
'name' => 'authorizationCode',
'type' => 'hidden',
'class' => 'medium',
'description' => esc_html__( 'You can find your unique Authorization code by clicking on the link above and logging into your AWeber account.', 'gravityformsaweber' ),
'feedback_callback' => array( $this, 'is_valid_key' ),
),
array(
'name' => 'auth_button',
'label' => '',
'type' => 'oauth_connect_button',
),
),
),
);
}
/**
* Initialize Aweber API.
*
* @since 4.0
*
* @return boolean|GF_Aweber_API False if the API failed to be initialized, or instance of the API object.
*/
public function initialize_api() {
if ( ! is_null( $this->api ) ) {
return is_object( $this->api );
}
// Load the API library file if necessary.
if ( ! class_exists( 'GF_Aweber_API' ) ) {
require_once 'includes/class-gf-aweber-api.php';
}
$arg = false;
$settings = $this->get_plugin_settings();
if ( empty( $settings ) ) {
$this->api = false;
return false;
}
if ( ! empty( $settings['auth_token'] ) ) {
$arg = $settings['auth_token'];
if ( is_array( $arg ) && ! empty( $arg['refresh_token'] ) && ! empty( $arg['time_created'] ) ) {
$arg = $this->maybe_renew_token( $arg );
}
} elseif ( ! empty( $settings['authorizationCode'] ) ) {
$arg = $this->get_aweber_object();
}
if ( empty( $arg ) ) {
$this->api = false;
return false;
}
$this->api = new GF_Aweber_API( $arg );
$this->log_debug( __METHOD__ . '(): API initialized.' );
return $this->api;
}
/**
* Checks if token should be renewed and tries to renew it.
*
* @since 4.0
*
* @param array $auth_data The stored authentication data.
*
* @return false|array False or auth data.
*/
public function maybe_renew_token( $auth_data ) {
if ( time() < ( $auth_data['time_created'] + $auth_data['expires_in'] ) ) {
return $auth_data;
}
// Call the refresh endpoint on Gravity API.
$args = array(
'body' => array(
'refreshtoken' => $auth_data['refresh_token'],
),
);
$response = wp_remote_post(
GF_Aweber_API::get_gravity_api_url( 'refresh' ),
$args
);
if ( is_wp_error( $response ) ) {
$this->log_error( __METHOD__ . '(): Unable to refresh token; ' . $response->get_error_message() );
}
// Check if the request was successful.
$response_code = wp_remote_retrieve_response_code( $response );
if ( $response_code === 200 ) {
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $response_body['access_token'] ) && ! empty( $response_body['refresh_token'] ) ) {
$this->log_debug( __METHOD__ . '(): Token was refreshed successfully.' );
return $this->update_auth_data( $response_body, true );
} else {
$this->log_error( __METHOD__ . '(): Missing auth_payload; ' . print_r( $response, true ) );
return false;
}
}
// Log that token could not be renewed.
$details = wp_remote_retrieve_body( $response );
$this->log_error( __METHOD__ . '(): Unable to refresh token; ' . $details );
return false;
}
/**
* Generate HTML for the button to start the OAuth process, or to disconnect an Aweber account.
*
* @since 4.0
*
* @param array $field Field settings.
* @param bool $echo Display field. Defaults to true.
*
* @return string HTML for the button to start the OAuth process, or to disconnect a Aweber account.
*/
public function settings_oauth_connect_button( $field, $echo = true ) {
// Check if Aweber API is available.
if ( ! $this->initialize_api() || $this->api->is_legacy_connection() ) {
if ( ! is_ssl() ) {
$settings_url = admin_url( 'admin.php?page=gf_settings&subview=' . $this->get_slug(), 'https' );
$alert_class = $this->is_gravityforms_supported( '2.5-beta' ) ? 'alert gforms_note_error' : 'alert_red';
ob_start();
?>
<div class="<?php echo esc_attr( $alert_class ); ?>">
<h4><?php esc_html_e( 'SSL Certificate Required', 'gravityformsaweber' ); ?></h4>
<?php
/* translators: 1: Open link tag, 2: Close link tag. */
printf( esc_html__( 'Make sure you have an SSL certificate installed and enabled, then %1$sclick here to continue%2$s.', 'gravityformsaweber' ), '<a href="' . $settings_url . '">', '</a>' );
?>
</div>
<?php
$html = ob_get_clean();
} else {
// Aweber API not initialized, display OAuth connect button.
$nonce = wp_create_nonce( $this->get_authentication_state_action() );
$settings_url = rawurlencode(
add_query_arg(
array(
'page' => 'gf_settings',
'subview' => $this->get_slug(),
),
admin_url( 'admin.php' )
)
);
if ( get_transient( "gravityapi_request_{$this->_slug}" ) ) {
delete_transient( "gravityapi_request_{$this->_slug}" );
}
set_transient( "gravityapi_request_{$this->_slug}", $nonce, 10 * MINUTE_IN_SECONDS );
// Generate a random string and store it in the Add-On settings, it will be returned with the redirect.
$settings = $this->get_plugin_settings();
// Load the API library file if necessary.
if ( ! class_exists( 'GF_Aweber_API' ) ) {
require_once 'includes/class-gf-aweber-api.php';
}
$oauth_url = add_query_arg(
array(
'redirect_to' => $settings_url,
'license' => GFCommon::get_key(),
'state' => $nonce,
),
GF_Aweber_API::get_gravity_api_url()
);
$connect_button = esc_html__( 'Connect to AWeber', 'gravityformsaweber' );
$html = sprintf(
'<a href="%1$s" class="primary button large" id="gform_aweber_connect_button">%2$s</a>',
$oauth_url,
/* translators: SVG button connect Aweber account */
$connect_button
);
}
} else {
$html = sprintf(
'<a href="#" class="button" id="gform_aweber_disconnect_button">%1$s</a>',
esc_html__( 'Disconnect from AWeber', 'gravityformsaweber' )
);
}
$html = sprintf( '<p class="connected_to_aweber_text">%s</p>', $html );
if ( $echo ) {
echo $html;
}
return $html;
}
/**
* When users are redirected back to the website after finishing the onboarding, get the auth tokens.
*
* @since 4.0
*/
public function maybe_update_auth_tokens() {
$payload = $this->get_oauth_payload();
if ( ! $payload || $this->is_save_postback() ) {
return;
}
// Verify state.
if ( rgpost( 'state' ) && ! wp_verify_nonce( rgar( $payload, 'state' ), $this->get_authentication_state_action() ) ) {
GFCommon::add_error_message( esc_html__( 'Unable to connect to AWeber due to mismatched state.', 'gravityformsaweber' ) );
return;
}
// If error is provided, display message.
if ( rgpost( 'auth_error' ) || isset( $payload['auth_error'] ) || empty( $payload['auth_payload'] ) ) {
// Add error message.
GFCommon::add_error_message( esc_html__( 'Unable to connect your AWeber account.', 'gravityformsaweber' ) );
}
$auth_data = json_decode( base64_decode( rgar( $payload, 'auth_payload' ) ), true );
$code = rgar( $auth_data, 'code' );
// If authorization state and code are provided, attempt to create an access token.
if ( $code && $this->is_plugin_settings( $this->_slug ) ) {
// Get current plugin settings.
$settings = $this->get_plugin_settings();
$access_token = rgars( $settings, 'auth_token/access_token', '' );
if ( ( '' === $access_token ) ) {
if ( ! $this->exchange_code_for_access_token( $code ) ) {
// Add error message.
GFCommon::add_error_message( esc_html__( 'Authentication with Aweber was not successful.', 'gravityformsaweber' ) );
return;
}
$this->log_debug( __METHOD__ . '(): Auth code was exchanged for token successfully.' );
wp_redirect(
add_query_arg(
array(
'page' => 'gf_settings',
'subview' => $this->get_slug(),
),
admin_url( 'admin.php' )
)
);
exit();
}
}
}
/**
* Get the authorization payload data.
*
* Returns the auth POST request if it's present, otherwise attempts to return a recent transient cache.
*
* @since 4.0
*
* @return array
*/
private function get_oauth_payload() {
$payload = array_filter(
array(
'auth_payload' => rgpost( 'auth_payload' ),
'auth_error' => rgpost( 'auth_error' ),
'state' => rgpost( 'state' ),
)
);
if ( count( $payload ) === 2 || isset( $payload['auth_error'] ) ) {
return $payload;
}
$payload = get_transient( "gravityapi_response_{$this->_slug}" );
if ( rgar( $payload, 'state' ) !== get_transient( "gravityapi_request_{$this->_slug}" ) ) {
return array();
}
delete_transient( "gravityapi_response_{$this->_slug}" );
return is_array( $payload ) ? $payload : array();
}
/**
* Get action name for authentication state.
*
* @since 4.0
*
* @return string
*/
public function get_authentication_state_action() {
return 'gform_aweber_authentication_state';
}
/**
* Adds a warning message if the add-on is still connected using OAuth 1.
*
* @since 4.0
*/
public function maybe_show_authentication_warning() {
$settings = $this->get_plugin_settings();
if ( empty( $settings ) || version_compare( rgar( $settings, 'reauth_version' ), self::LAST_REAUTHENTICATION_VERSION, '>=' ) ) {
return;
};
$message = sprintf(
/* translators: 1: Open link tag, 2: Close link tag. */
esc_html__( 'You are connected to AWeber using an authentication method that might be deprecated soon, please connect using the new authentication method to avoid any foreseeable issues. %1$sLearn more.%2$s', 'gravityformsaweber' ),
'<a target="_blank" href="https://docs.gravityforms.com/category/add-ons-gravity-forms/aweber-add-on">',
'</a>'
);
echo sprintf( '<div class="notice notice-error gf-notice"><p>%s</p></div>', $message );
}
/**
* Exchange code for access token and refresh token.
*
* @since 4.0
*
* @param string $code code provided by Aweber API to exchange for access token.
*
* @return boolean true if tokens successfully saved.
*/
private function exchange_code_for_access_token( $code = '' ) {
// Load the API library file if necessary.
if ( ! class_exists( 'GF_Aweber_API' ) ) {
require_once 'includes/class-gf-aweber-api.php';
}
$redirect_url = GF_Aweber_API::get_gravity_api_url( '/code' );
$response = wp_remote_post(
$redirect_url,
array( 'body' => array( 'code' => $code ) )
);
if ( is_wp_error( $response ) ) {
$this->log_error( __METHOD__ . '(): Exchange of code for tokens returned error: ' . $response->get_error_message() );
return false;
}
// Save new access token.
$response_body = wp_remote_retrieve_body( $response );
$response_data = json_decode( $response_body, true );
if ( ! $response_data ) {
$this->log_error( __METHOD__ . '(): Request to exchange code for tokens returned no data.' );
return false;
}
if ( ! rgempty( 'error', $response_data ) ) {
$this->log_error( __METHOD__ . '(): Request to exchange code for tokens returned error: ' . $response_data['error_description'] );
return false;
}
// If access and refresh token are provided, store in settings.
if ( ! rgempty( 'access_token', $response_data ) && ! rgempty( 'refresh_token', $response_data ) ) {
$this->update_auth_data( $response_data );
return true;
}
// Gravity API returned no tokens.
$this->log_error( __METHOD__ . '(): Request to exchange code for tokens returned no tokens.' );
return false;
}
/**
* Updates the stored authentication data with a new set of tokens
*
* @since 4.0
*
* @param array $new_auth_data The new tokens.
*
* @return array The updated auth data.
*/
public function update_auth_data( $new_auth_data, $is_refresh = false ) {
$settings = $this->get_plugin_settings();
$auth_data = array(
'access_token' => rgar( $new_auth_data, 'access_token' ),
'refresh_token' => rgar( $new_auth_data, 'refresh_token' ),
'expires_in' => rgar( $new_auth_data, 'expires_in' ),
'time_created' => time(),
);
$settings['auth_token'] = $auth_data;
// aweber_state was for one time use only.
unset( $settings['aweber_state'] );
// Set the API authentication version.
if ( ! $is_refresh ) {
$settings['reauth_version'] = self::LAST_REAUTHENTICATION_VERSION;
}
// Save plugin settings.
$this->update_plugin_settings( $settings );
return $auth_data;
}
/**
* Revoke refresh token and remove tokens from Settings. Then send JSON error object or { 'success' => true } .
*
* @since 4.0
*/
public function ajax_deauthorize() {
check_ajax_referer( 'gf_aweber_ajax', 'nonce' );
if ( ! GFCommon::current_user_can_any( $this->_capabilities_settings_page ) ) {
wp_send_json_error( array( 'message' => esc_html__( 'Access denied.', 'gravityformsaweber' ) ) );
}
// If API not available return empty array.
if ( ! $this->initialize_api() ) {
$this->log_error( __METHOD__ . '(): Unable to get methods because API is not initialized.' );
wp_send_json_error( array( 'message' => esc_html__( 'Unable to deauthorize because API could not be initialized.', 'gravityformsaweber' ) ) );
}
$result = $this->api->revoke_refresh_token();
if ( is_wp_error( $result ) ) {
$this->log_error( __METHOD__ . '(): Unable to revoke refresh token; ' . $result->get_error_message() );
wp_send_json_error( array( 'message' => $result->get_error_message() ) );
}
// Call parent method to prevent adding back of tokens.
parent::update_plugin_settings( array() );
GFCache::delete( 'aweber_accounts' );
wp_send_json_success();
}
/**
* Returns the AWeber app id to be used when authorizing the add-on with AWeber.
*
* @since 2.7.1
*
* @return string
*/
public function get_app_id() {
/**
* Allows a custom AWeber app id to be defined for use when authorizing the add-on with AWeber.
*
* @since 2.7.1
*
* @param string $app_id The AWeber app id.
*/
$app_id = apply_filters( 'gform_aweber_app_id', '2ad0d7d5' );
return $app_id;
}
/**
* Migrate the plugin settings.
*
* @since Unknown
* @access public
*
* @param array $settings Plugin settings.
*
* @uses GFAddOn::get_plugin_settings()
* @uses GFAWeber::get_aweber_tokens()
*/
public function update_plugin_settings( $settings ) {
if ( rgblank( rgar( $settings, 'authorizationCode' ) ) ) {
parent::update_plugin_settings( $settings );
return;
}
$saved_settings = $this->get_plugin_settings();
$requires_tokens = empty( $saved_settings['access_token'] ) || $saved_settings['authorizationCode'] != $settings['authorizationCode'];
if ( $requires_tokens ) {
$aweber_token = $this->get_aweber_tokens( $settings['authorizationCode'] );
$settings['access_token'] = $aweber_token['access_token'];
$settings['access_token_secret'] = $aweber_token['access_token_secret'];
} else {
$settings['access_token'] = $saved_settings['access_token'];
$settings['access_token_secret'] = $saved_settings['access_token_secret'];
}
parent::update_plugin_settings( $settings );
}
// # FEED SETTINGS -------------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the feed edit page.
*
* @since Unknown
* @access public
*
* @uses GFAWeber::create_list_field_map()
* @uses GFAWeber::get_aweber_accounts()
* @uses GFAWeber::is_accounts_hidden()
*
* @return array
*/
public function feed_settings_fields() {
return array(
array(
'title' => esc_html__( 'AWeber Feed', 'gravityformsaweber' ),
'description' => '',
'fields' => array(
array(
'name' => 'feedName',
'label' => esc_html__( 'Name', 'gravityformsaweber' ),
'type' => 'text',
'required' => true,
'class' => 'medium',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Name', 'gravityformsaweber' ),
esc_html__( 'Enter a feed name to uniquely identify this setup.', 'gravityformsaweber' )
),
),
array(
'name' => 'account',
'label' => esc_html__( 'Account', 'gravityformsaweber' ),
'type' => 'select',
'onchange' => 'jQuery(this).parents("form").submit();',
'hidden' => $this->is_accounts_hidden(),
'choices' => $this->get_aweber_accounts(),
'default_value' => $this->get_default_account(),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Account', 'gravityformsaweber' ),
esc_html__( 'Select the AWeber account you would like to add your contacts to.', 'gravityformsaweber' )
),
),
array(
'name' => 'contactList',
'label' => esc_html__( 'Contact List', 'gravityformsaweber' ),
'type' => 'select',
'choices' => $this->get_lists_as_choices(),
'onchange' => 'jQuery(this).parents("form").submit();',
'dependency' => array( $this, 'has_selected_account' ),
'no_choices' => esc_html__( 'Unable to get lists.', 'gravityformsaweber' ),
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Contact List', 'gravityformsaweber' ),
esc_html__( 'Select the AWeber list you would like to add your contacts to.', 'gravityformsaweber' )
),
),
array(
'name' => 'listFields',
'label' => esc_html__( 'Map Fields', 'gravityformsaweber' ),
'type' => 'field_map',
'dependency' => 'contactList',
'field_map' => $this->create_list_field_map(),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Map Fields', 'gravityformsaweber' ),
esc_html__( 'Associate your AWeber fields to the appropriate Gravity Form fields by selecting the appropriate form field from the list.', 'gravityformsaweber' )
),
),
array(
'name' => 'tags',
'type' => 'text',
'dependency' => 'contactList',
'class' => 'medium merge-tag-support mt-position-right mt-hide_all_fields',
'label' => esc_html__( 'Tags', 'gravityformsaweber' ),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Tags', 'gravityformsaweber' ),
esc_html__( 'Associate tags to your AWeber subscribers with a comma separated list. (e.g. new lead, Gravity Forms, web source)', 'gravityformsaweber' )
),
),
array(
'name' => 'optin',
'label' => esc_html__( 'Conditional Logic', 'gravityformsaweber' ),
'type' => 'feed_condition',
'dependency' => 'contactList',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Conditional Logic', 'gravityformsaweber' ),
esc_html__( 'When conditional logic is enabled, form submissions will only be exported to AWeber when the condition is met. When disabled all form submissions will be exported.', 'gravityformsaweber' )
),
),
),
),
);
}
/**
* Check if the account setting should be displayed.
*
* @since Unknown
* @access public
*
* @uses GFAddOn::get_setting()
* @uses GFAWeber::get_default_account()
* @uses GFAWeber::has_multiple_accounts()
* @uses GFAWeber::is_valid_account_id()
*
* @return bool
*/
public function is_accounts_hidden() {
// Get account ID.
$account_id = $this->get_setting( 'account', $this->get_default_account() );
if ( ( ! empty( $account_id ) && ! $this->is_valid_account_id( $account_id ) ) || $this->has_multiple_accounts() ) {
return false;
}
return true;
}
/**
* Has a choice been selected for the account setting?
*
* @since Unknown