'1024x1024 (Square, 1:1)', '1024x1536' => '1024x1536 (Portrait, 2:3)', '1536x1024' => '1536x1024 (Landscape, 3:2)', '1792x1024' => '1792x1024 (Widescreen, 16:9)', '1024x1792' => '1024x1792 (Vertical, 9:16)', '1920x640' => '1920x640 (Ultra-wide, 3:1)', '640x1920' => '640x1920 (Tall Banner, 1:3)', ); // Removed OpenAI models (deprecated by OpenAI). Saved settings using them move to gpt-image-2. const OPENAI_DEPRECATED_MODELS = array( 'gpt-image-1', 'gpt-image-1-mini', 'gpt-image-1.5', 'chatgpt-image-latest' ); // Retired OpenAI, Gemini and Grok models. Saved settings using them move to the replacement. const RETIRED_MODEL_REPLACEMENTS = array( 'gpt-image-2.5-flare' => 'gpt-image-2.6-flare', 'gpt-image-2.5-sunburst' => 'gpt-image-2.6-sunburst', 'gemini-2.5-flash-image' => 'gemini-3.1-flash-image', 'gemini-3.1-flash-image-preview' => 'gemini-3.1-flash-image', 'gemini-3-pro-image-preview' => 'gemini-3-pro-image', 'grok-imagine-image-pro' => 'grok-imagine-image-2.0', 'grok-imagine-image-quality' => 'grok-imagine-image-2.0', ); // Settings tabs, in nav order. Anything else in ?tab= falls back to 'general'. const TABS = array( 'general', 'featured', 'bulk', 'audit', 'inline', 'products', 'brand', 'audio', 'mcp' ); // GPT Image 2.6 models (Pro only). const OPENAI_GPT25_MODELS = array('gpt-image-2.6-flare', 'gpt-image-2.6-sunburst'); // Models that share the GPT Image 2 sizes and background option. const OPENAI_GPT2_FAMILY = array('gpt-image-2', 'gpt-image-2.6-flare', 'gpt-image-2.6-sunburst'); // Generation quality tiers accepted only by GPT Image 2.6. const OPENAI_GPT25_QUALITIES = array('xhigh', 'max'); /** * Whether a model uses the GPT Image 2 sizes and background option. * * 'chatgpt-image-latest' is a removed model that legacy settings remap to gpt-image-2. * * @param string $model Model slug. * @return bool */ public static function is_gpt2_family( $model ) { return in_array( $model, self::OPENAI_GPT2_FAMILY, true ) || $model === 'chatgpt-image-latest'; } /** * Whether a model is one of the GPT Image 2.6 models. * * @param string $model Model slug. * @return bool */ public static function is_gpt25_model( $model ) { return in_array( $model, self::OPENAI_GPT25_MODELS, true ); } /** * Settings page slug. * * @var string */ private $page = 'ai-featured-image-generator'; /** * Settings option name. * * @var string */ private $option = 'aifi_settings'; /** * Product Images module, when constructed. * * @var Product_Images|null */ private $product_images = null; /** * Give the settings screen the Product Images module. * * @param Product_Images $product_images Module. */ public function set_product_images( $product_images ) { $this->product_images = $product_images; } /** * Whether the multisite save hook is already on (one instance is enough). * * @var bool */ private static $network_save_hooked = false; /** * True while the plugin itself writes the option, so the form sanitizer * does not run a second time on an already clean value. * * @var bool */ private static $writing = false; /** * Initialize settings. * * @param bool $register_hooks False for a plain settings reader that adds no hooks. */ public function __construct( $register_hooks = true ) { if ( !$register_hooks ) { return; } add_action( 'admin_menu', array($this, 'add_settings_page') ); add_action( 'admin_init', array($this, 'register_settings') ); add_action( 'admin_init', array($this, 'redirect_after_save') ); add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') ); // options.php checks this capability before it saves the form. add_filter( 'option_page_capability_' . $this->page, array(__CLASS__, 'capability') ); // On multisite the plugin reads the network option, but options.php writes the // per-site one. Send the form save to the network option instead. if ( is_multisite() && !self::$network_save_hooked ) { self::$network_save_hooked = true; add_filter( 'pre_update_option_' . $this->option, array($this, 'save_network_settings'), 10, 2 ); } } /** * Capability for the settings screen and anything that changes the settings. * On multisite the settings (and the API keys) are shared by the whole network, * so only a network administrator may see or change them. * * @return string */ public static function capability() { return ( is_multisite() ? 'manage_network_options' : 'manage_options' ); } /** * Multisite: write a settings form save to the network option. * * Runs on pre_update_option_aifi_settings, after sanitize_option has already * cleaned the value, so it is written as is. Returning the old site value makes * update_option() stop without writing (or first adding) a per-site copy. * * @param mixed $value Sanitized new value. * @param mixed $old_value Current per-site value. * @return mixed The old per-site value. */ public function save_network_settings( $value, $old_value ) { if ( is_array( $value ) ) { $this->save_settings( $value ); } return $old_value; } /** * Redirect back to the correct tab after settings save. */ public function redirect_after_save() { // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( isset( $_POST['aifi_active_tab'] ) && isset( $_POST['option_page'] ) && $_POST['option_page'] === $this->page ) { $tab = sanitize_key( $_POST['aifi_active_tab'] ); if ( in_array( $tab, self::TABS, true ) ) { add_filter( 'wp_redirect', function ( $location ) use($tab) { if ( strpos( $location, 'page=' . $this->page ) !== false && preg_match( '/[?&]settings-updated=(1|true)(&|$)/', $location ) ) { return add_query_arg( 'tab', $tab, $location ); } return $location; } ); } } } /** * Enqueue scripts and styles for settings pages. * * @param string $hook Current admin page hook. */ public function enqueue_scripts( $hook ) { // Settings page if ( $hook === 'toplevel_page_ai-featured-image-generator' ) { wp_enqueue_media(); wp_enqueue_style( 'aifi-settings', AIFI_PLUGIN_URL . 'assets/css/settings.css', array(), AIFI_VERSION ); wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'aifi-settings', AIFI_PLUGIN_URL . 'assets/js/settings.js', array('jquery', 'wp-color-picker'), AIFI_VERSION, true ); $settings = $this->get_settings(); $model_choices = $this->get_model_choices(); // Never put an API key in here: this object is printed into the page. wp_localize_script( 'aifi-settings', 'aifiSettingsData', array( 'currentProvider' => $settings['ai_provider'] ?? 'openai', 'providers' => Providers::slugs(), 'currentModel' => $settings['ai_model'] ?? 'gpt-image-2', 'gpt2Family' => self::OPENAI_GPT2_FAMILY, 'gpt25Models' => self::OPENAI_GPT25_MODELS, 'modelsData' => $model_choices['models'], 'disabledModels' => $model_choices['disabled'], 'tabs' => self::TABS, 'optionName' => $this->option, 'restUrl' => rest_url( 'aifi/v1' ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'i18n' => array( 'advanced' => __( 'Advanced settings', 'ai-featured-image-generator' ), 'proSuffix' => __( '%s (Pro)', 'ai-featured-image-generator' ), 'styleNameExample' => __( 'e.g. Dreamy', 'ai-featured-image-generator' ), 'stylePromptExample' => __( 'e.g. in a dreamy impressionist style', 'ai-featured-image-generator' ), 'advancedHint' => __( 'File type, compression and other fine-tuning. The defaults suit most sites.', 'ai-featured-image-generator' ), 'removeStyle' => __( 'Remove style', 'ai-featured-image-generator' ), 'qualityMax' => __( 'Max compression', 'ai-featured-image-generator' ), 'qualityBalanced' => __( 'Balanced', 'ai-featured-image-generator' ), 'qualityLight' => __( 'Light compression', 'ai-featured-image-generator' ), 'qualityNearLossless' => __( 'Near-lossless', 'ai-featured-image-generator' ), 'outputSettings' => __( 'Output Settings', 'ai-featured-image-generator' ), 'logoCostOne' => __( 'Makes one image with %s, billed to your OpenAI account. It becomes your Brand Kit logo.', 'ai-featured-image-generator' ), 'logoCostTwo' => __( 'Makes two images with %s (a PNG logo and an SVG copy), billed to your OpenAI account.', 'ai-featured-image-generator' ), 'logoWorking' => __( 'Making your logo. This can take up to a minute.', 'ai-featured-image-generator' ), 'logoDone' => __( 'Logo saved as your Brand Kit logo.', 'ai-featured-image-generator' ), 'logoSvgSaved' => __( 'SVG copy saved to the Media Library (ID %d).', 'ai-featured-image-generator' ), 'logoFailed' => __( 'The logo could not be made. Please try again.', 'ai-featured-image-generator' ), 'aspectRatioOther' => __( 'Controls the Gemini canvas shape. Choose the resolution with the Image Size setting below.', 'ai-featured-image-generator' ), 'testing' => __( 'Testing...', 'ai-featured-image-generator' ), 'keyValid' => __( 'API key is valid!', 'ai-featured-image-generator' ), 'keyInvalid' => __( 'Invalid API key.', 'ai-featured-image-generator' ), 'chooseImage' => __( 'Choose Reference Image', 'ai-featured-image-generator' ), 'chooseLogo' => __( 'Choose Logo', 'ai-featured-image-generator' ), 'choosePodcastImage' => __( 'Choose Podcast Cover', 'ai-featured-image-generator' ), 'useImage' => __( 'Use this image', 'ai-featured-image-generator' ), 'styleName' => __( 'Style name', 'ai-featured-image-generator' ), 'promptDescription' => __( 'Prompt description', 'ai-featured-image-generator' ), 'copied' => __( 'Copied', 'ai-featured-image-generator' ), 'copyFailed' => __( 'Select the text and copy it manually.', 'ai-featured-image-generator' ), 'working' => __( 'Working, this can take a minute.', 'ai-featured-image-generator' ), 'saving' => __( 'Saving...', 'ai-featured-image-generator' ), 'genericError' => __( 'Something went wrong. Try again.', 'ai-featured-image-generator' ), 'openInMediaLibrary' => __( 'Open in Media Library', 'ai-featured-image-generator' ), 'createNewKey' => __( 'Create a new key', 'ai-featured-image-generator' ), 'showDetails' => __( 'Show details', 'ai-featured-image-generator' ), 'hideDetails' => __( 'Hide details', 'ai-featured-image-generator' ), 'loadingVoices' => __( 'Loading voices...', 'ai-featured-image-generator' ), 'enterKeyFirst' => __( 'Enter your ElevenLabs API key above to load the voices.', 'ai-featured-image-generator' ), 'noVoices' => __( 'No voices found for this key.', 'ai-featured-image-generator' ), 'chooseVoice' => __( 'Choose a voice', 'ai-featured-image-generator' ), 'makingSample' => __( 'Making a sample...', 'ai-featured-image-generator' ), 'playingSample' => __( 'Playing the sample.', 'ai-featured-image-generator' ), 'cannotPlay' => __( 'The browser could not play the sample.', 'ai-featured-image-generator' ), ), 'mcpUsername' => wp_get_current_user()->user_login, ) ); // Enqueue bulk generate assets (for the Bulk Generate tab) $is_premium = Providers::can_use_premium(); wp_enqueue_style( 'aifi-bulk', AIFI_PLUGIN_URL . 'assets/css/bulk.css', array('aifi-settings'), AIFI_VERSION ); wp_enqueue_script( 'aifi-bulk', AIFI_PLUGIN_URL . 'assets/js/bulk.js', array('jquery', 'aifi-settings'), AIFI_VERSION, true ); // Check for auto-start from bulk action redirect $auto_start = false; $post_ids = array(); $overwrite = false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['action'] ) && $_GET['action'] === 'generate' && isset( $_GET['post_ids'] ) ) { if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'aifi_bulk_generate' ) ) { $auto_start = true; $post_ids = array_map( 'absint', explode( ',', sanitize_text_field( wp_unslash( $_GET['post_ids'] ) ) ) ); $post_ids = array_values( array_filter( $post_ids ) ); } } $enabled_post_types = $this->get_enabled_post_types(); $post_type_options = array(); foreach ( $enabled_post_types as $pt ) { $obj = get_post_type_object( $pt ); if ( $obj ) { $post_type_options[] = array( 'value' => $pt, 'label' => $obj->labels->name, ); } } $upgrade_url = ( function_exists( 'afig_fs' ) ? afig_fs()->get_upgrade_url() : '#' ); $bulk_settings = $this->get_settings(); $bulk_provider = ( isset( $bulk_settings['ai_provider'] ) ? $bulk_settings['ai_provider'] : 'openai' ); $bulk_cost = $this->get_bulk_cost_context( $bulk_settings, $bulk_provider ); wp_localize_script( 'aifi-bulk', 'aifiBulkData', array( 'restUrl' => rest_url( 'aifi/v1' ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'autoStart' => $auto_start, 'postIds' => $post_ids, 'overwrite' => $overwrite, 'postTypes' => $post_type_options, 'isPremium' => $is_premium, 'upgradeUrl' => $upgrade_url, 'provider' => $bulk_provider, 'providerLabel' => $bulk_cost['provider_label'], 'modelLabel' => $bulk_cost['model_label'], 'pricePerImage' => $bulk_cost['price_per_image'], 'i18n' => array( 'processing' => __( 'Processing', 'ai-featured-image-generator' ), 'of' => __( 'of', 'ai-featured-image-generator' ), 'success' => __( 'Success', 'ai-featured-image-generator' ), 'skipped' => __( 'Skipped', 'ai-featured-image-generator' ), 'error' => __( 'Error', 'ai-featured-image-generator' ), 'cancelled' => __( 'Cancelled', 'ai-featured-image-generator' ), 'completed' => __( 'Completed!', 'ai-featured-image-generator' ), 'succeeded' => __( 'succeeded', 'ai-featured-image-generator' ), 'failed' => __( 'failed', 'ai-featured-image-generator' ), 'noPostsFound' => __( 'No posts found matching your filters.', 'ai-featured-image-generator' ), 'selectPosts' => __( 'Please select at least one post.', 'ai-featured-image-generator' ), 'tickPosts' => __( 'Tick the posts to work on.', 'ai-featured-image-generator' ), 'costLine' => __( 'Up to %1$d images with %2$s.', 'ai-featured-image-generator' ), 'costLineOne' => __( '1 image with %s.', 'ai-featured-image-generator' ), 'costPrice' => __( 'About %s in total.', 'ai-featured-image-generator' ), 'confirmCancel' => __( 'Are you sure you want to cancel? The current image will finish generating.', 'ai-featured-image-generator' ), 'loadingPosts' => __( 'Loading posts...', 'ai-featured-image-generator' ), 'confirmGenerateOne' => __( 'Generate 1 image?', 'ai-featured-image-generator' ), 'confirmGenerate' => __( 'Generate %d images?', 'ai-featured-image-generator' ), 'confirmProvider' => __( 'Provider: %s', 'ai-featured-image-generator' ), 'confirmCost' => __( 'Estimated cost: about $%1$s (%2$d x $%3$s)', 'ai-featured-image-generator' ), 'confirmBilled' => __( 'Each image is billed to your %s account.', 'ai-featured-image-generator' ), 'confirmReplace' => __( '%d of these posts already have a featured image and it will be REPLACED.', 'ai-featured-image-generator' ), 'confirmSkip' => __( '%d of the selected posts already have a featured image and will be skipped.', 'ai-featured-image-generator' ), 'confirmNothing' => __( 'All %d selected posts already have a featured image, and "Overwrite existing featured images" is off. Nothing will be generated. Continue anyway?', 'ai-featured-image-generator' ), 'confirmUndone' => __( 'This cannot be undone.', 'ai-featured-image-generator' ), ), ) ); // Product Images tab and its bulk card (the script also serves the product box). wp_enqueue_style( 'aifi-product-images', AIFI_PLUGIN_URL . 'assets/css/product-images.css', array('aifi-settings'), AIFI_VERSION ); wp_enqueue_script( 'aifi-product-images', AIFI_PLUGIN_URL . 'assets/js/product-images.js', array('jquery', 'wp-color-picker'), AIFI_VERSION, true ); if ( $this->product_images ) { $product_data = $this->product_images->script_data( 'settings' ); $product_data['i18n'] = array_merge( $product_data['i18n'], array( 'counting' => __( 'Loading products...', 'ai-featured-image-generator' ), 'counted' => __( 'Products found: %1$d. Without a photo: %2$d.', 'ai-featured-image-generator' ), 'editProduct' => __( 'Add a photo', 'ai-featured-image-generator' ), 'confirmRun' => __( 'This will make images with %2$s. Images: up to %1$d. Products: %3$d. Continue?', 'ai-featured-image-generator' ), 'confirmPrice' => __( 'Up to %s in total.', 'ai-featured-image-generator' ), 'noneToRun' => __( 'Tick the products to work on.', 'ai-featured-image-generator' ), 'noProducts' => __( 'No products found.', 'ai-featured-image-generator' ), 'selectedCount' => __( '%1$d / %2$d selected', 'ai-featured-image-generator' ), 'pageOf' => __( 'Page %1$d of %2$d', 'ai-featured-image-generator' ), 'rowDone' => __( 'Already done. Tick it to make new images.', 'ai-featured-image-generator' ), 'rowNoPhoto' => __( 'No photo.', 'ai-featured-image-generator' ), 'costSelected' => __( 'Up to %1$d images for %2$d products.', 'ai-featured-image-generator' ), 'costOneProduct' => __( 'Up to %d images for 1 product.', 'ai-featured-image-generator' ), 'costOneImage' => __( '1 image for 1 product.', 'ai-featured-image-generator' ), 'costPrice' => __( 'About %s in total.', 'ai-featured-image-generator' ), 'confirmRedo' => __( '%d of them were already made by AIFIG and get new images. In a set, AIFIG\'s earlier gallery images are swapped for the new ones.', 'ai-featured-image-generator' ), 'skippedPhoto' => __( 'skipped: add a product photo first', 'ai-featured-image-generator' ), 'skippedDone' => __( 'skipped: AIFIG already made its main image', 'ai-featured-image-generator' ), 'madeImages' => __( 'done, %d images', 'ai-featured-image-generator' ), 'madeImagesOne' => __( 'done, 1 image', 'ai-featured-image-generator' ), 'stopped' => __( 'Stopped.', 'ai-featured-image-generator' ), 'modelLabel' => ( $this->product_images->resolve_model() instanceof \WP_Error ? '' : trim( preg_replace( '/\\s*\\([^)]*\\)\\s*$/', '', (string) (Product_Images::model_labels()[$this->product_images->resolve_model()] ?? '') ) ) ), 'liveWarning' => __( 'Each product\'s main image is replaced on your store straight away. The originals are kept: put them back with Restore originals in the product\'s AI Product Images box.', 'ai-featured-image-generator' ), 'progressOne' => __( 'Product %1$d of %2$d: %3$s', 'ai-featured-image-generator' ), 'stopping' => __( 'Stopping after this product...', 'ai-featured-image-generator' ), 'endSummary' => __( 'Images made: %1$d. Products done: %2$d. Skipped: %3$d. Failed: %4$d.', 'ai-featured-image-generator' ), 'autoStopped' => __( 'Stopped after the same error 3 times in a row: %s', 'ai-featured-image-generator' ), 'leaveWarning' => __( 'Product images are still being made. Leaving now stops the run.', 'ai-featured-image-generator' ), 'fixCost' => __( 'Photos to remake: %1$d. Products: %2$d.', 'ai-featured-image-generator' ), 'fixConfirm' => __( 'This will remake photos with %2$s. Photos: %1$d. Products: %3$d. Continue?', 'ai-featured-image-generator' ), 'checking' => __( 'Checking product photos...', 'ai-featured-image-generator' ), 'checkingPage' => __( 'Checking product photos... page %1$d of %2$d', 'ai-featured-image-generator' ), 'fixCounted' => __( 'Products with photos that do not match your style: %1$d. Without a photo: %2$d.', 'ai-featured-image-generator' ), 'fixNone' => __( 'Every product photo matches your style. Nothing to fix.', 'ai-featured-image-generator' ), 'reloadList' => __( 'Click Load Products again for this choice.', 'ai-featured-image-generator' ), 'remakeN' => __( 'Remake %d photos', 'ai-featured-image-generator' ), 'remakeOne' => __( 'Remake 1 photo', 'ai-featured-image-generator' ), 'reasonBackground' => __( 'background', 'ai-featured-image-generator' ), 'reasonShape' => __( 'shape', 'ai-featured-image-generator' ), 'reasonSize' => __( 'too small', 'ai-featured-image-generator' ), 'reasonFormat' => __( 'file type', 'ai-featured-image-generator' ), 'reasonShot' => __( 'type of shot', 'ai-featured-image-generator' ), 'reasonLine' => __( '%1$s (%2$s)', 'ai-featured-image-generator' ), 'whichMain' => __( 'main image', 'ai-featured-image-generator' ), 'whichGallery' => __( 'gallery photo %d', 'ai-featured-image-generator' ), 'fixLive' => __( 'Each new photo replaces the old one on your store straight away. The originals are kept: put them back with Restore originals in the product\'s AI Product Images box.', 'ai-featured-image-generator' ), 'fixedPhotos' => __( 'fixed %d photos', 'ai-featured-image-generator' ), 'fixedOne' => __( 'fixed 1 photo', 'ai-featured-image-generator' ), 'fixedNone' => __( 'every photo already matches', 'ai-featured-image-generator' ), 'fixBusy' => __( 'skipped: another task was using this product. Try again in a moment.', 'ai-featured-image-generator' ), 'fixUnavailable' => __( 'skipped: this product could not be opened. It may have been deleted, or you cannot edit it.', 'ai-featured-image-generator' ), 'fixSummary' => __( 'Photos fixed: %1$d. Products: %2$d. Skipped: %3$d. Failed: %4$d.', 'ai-featured-image-generator' ), ) ); wp_localize_script( 'aifi-product-images', 'aifiProductData', $product_data ); } // Image Audit tab. wp_enqueue_style( 'aifi-audit', AIFI_PLUGIN_URL . 'assets/css/audit.css', array('aifi-settings'), AIFI_VERSION ); wp_enqueue_script( 'aifi-audit', AIFI_PLUGIN_URL . 'assets/js/audit.js', array('jquery', 'aifi-settings'), AIFI_VERSION, true ); wp_localize_script( 'aifi-audit', 'aifiAuditData', array( 'restUrl' => rest_url( 'aifi/v1' ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'isPremium' => Providers::can_use_premium(), 'upgradeUrl' => ( function_exists( 'afig_fs' ) ? afig_fs()->get_upgrade_url() : '#' ), 'largeKb' => Image_Audit::LARGE_KB, 'i18n' => array( 'scanning' => __( 'Checking your images...', 'ai-featured-image-generator' ), 'scanFailed' => __( 'The check did not finish. Please try again.', 'ai-featured-image-generator' ), 'noRows' => __( 'No images with this problem. Nice work.', 'ai-featured-image-generator' ), 'lockedMore' => __( 'Showing %1$d of %2$d.', 'ai-featured-image-generator' ), 'noAlt' => __( 'No alt text', 'ai-featured-image-generator' ), 'altLabel' => __( 'Alt text:', 'ai-featured-image-generator' ), 'featured' => __( 'Featured image', 'ai-featured-image-generator' ), 'inContent' => __( 'In the content', 'ai-featured-image-generator' ), 'gallery' => __( 'Product gallery', 'ai-featured-image-generator' ), 'external' => __( 'From another site', 'ai-featured-image-generator' ), 'blurry' => __( 'Blurry', 'ai-featured-image-generator' ), 'cutOff' => __( 'Possibly cut off: %s edge', 'ai-featured-image-generator' ), 'cutOffMany' => __( 'Possibly cut off: %s edges', 'ai-featured-image-generator' ), 'edges' => array( 'top' => __( 'top', 'ai-featured-image-generator' ), 'bottom' => __( 'bottom', 'ai-featured-image-generator' ), 'left' => __( 'left', 'ai-featured-image-generator' ), 'right' => __( 'right', 'ai-featured-image-generator' ), ), 'and' => __( 'and', 'ai-featured-image-generator' ), 'viewImage' => __( 'View image', 'ai-featured-image-generator' ), 'mediaLibrary' => __( 'Open in Media Library', 'ai-featured-image-generator' ), 'viewPost' => __( 'View post', 'ai-featured-image-generator' ), 'preview' => __( 'Preview', 'ai-featured-image-generator' ), 'newTab' => __( '(opens in a new tab)', 'ai-featured-image-generator' ), 'notUsed' => __( 'Not used in your content', 'ai-featured-image-generator' ), 'uploadedTo' => __( 'Originally uploaded to', 'ai-featured-image-generator' ), 'andMore' => __( 'and %d more', 'ai-featured-image-generator' ), 'edit' => __( 'Edit text', 'ai-featured-image-generator' ), 'save' => __( 'Save', 'ai-featured-image-generator' ), 'cancel' => __( 'Cancel', 'ai-featured-image-generator' ), 'saved' => __( 'Saved', 'ai-featured-image-generator' ), 'title' => __( 'Title', 'ai-featured-image-generator' ), 'alt' => __( 'Alt text', 'ai-featured-image-generator' ), 'caption' => __( 'Caption', 'ai-featured-image-generator' ), 'convert' => __( 'Convert to WebP', 'ai-featured-image-generator' ), 'converting' => __( 'Converting...', 'ai-featured-image-generator' ), 'converted' => __( 'Now WebP, %d%% smaller', 'ai-featured-image-generator' ), 'regenerate' => __( 'Regenerate in the editor', 'ai-featured-image-generator' ), 'error' => __( 'Something went wrong. Please try again.', 'ai-featured-image-generator' ), 'filling' => __( 'Filling alt text... %d done', 'ai-featured-image-generator' ), 'filled' => __( 'Done: %1$d filled, %2$d skipped.', 'ai-featured-image-generator' ), 'confirmFill' => __( 'Fill the missing alt text using your Alt Text settings on the General tab? Images that already have alt text are not changed.', 'ai-featured-image-generator' ), 'measuring' => __( 'Looking for blurry and cut-off pictures: %1$d of %2$d', 'ai-featured-image-generator' ), 'stillChecking' => __( 'Still checking your pictures. More may appear here when the check finishes.', 'ai-featured-image-generator' ), 'checked' => __( '%d images checked.', 'ai-featured-image-generator' ), 'checkingButton' => __( 'Checking...', 'ai-featured-image-generator' ), 'checkAgain' => __( 'Check again', 'ai-featured-image-generator' ), 'prev' => __( 'Previous', 'ai-featured-image-generator' ), 'next' => __( 'Next', 'ai-featured-image-generator' ), 'pageOf' => __( 'Page %1$d of %2$d', 'ai-featured-image-generator' ), 'pro' => __( 'Pro', 'ai-featured-image-generator' ), 'upgrade' => __( 'Upgrade to Pro to see and fix them all', 'ai-featured-image-generator' ), ), ) ); } } /** * Describe what a bulk run will cost, for the pre-run confirmation. * * Only prices the plugin already states in its own model labels are returned. * Every other provider gets a null price so the confirmation names who is * billing the user instead of inventing a dollar figure that would go stale. * * @param array $settings Plugin settings. * @param string $provider Active AI provider slug. * @return array { * @type string $provider_label Human-readable provider name. * @type string $model_label Human-readable model name, or '' when unknown. * @type float|null $price_per_image Known price per image, or null. * } */ private function get_bulk_cost_context( $settings, $provider ) { return self::get_cost_context( $settings, $provider ); } /** * Provider, model and (when it is fixed) price of the next image, for the * places that spend the user's API money: Bulk, the editor box, the Posts list * and the block. Prices are only given where the provider charges one flat * amount per image; elsewhere it depends on size and quality. * * @param array|null $settings Plugin settings, or null to read them. * @param string|null $provider Provider slug, or null for the active one. * @return array provider_label, model_label, price_per_image (float|null), line (sentence) */ public static function get_cost_context( $settings = null, $provider = null ) { if ( null === $settings ) { // A plain reader: a full instance would add the admin hooks a second time. $settings = ( new self(false) )->get_settings(); } if ( null === $provider ) { $provider = ( isset( $settings['ai_provider'] ) ? $settings['ai_provider'] : 'openai' ); } if ( !Providers::is_available( $provider ) ) { $provider = Providers::OPENAI; } $model = ( isset( $settings['ai_model'] ) ? (string) $settings['ai_model'] : '' ); // A retired model is billed as its replacement, so the line names that one. if ( isset( self::RETIRED_MODEL_REPLACEMENTS[$model] ) ) { $model = self::RETIRED_MODEL_REPLACEMENTS[$model]; } if ( !Providers::is_model_available( $provider, $model ) ) { $model = Providers::default_model( $provider ); } $known_prices = array( 'grok-imagine-image' => 0.02, 'grok-imagine-image-2.0' => 0.04, 'muse-image-1.0' => 0.01, ); $models = Providers::image_models( $provider ); $model_label = ( isset( $models[$model] ) ? trim( preg_replace( '/\\s*\\(.*\\)$/', '', $models[$model] ) ) : '' ); $price = ( isset( $known_prices[$model] ) ? $known_prices[$model] : null ); $provider_label = Providers::label( $provider ); $name = ( $model_label !== '' ? $model_label : $provider_label ); if ( null !== $price ) { $line = sprintf( /* translators: 1: model name, 2: price such as $0.02, 3: provider name */ __( 'Uses %1$s, about %2$s per image, billed to your %3$s account.', 'ai-featured-image-generator' ), $name, '$' . number_format( $price, 2 ), $provider_label ); } else { $line = sprintf( /* translators: 1: model name, 2: provider name */ __( 'Uses %1$s. Each image is billed to your %2$s account.', 'ai-featured-image-generator' ), $name, $provider_label ); } return array( 'provider_label' => $provider_label, 'model_label' => $model_label, 'price_per_image' => $price, 'line' => $line, ); } /** * Add settings page to WordPress admin. */ public function add_settings_page() { add_menu_page( __( 'AI Images', 'ai-featured-image-generator' ), __( 'AI Images', 'ai-featured-image-generator' ), self::capability(), $this->page, array($this, 'render_settings_page'), 'dashicons-format-image', 80 ); // Add Settings submenu (points to same page, replaces default submenu) add_submenu_page( $this->page, __( 'Settings', 'ai-featured-image-generator' ), __( 'Settings', 'ai-featured-image-generator' ), self::capability(), $this->page, array($this, 'render_settings_page') ); // Bulk Generate is a task, not a setting, so it gets its own menu entry that // opens the Bulk tab directly. add_submenu_page( $this->page, __( 'Bulk Generate', 'ai-featured-image-generator' ), __( 'Bulk Generate', 'ai-featured-image-generator' ), self::capability(), 'admin.php?page=' . $this->page . '&tab=bulk' ); // Image Audit is a task too: find and fix image problems. add_submenu_page( $this->page, __( 'Image Audit', 'ai-featured-image-generator' ), __( 'Image Audit', 'ai-featured-image-generator' ), self::capability(), 'admin.php?page=' . $this->page . '&tab=audit' ); } /** * Register settings. */ public function register_settings() { register_setting( $this->page, $this->option, array( 'sanitize_callback' => array($this, 'sanitize_settings'), ) ); // ===================== // General Tab // ===================== add_settings_section( 'aifi_general_api_section', __( 'API Configuration', 'ai-featured-image-generator' ), array($this, 'render_general_api_section'), $this->page . '-general' ); // AI Provider add_settings_field( 'ai_provider', __( 'AI Provider', 'ai-featured-image-generator' ), array($this, 'render_provider_field'), $this->page . '-general', 'aifi_general_api_section', array( 'label_for' => 'aifi-provider', ) ); // AI Model add_settings_field( 'ai_model', __( 'AI Model', 'ai-featured-image-generator' ), array($this, 'render_model_field'), $this->page . '-general', 'aifi_general_api_section', array( 'label_for' => 'aifi-model', ) ); // OpenAI API Key add_settings_field( 'openai_api_key', __( 'OpenAI API Key', 'ai-featured-image-generator' ), array($this, 'render_openai_api_key_field'), $this->page . '-general', 'aifi_general_api_section', array( 'class' => 'aifi-field-row aifi-openai-only', 'label_for' => 'aifi-openai-api-key', ) ); // Gemini API Key - Only for premium users if ( Providers::can_use_premium() ) { add_settings_field( 'gemini_api_key', __( 'Gemini API Key', 'ai-featured-image-generator' ), array($this, 'render_gemini_api_key_field'), $this->page . '-general', 'aifi_general_api_section', array( 'class' => 'aifi-field-row aifi-gemini-only', 'label_for' => 'aifi-gemini-api-key', ) ); // Meta Muse API Key - Only for premium users add_settings_field( 'muse_api_key', __( 'Meta Muse API Key (Beta)', 'ai-featured-image-generator' ), array($this, 'render_muse_api_key_field'), $this->page . '-general', 'aifi_general_api_section', array( 'class' => 'aifi-field-row aifi-muse-only', 'label_for' => 'aifi-muse-api-key', ) ); } // Grok API Key add_settings_field( 'grok_api_key', __( 'Grok / xAI API Key', 'ai-featured-image-generator' ), array($this, 'render_grok_api_key_field'), $this->page . '-general', 'aifi_general_api_section', array( 'class' => 'aifi-field-row aifi-grok-only', 'label_for' => 'aifi-grok-api-key', ) ); // Every key at a glance. The rows above show only the chosen provider's key, but // Audio and Product Images can use other keys, so each one can be opened here. add_settings_field( 'aifi_other_keys', __( 'Other API Keys', 'ai-featured-image-generator' ), array($this, 'render_other_keys_field'), $this->page . '-general', 'aifi_general_api_section', array( 'class' => 'aifi-field-row aifi-other-keys-row', ) ); // Alt Text Section add_settings_section( 'aifi_general_alt_text_section', __( 'Alt Text', 'ai-featured-image-generator' ), array($this, 'render_general_alt_text_section'), $this->page . '-general' ); // Auto Alt Text toggle add_settings_field( 'auto_alt_text', __( 'Auto Alt Text', 'ai-featured-image-generator' ), array($this, 'render_auto_alt_text_field'), $this->page . '-general', 'aifi_general_alt_text_section' ); // Alt Text Source add_settings_field( 'alt_text_source', __( 'Alt Text Source', 'ai-featured-image-generator' ), array($this, 'render_alt_text_source_field'), $this->page . '-general', 'aifi_general_alt_text_section', array( 'class' => 'aifi-field-row aifi-alt-text-enabled-only', 'label_for' => 'aifi-alt-text-source', ) ); // Custom Format add_settings_field( 'alt_text_custom_format', __( 'Custom Format', 'ai-featured-image-generator' ), array($this, 'render_alt_text_custom_format_field'), $this->page . '-general', 'aifi_general_alt_text_section', array( 'class' => 'aifi-field-row aifi-alt-text-enabled-only aifi-alt-text-custom-only', 'label_for' => 'aifi-alt-text-custom-format', ) ); // Overwrite Alt Text on Regeneration add_settings_field( 'alt_text_overwrite', __( 'Overwrite Alt Text on Regeneration', 'ai-featured-image-generator' ), array($this, 'render_alt_text_overwrite_field'), $this->page . '-general', 'aifi_general_alt_text_section', array( 'class' => 'aifi-field-row aifi-alt-text-enabled-only', ) ); // Availability Section add_settings_section( 'aifi_general_availability_section', __( 'Availability', 'ai-featured-image-generator' ), array($this, 'render_general_availability_section'), $this->page . '-general' ); // Enabled Post Types add_settings_field( 'enabled_post_types', __( 'Enabled Post Types', 'ai-featured-image-generator' ), array($this, 'render_enabled_post_types_field'), $this->page . '-general', 'aifi_general_availability_section' ); // ===================== // Featured Images Tab // ===================== add_settings_section( 'aifi_featured_section', __( 'Generation Defaults', 'ai-featured-image-generator' ), array($this, 'render_featured_section'), $this->page . '-featured' ); // Default Size (OpenAI) add_settings_field( 'default_size', __( 'Default Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_size_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-openai-only', ) ); // Gemini Aspect Ratio - Only for premium users if ( Providers::can_use_premium() ) { add_settings_field( 'gemini_aspect_ratio', __( 'Default Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_gemini_aspect_ratio_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-gemini-only', 'label_for' => 'gemini_aspect_ratio', ) ); // Gemini Image Size add_settings_field( 'gemini_image_size', __( 'Default Image Quality', 'ai-featured-image-generator' ), array($this, 'render_gemini_image_size_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-gemini-only aifi-gemini-3-pro-only', 'label_for' => 'gemini_image_size', ) ); // Gemini Output Format (WebP conversion is a Pro feature) add_settings_field( 'gemini_output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_gemini_output_format_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-gemini-only', 'label_for' => 'gemini_output_format', ) ); } // Grok Aspect Ratio add_settings_field( 'grok_aspect_ratio', __( 'Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_grok_aspect_ratio_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-grok-only', 'label_for' => 'grok_aspect_ratio', ) ); // Grok Resolution add_settings_field( 'grok_resolution', __( 'Resolution', 'ai-featured-image-generator' ), array($this, 'render_grok_resolution_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-grok-only', 'label_for' => 'grok_resolution', ) ); // Grok Output Format (shown to free users as a Pro teaser for WebP) add_settings_field( 'grok_output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_grok_output_format_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-grok-only', 'label_for' => 'grok_output_format', ) ); // Meta Muse (Pro only) if ( Providers::can_use_premium() ) { add_settings_field( 'muse_aspect_ratio', __( 'Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_muse_aspect_ratio_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-muse-only', 'label_for' => 'muse_aspect_ratio', ) ); add_settings_field( 'muse_output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_muse_output_format_field'), $this->page . '-featured', 'aifi_featured_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-muse-only', 'label_for' => 'muse_output_format', ) ); } // Default Style add_settings_field( 'default_style', __( 'Default Style', 'ai-featured-image-generator' ), array($this, 'render_style_field'), $this->page . '-featured', 'aifi_featured_section', array( 'label_for' => 'default_style', ) ); // Allow Text on Image add_settings_field( 'allow_text', __( 'Allow text on generated image', 'ai-featured-image-generator' ), array($this, 'render_allow_text_field'), $this->page . '-featured', 'aifi_featured_section' ); // Custom Styles now lives on the Brand & Styles tab; see below. // Output Settings Section add_settings_section( 'aifi_featured_output_section', __( 'Output Settings', 'ai-featured-image-generator' ), array($this, 'render_featured_output_section'), $this->page . '-featured' ); // Output Format add_settings_field( 'output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_output_format_field'), $this->page . '-featured', 'aifi_featured_output_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-openai-only', ) ); // Image Quality (only for lossy formats: WebP, JPEG) add_settings_field( 'image_quality', __( 'Output Compression (WebP/JPEG)', 'ai-featured-image-generator' ), array($this, 'render_quality_field'), $this->page . '-featured', 'aifi_featured_output_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-openai-only aifi-lossy-format-only', ) ); // Generation Quality (OpenAI, all GPT Image models) add_settings_field( 'openai_quality', __( 'Generation Quality', 'ai-featured-image-generator' ), array($this, 'render_openai_quality_field'), $this->page . '-featured', 'aifi_featured_output_section', array( 'class' => 'aifi-field-row aifi-openai-only', ) ); // Planning detail (GPT Image 2.6 only) add_settings_field( 'openai_complexity', __( 'Planning Detail', 'ai-featured-image-generator' ), array($this, 'render_openai_complexity_field'), $this->page . '-featured', 'aifi_featured_output_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-openai-only', 'label_for' => 'aifi-openai-complexity', ) ); // Background (gpt-image-2 only) add_settings_field( 'openai_background', __( 'Background', 'ai-featured-image-generator' ), array($this, 'render_openai_background_field'), $this->page . '-featured', 'aifi_featured_output_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-openai-only aifi-openai-gpt2-field', ) ); // Automation Section: auto-generate on publish (Pro; Free sees it locked). add_settings_section( 'aifi_featured_automation_section', __( 'Automation', 'ai-featured-image-generator' ), '__return_false', $this->page . '-featured' ); add_settings_field( 'auto_generate_on_publish', __( 'Auto-generate on Publish', 'ai-featured-image-generator' ), array($this, 'render_auto_generate_field'), $this->page . '-featured', 'aifi_featured_automation_section' ); // Animated images (GPT Image 2.6, Pro). if ( Providers::can_use_premium() ) { add_settings_section( 'aifi_featured_animated_section', __( 'Animated Images', 'ai-featured-image-generator' ), array($this, 'render_animated_section'), $this->page . '-featured' ); add_settings_field( 'openai_gif_frames', __( 'Frames', 'ai-featured-image-generator' ), array($this, 'render_gif_frames_field'), $this->page . '-featured', 'aifi_featured_animated_section', array( 'label_for' => 'aifi-gif-frames', ) ); add_settings_field( 'openai_gif_frame_ms', __( 'Frame Speed', 'ai-featured-image-generator' ), array($this, 'render_gif_frame_ms_field'), $this->page . '-featured', 'aifi_featured_animated_section', array( 'label_for' => 'aifi-gif-frame-ms', ) ); } // ===================== // Inline Images Tab (Pro) // ===================== add_settings_section( 'aifi_inline_section', __( 'Generation Defaults', 'ai-featured-image-generator' ), array($this, 'render_inline_section'), $this->page . '-inline' ); // Inline Default Style add_settings_field( 'inline_default_style', __( 'Default Style', 'ai-featured-image-generator' ), array($this, 'render_inline_style_field'), $this->page . '-inline', 'aifi_inline_section', array( 'label_for' => 'inline_default_style', ) ); // Inline Default Aspect Ratio / Size (provider-specific, matching Featured tab pattern) add_settings_field( 'inline_default_size', __( 'Default Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_inline_size_field'), $this->page . '-inline', 'aifi_inline_section', array( 'class' => 'aifi-field-row aifi-openai-only', ) ); if ( Providers::can_use_premium() ) { add_settings_field( 'inline_gemini_aspect_ratio', __( 'Default Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_inline_gemini_aspect_ratio_field'), $this->page . '-inline', 'aifi_inline_section', array( 'class' => 'aifi-field-row aifi-gemini-only', 'label_for' => 'inline_gemini_aspect_ratio', ) ); // Inline Gemini Output Format (Pro, WebP conversion) add_settings_field( 'inline_gemini_output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_inline_gemini_output_format_field'), $this->page . '-inline', 'aifi_inline_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-gemini-only', 'label_for' => 'inline_gemini_output_format', ) ); } add_settings_field( 'inline_grok_aspect_ratio', __( 'Default Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_inline_grok_aspect_ratio_field'), $this->page . '-inline', 'aifi_inline_section', array( 'class' => 'aifi-field-row aifi-grok-only', 'label_for' => 'inline_grok_aspect_ratio', ) ); // Inline Grok Output Format (shown to free users as a Pro teaser for WebP) add_settings_field( 'inline_grok_output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_inline_grok_output_format_field'), $this->page . '-inline', 'aifi_inline_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-grok-only', 'label_for' => 'inline_grok_output_format', ) ); // Inline Meta Muse (Pro only) if ( Providers::can_use_premium() ) { add_settings_field( 'inline_muse_aspect_ratio', __( 'Default Aspect Ratio', 'ai-featured-image-generator' ), array($this, 'render_inline_muse_aspect_ratio_field'), $this->page . '-inline', 'aifi_inline_section', array( 'class' => 'aifi-field-row aifi-muse-only', 'label_for' => 'inline_muse_aspect_ratio', ) ); add_settings_field( 'inline_muse_output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_inline_muse_output_format_field'), $this->page . '-inline', 'aifi_inline_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-muse-only', 'label_for' => 'inline_muse_output_format', ) ); } // Inline Output Settings Section add_settings_section( 'aifi_inline_output_section', __( 'Output Settings', 'ai-featured-image-generator' ), array($this, 'render_inline_output_section'), $this->page . '-inline' ); // Inline Output Format (OpenAI only) add_settings_field( 'inline_output_format', __( 'Output Format', 'ai-featured-image-generator' ), array($this, 'render_inline_output_format_field'), $this->page . '-inline', 'aifi_inline_output_section', array( 'class' => 'aifi-field-row aifi-advanced-row aifi-openai-only', 'label_for' => 'inline_output_format', ) ); // ===================== // Brand Kit Tab (Pro) // ===================== add_settings_section( 'aifi_brand_kit', __( 'Brand Kit', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_section'), $this->page . '-brand' ); if ( Providers::can_use_premium() ) { add_settings_field( 'brand_kit_enabled', __( 'Enable Brand Kit', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_enabled_field'), $this->page . '-brand', 'aifi_brand_kit' ); add_settings_field( 'brand_kit_colors', __( 'Brand Colors', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_colors_field'), $this->page . '-brand', 'aifi_brand_kit' ); add_settings_field( 'brand_kit_description', __( 'Style Description', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_description_field'), $this->page . '-brand', 'aifi_brand_kit', array( 'label_for' => 'brand_kit_description', ) ); add_settings_field( 'brand_kit_tone', __( 'Tone', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_tone_field'), $this->page . '-brand', 'aifi_brand_kit', array( 'label_for' => 'brand_kit_tone', ) ); add_settings_field( 'brand_kit_avoid', __( 'Avoid', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_avoid_field'), $this->page . '-brand', 'aifi_brand_kit', array( 'label_for' => 'brand_kit_avoid', ) ); add_settings_field( 'brand_kit_logo_id', __( 'Logo', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_logo_field'), $this->page . '-brand', 'aifi_brand_kit' ); add_settings_field( 'brand_kit_logo_placement', __( 'Logo Placement', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_logo_placement_field'), $this->page . '-brand', 'aifi_brand_kit' ); add_settings_field( 'brand_kit_reference_id', __( 'Reference Image (Gemini only)', 'ai-featured-image-generator' ), array($this, 'render_brand_kit_reference_field'), $this->page . '-brand', 'aifi_brand_kit' ); } // ===================== // Custom Styles (Pro), same tab, deliberately its own section. // // These are not part of the Brand Kit: the kit is applied to every image // automatically, while custom styles only add entries to the Style dropdowns // for the user to pick. Kept visually separate so "Enable Brand Kit" is not // mistaken for a switch that also turns these off. // // They belong on this tab rather than Featured Images because they feed every // surface, the block, the post editor meta box, and both style dropdowns. // ===================== add_settings_section( 'aifi_custom_styles', __( 'Custom Styles', 'ai-featured-image-generator' ), array($this, 'render_custom_styles_section'), $this->page . '-brand' ); add_settings_field( 'custom_styles', __( 'Custom Styles', 'ai-featured-image-generator' ), array($this, 'render_custom_styles_field'), $this->page . '-brand', 'aifi_custom_styles' ); // ===================== // Product Images Tab (Pro): store images from a product's real photo // ===================== add_settings_section( 'aifi_products_intro', __( 'Product Images', 'ai-featured-image-generator' ), array($this, 'render_products_intro_section'), $this->page . '-products' ); if ( Providers::can_use_premium() ) { add_settings_section( 'aifi_products_look', __( 'Style', 'ai-featured-image-generator' ), '__return_false', $this->page . '-products' ); add_settings_field( 'product_mode', __( 'Shot for Single Images', 'ai-featured-image-generator' ), array($this, 'render_product_select_field'), $this->page . '-products', 'aifi_products_look', array( 'key' => 'product_mode', 'list' => 'modes', 'label_for' => 'aifi-product_mode', ) ); add_settings_field( 'product_background', __( 'Background', 'ai-featured-image-generator' ), array($this, 'render_product_background_field'), $this->page . '-products', 'aifi_products_look' ); add_settings_field( 'product_style', __( 'Mood', 'ai-featured-image-generator' ), array($this, 'render_product_select_field'), $this->page . '-products', 'aifi_products_look', array( 'key' => 'product_style', 'list' => 'styles', 'label_for' => 'aifi-product_style', ) ); add_settings_field( 'product_add_logo', __( 'Logo', 'ai-featured-image-generator' ), array($this, 'render_product_add_logo_field'), $this->page . '-products', 'aifi_products_look' ); add_settings_section( 'aifi_products_model', __( 'Model and Size', 'ai-featured-image-generator' ), '__return_false', $this->page . '-products' ); add_settings_field( 'product_model', __( 'Model', 'ai-featured-image-generator' ), array($this, 'render_product_select_field'), $this->page . '-products', 'aifi_products_model', array( 'key' => 'product_model', 'list' => 'models', 'label_for' => 'aifi-product_model', ) ); add_settings_field( 'product_size', __( 'Size', 'ai-featured-image-generator' ), array($this, 'render_product_select_field'), $this->page . '-products', 'aifi_products_model', array( 'key' => 'product_size', 'list' => 'sizes', 'label_for' => 'aifi-product_size', ) ); add_settings_section( 'aifi_products_set', __( 'Product Set', 'ai-featured-image-generator' ), '__return_false', $this->page . '-products' ); add_settings_field( 'product_set', __( 'Images in a Set', 'ai-featured-image-generator' ), array($this, 'render_product_set_field'), $this->page . '-products', 'aifi_products_set', array( 'label_for' => 'aifi-product-set-count', ) ); } // ===================== // Audio Tab (Pro): Listen to this post // ===================== add_settings_section( 'aifi_audio', __( 'Listen to this post', 'ai-featured-image-generator' ), array($this, 'render_audio_section'), $this->page . '-audio' ); if ( Providers::can_use_premium() ) { $audio_settings = $this->get_settings(); $audio_provider = ( isset( $audio_settings['audio_provider'] ) && in_array( $audio_settings['audio_provider'], Audio::PROVIDERS, true ) ? $audio_settings['audio_provider'] : Audio::DEFAULT_PROVIDER ); // Rows for the other providers start hidden; settings.js swaps them when the choice changes. $audio_row = function ( $for ) use($audio_provider) { return 'aifi-field-row aifi-audio-for aifi-audio-for-' . $for . (( $for !== $audio_provider ? ' aifi-audio-off' : '' )); }; add_settings_field( 'audio_enabled', __( 'Player', 'ai-featured-image-generator' ), array($this, 'render_audio_enabled_field'), $this->page . '-audio', 'aifi_audio' ); add_settings_field( 'audio_provider', __( 'Voice provider', 'ai-featured-image-generator' ), array($this, 'render_audio_provider_field'), $this->page . '-audio', 'aifi_audio' ); add_settings_field( 'elevenlabs_api_key', __( 'ElevenLabs API Key', 'ai-featured-image-generator' ), array($this, 'render_elevenlabs_api_key_field'), $this->page . '-audio', 'aifi_audio', array( 'class' => $audio_row( 'elevenlabs' ), 'label_for' => 'aifi-elevenlabs-api-key', ) ); add_settings_field( 'elevenlabs_model', __( 'ElevenLabs model', 'ai-featured-image-generator' ), array($this, 'render_elevenlabs_model_field'), $this->page . '-audio', 'aifi_audio', array( 'class' => $audio_row( 'elevenlabs' ), 'label_for' => 'aifi-elevenlabs-model', ) ); add_settings_field( 'elevenlabs_voice', __( 'ElevenLabs voice', 'ai-featured-image-generator' ), array($this, 'render_elevenlabs_voice_field'), $this->page . '-audio', 'aifi_audio', array( 'class' => $audio_row( 'elevenlabs' ), 'label_for' => 'aifi-elevenlabs-voice', ) ); add_settings_field( 'grok_voice', __( 'Grok voice', 'ai-featured-image-generator' ), array($this, 'render_grok_voice_field'), $this->page . '-audio', 'aifi_audio', array( 'class' => $audio_row( 'grok' ), 'label_for' => 'aifi-grok-voice', ) ); add_settings_field( 'gemini_tts_model', __( 'Gemini model', 'ai-featured-image-generator' ), array($this, 'render_gemini_tts_model_field'), $this->page . '-audio', 'aifi_audio', array( 'class' => $audio_row( 'gemini' ), 'label_for' => 'aifi-gemini-tts-model', ) ); add_settings_field( 'gemini_voice', __( 'Gemini voice', 'ai-featured-image-generator' ), array($this, 'render_gemini_voice_field'), $this->page . '-audio', 'aifi_audio', array( 'class' => $audio_row( 'gemini' ), 'label_for' => 'aifi-gemini-voice', ) ); add_settings_field( 'audio_label', __( 'Player label', 'ai-featured-image-generator' ), array($this, 'render_audio_label_field'), $this->page . '-audio', 'aifi_audio', array( 'label_for' => 'aifi-audio-label', ) ); add_settings_field( 'audio_post_types', __( 'Post types', 'ai-featured-image-generator' ), array($this, 'render_audio_post_types_field'), $this->page . '-audio', 'aifi_audio' ); // Podcast feed: the recordings as a podcast that apps can follow. add_settings_section( 'aifi_podcast', __( 'Podcast feed', 'ai-featured-image-generator' ), array($this, 'render_podcast_section'), $this->page . '-audio' ); add_settings_field( 'podcast_enabled', __( 'Podcast feed', 'ai-featured-image-generator' ), array($this, 'render_podcast_enabled_field'), $this->page . '-audio', 'aifi_podcast' ); $podcast_text_fields = array( 'podcast_title' => __( 'Show title', 'ai-featured-image-generator' ), 'podcast_description' => __( 'Show description', 'ai-featured-image-generator' ), 'podcast_author' => __( 'Author', 'ai-featured-image-generator' ), 'podcast_email' => __( 'Owner email', 'ai-featured-image-generator' ), ); foreach ( $podcast_text_fields as $key => $label ) { add_settings_field( $key, $label, array($this, 'render_podcast_text_field'), $this->page . '-audio', 'aifi_podcast', array( 'key' => $key, 'label_for' => 'aifi-' . str_replace( '_', '-', $key ), ) ); } add_settings_field( 'podcast_image', __( 'Cover image', 'ai-featured-image-generator' ), array($this, 'render_podcast_image_field'), $this->page . '-audio', 'aifi_podcast' ); add_settings_field( 'podcast_category', __( 'Category', 'ai-featured-image-generator' ), array($this, 'render_podcast_category_field'), $this->page . '-audio', 'aifi_podcast', array( 'label_for' => 'aifi-podcast-category', ) ); add_settings_field( 'podcast_explicit', __( 'Explicit content', 'ai-featured-image-generator' ), array($this, 'render_podcast_explicit_field'), $this->page . '-audio', 'aifi_podcast' ); add_settings_field( 'podcast_limit', __( 'Episodes in the feed', 'ai-featured-image-generator' ), array($this, 'render_podcast_limit_field'), $this->page . '-audio', 'aifi_podcast', array( 'label_for' => 'aifi-podcast-limit', ) ); } } /** * Intro copy for the Custom Styles section. */ public function render_custom_styles_section() { echo '
' . esc_html__( 'Styles you can choose per image. Unlike the Brand Kit above, these are not applied automatically. They appear as options in every Style dropdown: the post editor, the AI Image Generator block, and bulk generation.', 'ai-featured-image-generator' ) . '
'; } /** * Render settings page. */ public function render_settings_page() { if ( !current_user_can( self::capability() ) ) { return; } $is_premium = Providers::can_use_premium(); $settings = $this->get_settings(); $has_api_key = Providers::has_any_api_key( $settings ); // Determine active tab (default to 'general'). An unknown tab falls back to 'general'. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only choice of the tab to show. $active_tab = ( isset( $_GET['tab'] ) && is_string( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'general' ); if ( !in_array( $active_tab, self::TABS, true ) ) { $active_tab = 'general'; } // Inline images now share the Post Images tab with featured images; old links still work and // open the Inline side of the tab's switch. $images_side = ( 'inline' === $active_tab ? 'inline' : 'featured' ); if ( 'inline' === $active_tab ) { $active_tab = 'featured'; } // Product Images (Pro) shows one part at a time: Style (the settings, side=look) or Bulk // images. Free has only the locked Style card, so no switch. $has_product_tools = Providers::can_use_premium(); $products_side = ( isset( $_GET['side'] ) ? sanitize_key( wp_unslash( $_GET['side'] ) ) : 'look' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only view choice. if ( 'match' === $products_side ) { // Old links to Make them match: fixing photos lives in Bulk images now. $products_side = 'make'; } if ( !$has_product_tools || !in_array( $products_side, array('look', 'make'), true ) ) { $products_side = 'look'; } // Bulk Generate and Image Audit are tools with their own menu entries, not settings tabs. $is_tool = in_array( $active_tab, array('bulk', 'audit'), true ); if ( 'bulk' === $active_tab ) { $page_title = __( 'Bulk Generate', 'ai-featured-image-generator' ); } elseif ( 'audit' === $active_tab ) { $page_title = __( 'Image Audit', 'ai-featured-image-generator' ); } else { $page_title = __( 'AI Featured Image Generator', 'ai-featured-image-generator' ); } ?>
Node.js' ); ?>
>
>
", shown highlighted */ esc_html__( 'Start each request with %s. It works in every app.', 'ai-featured-image-generator' ), '' . esc_html( $demo_first['prefix'] ) . '' ); ?>
{).', 'ai-featured-image-generator' ),
'text' => $section,
),
'empty' => array(
'label' => __( 'Nothing, or just {}', 'ai-featured-image-generator' ),
'how' => __( 'Replace everything in the file with this.', 'ai-featured-image-generator' ),
'text' => $json,
),
'servers' => array(
'label' => __( 'Other MCP servers', 'ai-featured-image-generator' ),
'how' => __( 'Paste this right after the line "mcpServers": {.', 'ai-featured-image-generator' ),
'text' => $entry . ',',
),
),
'variant_default' => 'settings',
'snippet_note' => '',
'node' => true,
),
'cursor' => array(
'label' => 'Cursor',
'steps' => array(__( 'Open Cursor, then Settings, Tools and MCP, Add Custom MCP. This opens the file below.', 'ai-featured-image-generator' ), __( 'Paste the text that matches your file. Save.', 'ai-featured-image-generator' ), __( 'Back in Cursor, a green dot means you are connected.', 'ai-featured-image-generator' )),
'file' => array(
'mac' => '~/.cursor/mcp.json',
'windows' => '%USERPROFILE%\\.cursor\\mcp.json',
),
'snippet' => array(
'step' => 1,
'copy_label' => __( 'Copy text for Cursor', 'ai-featured-image-generator' ),
'text' => $entry,
),
'variants' => array(
'empty' => array(
'label' => __( 'No servers yet', 'ai-featured-image-generator' ),
'how' => __( 'Replace everything in the file with this.', 'ai-featured-image-generator' ),
'text' => $json,
),
'servers' => array(
'label' => __( 'Other servers', 'ai-featured-image-generator' ),
'how' => __( 'Paste this right after the line "mcpServers": {.', 'ai-featured-image-generator' ),
'text' => $entry . ',',
),
),
'variant_default' => 'empty',
'snippet_note' => '',
'node' => true,
),
'claude-code' => array(
'label' => 'Claude Code',
'steps' => array(__( 'Open a terminal in any project.', 'ai-featured-image-generator' ), __( 'Paste this command and press Enter.', 'ai-featured-image-generator' ), __( 'Start Claude Code. You are connected.', 'ai-featured-image-generator' )),
'snippet' => array(
'step' => 1,
'copy_label' => __( 'Copy command for Claude Code', 'ai-featured-image-generator' ),
'text' => "claude mcp add --transport http " . $conn . " " . $server_url . " --header \"Authorization: Basic YOUR-BASE64\"",
),
'snippet_note' => __( 'YOUR-BASE64 is filled in right after you create a key in step 2.', 'ai-featured-image-generator' ),
'node' => false,
),
'vscode' => array(
'label' => 'VS Code',
'steps' => array(__( 'In your project, create or open .vscode/mcp.json.', 'ai-featured-image-generator' ), __( 'Paste the text that matches your file. Save.', 'ai-featured-image-generator' ), __( 'Start the server from the MCP panel. You are connected.', 'ai-featured-image-generator' )),
'snippet' => array(
'step' => 1,
'copy_label' => __( 'Copy config for VS Code', 'ai-featured-image-generator' ),
'text' => str_replace( '"mcpServers"', '"servers"', $json ),
),
'variants' => array(
'empty' => array(
'label' => __( 'New or empty file', 'ai-featured-image-generator' ),
'how' => __( 'Replace everything in the file with this.', 'ai-featured-image-generator' ),
'text' => str_replace( '"mcpServers"', '"servers"', $json ),
),
'servers' => array(
'label' => __( 'Other servers', 'ai-featured-image-generator' ),
'how' => __( 'Paste this right after the line "servers": {.', 'ai-featured-image-generator' ),
'text' => $entry . ',',
),
),
'variant_default' => 'empty',
'snippet_note' => '',
'node' => true,
),
'other' => array(
'label' => __( 'Other', 'ai-featured-image-generator' ),
'steps' => array(__( 'Any app that supports MCP over HTTP with a custom header can connect. Give it this address and this header. YOUR-BASE64 is your username and key joined with a colon, base64 encoded; it is filled in once you create a key.', 'ai-featured-image-generator' )),
'snippet' => array(
'step' => 0,
'copy_label' => __( 'Copy address and header', 'ai-featured-image-generator' ),
'text' => $server_url . "\nAuthorization: Basic YOUR-BASE64",
),
'snippet_note' => __( 'claude.ai in the browser and ChatGPT cannot connect yet: they need a sign-in method the MCP Adapter does not provide.', 'ai-featured-image-generator' ),
'node' => false,
),
);
}
/**
* What the assistant can do, split by plan and grouped by job, for the Your plan
* card on Free sites. Written as use cases, covering every tool in
* Abilities::FREE_ABILITIES and PRO_ABILITIES; keep it in step when tools change.
*
* @param int $limit Free assistant images.
* @return array 'free' and 'pro', each group label => lines.
*/
private function get_mcp_plan_features( $limit ) {
// The image limit first, then the strongest uses, in both groups and lines. marks the bulk part of a
// line: one request that covers many posts or images.
return array(
'free' => array(
sprintf( __( 'AI images (%d in total, not per month)', 'ai-featured-image-generator' ), $limit ) => array(__( 'Make featured images for any post', 'ai-featured-image-generator' ), __( 'Make an image for your Media Library', 'ai-featured-image-generator' )),
__( 'Image health (unlimited)', 'ai-featured-image-generator' ) => array(
__( 'Get a health report on all your images', 'ai-featured-image-generator' ),
__( 'List every image missing alt text', 'ai-featured-image-generator' ),
__( 'List heavy, blurry or cut-off images', 'ai-featured-image-generator' ),
__( 'Let the assistant look at an image', 'ai-featured-image-generator' )
),
__( 'Alt text and captions (unlimited)', 'ai-featured-image-generator' ) => array(__( 'Write alt text for every image missing it', 'ai-featured-image-generator' ), __( 'Edit image titles, captions and descriptions', 'ai-featured-image-generator' )),
__( 'Search (unlimited)', 'ai-featured-image-generator' ) => array(__( 'List posts by category, author or date', 'ai-featured-image-generator' ), __( 'Find Media Library images by name', 'ai-featured-image-generator' ), __( 'Read a post to write a fitting image prompt', 'ai-featured-image-generator' )),
),
'pro' => array(
__( 'AI images', 'ai-featured-image-generator' ) => array(__( 'Make unlimited images', 'ai-featured-image-generator' ), __( 'Regenerate a whole category at once', 'ai-featured-image-generator' ), __( 'Use any AI provider and model', 'ai-featured-image-generator' )),
__( 'Image tools', 'ai-featured-image-generator' ) => array(
__( 'Convert your whole library to WebP', 'ai-featured-image-generator' ),
__( 'Rename every image file for SEO', 'ai-featured-image-generator' ),
__( 'Match every post to a fitting library photo', 'ai-featured-image-generator' ),
__( 'Break up long posts with images', 'ai-featured-image-generator' ),
__( 'Remove backgrounds for product shots', 'ai-featured-image-generator' )
),
__( 'Brand Kit', 'ai-featured-image-generator' ) => array(
__( 'Stamp your logo on every post, or undo', 'ai-featured-image-generator' ),
__( 'Refresh every image after a rebrand', 'ai-featured-image-generator' ),
__( 'Build a Brand Kit from your site', 'ai-featured-image-generator' ),
__( 'Change colours, tone and style', 'ai-featured-image-generator' ),
__( 'Place your logo and preview it', 'ai-featured-image-generator' )
),
),
);
}
/**
* Requests the How to use demo types out, with what the assistant replies.
*
* The "On page . '&tab=general' ) ) . '">' . esc_html__( 'Alt Text settings on the General tab', 'ai-featured-image-generator' ) . '' ); ?>
product_images ) { return; } $plan = $this->product_images->set_plan(); $terms = $this->product_cat_terms(); ?>
page . '&tab=products&side=look' ) ) . '" data-aifi-pside="look">' . esc_html__( 'Style', 'ai-featured-image-generator' ) . '' ); ?>
$this->page,
'tab' => 'mcp',
), admin_url( 'admin.php' ) ) ) . '">' . esc_html__( 'Connect an AI assistant (MCP)', 'ai-featured-image-generator' ) . '',
'' . esc_html( sprintf(
/* translators: %s: this site's name */
__( 'On %s, add featured images to every post that is missing one.', 'ai-featured-image-generator' ),
Abilities::site_label()
) ) . '
'
);
?>
get_settings(); $source = ( isset( $settings['alt_text_source'] ) ? $settings['alt_text_source'] : 'post_title' ); $sources = array( 'post_title' => __( 'Post Title', 'ai-featured-image-generator' ), 'prompt' => __( 'Image Prompt', 'ai-featured-image-generator' ), 'custom' => __( 'Custom Format', 'ai-featured-image-generator' ), ); ?>
get_settings(); $format = ( isset( $settings['alt_text_custom_format'] ) ? $settings['alt_text_custom_format'] : '{title}' ); ?>
{title}: ,
{prompt}: ,
{style}: ,
{site_name}:
' . esc_html( '{title} - {style} style - {site_name}' ) . '' ); ?>
get_settings(); $overwrite = ( isset( $settings['alt_text_overwrite'] ) ? (bool) $settings['alt_text_overwrite'] : true ); ?>get_settings(); $current_provider = ( isset( $settings['ai_provider'] ) ? $settings['ai_provider'] : 'openai' ); // Provider capabilities data $capabilities = array( 'openai' => array( 'formats' => 'PNG, JPEG, WebP', 'sizes' => '7 sizes (1:1, 2:3, 3:2, 16:9, 9:16, 3:1, 1:3)', 'quality' => true, 'styles' => 'Plugin styles + Custom styles', 'models' => '1 model (Free) · 3 models (Pro)', ), 'gemini' => array( 'formats' => 'PNG · WebP (Pro)', 'sizes' => '10 aspect ratios', 'quality' => false, 'styles' => 'Plugin styles + Custom styles', 'models' => '3 models', ), 'grok' => array( 'formats' => 'JPEG · WebP (Pro)', 'sizes' => '9 aspect ratios · 1K or 2K', 'quality' => false, 'styles' => 'Plugin styles + Custom styles', 'models' => '1 model (Free) · 2 models (Pro)', ), 'muse' => array( 'formats' => 'WebP, PNG, JPEG', 'sizes' => '9 aspect ratios', 'quality' => false, 'styles' => 'Plugin styles + Custom styles', 'models' => '1 model (Pro)', ), ); // On Pro, show what the site has, without the Free / Pro split. if ( Providers::can_use_premium() ) { $capabilities['openai']['models'] = '3 models'; $capabilities['gemini']['formats'] = 'PNG · WebP'; $capabilities['grok']['formats'] = 'JPEG · WebP'; $capabilities['grok']['models'] = '2 models'; $capabilities['muse']['models'] = '1 model'; } $cap = ( isset( $capabilities[$current_provider] ) ? $capabilities[$current_provider] : $capabilities['openai'] ); ?>
' . esc_html( $this->get_provider_label( $current_provider ) ) . '
+ “”,
get_settings(); $style = ( isset( $settings['inline_default_style'] ) ? $settings['inline_default_style'] : 'realistic' ); $common_styles = array( 'none' => __( 'None (No Style)', 'ai-featured-image-generator' ), 'realistic' => __( 'Realistic', 'ai-featured-image-generator' ), 'artistic' => __( 'Artistic', 'ai-featured-image-generator' ), 'cartoon' => __( 'Cartoon', 'ai-featured-image-generator' ), 'minimalist' => __( 'Minimalist', 'ai-featured-image-generator' ), ); $creative_styles = array( 'sketch' => __( 'Sketch', 'ai-featured-image-generator' ), 'watercolor' => __( 'Watercolor', 'ai-featured-image-generator' ), '3d' => __( '3D Render', 'ai-featured-image-generator' ), 'pixel' => __( 'Pixel Art', 'ai-featured-image-generator' ), 'cyberpunk' => __( 'Cyberpunk', 'ai-featured-image-generator' ), 'fantasy' => __( 'Fantasy', 'ai-featured-image-generator' ), 'anime' => __( 'Anime', 'ai-featured-image-generator' ), 'technicolor' => __( 'Technicolor', 'ai-featured-image-generator' ), ); ?> get_custom_styles(); ?> get_settings(); $size = ( isset( $settings['inline_default_size'] ) ? $settings['inline_default_size'] : '1024x1024' ); $gpt2_only_sizes = array( '1792x1024', '1024x1792', '1920x640', '640x1920' ); ?>get_settings(); $aspect_ratio = ( isset( $settings['inline_gemini_aspect_ratio'] ) ? $settings['inline_gemini_aspect_ratio'] : '16:9' ); $ratios = array( '1:1' => __( '1:1 (Square)', 'ai-featured-image-generator' ), '16:9' => __( '16:9 (Wide)', 'ai-featured-image-generator' ), '9:16' => __( '9:16 (Vertical)', 'ai-featured-image-generator' ), '3:2' => __( '3:2 (Landscape)', 'ai-featured-image-generator' ), '2:3' => __( '2:3 (Portrait)', 'ai-featured-image-generator' ), '4:3' => __( '4:3 (Landscape)', 'ai-featured-image-generator' ), '3:4' => __( '3:4 (Portrait)', 'ai-featured-image-generator' ), '5:4' => __( '5:4 (Landscape)', 'ai-featured-image-generator' ), '4:5' => __( '4:5 (Portrait)', 'ai-featured-image-generator' ), '21:9' => __( '21:9 (Cinematic)', 'ai-featured-image-generator' ), ); ?>
get_settings(); $current = ( isset( $settings['inline_grok_aspect_ratio'] ) ? $settings['inline_grok_aspect_ratio'] : '1:1' ); $ratios = array( '1:1' => __( 'Square (1:1)', 'ai-featured-image-generator' ), '16:9' => __( 'Landscape (16:9)', 'ai-featured-image-generator' ), '9:16' => __( 'Portrait (9:16)', 'ai-featured-image-generator' ), '4:3' => __( 'Landscape (4:3)', 'ai-featured-image-generator' ), '3:4' => __( 'Portrait (3:4)', 'ai-featured-image-generator' ), '3:2' => __( 'Landscape (3:2)', 'ai-featured-image-generator' ), '2:3' => __( 'Portrait (2:3)', 'ai-featured-image-generator' ), '2:1' => __( 'Wide (2:1)', 'ai-featured-image-generator' ), '1:2' => __( 'Tall (1:2)', 'ai-featured-image-generator' ), ); ?>
get_upgrade_url() : '#' ); $settings = $this->get_settings(); $default = ( $is_premium ? 'webp' : 'jpeg' ); $output_format = ( isset( $settings['inline_grok_output_format'] ) ? $settings['inline_grok_output_format'] : $default ); // WebP is Pro-only: never show it selected for free users. if ( !$is_premium && $output_format === 'webp' ) { $output_format = 'jpeg'; } $formats = array( 'jpeg' => __( 'JPEG (Original)', 'ai-featured-image-generator' ), 'webp' => __( 'WebP (Recommended)', 'ai-featured-image-generator' ), ); ?>
' . esc_html__( 'Upgrade to Pro', 'ai-featured-image-generator' ) . ''
);
?>
get_upgrade_url() ) . '">' . esc_html__( 'Upgrade to Pro', 'ai-featured-image-generator' ) . '' ); ?>
12 ? substr( $saved, -4 ) : '' )); } ?> placeholder="" data-saved="1" aria-describedby="-help">get_settings(); $items = array( 'openai' => array(__( 'OpenAI', 'ai-featured-image-generator' ), __( 'Images and Product Images.', 'ai-featured-image-generator' )), 'gemini' => array(__( 'Gemini', 'ai-featured-image-generator' ), __( 'Images, Product Images and Audio voices.', 'ai-featured-image-generator' )), 'grok' => array(__( 'Grok / xAI', 'ai-featured-image-generator' ), __( 'Images and Audio voices.', 'ai-featured-image-generator' )), 'muse' => array(__( 'Meta Muse', 'ai-featured-image-generator' ), __( 'Images.', 'ai-featured-image-generator' )), ); if ( !Providers::can_use_premium() ) { // Their key rows are Pro only, so there is nothing to open on Free. unset($items['gemini'], $items['muse']); } ?>
platform.openai.com/api-keys'
);
?>
→
console.x.ai' ); ?>
get_settings(); $this->render_api_key_input( 'muse_api_key', 'muse', ( isset( $settings['muse_api_key'] ) ? (string) $settings['muse_api_key'] : '' ) ); ?>dev.meta.ai' ); ?>
label. * * @return array */ private function get_muse_ratios() { return array( '1:1' => __( 'Square (1:1)', 'ai-featured-image-generator' ), '16:9' => __( 'Landscape (16:9)', 'ai-featured-image-generator' ), '9:16' => __( 'Portrait (9:16)', 'ai-featured-image-generator' ), '4:3' => __( 'Landscape (4:3)', 'ai-featured-image-generator' ), '3:4' => __( 'Portrait (3:4)', 'ai-featured-image-generator' ), '3:2' => __( 'Landscape (3:2)', 'ai-featured-image-generator' ), '2:3' => __( 'Portrait (2:3)', 'ai-featured-image-generator' ), '2:1' => __( 'Wide (2:1)', 'ai-featured-image-generator' ), '1:2' => __( 'Tall (1:2)', 'ai-featured-image-generator' ), ); } /** * Shared select for the Muse aspect ratio fields. * * @param string $key Settings key. * @param string $description Help text. */ private function render_muse_ratio_select( $key, $description ) { $settings = $this->get_settings(); $current = ( isset( $settings[$key] ) ? $settings[$key] : '1:1' ); ?> get_settings(); $current = ( isset( $settings[$key] ) ? $settings[$key] : 'webp' ); $formats = array( 'webp' => __( 'WebP (Recommended)', 'ai-featured-image-generator' ), 'png' => __( 'PNG', 'ai-featured-image-generator' ), 'jpeg' => __( 'JPEG', 'ai-featured-image-generator' ), ); ?> render_muse_ratio_select( 'muse_aspect_ratio', __( 'Choose the default aspect ratio for Meta Muse generated images.', 'ai-featured-image-generator' ) ); } /** * Render Meta Muse output format field (Pro only). */ public function render_muse_output_format_field() { $this->render_muse_format_select( 'muse_output_format', __( 'Meta Muse returns the chosen format directly. WebP gives the smallest files at the same visual quality.', 'ai-featured-image-generator' ) ); } /** * Render inline Meta Muse aspect ratio field (Pro only). */ public function render_inline_muse_aspect_ratio_field() { $this->render_muse_ratio_select( 'inline_muse_aspect_ratio', __( 'Choose the default aspect ratio for Meta Muse inline images.', 'ai-featured-image-generator' ) ); } /** * Render inline Meta Muse output format field (Pro only). */ public function render_inline_muse_output_format_field() { $this->render_muse_format_select( 'inline_muse_output_format', __( 'Meta Muse returns the chosen format directly. WebP gives the smallest files at the same visual quality.', 'ai-featured-image-generator' ) ); } /** * Build a warning line shown when the server cannot encode WebP. * * Returns an empty string when WebP conversion is supported, so capable * servers (the large majority) display nothing. * * @return string HTML fragment or ''. */ private function webp_capability_notice() { if ( !class_exists( '\\AIFI\\Generator' ) || \AIFI\Generator::webp_conversion_supported() ) { return ''; } return 'webp_capability_notice(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped fragment ?>
get_upgrade_url() : '#' ); $settings = $this->get_settings(); $default = ( $is_premium ? 'webp' : 'jpeg' ); $output_format = ( isset( $settings['grok_output_format'] ) ? $settings['grok_output_format'] : $default ); // WebP is Pro-only: never show it selected for free users. if ( !$is_premium && $output_format === 'webp' ) { $output_format = 'jpeg'; } $formats = array( 'jpeg' => __( 'JPEG (Original)', 'ai-featured-image-generator' ), 'webp' => __( 'WebP (Recommended)', 'ai-featured-image-generator' ), ); ?>
' . esc_html__( 'Upgrade to Pro', 'ai-featured-image-generator' ) . ''
);
?>
webp_capability_notice();
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped fragment
?>
get_settings(); $current = ( isset( $settings['grok_resolution'] ) ? $settings['grok_resolution'] : '1k' ); ?>
get_settings(); $size = ( isset( $settings['default_size'] ) ? $settings['default_size'] : '1024x1024' ); $gpt2_only_sizes = array( '1792x1024', '1024x1792', '1920x640', '640x1920' ); ?>
translated label. */ public static function get_trending_styles() { return array( 'toy' => __( 'Toy Figure', 'ai-featured-image-generator' ), 'miniature' => __( 'Miniature World', 'ai-featured-image-generator' ), 'clay' => __( 'Claymation', 'ai-featured-image-generator' ), 'cinematic' => __( 'Cinematic', 'ai-featured-image-generator' ), 'lofi' => __( 'Lo-Fi Cozy', 'ai-featured-image-generator' ), 'instant' => __( 'Vintage Instant Photo', 'ai-featured-image-generator' ), 'collage' => __( 'Magazine Collage', 'ai-featured-image-generator' ), 'papercut' => __( 'Paper Cut', 'ai-featured-image-generator' ), ); } public function get_custom_styles() { if ( !Providers::can_use_premium() ) { return array(); } $settings = $this->get_settings(); return ( isset( $settings['custom_styles'] ) ? (array) $settings['custom_styles'] : array() ); } /** * Render custom styles field (Pro only). */ public function render_custom_styles_field() { $is_premium = Providers::can_use_premium(); if ( !$is_premium ) { // One call to action per screen: the Brand Kit preview above carries it. echo '
' . esc_html__( 'Save your own style names and prompts, and pick them in every Style list. Included with Pro.', 'ai-featured-image-generator' ) . '
'; return; } $custom_styles = $this->get_custom_styles(); ?>
elevenlabs.io' ); ?>
get_settings(); $current = ( isset( $settings['elevenlabs_model'] ) ? $settings['elevenlabs_model'] : Audio::DEFAULT_ELEVENLABS_MODEL ); ?> get_settings(); $this->render_remote_voice_picker( 'elevenlabs', ( isset( $settings['elevenlabs_voice'] ) ? (string) $settings['elevenlabs_voice'] : '' ), ( isset( $settings['elevenlabs_voice_name'] ) ? (string) $settings['elevenlabs_voice_name'] : '' ), 'aifi-elevenlabs-model' ); ?> get_settings(); $voice = ( !empty( $settings['grok_voice'] ) ? (string) $settings['grok_voice'] : Audio::DEFAULT_GROK_VOICE ); $name = ( !empty( $settings['grok_voice_name'] ) ? (string) $settings['grok_voice_name'] : ucfirst( $voice ) ); $this->render_remote_voice_picker( 'grok', $voice, $name ); ?> get_settings(); $current = ( isset( $settings['gemini_tts_model'] ) ? $settings['gemini_tts_model'] : Audio::DEFAULT_GEMINI_MODEL ); ?> get_settings(); $current = ( isset( $settings['gemini_voice'] ) ? $settings['gemini_voice'] : Audio::DEFAULT_GEMINI_VOICE ); ?> get_settings(); $label = ( isset( $settings['audio_label'] ) ? (string) $settings['audio_label'] : '' ); ?> get_settings(); // Offered: the enabled types plus WooCommerce products. Ticked by default: the enabled types only. $enabled = $this->get_content_post_types(); $chosen = ( isset( $settings['audio_post_types'] ) && is_array( $settings['audio_post_types'] ) ? $settings['audio_post_types'] : $this->get_enabled_post_types() ); ?> get_settings(); ?>>
>
get_settings(); $value = ( isset( $s[$key] ) ? $s[$key] : $d[$key] ?? '' ); ?> product_images ? $this->product_images->resolve_model() : null ); if ( is_string( $fallback ) && $fallback !== (string) $value ) { $model_labels = Product_Images::model_labels(); ?>get_settings(); $kit = new Brand_Kit($this); $has_logo = $kit->has_logo(); ?>
page . '&tab=brand' ) ) . '">' . esc_html__( 'Brand & Styles', 'ai-featured-image-generator' ) . '' ); echo ' '; } esc_html_e( 'Only the logo is used. Brand colours, tone and description never change your products.', 'ai-featured-image-generator' ); ?>
get_settings(); $d = Product_Images::defaults(); $count = ( isset( $s['product_set_count'] ) ? (int) $s['product_set_count'] : $d['product_set_count'] ); $modes = Product_Images::normalize_set_modes( ( isset( $s['product_set_modes'] ) && is_array( $s['product_set_modes'] ) ? $s['product_set_modes'] : $d['product_set_modes'] ), Product_Images::SET_MAX ); $labels = Product_Images::labels()['modes']; ?>>
get_settings(); ?> get_settings(); $value = ( isset( $settings[$key] ) ? (string) $settings[$key] : '' ); $id = 'aifi-' . str_replace( '_', '-', $key ); $site = wp_strip_all_tags( html_entity_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ) ); $tagline = wp_strip_all_tags( html_entity_decode( (string) get_bloginfo( 'description' ), ENT_QUOTES, 'UTF-8' ) ); $name = esc_attr( $this->option ) . '[' . esc_attr( $key ) . ']'; if ( 'podcast_description' === $key ) { ?> get_settings(); $image_id = ( isset( $settings['podcast_image'] ) ? (int) $settings['podcast_image'] : 0 ); $this->render_media_picker( 'podcast_image', 'aifi-podcast-image', 'podcast', $image_id ); ?> get_settings(); $current = ( isset( $settings['podcast_category'] ) ? (string) $settings['podcast_category'] : Podcast::DEFAULT_CATEGORY ); ?> get_settings(); $explicit = isset( $settings['podcast_explicit'] ) && 'yes' === $settings['podcast_explicit']; ?> get_settings(); $limit = ( isset( $settings['podcast_limit'] ) ? (int) $settings['podcast_limit'] : Podcast::DEFAULT_LIMIT ); ?> get_upgrade_url() : '#' ); $img_url = AIFI_PLUGIN_URL . 'assets/img/'; ?> get_brand_kit(); $enabled = !empty( $brand_kit['enabled'] ); ?>get_brand_kit(); $colors = ( isset( $brand_kit['colors'] ) ? array_values( (array) $brand_kit['colors'] ) : array() ); ?>
get_brand_kit(); $description = ( isset( $brand_kit['description'] ) ? $brand_kit['description'] : '' ); ?>
get_brand_kit(); $tone = ( isset( $brand_kit['tone'] ) ? $brand_kit['tone'] : '' ); ?>
get_brand_kit(); $avoid = ( isset( $brand_kit['avoid'] ) ? $brand_kit['avoid'] : '' ); ?>
get_brand_kit(); $reference_id = ( isset( $brand_kit['reference_id'] ) ? (int) $brand_kit['reference_id'] : 0 ); $this->render_media_picker( 'brand_kit_reference_id', 'aifi-brand-kit-reference-id', 'reference', $reference_id ); ?>
get_brand_kit(); $logo_id = ( isset( $brand_kit['logo_id'] ) ? (int) $brand_kit['logo_id'] : 0 ); $this->render_media_picker( 'brand_kit_logo_id', 'aifi-brand-kit-logo-id', 'logo', $logo_id ); ?>
render_logo_generator(); } } /** * "Generate a logo" card under the logo picker (GPT Image 2.6, Pro). */ private function render_logo_generator() { $settings = $this->get_settings(); $has_key = Providers::has_api_key( $settings, 'openai' ); $prefill = ( isset( $settings['brand_kit_description'] ) ? (string) $settings['brand_kit_description'] : '' ); $saved = $settings['ai_model'] ?? ''; $model = ( 'openai' === ($settings['ai_provider'] ?? '') && self::is_gpt25_model( $saved ) ? $saved : 'gpt-image-2.6-flare' ); $models = Providers::image_models( 'openai' ); $label = ( isset( $models[$model] ) ? trim( preg_replace( '/\\s*\\(.*\\)$/', '', $models[$model] ) ) : $model ); ?>
get_settings(); $output_format = ( isset( $settings['output_format'] ) ? $settings['output_format'] : 'webp' ); $formats = array( 'webp' => __( 'WebP (Recommended)', 'ai-featured-image-generator' ), 'png' => 'PNG', 'jpeg' => 'JPEG', ); ?>
get_settings(); $quality = ( isset( $settings['openai_quality'] ) ? $settings['openai_quality'] : 'high' ); $options = array( 'high' => __( 'High (Default, best results)', 'ai-featured-image-generator' ), 'medium' => __( 'Medium (Faster, moderate detail)', 'ai-featured-image-generator' ), 'low' => __( 'Low (Fastest and cheapest, good for drafts)', 'ai-featured-image-generator' ), 'xhigh' => __( 'Extra High (GPT Image 2.6 only)', 'ai-featured-image-generator' ), 'max' => __( 'Max (GPT Image 2.6 only, most detail)', 'ai-featured-image-generator' ), ); ?>
get_settings(); $is_premium = Providers::can_use_premium(); $value = ( isset( $settings['openai_complexity'] ) ? $settings['openai_complexity'] : 'auto' ); $options = array( 'auto' => __( 'Auto (model default, High)', 'ai-featured-image-generator' ), 'high' => __( 'High', 'ai-featured-image-generator' ), 'medium' => __( 'Medium', 'ai-featured-image-generator' ), 'low' => __( 'Low (fastest for simple images)', 'ai-featured-image-generator' ), ); ?>
get_settings(); $frames = ( isset( $settings['openai_gif_frames'] ) ? (int) $settings['openai_gif_frames'] : 9 ); ?>
get_settings(); $ms = ( isset( $settings['openai_gif_frame_ms'] ) ? (int) $settings['openai_gif_frame_ms'] : 200 ); ?>
get_settings(); $background = ( isset( $settings['openai_background'] ) ? $settings['openai_background'] : 'auto' ); $options = array( 'auto' => __( 'Auto (Model decides)', 'ai-featured-image-generator' ), 'transparent' => __( 'Transparent cutout (subject only, saved as PNG)', 'ai-featured-image-generator' ), 'opaque' => __( 'Solid background (standard framing)', 'ai-featured-image-generator' ), ); ?>
', esc_attr( $this->option ) ); } $settings = $this->get_settings(); $enabled_post_types = ( isset( $settings['enabled_post_types'] ) ? (array) $settings['enabled_post_types'] : array('post', 'page') ); // Free users always have post and page enabled $free_post_types = array('post', 'page'); // Get all public post types $post_types = get_post_types( array( 'public' => true, ), 'objects' ); // Remove attachment from the list (media library items). WooCommerce products have // their own box on the Product Images tab, so they are never listed here. unset($post_types['attachment'], $post_types['product']); ?> (model => label). * @type array $disabled provider => model slugs shown locked. * } */ private function get_model_choices() { $models = array( 'openai' => array( 'gpt-image-2.6-flare' => 'GPT Image 2.6 Flare (Fast, Recommended)', 'gpt-image-2.6-sunburst' => 'GPT Image 2.6 Sunburst (Best Detail)', 'gpt-image-2' => 'GPT Image 2', ), 'grok' => array( 'grok-imagine-image-2.0' => 'Grok Imagine Image 2.0 ($0.04/image)', 'grok-imagine-image' => 'Grok Imagine Image ($0.02/image)', ), ); // Gemini and Muse models only exist on the Pro build. if ( function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $models['gemini'] = array( 'gemini-nano-banana-2.1' => 'Nano Banana 2.1 (Gemini, fast)', 'gemini-3.1-flash-image' => 'Nano Banana 2 (Gemini 3.1 Flash, deprecated by Google)', 'gemini-3-pro-image' => 'Nano Banana Pro (Gemini 3 Pro)', 'gemini-3.1-flash-lite-image' => 'Nano Banana 2 Lite (fastest, lowest cost, 1K)', ); $models['muse'] = array( 'muse-image-1.0' => 'Muse Image 1.0', ); } // Pro-only models that free users can see but not select. $disabled = array(); if ( !Providers::can_use_premium() ) { $disabled['grok'] = array_values( array_diff( array_keys( $models['grok'] ), array('grok-imagine-image') ) ); $disabled['openai'] = self::OPENAI_GPT25_MODELS; } return array( 'models' => $models, 'disabled' => $disabled, ); } /** * Render model field. */ public function render_model_field() { $settings = $this->get_settings(); $model = ( isset( $settings['ai_model'] ) ? $settings['ai_model'] : 'gpt-image-2' ); $provider = ( isset( $settings['ai_provider'] ) ? $settings['ai_provider'] : 'openai' ); $choices = $this->get_model_choices(); $openai_models = $choices['models']['openai']; $grok_models = $choices['models']['grok']; // Gemini and Muse are only listed on the Pro build. $gemini_models = ( isset( $choices['models']['gemini'] ) ? $choices['models']['gemini'] : array() ); $muse_models = ( isset( $choices['models']['muse'] ) ? $choices['models']['muse'] : array() ); $can_use_premium = Providers::can_use_premium(); $grok_free_models = array('grok-imagine-image'); // If non-premium user has a Pro provider selected, force to OpenAI if ( (!function_exists( 'afig_fs' ) || !afig_fs()->is__premium_only()) && Providers::is_premium( $provider ) ) { $provider = 'openai'; $model = 'gpt-image-2'; } // If Muse provider and no Muse model is set, default to Muse Image 1.0 if ( $provider === 'muse' && !isset( $muse_models[$model] ) ) { $model = 'muse-image-1.0'; } // If Gemini provider and no Gemini model is set, default to Nano Banana 2 if ( $provider === 'gemini' && !isset( $gemini_models[$model] ) ) { $model = 'gemini-nano-banana-2.1'; } // If Grok provider and no valid model is set, default if ( $provider === 'grok' && !isset( $grok_models[$model] ) ) { $model = 'grok-imagine-image'; } // Free users with a Pro-only Grok model saved get forced to standard if ( $provider === 'grok' && !$can_use_premium && !in_array( $model, $grok_free_models, true ) ) { $model = 'grok-imagine-image'; } // Free users with a GPT Image 2.6 model saved get forced to GPT Image 2 if ( $provider === 'openai' && !$can_use_premium && self::is_gpt25_model( $model ) ) { $model = 'gpt-image-2'; } // Determine which models to show and which are disabled if ( $provider === 'gemini' && function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $current_models = $gemini_models; $current_disabled = array(); } elseif ( $provider === 'muse' && function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $current_models = $muse_models; $current_disabled = array(); } elseif ( $provider === 'grok' ) { $current_models = $grok_models; $current_disabled = ( $can_use_premium ? array() : array_diff( array_keys( $grok_models ), $grok_free_models ) ); } else { $current_models = $openai_models; $current_disabled = ( $can_use_premium ? array() : self::OPENAI_GPT25_MODELS ); } ?> is__premium_only() ) { ?> get_settings(); $sanitized = ( is_array( $existing ) ? $existing : array() ); // Without an active licence the form shows the Free fallback of a stored Pro choice // (OpenAI, GPT Image 2, High), and get_settings() reads it that way too. Keep the stored // choice so it comes back once the licence is active again; the runtime already // downgrades it meanwhile. Read the stored values before anything below changes them. $stored_choice = ( Providers::can_use_premium() ? array() : $this->get_stored_model_choice() ); // AI Provider - Gemini only allowed for premium users // Only update if present in form input (not all tabs include the provider field) if ( isset( $input['ai_provider'] ) ) { $allowed_providers = Providers::available_slugs(); $sanitized['ai_provider'] = ( in_array( $input['ai_provider'], $allowed_providers, true ) ? $input['ai_provider'] : 'openai' ); } // If ai_provider is not in input, keep the existing value from $sanitized (copied from $existing) // API Keys. The form never shows a saved key, so an empty field keeps the saved one; // only the "Remove key" box (_remove_key) clears it. $remove_keys = ( isset( $input['_remove_key'] ) && is_array( $input['_remove_key'] ) ? $input['_remove_key'] : array() ); $this->apply_api_key( $sanitized, $input, $remove_keys, 'openai_api_key' ); if ( !empty( $remove_keys['openai_api_key'] ) ) { // A legacy copy would otherwise keep the removed key working. unset($sanitized['api_key']); } // Gemini API Key - Only save for premium users if ( function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $this->apply_api_key( $sanitized, $input, $remove_keys, 'gemini_api_key' ); } // Meta Muse API Key - Only save for premium users if ( function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $this->apply_api_key( $sanitized, $input, $remove_keys, 'muse_api_key' ); } // Grok API Key $this->apply_api_key( $sanitized, $input, $remove_keys, 'grok_api_key' ); // Legacy support: if old 'api_key' field exists, migrate it to openai_api_key if ( isset( $input['api_key'] ) && !isset( $input['openai_api_key'] ) && is_scalar( $input['api_key'] ) ) { $legacy_key = sanitize_text_field( $input['api_key'] ); if ( '' !== $legacy_key ) { $sanitized['openai_api_key'] = $legacy_key; } } if ( isset( $input['default_size'] ) ) { $all_sizes = array_keys( self::OPENAI_SIZES ); $requested = sanitize_text_field( $input['default_size'] ); $sanitized['default_size'] = ( in_array( $requested, $all_sizes, true ) ? $requested : '1024x1024' ); } if ( isset( $input['default_style'] ) ) { $style_val = sanitize_text_field( $input['default_style'] ); $valid_builtin = array_merge( array( 'none', 'realistic', 'artistic', 'cartoon', 'sketch', 'watercolor', '3d', 'pixel', 'cyberpunk', 'fantasy', 'anime', 'minimalist', 'technicolor' ), array_keys( self::get_trending_styles() ) ); // A custom style is Pro. While the licence is not active, the stored custom choice is // kept (the field posts it back unchanged) so it returns when the licence does. $is_custom_style = (bool) preg_match( '/^custom_\\d+$/', $style_val ) && (Providers::can_use_premium() || isset( $existing['default_style'] ) && $existing['default_style'] === $style_val); $sanitized['default_style'] = ( in_array( $style_val, $valid_builtin, true ) || $is_custom_style ? $style_val : 'realistic' ); } // Which field groups were actually part of this submission. Every checkbox // ships a hidden 0 companion and every repeater ships a _submitted marker, so // presence in $input is now an honest signal. This replaces the old // aifi_active_tab gating, which discarded edits made on any other tab. $submitted = ( isset( $input['_submitted'] ) && is_array( $input['_submitted'] ) ? $input['_submitted'] : array() ); if ( isset( $input['allow_text'] ) ) { $sanitized['allow_text'] = ( !empty( $input['allow_text'] ) ? 1 : 0 ); } // Alt Text settings if ( isset( $input['auto_alt_text'] ) ) { $sanitized['auto_alt_text'] = ( !empty( $input['auto_alt_text'] ) ? 1 : 0 ); } if ( isset( $input['alt_text_source'] ) ) { $allowed_sources = array('post_title', 'prompt', 'custom'); $sanitized['alt_text_source'] = ( in_array( $input['alt_text_source'], $allowed_sources, true ) ? $input['alt_text_source'] : 'post_title' ); } if ( isset( $input['alt_text_custom_format'] ) ) { $sanitized['alt_text_custom_format'] = sanitize_text_field( $input['alt_text_custom_format'] ); } if ( isset( $input['alt_text_overwrite'] ) ) { $sanitized['alt_text_overwrite'] = ( !empty( $input['alt_text_overwrite'] ) ? 1 : 0 ); } if ( isset( $input['output_format'] ) ) { $allowed_formats = array('webp', 'png', 'jpeg'); $sanitized['output_format'] = ( in_array( $input['output_format'], $allowed_formats ) ? $input['output_format'] : 'webp' ); } if ( isset( $input['image_quality'] ) ) { $quality = intval( $input['image_quality'] ); $sanitized['image_quality'] = max( 1, min( 100, $quality ) ); // Ensure quality is between 1 and 100 } // OpenAI generation quality (content tier: low / medium / high) if ( isset( $input['openai_quality'] ) ) { $allowed = array( 'high', 'medium', 'low', 'xhigh', 'max' ); $sanitized['openai_quality'] = ( in_array( $input['openai_quality'], $allowed, true ) ? $input['openai_quality'] : 'high' ); } // OpenAI background (gpt-image-2 only) if ( isset( $input['openai_background'] ) ) { $allowed = array('auto', 'transparent', 'opaque'); $sanitized['openai_background'] = ( in_array( $input['openai_background'], $allowed, true ) ? $input['openai_background'] : 'auto' ); } // Gemini-specific settings - Only save for premium users if ( function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { if ( isset( $input['gemini_aspect_ratio'] ) ) { $allowed_ratios = array( '1:1', '2:3', '3:2', '3:4', '4:3', '4:5', '5:4', '9:16', '16:9', '21:9' ); $sanitized['gemini_aspect_ratio'] = ( in_array( $input['gemini_aspect_ratio'], $allowed_ratios ) ? $input['gemini_aspect_ratio'] : '16:9' ); } if ( isset( $input['gemini_image_size'] ) ) { $allowed_sizes = array('1K', '2K', '4K'); $sanitized['gemini_image_size'] = ( in_array( $input['gemini_image_size'], $allowed_sizes ) ? $input['gemini_image_size'] : '1K' ); } // Gemini output format: WebP (converted) or PNG (native). Defaults to WebP. if ( isset( $input['gemini_output_format'] ) ) { $allowed_formats = array('webp', 'png'); $sanitized['gemini_output_format'] = ( in_array( $input['gemini_output_format'], $allowed_formats, true ) ? $input['gemini_output_format'] : 'webp' ); } } // Grok settings if ( isset( $input['grok_aspect_ratio'] ) ) { $allowed_ratios = array( '1:1', '16:9', '9:16', '4:3', '3:4', '3:2', '2:3', '2:1', '1:2' ); $sanitized['grok_aspect_ratio'] = ( in_array( $input['grok_aspect_ratio'], $allowed_ratios ) ? $input['grok_aspect_ratio'] : '1:1' ); } if ( isset( $input['grok_resolution'] ) ) { $allowed_resolutions = array('1k', '2k'); $sanitized['grok_resolution'] = ( in_array( $input['grok_resolution'], $allowed_resolutions ) ? $input['grok_resolution'] : '1k' ); } // Grok output format: WebP conversion is Pro-only; free users keep native JPEG. if ( isset( $input['grok_output_format'] ) ) { $grok_premium = Providers::can_use_premium(); $allowed_formats = ( $grok_premium ? array('webp', 'jpeg') : array('jpeg') ); $default_format = ( $grok_premium ? 'webp' : 'jpeg' ); $sanitized['grok_output_format'] = ( in_array( $input['grok_output_format'], $allowed_formats, true ) ? $input['grok_output_format'] : $default_format ); } // Inline Grok output format: shown to free users (Grok inline is free), but // WebP conversion is Pro-only, free users revert to native JPEG. if ( isset( $input['inline_grok_output_format'] ) ) { $grok_premium = Providers::can_use_premium(); $allowed_formats = ( $grok_premium ? array('webp', 'jpeg') : array('jpeg') ); $default_format = ( $grok_premium ? 'webp' : 'jpeg' ); $sanitized['inline_grok_output_format'] = ( in_array( $input['inline_grok_output_format'], $allowed_formats, true ) ? $input['inline_grok_output_format'] : $default_format ); } // Meta Muse settings (Pro only): nine ratios, three native formats. if ( function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $muse_ratios = array( '1:1', '16:9', '9:16', '4:3', '3:4', '3:2', '2:3', '2:1', '1:2' ); $muse_formats = array('webp', 'png', 'jpeg'); foreach ( array('muse_aspect_ratio', 'inline_muse_aspect_ratio') as $muse_key ) { if ( isset( $input[$muse_key] ) ) { $sanitized[$muse_key] = ( in_array( $input[$muse_key], $muse_ratios, true ) ? $input[$muse_key] : '1:1' ); } } foreach ( array('muse_output_format', 'inline_muse_output_format') as $muse_key ) { if ( isset( $input[$muse_key] ) ) { $sanitized[$muse_key] = ( in_array( $input[$muse_key], $muse_formats, true ) ? $input[$muse_key] : 'webp' ); } } } // AI Model - validate based on provider if ( isset( $input['ai_model'] ) ) { if ( is_string( $input['ai_model'] ) && isset( self::RETIRED_MODEL_REPLACEMENTS[$input['ai_model']] ) ) { $input['ai_model'] = self::RETIRED_MODEL_REPLACEMENTS[$input['ai_model']]; } $provider = $sanitized['ai_provider']; $openai_models = array('gpt-image-2'); if ( Providers::can_use_premium() ) { $openai_models = array_merge( $openai_models, self::OPENAI_GPT25_MODELS ); } $gemini_models = array( 'gemini-nano-banana-2.1', 'gemini-3.1-flash-image', 'gemini-3-pro-image', 'gemini-3.1-flash-lite-image' ); $grok_models_valid = ( Providers::can_use_premium() ? array('grok-imagine-image', 'grok-imagine-image-2.0') : array('grok-imagine-image') ); if ( $provider === 'openai' ) { $sanitized['ai_model'] = ( in_array( $input['ai_model'], $openai_models ) ? $input['ai_model'] : 'gpt-image-2' ); } elseif ( $provider === 'gemini' && function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $sanitized['ai_model'] = ( in_array( $input['ai_model'], $gemini_models ) ? $input['ai_model'] : 'gemini-nano-banana-2.1' ); } elseif ( $provider === 'grok' ) { $sanitized['ai_model'] = ( in_array( $input['ai_model'], $grok_models_valid ) ? $input['ai_model'] : 'grok-imagine-image' ); } elseif ( $provider === 'muse' && function_exists( 'afig_fs' ) && afig_fs()->is__premium_only() ) { $sanitized['ai_model'] = ( in_array( $input['ai_model'], array('muse-image-1.0'), true ) ? $input['ai_model'] : 'muse-image-1.0' ); } else { $sanitized['ai_model'] = 'gpt-image-2'; } } // Lapsed licence: put back a stored Pro choice that the form only showed as its Free // fallback. A field the user changed (settings.js sets its _touched marker) is saved. if ( $stored_choice ) { $touched = ( isset( $input['_touched'] ) && is_array( $input['_touched'] ) ? $input['_touched'] : array() ); $fallback = $this->model_choice_fallbacks( $stored_choice ); foreach ( array('ai_provider', 'ai_model', 'openai_quality') as $choice_key ) { if ( !isset( $stored_choice[$choice_key] ) || !empty( $touched[$choice_key] ) ) { continue; } // A model or quality only belongs to the provider it was stored with. if ( 'ai_provider' !== $choice_key && ($sanitized['ai_provider'] ?? '') !== ($stored_choice['ai_provider'] ?? '') ) { continue; } if ( $stored_choice[$choice_key] !== $fallback[$choice_key] && ($sanitized[$choice_key] ?? null) === $fallback[$choice_key] ) { $sanitized[$choice_key] = $stored_choice[$choice_key]; } } } // Extra High and Max exist only on GPT Image 2.6. Runs after ai_model is sanitized. // Only for OpenAI: with another provider the OpenAI quality is not in use, so it is kept. if ( isset( $sanitized['openai_quality'] ) && 'openai' === ($sanitized['ai_provider'] ?? 'openai') && in_array( $sanitized['openai_quality'], self::OPENAI_GPT25_QUALITIES, true ) && !self::is_gpt25_model( $sanitized['ai_model'] ?? 'gpt-image-2' ) ) { $sanitized['openai_quality'] = 'high'; } // Enabled Post Types - Pro only feature. // The _submitted marker is rendered only for Pro, so a free save never // reaches this and a Pro save with every box unticked is still honoured. if ( Providers::can_use_premium() && !empty( $submitted['enabled_post_types'] ) ) { if ( isset( $input['enabled_post_types'] ) && is_array( $input['enabled_post_types'] ) ) { // Get all valid public post types $valid_post_types = array_keys( get_post_types( array( 'public' => true, ), 'names' ) ); // Filter and sanitize the input $sanitized['enabled_post_types'] = array_values( array_filter( array_map( 'sanitize_key', $input['enabled_post_types'] ), function ( $pt ) use($valid_post_types) { // Products are handled on the Product Images tab, never here. return 'product' !== $pt && in_array( $pt, $valid_post_types, true ); } ) ); // Ensure at least one post type is selected if ( empty( $sanitized['enabled_post_types'] ) ) { $sanitized['enabled_post_types'] = array('post', 'page'); } } else { // If nothing is checked on General tab, default to post and page $sanitized['enabled_post_types'] = array('post', 'page'); } } // Group absent from the submission: keep the existing value copied from $existing. // Inline Image settings shown on Free too: style, size, the Grok // ratio and the OpenAI output format. Custom styles are the only Pro value here. if ( isset( $input['inline_default_style'] ) ) { $valid_styles = array_merge( array( 'none', 'realistic', 'artistic', 'cartoon', 'sketch', 'watercolor', '3d', 'pixel', 'cyberpunk', 'fantasy', 'anime', 'minimalist', 'technicolor' ), array_keys( self::get_trending_styles() ) ); $inline_style_val = sanitize_text_field( $input['inline_default_style'] ); $is_inline_custom = (bool) preg_match( '/^custom_\\d+$/', $inline_style_val ) && (Providers::can_use_premium() || isset( $existing['inline_default_style'] ) && $existing['inline_default_style'] === $inline_style_val); $sanitized['inline_default_style'] = ( in_array( $inline_style_val, $valid_styles, true ) || $is_inline_custom ? $inline_style_val : 'realistic' ); } if ( isset( $input['inline_default_size'] ) ) { $all_sizes = array_keys( self::OPENAI_SIZES ); $requested_inline = sanitize_text_field( $input['inline_default_size'] ); $sanitized['inline_default_size'] = ( in_array( $requested_inline, $all_sizes, true ) ? $requested_inline : '1024x1024' ); } if ( isset( $input['inline_grok_aspect_ratio'] ) ) { $valid_ratios = array( '1:1', '16:9', '9:16', '4:3', '3:4', '3:2', '2:3', '2:1', '1:2' ); $sanitized['inline_grok_aspect_ratio'] = ( in_array( $input['inline_grok_aspect_ratio'], $valid_ratios, true ) ? $input['inline_grok_aspect_ratio'] : '1:1' ); } if ( isset( $input['inline_output_format'] ) ) { $allowed_formats = array('webp', 'png', 'jpeg'); $sanitized['inline_output_format'] = ( in_array( $input['inline_output_format'], $allowed_formats, true ) ? $input['inline_output_format'] : 'webp' ); } // Inline Image settings (Pro only) if ( Providers::can_use_premium() ) { if ( isset( $input['inline_gemini_aspect_ratio'] ) ) { $valid_ratios = array( '1:1', '16:9', '9:16', '3:2', '2:3', '4:3', '3:4', '5:4', '4:5', '21:9' ); $sanitized['inline_gemini_aspect_ratio'] = ( in_array( $input['inline_gemini_aspect_ratio'], $valid_ratios, true ) ? $input['inline_gemini_aspect_ratio'] : '16:9' ); } // Inline Gemini output format: WebP (converted) or PNG (native). Defaults to WebP. if ( isset( $input['inline_gemini_output_format'] ) ) { $allowed_formats = array('webp', 'png'); $sanitized['inline_gemini_output_format'] = ( in_array( $input['inline_gemini_output_format'], $allowed_formats, true ) ? $input['inline_gemini_output_format'] : 'webp' ); } // Custom Styles. The marker lets an emptied repeater clear the list. if ( !empty( $submitted['custom_styles'] ) ) { $sanitized['custom_styles'] = array(); // Styles are referenced by position (custom_0, custom_1, ...). Each row carries // the position it had when the page loaded ('new' for an added row), so a // default that points at a style can follow it when rows above are deleted. $style_index_map = array(); if ( isset( $input['custom_styles'] ) && is_array( $input['custom_styles'] ) ) { foreach ( $input['custom_styles'] as $row_key => $cs ) { if ( !is_array( $cs ) ) { continue; } $name = sanitize_text_field( $cs['name'] ?? '' ); $prompt = sanitize_text_field( $cs['prompt'] ?? '' ); if ( $name !== '' && $prompt !== '' ) { $orig = ( isset( $cs['orig'] ) ? (string) $cs['orig'] : (string) $row_key ); if ( preg_match( '/^\\d+$/', $orig ) ) { $style_index_map[(int) $orig] = count( $sanitized['custom_styles'] ); } $sanitized['custom_styles'][] = array( 'name' => $name, 'prompt' => $prompt, ); } } } foreach ( array('default_style', 'inline_default_style') as $style_key ) { $sanitized[$style_key] = self::remap_custom_style( $sanitized[$style_key] ?? 'realistic', $style_index_map ); } } // Listen to this post. The marker is only rendered with the Audio fields. if ( !empty( $submitted['audio'] ) ) { $sanitized['audio_enabled'] = ( !empty( $input['audio_enabled'] ) ? 1 : 0 ); if ( isset( $input['audio_provider'] ) ) { $sanitized['audio_provider'] = ( in_array( $input['audio_provider'], Audio::PROVIDERS, true ) ? $input['audio_provider'] : Audio::DEFAULT_PROVIDER ); } $this->apply_api_key( $sanitized, $input, $remove_keys, 'elevenlabs_api_key' ); if ( isset( $input['elevenlabs_model'] ) ) { $sanitized['elevenlabs_model'] = ( isset( Audio::elevenlabs_models()[$input['elevenlabs_model']] ) ? $input['elevenlabs_model'] : Audio::DEFAULT_ELEVENLABS_MODEL ); } foreach ( array('elevenlabs_voice', 'grok_voice') as $voice_key ) { if ( isset( $input[$voice_key] ) ) { $sanitized[$voice_key] = substr( preg_replace( '/[^A-Za-z0-9_\\-]/', '', (string) $input[$voice_key] ), 0, 64 ); } } foreach ( array('elevenlabs_voice_name', 'grok_voice_name') as $name_key ) { if ( isset( $input[$name_key] ) ) { $sanitized[$name_key] = mb_substr( sanitize_text_field( $input[$name_key] ), 0, 100 ); } } if ( isset( $input['gemini_tts_model'] ) ) { $sanitized['gemini_tts_model'] = ( isset( Audio::gemini_models()[$input['gemini_tts_model']] ) ? $input['gemini_tts_model'] : Audio::DEFAULT_GEMINI_MODEL ); } if ( isset( $input['gemini_voice'] ) ) { $sanitized['gemini_voice'] = ( isset( Audio::gemini_voices()[$input['gemini_voice']] ) ? $input['gemini_voice'] : Audio::DEFAULT_GEMINI_VOICE ); } if ( isset( $input['audio_label'] ) ) { $sanitized['audio_label'] = mb_substr( sanitize_text_field( $input['audio_label'] ), 0, 80 ); } if ( isset( $input['audio_post_types'] ) && is_array( $input['audio_post_types'] ) ) { $sanitized['audio_post_types'] = array_values( array_intersect( $this->get_content_post_types(), array_map( 'sanitize_key', $input['audio_post_types'] ) ) ); } } // Podcast feed, on the Audio tab. if ( !empty( $submitted['podcast'] ) ) { $sanitized['podcast_enabled'] = ( !empty( $input['podcast_enabled'] ) ? 1 : 0 ); foreach ( array( 'podcast_title' => 255, 'podcast_author' => 255, ) as $text_key => $max ) { if ( isset( $input[$text_key] ) ) { $sanitized[$text_key] = mb_substr( sanitize_text_field( $input[$text_key] ), 0, $max ); } } if ( isset( $input['podcast_description'] ) ) { $sanitized['podcast_description'] = mb_substr( sanitize_textarea_field( $input['podcast_description'] ), 0, 4000 ); } if ( isset( $input['podcast_email'] ) ) { $sanitized['podcast_email'] = sanitize_email( $input['podcast_email'] ); } if ( isset( $input['podcast_image'] ) ) { $image_id = absint( $input['podcast_image'] ); $sanitized['podcast_image'] = ( $image_id && wp_attachment_is_image( $image_id ) ? $image_id : 0 ); if ( $sanitized['podcast_image'] ) { $meta = wp_get_attachment_metadata( $sanitized['podcast_image'] ); $w = ( isset( $meta['width'] ) ? (int) $meta['width'] : 0 ); $h = ( isset( $meta['height'] ) ? (int) $meta['height'] : 0 ); if ( $w && $h && ($w !== $h || $w < 1400 || $w > 3000) ) { add_settings_error( $this->option, 'aifi_podcast_image_size', __( 'The podcast cover was saved, but podcast apps ask for a square image between 1400 and 3000 pixels wide. Some apps may not list the show until it is replaced.', 'ai-featured-image-generator' ), 'warning' ); } } } if ( isset( $input['podcast_category'] ) ) { $sanitized['podcast_category'] = ( in_array( $input['podcast_category'], Podcast::CATEGORIES, true ) ? $input['podcast_category'] : Podcast::DEFAULT_CATEGORY ); } if ( isset( $input['podcast_explicit'] ) ) { $sanitized['podcast_explicit'] = ( 'yes' === $input['podcast_explicit'] ? 'yes' : 'no' ); } if ( isset( $input['podcast_limit'] ) ) { $sanitized['podcast_limit'] = max( 1, min( 200, absint( $input['podcast_limit'] ) ) ); } } // Auto-generate on publish, on the Featured Images tab. if ( !empty( $submitted['auto_generate'] ) ) { $sanitized['auto_generate_on_publish'] = ( !empty( $input['auto_generate_on_publish'] ) ? 1 : 0 ); $sanitized['auto_generate_prompt_from_content'] = ( !empty( $input['auto_generate_prompt_from_content'] ) ? 1 : 0 ); } // Product Images tab. The marker comes from the Background field. if ( !empty( $submitted['products'] ) ) { $sanitized = Product_Images::sanitize( $input, $sanitized ); } // GPT Image 2.6: planning detail and animated image defaults. if ( isset( $input['openai_complexity'] ) ) { $sanitized['openai_complexity'] = ( in_array( $input['openai_complexity'], array( 'auto', 'low', 'medium', 'high' ), true ) ? $input['openai_complexity'] : 'auto' ); } if ( isset( $input['openai_gif_frames'] ) || isset( $input['openai_gif_frame_ms'] ) ) { $anim = Generator::normalize_animation( ( isset( $input['openai_gif_frames'] ) ? (int) $input['openai_gif_frames'] : (int) ($sanitized['openai_gif_frames'] ?? 9) ), ( isset( $input['openai_gif_frame_ms'] ) ? (int) $input['openai_gif_frame_ms'] : (int) ($sanitized['openai_gif_frame_ms'] ?? 200) ) ); $sanitized['openai_gif_frames'] = $anim['frames']; $sanitized['openai_gif_frame_ms'] = $anim['frame_ms']; } // Brand Kit. The marker is only rendered when the section itself is, // so a save that never showed these fields leaves the stored kit alone. if ( !empty( $submitted['brand_kit'] ) ) { $sanitized['brand_kit_enabled'] = ( !empty( $input['brand_kit_enabled'] ) ? 1 : 0 ); $sanitized['brand_kit_colors'] = self::normalize_brand_colors( ( isset( $input['brand_kit_colors'] ) && is_array( $input['brand_kit_colors'] ) ? $input['brand_kit_colors'] : array() ) ); if ( isset( $input['brand_kit_description'] ) ) { $sanitized['brand_kit_description'] = sanitize_textarea_field( $input['brand_kit_description'] ); } if ( isset( $input['brand_kit_tone'] ) ) { $sanitized['brand_kit_tone'] = sanitize_text_field( $input['brand_kit_tone'] ); } if ( isset( $input['brand_kit_avoid'] ) ) { $sanitized['brand_kit_avoid'] = sanitize_textarea_field( $input['brand_kit_avoid'] ); } foreach ( array('brand_kit_reference_id', 'brand_kit_logo_id') as $attachment_key ) { if ( isset( $input[$attachment_key] ) ) { $attachment_id = absint( $input[$attachment_key] ); $sanitized[$attachment_key] = ( wp_attachment_is_image( $attachment_id ) ? $attachment_id : 0 ); } } if ( isset( $input['brand_kit_logo_position'] ) ) { $allowed_positions = array( 'top-left', 'top-right', 'bottom-left', 'bottom-right', 'center' ); $sanitized['brand_kit_logo_position'] = ( in_array( $input['brand_kit_logo_position'], $allowed_positions, true ) ? $input['brand_kit_logo_position'] : 'bottom-right' ); } if ( isset( $input['brand_kit_logo_size'] ) ) { $sanitized['brand_kit_logo_size'] = max( 5, min( 40, absint( $input['brand_kit_logo_size'] ) ) ); } if ( isset( $input['brand_kit_logo_opacity'] ) ) { $sanitized['brand_kit_logo_opacity'] = max( 10, min( 100, intval( $input['brand_kit_logo_opacity'] ) ) ); } if ( isset( $input['brand_kit_logo_margin'] ) ) { $sanitized['brand_kit_logo_margin'] = max( 0, min( 100, intval( $input['brand_kit_logo_margin'] ) ) ); } // Offsets may be negative (left / up), so intval rather than absint. foreach ( array('brand_kit_logo_offset_x', 'brand_kit_logo_offset_y') as $offset_key ) { if ( isset( $input[$offset_key] ) ) { $sanitized[$offset_key] = max( -100, min( 100, intval( $input[$offset_key] ) ) ); } } $this->note_brand_kit_change( $existing, $sanitized ); } } // The submission markers are transport only; never store them. unset($sanitized['_submitted'], $sanitized['_remove_key'], $sanitized['_touched']); return $sanitized; } /** * Save one API key field from the form. An empty field keeps the saved key, because * the form never shows it; a ticked "Remove key" box clears it. * * @param array $sanitized Settings being built (changed in place). * @param mixed $input Form input. * @param array $remove_keys The _remove_key marker from the form. * @param string $field Key field, such as openai_api_key. */ private function apply_api_key( array &$sanitized, $input, array $remove_keys, $field ) { if ( !empty( $remove_keys[$field] ) ) { $sanitized[$field] = ''; return; } if ( !isset( $input[$field] ) || !is_scalar( $input[$field] ) ) { return; } $value = sanitize_text_field( (string) $input[$field] ); if ( '' !== $value ) { $sanitized[$field] = $value; } } /** * Move a custom style reference (custom_N) to the style's new position after a save * of the Custom Styles list. A reference to a deleted style falls back to Realistic. * * @param string $style Saved style value. * @param array $map Position the style had when the page loaded => new position. * @return string */ public static function remap_custom_style( $style, array $map ) { if ( !is_string( $style ) || !preg_match( '/^custom_(\\d+)$/', $style, $m ) ) { return $style; } return ( isset( $map[(int) $m[1]] ) ? 'custom_' . $map[(int) $m[1]] : 'realistic' ); } /** * Provider, model and OpenAI quality exactly as stored, before the read-time licence * fallbacks of get_settings(). Retired and removed models read as their replacements. * * @return array Only the keys that are stored. */ private function get_stored_model_choice() { $stored = ( is_multisite() ? get_network_option( null, $this->option ) : get_option( $this->option ) ); if ( !is_array( $stored ) ) { return array(); } $choice = array(); if ( isset( $stored['ai_provider'] ) && Providers::is_registered( $stored['ai_provider'] ) ) { $choice['ai_provider'] = $stored['ai_provider']; } if ( isset( $choice['ai_provider'] ) && !empty( $stored['ai_model'] ) && is_string( $stored['ai_model'] ) ) { $model = $stored['ai_model']; if ( in_array( $model, self::OPENAI_DEPRECATED_MODELS, true ) ) { $model = 'gpt-image-2'; } if ( isset( self::RETIRED_MODEL_REPLACEMENTS[$model] ) ) { $model = self::RETIRED_MODEL_REPLACEMENTS[$model]; } $choice['ai_model'] = $model; } if ( isset( $stored['openai_quality'] ) && in_array( $stored['openai_quality'], array( 'high', 'medium', 'low', 'xhigh', 'max' ), true ) ) { $choice['openai_quality'] = $stored['openai_quality']; } return $choice; } /** * The values the settings form shows for a stored choice while the licence is * inactive: OpenAI for a Pro provider, the provider's default for a Pro model, and * High for the GPT Image 2.6 qualities. * * @param array $stored From get_stored_model_choice(). * @return array ai_provider, ai_model, openai_quality. */ private function model_choice_fallbacks( array $stored ) { $provider = ( isset( $stored['ai_provider'] ) ? $stored['ai_provider'] : 'openai' ); $fb_provider = ( Providers::is_available( $provider ) ? $provider : 'openai' ); $model = ( isset( $stored['ai_model'] ) ? $stored['ai_model'] : '' ); $fb_model = ( $fb_provider === $provider && Providers::is_model_available( $provider, $model ) ? $model : Providers::default_model( $fb_provider ) ); $quality = ( isset( $stored['openai_quality'] ) ? $stored['openai_quality'] : 'high' ); return array( 'ai_provider' => $fb_provider, 'ai_model' => $fb_model, 'openai_quality' => ( in_array( $quality, self::OPENAI_GPT25_QUALITIES, true ) ? 'high' : $quality ), ); } /** * Option holding the time the Brand Kit last changed, so "images made before * the current kit" can be found. */ const BRAND_KIT_UPDATED_OPTION = 'aifi_brand_kit_updated'; /** * When the Brand Kit last changed (Unix time), 0 if never. Stored next to the * settings, so on multisite it is network-wide like the kit itself. * * @return int */ public static function get_brand_kit_updated() { return (int) (( is_multisite() ? get_network_option( null, self::BRAND_KIT_UPDATED_OPTION, 0 ) : get_option( self::BRAND_KIT_UPDATED_OPTION, 0 ) )); } private static function touch_brand_kit_updated() { if ( is_multisite() ) { update_network_option( null, self::BRAND_KIT_UPDATED_OPTION, time() ); } else { update_option( self::BRAND_KIT_UPDATED_OPTION, time() ); } } /** * Brand colours in their one stored form: valid hex, lowercase, no duplicates, at most 3. * Used by the settings form and the assistant tools alike. * * @param mixed $value Array of colours, or a comma or space separated string. * @return string[] */ public static function normalize_brand_colors( $value ) { $list = ( is_array( $value ) ? $value : wp_parse_list( (string) $value ) ); $clean = array(); foreach ( $list as $color ) { if ( !is_scalar( $color ) ) { continue; } // sanitize_hex_color() ships with the customizer on older installs, so this // matches core's pattern directly rather than depending on that file being loaded. $color = strtolower( trim( (string) $color ) ); if ( preg_match( '|^#([a-f0-9]{3}){1,2}$|', $color ) ) { $clean[] = $color; } } return array_values( array_slice( array_unique( $clean ), 0, 3 ) ); } /** * Brand Kit keys that may be changed outside the settings form, with their rules. * * @return array key => array( type, min, max | allowed ) */ public static function brand_kit_field_rules() { return array( 'brand_kit_enabled' => array( 'type' => 'bool', ), 'brand_kit_colors' => array( 'type' => 'colors', ), 'brand_kit_description' => array( 'type' => 'textarea', ), 'brand_kit_tone' => array( 'type' => 'text', ), 'brand_kit_avoid' => array( 'type' => 'textarea', ), 'brand_kit_reference_id' => array( 'type' => 'image', ), 'brand_kit_logo_id' => array( 'type' => 'image', ), 'brand_kit_logo_position' => array( 'type' => 'enum', 'allowed' => array( 'top-left', 'top-right', 'bottom-left', 'bottom-right', 'center' ), 'default' => 'bottom-right', ), 'brand_kit_logo_size' => array( 'type' => 'int', 'min' => 5, 'max' => 40, ), 'brand_kit_logo_opacity' => array( 'type' => 'int', 'min' => 10, 'max' => 100, ), 'brand_kit_logo_margin' => array( 'type' => 'int', 'min' => 0, 'max' => 100, ), 'brand_kit_logo_offset_x' => array( 'type' => 'int', 'min' => -100, 'max' => 100, ), 'brand_kit_logo_offset_y' => array( 'type' => 'int', 'min' => -100, 'max' => 100, ), ); } /** * Change Brand Kit fields from code (the AI assistant tools use this), with the * same rules as the settings form. Keys not in $fields are left alone. * * @param array $fields brand_kit_* keys and raw values. * @return array|\WP_Error array( 'changed' => key => array( 'from', 'to' ), 'kit' => get_brand_kit() ) */ public function update_brand_kit( array $fields ) { if ( !Providers::can_use_premium() ) { return new \WP_Error('premium_required', __( 'The Brand Kit is a Pro feature.', 'ai-featured-image-generator' ), array( 'status' => 403, )); } $rules = self::brand_kit_field_rules(); $existing = $this->get_settings(); $settings = ( is_array( $existing ) ? $existing : array() ); $changed = array(); foreach ( $fields as $key => $value ) { if ( !isset( $rules[$key] ) ) { continue; } $rule = $rules[$key]; switch ( $rule['type'] ) { case 'bool': // "false", "0" and "" mean off; a plain truthiness check would read "false" as on. $clean = ( rest_sanitize_boolean( $value ) ? 1 : 0 ); break; case 'colors': $clean = self::normalize_brand_colors( $value ); $given = array_filter( array_map( 'trim', array_map( 'strval', array_filter( ( is_array( $value ) ? $value : wp_parse_list( (string) $value ) ), 'is_scalar' ) ) ) ); if ( $given && !$clean ) { // Never wipe the saved colours because of input we could not read. return new \WP_Error('invalid_colors', __( 'None of those are hex colours like #1a2b3c, so the colours were not changed.', 'ai-featured-image-generator' ), array( 'status' => 400, )); } break; case 'textarea': $clean = sanitize_textarea_field( (string) $value ); break; case 'text': $clean = sanitize_text_field( (string) $value ); break; case 'image': $id = absint( $value ); if ( $id && !wp_attachment_is_image( $id ) ) { return new \WP_Error('invalid_attachment', sprintf( /* translators: %d: attachment ID */ __( 'Attachment %d is not an image in the Media Library.', 'ai-featured-image-generator' ), $id ), array( 'status' => 400, )); } $clean = $id; break; case 'enum': $clean = ( in_array( $value, $rule['allowed'], true ) ? $value : $rule['default'] ); break; default: $clean = max( $rule['min'], min( $rule['max'], intval( $value ) ) ); } $before = ( isset( $settings[$key] ) ? $settings[$key] : null ); // Colours saved before they were normalised may differ only in case or order of duplicates. $same = ( 'colors' === $rule['type'] ? self::normalize_brand_colors( (array) $before ) === $clean : $before === $clean ); if ( !$same ) { $changed[substr( $key, strlen( 'brand_kit_' ) )] = array( 'from' => $before, 'to' => $clean, ); $settings[$key] = $clean; } } if ( $changed ) { $this->save_settings( $settings ); self::touch_brand_kit_updated(); } return array( 'changed' => $changed, 'kit' => $this->get_brand_kit(), ); } /** * Record when the Brand Kit last changed through the settings form. * * @param array $before Settings before the save. * @param array $after Settings being saved. */ private function note_brand_kit_change( $before, $after ) { foreach ( self::brand_kit_field_rules() as $key => $rule ) { $old = ( isset( $before[$key] ) ? $before[$key] : null ); $new = ( isset( $after[$key] ) ? $after[$key] : null ); if ( 'colors' === $rule['type'] ) { $old = self::normalize_brand_colors( (array) $old ); $new = self::normalize_brand_colors( (array) $new ); } if ( $old != $new ) { // phpcs:ignore Universal.Operators.StrictComparisons -- form values arrive as strings. self::touch_brand_kit_updated(); return; } } } /** * Write the settings option the same way the settings form does. * * @param array $settings Full settings array. */ private function save_settings( $settings ) { // On admin requests the form sanitiser is hooked to this option. It only // accepts fields from a submitted form (it looks for the form's section // markers), so it would quietly drop these already-clean values. Unhook it // for this one write. $sanitizer = array($this, 'sanitize_settings'); $hooked = has_filter( 'sanitize_option_' . $this->option, $sanitizer ); if ( false !== $hooked ) { remove_filter( 'sanitize_option_' . $this->option, $sanitizer, $hooked ); } self::$writing = true; try { if ( is_multisite() ) { update_network_option( null, $this->option, $settings ); } else { update_option( $this->option, $settings ); } } finally { self::$writing = false; } if ( false !== $hooked ) { add_filter( 'sanitize_option_' . $this->option, $sanitizer, $hooked ); } } /** * Whether a stored settings key belonged to Stability AI, removed in 2.2.2. * * @param string $key Settings key. * @return bool */ private static function is_stability_key( $key ) { return is_string( $key ) && (0 === strpos( $key, 'stability_' ) || 0 === strpos( $key, 'inline_stability_' )); } /** * The provider a site moves to when its saved provider no longer exists: OpenAI, * or Grok when Grok has a key and OpenAI has none. * * @param array $settings Stored settings. * @return string Provider slug. */ private static function removed_provider_fallback( array $settings ) { $has_openai = !empty( $settings['openai_api_key'] ) || !empty( $settings['api_key'] ); return ( !$has_openai && !empty( $settings['grok_api_key'] ) ? 'grok' : 'openai' ); } /** * Delete what Stability AI left in the stored settings: its key, its options and, * when it was the active provider, the provider choice. Stability was removed in 2.2.2. * * @return string 'provider' when Stability was the active provider, 'key' when only * a key was stored, '' when there was nothing to tell the owner about. */ public function remove_stability_data() { $stored = ( is_multisite() ? get_network_option( null, $this->option ) : get_option( $this->option ) ); if ( !is_array( $stored ) ) { return ''; } $found = ''; $changed = false; foreach ( array_keys( $stored ) as $key ) { if ( !self::is_stability_key( $key ) ) { continue; } if ( 'stability_api_key' === $key && '' !== trim( (string) $stored[$key] ) ) { $found = 'key'; } unset($stored[$key]); $changed = true; } if ( isset( $stored['ai_provider'] ) && 'stability' === $stored['ai_provider'] ) { $found = 'provider'; $stored['ai_provider'] = self::removed_provider_fallback( $stored ); $stored['ai_model'] = Providers::default_model( $stored['ai_provider'] ); $changed = true; } if ( $changed ) { $this->save_settings( $stored ); } return $found; } /** * Get plugin settings. * * @return array */ public function get_settings() { if ( is_multisite() ) { $settings = get_network_option( null, $this->option ); } else { $settings = get_option( $this->option ); } if ( isset( $settings['auto_generate'] ) ) { unset($settings['auto_generate']); } // Legacy support: migrate old 'api_key' to 'openai_api_key' if ( isset( $settings['api_key'] ) && !isset( $settings['openai_api_key'] ) ) { $settings['openai_api_key'] = $settings['api_key']; unset($settings['api_key']); } // Stability AI was removed in 2.2.2: its key and options are never read again. if ( is_array( $settings ) ) { foreach ( array_keys( $settings ) as $stored_key ) { if ( self::is_stability_key( $stored_key ) ) { unset($settings[$stored_key]); } } } // Ensure ai_provider is a known provider this site may use. A missing, empty or unknown // value falls back to OpenAI, and so does a Pro-only provider (Gemini, Muse) on the Free // build, which has no code for them. A Pro build with an expired license keeps the choice, // so the owner gets a clear "Pro feature" message instead of a silent switch to OpenAI. if ( !is_array( $settings ) ) { $settings = array(); } $provider = ( isset( $settings['ai_provider'] ) ? $settings['ai_provider'] : '' ); if ( !Providers::is_registered( $provider ) ) { $settings['ai_provider'] = self::removed_provider_fallback( $settings ); if ( !isset( Providers::image_models( $settings['ai_provider'] )[$settings['ai_model'] ?? ''] ) ) { $settings['ai_model'] = Providers::default_model( $settings['ai_provider'] ); } } elseif ( Providers::is_premium( $provider ) && (!function_exists( 'afig_fs' ) || !afig_fs()->is__premium_only()) ) { $settings['ai_provider'] = 'openai'; $settings['ai_model'] = Providers::default_model( 'openai' ); } // An empty model (an imported or hand-edited option) uses the provider's default. if ( empty( $settings['ai_model'] ) ) { $settings['ai_model'] = Providers::default_model( $settings['ai_provider'] ); } // Removed (deprecated) OpenAI models move to GPT Image 2, OpenAI's recommended replacement. if ( in_array( $settings['ai_model'] ?? '', self::OPENAI_DEPRECATED_MODELS, true ) ) { $settings['ai_model'] = 'gpt-image-2'; } // Retired Gemini and Grok models move to their replacements. if ( isset( self::RETIRED_MODEL_REPLACEMENTS[$settings['ai_model'] ?? ''] ) ) { $settings['ai_model'] = self::RETIRED_MODEL_REPLACEMENTS[$settings['ai_model']]; } // GPT Image 2.6 is Pro only. Users without Pro (e.g. expired license) fall back to GPT Image 2. if ( $settings['ai_provider'] === 'openai' && self::is_gpt25_model( $settings['ai_model'] ?? '' ) && !Providers::can_use_premium() ) { $settings['ai_model'] = 'gpt-image-2'; } // Grok defaults if ( !isset( $settings['grok_aspect_ratio'] ) ) { $settings['grok_aspect_ratio'] = '1:1'; } if ( !isset( $settings['grok_resolution'] ) ) { $settings['grok_resolution'] = '1k'; } // Output format defaults for providers that need server-side WebP conversion. // WebP (the lightweight default) is a Pro feature; free users keep native output. $can_use_premium_fmt = Providers::can_use_premium(); if ( !isset( $settings['gemini_output_format'] ) ) { $settings['gemini_output_format'] = 'webp'; } if ( !isset( $settings['grok_output_format'] ) ) { $settings['grok_output_format'] = ( $can_use_premium_fmt ? 'webp' : 'jpeg' ); } // Meta Muse defaults if ( !isset( $settings['muse_aspect_ratio'] ) ) { $settings['muse_aspect_ratio'] = '1:1'; } if ( !isset( $settings['muse_output_format'] ) ) { $settings['muse_output_format'] = 'webp'; } // OpenAI gpt-image-2 defaults if ( !isset( $settings['openai_quality'] ) ) { $settings['openai_quality'] = 'high'; } if ( !isset( $settings['openai_background'] ) ) { $settings['openai_background'] = 'auto'; } // Gemini defaults if ( !isset( $settings['gemini_aspect_ratio'] ) ) { $settings['gemini_aspect_ratio'] = '16:9'; } if ( !isset( $settings['gemini_image_size'] ) ) { $settings['gemini_image_size'] = '1K'; } // Inline image defaults if ( !isset( $settings['inline_default_style'] ) ) { $settings['inline_default_style'] = ( isset( $settings['default_style'] ) ? $settings['default_style'] : 'realistic' ); } if ( !isset( $settings['inline_default_size'] ) ) { $settings['inline_default_size'] = ( isset( $settings['default_size'] ) ? $settings['default_size'] : '1024x1024' ); } if ( !isset( $settings['inline_gemini_aspect_ratio'] ) ) { $settings['inline_gemini_aspect_ratio'] = ( isset( $settings['gemini_aspect_ratio'] ) ? $settings['gemini_aspect_ratio'] : '16:9' ); } if ( !isset( $settings['inline_grok_aspect_ratio'] ) ) { $settings['inline_grok_aspect_ratio'] = ( isset( $settings['grok_aspect_ratio'] ) ? $settings['grok_aspect_ratio'] : '1:1' ); } if ( !isset( $settings['inline_output_format'] ) ) { $settings['inline_output_format'] = ( isset( $settings['output_format'] ) ? $settings['output_format'] : 'webp' ); } if ( !isset( $settings['inline_gemini_output_format'] ) ) { $settings['inline_gemini_output_format'] = ( isset( $settings['gemini_output_format'] ) ? $settings['gemini_output_format'] : 'webp' ); } if ( !isset( $settings['inline_grok_output_format'] ) ) { $settings['inline_grok_output_format'] = ( isset( $settings['grok_output_format'] ) ? $settings['grok_output_format'] : 'webp' ); } if ( !isset( $settings['inline_muse_aspect_ratio'] ) ) { $settings['inline_muse_aspect_ratio'] = ( isset( $settings['muse_aspect_ratio'] ) ? $settings['muse_aspect_ratio'] : '1:1' ); } if ( !isset( $settings['inline_muse_output_format'] ) ) { $settings['inline_muse_output_format'] = ( isset( $settings['muse_output_format'] ) ? $settings['muse_output_format'] : 'webp' ); } // Alt text defaults if ( !isset( $settings['auto_alt_text'] ) ) { $settings['auto_alt_text'] = 1; } if ( !isset( $settings['alt_text_source'] ) ) { $settings['alt_text_source'] = 'post_title'; } if ( !isset( $settings['alt_text_custom_format'] ) ) { $settings['alt_text_custom_format'] = '{title}'; } if ( !isset( $settings['alt_text_overwrite'] ) ) { $settings['alt_text_overwrite'] = 0; } // Brand Kit defaults if ( !isset( $settings['brand_kit_enabled'] ) ) { $settings['brand_kit_enabled'] = 0; } if ( !isset( $settings['brand_kit_colors'] ) ) { $settings['brand_kit_colors'] = array(); } if ( !isset( $settings['brand_kit_description'] ) ) { $settings['brand_kit_description'] = ''; } if ( !isset( $settings['brand_kit_tone'] ) ) { $settings['brand_kit_tone'] = ''; } if ( !isset( $settings['brand_kit_avoid'] ) ) { $settings['brand_kit_avoid'] = ''; } if ( !isset( $settings['brand_kit_reference_id'] ) ) { $settings['brand_kit_reference_id'] = 0; } if ( !isset( $settings['brand_kit_logo_id'] ) ) { $settings['brand_kit_logo_id'] = 0; } if ( !isset( $settings['brand_kit_logo_position'] ) ) { $settings['brand_kit_logo_position'] = 'bottom-right'; } if ( !isset( $settings['brand_kit_logo_size'] ) ) { $settings['brand_kit_logo_size'] = 15; } if ( !isset( $settings['brand_kit_logo_opacity'] ) ) { $settings['brand_kit_logo_opacity'] = 100; } if ( !isset( $settings['brand_kit_logo_margin'] ) ) { $settings['brand_kit_logo_margin'] = 20; } if ( !isset( $settings['brand_kit_logo_offset_x'] ) ) { $settings['brand_kit_logo_offset_x'] = 0; } if ( !isset( $settings['brand_kit_logo_offset_y'] ) ) { $settings['brand_kit_logo_offset_y'] = 0; } return ( is_array( $settings ) ? $settings : array() ); } /** * Check if current user can use premium features. * * @return bool */ public static function can_use_premium() { return Providers::can_use_premium(); } /** * Get enabled post types for the AI image generator. * * Free version: Only 'post' and 'page' * Pro version: Custom selection from settings * * @return array List of enabled post type names. */ public function get_enabled_post_types() { // Free version: only post and page if ( !Providers::can_use_premium() ) { return array('post', 'page'); } // Pro version: get from settings $settings = $this->get_settings(); if ( isset( $settings['enabled_post_types'] ) && is_array( $settings['enabled_post_types'] ) && !empty( $settings['enabled_post_types'] ) ) { // WooCommerce products have their own box (Product Images), never the featured image tools. $types = array_values( array_diff( $settings['enabled_post_types'], array('product') ) ); if ( $types ) { return $types; } } // Default to post and page if not set return array('post', 'page'); } /** * Post types whose content the plugin works with beyond featured images: the enabled * types plus WooCommerce products. Used by Image Audit, Listen to this post and the * assistant tools. Products never get the featured image box, column, bulk or auto-generate. * * @return string[] */ public function get_content_post_types() { $types = $this->get_enabled_post_types(); if ( post_type_exists( 'product' ) && !in_array( 'product', $types, true ) ) { $types[] = 'product'; } return $types; } }