PluginProbe
Auto Alt Text / 2.7.0
Auto Alt Text v2.7.0
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 / MediaLibrary.php

MediaLibrary.php in Auto Alt Text 2.7.0, at src/App/Admin/MediaLibrary.php

145 lines 4.6 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\Services\AltTextService;
6 use AATXT\App\Utilities\AssetsManager;
7 use AATXT\Config\Constants;
8
9 class MediaLibrary
10 {
11 /**
12 * Alt text generation service
13 *
14 * @var AltTextService
15 */
16 private $altTextService;
17
18 /**
19 * Assets manager for handling Vite manifests
20 *
21 * @var AssetsManager
22 */
23 private $assetsManager;
24
25 /**
26 * Constructor
27 *
28 * @param AltTextService $altTextService Service for generating alt text
29 * @param AssetsManager $assetsManager Manager for asset URLs
30 */
31 public function __construct(AltTextService $altTextService, AssetsManager $assetsManager)
32 {
33 $this->altTextService = $altTextService;
34 $this->assetsManager = $assetsManager;
35 }
36
37 public function register(): void
38 {
39 add_action('admin_enqueue_scripts', [$this, 'enqueue'], 1);
40
41 // Render custom template in media modal
42 add_action('print_media_templates', [$this, 'renderGenerateButtonTemplate']);
43
44 // Add button to generate alt text in media library
45 add_filter('attachment_fields_to_edit', [$this, 'addGenerateAltTextButton'], 10, 2);
46
47 // Handle AJAX request to generate alt text
48 add_action('wp_ajax_generate_alt_text', [$this, 'generateAltText']);
49 }
50
51 public function enqueue(): void
52 {
53 $screen = get_current_screen();
54
55 // Load script in Media Library and in any post editing/modal (all CPTs)
56 if (! $screen || in_array($screen->base, ['upload', 'post'], true)) {
57 $mediaLibraryJs = $this->assetsManager->getAssetUrl('resources/js/media-library.js', false);
58 wp_enqueue_script(
59 Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE,
60 $mediaLibraryJs,
61 ['jquery'],
62 false,
63 true
64 );
65
66 wp_localize_script(
67 Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE,
68 'AATXT',
69 [
70 'altTextNonce' => wp_create_nonce(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE),
71 'ajaxUrl' => admin_url('admin-ajax.php'),
72 ]
73 );
74 }
75 }
76
77 public function renderGenerateButtonTemplate(): void
78 {
79 ?>
80 <script type="text/html" id="tmpl-aatxt-generate-alt-text">
81 <# if ( data.type === 'image' ) { #>
82 <button class="button aatxt-generate-alt-text" data-post-id="{{ data.id }}">
83 <?php esc_html_e('Generate Alt Text', 'auto-alt-text'); ?>
84 </button>
85 <span class="spinner"></span>
86 <# } #>
87 </script>
88
89 <?php
90 }
91
92 public function addGenerateAltTextButton(array $form_fields, \WP_Post $post): array
93 {
94 if (! wp_attachment_is_image($post->ID)) {
95 return $form_fields;
96 }
97
98 $mimeType = get_post_mime_type($post->ID);
99 $altTextGenerationTypology = PluginOptions::typology();
100
101 if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI
102 && ! in_array($mimeType, Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES, true)
103 ) {
104 return $form_fields;
105 }
106
107 if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE
108 && ! in_array($mimeType, Constants::AATXT_AZURE_ALLOWED_MIME_TYPES, true)
109 ) {
110 return $form_fields;
111 }
112
113 $form_fields['generate_alt_text'] = [
114 'label' => get_post_mime_type($post->ID),
115 'input' => 'html',
116 'html' => '<button type="button" class="button" id="generate-alt-text-button" data-post-id="' . $post->ID . '">'
117 . esc_html__('Generate Alt Text', 'auto-alt-text') .
118 '</button><span id="loading-spinner" class="spinner" style="float:none; margin-left:5px; display:none;"></span>',
119 'helps' => '',
120 ];
121
122 return $form_fields;
123 }
124
125 public function generateAltText(): void
126 {
127 check_ajax_referer(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE, 'nonce');
128
129 $postId = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
130 if (! $postId) {
131 wp_send_json_error('Invalid Post ID');
132 return;
133 }
134
135 $mediaUrl = wp_get_attachment_url($postId);
136 if (! $mediaUrl) {
137 wp_send_json_error('Media not found');
138 return;
139 }
140
141 $generatedAltText = $this->altTextService->generateForAttachment($postId);
142 wp_send_json_success(['alt_text' => $generatedAltText]);
143 }
144 }
145