ID ) ) { return $fields; } $fields['aatg_generate_alt_text'] = array( 'label' => __( 'Alt Text & Title Generator', 'ai-auto-alt-text-generator' ), 'input' => 'html', 'html' => '' . '' . esc_html( get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) ) . '
' . '' . esc_html( get_the_title( $post->ID ) ) . '', 'helps' => __( 'Click to generate alt text (and a title if enabled) using your selected AI model.', 'ai-auto-alt-text-generator' ), ); return $fields; } add_filter( 'attachment_fields_to_edit', 'aatg_attachment_fields_to_edit', 10, 2 ); /** * Admin screens where the media modal or attachment editor can show the button. * * @return string[] */ function aatg_admin_script_screens() { return array( 'upload.php', 'post.php', 'post-new.php', 'media-new.php', 'site-editor.php', 'widgets.php' ); } /** * Enqueue the Media Library button script on media and editor screens. * * @param string $hook Current admin page. */ function aatg_enqueue_admin_scripts( $hook = '' ) { if ( in_array( $hook, aatg_admin_script_screens(), true ) ) { aatg_enqueue_media_button_script(); } } add_action( 'admin_enqueue_scripts', 'aatg_enqueue_admin_scripts' ); /** * Enqueue the button script with every media modal (WooCommerce category * thumbnails, Site Icon, menus, …) for users who can upload. */ function aatg_enqueue_media_modal_script() { if ( current_user_can( 'upload_files' ) ) { aatg_enqueue_media_button_script(); } } add_action( 'wp_enqueue_media', 'aatg_enqueue_media_modal_script' ); /** * Register, localise and enqueue the Media Library button script. */ function aatg_enqueue_media_button_script() { if ( wp_script_is( 'aatg-admin-script', 'enqueued' ) ) { return; } wp_enqueue_script( 'aatg-admin-script', AATG_PLUGIN_URL . 'assets/admin.js', array( 'jquery' ), AATG_VERSION, true ); wp_localize_script( 'aatg-admin-script', 'aatg_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aatg_nonce' ), 'i18n' => array( 'generate' => __( 'Generate Alt Text & Title', 'ai-auto-alt-text-generator' ), 'generating' => __( 'Generating…', 'ai-auto-alt-text-generator' ), 'altUpdated' => __( 'Alt text updated:', 'ai-auto-alt-text-generator' ), 'titleUpdated' => __( 'Title updated:', 'ai-auto-alt-text-generator' ), 'note' => __( 'Note:', 'ai-auto-alt-text-generator' ), 'error' => __( 'Error:', 'ai-auto-alt-text-generator' ), 'unknownError' => __( 'Unknown error.', 'ai-auto-alt-text-generator' ), 'requestFailed' => __( 'Request failed.', 'ai-auto-alt-text-generator' ), ), ) ); } /** * AJAX: generate alt text and title for one attachment. */ function aatg_generate_alt_text_ajax() { check_ajax_referer( 'aatg_nonce', 'nonce' ); $attachment_id = isset( $_POST['attachment_id'] ) ? absint( $_POST['attachment_id'] ) : 0; if ( ! $attachment_id ) { wp_send_json_error( __( 'Missing attachment ID.', 'ai-auto-alt-text-generator' ), 400 ); } if ( ! current_user_can( 'upload_files' ) || ! current_user_can( 'edit_post', $attachment_id ) ) { wp_send_json_error( __( 'You are not allowed to edit this image.', 'ai-auto-alt-text-generator' ), 403 ); } $result = aatg_generate_text_and_title( $attachment_id ); if ( is_wp_error( $result ) ) { wp_send_json_error( $result->get_error_message() ); } wp_send_json_success( array( 'alt_text' => $result['alt_text'], 'image_title' => get_post_field( 'post_title', $attachment_id ), 'warning' => $result['warning'], ) ); } add_action( 'wp_ajax_aatg_generate_alt_text_ajax', 'aatg_generate_alt_text_ajax' ); /** * Attachments created during this request. wp_generate_attachment_metadata() * reports context 'create' for uploads *and* for thumbnail regeneration * (wp media regenerate, Regenerate Thumbnails, …), so only IDs seen on * add_attachment are treated as new uploads. * * @param string $op 'add', 'has' or 'remove'. * @param int $id Attachment ID. * @return bool */ function aatg_new_upload_registry( $op, $id ) { static $ids = array(); $id = (int) $id; if ( 'add' === $op ) { $ids[ $id ] = true; } elseif ( 'remove' === $op ) { unset( $ids[ $id ] ); } return isset( $ids[ $id ] ); } /** * @param int $post_ID New attachment ID. */ function aatg_remember_new_upload( $post_ID ) { aatg_new_upload_registry( 'add', $post_ID ); } add_action( 'add_attachment', 'aatg_remember_new_upload' ); /** * Forget an attachment once its first metadata pass is over. * * @param array $metadata Attachment metadata. * @param int $attachment_id Attachment ID. * @return array Unchanged metadata. */ function aatg_forget_new_upload( $metadata, $attachment_id ) { aatg_new_upload_registry( 'remove', $attachment_id ); return $metadata; } add_filter( 'wp_generate_attachment_metadata', 'aatg_forget_new_upload', 99, 2 ); /** * Generate alt text (and title) for a new upload once WordPress has created the * image sizes, so the chosen size is available. Errors are logged, never shown, * so an upload is never blocked. Existing images (thumbnail regeneration) and * images that already have real alt text are left alone. * * @param array $metadata Attachment metadata (not saved yet). * @param int $attachment_id Attachment ID. * @param string $context 'create' or 'update'. * @return array Unchanged metadata. */ function aatg_on_generate_metadata( $metadata, $attachment_id, $context = 'create' ) { if ( 'create' !== $context || ! aatg_new_upload_registry( 'has', $attachment_id ) || ! wp_attachment_is_image( $attachment_id ) ) { return $metadata; } if ( ! aatg_is_low_quality_alt( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ), $attachment_id ) ) { return $metadata; } /** * Filter whether a new upload gets alt text automatically. * Return false to only generate from the Media Library button or bulk tool. * * @param bool $generate Whether to generate on upload. * @param int $attachment_id Attachment ID. */ if ( ! apply_filters( 'aatg_generate_on_upload', true, $attachment_id ) ) { return $metadata; } $result = aatg_generate_text_and_title( $attachment_id, is_array( $metadata ) ? $metadata : null, 'upload' ); if ( is_wp_error( $result ) ) { aatg_log( sprintf( 'Alt text not generated on upload for attachment %d: %s', $attachment_id, $result->get_error_message() ) ); } return $metadata; } add_filter( 'wp_generate_attachment_metadata', 'aatg_on_generate_metadata', 20, 3 );