Skip to content

Commit 4124d36

Browse files
committed
Update API class to use access token if it is available.
1 parent cec3d88 commit 4124d36

File tree

4 files changed

+84
-25
lines changed

4 files changed

+84
-25
lines changed

includes/class-mailchimp-admin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function finish_oauth_process() {
124124
if ( $result && ! empty( $result['access_token'] ) && ! empty( $result['data_center'] ) ) {
125125
// Clean up the old data.
126126
delete_option( 'mailchimp_sf_access_token' );
127-
delete_option( 'mailchimp_sf_data_center' );
127+
delete_option( 'mc_datacenter' );
128128

129129
delete_site_transient( 'mailchimp_sf_oauth_secret' );
130130

@@ -172,7 +172,7 @@ public function verify_and_save_oauth_token( $access_token, $data_center ) {
172172
$access_token = $data_encryption->encrypt( $access_token );
173173

174174
update_option( 'mailchimp_sf_access_token', $access_token );
175-
update_option( 'mailchimp_sf_data_center', $data_center );
175+
update_option( 'mc_datacenter', $data_center );
176176
update_option( 'mc_user', $user );
177177
return true;
178178

lib/mailchimp/mailchimp.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
*/
1111
class MailChimp_API {
1212

13+
/**
14+
* The access token.
15+
*
16+
* @var string
17+
*/
18+
public $access_token;
19+
1320
/**
1421
* The API key
1522
*
@@ -34,26 +41,34 @@ class MailChimp_API {
3441
/**
3542
* Initialize the class
3643
*
37-
* @param string $api_key The API key.
38-
* @throws Exception If no api key is set
44+
* @param string $access_token Access token or API key. If data center is not provided, we'll assume that this is an API key.
45+
* @param string $data_center The data center. If not provided, we'll assume the data center is in the API key itself.
46+
* @throws Exception If no api key or access token is set
3947
*/
40-
public function __construct( $api_key ) {
41-
$api_key = trim( $api_key );
42-
if ( ! $api_key ) {
48+
public function __construct( $access_token, $data_center = '' ) {
49+
$access_token = trim( $access_token );
50+
if ( ! $access_token ) {
4351
throw new Exception(
4452
esc_html(
4553
sprintf(
46-
/* translators: %s: api key */
47-
__( 'Invalid API Key: %s', 'mailchimp' ),
48-
$api_key
54+
/* translators: %s: access token */
55+
__( 'Invalid Access Token or API key: %s', 'mailchimp' ),
56+
$access_token
4957
)
5058
)
5159
);
5260
}
5361

54-
$this->key = $api_key;
55-
$dc = explode( '-', $api_key );
56-
$this->datacenter = empty( $dc[1] ) ? 'us1' : $dc[1];
62+
// No data center provided, so we'll assume it's in the API key.
63+
if ( ! $data_center ) {
64+
$this->key = $access_token;
65+
$dc = explode( '-', $access_token );
66+
$this->datacenter = empty( $dc[1] ) ? 'us1' : $dc[1];
67+
} else {
68+
$this->access_token = $access_token;
69+
$this->datacenter = $data_center;
70+
}
71+
5772
$this->api_url = 'https://' . $this->datacenter . '.api.mailchimp.com/3.0/';
5873
}
5974

@@ -84,12 +99,20 @@ public function get( $endpoint, $count = 10, $fields = array() ) {
8499
$url .= "?{$query_params}";
85100
}
86101

102+
$headers = array();
103+
// If we have an access token, use that, otherwise use the API key.
104+
if ( $this->access_token ) {
105+
$headers['Authorization'] = 'Bearer ' . $this->access_token;
106+
} else {
107+
$headers['Authorization'] = 'apikey ' . $this->key;
108+
}
109+
87110
$args = array(
88111
'timeout' => 5,
89112
'redirection' => 5,
90113
'httpversion' => '1.1',
91114
'user-agent' => 'Mailchimp WordPress Plugin/' . get_bloginfo( 'url' ),
92-
'headers' => array( 'Authorization' => 'apikey ' . $this->key ),
115+
'headers' => $headers,
93116
);
94117

95118
$request = wp_remote_get( $url, $args );
@@ -120,13 +143,21 @@ public function get( $endpoint, $count = 10, $fields = array() ) {
120143
public function post( $endpoint, $body, $method = 'POST' ) {
121144
$url = $this->api_url . $endpoint;
122145

146+
$headers = array();
147+
// If we have an access token, use that, otherwise use the API key.
148+
if ( $this->access_token ) {
149+
$headers['Authorization'] = 'Bearer ' . $this->access_token;
150+
} else {
151+
$headers['Authorization'] = 'apikey ' . $this->key;
152+
}
153+
123154
$args = array(
124155
'method' => $method,
125156
'timeout' => 5,
126157
'redirection' => 5,
127158
'httpversion' => '1.1',
128159
'user-agent' => 'Mailchimp WordPress Plugin/' . get_bloginfo( 'url' ),
129-
'headers' => array( 'Authorization' => 'apikey ' . $this->key ),
160+
'headers' => $headers,
130161
'body' => wp_json_encode( $body ),
131162
);
132163
$request = wp_remote_post( $url, $args );

mailchimp.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
require_once $path . 'lib/mailchimp/mailchimp.php';
5050
}
5151

52+
// Encryption utility class.
53+
require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-data-encryption.php';
54+
5255
// includes the widget code so it can be easily called either normally or via ajax
5356
require_once 'mailchimp_widget.php';
5457

@@ -58,8 +61,8 @@
5861
// Upgrade routines.
5962
require_once 'mailchimp_upgrade.php';
6063

61-
// Init Admin functions
62-
require_once plugin_dir_path( __FILE__ ) . 'includes/mailchimp-admin.php';
64+
// Init Admin functions.
65+
require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-admin.php';
6366
$admin = new MailChimp_Admin();
6467
$admin->init();
6568

@@ -149,18 +152,22 @@ function mailchimp_admin_page_scripts( $hook_suffix ) {
149152
return;
150153
}
151154

152-
wp_enqueue_style( 'mailchimp_sf_admin_css', MCSF_URL . 'css/admin.css', array(), true );
155+
wp_enqueue_style( 'mailchimp_sf_admin_css', MCSF_URL . 'css/admin.css', array( 'wp-jquery-ui-dialog' ), true );
153156
wp_enqueue_script( 'showMe', MCSF_URL . 'js/hidecss.js', array( 'jquery' ), MCSF_VER, true );
154-
wp_enqueue_script( 'mailchimp_sf_admin', MCSF_URL . 'js/admin.js', array( 'jquery' ), MCSF_VER, true );
157+
wp_enqueue_script( 'mailchimp_sf_admin', MCSF_URL . 'js/admin.js', array( 'jquery', 'jquery-ui-dialog' ), MCSF_VER, true );
155158

156159
wp_localize_script(
157160
'mailchimp_sf_admin',
158161
'mailchimp_sf_admin_params',
159162
array(
160-
'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
161-
'oauth_start_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_start_nonce' ),
162-
'oauth_finish_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_finish_nonce' ),
163-
'oauth_window_name' => esc_html__( 'Mailchimp For WordPress OAuth', 'mailchimp' ),
163+
'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
164+
'oauth_start_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_start_nonce' ),
165+
'oauth_finish_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_finish_nonce' ),
166+
'oauth_window_name' => esc_html__( 'Mailchimp For WordPress OAuth', 'mailchimp' ),
167+
'generic_error' => esc_html__( 'An error occurred. Please try again.', 'mailchimp' ),
168+
'modal_title' => esc_html__( 'Login Popup is blocked!', 'mailchimp' ),
169+
'modal_button_try_again' => esc_html__( 'Try again', 'mailchimp' ),
170+
'modal_button_cancel' => esc_html__( 'No, cancel!', 'mailchimp' ),
164171
)
165172
);
166173
}
@@ -263,7 +270,7 @@ function mailchimp_sf_request_handler() {
263270
}
264271

265272
// erase auth information
266-
$options = array( 'mc_api_key', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key' );
273+
$options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key' );
267274
mailchimp_sf_delete_options( $options );
268275
break;
269276
case 'change_form_settings':
@@ -398,6 +405,14 @@ function mailchimp_sf_auth_nonce_salt() {
398405
* @return MailChimp_API | false
399406
*/
400407
function mailchimp_sf_get_api() {
408+
// Check for the access token first.
409+
$access_token = mailchimp_sf_get_access_token();
410+
$data_center = get_option( 'mc_datacenter' );
411+
if ( ! empty( $access_token ) && ! empty( $data_center ) ) {
412+
return new MailChimp_API( $access_token, $data_center );
413+
}
414+
415+
// Check for the API key if the access token is not available.
401416
$key = get_option( 'mc_api_key' );
402417
if ( $key ) {
403418
return new MailChimp_API( $key );
@@ -1410,3 +1425,16 @@ function mailchimp_sf_create_nonce( $action = -1 ) {
14101425

14111426
return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
14121427
}
1428+
1429+
/**
1430+
* Get Mailchimp Access Token.
1431+
*
1432+
* @since x.x.x
1433+
* @return string|bool
1434+
*/
1435+
function mailchimp_sf_get_access_token() {
1436+
$access_token = get_option( 'mailchimp_sf_access_token' );
1437+
$data_encryption = new MailChimp_Data_Encryption();
1438+
1439+
return $data_encryption->decrypt( $access_token );
1440+
}

views/setup_page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232

3333
// If we don't have an API Key, do a login form
34-
if ( ! $user || ! get_option( 'mc_api_key' ) ) {
34+
if ( ! $user || ( ! get_option( 'mc_api_key' ) && ! mailchimp_sf_get_access_token() ) ) {
3535
?>
3636
<div>
3737
<h3 class="mc-h2"><?php esc_html_e( 'Log In', 'mailchimp' ); ?></h3>

0 commit comments

Comments
 (0)