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 +29 -97 3.0.32.5.2 View file →
@@ -1,52 +1,39 @@
1 1 <?php
2 2
3 3 namespace AATXT\App\Admin;
4 4
5 -use AATXT\App\Services\AltTextService;
5 +use AATXT\App\Setup;
6 6 use AATXT\App\Utilities\AssetsManager;
7 7 use AATXT\Config\Constants;
8 8
9 9 class MediaLibrary
10 10 {
11 - /**
12 - * Alt text generation service
13 - *
14 - * @var AltTextService
15 - */
16 - private $altTextService;
11 + private static ?self $instance = null;
12 + private static AssetsManager $assetsManager;
17 13
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)
14 + private function __construct()
32 15 {
33 - $this->altTextService = $altTextService;
34 - $this->assetsManager = $assetsManager;
16 + //
35 17 }
36 18
37 - public function register(): void
19 + public static function register(): void
38 20 {
39 - add_action('admin_enqueue_scripts', [$this, 'enqueue'], 1);
21 + if (is_null(self::$instance)) {
22 + self::$instance = new self();
23 + }
24 + self::$assetsManager = AssetsManager::make();
40 25
26 + add_action('admin_enqueue_scripts', [self::$instance, 'enqueue'], 1);
27 +
41 28 // Render custom template in media modal
42 - add_action('print_media_templates', [$this, 'renderGenerateButtonTemplate']);
29 + add_action('print_media_templates', [self::$instance, 'renderGenerateButtonTemplate']);
43 30
44 31 // Add button to generate alt text in media library
45 - add_filter('attachment_fields_to_edit', [$this, 'addGenerateAltTextButton'], 10, 2);
32 + add_filter('attachment_fields_to_edit', [self::$instance, 'addGenerateAltTextButton'], 10, 2);
46 33
47 - // Register REST route to generate alt text
48 - add_action('rest_api_init', [$this, 'registerRestRoutes']);
34 + // Handle AJAX request to generate alt text
35 + add_action('wp_ajax_generate_alt_text', [self::$instance, 'generateAltText']);
49 36 }
50 37
51 38 public function enqueue(): void
52 39 {
@@ -53,9 +40,9 @@
53 40 $screen = get_current_screen();
54 41
55 42 // Load script in Media Library and in any post editing/modal (all CPTs)
56 43 if (! $screen || in_array($screen->base, ['upload', 'post'], true)) {
57 - $mediaLibraryJs = $this->assetsManager->getAssetUrl('resources/js/media-library.js', false);
44 + $mediaLibraryJs = self::$assetsManager->getAssetUrl('resources/js/media-library.js', false);
58 45 wp_enqueue_script(
59 46 Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE,
60 47 $mediaLibraryJs,
61 48 ['jquery'],
@@ -66,12 +53,10 @@
66 53 wp_localize_script(
67 54 Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE,
68 55 'AATXT',
69 56 [
70 - 'restNonce' => wp_create_nonce('wp_rest'),
71 - 'restUrl' => esc_url_raw(
72 - rest_url(Constants::AATXT_REST_NAMESPACE . Constants::AATXT_REST_ROUTE_GENERATE_ALT_TEXT)
73 - ),
57 + 'altTextNonce' => wp_create_nonce(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE),
58 + 'ajaxUrl' => admin_url('admin-ajax.php'),
74 59 ]
75 60 );
76 61 }
77 62 }
@@ -123,77 +108,24 @@
123 108
124 109 return $form_fields;
125 110 }
126 111
127 - /**
128 - * Register the REST API routes handled by this class.
129 - *
130 - * @return void
131 - */
132 - public function registerRestRoutes(): void
112 + public function generateAltText(): void
133 113 {
134 - register_rest_route(
135 - Constants::AATXT_REST_NAMESPACE,
136 - Constants::AATXT_REST_ROUTE_GENERATE_ALT_TEXT,
137 - [
138 - 'methods' => 'POST',
139 - 'callback' => [$this, 'generateAltText'],
140 - 'permission_callback' => [$this, 'canGenerateAltText'],
141 - 'args' => [
142 - 'post_id' => [
143 - 'required' => true,
144 - 'type' => 'integer',
145 - 'sanitize_callback' => 'absint',
146 - 'validate_callback' => static function ($value): bool {
147 - return absint($value) > 0;
148 - },
149 - ],
150 - ],
151 - ]
152 - );
153 - }
114 + check_ajax_referer(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE, 'nonce');
154 115
155 - /**
156 - * Permission check for the alt text generation endpoint.
157 - *
158 - * @param \WP_REST_Request<array<string, mixed>> $request The REST request.
159 - * @return bool True if the current user may generate alt text.
160 - */
161 - public function canGenerateAltText(\WP_REST_Request $request): bool
162 - {
163 - $postId = absint($request->get_param('post_id'));
164 -
165 - return $postId > 0 && current_user_can('edit_post', $postId);
166 - }
167 -
168 - /**
169 - * Generate alt text for an attachment.
170 - *
171 - * @param \WP_REST_Request<array<string, mixed>> $request The REST request.
172 - * @return \WP_REST_Response|\WP_Error The generated alt text or an error.
173 - */
174 - public function generateAltText(\WP_REST_Request $request)
175 - {
176 - $postId = absint($request->get_param('post_id'));
177 -
178 - if (! wp_attachment_is_image($postId)) {
179 - return new \WP_Error(
180 - 'aatxt_invalid_attachment',
181 - __('The provided ID is not an image attachment.', 'auto-alt-text'),
182 - ['status' => 404]
183 - );
116 + $postId = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
117 + if (! $postId) {
118 + wp_send_json_error('Invalid Post ID');
119 + return;
184 120 }
185 121
186 122 $mediaUrl = wp_get_attachment_url($postId);
187 123 if (! $mediaUrl) {
188 - return new \WP_Error(
189 - 'aatxt_media_not_found',
190 - __('Media not found.', 'auto-alt-text'),
191 - ['status' => 404]
192 - );
124 + wp_send_json_error('Media not found');
125 + return;
193 126 }
194 127
195 - $generatedAltText = $this->altTextService->generateForAttachment($postId);
196 -
197 - return new \WP_REST_Response(['alt_text' => $generatedAltText], 200);
128 + $generatedAltText = Setup::altText($postId);
129 + wp_send_json_success(['alt_text' => $generatedAltText]);
198 130 }
199 131 }