| @@ -1,79 +1,100 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AATXT\App\Admin; |
| 4 | 4 | |
| 5 | -use AATXT\App\Setup; | |
| 5 | +use AATXT\App\Services\AltTextService; | |
| 6 | 6 | use AATXT\App\Utilities\AssetsManager; |
| 7 | 7 | use AATXT\Config\Constants; |
| 8 | 8 | |
| 9 | 9 | class MediaLibrary |
| 10 | 10 | { |
| 11 | - private static ?self $instance = null; | |
| 12 | - private static AssetsManager $assetsManager; | |
| 11 | + /** | |
| 12 | + * Alt text generation service | |
| 13 | + * | |
| 14 | + * @var AltTextService | |
| 15 | + */ | |
| 16 | + private $altTextService; | |
| 13 | 17 | |
| 14 | - private function __construct() | |
| 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) | |
| 15 | 32 | { |
| 16 | - // | |
| 33 | + $this->altTextService = $altTextService; | |
| 34 | + $this->assetsManager = $assetsManager; | |
| 17 | 35 | } |
| 18 | 36 | |
| 19 | - public static function register(): void | |
| 37 | + public function register(): void | |
| 20 | 38 | { |
| 21 | - if (is_null(self::$instance)) { | |
| 22 | - self::$instance = new self(); | |
| 23 | - } | |
| 24 | - self::$assetsManager = AssetsManager::make(); | |
| 39 | + add_action('admin_enqueue_scripts', [$this, 'enqueue'], 1); | |
| 25 | 40 | |
| 26 | - add_action('admin_enqueue_scripts', [self::$instance, 'enqueue'], 1); | |
| 41 | + // Render custom template in media modal | |
| 42 | + add_action('print_media_templates', [$this, 'renderGenerateButtonTemplate']); | |
| 27 | 43 | |
| 28 | 44 | // Add button to generate alt text in media library |
| 29 | - add_filter('attachment_fields_to_edit', [self::$instance, 'addGenerateAltTextButton'], 10, 2); | |
| 45 | + add_filter('attachment_fields_to_edit', [$this, 'addGenerateAltTextButton'], 10, 2); | |
| 30 | 46 | |
| 31 | - // Handle AJAX request to generate alt text | |
| 32 | - add_action('wp_ajax_generate_alt_text', [self::$instance, 'generateAltText']); | |
| 47 | + // Register REST route to generate alt text | |
| 48 | + add_action('rest_api_init', [$this, 'registerRestRoutes']); | |
| 33 | 49 | } |
| 34 | 50 | |
| 35 | - public function generateAltText(): void | |
| 51 | + public function enqueue(): void | |
| 36 | 52 | { |
| 37 | - check_ajax_referer(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE, 'nonce'); | |
| 53 | + $screen = get_current_screen(); | |
| 38 | 54 | |
| 39 | - // Recupera l'ID del media dalla richiesta AJAX | |
| 40 | - $postId = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; | |
| 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 | + ); | |
| 41 | 65 | |
| 42 | - if (!$postId) { | |
| 43 | - wp_send_json_error('Invalid Post ID'); | |
| 44 | - return; | |
| 66 | + wp_localize_script( | |
| 67 | + Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE, | |
| 68 | + 'AATXT', | |
| 69 | + [ | |
| 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 | + ), | |
| 74 | + ] | |
| 75 | + ); | |
| 45 | 76 | } |
| 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 | 77 | } |
| 59 | 78 | |
| 60 | - public static function enqueue(): void | |
| 79 | + public function renderGenerateButtonTemplate(): void | |
| 61 | 80 | { |
| 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); | |
| 81 | + ?> | |
| 82 | + <script type="text/html" id="tmpl-aatxt-generate-alt-text"> | |
| 83 | + <# if ( data.type === 'image' ) { #> | |
| 84 | + <button class="button aatxt-generate-alt-text" data-post-id="{{ data.id }}"> | |
| 85 | + <?php esc_html_e('Generate Alt Text', 'auto-alt-text'); ?> | |
| 86 | + </button> | |
| 87 | + <span class="spinner"></span> | |
| 88 | + <# } #> | |
| 89 | + </script> | |
| 66 | 90 | |
| 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 | - } | |
| 91 | + <?php | |
| 71 | 92 | } |
| 72 | 93 | |
| 73 | - public static function addGenerateAltTextButton(array $form_fields, \WP_Post $post): array | |
| 94 | + public function addGenerateAltTextButton(array $form_fields, \WP_Post $post): array | |
| 74 | 95 | { |
| 75 | - if (!wp_attachment_is_image($post->ID)) { | |
| 96 | + if (! wp_attachment_is_image($post->ID)) { | |
| 76 | 97 | return $form_fields; |
| 77 | 98 | } |
| 78 | 99 | |
| 79 | 100 | $mimeType = get_post_mime_type($post->ID); |
| @@ -78,25 +99,101 @@ | ||
| 78 | 99 | |
| 79 | 100 | $mimeType = get_post_mime_type($post->ID); |
| 80 | 101 | $altTextGenerationTypology = PluginOptions::typology(); |
| 81 | 102 | |
| 82 | - if($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI && !in_array($mimeType, Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES, true)) { | |
| 103 | + if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI | |
| 104 | + && ! in_array($mimeType, Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES, true) | |
| 105 | + ) { | |
| 83 | 106 | return $form_fields; |
| 84 | 107 | } |
| 85 | 108 | |
| 86 | - if($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE && !in_array($mimeType, Constants::AATXT_AZURE_ALLOWED_MIME_TYPES, true)) { | |
| 109 | + if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE | |
| 110 | + && ! in_array($mimeType, Constants::AATXT_AZURE_ALLOWED_MIME_TYPES, true) | |
| 111 | + ) { | |
| 87 | 112 | return $form_fields; |
| 88 | 113 | } |
| 89 | 114 | |
| 90 | - $form_fields['generate_alt_text'] = array( | |
| 115 | + $form_fields['generate_alt_text'] = [ | |
| 91 | 116 | 'label' => get_post_mime_type($post->ID), |
| 92 | 117 | 'input' => 'html', |
| 93 | - 'html' => '<button type="button" class="button" id="generate-alt-text-button" data-post-id="' . $post->ID . '">' | |
| 118 | + 'html' => '<button type="button" class="button" id="generate-alt-text-button" data-post-id="' . $post->ID . '">' | |
| 94 | 119 | . 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>', | |
| 120 | + '</button><span id="loading-spinner" class="spinner" style="float:none; margin-left:5px; display:none;"></span>', | |
| 121 | + 'helps' => '', | |
| 122 | + ]; | |
| 96 | 123 | |
| 97 | - 'helps' => '', | |
| 124 | + return $form_fields; | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Register the REST API routes handled by this class. | |
| 129 | + * | |
| 130 | + * @return void | |
| 131 | + */ | |
| 132 | + public function registerRestRoutes(): void | |
| 133 | + { | |
| 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 | + ] | |
| 98 | 152 | ); |
| 153 | + } | |
| 99 | 154 | |
| 100 | - return $form_fields; | |
| 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); | |
| 101 | 166 | } |
| 102 | -} | |
| 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 | + ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + $mediaUrl = wp_get_attachment_url($postId); | |
| 187 | + if (! $mediaUrl) { | |
| 188 | + return new \WP_Error( | |
| 189 | + 'aatxt_media_not_found', | |
| 190 | + __('Media not found.', 'auto-alt-text'), | |
| 191 | + ['status' => 404] | |
| 192 | + ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + $generatedAltText = $this->altTextService->generateForAttachment($postId); | |
| 196 | + | |
| 197 | + return new \WP_REST_Response(['alt_text' => $generatedAltText], 200); | |
| 198 | + } | |
| 199 | +} | |