From cc4c225ce0069478075ac8640cb1862ccea4a69e Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Wed, 2 Oct 2024 10:52:58 +0530 Subject: [PATCH 1/2] Stop clearing the Mailchimp list ID upon logout to keep the Mailchimp settings persistent after logout. --- mailchimp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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': From de7605990996467e6e712efba5607326cfacf4ef Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Wed, 2 Oct 2024 11:27:21 +0530 Subject: [PATCH 2/2] Clear Mailchimp list ID on login if list is not available under account (ie. login with different user) --- includes/class-mailchimp-admin.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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 );