'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, 'orderby' => 'ID', 'order' => 'DESC', ); $args = wp_parse_args($args_override, $default_args); return get_posts($args); } public function alt_description_page() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.', 'ada-compliance-guardian')); } ?>

'; printf( wp_kses_post(__('The Plan API Key is not configured. This is required to track usage. Please visit the Initial Setup page to enter your key.', 'ada-compliance-guardian')), esc_url($setup_page_url) ); echo '

'; return; } $api_key = get_option(self::OPTION_API_KEY, ''); if (empty($api_key)) { echo '

'; printf( wp_kses_post(__('The Image Alt Text Generation API Key is not configured. This plugin requires a key to function. Please visit the Initial Setup page to enter your API key.', 'ada-compliance-guardian')), esc_url($setup_page_url) ); echo '

'; return; } ?>

'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'OR', array('key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS'), array('key' => '_wp_attachment_image_alt', 'value' => '', 'compare' => '=') ) ); $missing_alt_images_query = new WP_Query($args_missing_count); $missing_alt_images_count = $missing_alt_images_query->post_count; ?>

get_all_media_images(); if (!empty($all_images)) { echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ($all_images as $image) { $image_id = $image->ID; $image_url = wp_get_attachment_url($image_id); $image_thumb_url_array = wp_get_attachment_image_src($image_id, 'thumbnail'); $image_thumb_url = $image_thumb_url_array ? $image_thumb_url_array[0] : ''; $image_title = get_the_title($image_id); $current_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); $file_path = get_attached_file($image_id); $filename = $file_path ? basename($file_path) : __('N/A', 'ada-compliance-guardian'); echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html__('Image', 'ada-compliance-guardian') . '' . esc_html__('Details', 'ada-compliance-guardian') . '' . esc_html__('Image Alt Text & Actions', 'ada-compliance-guardian') . '
'; if ($image_thumb_url) { echo '' . esc_attr($current_alt) . ''; } else { echo '' . esc_html__('No thumb', 'ada-compliance-guardian') . ''; } echo ''; echo '' . esc_html($image_title ? $image_title : __('Image ID:', 'ada-compliance-guardian') . ' ' . $image_id) . '
'; echo '' . esc_html($filename) . '
'; echo '' . esc_html__('Current Alt: ', 'ada-compliance-guardian') . '' . esc_html($current_alt) . ''; if (empty($current_alt)) { echo '
' . esc_html__('Alt text is missing!', 'ada-compliance-guardian') . ''; } else { echo '
' . esc_html__('Alt text present.', 'ada-compliance-guardian') . ''; } echo '
'; echo ''; echo ''; echo '
'; } else { echo '

' . esc_html__('No images found in the media library.', 'ada-compliance-guardian') . '

'; } ?>
visit the Initial Setup page to enter your API key.', 'ada-compliance-guardian')), esc_url(admin_url('admin.php?page=ada-compliance-guardian-setup')) ); if (empty($api_key)) { return array('error' => __('Image Alt Text Generation API Key is not configured. ', 'ada-compliance-guardian') . $setup_page_url_html, 'error_code' => 400); } if (strpos($api_key, 'AIza') !== 0) { return array('error' => __('The configured Image Alt Text Generation API Key appears to be invalid. ', 'ada-compliance-guardian') . $setup_page_url_html, 'error_code' => 400); } $gemini_api_url = 'https://generativelanguage.googleapis.com/v1beta/models/' . self::GEMINI_MODEL . ':generateContent?key=' . $api_key; $request_body = array( 'contents' => array(array('parts' => array(array('text' => $prompt_text), array('inline_data' => array('mime_type' => $image_mime_type, 'data' => $base64_image_data))))), 'generationConfig' => array('temperature' => 0.45, 'topK' => 32, 'topP' => 1, 'maxOutputTokens' => 150), 'safetySettings' => array( array('category' => 'HARM_CATEGORY_HARASSMENT', 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'), array('category' => 'HARM_CATEGORY_HATE_SPEECH', 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'), array('category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'), array('category' => 'HARM_CATEGORY_DANGEROUS_CONTENT', 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'), ) ); $api_response = wp_remote_post($gemini_api_url, array( 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json'), 'body' => wp_json_encode($request_body), 'timeout' => 45, )); if (is_wp_error($api_response)) { return array('error' => __('Image Alt Text Generation API request failed: ', 'ada-compliance-guardian') . $api_response->get_error_message(), 'error_code' => 500); } $response_body = wp_remote_retrieve_body($api_response); $response_code = wp_remote_retrieve_response_code($api_response); $decoded_response = json_decode($response_body, true); if ($response_code !== 200 || !$decoded_response) { $api_error_detail = isset($decoded_response['error']['message']) ? $decoded_response['error']['message'] : substr($response_body, 0, 500); if ($response_code === 400 && strpos($api_error_detail, 'API key not valid') !== false) { return array('error' => __('The Image Alt Text Generation API Key is not valid. ', 'ada-compliance-guardian') . $setup_page_url_html . '
Details: ' . esc_html($api_error_detail) . '', 'error_code' => $response_code); } return array('error' => sprintf(__('Image Alt Text Generation API error (HTTP %1$s): %2$s', 'ada-compliance-guardian'), $response_code, esc_html($api_error_detail)), 'error_code' => $response_code); } if (isset($decoded_response['candidates'][0]['content']['parts'][0]['text'])) { return array('alt_text' => sanitize_text_field(trim($decoded_response['candidates'][0]['content']['parts'][0]['text']))); } elseif (isset($decoded_response['candidates'][0]['finishReason']) && $decoded_response['candidates'][0]['finishReason'] !== 'STOP') { $reason = $decoded_response['candidates'][0]['finishReason']; $safety_ratings_info = ''; if (isset($decoded_response['candidates'][0]['safetyRatings'])) { foreach ($decoded_response['candidates'][0]['safetyRatings'] as $rating) { if ($rating['probability'] !== 'NEGLIGIBLE') { $safety_ratings_info .= sprintf(esc_html__('Safety concern: %1$s (%2$s). ', 'ada-compliance-guardian'), $rating['category'], $rating['probability']); } } } return array('error' => sprintf(esc_html__('Generation stopped. Reason: %1$s. %2$s', 'ada-compliance-guardian'), esc_html($reason), $safety_ratings_info) . ' Raw: ' . esc_html(substr($response_body,0,200)), 'error_code' => 500); } elseif (isset($decoded_response['promptFeedback']['blockReason'])) { return array('error' => sprintf(esc_html__('Prompt blocked. Reason: %s.', 'ada-compliance-guardian'), esc_html($decoded_response['promptFeedback']['blockReason'])), 'error_code' => 400); } else { return array('error' => __('Could not extract text. Check API console.', 'ada-compliance-guardian') . ' Raw: ' . esc_html(substr($response_body,0,200)), 'error_code' => 500); } } public function handle_generate_alt_text_ajax() { check_ajax_referer('adacogu_gemini_nonce', 'nonce_generate'); if (!current_user_can('edit_posts')) { wp_send_json_error(__('Permission denied to generate alt text.', 'ada-compliance-guardian'), 403); return; } $validation = $this->_validate_api_usage('image_alt'); if (!$validation['success']) { wp_send_json_error($validation['message'], 403); return; } $api_key = get_option(self::OPTION_API_KEY, ''); if (empty($api_key)) { $setup_page_url = admin_url('admin.php?page=ada-compliance-guardian-setup'); $error_message = sprintf( wp_kses_post(__('The Image Alt Text Generation API Key is not configured. Please go to the Initial Setup page to enter your API key.', 'ada-compliance-guardian')), esc_url($setup_page_url) ); wp_send_json_error($error_message, 400); return; } $image_id = isset($_POST['image_id']) ? intval($_POST['image_id']) : 0; $image_url = isset($_POST['image_url']) ? esc_url_raw($_POST['image_url']) : ''; $generation_mode = isset($_POST['generation_mode']) ? sanitize_text_field($_POST['generation_mode']) : 'default'; $previous_suggestion_from_js = isset($_POST['previous_alt_text']) ? sanitize_text_field(wp_unslash($_POST['previous_alt_text'])) : ''; $is_regenerate_suggestion_click = !empty($previous_suggestion_from_js); if (isset($_POST['current_wp_alt'])) { $current_wp_alt_for_prompt = sanitize_text_field(wp_unslash($_POST['current_wp_alt'])); } else { $current_wp_alt_for_prompt = get_post_meta($image_id, '_wp_attachment_image_alt', true); } if (!$image_id || empty($image_url)) { wp_send_json_error(__('Missing image data.', 'ada-compliance-guardian'), 400); return; } if (get_post_type($image_id) !== 'attachment') { wp_send_json_error(__('Invalid Post ID. Not an attachment.', 'ada-compliance-guardian'), 400); return; } $prompt_text = ''; if ($generation_mode === 'seo_ada') { $prompt_text = __('Generate an alt text for this image that is both highly descriptive for ADA compliance (for visually impaired users) and optimized for SEO. Subtly incorporate relevant keywords if identifiable and appropriate, but ensure the primary focus is a clear, accurate, and comprehensive description of the image\'s content and context.', 'ada-compliance-guardian'); if (!empty($current_wp_alt_for_prompt)) { $prompt_text .= ' ' . sprintf(__('The current alt text is: "%s". Use this as a reference to improve upon, or create a new one if significantly better.', 'ada-compliance-guardian'), $current_wp_alt_for_prompt); } $prompt_text .= ' ' . __('Do not include phrases like "alt text:", "image of", "photo of", or "description:". Just provide the optimized alt text itself. Make it around 100-125 characters if possible, but prioritize description quality.', 'ada-compliance-guardian'); } elseif ($is_regenerate_suggestion_click) { $prompt_text = __('Provide a concise and descriptive alt text for this image, suitable for visually impaired users according to ADA guidelines. Focus on the main subject, its context, and any important details.', 'ada-compliance-guardian'); $prompt_text .= ' ' . sprintf(__('Please make it different from the previous suggestion: "%s".', 'ada-compliance-guardian'), $previous_suggestion_from_js); $prompt_text .= ' ' . __('Do not include phrases like "alt text:", "image of", "photo of", or "description:". Just provide the description itself. Aim for 100-125 characters.', 'ada-compliance-guardian'); } elseif (!empty($current_wp_alt_for_prompt)) { $prompt_text = sprintf( __('The current alt text for this image is: "%s". Please provide an improved and more detailed descriptive alt text for this image, specifically enhancing it for ADA compliance. Elaborate on the main subject, its context, and any important visual details that a visually impaired user would need to understand the image thoroughly.', 'ada-compliance-guardian'), $current_wp_alt_for_prompt ); $prompt_text .= ' ' . __('Do not include phrases like "alt text:", "image of", "photo of", or "description:". Just provide the new, more descriptive alt text. Aim for 100-125 characters.', 'ada-compliance-guardian'); } else { $prompt_text = __('Provide a concise and descriptive alt text for this image, suitable for visually impaired users according to ADA guidelines. Focus on the main subject, its context, and any important details.', 'ada-compliance-guardian'); $prompt_text .= ' ' . __('Do not include phrases like "alt text:", "image of", "photo of", or "description:". Just provide the description itself. Aim for 100-125 characters.', 'ada-compliance-guardian'); } $response = wp_remote_get($image_url, array('timeout' => 20)); if (is_wp_error($response)) { wp_send_json_error(__('Failed to fetch image: ', 'ada-compliance-guardian') . $response->get_error_message(), 500); return; } $image_data = wp_remote_retrieve_body($response); $image_mime_type = wp_remote_retrieve_header($response, 'content-type'); if (empty($image_data) || strpos($image_mime_type, 'image/') !== 0) { wp_send_json_error(__('Failed to retrieve valid image data or unsupported MIME type: ', 'ada-compliance-guardian') . esc_html($image_mime_type), 500); return; } $base64_image_data = base64_encode($image_data); $gemini_result = $this->_call_gemini_api($image_url, $image_mime_type, $base64_image_data, $prompt_text); if (isset($gemini_result['error'])) { wp_send_json_error($gemini_result['error'], isset($gemini_result['error_code']) ? $gemini_result['error_code'] : 500); } else { wp_send_json_success(array('alt_text' => $gemini_result['alt_text'], 'image_id' => $image_id)); } } public function handle_new_attachment_alt_generation($attachment_id) { $is_auto_alt_enabled = (get_option(self::OPTION_AUTO_ALT_ON_UPLOAD, '0') === '1'); if (!$is_auto_alt_enabled) { return; } $api_key = get_option(self::OPTION_API_KEY, ''); if (empty($api_key)) { error_log('ADA Guardian: Image Alt Text Generation API Key missing, cannot auto-generate alt text for attachment ID ' . $attachment_id); return; } $mime_type = get_post_mime_type($attachment_id); if (!$mime_type || strpos($mime_type, 'image/') !== 0) { return; } $validation = $this->_validate_api_usage('image_alt'); if (!$validation['success']) { $error_message = isset($validation['message']) ? wp_strip_all_tags($validation['message']) : 'Validation failed for auto-generation.'; error_log('ADA Guardian: Usage validation failed for attachment ID ' . $attachment_id . '. Reason: ' . $error_message); update_post_meta($attachment_id, '_adacogu_auto_alt_error', 'Validation Failed: ' . $error_message); return; } $existing_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); if (!empty($existing_alt)) { return; } $image_url = wp_get_attachment_url($attachment_id); $file_path = get_attached_file($attachment_id); if (!$file_path || !file_exists($file_path)) { error_log('ADA Guardian: File path not found for attachment ID ' . $attachment_id); return; } $max_file_size_bytes = 3.5 * 1024 * 1024; // 3.5 MB if (filesize($file_path) > $max_file_size_bytes) { error_log('ADA Guardian: Image file too large (' . filesize($file_path) . ' bytes) for auto alt text generation. Attachment ID: ' . $attachment_id); update_post_meta($attachment_id, '_adacogu_auto_alt_error', 'Image too large for auto-generation.'); return; } $image_file_data = @file_get_contents($file_path); if ($image_file_data === false) { error_log('ADA Guardian: Could not read file contents for attachment ID ' . $attachment_id); return; } $base64_image_data = base64_encode($image_file_data); $prompt_text = __('Provide a concise and descriptive alt text for this image, suitable for visually impaired users according to ADA guidelines. Focus on the main subject, its context, and any important details. Do not include phrases like "alt text:", "image of", "photo of", or "description:". Just provide the description itself. Aim for 100-125 characters.', 'ada-compliance-guardian'); $gemini_result = $this->_call_gemini_api($image_url, $mime_type, $base64_image_data, $prompt_text); if (isset($gemini_result['alt_text'])) { $generated_alt = sanitize_text_field(trim($gemini_result['alt_text'])); if (!empty($generated_alt)) { update_post_meta($attachment_id, '_wp_attachment_image_alt', $generated_alt); update_post_meta($attachment_id, '_adacogu_auto_alt_generated', '1'); } } else { $error_message = isset($gemini_result['error']) ? $gemini_result['error'] : __('Unknown API error during auto-generation.', 'ada-compliance-guardian'); error_log('ADA Guardian: API error for attachment ID ' . $attachment_id . ': ' . (is_string($error_message) ? $error_message : wp_json_encode($error_message)) ); update_post_meta($attachment_id, '_adacogu_auto_alt_error', is_string($error_message) ? wp_strip_all_tags($error_message) : 'API Error' ); } } public function handle_get_missing_alt_image_ids_ajax() { check_ajax_referer('adacogu_get_missing_ids_nonce', 'nonce_get_missing_ids'); if (!current_user_can('manage_options')) { wp_send_json_error(__('Permission denied.', 'ada-compliance-guardian'), 403); return; } $args_missing = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'OR', array('key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS'), array('key' => '_wp_attachment_image_alt', 'value' => '', 'compare' => '=') ) ); $query = new WP_Query($args_missing); $missing_images_posts = $query->get_posts(); $images_data = array(); if (empty($missing_images_posts)) { wp_send_json_success(array('images' => [])); return; } foreach ($missing_images_posts as $image_post) { $image_id = $image_post->ID; $image_url = wp_get_attachment_url($image_id); $file_path = get_attached_file($image_id); $filename = $file_path ? basename($file_path) : __('ID: ', 'ada-compliance-guardian') . $image_id; if ($image_url) { $images_data[] = array( 'image_id' => $image_id, 'image_url' => $image_url, 'filename' => $filename ); } } wp_send_json_success(array('images' => $images_data)); } public function handle_get_all_image_data_for_seo_ada_ajax() { check_ajax_referer('adacogu_get_all_for_seo_ada_nonce', 'nonce_get_all_for_seo_ada'); if (!current_user_can('manage_options')) { wp_send_json_error(__('Permission denied.', 'ada-compliance-guardian'), 403); return; } $all_image_posts = $this->get_all_media_images(); $images_data = array(); if (empty($all_image_posts)) { wp_send_json_success(array('images' => [])); return; } foreach ($all_image_posts as $image_post) { $image_id = $image_post->ID; $image_url = wp_get_attachment_url($image_id); $file_path = get_attached_file($image_id); $filename = $file_path ? basename($file_path) : __('ID: ', 'ada-compliance-guardian') . $image_id; $current_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); if ($image_url) { $images_data[] = array( 'image_id' => $image_id, 'image_url' => $image_url, 'filename' => $filename, 'current_alt'=> $current_alt ); } } wp_send_json_success(array('images' => $images_data)); } public function handle_generate_missing_ada_alt_text_ajax() { check_ajax_referer('adacogu_gemini_missing_nonce', 'nonce_generate_missing'); if (!current_user_can('manage_options')) { wp_send_json_error(__('Permission denied.', 'ada-compliance-guardian'), 403); return; } $api_key = get_option(self::OPTION_API_KEY, ''); if (empty($api_key)) { $setup_page_url = admin_url('admin.php?page=ada-compliance-guardian-setup'); $error_message = sprintf( wp_kses_post(__('The Image Alt Text Generation API Key is not configured. Please go to the Initial Setup page to enter your API key.', 'ada-compliance-guardian')), esc_url($setup_page_url) ); wp_send_json_error($error_message, 400); return; } $args_missing = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'OR', array('key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS'), array('key' => '_wp_attachment_image_alt', 'value' => '', 'compare' => '=') ) ); $query = new WP_Query($args_missing); $missing_images = $query->get_posts(); if (empty($missing_images)) { wp_send_json_success(array('message' => __('No images found missing alt text.', 'ada-compliance-guardian'), 'results' => [])); return; } $results = []; $prompt_base = __('Provide a concise and descriptive alt text for this image, suitable for visually impaired users according to ADA guidelines. Focus on the main subject, its context, and any important details. Do not include phrases like "alt text:", "image of", "photo of", or "description:". Just provide the description itself. Aim for 100-125 characters.', 'ada-compliance-guardian'); foreach ($missing_images as $image) { $image_id = $image->ID; $image_url = wp_get_attachment_url($image_id); if (!$image_url) { $results[] = array('image_id' => $image_id, 'error' => __('Could not retrieve image URL.', 'ada-compliance-guardian')); continue; } $img_response = wp_remote_get($image_url, array('timeout' => 15)); if (is_wp_error($img_response) || wp_remote_retrieve_response_code($img_response) !== 200) { $results[] = array('image_id' => $image_id, 'error' => __('Failed to fetch image.', 'ada-compliance-guardian')); continue; } $image_data = wp_remote_retrieve_body($img_response); $image_mime_type = wp_remote_retrieve_header($img_response, 'content-type'); if (empty($image_data) || strpos($image_mime_type, 'image/') !== 0) { $results[] = array('image_id' => $image_id, 'error' => __('Invalid image data.', 'ada-compliance-guardian')); continue; } $base64_image_data = base64_encode($image_data); $gemini_result = $this->_call_gemini_api($image_url, $image_mime_type, $base64_image_data, $prompt_base); if (isset($gemini_result['alt_text'])) { $results[] = array('image_id' => $image_id, 'alt_text' => $gemini_result['alt_text']); } else { $results[] = array('image_id' => $image_id, 'error' => isset($gemini_result['error']) ? $gemini_result['error'] : __('Unknown error from API.', 'ada-compliance-guardian')); } } wp_send_json_success(array('results' => $results)); } public function handle_save_all_alt_text_ajax() { check_ajax_referer('adacogu_save_all_alt_nonce', 'nonce_save_all'); if (!current_user_can('edit_others_posts')) { wp_send_json_error(__('Permission denied.', 'ada-compliance-guardian'), 403); return; } $alt_texts_to_save_json = isset($_POST['alt_texts']) ? wp_unslash($_POST['alt_texts']) : '[]'; $alt_texts_to_save = json_decode($alt_texts_to_save_json, true); if (json_last_error() !== JSON_ERROR_NONE || !is_array($alt_texts_to_save)) { wp_send_json_error(__('No alt texts provided or data is malformed.', 'ada-compliance-guardian'), 400); return; } // **SANITIZATION FIX** $sanitized_alt_texts = []; foreach ($alt_texts_to_save as $item) { $sanitized_item = []; $sanitized_item['image_id'] = isset($item['image_id']) ? intval($item['image_id']) : 0; $sanitized_item['alt_text'] = isset($item['alt_text']) ? sanitize_text_field($item['alt_text']) : ''; $sanitized_alt_texts[] = $sanitized_item; } $alt_texts_to_save = $sanitized_alt_texts; $save_statuses = []; foreach ($alt_texts_to_save as $item) { $image_id = $item['image_id']; $alt_text = $item['alt_text']; if (!$image_id || get_post_type($image_id) !== 'attachment') { $save_statuses[] = array('image_id' => $image_id, 'status' => 'error', 'message' => __('Invalid image ID.', 'ada-compliance-guardian'), 'new_alt_text' => $alt_text); continue; } if (!current_user_can('edit_post', $image_id)) { $save_statuses[] = array('image_id' => $image_id, 'status' => 'error', 'message' => __('Permission denied to edit this item.', 'ada-compliance-guardian'), 'new_alt_text' => $alt_text); continue; } $current_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); if ($current_alt === $alt_text) { $save_statuses[] = array('image_id' => $image_id, 'status' => 'unchanged', 'new_alt_text' => $alt_text, 'message' => __('Alt text is already set to this value.', 'ada-compliance-guardian')); } elseif (update_post_meta($image_id, '_wp_attachment_image_alt', $alt_text)) { $save_statuses[] = array('image_id' => $image_id, 'status' => 'saved', 'new_alt_text' => $alt_text, 'message' => __('Alt text saved successfully!', 'ada-compliance-guardian')); } else { $save_statuses[] = array('image_id' => $image_id, 'status' => 'error', 'message' => __('Failed to update in database. The value might be the same or an error occurred.', 'ada-compliance-guardian'), 'new_alt_text' => $alt_text); } } wp_send_json_success(array('results' => $save_statuses)); } public function handle_save_alt_text_ajax() { check_ajax_referer('adacogu_save_alt_nonce', 'nonce_save'); $image_id = isset($_POST['image_id']) ? intval($_POST['image_id']) : 0; $alt_text = isset($_POST['alt_text']) ? sanitize_text_field(wp_unslash($_POST['alt_text'])) : ''; if (!$image_id) { wp_send_json_error(__('Missing image ID.', 'ada-compliance-guardian'), 400); return; } if (!current_user_can('edit_post', $image_id)) { wp_send_json_error(__('Permission denied to edit this media item.', 'ada-compliance-guardian'), 403); return; } if (get_post_type($image_id) !== 'attachment') { wp_send_json_error(__('Invalid image ID. Not an attachment.', 'ada-compliance-guardian'), 400); return; } $current_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); if ($current_alt === $alt_text) { wp_send_json_success(array( 'message' => __('Alt text is already set to this value.', 'ada-compliance-guardian'), 'status' => 'unchanged', 'new_alt_text' => $alt_text )); return; } if (update_post_meta($image_id, '_wp_attachment_image_alt', $alt_text)) { wp_send_json_success(array( 'message' => __('Alt text saved successfully!', 'ada-compliance-guardian'), 'status' => 'saved', 'new_alt_text' => $alt_text )); } else { wp_send_json_error(__('Failed to update alt text in the database. The value might be the same or an error occurred.', 'ada-compliance-guardian'), 500); } } }