PluginProbe
Auto Alt Text / 1.3.1
Auto Alt Text v1.3.1
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 1.3.1, at src/App/Admin/PluginOptions.php

476 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
44 /**
45 * Encrypt data
46 * @param ?string $newValue
47 * @param ?string $oldValue
48 * @return ?string
49 */
50 public function encryptDataOnUpdate(?string $newValue, ?string $oldValue): ?string
51 {
52 if (!empty($newValue)) {
53 $newValue = (Encryption::make())->encrypt($newValue);
54 }
55 return $newValue;
56 }
57
58 /**
59 * @return void
60 */
61 public static function enqueueAdminScripts(): void
62 {
63 $screen = get_current_screen();
64 $isMainOptionsPage = $screen->id === 'toplevel_page_' . Constants::AATXT_PLUGIN_OPTIONS_PAGE_SLUG;
65
66 if ($isMainOptionsPage || strpos($screen->id, Constants::AATXT_PLUGIN_SLUG . '_') !== false) {
67
68 $adminCss = self::$assetsManager->getAssetUrl('resources/js/admin.js', true);
69
70 wp_enqueue_style(Constants::AATXT_PLUGIN_ASSETS_HANDLE, $adminCss, [], false);
71
72 if ($isMainOptionsPage) {
73 $adminJs = self::$assetsManager->getAssetUrl('resources/js/admin.js', false);
74 wp_enqueue_script(Constants::AATXT_PLUGIN_ASSETS_HANDLE, $adminJs, [], false);
75 }
76
77 }
78 }
79
80 /**
81 * Create options pages
82 * @return void
83 */
84 public static function addOptionsPageToTheMenu(): void
85 {
86 add_menu_page('Auto Alt Text Options', 'Auto Alt Text', 'manage_options', Constants::AATXT_PLUGIN_OPTIONS_PAGE_SLUG, [self::$instance, 'optionsMainPage'], null, 99);
87 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']);
88 }
89
90 /**
91 * Implement the page showing error log
92 * @return void
93 */
94 public static function logOptionsPage(): void
95 {
96 ?>
97 <div class="wrap">
98 <h1><?php esc_html_e('Auto Alt Text Error Log', 'auto-alt-text') ?></h1>
99 <div class="aat-options plugin-description">
100 <p>
101 <?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'); ?>
102 <br>
103 <?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'); ?>
104 </p>
105 <?php
106 $logs = DBLogger::make()->getImageLog();
107 if ($logs) {
108 echo '<textarea id="error-log" name="error-log" readonly>' . esc_textarea($logs) . '</textarea>';
109 } else {
110 echo '<p>' . esc_html__('There is no log file yet!', 'auto-alt-text') . '</p>';
111 }
112
113 ?>
114 </div>
115 </div>
116 <?php
117 }
118
119 /**
120 * Implement the main option page
121 * @return void
122 */
123 public static function optionsMainPage(): void
124 {
125 ?>
126 <div class="wrap">
127 <h1><?php esc_html_e('Auto Alt Text Options', 'auto-alt-text') ?></h1>
128
129 <div class="aat-options plugin-description">
130 <p>
131 <?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'); ?>
132 <br>
133 <?php esc_html_e("The following methods are available to generate the alt text:", 'auto-alt-text'); ?>
134 </p>
135 <ul>
136 <li>
137 <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'); ?>
138 </li>
139 <li>
140 <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'); ?>
141 </li>
142 <li>
143 <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'); ?>
144 </li>
145 <li>
146 <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'); ?>
147 </li>
148 </ul>
149 <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>
150 <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>
151 <p>
152 <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'); ?>
153 <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>
154 </p>
155 </div>
156 <form method="post" action="options.php" class="aat-options">
157 <?php
158 settings_fields('auto_alt_text_options');
159
160 echo '<div>';
161 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_TYPOLOGY) . '">' . esc_html__('Generation method', 'auto-alt-text') . '</label>';
162 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>';
163 $typology = get_option(Constants::AATXT_OPTION_FIELD_TYPOLOGY);
164 ?>
165 <select name="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_TYPOLOGY); ?>"
166 id="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_TYPOLOGY); ?>">
167 <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>
168 <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>
169 <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>
170 <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>
171 <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>
172 </select>
173 <?php
174 echo '</div>';
175
176 echo '<div class="plugin-option type-article-title"><strong>' . esc_html__('Notice', 'auto-alt-text') . '</strong>: ' .
177 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') . ' ' .
178 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') .
179 '</div>';
180
181
182 echo '<div class="plugin-option type-openai"><strong>' . esc_html__('Notice', 'auto-alt-text') . '</strong>: ' .
183 esc_html__('This plugin leverages the new "gpt-4-vision-preview" model from OpenAI to identify the content of the image.', 'auto-alt-text') . ' ' .
184 esc_html__('As the name suggests, this model is still in a preview stage and OpenAI states:', 'auto-alt-text') . ' "<em>This is a preview model version and not suited yet for production traffic</em>".<br>' .
185 esc_html__('Therefore, it is necessary to select a fallback model in case gpt-4-preview fails.', 'auto-alt-text') . '<br>' .
186 esc_html__('The fallback models are not able to read the content of the image but will rely exclusively on the name of the image file, guessing its content.', 'auto-alt-text') . '<br>' .
187 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>.' .
188 '</div>';
189
190 echo '<div class="plugin-option type-openai">';
191 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI) . '">' . esc_html__('OpenAI API Key', 'auto-alt-text') . '</label>';
192 echo '<p class="description">' . esc_html__("Enter your API Key", 'auto-alt-text') . '</p>';
193 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI);
194 echo '<input type="password" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI) . '" value="' . esc_attr((Encryption::make())->decrypt($apiKey)) . '" />';
195 echo '</div>';
196
197 echo '<div class="plugin-option type-openai">';
198 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI) . '">' . esc_html__('Prompt', 'auto-alt-text') . '</label>';
199 echo '<p class="description">' . esc_html__("Enter a specific and detailed prompt according to your needs.", 'auto-alt-text') . '</p>';
200 $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);
201 $prompt = get_option(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI) ?: $defaultPrompt;
202 echo '<textarea name="' . esc_attr(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI) . '" rows="5" cols="50">' . esc_textarea($prompt) . '</textarea>';
203 echo '</div>';
204
205 echo '<div class="plugin-option type-openai">';
206 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_FALLBACK_MODEL_OPENAI) . '">' . esc_html__('Fallback OpenAi Model', 'auto-alt-text') . '</label>';
207 echo '<p class="description">' . esc_html__("Choose the alternative OpenAI model you want to use to generate the alt text when the gpt-4-vision-preview model fails.", 'auto-alt-text') . '</p>';
208 $modelSaved = get_option(Constants::AATXT_OPTION_FIELD_FALLBACK_MODEL_OPENAI);
209 ?>
210
211 <select name="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_FALLBACK_MODEL_OPENAI); ?>"
212 id="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_FALLBACK_MODEL_OPENAI); ?>">
213 <?php
214 foreach (Constants::AATXT_OPENAI_MODELS as $modelName) :
215 ?>
216 <option value="<?php echo esc_attr($modelName); ?>" <?php echo self::isModelSelected($modelSaved, $modelName) ? 'selected="selected"' : ''; ?>><?php echo esc_html($modelName); ?></option>
217 <?php
218 endforeach;
219 ?>
220 </select>
221
222 <?php
223 echo '</div>';
224
225 echo '<div class="plugin-option type-openai">';
226 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_FALLBACK_PROMPT_OPENAI) . '">' . esc_html__('Fallback Prompt', 'auto-alt-text') . '</label>';
227 echo '<p class="description">' . esc_html__("Enter a specific and detailed prompt according to your needs.", 'auto-alt-text') . '</p>';
228 $defaultPrompt = sprintf(esc_html__("Act like an SEO expert and write an English alt text for this image %s, using a maximum of 125 characters. Just return the text without any additional comments.", 'auto-alt-text'), Constants::AATXT_IMAGE_URL_TAG);
229 $fallbackPrompt = get_option(Constants::AATXT_OPTION_FIELD_FALLBACK_PROMPT_OPENAI) ?: $defaultPrompt;
230 echo '<textarea name="' . esc_attr(Constants::AATXT_OPTION_FIELD_FALLBACK_PROMPT_OPENAI) . '" rows="5" cols="50">' . esc_textarea($fallbackPrompt) . '</textarea>';
231 echo '</div>';
232
233 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>';
234
235 echo '<div class="plugin-option type-azure">';
236 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>';
237 echo '<p class="description">' . esc_html__("Enter the API key for the Computer Vision service of your Azure account.", 'auto-alt-text') . '</p>';
238 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION);
239 echo '<input type="password" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION) . '" value="' . esc_attr((Encryption::make())->decrypt($apiKey)) . '" />';
240 echo '</div>';
241
242 echo '<div class="plugin-option type-azure">';
243 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION) . '">' . esc_html__('Azure Computer Vision Endpoint', 'auto-alt-text') . '</label>';
244 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>';
245 $endpoint = get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION);
246 echo '<input type="text" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION) . '" value="' . esc_attr($endpoint) . '" />';
247 echo '</div>';
248
249 echo '<div class="plugin-option type-azure">' .
250 '<strong>' . esc_html__('The default alt text language is English.', 'auto-alt-text') . '</strong><br>' .
251 esc_html__('If you want to translate into another language, enter the following data necessary for the translation API to work.', 'auto-alt-text') . ' ' .
252 esc_html__('After saving the changes you can select the desired language.', 'auto-alt-text') .
253
254 '</div>';
255
256 echo '<div class="plugin-option type-azure">';
257 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>';
258 echo '<p class="description">' . esc_html__("Enter your API key for the Azure Translate Instance service.", 'auto-alt-text') . '</p>';
259 $translationApiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE);
260 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" />';
261 echo '</div>';
262
263 echo '<div class="plugin-option type-azure">';
264 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE) . '">' . esc_html__('Azure Translate Instance Endpoint', 'auto-alt-text') . '</label>';
265 echo '<p class="description">' . esc_html__("Enter the endpoint of the Translate Instance service", 'auto-alt-text') . ' (es. https://api.cognitive.microsofttranslator.com/)</p>';
266 $translationEndpoint = get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE);
267 echo '<input type="text" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE) . '" value="' . esc_attr($translationEndpoint) . '" class="notRequired" />';
268 echo '</div>';
269
270 echo '<div class="plugin-option type-azure">';
271 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE) . '">' . esc_html__('Azure Translate Instance Region', 'auto-alt-text') . '</label>';
272 echo '<p class="description">' . esc_html__("Enter the region of the Azure Translate Instance service.", 'auto-alt-text') . ' (es. westeurope)</p>';
273 $translationRegion = get_option(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE);
274 echo '<input type="text" name="' . esc_attr(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE) . '" value="' . esc_attr($translationRegion) . '" class="notRequired" />';
275 echo '</div>';
276
277 if ($translationApiKey && $translationEndpoint && $translationRegion):
278 echo '<div class="plugin-option type-azure">';
279 echo '<label for="' . esc_attr(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE) . '">' . esc_html__('Alt Text Language', 'auto-alt-text') . '</label>';
280 echo '<p class="description">' . esc_html__("Select the language in which the alt text should be written.", 'auto-alt-text') . '</p>';
281 $currentLanguage = get_option(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE);
282
283 try {
284 $supportedLanguages = (AzureTranslator::make())->supportedLanguages();
285 } catch (AzureTranslateInstanceException $e) {
286 $supportedLanguages = [
287 "en" => [
288 "name" => "English",
289 "nativeName" => "English",
290 "dir" => "ltr",
291 ]
292 ];
293 echo '<p style="color:red"><strong>' . esc_html($e->getMessage()) . '</strong></p>';
294 }
295
296 ?>
297 <select name="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE); ?>"
298 id="<?php echo esc_attr(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE); ?>">
299 <?php
300 foreach ($supportedLanguages as $key => $language):
301 ?>
302 <option value="<?php echo esc_attr($key) ?>" <?php echo esc_attr(self::selected($currentLanguage, $key)); ?>><?php echo esc_html($language['name']); ?></option>
303 <?php
304 endforeach;
305 ?>
306 </select>
307
308 <?php
309
310 echo '</div>';
311 endif;
312
313 submit_button();
314 ?>
315 </form>
316 </div>
317 <?php
318 }
319
320 /**
321 * If option is selected return the selected attribute
322 * @param string $selectedValue
323 * @param string $inputValue
324 * @return string
325 */
326 public static function selected(string $selectedValue, string $inputValue): string
327 {
328 if (empty($selectedValue) && 'en' == $inputValue) {
329 return ' selected';
330 }
331 return $selectedValue == $inputValue ? ' selected' : '';
332 }
333
334
335 /**
336 * Register input fields and option settings
337 * @return void
338 */
339 public static function setupPluginOptions(): void
340 {
341 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI);
342 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI, [self::class, 'sanitizeTextArea']);
343 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_TYPOLOGY, [self::class, 'sanitizeText']);
344 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_FALLBACK_MODEL_OPENAI, [self::class, 'sanitizeText']);
345 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_FALLBACK_PROMPT_OPENAI, [self::class, 'sanitizeTextArea']);
346 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION);
347 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION, [self::class, 'sanitizeUrl']);
348 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE);
349 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE, [self::class, 'sanitizeUrl']);
350 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE, [self::class, 'sanitizeText']);
351 register_setting('auto_alt_text_options', Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE, [self::class, 'sanitizeText']);
352 }
353
354 /**
355 * Check if a model is selected
356 * @param string $modelSaved
357 * @param string $currentModel
358 * @return bool
359 */
360 public static function isModelSelected(string $modelSaved, string $currentModel): bool
361 {
362 if (empty($modelSaved)) {
363 return Constants::AATXT_OPENAI_DEFAULT_MODEL == $currentModel;
364 }
365
366 return $modelSaved == $currentModel;
367 }
368
369 /**
370 * @return string
371 */
372 public static function typology(): string
373 {
374 return get_option(Constants::AATXT_OPTION_FIELD_TYPOLOGY);
375 }
376
377 /**
378 * @return string
379 */
380 public static function prompt(): string
381 {
382 return get_option(Constants::AATXT_OPTION_FIELD_PROMPT_OPENAI);
383 }
384
385 /**
386 * @return string
387 */
388 public static function apiKeyOpenAI(): string
389 {
390 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_OPENAI);
391 return (Encryption::make())->decrypt($apiKey);
392 }
393
394 /**
395 * @return string
396 */
397 public static function apiKeyAzureComputerVision(): string
398 {
399 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_COMPUTER_VISION);
400 return (Encryption::make())->decrypt($apiKey);
401 }
402
403 /**
404 * @return string
405 */
406 public static function endpointAzureComputerVision(): string
407 {
408 return get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_COMPUTER_VISION);
409 }
410
411 /**
412 * @return string
413 */
414 public static function apiKeyAzureTranslateInstance(): string
415 {
416 $apiKey = get_option(Constants::AATXT_OPTION_FIELD_API_KEY_AZURE_TRANSLATE_INSTANCE);
417 return (Encryption::make())->decrypt($apiKey);
418 }
419
420 /**
421 * @return string
422 */
423 public static function endpointAzureTranslateInstance(): string
424 {
425 return get_option(Constants::AATXT_OPTION_FIELD_ENDPOINT_AZURE_TRANSLATE_INSTANCE);
426 }
427
428 /**
429 * @return string
430 */
431 public static function regionAzureTranslateInstance(): string
432 {
433 return get_option(Constants::AATXT_OPTION_FIELD_REGION_AZURE_TRANSLATE_INSTANCE);
434 }
435
436 /**
437 * @return string
438 */
439 public static function languageAzureTranslateInstance(): string
440 {
441 return get_option(Constants::AATXT_OPTION_FIELD_LANGUAGE_AZURE_TRANSLATE_INSTANCE) ?: 'en';
442 }
443
444 /**
445 * @return string
446 */
447 public static function model(): string
448 {
449 return get_option(Constants::AATXT_OPTION_FIELD_FALLBACK_MODEL_OPENAI);
450 }
451
452 /**
453 * @return string
454 */
455 public static function fallbackPrompt(): string
456 {
457 return get_option(Constants::AATXT_OPTION_FIELD_FALLBACK_PROMPT_OPENAI);
458 }
459
460 public static function sanitizeUrl($input): string
461 {
462 return sanitize_url($input);
463 }
464
465 public static function sanitizeText($input): string
466 {
467 return sanitize_text_field($input);
468 }
469
470 public static function sanitizeTextArea($input): string
471 {
472 return sanitize_textarea_field($input);
473 }
474
475 }
476