Skip to content

Conversation

@n7studios
Copy link
Contributor

Summary

When an access token is either invalid, or expired and fails to refresh, the Kit Plugin currently retains the access token, refresh token and expiry date in the Plugin's settings.

This results in many API requests which 401 - the below from a single WordPress administration screen (34 requests):

before

As a result, the API has millions of 401 requests over a 24 hour period:

image-2025-11-07-EpQlwEmm@2x

Whilst caching strategies are already implemented in the Plugin, these do not apply if:

  • the access token expires, and
  • the underlying cached resources have reached expired

This PR resolves by using an updated version of the WordPress Libraries, listening for the hook when an access token is invalid or refreshing an expired token fails, deleting said tokens from the Plugin's settings. This prevents repetitive requests that will fail - the below from the same WordPress administration screen (1 request):

after

PR's already exist to:

  • automatically refresh an expired access token using WordPress' cron,
  • automatically refresh an expired access token when used in an API request,

It's unclear why some sites still end up in a situation where their access tokens expire and do not get refreshed. These individual cases will need to be examined after this update.

Testing

  • testAccessTokenDeletedWhenRefreshFails: Test that the access token, refresh token and expiry are deleted from the Plugin's settings when refreshing an expired access token fails
  • testAccessTokenDeletedWhenInvalid: Test that the access token, refresh token and expiry are deleted from the Plugin's settings when an invalid access token is used

Checklist

@github-actions
Copy link

WordPress Playground

🚀 Your PR has been built and is ready for testing in WordPress Playground!

Click here to test your changes in WordPress Playground

@n7studios n7studios requested review from a team, corydhmiller and noelherrick and removed request for a team November 11, 2025 11:57
@n7studios n7studios marked this pull request as ready for review November 11, 2025 11:57
* @param WP_Error $result Error result.
* @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
*/
public function maybe_delete_credentials( $result, $client_id ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the server returns a 5xx error? Would we just delete the credentials? What about a 429 rate limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe_delete_credentials is only called when the Kit WordPress Libraries fires specific hooks:

Code in this PR that checks for these hooks:
https://github.com/Kit/convertkit-wordpress/blob/delete-invalid-access-token-from-settings/includes/class-convertkit-settings.php#L52-L55

Other errors, such as a 429 or 5xx, won't result in tokens being deleted from the Plugin's settings. Libraries return a WP_Error, which the Plugin then handles accordingly.

Copy link
Contributor

@noelherrick noelherrick Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if we get a non-401 from the refresh itself, wouldn't that cause the tokens to be deleted?

Copy link
Contributor Author

@n7studios n7studios Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Removed convertkit_api_refresh_token_error hook, ensuring access token only deleted if the API class uses an invalid access token, not an expired one.

@github-actions
Copy link

WordPress Playground

🚀 Your PR has been built and is ready for testing in WordPress Playground!

Click here to test your changes in WordPress Playground

@n7studios n7studios merged commit ca61af3 into main Nov 18, 2025
200 of 215 checks passed
@n7studios n7studios deleted the delete-invalid-access-token-from-settings branch December 5, 2025 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants