diff --git a/includes/class-mailchimp-admin.php b/includes/class-mailchimp-admin.php index b405917..0a70f5d 100644 --- a/includes/class-mailchimp-admin.php +++ b/includes/class-mailchimp-admin.php @@ -306,8 +306,23 @@ public function verify_and_save_oauth_token( $access_token, $data_center ) { update_option( 'mailchimp_sf_access_token', $data_encryption->encrypt( $access_token ) ); update_option( 'mc_datacenter', sanitize_text_field( $data_center ) ); update_option( 'mc_user', $this->sanitize_data( $user ) ); - return true; + // Clear Mailchimp List ID if saved list is not available. + $lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id' ) ); + if ( ! is_wp_error( $lists ) ) { + $lists = $lists['lists'] ?? array(); + $saved_list_id = get_option( 'mc_list_id' ); + $list_ids = array_map( + function ( $ele ) { + return $ele['id']; + }, + $lists + ); + if ( ! in_array( $saved_list_id, $list_ids, true ) ) { + delete_option( 'mc_list_id' ); + } + } + return true; } else { $msg = esc_html__( 'API Key must belong to "Owner", "Admin", or "Manager."', 'mailchimp' ); return new WP_Error( 'mailchimp-sf-invalid-role', $msg ); diff --git a/mailchimp.php b/mailchimp.php index ddd0bee..12c7e60 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -217,7 +217,7 @@ function mailchimp_sf_request_handler() { } // erase auth information - $options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mailchimp_sf_auth_error', 'mailchimp_sf_waiting_for_login', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key', 'mc_list_id' ); + $options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mailchimp_sf_auth_error', 'mailchimp_sf_waiting_for_login', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key' ); mailchimp_sf_delete_options( $options ); break; case 'change_form_settings':