PluginProbe
Auto Alt Text / 2.5.2
Auto Alt Text v2.5.2
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
← All changes | src/App/AIProviders/Azure/AzureTranslator.php +52 -50 2.6.02.5.2 View file →
@@ -1,36 +1,24 @@
1 1 <?php
2 2
3 3 namespace AATXT\App\AIProviders\Azure;
4 4
5 +use AATXT\App\Admin\PluginOptions;
5 6 use AATXT\App\AIProviders\AITranslatorInterface;
6 -use AATXT\App\Configuration\AzureConfig;
7 7 use AATXT\App\Exceptions\Azure\AzureTranslateInstanceException;
8 -use AATXT\App\Infrastructure\Http\HttpClientInterface;
9 8
10 -/**
11 - * Azure Translator provider for translating text.
12 - *
13 - * This class uses dependency injection to receive HTTP client and configuration,
14 - * removing static dependencies on PluginOptions.
15 - */
16 9 class AzureTranslator implements AITranslatorInterface
17 10 {
18 - private HttpClientInterface $httpClient;
19 - private AzureConfig $config;
20 11
21 - /**
22 - * Constructor.
23 - *
24 - * @param HttpClientInterface $httpClient HTTP client for API calls
25 - * @param AzureConfig $config Azure configuration with API keys and endpoints
26 - */
27 - public function __construct(HttpClientInterface $httpClient, AzureConfig $config)
12 + private function __construct()
28 13 {
29 - $this->httpClient = $httpClient;
30 - $this->config = $config;
31 14 }
32 15
16 + public static function make(): AzureTranslator
17 + {
18 + return new self();
19 + }
20 +
33 21 /**
34 22 * Translate a string sending a request to the Azure translation Api
35 23 * @param string $text
36 24 * @param string $language
@@ -38,11 +26,11 @@
38 26 * @throws AzureTranslateInstanceException
39 27 */
40 28 public function translate(string $text, string $language): string
41 29 {
42 - $apiKey = $this->config->getTranslationApiKey();
43 - $region = $this->config->getRegion();
44 - $endpoint = $this->config->getTranslationEndpoint();
30 + $apiKey = PluginOptions::apiKeyAzureTranslateInstance();
31 + $region = PluginOptions::regionAzureTranslateInstance();
32 + $endpoint = PluginOptions::endpointAzureTranslateInstance();
45 33
46 34 if (empty($apiKey) || empty($region) || empty($endpoint)) {
47 35 return $text;
48 36 }
@@ -47,28 +35,42 @@
47 35 return $text;
48 36 }
49 37
50 38 $route = "translate?api-version=3.0&from=en&to=" . $language;
51 - $url = $endpoint . $route;
52 39
53 - $headers = [
54 - 'Content-type' => 'application/json',
55 - 'Ocp-Apim-Subscription-Key' => $apiKey,
56 - 'Ocp-Apim-Subscription-Region' => $region,
57 - ];
58 -
59 - $payload = [
40 + $response = wp_remote_post(
41 + $endpoint . $route,
60 42 [
61 - 'Text' => $text
43 + 'headers' => [
44 + 'Content-type' => 'application/json',
45 + 'Ocp-Apim-Subscription-Key' => $apiKey,
46 + 'Ocp-Apim-Subscription-Region' => $region,
47 + ],
48 + 'body' => json_encode([
49 + [
50 + 'Text' => $text
51 + ]
52 + ]),
53 + 'method' => 'POST',
62 54 ]
63 - ];
55 + );
64 56
65 - try {
66 - $bodyResult = $this->httpClient->post($url, $headers, $payload);
67 - } catch (\Exception $e) {
68 - throw new AzureTranslateInstanceException('HTTP request failed: ' . $e->getMessage());
57 +
58 + //$bodyResult = json_decode(wp_remote_retrieve_body($response), true);
59 +
60 + $responseBody = wp_remote_retrieve_body($response);
61 + if (empty($responseBody)) {
62 + if (is_object($response) && property_exists($response, 'errors') && array_key_exists('http_request_failed', $response->errors)) {
63 + throw new AzureTranslateInstanceException("Error: " . $response->errors['http_request_failed'][0]);
64 + }
65 + if (is_array($response) && array_key_exists('response', $response)) {
66 + throw new AzureTranslateInstanceException("Code: " . $response['response']['code'] . " - " . $response['response']['message']);
67 + }
68 + throw new AzureTranslateInstanceException("Error: please check if the Azure endpoint in plugin options is right");
69 69 }
70 70
71 + $bodyResult = json_decode($responseBody, true);
72 +
71 73 if (array_key_exists('error', $bodyResult)) {
72 74 throw new AzureTranslateInstanceException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']);
73 75 }
74 76
@@ -81,37 +83,37 @@
81 83 * @throws AzureTranslateInstanceException
82 84 */
83 85 public function supportedLanguages(): array
84 86 {
85 - $apiKey = $this->config->getTranslationApiKey();
87 + $apiKey = PluginOptions::apiKeyAzureTranslateInstance();
86 88 if (empty($apiKey)) {
87 89 return [];
88 90 }
89 - $endpoint = $this->config->getTranslationEndpoint();
91 + $endpoint = PluginOptions::endpointAzureTranslateInstance();
90 92 if (empty($endpoint)) {
91 93 return [];
92 94 }
93 95
94 96 $route = 'languages?api-version=3.0';
97 +
95 98 $url = $endpoint . $route;
96 99
97 - $headers = [
100 + $headers = array(
98 101 'Content-type' => 'application/json',
99 102 'Ocp-Apim-Subscription-Key' => $apiKey
100 - ];
103 + );
101 104
102 - try {
103 - $bodyResult = $this->httpClient->get($url, $headers);
104 - } catch (\Exception $e) {
105 - throw new AzureTranslateInstanceException(
106 - esc_html__('No language retrieved: maybe the translation endpoint is wrong. Please check it out and try again.', 'auto-alt-text')
107 - );
108 - }
105 + $response = wp_remote_get(
106 + $url,
107 + array(
108 + 'headers' => $headers
109 + )
110 + );
109 111
112 + $bodyResult = json_decode(wp_remote_retrieve_body($response), true);
113 +
110 114 if (empty($bodyResult)) {
111 - throw new AzureTranslateInstanceException(
112 - esc_html__('No language retrieved: maybe the translation endpoint is wrong. Please check it out and try again.', 'auto-alt-text')
113 - );
115 + throw new AzureTranslateInstanceException(esc_html__('No language retrieved: maybe the translation endpoint is wrong. Please check it out and try again.', 'auto-alt-text'));
114 116 }
115 117
116 118 if (array_key_exists('error', $bodyResult)) {
117 119 throw new AzureTranslateInstanceException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']);