vault = $vault; } /** * Return a copy of the client that uses the given key. */ public function with_key( string $key ): Client { $clone = clone $this; $clone->key_override = trim( $key ); return $clone; } public function account_summary(): array { return $this->request( 'GET', '/v2/account/summary' ); } /** * @return array code => name */ public function supported_languages(): array { $cached = get_transient( 'translateplus_languages' ); if ( is_array( $cached ) && $cached ) { return $cached; } $res = $this->request( 'GET', '/v2/supported-languages' ); $list = array(); foreach ( (array) ( $res['supported_languages'] ?? array() ) as $name => $code ) { if ( 'auto' !== $code ) { $list[ (string) $code ] = (string) $name; } } asort( $list ); set_transient( 'translateplus_languages', $list, DAY_IN_SECONDS ); return $list; } public function translate( string $text, string $source, string $target ): string { $res = $this->request( 'POST', '/v2/translate', compact( 'text', 'source', 'target' ) ); return (string) ( $res['translations']['translation'] ?? '' ); } /** * Translate many short texts. Splits into API-sized batches and keeps order. * * @param string[] $texts Input texts. * @return array Translations in the same order; null on per-item failure. */ public function translate_batch( array $texts, string $source, string $target ): array { $texts = array_values( $texts ); $out = array(); foreach ( array_chunk( $texts, self::MAX_BATCH ) as $chunk ) { $res = $this->request( 'POST', '/v2/translate/batch', array( 'texts' => $chunk, 'source' => $source, 'target' => $target ) ); $items = (array) ( $res['translations'] ?? array() ); foreach ( $chunk as $i => $unused ) { $item = $items[ $i ] ?? null; $out[] = ( is_array( $item ) && ( $item['success'] ?? true ) && isset( $item['translation'] ) ) ? (string) $item['translation'] : null; } } return $out; } public function translate_html( string $html, string $source, string $target ): string { $res = $this->request( 'POST', '/v2/translate/html', compact( 'html', 'source', 'target' ) ); return (string) ( $res['html'] ?? '' ); } /** * @return array{subject:string, html_body:string} */ public function translate_email( string $subject, string $email_body, string $source, string $target ): array { $res = $this->request( 'POST', '/v2/translate/email', compact( 'subject', 'email_body', 'source', 'target' ) ); return array( 'subject' => (string) ( $res['subject'] ?? $subject ), 'html_body' => (string) ( $res['html_body'] ?? $email_body ), ); } public function detect_language( string $text ): string { $res = $this->request( 'POST', '/v2/language_detect', array( 'text' => mb_substr( $text, 0, 1000 ) ) ); return (string) ( $res['language_detection']['language'] ?? '' ); } /** * Start an async i18n file job (PO/JSON/YAML). * * @param string[] $targets Target language codes. */ public function create_i18n_job( string $file_path, string $filename, string $source, array $targets, string $webhook_url = '' ): array { $boundary = wp_generate_password( 24, false ); $fields = array( 'source_language' => $source, 'target_languages' => implode( ',', $targets ), ); if ( $webhook_url ) { $fields['webhook_url'] = $webhook_url; } $body = ''; foreach ( $fields as $name => $value ) { $body .= "--{$boundary}\r\nContent-Disposition: form-data; name=\"{$name}\"\r\n\r\n{$value}\r\n"; } $body .= "--{$boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" . basename( $filename ) . "\"\r\nContent-Type: application/octet-stream\r\n\r\n"; $body .= file_get_contents( $file_path ) . "\r\n--{$boundary}--\r\n"; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents return $this->request( 'POST', '/v2/translate/i18n', $body, 'multipart/form-data; boundary=' . $boundary ); } public function i18n_job_status( string $job_id ): array { return $this->request( 'GET', '/v2/translate/i18n/jobs/' . rawurlencode( $job_id ) . '/status' ); } public function i18n_job_download( string $job_id, string $lang ): string { return $this->request( 'GET', '/v2/translate/i18n/jobs/' . rawurlencode( $job_id ) . '/download?language_code=' . rawurlencode( $lang ), null, null, true ); } /** * Perform a request with retries on transient failures. * * @param array|string|null $body JSON-able array, raw string body, or null. * @return array|string Decoded JSON, or raw body when $raw is true. * @throws ApiException On failure. */ protected function request( string $method, string $path, $body = null, ?string $content_type = null, bool $raw = false ) { $key = null !== $this->key_override ? $this->key_override : $this->vault->get(); if ( '' === $key ) { throw ApiException::no_key(); } $args = array( 'method' => $method, 'timeout' => 60, 'headers' => array( 'X-API-KEY' => $key, 'Accept' => 'application/json', 'User-Agent' => 'TranslatePlus-WordPress/' . TRANSLATEPLUS_VERSION . '; ' . home_url(), ), ); if ( null !== $body ) { $args['headers']['Content-Type'] = $content_type ?: 'application/json'; $args['body'] = is_array( $body ) ? wp_json_encode( $body ) : $body; } /** * Filter request args before they are sent to TranslatePlus. * * @param array $args HTTP args. * @param string $path API path. */ $args = apply_filters( 'translateplus_api_request_args', $args, $path ); $url = apply_filters( 'translateplus_api_base', TRANSLATEPLUS_API_BASE ) . $path; $attempts = (int) apply_filters( 'translateplus_api_retries', 3 ); $last = null; for ( $i = 0; $i < max( 1, $attempts ); $i++ ) { if ( $i > 0 ) { usleep( (int) ( ( 2 ** $i ) * 250000 + wp_rand( 0, 250000 ) ) ); // Exponential backoff with jitter. } $response = wp_remote_request( $url, $args ); if ( is_wp_error( $response ) ) { $last = new ApiException( ApiException::NETWORK, $response->get_error_message() ); continue; } $status = (int) wp_remote_retrieve_response_code( $response ); $text = (string) wp_remote_retrieve_body( $response ); if ( $status >= 200 && $status < 300 ) { if ( $raw ) { return $text; } $json = json_decode( $text, true ); if ( ! is_array( $json ) ) { throw ApiException::malformed( (int) $status ); } return $json; } $last = ApiException::from_status( $status, $text ); if ( ! $last->is_retryable() ) { break; } } throw $last; } }