PluginProbe
Auto Alt Text / 2.5.0
Auto Alt Text v2.5.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.5.0, at src/App/Admin/MediaLibrary.php

132 lines 4.2 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\Setup;
6 use AATXT\App\Utilities\AssetsManager;
7 use AATXT\Config\Constants;
8
9 class MediaLibrary
10 {
11 private static ?self $instance = null;
12 private static AssetsManager $assetsManager;
13
14 private function __construct()
15 {
16 //
17 }
18
19 public static function register(): void
20 {
21 if (is_null(self::$instance)) {
22 self::$instance = new self();
23 }
24 self::$assetsManager = AssetsManager::make();
25
26 add_action('admin_enqueue_scripts', [self::$instance, 'enqueue'], 1);
27
28 // Render custom template in media modal
29 add_action('print_media_templates', [self::$instance, 'renderGenerateButtonTemplate']);
30
31 // Add button to generate alt text in media library
32 add_filter('attachment_fields_to_edit', [self::$instance, 'addGenerateAltTextButton'], 10, 2);
33
34 // Handle AJAX request to generate alt text
35 add_action('wp_ajax_generate_alt_text', [self::$instance, 'generateAltText']);
36 }
37
38 public function enqueue(): void
39 {
40 $screen = get_current_screen();
41
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 );
52
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 );
61 }
62 }
63
64 public function renderGenerateButtonTemplate(): void
65 {
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>
75
76 <?php
77 }
78
79 public function addGenerateAltTextButton(array $form_fields, \WP_Post $post): array
80 {
81 if (! wp_attachment_is_image($post->ID)) {
82 return $form_fields;
83 }
84
85 $mimeType = get_post_mime_type($post->ID);
86 $altTextGenerationTypology = PluginOptions::typology();
87
88 if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_OPENAI
89 && ! in_array($mimeType, Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES, true)
90 ) {
91 return $form_fields;
92 }
93
94 if ($altTextGenerationTypology === Constants::AATXT_OPTION_TYPOLOGY_CHOICE_AZURE
95 && ! in_array($mimeType, Constants::AATXT_AZURE_ALLOWED_MIME_TYPES, true)
96 ) {
97 return $form_fields;
98 }
99
100 $form_fields['generate_alt_text'] = [
101 'label' => get_post_mime_type($post->ID),
102 'input' => 'html',
103 'html' => '<button type="button" class="button" id="generate-alt-text-button" data-post-id="' . $post->ID . '">'
104 . esc_html__('Generate Alt Text', 'auto-alt-text') .
105 '</button><span id="loading-spinner" class="spinner" style="float:none; margin-left:5px; display:none;"></span>',
106 'helps' => '',
107 ];
108
109 return $form_fields;
110 }
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 }
132