__( 'AI Auto Alt Text', 'ai-auto-alt-text-generator' ), 'description' => __( 'Generate accessible, SEO-friendly image alt text and titles.', 'ai-auto-alt-text-generator' ), ) ); } add_action( 'wp_abilities_api_categories_init', 'aatg_register_ability_category' ); /** * Register the plugin's abilities so WordPress core AI, agents, MCP servers and * automation tools can generate alt text programmatically. No-op on WP < 6.9. */ function aatg_register_abilities() { if ( ! function_exists( 'wp_register_ability' ) ) { return; } wp_register_ability( 'ai-auto-alt-text/generate-alt-text', array( 'label' => __( 'Generate image alt text', 'ai-auto-alt-text-generator' ), 'description' => __( 'Generate accessible, SEO-friendly alt text (and, when the plugin\'s auto-title setting is on, a title) for a WordPress image attachment using the configured AI vision model.', 'ai-auto-alt-text-generator' ), 'category' => 'ai-auto-alt-text', 'input_schema' => array( 'type' => 'object', 'properties' => array( 'attachment_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => __( 'The ID of the image attachment to describe.', 'ai-auto-alt-text-generator' ), ), ), 'required' => array( 'attachment_id' ), ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'attachment_id' => array( 'type' => 'integer' ), 'alt_text' => array( 'type' => 'string' ), 'title' => array( 'type' => 'string' ), ), ), 'execute_callback' => 'aatg_ability_generate_alt_text', 'permission_callback' => 'aatg_ability_permission_check', ) ); } add_action( 'wp_abilities_api_init', 'aatg_register_abilities' ); /** * Permission callback for the generate-alt-text ability: the user must be able * to upload files and edit this attachment. * * @param array $input Ability input (expects 'attachment_id'). * @return bool */ function aatg_ability_permission_check( $input = array() ) { $attachment_id = is_array( $input ) && isset( $input['attachment_id'] ) ? absint( $input['attachment_id'] ) : 0; return current_user_can( 'upload_files' ) && $attachment_id && current_user_can( 'edit_post', $attachment_id ); } /** * Execute callback for the generate-alt-text ability. * * @param array $input Validated input (expects 'attachment_id'). * @return array|WP_Error Structured result, or WP_Error on failure. */ function aatg_ability_generate_alt_text( $input ) { $attachment_id = isset( $input['attachment_id'] ) ? absint( $input['attachment_id'] ) : 0; if ( ! $attachment_id ) { return new WP_Error( 'aatg_invalid_input', __( 'A valid attachment_id is required.', 'ai-auto-alt-text-generator' ) ); } $result = aatg_generate_text_and_title( $attachment_id ); if ( is_wp_error( $result ) ) { return $result; } return array( 'attachment_id' => $attachment_id, 'alt_text' => isset( $result['alt_text'] ) ? (string) $result['alt_text'] : '', 'title' => isset( $result['title'] ) ? (string) $result['title'] : '', ); }