fix: add Cloudflare token to encrypted fields list (fixes 401 on image gen)

prautoblogger_cloudflare_ai_token was missing from the $encrypted array
in sanitize_field(), so the token was stored as plaintext. On read,
decrypt() failed on the non-encrypted value and returned empty string,
causing HTTP 401 on every Cloudflare Workers AI call.

Fix: add the field to the encrypted list so future saves encrypt it.
Also add legacy plaintext detection in get_api_token() and the validator
so the existing DB value works immediately and gets auto-encrypted on
first use.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-18 07:24:01 +07:00
parent 51b385b938
commit 49aee0b446
3 changed files with 25 additions and 6 deletions

View File

@@ -40,8 +40,14 @@ class PRAutoBlogger_Cloudflare_Image_Validator {
);
}
$encrypted = (string) get_option( 'prautoblogger_cloudflare_ai_token', '' );
$api_token = '' === $encrypted ? '' : (string) PRAutoBlogger_Encryption::decrypt( $encrypted );
$stored = (string) get_option( 'prautoblogger_cloudflare_ai_token', '' );
$api_token = '';
if ( '' !== $stored ) {
// Handle legacy plaintext tokens (saved before the field was in the encrypted list).
$api_token = PRAutoBlogger_Encryption::is_encrypted( $stored )
? (string) PRAutoBlogger_Encryption::decrypt( $stored )
: $stored;
}
if ( '' === $api_token ) {
return $this->err(
'token_empty',