45, 'redirection' => 3, 'headers' => array( 'Content-Type' => 'application/json' ), ); $args = wp_parse_args( $args, $defaults ); $response = wp_remote_request( $url, $args ); if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); if ( $code < 200 || $code >= 300 ) { return new WP_Error( 'aisc_http_' . $code, sprintf( /* translators: 1: HTTP status code, 2: error message from the API. */ __( 'HTTP %1$d — %2$s', 'ai-slop-checker' ), $code, self::error_message( $body, $code ) ), array( 'status' => $code ) ); } $decoded = json_decode( $body, true ); if ( null === $decoded && JSON_ERROR_NONE !== json_last_error() ) { return new WP_Error( 'aisc_bad_json', __( 'API returned a response that was not valid JSON.', 'ai-slop-checker' ) ); } if ( $cache_key ) { set_transient( $cache_key, $decoded, self::CACHE_TTL ); } return $decoded; } /** * Build a namespaced cache key. * * @param string $prefix Client prefix. * @param mixed $seed Anything hashable. * @return string */ public static function cache_key( $prefix, $seed ) { return 'aisc_' . $prefix . '_' . md5( maybe_serialize( $seed ) . AISC_VERSION ); } /** * Turn an error response into something an editor can act on. * * Both providers return structured JSON errors, so the useful sentence is * pulled out rather than dumping the raw body into the admin panel. A plain * hint is appended for the statuses people actually hit. * * @param string $body Response body. * @param int $code HTTP status code. * @return string */ private static function error_message( $body, $code ) { $decoded = json_decode( (string) $body, true ); $message = ''; if ( is_array( $decoded ) ) { // Gemini: {"error":{"message":"..."}}. Perplexity varies. foreach ( array( 'error', 'detail', 'message' ) as $key ) { if ( empty( $decoded[ $key ] ) ) { continue; } $candidate = $decoded[ $key ]; if ( is_array( $candidate ) ) { $candidate = $candidate['message'] ?? ( $candidate['detail'] ?? '' ); } if ( is_string( $candidate ) && '' !== trim( $candidate ) ) { $message = trim( $candidate ); break; } } } if ( '' === $message ) { $message = self::excerpt( $body ); } $hint = self::hint( $message, $code ); if ( $hint ) { $message .= ' (' . $hint . ')'; } return $message; } /** * Plain-language hint for the failures that actually come up. * * The message is inspected before the status code, because the codes are not * reliable on their own: Gemini answers a bad API key with 400, so keying the * hint off the status alone would tell someone to go and check their model * name while the real problem is the key. * * @param string $message Extracted provider message. * @param int $code HTTP status code. * @return string */ private static function hint( $message, $code ) { $patterns = array( '/api\s*key|unauthenri?zed|unauthorized|authentication|credential|permission denied/i' => __( 'the API key looks wrong, or lacks access to this API', 'ai-slop-checker' ), '/quota|rate limit|exhausted|too many requests|billing/i' => __( 'rate limit or quota reached — try again shortly', 'ai-slop-checker' ), '/\bmodel\b.*(not found|does not exist|must be one of|unsupported|invalid)|(not found|unsupported).*\bmodel\b/i' => __( 'check the model name in settings', 'ai-slop-checker' ), '/tool|google_search|grounding/i' => __( 'this model may not support Search grounding — try another', 'ai-slop-checker' ), ); foreach ( $patterns as $regex => $hint ) { if ( preg_match( $regex, $message ) ) { return $hint; } } switch ( (int) $code ) { case 400: return __( 'check the model name and API key in settings', 'ai-slop-checker' ); case 401: case 403: return __( 'the API key looks wrong, or lacks access to this API', 'ai-slop-checker' ); case 404: return __( 'that model does not exist for this API version', 'ai-slop-checker' ); case 429: return __( 'rate limit or quota reached — try again shortly', 'ai-slop-checker' ); case 500: case 502: case 503: return __( 'the provider is having trouble — try again shortly', 'ai-slop-checker' ); default: return ''; } } /** * Short excerpt of a response body for error messages. * * @param string $body Response body. * @return string */ private static function excerpt( $body ) { $body = trim( wp_strip_all_tags( (string) $body ) ); if ( strlen( $body ) > 300 ) { $body = substr( $body, 0, 300 ) . '…'; } return $body; } }