['Authorization' => 'Bearer ' . $secret], 'redirection' => 0, 'timeout' => 4, ], ); $usage = null; if (!is_wp_error($response) && (int) wp_remote_retrieve_response_code($response) === 200) { $usage = self::parse(json_decode((string) wp_remote_retrieve_body($response), true)); } set_transient(self::TRANSIENT, ['siteKey' => $siteKey, 'usage' => $usage], self::CACHE_SEC); return $usage; } public static function forget(): void { delete_transient(self::TRANSIENT); } /** * @return array{billingUrl:string,creditsAvailable:int,creditsPercent:int,creditsUsed:float,draftsIncluded:int,draftsPercent:int,draftsUsed:int,planName:string,resetsAt:string,state:string}|null */ public static function parse(mixed $body): ?array { if (!is_array($body) || !isset($body['draftsUsed'], $body['draftsIncluded'], $body['creditsUsed'], $body['creditsAvailable'])) { return null; } $state = (string) ($body['state'] ?? 'ok'); return [ 'billingUrl' => esc_url_raw((string) ($body['billingUrl'] ?? '')), 'creditsAvailable' => (int) $body['creditsAvailable'], 'creditsPercent' => max(0, min(100, (int) ($body['creditsPercent'] ?? 0))), 'creditsUsed' => (float) $body['creditsUsed'], 'draftsIncluded' => (int) $body['draftsIncluded'], 'draftsPercent' => max(0, min(100, (int) ($body['draftsPercent'] ?? 0))), 'draftsUsed' => (int) $body['draftsUsed'], 'planName' => sanitize_text_field((string) ($body['planName'] ?? '')), 'resetsAt' => sanitize_text_field((string) ($body['resetsAt'] ?? '')), 'state' => in_array($state, ['near', 'ok', 'reached'], true) ? $state : 'ok', ]; } }