Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mailchimp): fatal error with invalid API keys #1690

Merged
merged 9 commits into from
Nov 11, 2024
5 changes: 5 additions & 0 deletions includes/class-newspack-newsletters-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ private static function render_oauth_authorization() {
private static function render_lists_table() {
$lists = Newspack_Newsletters_Subscription::get_lists();
if ( is_wp_error( $lists ) || empty( $lists ) ) {
?>
<div class="notice notice-error">
<p><?php echo esc_html( is_wp_error( $lists ) ? $lists->get_error_message() : __( 'No list data found.', 'newspack-newsletters' ) ); ?></p>
</div>
<?php
return;
}
?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public static function init() {
add_action( 'wp_ajax_' . self::AJAX_ACTION, [ __CLASS__, 'handle_dispatch_refresh' ] );
add_action( 'wp_ajax_nopriv_' . self::AJAX_ACTION, [ __CLASS__, 'handle_dispatch_refresh' ] );

// Invalidate all cached data if API key changes.
add_action( 'update_option_newspack_mailchimp_api_key', [ __CLASS__, 'invalidate_cache' ] );

add_action( self::CRON_HOOK, [ __CLASS__, 'handle_cron' ] );
add_filter( 'cron_schedules', [ __CLASS__, 'add_cron_interval' ] ); // phpcs:ignore

Expand Down Expand Up @@ -285,10 +288,14 @@ private static function update_cache( $list_id, $data ) {
/**
* Clears the cache errors for a given list
*
* @param string $list_id The List ID.
* @param string|null $list_id The List ID, or null for all lists.
* @return void
*/
private static function clear_errors( $list_id ) {
private static function clear_errors( $list_id = null ) {
if ( ! $list_id ) {
delete_option( self::ERRORS_OPTION );
return;
}
$errors = get_option( self::ERRORS_OPTION, [] );
if ( isset( $errors[ $list_id ] ) ) {
unset( $errors[ $list_id ] );
Expand All @@ -300,11 +307,22 @@ private static function clear_errors( $list_id ) {
/**
* Stores the last error for a given list, if the cache is older than self::SURFACE_ERRORS_AFTER
*
* @param string $list_id The List ID.
* @param string $error The error message.
* @param string|null $list_id The List ID, or null for all lists.
* @param string $error The error message.
*/
private static function maybe_add_error( $list_id, $error ) {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: handling error while fetching cache for list ' . $list_id );
private static function maybe_add_error( $list_id = null, $error = '' ) {
Newspack_Newsletters_Logger::log(
sprintf(
'Mailchimp cache: handling error while fetching cache for %s',
$list_id ? 'list ' . $list_id : 'all lists'
)
);
if ( ! $list_id ) {
$list_id = 'lists';
}
if ( ! $error ) {
$error = __( 'Unknown error', 'newspack_newsletters' );
}
$cache_date = get_option( self::get_cache_date_key( $list_id ) );
if ( $cache_date && ( time() - $cache_date ) > self::SURFACE_ERRORS_AFTER ) {
$errors = get_option( self::ERRORS_OPTION, [] );
Expand Down Expand Up @@ -518,6 +536,16 @@ private static function refresh_cached_data( $list_id ) {
}
}

/**
* Invalidate cached data by clearing the cache date key for all lists.
*/
public static function invalidate_cache() {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: Invalidating cached data' );
delete_option( self::get_cache_date_key() );
delete_option( self::get_lists_cache_key() );
self::clear_errors();
}

/**
* Handles the cron job and triggers the async requests to refresh the cache for all lists
*
Expand All @@ -529,6 +557,7 @@ public static function handle_cron() {
$lists = self::fetch_lists(); // Force a cache refresh.
} catch ( Exception $e ) {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: Error refreshing lists cache: ' . $e->getMessage() );
self::maybe_add_error( null, $e->getMessage() );
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ public function validate( $result, $preferred_error = null ) {
throw new Exception( esc_html__( 'A Mailchimp error has occurred.', 'newspack-newsletters' ) );
}
}
if ( ! empty( $result['status'] ) && in_array( $result['status'], [ 400, 404 ] ) ) {
if ( ! empty( $result['status'] ) && (int) $result['status'] >= 400 ) {
if ( $preferred_error && ! Newspack_Newsletters::debug_mode() ) {
if ( ! empty( $result['detail'] ) ) {
$preferred_error .= ' ' . $result['detail'];
Expand Down