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/Setup.php +69 -5 2.0.02.5.2 View file →
@@ -1,8 +1,11 @@
1 1 <?php
2 2
3 3 namespace AATXT\App;
4 4
5 +use AATXT\App\Admin\MediaLibrary;
6 +use AATXT\App\AIProviders\Anthropic\AnthropicResponse;
7 +use AATXT\App\Exceptions\Anthropic\AnthropicException;
5 8 use AATXT\App\Logging\DBLogger;
6 9 use AATXT\App\Admin\PluginOptions;
7 10 use AATXT\App\AIProviders\Azure\AzureComputerVisionCaptionsResponse;
8 11 use AATXT\App\AIProviders\OpenAI\Fallback;
@@ -33,8 +36,10 @@
33 36 }
34 37
35 38 //Register plugin options pages
36 39 PluginOptions::register();
40 + //Register medial library hooks
41 + MediaLibrary::register();
37 42
38 43 register_activation_hook(AATXT_FILE_ABSPATH, [self::$instance, 'activatePlugin']);
39 44 register_deactivation_hook(AATXT_FILE_ABSPATH, [self::$instance, 'deactivatePlugin']);
40 45
@@ -112,11 +117,30 @@
112 117
113 118 if ($mediaUpdated === 0) {
114 119 printf('<div id="message" class="notice notice-error is-dismissible"><p>' . esc_attr__('No Alt Text has been set.', 'auto-alt-text') . ' %s</p></div>', $errorLogDisclaimer);
115 120 } elseif ($mediaSelected === $mediaUpdated) {
121 + /**
122 + * translators:
123 + * %s = number of images processed
124 + */
116 125 printf('<div id="message" class="updated notice is-dismissible"><p>' . esc_attr__('The Alt Text has been set for %s media.', 'auto-alt-text') . '</p></div>', $mediaUpdated);
117 126 } else {
118 - printf('<div id="message" class="notice notice-warning is-dismissible"><p>' . esc_attr__('The Alt Text has been set for %s of %s media.', 'auto-alt-text') . ' %s</p></div>', $mediaUpdated, $mediaSelected, $errorLogDisclaimer);
127 + /**
128 + * translators:
129 + * %1$s = number of media items successfully updated
130 + * %2$s = total number of media items selected
131 + * %3$s = HTML disclaimer/link to the error log
132 + */
133 + printf(
134 + '<div id="message" class="notice notice-warning is-dismissible"><p>%s</p></div>',
135 + sprintf(
136 + /* translators: 1 = updated count, 2 = selected count, 3 = disclaimer HTML */
137 + __( 'The Alt Text has been set for %1$s of %2$s media. %3$s', 'auto-alt-text' ),
138 + number_format_i18n( $mediaUpdated ),
139 + number_format_i18n( $mediaSelected ),
140 + wp_kses_post( $errorLogDisclaimer )
141 + )
142 + );
119 143 }
120 144 }
121 145 }
122 146
@@ -161,8 +185,16 @@
161 185 load_plugin_textdomain('auto-alt-text', false, AATXT_LANGUAGES_RELATIVE_PATH);
162 186 }
163 187
164 188 /**
189 + * @param array<string> $allowedMimeTypes
190 + */
191 + private static function allowedMimeTypesList(array $allowedMimeTypes): string
192 + {
193 + return str_replace('image/', '' , implode(', ', $allowedMimeTypes));
194 + }
195 +
196 + /**
165 197 * @param int $postId
166 198 * @return string
167 199 */
168 200 public static function altText(int $postId): string
@@ -170,12 +202,25 @@
170 202 if (!wp_attachment_is_image($postId)) {
171 203 return '';
172 204 }
173 205
174 - $altText = '';
206 + if (PluginOptions::preserveExistingAltText()) {
207 + $altText = get_post_meta($postId, '_wp_attachment_image_alt', TRUE);
208 +
209 + if (!empty($altText)) {
210 + return $altText;
211 + }
212 + }
213 +
214 + $mimeType = get_post_mime_type($postId);
215 +
175 216 switch (PluginOptions::typology()) {
176 217 case Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE:
177 - // If Azure is selected as alt text generating typology
218 + if (!in_array($mimeType, Constants::AATXT_AZURE_ALLOWED_MIME_TYPES, true)) {
219 + $formats = self::allowedMimeTypesList(Constants::AATXT_AZURE_ALLOWED_MIME_TYPES);
220 + (DBLogger::make())->writeImageLog($postId, "You uploaded an unsupported image. Please make sure your image has of one the following formats: $formats");
221 + return '';
222 + }
178 223 try {
179 224 $altText = (AltTextGeneratorAi::make(AzureComputerVisionCaptionsResponse::make()))->altText($postId);
180 225 } catch (AzureException $e) {
181 226 (DBLogger::make())->writeImageLog($postId, "Azure - " . $e->getMessage());
@@ -180,10 +225,29 @@
180 225 } catch (AzureException $e) {
181 226 (DBLogger::make())->writeImageLog($postId, "Azure - " . $e->getMessage());
182 227 }
183 228 break;
229 + case Constants::AATXT_OPTION_TYPOLOGY_CHOICE_ANTHROPIC:
230 + if (!in_array($mimeType, Constants::AATXT_ANTHROPIC_ALLOWED_MIME_TYPES, true)) {
231 + $formats = self::allowedMimeTypesList(Constants::AATXT_ANTHROPIC_ALLOWED_MIME_TYPES);
232 + (DBLogger::make())->writeImageLog($postId, "You uploaded an unsupported image. Please make sure your image has of one the following formats: $formats");
233 + return '';
234 + }
235 +
236 + try {
237 + $altText = (AltTextGeneratorAi::make(AnthropicResponse::make()))->altText($postId);
238 + } catch (AnthropicException $e) {
239 + $errorMessage = "Anthropic - " . ' - ' . $e->getMessage();
240 + (DBLogger::make())->writeImageLog($postId, $errorMessage);
241 + }
242 +
243 + break;
184 244 case Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI:
185 - // If OpenAI is selected as alt text generating typology
245 + if (!in_array($mimeType, Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES, true)) {
246 + $formats = self::allowedMimeTypesList(Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES);
247 + (DBLogger::make())->writeImageLog($postId, "You uploaded an unsupported image. Please make sure your image has of one the following formats: $formats");
248 + return '';
249 + }
186 250 try {
187 251 $altText = (AltTextGeneratorAi::make(OpenAIVision::make()))->altText($postId);
188 252 } catch (OpenAIException $e) {
189 253 //If vision model fails, try with a fallback model
@@ -214,9 +278,9 @@
214 278 default:
215 279 return '';
216 280 }
217 281
218 - return $altText;
282 + return $altText ?? '';
219 283 }
220 284
221 285 /**
222 286 * @param int $postId