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

87 lines 2.8 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 // Add button to generate alt text in media library
29 add_filter('attachment_fields_to_edit', [self::$instance, 'addGenerateAltTextButton'], 10, 2);
30
31 // Handle AJAX request to generate alt text
32 add_action('wp_ajax_generate_alt_text', [self::$instance, 'generateAltText']);
33 }
34
35 public function generateAltText(): void
36 {
37 check_ajax_referer(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE, 'nonce');
38
39 // Recupera l'ID del media dalla richiesta AJAX
40 $postId = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
41
42 if (!$postId) {
43 wp_send_json_error('Invalid Post ID');
44 return;
45 }
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 }
59
60 public static function enqueue(): void
61 {
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 wp_localize_script(Constants::AATXT_PLUGIN_MEDIA_LIBRARY_HANDLE, 'AATXT', [
68 'altTextNonce' => wp_create_nonce(Constants::AATXT_AJAX_GENERATE_ALT_TEXT_NONCE),
69 ]);
70 }
71 }
72
73 public static function addGenerateAltTextButton(array $form_fields, \WP_Post $post): array
74 {
75 $form_fields['generate_alt_text'] = array(
76 'label' => '',
77 'input' => 'html',
78 'html' => '<button type="button" class="button" id="generate-alt-text-button" data-post-id="' . $post->ID . '">'
79 . esc_html__('Generate Alt Text', 'auto-alt-text') .
80 '</button><span id="loading-spinner" class="spinner" style="float:none; margin-left: 5px; display: none;"></span>',
81
82 'helps' => '',
83 );
84
85 return $form_fields;
86 }
87 }