api_key = sanitize_text_field($api_key); $this->base_url = 'https://app.alttextlab.com/api/'; } /** * Fetches user account information from the API service. * */ public function get_account() { $api_version = 'v1'; $response = $this->request('GET', $api_version, '/subscriptions/info'); if (!is_array($response) || is_wp_error($response)) { return false; } /** * Verify if the response status indicates failure (any non-2xx HTTP code) */ if (substr(wp_remote_retrieve_response_code($response), 0, 1) != '2') { return false; } $response = json_decode(wp_remote_retrieve_body($response), true); $account = array( 'plan' => sanitize_text_field($response['planName']), 'isActive' => $response['isActive'], 'credits' => intval($response['credits']), 'nextReceiving' => sanitize_text_field($response['nextReceiving']), 'nextReceivingAt' => sanitize_text_field($response['nextReceivingAt']) ); return $account; } /** * Produces alternative text descriptions for the given image. * */ public function generate_alt_text($attachment_id, $attachment_url, $api_options, &$response_code) { $api_version = 'v1'; $body = $this->build_body($attachment_id, $attachment_url, $api_options); $response = $this->request('POST', $api_version, '/alt-text/generate', $body, intval(get_option('alttextlab_timeout', 20))); $response_code = wp_remote_retrieve_response_code($response); $attachment_edit_url = empty($attachment_id) ? $attachment_url : get_edit_post_link($attachment_id); if (!is_array($response) || is_wp_error($response)) { error_log(print_r($response, true)); $this->log_api_error($attachment_id, $attachment_edit_url, $response_code, 'Unknown error.', 'alttextlab_try_later_notice', MINUTE_IN_SECONDS); return false; } $response_body = json_decode(wp_remote_retrieve_body($response), true); if ($response_code == '401') { error_log(print_r($response, true)); $error_message = 'API_KEY_IS_INVALID'; $this->log_api_error($attachment_id, $attachment_edit_url, $response_code, $error_message, 'alttextlab_api_key_invalid_notice', MINUTE_IN_SECONDS); return 'API_KEY_IS_INVALID'; } else if ($response_code == '403') { $error_message = $response_body['key'] ? : 'Something went wrong!'; error_log(print_r($response, true)); if ($error_message === 'NOT_ENOUGH_FUNDS') { $this->log_api_error($attachment_id, $attachment_edit_url, $response_code, $error_message, 'alttextlab_insufficient_credits', MONTH_IN_SECONDS); return 'NOT_ENOUGH_FUNDS'; } $this->log_api_error($attachment_id, $attachment_edit_url, $response_code, $error_message, 'alttextlab_try_later_notice', MINUTE_IN_SECONDS); return false; } else if($response_code == '429'){ $error_message = $response_body['key']; error_log(print_r($response, true)); if ($error_message === 'MONTHLY_LIMIT_EXCEEDED') { $this->log_api_error($attachment_id, $attachment_edit_url, $response_code, $error_message, 'alttextlab_monthly_limit_exceeded', MONTH_IN_SECONDS); return 'MONTHLY_LIMIT_EXCEEDED'; } $this->log_api_error($attachment_id, $attachment_edit_url, $response_code, $error_message, 'alttextlab_try_later_notice', MINUTE_IN_SECONDS); return false; } elseif (substr($response_code, 0, 1) != '2') { error_log(print_r($response, true)); $this->log_api_error($attachment_id, $attachment_edit_url, $response_code, 'API error.', 'alttextlab_try_later_notice', MINUTE_IN_SECONDS); return false; } return $response_body; } /** * Builds the request payload for the alt text generation API. * */ private function build_body($attachment_id, $attachment_url, $api_options) { $body = []; if (empty($attachment_id) || get_option('alttextlab_public') === 'yes') { // When dealing with public websites, create ALT text by sending the image URL to the API $body = array( 'imageUrl' => $attachment_url ); } else { // In the case of private websites, generate ALT text by base64 encoding and transmitting the image file directly to the API $body = array( 'imageRaw' => base64_encode(file_get_contents(get_attached_file($attachment_id))) ); } $body['source'] = 'wordpress'; return array_merge($body, $api_options); } /** * Sends an HTTP request to the AltTextLab API. * */ private function request($method, $version, $endpoint, $body = null, $timeout = 20) { $args = [ 'headers' => [ 'Content-Type' => 'application/json', 'X-Api-Key' => $this->api_key, ], 'timeout' => $timeout, ]; if ($body) { $args['body'] = wp_json_encode($body); } $url = $this->base_url . $version . $endpoint; return $method === 'GET' ? wp_remote_get($url, $args) : wp_remote_post($url, $args); } private function log_api_error($id, $url, $code, $message, $transient_key, $transient_duration) { AltTextLab_Logger::log_error( sprintf('[%d] Image #%d: %s', (int)$code, esc_url($url), (int)$id, esc_html($message)) ); set_transient($transient_key, true, $transient_duration); } }