mime_is_supported($mime)) { return $metadata; } $existing = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); if ($existing && !AIT_IAT_Settings::get('overwrite_existing_alt')) { return $metadata; } $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0; $json_alt_fields = apply_filters('ait_iat_generate_alt_text', [], $attachment_id, null); if ( ! empty( $json_alt_fields ) ) { update_post_meta($attachment_id, '_wp_attachment_image_alt', $json_alt_fields['alt_text']); do_action('ait_iat_alt_generated', $attachment_id, $json_alt_fields['alt_text']); if ( ! empty( $json_alt_fields['caption'] ) || ! empty( $json_alt_fields['title'] ) || ! empty( $json_alt_fields['description'] ) ) { $args = ['ID' => $attachment_id]; if ( ! empty( $json_alt_fields['title'] ) ) { $args['post_title'] = $json_alt_fields['title']; } if ( ! empty( $json_alt_fields['caption'] ) ) { $args['post_excerpt'] = $json_alt_fields['caption']; } if ( ! empty( $json_alt_fields['description'] ) ) { $args['post_content'] = $json_alt_fields['description']; } wp_update_post( $args ); if ( ! empty( $json_alt_fields['filename'] ) ) { AIT_IAT_Alt_Generator::maybe_rename_attachment_file( $attachment_id, $json_alt_fields['filename'] ); } } } return $metadata; } public function add_regenerate_alt_button($form_fields, $post) { $mime = get_post_mime_type($post->ID); if (!$this->mime_is_supported($mime)) { return $form_fields; } $form_fields['ait_iat_regen_alt'] = [ 'label' => __('AI: Regenerate Alt Text', 'ai-image-alt-text'), 'input' => 'html', 'html' => '', 'helps' => __('Use OpenAI to regenerate a new alt text for this image.', 'ai-image-alt-text'), ]; return $form_fields; } public static function ajax_regenerate_alt() { if ( ! current_user_can('upload_files') || ! check_ajax_referer('ait_iat_regen_alt_nonce', 'security', false) ) { wp_send_json_error(['message' => 'Unauthorized']); } $attachment_id = isset( $_POST['attachment_id'] ) ? absint($_POST['attachment_id']) : 0; if (! $attachment_id || get_post_type($attachment_id) !== 'attachment') { wp_send_json_error(['message' => 'Invalid attachment']); } $mime = get_post_mime_type( $attachment_id ); if ( ! self::mime_is_supported( $mime ) ) { wp_send_json_error(['message' => 'Mime type not supported']); } $bypass = function($allowed, $id) {return true;}; add_filter('ait_iat_should_generate', $bypass, 9999, 2); $json_alt_fields = AIT_IAT_Alt_Generator::generate_alt_text('', $attachment_id); remove_filter('ait_iat_should_generate', $bypass, 9999, 2); if ( ! empty( $json_alt_fields ) && ! empty( $json_alt_fields['alt_text'] ) ) { update_post_meta($attachment_id, '_wp_attachment_image_alt', $json_alt_fields['alt_text']); do_action('ait_iat_alt_generated', $attachment_id, $json_alt_fields['alt_text']); if ( ! empty( $json_alt_fields['caption'] ) || ! empty( $json_alt_fields['title'] ) || ! empty( $json_alt_fields['description'] ) ) { $args = ['ID' => $attachment_id]; if ( ! empty( $json_alt_fields['title'] ) ) { $args['post_title'] = $json_alt_fields['title']; } if ( ! empty( $json_alt_fields['caption'] ) ) { $args['post_excerpt'] = $json_alt_fields['caption']; } if ( ! empty( $json_alt_fields['description'] ) ) { $args['post_content'] = $json_alt_fields['description']; } wp_update_post( $args ); if ( ! empty( $json_alt_fields['filename'] ) ) { $filename = AIT_IAT_Alt_Generator::maybe_rename_attachment_file( $attachment_id, $json_alt_fields['filename'] ); $json_alt_fields['filename'] = $filename; } } wp_send_json_success($json_alt_fields); } else { wp_send_json_error(['message' => 'No alt text generated']); } } public static function mime_is_supported($mime, $supported_mime_types = []) { $supported_mime_types = ! empty( $supported_mime_types ) ? $supported_mime_types : AIT_IAT_Settings::get_supported_mime_types(); foreach ($supported_mime_types as $supported) { if ($supported === $mime) return true; if (strpos($supported, '/*') !== false) { $prefix = rtrim($supported, '*'); if (strpos($mime, $prefix) === 0) return true; } } return false; } }