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/Admin/MediaLibrary.php +67 -38 2.3.22.5.2 View file →
@@ -24,8 +24,11 @@
24 24 self::$assetsManager = AssetsManager::make();
25 25
26 26 add_action('admin_enqueue_scripts', [self::$instance, 'enqueue'], 1);
27 27
28 + // Render custom template in media modal
29 + add_action('print_media_templates', [self::$instance, 'renderGenerateButtonTemplate']);
30 +
28 31 // Add button to generate alt text in media library
29 32 add_filter('attachment_fields_to_edit', [self::$instance, 'addGenerateAltTextButton'], 10, 2);
30 33
31 34 // Handle AJAX request to generate alt text
@@ -31,49 +34,52 @@
31 34 // Handle AJAX request to generate alt text
32 35 add_action('wp_ajax_generate_alt_text', [self::$instance, 'generateAltText']);
33 36 }
34 37
35 - public function generateAltText(): void
38 + public function enqueue(): void
36 39 {
37 - check_ajax_referer(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE, 'nonce');
40 + $screen = get_current_screen();
38 41
39 - // Recupera l'ID del media dalla richiesta AJAX
40 - $postId = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
42 + // Load script in Media Library and in any post editing/modal (all CPTs)
43 + if (! $screen || in_array($screen->base, ['upload', 'post'], true)) {
44 + $mediaLibraryJs = self::$assetsManager->getAssetUrl('resources/js/media-library.js', false);
45 + wp_enqueue_script(
46 + Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE,
47 + $mediaLibraryJs,
48 + ['jquery'],
49 + false,
50 + true
51 + );
41 52
42 - if (!$postId) {
43 - wp_send_json_error('Invalid Post ID');
44 - return;
53 + wp_localize_script(
54 + Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE,
55 + 'AATXT',
56 + [
57 + 'altTextNonce' => wp_create_nonce(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE),
58 + 'ajaxUrl' => admin_url('admin-ajax.php'),
59 + ]
60 + );
45 61 }
46 -
47 - // Recupera l'URL del media
48 - $mediaUrl = wp_get_attachment_url($postId);
49 -
50 - if (!$mediaUrl) {
51 - wp_send_json_error('Media not found');
52 - return;
53 - }
54 -
55 - $generatedAltText = Setup::altText($postId);
56 -
57 - wp_send_json_success(['alt_text' => $generatedAltText]);
58 62 }
59 63
60 - public static function enqueue(): void
64 + public function renderGenerateButtonTemplate(): void
61 65 {
62 - $screen = get_current_screen();
63 - if ($screen && ( ($screen->id === 'upload' && $screen->base === 'upload') || ($screen->id === 'attachment')) ) {
64 - $mediaLibraryJs = self::$assetsManager->getAssetUrl('resources/js/media-library.js', false);
65 - wp_enqueue_script(Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE, $mediaLibraryJs, [], false);
66 + ?>
67 + <script type="text/html" id="tmpl-aatxt-generate-alt-text">
68 + <# if ( data.type === 'image' ) { #>
69 + <button class="button aatxt-generate-alt-text" data-post-id="{{ data.id }}">
70 + <?php esc_html_e('Generate Alt Text', 'auto-alt-text'); ?>
71 + </button>
72 + <span class="spinner"></span>
73 + <# } #>
74 + </script>
66 75
67 - wp_localize_script(Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE, 'AATXT', [
68 - 'altTextNonce' => wp_create_nonce(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE),
69 - ]);
70 - }
76 + <?php
71 77 }
72 78
73 - public static function addGenerateAltTextButton(array $form_fields, \WP_Post $post): array
79 + public function addGenerateAltTextButton(array $form_fields, \WP_Post $post): array
74 80 {
75 - if (!wp_attachment_is_image($post->ID)) {
81 + if (! wp_attachment_is_image($post->ID)) {
76 82 return $form_fields;
77 83 }
78 84
79 85 $mimeType = get_post_mime_type($post->ID);
@@ -78,25 +84,48 @@
78 84
79 85 $mimeType = get_post_mime_type($post->ID);
80 86 $altTextGenerationTypology = PluginOptions::typology();
81 87
82 - if($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI && !in_array($mimeType, Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES, true)) {
88 + if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI
89 + && ! in_array($mimeType, Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES, true)
90 + ) {
83 91 return $form_fields;
84 92 }
85 93
86 - if($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE && !in_array($mimeType, Constants::AATXT_AZURE_ALLOWED_MIME_TYPES, true)) {
94 + if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE
95 + && ! in_array($mimeType, Constants::AATXT_AZURE_ALLOWED_MIME_TYPES, true)
96 + ) {
87 97 return $form_fields;
88 98 }
89 99
90 - $form_fields['generate_alt_text'] = array(
100 + $form_fields['generate_alt_text'] = [
91 101 'label' => get_post_mime_type($post->ID),
92 102 'input' => 'html',
93 - 'html' => '<button type="button" class="button" id="generate-alt-text-button" data-post-id="' . $post->ID . '">'
103 + 'html' => '<button type="button" class="button" id="generate-alt-text-button" data-post-id="' . $post->ID . '">'
94 104 . esc_html__('Generate Alt Text', 'auto-alt-text') .
95 - '</button><span id="loading-spinner" class="spinner" style="float:none; margin-left: 5px; display: none;"></span>',
96 -
105 + '</button><span id="loading-spinner" class="spinner" style="float:none; margin-left:5px; display:none;"></span>',
97 106 'helps' => '',
98 - );
107 + ];
99 108
100 109 return $form_fields;
101 110 }
102 -}
111 +
112 + public function generateAltText(): void
113 + {
114 + check_ajax_referer(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE, 'nonce');
115 +
116 + $postId = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
117 + if (! $postId) {
118 + wp_send_json_error('Invalid Post ID');
119 + return;
120 + }
121 +
122 + $mediaUrl = wp_get_attachment_url($postId);
123 + if (! $mediaUrl) {
124 + wp_send_json_error('Media not found');
125 + return;
126 + }
127 +
128 + $generatedAltText = Setup::altText($postId);
129 + wp_send_json_success(['alt_text' => $generatedAltText]);
130 + }
131 +}