PluginProbe
Auto Alt Text / 2.3.4
Auto Alt Text v2.3.4
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
auto-alt-text / src / App / Admin / PluginOptions.php

PluginOptions.php in Auto Alt Text 2.3.4, at src/App/Admin/PluginOptions.php

486 lines 26.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AATXT\App\Admin;
4
5 use AATXT\App\Logging\DBLogger;
6 use AATXT\App\AIProviders\Azure\AzureTranslator;
7 use AATXT\App\Exceptions\Azure\AzureTranslateInstanceException;
8 use AATXT\App\Utilities\AssetsManager;
9 use AATXT\App\Utilities\Encryption;
10 use AATXT\Config\Constants;
11
12 class PluginOptions
13 {
14 private static ?self $instance = null;
15 private static AssetsManager $assetsManager;
16
17 private function __construct()
18 {
19 //
20 }
21
22 /**
23 * Manage the necessary hooks to implement plugin options and their pages
24 * @return void
25 */
26 public static function register(): void
27 {
28 if (is_null(self::$instance)) {
29 self::$instance = new self();
30 }
31
32 self::$assetsManager = AssetsManager::make();
33
34 add_action('admin_enqueue_scripts', [self::$instance, 'enqueueAdminScripts'], 1);
35 add_action('admin_menu', [self::$instance, 'addOptionsPageToTheMenu']);
36 add_action('admin_init', [self::$instance, 'setupPluginOptions']);
37
38 // Encrypt API Keys on update
39 add_action('pre_update_option_' . Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION, [self::$instance, 'encryptDataOnUpdate'], 10, 3);
40 add_action('pre_update_option_' . Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE, [self::$instance, 'encryptDataOnUpdate'], 10, 3);
41 add_action('pre_update_option_' . Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI, [self::$instance, 'encryptDataOnUpdate'], 10, 3);
42
43 add_action('admin_notices', [self::$instance, 'encryptionErrorNotice']);
44 }
45
46 /**
47 * Show an error notice if the encryption of the API Key fails
48 * @return void
49 */
50 public function encryptionErrorNotice(): void
51 {
52 $raw = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI);
53 $decrypted = Encryption::make()->decrypt((string) $raw);
54
55 if ($raw && $decrypted === '') {
56 $screen = get_current_screen();
57 $settingsScreenId = 'toplevel_page_' . Constants::AATXT_PLUGIN_OPTIONS_PAGE_SLUG;
58 $showLink = ! ( $screen && $screen->id === $settingsScreenId );
59
60 $settingsUrl = esc_url( menu_page_url(Constants::AATXT_PLUGIN_OPTIONS_PAGE_SLUG, false) );
61
62 echo '<div class="notice notice-error is-dismissible">';
63 echo '<p>';
64 // Titolo in grassetto
65 echo '<strong>' . esc_html__('Auto Alt Text', 'auto-alt-text') . ':</strong> ';
66 // Messaggio di errore
67 echo esc_html__(
68 'There was a problem with the encryption of your API Key for alt text generation. Please re-enter the key and save.',
69 'auto-alt-text'
70 );
71 // Link condizionale
72 if ($showLink) {
73 echo ' <a href="' . $settingsUrl . '">'
74 . esc_html__('Go to settings page', 'auto-alt-text')
75 . '</a>.';
76 }
77 echo '</p>';
78 echo '</div>';
79 }
80 }
81
82
83 /**
84 * Encrypt data
85 * @param ?string $newValue
86 * @param ?string $oldValue
87 * @return ?string
88 */
89 public function encryptDataOnUpdate(?string $newValue, ?string $oldValue): ?string
90 {
91 if (!empty($newValue)) {
92 $newValue = (Encryption::make())->encrypt($newValue);
93 }
94 return $newValue;
95 }
96
97 /**
98 * @return void
99 */
100 public static function enqueueAdminScripts(): void
101 {
102 $screen = get_current_screen();
103 $isMainOptionsPage = $screen->id === 'toplevel_page_' . Constants::AATXT_PLUGIN_OPTIONS_PAGE_SLUG;
104
105 if ($isMainOptionsPage || strpos($screen->id, Constants::AATXT_PLUGIN_SLUG . '_') !== false) {
106
107 $adminCss = self::$assetsManager->getAssetUrl('resources/js/admin.js', true);
108
109 wp_enqueue_style(Constants::AATXT_PLUGIN_ASSETS_HANDLE, $adminCss, [], false);
110
111 if ($isMainOptionsPage) {
112 $adminJs = self::$assetsManager->getAssetUrl('resources/js/admin.js', false);
113 wp_enqueue_script(Constants::AATXT_PLUGIN_ASSETS_HANDLE, $adminJs, [], false);
114 }
115
116 }
117 }
118
119 /**
120 * Create options pages
121 * @return void
122 */
123 public static function addOptionsPageToTheMenu(): void
124 {
125 add_menu_page('Auto Alt Text Options', 'Auto Alt Text', 'manage_options', Constants::AATXT_PLUGIN_OPTIONS_PAGE_SLUG, [self::$instance, 'optionsMainPage'], null, 99);
126 add_submenu_page(Constants::AATXT_PLUGIN_OPTIONS_PAGE_SLUG, 'Error Log', 'Error log', 'manage_options', Constants::AATXT_PLUGIN_OPTION_LOG_PAGE_SLUG, [self::$instance, 'logOptionsPage']);
127 }
128
129 /**
130 * Implement the page showing error log
131 * @return void
132 */
133 public static function logOptionsPage(): void
134 {
135 ?>
136 <div class="wrap">
137 <h1><?php esc_html_e('Auto Alt Text Error Log', 'auto-alt-text') ?></h1>
138 <div class="aat-options plugin-description">
139 <p>
140 <?php esc_html_e("If the alt text of your images was not generated correctly, you can find the reason in this log.", 'auto-alt-text'); ?>
141 <br>
142 <?php esc_html_e("Each line corresponds to an error generated by a call to the Azure or OpenAI API after uploading an image.", 'auto-alt-text'); ?>
143 </p>
144 <?php
145 $logs = DBLogger::make()->getImageLog();
146 if ($logs) {
147 echo '<textarea id="error-log" name="error-log" readonly>' . esc_textarea($logs) . '</textarea>';
148 } else {
149 echo '<p>' . esc_html__('There is no error log yet!', 'auto-alt-text') . '</p>';
150 }
151
152 ?>
153 </div>
154 </div>
155 <?php
156 }
157
158 /**
159 * Implement the main option page
160 * @return void
161 */
162 public static function optionsMainPage(): void
163 {
164 ?>
165 <div class="wrap">
166 <h1><?php esc_html_e('Auto Alt Text Options', 'auto-alt-text') ?></h1>
167
168 <div class="aat-options plugin-description">
169 <p>
170 <?php esc_html_e("This plugin allows you to automatically generate Alt Text for the images that are uploaded to the site's media library.", 'auto-alt-text'); ?>
171 <br>
172 <?php esc_html_e("The following methods are available to generate the alt text:", 'auto-alt-text'); ?>
173 </p>
174 <ul>
175 <li>
176 <strong><?php esc_html_e("OpenAI's APIs", 'auto-alt-text'); ?></strong>: <?php esc_html_e("the image will be analyzed by the AI services provided by OpenAI and an alt text will be generated based on the prompt you set;", 'auto-alt-text'); ?>
177 </li>
178 <li>
179 <strong><?php esc_html_e("Azure's APIs", 'auto-alt-text'); ?></strong>: <?php esc_html_e("the image will be analyzed by the AI services provided by Azure and an alt text will be generated in the language of your choice;", 'auto-alt-text'); ?>
180 </li>
181 <li>
182 <strong><?php esc_html_e("Title of the article (not AI)", 'auto-alt-text'); ?></strong>: <?php esc_html_e("if the image is uploaded within an article, the title of the article will be used as alt text;", 'auto-alt-text'); ?>
183 </li>
184 <li>
185 <strong><?php esc_html_e("Title of the attachment (not AI)", 'auto-alt-text'); ?></strong>: <?php esc_html_e("the title of the attachment will be copied into the alt text;", 'auto-alt-text'); ?>
186 </li>
187 </ul>
188 <p><?php esc_html_e("Once all the necessary data for the chosen generation method has been entered, the alt texts will be created automatically upon uploading each image.", 'auto-alt-text'); ?></p>
189 <p><strong>New</strong>: <?php esc_html_e("For images already in the media library, you can create bulk alt texts. Open the Media Library in the \"list\" view, select the images for which to generate the alt text, and choose the \"Generate alt text\" bulk action. (Depending on the number of images chosen and their weight, this may take some time.)", 'auto-alt-text'); ?></p>
190 <p>
191 <strong><?php esc_html_e('Pay attention please:', 'auto-alt-text') ?></strong> <?php esc_html_e("if the alt text for an image is not generated, check the logs on the", 'auto-alt-text'); ?>
192 <a href="<?php esc_url(menu_page_url(Constants::AATXT_PLUGIN_OPTION_LOG_PAGE_SLUG, true)); ?>"><?php esc_html_e('designated page.', 'auto-alt-text') ?></a>
193 </p>
194 </div>
195 <form method="post" action="options.php" class="aat-options">
196 <?php
197 settings_fields('auto_alt_text_options');
198
199 echo '<div>';
200 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_TYPOLOGY) . '">' . esc_html__('Generation method', 'auto-alt-text') . '</label>';
201 echo '<p class="description">' . esc_html__("Which method do you want to use to generate the alt text for the images?", 'auto-alt-text') . '</p>';
202 $typology = self::typology();
203
204 ?>
205
206 <select name="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_TYPOLOGY); ?>"
207 id="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_TYPOLOGY); ?>">
208 <option value="<?php echo esc_attr(Constants::AATXT_OPTION_TYPOLOGY_DEACTIVATED); ?>"<?php echo esc_attr(self::selected($typology, Constants::AATXT_OPTION_TYPOLOGY_DEACTIVATED)); ?>><?php esc_html_e("Deactivated", 'auto-alt-text'); ?></option>
209 <option value="<?php echo esc_attr(Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI); ?>"<?php echo esc_attr(self::selected($typology, Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI)); ?>><?php esc_html_e("OpenAI's APIs", 'auto-alt-text'); ?></option>
210 <option value="<?php echo esc_attr(Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE); ?>"<?php echo esc_attr(self::selected($typology, Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE)); ?>><?php esc_html_e("Azure's APIs", 'auto-alt-text'); ?></option>
211 <option value="<?php echo esc_attr(Constants::AATXT_OPTION_TYPOLOGY_CHOICE_ARTICLE_TITLE); ?>"<?php echo esc_attr(self::selected($typology, Constants::AATXT_OPTION_TYPOLOGY_CHOICE_ARTICLE_TITLE)); ?>><?php esc_html_e("Title of the article (not AI)", 'auto-alt-text'); ?></option>
212 <option value="<?php echo esc_attr(Constants::AATXT_OPTION_TYPOLOGY_CHOICE_ATTACHMENT_TITLE); ?>"<?php echo esc_attr(self::selected($typology, Constants::AATXT_OPTION_TYPOLOGY_CHOICE_ATTACHMENT_TITLE)); ?>><?php esc_html_e("Title of the attachment (not AI)", 'auto-alt-text'); ?></option>
213 </select>
214 <?php
215 echo '</div>';
216
217 echo '<div class="plugin-option type-article-title"><strong>' . esc_html__('Notice', 'auto-alt-text') . '</strong>: ' .
218 esc_html__('If you try to insert an image into a post that has not yet been saved as a draft or published, the plugin cannot generate an alt text based on the post\'s title since the title itself has not yet been saved.', 'auto-alt-text') . ' ' .
219 esc_html__('Therefore, the alt text "Auto draft" will be inserted. To avoid this behavior, save the article draft first and then upload the image.', 'auto-alt-text') .
220 '</div>';
221
222 $openaiModel = self::openAiModel();
223
224 echo '<div class="plugin-option type-openai">';
225 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_MODEL_OPENAI) . '">' . esc_html__('Model', 'auto-alt-text') . '</label>';
226
227 echo '<select name="' . esc_attr(Constants::AATXT_OPTION_FIELD_MODEL_OPENAI) . '" id="' . esc_attr(Constants::AATXT_OPTION_FIELD_MODEL_OPENAI) . '">';
228 foreach(Constants::AATXT_OPTION_FIELD_MODEL_OPENAI_OPTIONS as $key => $value) {
229 echo '<option value="' . esc_attr($key) . '" ' . esc_attr(self::selected($openaiModel, $key)) . '>' . esc_html($value) . '</option>';
230 }
231 echo '</select>';
232 echo '</div>';
233
234 echo '<div class="plugin-option type-openai"><strong>' . esc_html__('Notice', 'auto-alt-text') . '</strong>: ' .
235 esc_html__('Rarely it may happen that the chosen model fails to generate correct alt text for the image.', 'auto-alt-text') . ' ' .
236 esc_html__('In these cases, the call to the api will be re-performed using the gpt-4o-mini model as fallback.', 'auto-alt-text') . '<br>' .
237 esc_html__('In case of errors, it is still possible to find the specific reason stated on the', 'auto-alt-text') . ' <a href="' . esc_url(menu_page_url(Constants::AATXT_PLUGIN_OPTION_LOG_PAGE_SLUG, false)) . '">' . esc_html__('error log page', 'auto-alt-text') . '</a>.' .
238 '</div>';
239
240 echo '<div class="plugin-option type-openai">';
241 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI) . '">' . esc_html__('OpenAI API Key', 'auto-alt-text') . '</label>';
242 echo '<p class="description">' . esc_html__("Enter your API Key", 'auto-alt-text') . '</p>';
243 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI);
244 echo '<input type="password" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI) . '" value="' . esc_attr((Encryption::make())->decrypt($apiKey)) . '" />';
245 echo '</div>';
246
247 echo '<div class="plugin-option type-openai">';
248 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI) . '">' . esc_html__('Prompt', 'auto-alt-text') . '</label>';
249 echo '<p class="description">' . esc_html__("Enter a specific and detailed prompt according to your needs.", 'auto-alt-text') . '</p>';
250 $defaultPrompt = sprintf(esc_html__("Act like an SEO expert and write an alt text of up to 125 characters for this image.", 'auto-alt-text'), Constants::AATXT_IMAGE_URL_TAG);
251 $prompt = get_option(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI) ?: $defaultPrompt;
252 echo '<textarea name="' . esc_attr(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI) . '" rows="5" cols="50">' . esc_textarea($prompt) . '</textarea>';
253 echo '</div>';
254
255 echo '<div class="plugin-option type-azure">' . esc_html__("Fill out the following fields to leverage Azure's computer vision services to generate the Alt texts.", 'auto-alt-text') . '</div>';
256
257 echo '<div class="plugin-option type-azure">';
258 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION) . '">' . esc_html__('Azure Computer Vision API Key', 'auto-alt-text') . '</label>';
259 echo '<p class="description">' . esc_html__("Enter the API key for the Computer Vision service of your Azure account.", 'auto-alt-text') . '</p>';
260 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION);
261 echo '<input type="password" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION) . '" value="' . esc_attr((Encryption::make())->decrypt($apiKey)) . '" />';
262 echo '</div>';
263
264 echo '<div class="plugin-option type-azure">';
265 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION) . '">' . esc_html__('Azure Computer Vision Endpoint', 'auto-alt-text') . '</label>';
266 echo '<p class="description">' . esc_html__("Enter the endpoint of the Computer Vision service.", 'auto-alt-text') . ' (es. https://computer-vision-france-central.cognitiveservices.azure.com/)</p>';
267 $endpoint = get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION);
268 echo '<input type="text" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION) . '" value="' . esc_attr($endpoint) . '" />';
269 echo '</div>';
270
271 echo '<div class="plugin-option type-azure">' .
272 '<strong>' . esc_html__('The default alt text language is English.', 'auto-alt-text') . '</strong><br>' .
273 esc_html__('If you want to translate into another language, enter the following data necessary for the translation API to work.', 'auto-alt-text') . ' ' .
274 esc_html__('After saving the changes you can select the desired language.', 'auto-alt-text') .
275 '</div>';
276
277 echo '<div class="plugin-option type-azure">';
278 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE) . '">' . esc_html__('Azure Translate Instance API Key', 'auto-alt-text') . '</label>';
279 echo '<p class="description">' . esc_html__("Enter your API key for the Azure Translate Instance service.", 'auto-alt-text') . '</p>';
280 $translationApiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE);
281 echo '<input type="password" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE) . '" value="' . esc_attr((Encryption::make())->decrypt($translationApiKey)) . '" class="notRequired" />';
282 echo '</div>';
283
284 echo '<div class="plugin-option type-azure">';
285 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE) . '">' . esc_html__('Azure Translate Instance Endpoint', 'auto-alt-text') . '</label>';
286 echo '<p class="description">' . esc_html__("Enter the endpoint of the Translate Instance service", 'auto-alt-text') . ' (es. https://api.cognitive.microsofttranslator.com/)</p>';
287 $translationEndpoint = get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE);
288 echo '<input type="text" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE) . '" value="' . esc_attr($translationEndpoint) . '" class="notRequired" />';
289 echo '</div>';
290
291 echo '<div class="plugin-option type-azure">';
292 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE) . '">' . esc_html__('Azure Translate Instance Region', 'auto-alt-text') . '</label>';
293 echo '<p class="description">' . esc_html__("Enter the region of the Azure Translate Instance service.", 'auto-alt-text') . ' (es. westeurope)</p>';
294 $translationRegion = get_option(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE);
295 echo '<input type="text" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE) . '" value="' . esc_attr($translationRegion) . '" class="notRequired" />';
296 echo '</div>';
297
298 if ($translationApiKey && $translationEndpoint && $translationRegion):
299 echo '<div class="plugin-option type-azure">';
300 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE) . '">' . esc_html__('Alt Text Language', 'auto-alt-text') . '</label>';
301 echo '<p class="description">' . esc_html__("Select the language in which the alt text should be written.", 'auto-alt-text') . '</p>';
302 $currentLanguage = get_option(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE);
303
304 try {
305 $supportedLanguages = (AzureTranslator::make())->supportedLanguages();
306 } catch (AzureTranslateInstanceException $e) {
307 $supportedLanguages = [
308 "en" => [
309 "name" => "English",
310 "nativeName" => "English",
311 "dir" => "ltr",
312 ]
313 ];
314 echo '<p style="color:red"><strong>' . esc_html($e->getMessage()) . '</strong></p>';
315 }
316
317 ?>
318 <select name="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE); ?>"
319 id="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE); ?>">
320 <?php
321 foreach ($supportedLanguages as $key => $language):
322 ?>
323 <option value="<?php echo esc_attr($key) ?>" <?php echo esc_attr(self::selected($currentLanguage, $key)); ?>><?php echo esc_html($language['name']); ?></option>
324 <?php
325 endforeach;
326 ?>
327 </select>
328
329 <?php
330
331 echo '</div>';
332 endif;
333
334 echo '<div class="plugin-option type-article-title type-attachment-title type-openai type-azure">';
335 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_PRESERVE_EXISTING_ALT_TEXT) . '">' . esc_html__('Keep existing alt text', 'auto-alt-text') . '</label>';
336 echo '<p class="description">' . esc_html__("If checked, the existing alt text of images will not be overwritten.", 'auto-alt-text') . '</p>';
337 $preserveAltText = get_option(Constants::AATXT_OPTION_FIELD_PRESERVE_EXISTING_ALT_TEXT);
338 echo '<input type="checkbox" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_PRESERVE_EXISTING_ALT_TEXT) . '" value="1" class="notRequired" ' . checked(1, $preserveAltText, false) . ' />';
339 echo '</div>';
340
341 submit_button();
342 ?>
343 </form>
344 </div>
345 <?php
346 }
347
348 /**
349 * If option is selected return the selected attribute
350 * @param string $selectedValue
351 * @param string $inputValue
352 * @return string
353 */
354 public static function selected(string $selectedValue, string $inputValue): string
355 {
356 if (empty($selectedValue) && 'en' == $inputValue) {
357 return ' selected';
358 }
359 return $selectedValue == $inputValue ? ' selected' : '';
360 }
361
362
363 /**
364 * Register input fields and option settings
365 * @return void
366 */
367 public static function setupPluginOptions(): void
368 {
369 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI);
370 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI, [self::class, 'sanitizeTextArea']);
371 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_TYPOLOGY, [self::class, 'sanitizeText']);
372 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_MODEL_OPENAI, [self::class, 'sanitizeText']);
373 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION);
374 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION, [self::class, 'sanitizeUrl']);
375 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE);
376 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE, [self::class, 'sanitizeUrl']);
377 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE, [self::class, 'sanitizeText']);
378 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE, [self::class, 'sanitizeText']);
379 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_PRESERVE_EXISTING_ALT_TEXT);
380 }
381
382 /**
383 * @return string
384 */
385 public static function typology(): string
386 {
387 return get_option(Constants::AATXT_OPTION_FIELD_TYPOLOGY);
388 }
389
390 public static function openAiModel(): string
391 {
392 return get_option(Constants::AATXT_OPTION_FIELD_MODEL_OPENAI) ?: Constants::AATXT_GPT4O;
393 }
394
395 /**
396 * @return string
397 */
398 public static function prompt(): string
399 {
400 return get_option(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI);
401 }
402
403 /**
404 * @return string
405 */
406 public static function apiKeyOpenAI(): string
407 {
408 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI);
409 return (Encryption::make())->decrypt($apiKey);
410 }
411
412 /**
413 * @return string
414 */
415 public static function apiKeyAzureComputerVision(): string
416 {
417 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION);
418 return (Encryption::make())->decrypt($apiKey);
419 }
420
421 /**
422 * @return string
423 */
424 public static function endpointAzureComputerVision(): string
425 {
426 return get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION);
427 }
428
429 /**
430 * @return string
431 */
432 public static function apiKeyAzureTranslateInstance(): string
433 {
434 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE);
435 return (Encryption::make())->decrypt($apiKey);
436 }
437
438 /**
439 * @return string
440 */
441 public static function endpointAzureTranslateInstance(): string
442 {
443 return get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE);
444 }
445
446 /**
447 * @return string
448 */
449 public static function regionAzureTranslateInstance(): string
450 {
451 return get_option(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE);
452 }
453
454 /**
455 * @return string
456 */
457 public static function languageAzureTranslateInstance(): string
458 {
459 return get_option(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE) ?: 'en';
460 }
461
462 /**
463 * @return bool
464 */
465 public static function preserveExistingAltText(): bool
466 {
467 return get_option(Constants::AATXT_OPTION_FIELD_PRESERVE_EXISTING_ALT_TEXT);
468 }
469
470 public static function sanitizeUrl($input): string
471 {
472 return sanitize_url($input);
473 }
474
475 public static function sanitizeText($input): string
476 {
477 return sanitize_text_field($input);
478 }
479
480 public static function sanitizeTextArea($input): string
481 {
482 return sanitize_textarea_field($input);
483 }
484
485 }
486