Skip to content

Commit 896076b

Browse files
committed
Added admin notice for the invalid/revoked token.
1 parent 54d1725 commit 896076b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

includes/class-mailchimp-admin.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class MailChimp_Admin {
2929
* Initialize the class
3030
*/
3131
public function init() {
32+
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
3233
add_action( 'wp_ajax_mailchimp_sf_oauth_start', array( $this, 'start_oauth_process' ) );
3334
add_action( 'wp_ajax_mailchimp_sf_oauth_finish', array( $this, 'finish_oauth_process' ) );
3435
}
@@ -124,6 +125,7 @@ public function finish_oauth_process() {
124125
if ( $result && ! empty( $result['access_token'] ) && ! empty( $result['data_center'] ) ) {
125126
// Clean up the old data.
126127
delete_option( 'mailchimp_sf_access_token' );
128+
delete_option( 'mailchimp_sf_auth_error' );
127129
delete_option( 'mc_datacenter' );
128130

129131
delete_site_transient( 'mailchimp_sf_oauth_secret' );
@@ -181,4 +183,31 @@ public function verify_and_save_oauth_token( $access_token, $data_center ) {
181183
return new WP_Error( 'mailchimp-sf-invalid-role', $msg );
182184
}
183185
}
186+
187+
/**
188+
* Display admin notices.
189+
*
190+
* @since x.x.x
191+
*/
192+
public function admin_notices() {
193+
// display a notice if the access token is invalid/revoked.
194+
if ( get_option( 'mailchimp_sf_auth_error', false ) && current_user_can( 'manage_options' ) && get_option( 'mailchimp_sf_access_token', '' ) ) {
195+
?>
196+
<div class="notice notice-warning is-dismissible">
197+
<p>
198+
<?php
199+
$message = sprintf(
200+
/* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */
201+
__( 'Heads up! There may be a problem with your connection to Mailchimp. Please %1$sre-connect%2$s your Mailchimp account to fix the issue.', 'mailchimp' ),
202+
'<a href="' . esc_url( admin_url( 'admin.php?page=mailchimp_sf_options' ) ) . '">',
203+
'</a>'
204+
);
205+
206+
echo wp_kses( $message, array( 'a' => array( 'href' => array() ) ) );
207+
?>
208+
</p>
209+
</div>
210+
<?php
211+
}
212+
}
184213
}

lib/mailchimp/mailchimp.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public function get( $endpoint, $count = 10, $fields = array() ) {
124124
if ( is_array( $request ) && 200 === $request['response']['code'] ) {
125125
return json_decode( $request['body'], true );
126126
} elseif ( is_array( $request ) && $request['response']['code'] ) {
127+
// Check if Access Token is invalid/revoked.
128+
if ( in_array( $request['response']['code'], array( 401, 403 ), true ) ) {
129+
update_option( 'mailchimp_sf_auth_error', true );
130+
}
131+
127132
$error = json_decode( $request['body'], true );
128133
$error = new WP_Error( 'mailchimp-get-error', $error['detail'] );
129134
return $error;
@@ -169,6 +174,11 @@ public function post( $endpoint, $body, $method = 'POST' ) {
169174
return new WP_Error( 'mc-subscribe-error', $request->get_error_message() );
170175
}
171176

177+
// Check if Access Token is invalid/revoked.
178+
if ( is_array( $request ) && in_array( $request['response']['code'], array( 401, 403 ), true ) ) {
179+
update_option( 'mailchimp_sf_auth_error', true );
180+
}
181+
172182
$body = json_decode( $request['body'], true );
173183
$merges = get_option( 'mc_merge_vars' );
174184
$field_name = '';

mailchimp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function mailchimp_sf_request_handler() {
252252
}
253253

254254
// erase auth information
255-
$options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key' );
255+
$options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mailchimp_sf_auth_error', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key' );
256256
mailchimp_sf_delete_options( $options );
257257
break;
258258
case 'change_form_settings':

0 commit comments

Comments
 (0)