settings = $settings; add_action('add_meta_boxes', array($this, 'add_meta_box')); add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); add_action('init', array($this, 'register_meta')); add_action('save_post', array($this, 'save_custom_prompt'), 10, 2); } /** * Register _aifi_custom_prompt post meta so it is accessible in the REST API * (needed for Block Editor persistence). It is shown only in the "edit" context, so * visitors reading the public REST API never see it. */ public function register_meta() { register_post_meta('', '_aifi_custom_prompt', array( 'show_in_rest' => array( 'schema' => array( 'type' => 'string', 'context' => array('edit'), ), ), 'single' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field', 'auth_callback' => function() { return current_user_can('edit_posts'); }, )); } /** * Save the custom prompt from the Classic Editor meta box form submission. * * @param int $post_id Post ID. * @param \WP_Post $post Post object. */ public function save_custom_prompt($post_id, $post) { // Skip autosaves and revisions if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (wp_is_post_revision($post_id)) { return; } // Verify nonce if (!isset($_POST['aifi_meta_box_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['aifi_meta_box_nonce'])), 'aifi_meta_box')) { return; } if (!current_user_can('edit_post', $post_id)) { return; } if (isset($_POST['aifi_custom_prompt'])) { // update_post_meta() unslashes its value, so slash it again to keep typed backslashes. update_post_meta($post_id, '_aifi_custom_prompt', wp_slash(sanitize_textarea_field(wp_unslash($_POST['aifi_custom_prompt'])))); } } /** * Add meta box to post editor. */ public function add_meta_box() { // Get enabled post types from settings $post_types = $this->settings->get_enabled_post_types(); // Add meta box for each enabled post type foreach ($post_types as $post_type) { // Verify the post type exists and supports featured images if (!post_type_exists($post_type)) { continue; } add_meta_box( 'aifi_meta_box', __('AI Featured Image', 'ai-featured-image-generator'), array($this, 'render_meta_box'), $post_type, 'side', 'high' ); } } /** * Enqueue admin scripts and styles. * * @param string $hook Current admin page. */ public function enqueue_scripts($hook) { // Check if we're on a post edit screen if (!in_array($hook, array('post.php', 'post-new.php'))) { return; } // Get the current post type from the screen $screen = get_current_screen(); if (!$screen || empty($screen->post_type)) { return; } // Get enabled post types and check if current post type is enabled $enabled_post_types = $this->settings->get_enabled_post_types(); // Products have the AI Product Images box instead, which shows the audio section // when products are ticked on the Audio tab; that section needs these scripts. $audio_product = 'product' === $screen->post_type && Providers::can_use_premium() && in_array('product', (new Audio($this->settings))->post_types(), true); if (!in_array($screen->post_type, $enabled_post_types, true) && !$audio_product) { return; } // Classic or block editor for this screen. The Classic Editor plugin can be active while // a user switches one post to the block editor, so ask the screen, not the plugin list. $is_classic_editor = ! ( method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ); wp_enqueue_style( 'aifi-admin', AIFI_PLUGIN_URL . 'assets/css/admin.css', array(), AIFI_VERSION ); wp_enqueue_script( 'aifi-admin', AIFI_PLUGIN_URL . 'assets/js/admin.js', array('jquery'), AIFI_VERSION, true ); $settings_data = $this->settings->get_settings(); $provider = isset($settings_data['ai_provider']) ? $settings_data['ai_provider'] : 'openai'; wp_localize_script('aifi-admin', 'aifiData', array( 'nonce' => wp_create_nonce('wp_rest'), 'restUrl' => rest_url('aifi/v1/generate'), 'removeBgUrl' => rest_url('aifi/v1/remove-background'), 'generatePromptUrl' => rest_url('aifi/v1/generate-prompt'), 'savePromptUrl' => rest_url('aifi/v1/save-prompt'), 'audioGenerateUrl' => rest_url('aifi/v1/audio/generate'), 'audioStatusUrl' => rest_url('aifi/v1/audio/status'), 'audioDeleteUrl' => rest_url('aifi/v1/audio/delete'), 'audioToggleUrl' => rest_url('aifi/v1/audio/toggle'), 'audioEnabled' => Providers::can_use_premium(), 'isClassicEditor' => $is_classic_editor, 'provider' => $provider, 'sunburstModel' => 'gpt-image-2.6-sunburst', 'i18n' => array( 'generating' => __('Generating image...', 'ai-featured-image-generator'), 'success' => __('Image generated successfully!', 'ai-featured-image-generator'), 'error' => __('Error generating image. Please try again.', 'ai-featured-image-generator'), 'noApiKey' => __('Please configure your API key in the settings.', 'ai-featured-image-generator'), 'confirmOverwrite' => __('This will replace your current featured image. Continue?', 'ai-featured-image-generator'), 'removingBg' => __('Removing background...', 'ai-featured-image-generator'), 'removeBgSuccess' => __('Background removed successfully!', 'ai-featured-image-generator'), 'removeBgError' => __('Error removing background. Please try again.', 'ai-featured-image-generator'), 'generatingPrompt' => __('Generating prompt from content...', 'ai-featured-image-generator'), 'promptGenerated' => __('Prompt generated and saved!', 'ai-featured-image-generator'), 'promptError' => __('Error generating prompt. Please try again.', 'ai-featured-image-generator'), 'emptyContent' => __('This post has no content to generate a prompt from.', 'ai-featured-image-generator'), 'audioNone' => __('No audio yet.', 'ai-featured-image-generator'), 'audioStale' => __('The post changed since this audio was made. Regenerate it to match.', 'ai-featured-image-generator'), /* translators: 1: length such as "4 min 12 s", 2: provider and voice such as "ElevenLabs (Rachel)" */ 'audioReady' => __('Audio ready: %1$s, %2$s.', 'ai-featured-image-generator'), 'audioGenerate' => __('Generate audio', 'ai-featured-image-generator'), 'audioRegenerate' => __('Regenerate audio', 'ai-featured-image-generator'), 'audioMaking' => __('Making audio...', 'ai-featured-image-generator'), /* translators: 1: part number, 2: number of parts */ 'audioPart' => __('Making audio, part %1$s of %2$s...', 'ai-featured-image-generator'), 'audioDone' => __('Audio is ready.', 'ai-featured-image-generator'), 'audioError' => __('Could not make the audio. Please try again.', 'ai-featured-image-generator'), 'audioRemoved' => __('Audio removed.', 'ai-featured-image-generator'), 'audioConfirmReplace' => __('This replaces the current recording. Continue?', 'ai-featured-image-generator'), 'audioConfirmRemove' => __('Remove this recording? The audio file is deleted from the Media Library.', 'ai-featured-image-generator'), 'audioUnsaved' => __('The audio is made from the last saved version of the post, and you have unsaved changes. Continue anyway?', 'ai-featured-image-generator'), ), )); } /** * Render meta box content. * * @param \WP_Post $post Post object. */ public function render_meta_box($post) { if (!current_user_can('edit_post', $post->ID)) { return; } $settings_data = $this->settings->get_settings(); $default_style = isset($settings_data['default_style']) ? $settings_data['default_style'] : 'realistic'; $allow_text = isset($settings_data['allow_text']) ? (bool)$settings_data['allow_text'] : false; // Check if appropriate API key is configured based on provider $provider = isset($settings_data['ai_provider']) ? $settings_data['ai_provider'] : 'openai'; // Check if user can use premium features $can_use_premium = Providers::can_use_premium(); $upgrade_url = function_exists('afig_fs') ? afig_fs()->get_upgrade_url() : '#'; // Brand_Kit::is_active() is the single source of truth for "there is a kit worth applying"; // it is Pro-gated internally and false for free users. $brand_kit = new Brand_Kit($this->settings); $brand_kit_ready = $brand_kit->is_active(); $brand_kit_url = admin_url('admin.php?page=ai-featured-image-generator&tab=brand'); // If non-premium user has a Pro provider selected, show upgrade notice and force OpenAI if ( ! Providers::is_available( $provider ) ) { // Only administrators can buy and activate a licence, so only they get the link. printf( '

%s %s

', /* translators: %s: provider name */ sprintf( esc_html__('%s is a Pro feature.', 'ai-featured-image-generator'), esc_html( Providers::label( $provider ) ) ), current_user_can( Settings::capability() ) ? '' . esc_html__('Upgrade to Pro', 'ai-featured-image-generator') . '' : '' ); // Force provider to OpenAI for the rest of this render $provider = 'openai'; } $provider_label = Providers::label($provider); if ( ! Providers::has_api_key($settings_data, $provider)) { printf( '

%s %s

', sprintf( /* translators: %s: provider name */ esc_html__('API key not configured for %s.', 'ai-featured-image-generator'), esc_html($provider_label) ), esc_url(admin_url('admin.php?page=ai-featured-image-generator')), esc_html__('Configure now', 'ai-featured-image-generator') ); // Audio has its own provider and key, so it still works without an image key. echo '
'; $this->render_audio_section($post); echo '
'; return; } wp_nonce_field('aifi_meta_box', 'aifi_meta_box_nonce'); ?>

' . esc_html__('Upgrade to Pro', 'ai-featured-image-generator') . '' ); ?>

' . esc_html__('Edit Brand Kit', 'ai-featured-image-generator') . '' ); ?>

' . esc_html__('Create one', 'ai-featured-image-generator') . '' ); ?>

settings->get_settings() )['line'] ); ?>

ID ); if ( $auto_state ) : ?>

post_status && ! has_post_thumbnail( $post ) && Providers::can_use_premium() && ! empty( $settings_data['auto_generate_on_publish'] ) ) : ?>

ID); if ($has_tools_key) : ?>

get_upgrade_url()) . '">' . esc_html__('Upgrade to Pro', 'ai-featured-image-generator') . '' ); ?>

render_audio_section($post); ?>

get_upgrade_url()) . '">' . esc_html__('Upgrade to Pro', 'ai-featured-image-generator') . '' ); ?>

settings); $cfg = $audio->config(); if ( ! $audio->post_type_allowed($post) ) : ?>

status($post); $estimate = $audio->estimate($post); $has_key = $audio->has_key($cfg['provider']); ?>