label. */ function aatg_get_image_size_options() { return array( 'thumbnail' => __( 'Thumbnail', 'ai-auto-alt-text-generator' ), 'medium' => __( 'Medium', 'ai-auto-alt-text-generator' ), 'large' => __( 'Large', 'ai-auto-alt-text-generator' ), 'full' => __( 'Full', 'ai-auto-alt-text-generator' ), ); } /** * Where to create an API key for each provider. * * @return array Provider ID => URL. */ function aatg_get_api_key_urls() { return array( 'openai' => 'https://platform.openai.com/api-keys', 'anthropic' => 'https://platform.claude.com/settings/keys', 'deepseek' => 'https://platform.deepseek.com/api_keys', ); } /* ---------- sanitising ---------- */ /** * Keep a value only if it is one of the allowed choices. * * @param mixed $value Submitted value. * @param array $allowed Allowed values. * @param string $default Fallback. * @return string */ function aatg_sanitize_choice( $value, array $allowed, $default ) { $value = is_scalar( $value ) ? (string) $value : ''; return in_array( $value, $allowed, true ) ? $value : $default; } /** * A known model ID; retired models map to their replacement. * * @param mixed $value Submitted value. * @return string */ function aatg_sanitize_model( $value ) { $value = is_scalar( $value ) ? (string) $value : ''; $retired = aatg_get_retired_models(); if ( isset( $retired[ $value ] ) ) { return $retired[ $value ]; } return array_key_exists( $value, aatg_get_models() ) ? $value : AATG_DEFAULT_MODEL; } /** * An integer clamped to a range; non-numbers give the default. * * @param mixed $value Submitted value. * @param int $min Minimum. * @param int $max Maximum. * @param int $default Fallback for non-numeric input. * @return int */ function aatg_sanitize_int_range( $value, $min, $max, $default ) { if ( ! is_numeric( $value ) ) { return (int) $default; } return max( (int) $min, min( (int) $max, (int) $value ) ); } /** * 'on' or 'off' for a checkbox (unchecked boxes submit nothing). * * @param mixed $value Submitted value. * @return string */ function aatg_sanitize_checkbox( $value ) { return 'on' === $value ? 'on' : 'off'; } /** * Secret fields are never sent back to the browser, so a blank submission of * the settings form keeps the saved value; the matching "remove" checkbox * clears it. Updates from code or WP-CLI store exactly what they are given. * * Hooked to pre_update_option_{option}. * * @param mixed $value New value. * @param mixed $old_value Saved value. * @param string $option Option name. * @return string */ function aatg_sanitize_api_key( $value, $old_value, $option ) { $clear_fields = array( 'aatg_openai_api_key' => 'aatg_clear_openai_key', 'aatg_anthropic_api_key' => 'aatg_clear_anthropic_key', 'aatg_deepseek_api_key' => 'aatg_clear_deepseek_key', 'aatg_webhook_secret' => 'aatg_clear_webhook_secret', ); $value = trim( sanitize_text_field( (string) $value ) ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- options.php has verified the settings nonce. $from_form = isset( $_POST['option_page'] ) && 'aatg_options_group' === $_POST['option_page']; if ( ! $from_form ) { return $value; } if ( isset( $clear_fields[ $option ] ) && ! empty( $_POST[ $clear_fields[ $option ] ] ) ) { return ''; } // phpcs:enable return '' === $value ? (string) $old_value : $value; } foreach ( array( 'aatg_openai_api_key', 'aatg_anthropic_api_key', 'aatg_deepseek_api_key', 'aatg_webhook_secret' ) as $aatg_secret_option ) { add_filter( 'pre_update_option_' . $aatg_secret_option, 'aatg_sanitize_api_key', 10, 3 ); } unset( $aatg_secret_option ); /* ---------- registration ---------- */ /** * Register plugin settings, sections and fields. */ function aatg_register_settings_init() { $group = 'aatg_options_group'; $settings = array( 'aatg_openai_model' => array( 'string', 'aatg_sanitize_model', AATG_DEFAULT_MODEL ), 'aatg_openai_api_key' => array( 'string', 'sanitize_text_field', '' ), 'aatg_anthropic_api_key' => array( 'string', 'sanitize_text_field', '' ), 'aatg_deepseek_api_key' => array( 'string', 'sanitize_text_field', '' ), 'aatg_language' => array( 'string', function ( $value ) { return aatg_sanitize_choice( $value, array_keys( aatg_get_language_options() ), 'en_US' ); }, 'en_US', ), 'aatg_image_size' => array( 'string', function ( $value ) { return aatg_sanitize_choice( $value, array_keys( aatg_get_image_size_options() ), 'large' ); }, 'large', ), 'aatg_image_delivery' => array( 'string', function ( $value ) { return aatg_sanitize_choice( $value, array( 'embed', 'url' ), 'embed' ); }, 'embed', ), 'aatg_use_page_context' => array( 'string', 'aatg_sanitize_checkbox', 'on' ), 'aatg_send_filename' => array( 'string', 'aatg_sanitize_checkbox', 'on' ), 'aatg_site_context' => array( 'string', 'sanitize_textarea_field', '' ), 'aatg_auto_title' => array( 'string', 'aatg_sanitize_checkbox', 'on' ), 'aatg_image_detail' => array( 'string', function ( $value ) { return aatg_sanitize_choice( $value, array( 'low', 'high' ), 'low' ); }, 'low', ), 'aatg_bulk_batch_size' => array( 'integer', function ( $value ) { return aatg_sanitize_int_range( $value, 1, 10, 4 ); }, 4, ), 'aatg_bulk_delay' => array( 'integer', function ( $value ) { return aatg_sanitize_int_range( $value, 0, 60, 3 ); }, 3, ), 'aatg_request_timeout' => array( 'integer', function ( $value ) { return aatg_sanitize_int_range( $value, 10, 120, 30 ); }, 30, ), 'aatg_webhook_url' => array( 'string', 'esc_url_raw', '' ), 'aatg_webhook_secret' => array( 'string', 'sanitize_text_field', '' ), ); foreach ( $settings as $name => $setting ) { register_setting( $group, $name, array( 'type' => $setting[0], 'sanitize_callback' => $setting[1], 'default' => $setting[2], ) ); } $page = 'aatg-settings'; add_settings_section( 'aatg_settings_section', __( 'AI provider & model', 'ai-auto-alt-text-generator' ), 'aatg_section_callback', $page ); add_settings_section( 'aatg_content_section', __( 'Images & context', 'ai-auto-alt-text-generator' ), 'aatg_content_section_callback', $page ); add_settings_section( 'aatg_rate_limit_section', __( 'Performance & Rate Limits', 'ai-auto-alt-text-generator' ), 'aatg_rate_limit_section_callback', $page ); add_settings_section( 'aatg_integrations_section', __( 'Integrations & Webhooks', 'ai-auto-alt-text-generator' ), 'aatg_integrations_section_callback', $page ); add_settings_field( 'aatg_provider', __( 'AI provider', 'ai-auto-alt-text-generator' ), 'aatg_provider_render', $page, 'aatg_settings_section', array( 'label_for' => 'aatg_provider' ) ); add_settings_field( 'aatg_openai_model', __( 'Model', 'ai-auto-alt-text-generator' ), 'aatg_openai_model_render', $page, 'aatg_settings_section', array( 'label_for' => 'aatg_openai_model' ) ); foreach ( aatg_get_providers() as $provider => $label ) { add_settings_field( 'aatg_' . $provider . '_api_key', /* translators: %s = provider name */ sprintf( __( '%s API key', 'ai-auto-alt-text-generator' ), $label ), 'aatg_api_key_render', $page, 'aatg_settings_section', array( 'provider' => $provider, 'label_for' => 'aatg_' . $provider . '_api_key', 'class' => 'aatg-key-row aatg-key-row-' . $provider, ) ); } add_settings_field( 'aatg_language', __( 'Output language', 'ai-auto-alt-text-generator' ), 'aatg_language_render', $page, 'aatg_settings_section', array( 'label_for' => 'aatg_language' ) ); add_settings_field( 'aatg_auto_title', __( 'Automatically generate image title', 'ai-auto-alt-text-generator' ), 'aatg_auto_title_render', $page, 'aatg_content_section' ); add_settings_field( 'aatg_use_page_context', __( 'Use page context', 'ai-auto-alt-text-generator' ), 'aatg_use_page_context_render', $page, 'aatg_content_section' ); add_settings_field( 'aatg_site_context', __( 'Site context', 'ai-auto-alt-text-generator' ), 'aatg_site_context_render', $page, 'aatg_content_section', array( 'label_for' => 'aatg_site_context' ) ); add_settings_field( 'aatg_send_filename', __( 'Send image file name', 'ai-auto-alt-text-generator' ), 'aatg_send_filename_render', $page, 'aatg_content_section' ); add_settings_field( 'aatg_image_size', __( 'Image size to send', 'ai-auto-alt-text-generator' ), 'aatg_image_size_render', $page, 'aatg_content_section', array( 'label_for' => 'aatg_image_size' ) ); add_settings_field( 'aatg_image_delivery', __( 'Image delivery', 'ai-auto-alt-text-generator' ), 'aatg_image_delivery_render', $page, 'aatg_content_section', array( 'label_for' => 'aatg_image_delivery' ) ); add_settings_field( 'aatg_image_detail', __( 'Image detail quality', 'ai-auto-alt-text-generator' ), 'aatg_image_detail_render', $page, 'aatg_rate_limit_section', array( 'label_for' => 'aatg_image_detail' ) ); add_settings_field( 'aatg_bulk_batch_size', __( 'Bulk batch size', 'ai-auto-alt-text-generator' ), 'aatg_bulk_batch_size_render', $page, 'aatg_rate_limit_section', array( 'label_for' => 'aatg_bulk_batch_size' ) ); add_settings_field( 'aatg_bulk_delay', __( 'Bulk optimiser delay (seconds)', 'ai-auto-alt-text-generator' ), 'aatg_bulk_delay_render', $page, 'aatg_rate_limit_section', array( 'label_for' => 'aatg_bulk_delay' ) ); add_settings_field( 'aatg_request_timeout', __( 'Request timeout (seconds)', 'ai-auto-alt-text-generator' ), 'aatg_request_timeout_render', $page, 'aatg_rate_limit_section', array( 'label_for' => 'aatg_request_timeout' ) ); add_settings_field( 'aatg_webhook_url', __( 'Webhook URL', 'ai-auto-alt-text-generator' ), 'aatg_webhook_url_render', $page, 'aatg_integrations_section', array( 'label_for' => 'aatg_webhook_url' ) ); add_settings_field( 'aatg_webhook_secret', __( 'Webhook signing secret', 'ai-auto-alt-text-generator' ), 'aatg_webhook_secret_render', $page, 'aatg_integrations_section', array( 'label_for' => 'aatg_webhook_secret' ) ); } add_action( 'admin_init', 'aatg_register_settings_init' ); /* ---------- section descriptions ---------- */ function aatg_section_callback() { echo '

' . esc_html__( 'Choose the AI provider and vision model, and add an API key for that provider. Keys are stored in your database (or defined in wp-config.php) and only ever sent to that provider.', 'ai-auto-alt-text-generator' ) . '

'; } function aatg_content_section_callback() { echo '

' . esc_html__( 'Control what is sent with each image. Page context helps the model pick relevant details, such as the heading and text around the image or the product it shows.', 'ai-auto-alt-text-generator' ) . '

'; } function aatg_rate_limit_section_callback() { echo '

' . esc_html__( 'Tune these settings to reduce token usage and avoid rate limits during bulk runs.', 'ai-auto-alt-text-generator' ) . '

'; } function aatg_integrations_section_callback() { echo '

' . esc_html__( 'Optionally POST to an external URL each time alt text or a title is generated — useful for automation tools (Zapier, Make), logging, or syncing to other systems.', 'ai-auto-alt-text-generator' ) . '

'; } /* ---------- field renderers ---------- */ /** * Print a ', esc_attr( $id ) ); foreach ( $options as $value => $label ) { printf( '', esc_attr( $value ), selected( $current, $value, false ), esc_html( $label ) ); } echo ''; } /** * Print a field description. * * @param string $text Text. */ function aatg_render_description( $text ) { echo '

' . esc_html( $text ) . '

'; } /** * Provider dropdown. Not saved: it filters the Model list and key rows. */ function aatg_provider_render() { $current = aatg_get_model_provider( aatg_get_selected_model() ); echo ''; } function aatg_openai_model_render() { $current = aatg_get_selected_model(); $models = aatg_get_models(); echo ''; aatg_render_description( __( 'All listed models can read images. The default models are the fastest and cheapest; higher-quality models cost more per image.', 'ai-auto-alt-text-generator' ) ); } /** * Masked API key field for one provider. * * @param array $args array( 'provider' => string ). */ function aatg_api_key_render( $args ) { $provider = $args['provider']; $id = 'aatg_' . $provider . '_api_key'; $source = aatg_api_key_source( $provider ); $urls = aatg_get_api_key_urls(); if ( 'constant' === $source || 'env' === $source ) { printf( '', esc_attr( $id ), esc_attr( 'constant' === $source ? __( 'Defined in wp-config.php', 'ai-auto-alt-text-generator' ) : __( 'Defined in the server environment', 'ai-auto-alt-text-generator' ) ) ); /* translators: %s = constant name, e.g. AATG_OPENAI_API_KEY */ aatg_render_description( sprintf( __( 'Set by %s, which takes priority over a saved key.', 'ai-auto-alt-text-generator' ), 'AATG_' . strtoupper( $provider ) . '_API_KEY' ) ); return; } $saved = 'option' === $source; printf( '', esc_attr( $id ), esc_attr( $saved ? __( 'Saved — leave blank to keep', 'ai-auto-alt-text-generator' ) : __( 'Paste your API key', 'ai-auto-alt-text-generator' ) ) ); if ( $saved ) { printf( ' ', esc_attr( $provider ), esc_html__( 'Remove saved key', 'ai-auto-alt-text-generator' ) ); } if ( isset( $urls[ $provider ] ) ) { printf( '

%s

', esc_url( $urls[ $provider ] ), esc_html__( 'Get an API key', 'ai-auto-alt-text-generator' ) ); } } function aatg_language_render() { aatg_render_select( 'aatg_language', aatg_get_language_options(), get_option( 'aatg_language', 'en_US' ) ); } function aatg_auto_title_render() { printf( '', checked( get_option( 'aatg_auto_title', 'on' ), 'on', false ), esc_html__( 'Also write an SEO-friendly title (in the same request).', 'ai-auto-alt-text-generator' ) ); } function aatg_use_page_context_render() { printf( '', checked( get_option( 'aatg_use_page_context', 'on' ), 'on', false ), esc_html__( 'Send a short summary of where the image is used: the page title, nearby heading and text, caption, and WooCommerce product details.', 'ai-auto-alt-text-generator' ) ); } function aatg_site_context_render() { printf( '', esc_textarea( get_option( 'aatg_site_context', '' ) ) ); aatg_render_description( __( 'Optional: what your site is about, your audience or brand voice. Sent with every image.', 'ai-auto-alt-text-generator' ) ); } function aatg_send_filename_render() { printf( '', checked( get_option( 'aatg_send_filename', 'on' ), 'on', false ), esc_html__( 'Include the image’s file name (e.g. “man-on-a-horse.jpg”) for extra context. Camera-style names such as IMG_1234 are never sent.', 'ai-auto-alt-text-generator' ) ); } function aatg_image_size_render() { aatg_render_select( 'aatg_image_size', aatg_get_image_size_options(), get_option( 'aatg_image_size', 'large' ) ); aatg_render_description( __( 'Large is usually plenty. Images over 2048 px are scaled down before sending.', 'ai-auto-alt-text-generator' ) ); } function aatg_image_delivery_render() { aatg_render_select( 'aatg_image_delivery', array( 'embed' => __( 'Embed the image in the request (recommended)', 'ai-auto-alt-text-generator' ), 'url' => __( 'Send the public image URL', 'ai-auto-alt-text-generator' ), ), get_option( 'aatg_image_delivery', 'embed' ) ); aatg_render_description( __( 'Embedding works on local, staging and private sites. A public URL only works if the provider can download the image.', 'ai-auto-alt-text-generator' ) ); } function aatg_image_detail_render() { aatg_render_select( 'aatg_image_detail', array( 'low' => __( 'Low', 'ai-auto-alt-text-generator' ), 'high' => __( 'High', 'ai-auto-alt-text-generator' ), ), get_option( 'aatg_image_detail', 'low' ) ); aatg_render_description( __( 'OpenAI and DeepSeek only. Low uses fewer tokens and is usually sufficient; High suits detailed images at higher cost.', 'ai-auto-alt-text-generator' ) ); } function aatg_bulk_batch_size_render() { printf( '', (int) get_option( 'aatg_bulk_batch_size', 4 ) ); aatg_render_description( __( 'Number of images per batch (lower values reduce rate-limit risk).', 'ai-auto-alt-text-generator' ) ); } function aatg_bulk_delay_render() { printf( '', (int) get_option( 'aatg_bulk_delay', 3 ) ); aatg_render_description( __( 'Seconds to wait between each batch during a bulk run. Increase this if you hit 429 rate limits.', 'ai-auto-alt-text-generator' ) ); } function aatg_request_timeout_render() { printf( '', (int) aatg_get_request_timeout() ); aatg_render_description( __( 'How long to wait for the AI provider before giving up (one retry with a longer wait follows a timeout).', 'ai-auto-alt-text-generator' ) ); } function aatg_webhook_url_render() { printf( '', esc_attr( get_option( 'aatg_webhook_url', '' ) ) ); aatg_render_description( __( 'Leave blank to disable. Requests are sent non-blocking, so uploads are not slowed.', 'ai-auto-alt-text-generator' ) ); } function aatg_webhook_secret_render() { $saved = '' !== (string) get_option( 'aatg_webhook_secret', '' ); printf( '', esc_attr( $saved ? __( 'Saved — leave blank to keep', 'ai-auto-alt-text-generator' ) : '' ) ); if ( $saved ) { printf( ' ', esc_html__( 'Remove saved secret', 'ai-auto-alt-text-generator' ) ); } aatg_render_description( __( 'Optional. If set, each request includes an X-AATG-Signature header (HMAC-SHA256 of the body) so your endpoint can verify authenticity.', 'ai-auto-alt-text-generator' ) ); } /* ---------- retired-model notice ---------- */ /** * One-time notice after the upgrade moved the site off a retired model. */ function aatg_render_model_migration_notice() { $old = get_option( 'aatg_model_migrated_from' ); if ( ! $old || ! current_user_can( 'manage_options' ) ) { return; } $models = aatg_get_models(); $new = aatg_get_selected_model(); $label = isset( $models[ $new ] ) ? preg_replace( '/\s+—.*$/u', '', $models[ $new ]['label'] ) : $new; $dismiss = wp_nonce_url( admin_url( 'admin-post.php?action=aatg_dismiss_model_notice' ), 'aatg_dismiss_model_notice' ); echo '

'; printf( /* translators: 1: old model ID, 2: new model name */ esc_html__( 'AI Auto Alt Text Generator switched your model from %1$s, which OpenAI is retiring, to %2$s. It is faster and costs less per image.', 'ai-auto-alt-text-generator' ), '' . esc_html( $old ) . '', '' . esc_html( $label ) . '' ); printf( ' %s · %s', esc_url( admin_url( 'options-general.php?page=aatg-settings' ) ), esc_html__( 'Review settings', 'ai-auto-alt-text-generator' ), esc_url( $dismiss ), esc_html__( 'Dismiss', 'ai-auto-alt-text-generator' ) ); echo '

'; } add_action( 'admin_notices', 'aatg_render_model_migration_notice' ); /** * Dismiss the retired-model notice. */ function aatg_dismiss_model_notice() { check_admin_referer( 'aatg_dismiss_model_notice' ); if ( current_user_can( 'manage_options' ) ) { delete_option( 'aatg_model_migrated_from' ); } wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'options-general.php?page=aatg-settings' ) ); exit; } add_action( 'admin_post_aatg_dismiss_model_notice', 'aatg_dismiss_model_notice' ); /* ---------- languages ---------- */ /** * Map of supported output languages. * Keys are option values; values are visible labels. */ function aatg_get_language_options() { return array( 'en_US' => __( 'English (US)', 'ai-auto-alt-text-generator' ), 'en_GB' => __( 'English (UK)', 'ai-auto-alt-text-generator' ), 'es_ES' => __( 'Spanish (ES)', 'ai-auto-alt-text-generator' ), 'es_MX' => __( 'Spanish (LATAM)', 'ai-auto-alt-text-generator' ), 'fr_FR' => __( 'French', 'ai-auto-alt-text-generator' ), 'de_DE' => __( 'German', 'ai-auto-alt-text-generator' ), 'it_IT' => __( 'Italian', 'ai-auto-alt-text-generator' ), 'pt_PT' => __( 'Portuguese (PT)', 'ai-auto-alt-text-generator' ), 'pt_BR' => __( 'Portuguese (BR)', 'ai-auto-alt-text-generator' ), 'nl_NL' => __( 'Dutch', 'ai-auto-alt-text-generator' ), 'sv_SE' => __( 'Swedish', 'ai-auto-alt-text-generator' ), 'da_DK' => __( 'Danish', 'ai-auto-alt-text-generator' ), 'fi_FI' => __( 'Finnish', 'ai-auto-alt-text-generator' ), 'no_NO' => __( 'Norwegian', 'ai-auto-alt-text-generator' ), 'pl_PL' => __( 'Polish', 'ai-auto-alt-text-generator' ), 'cs_CZ' => __( 'Czech', 'ai-auto-alt-text-generator' ), 'tr_TR' => __( 'Turkish', 'ai-auto-alt-text-generator' ), 'ru_RU' => __( 'Russian', 'ai-auto-alt-text-generator' ), 'ja_JP' => __( 'Japanese','ai-auto-alt-text-generator' ), 'ko_KR' => __( 'Korean', 'ai-auto-alt-text-generator' ), 'zh_CN' => __( 'Chinese (Simplified)', 'ai-auto-alt-text-generator' ), 'zh_TW' => __( 'Chinese (Traditional)', 'ai-auto-alt-text-generator' ), 'ar' => __( 'Arabic', 'ai-auto-alt-text-generator' ), 'hi_IN' => __( 'Hindi', 'ai-auto-alt-text-generator' ), ); }