'GPT-4o (Default)', 'gpt-4-turbo' => 'GPT-4 Turbo (Default)', ]; /** * Default OpenAI model (used if nothing is selected or fetch fails badly). * * @var string */ public const DEFAULT_MODEL = 'gpt-4o'; // Fallback if not set, prefer newer /** * Constructor. * Hooks into WordPress admin actions. */ public function __construct() { add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); // Action to clear transient when settings are updated. add_action( 'update_option_' . $this->option_name, array( $this, 'clear_models_cache' ), 10, 0 ); // AJAX handler for refreshing models. add_action( 'wp_ajax_ai_seo_refresh_models', array( $this, 'handle_ajax_refresh_models' ) ); // Enqueue scripts for settings page. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_settings_scripts' ) ); // Enqueue styles for settings page. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_settings_styles' ) ); } /** * Clears the cached list of models. */ public function clear_models_cache(): void { delete_transient( self::MODELS_TRANSIENT_KEY ); } /** * Adds the settings page to the WordPress admin menu. */ public function add_settings_page(): void { // --- SVG Icon Logic --- // $icon_svg_path = AI_SEO_TOOLS_PATH . 'assets/images/menu-icon.svg'; $menu_icon_uri = 'dashicons-search'; // Default fallback icon if ( file_exists( $icon_svg_path ) ) { $svg_content = file_get_contents( $icon_svg_path ); if ( false !== $svg_content ) { // Clean SVG (optional but recommended: remove comments, unnecessary attributes) // Basic cleaning: remove newlines and excessive whitespace for data URI $svg_content = preg_replace('/\s+/S', " ", $svg_content); $svg_content = trim($svg_content); // Encode as Base64 $base64_svg = base64_encode( $svg_content ); $menu_icon_uri = 'data:image/svg+xml;base64,' . $base64_svg; } } // --- End SVG Icon Logic --- // add_menu_page( esc_html__( 'AI SEO Tools Settings', 'ai-seo-tools' ), // Page Title (for browser tab and H1) esc_html__( 'AI SEO Tools', 'ai-seo-tools' ), // Menu Title (the text in the sidebar) 'manage_options', // Capability required to see the menu $this->settings_page_slug, // Menu Slug (unique identifier, used in URL) array( $this, 'render_settings_page' ), // Function that renders the page content $menu_icon_uri, // Icon: Base64 encoded SVG or fallback Dashicon 76 // Position (lower number = higher up, 76 is below Tools) ); } /** * Registers the settings, section, and fields. */ public function register_settings(): void { // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic -- All fields are fully sanitized in sanitize_options() register_setting( $this->option_group, $this->option_name, array( $this, 'sanitize_options' ) // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic -- All fields are fully sanitized in sanitize_options() ); add_settings_section( 'ai_seo_tools_openai_section', esc_html__( 'OpenAI API Settings', 'ai-seo-tools' ), array( $this, 'render_openai_section' ), $this->settings_page_slug ); add_settings_field( 'openai_api_key', esc_html__( 'OpenAI API Key', 'ai-seo-tools' ), array( $this, 'render_api_key_field' ), $this->settings_page_slug, 'ai_seo_tools_openai_section' ); add_settings_field( 'openai_model', esc_html__( 'OpenAI Model (Vision)', 'ai-seo-tools' ), array( $this, 'render_model_field' ), $this->settings_page_slug, 'ai_seo_tools_openai_section' ); // Add language selection field for alt text (customizable) add_settings_field( 'alt_text_language_custom_enable', esc_html__( 'Custom Alt Text Language', 'ai-seo-tools' ), array( $this, 'render_alt_text_language_custom_field' ), $this->settings_page_slug, 'ai_seo_tools_openai_section' ); add_settings_field( 'alt_text_custom_prompt', esc_html__( 'Custom Alt Text Prompt', 'ai-seo-tools' ), array( $this, 'render_alt_text_custom_prompt_field' ), $this->settings_page_slug, 'ai_seo_tools_openai_section' ); // --- Add Modules Section --- // add_settings_section( 'ai_seo_tools_modules_section', esc_html__( 'Modules', 'ai-seo-tools' ), array( $this, 'render_modules_section' ), $this->settings_page_slug ); add_settings_field( 'enable_alt_text_module', esc_html__( 'Alt Text Generator for Images', 'ai-seo-tools' ), array( $this, 'render_enable_alt_text_field' ), $this->settings_page_slug, 'ai_seo_tools_modules_section' ); add_settings_field( 'enable_content_refresh_module', esc_html__( 'Content Refresh & SEO Optimizer', 'ai-seo-tools' ), array( $this, 'render_enable_content_refresh_field' ), $this->settings_page_slug, 'ai_seo_tools_modules_section' ); // Add Auto Tagging module toggle add_settings_field( 'enable_auto_tagging_module', esc_html__( 'Auto Tagging for Posts', 'ai-seo-tools' ), array( $this, 'render_enable_auto_tagging_field' ), $this->settings_page_slug, 'ai_seo_tools_modules_section' ); add_settings_field( 'content_refresh_max_tokens', esc_html__( 'Max Tokens for Content Refresh', 'ai-seo-tools' ), array( $this, 'render_content_refresh_max_tokens_field' ), $this->settings_page_slug, 'ai_seo_tools_openai_section' ); add_settings_field( 'content_refresh_rewrite_strength', esc_html__( 'Rewrite Strength', 'ai-seo-tools' ), array( $this, 'render_content_refresh_rewrite_strength_field' ), $this->settings_page_slug, 'ai_seo_tools_openai_section' ); // --- Add Bulk Processing Settings Section --- // add_settings_section( 'ai_seo_tools_bulk_section', esc_html__( 'Alt Text Bulk Processing Settings', 'ai-seo-tools' ), array( $this, 'render_bulk_section' ), $this->settings_page_slug ); add_settings_field( 'bulk_processing_delay', esc_html__( 'Delay Between Images', 'ai-seo-tools' ), array( $this, 'render_bulk_delay_field' ), $this->settings_page_slug, 'ai_seo_tools_bulk_section' ); add_settings_field( 'image_detail_level', esc_html__( 'Image Detail Level', 'ai-seo-tools' ), array( $this, 'render_detail_level_field' ), $this->settings_page_slug, 'ai_seo_tools_bulk_section' ); // --- Auto Tagging Settings Section and Fields --- // add_settings_section( 'ai_seo_tools_auto_tagging_section', esc_html__( 'Auto Tagging Settings', 'ai-seo-tools' ), array( $this, 'render_auto_tagging_settings_section' ), $this->settings_page_slug ); add_settings_field( 'auto_tagging_max_tags', esc_html__( 'Max Tags per Post', 'ai-seo-tools' ), array( $this, 'render_auto_tagging_max_tags_field' ), $this->settings_page_slug, 'ai_seo_tools_auto_tagging_section' ); add_settings_field( 'auto_tagging_confidence_threshold', esc_html__( 'Confidence Threshold', 'ai-seo-tools' ), array( $this, 'render_auto_tagging_confidence_threshold_field' ), $this->settings_page_slug, 'ai_seo_tools_auto_tagging_section' ); add_settings_field( 'auto_tagging_stop_words', esc_html__( 'Stop-Words List', 'ai-seo-tools' ), array( $this, 'render_auto_tagging_stop_words_field' ), $this->settings_page_slug, 'ai_seo_tools_auto_tagging_section' ); // --- End Bulk Processing Settings Section --- // } /** * Sanitizes the option values before saving. * * @param array $input The input options. * @return array The sanitized options. */ public function sanitize_options( array $input ): array { // Nonce verification for settings form $nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : ''; if ( ! $nonce || ! wp_verify_nonce( $nonce, $this->option_group . '-options' ) ) { wp_die( esc_html__( 'Security check failed. Please try again.', 'ai-seo-tools' ) ); } $sanitized_input = array(); $options = get_option( $this->option_name ); // Get existing options // Sanitize API Key. $new_api_key = isset( $input['openai_api_key'] ) ? sanitize_text_field( $input['openai_api_key'] ) : null; $old_api_key = $options['openai_api_key'] ?? null; if ( $new_api_key !== null ) { $sanitized_input['openai_api_key'] = $new_api_key; // If key changes, clear the model cache. if ($new_api_key !== $old_api_key) { $this->clear_models_cache(); } } elseif ( $old_api_key !== null) { $sanitized_input['openai_api_key'] = $old_api_key; // Keep existing if not set in input } // Fetch available models based on the *potentially new* API key for validation. $current_api_key = $sanitized_input['openai_api_key'] ?? null; $available_models = $this->get_available_models( $current_api_key ); // Pass key to fetcher // Sanitize Model Selection. if ( isset( $input['openai_model'] ) && array_key_exists( $input['openai_model'], $available_models ) ) { $sanitized_input['openai_model'] = $input['openai_model']; } else { // Fallback logic: Keep old if valid, otherwise use default. $old_model = $options['openai_model'] ?? self::DEFAULT_MODEL; if ( array_key_exists( $old_model, $available_models ) ) { $sanitized_input['openai_model'] = $old_model; } else { $sanitized_input['openai_model'] = self::DEFAULT_MODEL; // If default isn't in the available list either, pick the first available one? if (!empty($available_models) && !array_key_exists(self::DEFAULT_MODEL, $available_models)) { // Use array_key_first for PHP 7.3+ if (function_exists('array_key_first')) { $sanitized_input['openai_model'] = array_key_first($available_models); } else { // Fallback for older PHP: reset pointer and get key reset($available_models); $sanitized_input['openai_model'] = key($available_models); } } } } // Sanitize Module Enable/Disable Checkboxes $sanitized_input['enable_alt_text_module'] = ( isset( $input['enable_alt_text_module'] ) && $input['enable_alt_text_module'] === '1' ) ? '1' : '0'; $sanitized_input['enable_content_refresh_module'] = ( isset( $input['enable_content_refresh_module'] ) && $input['enable_content_refresh_module'] === '1' ) ? '1' : '0'; // Sanitize Auto Tagging module toggle $sanitized_input['enable_auto_tagging_module'] = ( isset( $input['enable_auto_tagging_module'] ) && $input['enable_auto_tagging_module'] === '1' ) ? '1' : '0'; // Sanitize Bulk Processing Delay $sanitized_input['bulk_processing_delay'] = isset( $input['bulk_processing_delay'] ) ? absint( $input['bulk_processing_delay'] ) : 20; // Default 20 seconds // Ensure minimum delay (e.g., 1 second) to prevent issues if ($sanitized_input['bulk_processing_delay'] < 1) { $sanitized_input['bulk_processing_delay'] = 1; } // Sanitize Detail Level $valid_details = ['low', 'high']; // Add 'auto'? OpenAI default is auto, but low/high give more control. $sanitized_input['image_detail_level'] = isset( $input['image_detail_level'] ) && in_array( $input['image_detail_level'], $valid_details, true ) ? sanitize_key($input['image_detail_level']) : 'low'; // Default 'low' // Sanitize custom alt text language $sanitized_input['alt_text_language_custom_enable'] = !empty($input['alt_text_language_custom_enable']) ? '1' : ''; $sanitized_input['alt_text_language_custom'] = isset($input['alt_text_language_custom']) ? sanitize_text_field($input['alt_text_language_custom']) : ''; // Sanitize custom alt text prompt enable $sanitized_input['alt_text_custom_prompt_enable'] = !empty($input['alt_text_custom_prompt_enable']) ? '1' : ''; // Sanitize custom alt text prompt $sanitized_input['alt_text_custom_prompt'] = isset($input['alt_text_custom_prompt']) ? trim(wp_kses_post($input['alt_text_custom_prompt'])) : ''; // Sanitize content refresh max tokens $max_tokens = isset($input['content_refresh_max_tokens']) ? intval($input['content_refresh_max_tokens']) : 10000; $max_tokens = max(500, $max_tokens); $sanitized_input['content_refresh_max_tokens'] = $max_tokens; // Sanitize content refresh rewrite strength $allowed_strengths = ['minimal', 'medium', 'maximal']; $strength = isset($input['content_refresh_rewrite_strength']) && in_array($input['content_refresh_rewrite_strength'], $allowed_strengths, true) ? $input['content_refresh_rewrite_strength'] : 'maximal'; $sanitized_input['content_refresh_rewrite_strength'] = $strength; // Sanitize Auto Tagging Settings $max_tags = isset( $input['auto_tagging_max_tags'] ) ? absint( $input['auto_tagging_max_tags'] ) : 5; $max_tags = max( 1, min( 20, $max_tags ) ); $sanitized_input['auto_tagging_max_tags'] = $max_tags; $threshold = isset( $input['auto_tagging_confidence_threshold'] ) ? floatval( $input['auto_tagging_confidence_threshold'] ) : 0.75; $threshold = max( 0, min( 1, $threshold ) ); $sanitized_input['auto_tagging_confidence_threshold'] = $threshold; $sanitized_input['auto_tagging_stop_words'] = isset( $input['auto_tagging_stop_words'] ) ? sanitize_text_field( $input['auto_tagging_stop_words'] ) : ''; return $sanitized_input; } /** * Fetches the list of models from OpenAI API. * * @param string|null $api_key The API key to use. * @return array|WP_Error Array of model_id => label on success, WP_Error on failure. */ private function fetch_openai_models(?string $api_key) { if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', esc_html__( 'API key is required to fetch models.', 'ai-seo-tools' ) ); } $api_endpoint = 'https://api.openai.com/v1/models'; $args = [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, ], 'timeout' => 20, // Shorter timeout for model list ]; $response = wp_remote_get( $api_endpoint, $args ); if ( is_wp_error( $response ) ) { return $response; } $response_code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( $response_code !== 200 || ! isset( $data['data'] ) || ! is_array( $data['data'] ) ) { $error_message = isset($data['error']['message']) ? $data['error']['message'] : esc_html__('Invalid response from API.', 'ai-seo-tools'); return new WP_Error( 'api_error', $error_message, [ 'status' => $response_code ] ); } $models = []; foreach ( $data['data'] as $model ) { if ( isset( $model['id'] ) ) { $label = $model['id']; if (isset($model['owned_by'])) { // $label .= ' (' . $model['owned_by'] . ')'; // Optional: Add owner info } $models[ $model['id'] ] = $label; } } // Sort models alphabetically by key (ID) ksort( $models ); if (empty($models)) { return new WP_Error( 'no_gpt_models', esc_html__( 'No models found via API.', 'ai-seo-tools' ) ); } return $models; } /** * Gets available models, using cache if possible. * * @param string|null $api_key Optional API key (used if not cached). * @return array Available models [id => label]. Returns default list on failure. */ private function get_available_models(?string $api_key = null): array { $cached_models = get_transient( self::MODELS_TRANSIENT_KEY ); if ( false !== $cached_models && is_array($cached_models) ) { return $cached_models; } // If not cached, try fetching. if ( $api_key === null ) { $options = get_option( $this->option_name ); $api_key = $options['openai_api_key'] ?? null; } $fetched_models = $this->fetch_openai_models( $api_key ); if ( ! is_wp_error( $fetched_models ) && ! empty( $fetched_models ) ) { // Cache indefinitely (until manually cleared or API key changes). set_transient( self::MODELS_TRANSIENT_KEY, $fetched_models, 0 ); return $fetched_models; } else { // Fetch failed or no API key, return the hardcoded default list as a fallback. return $this->default_vision_models; } } /** * Renders the description for the OpenAI section. */ public function render_openai_section(): void { echo '
' . esc_html__( 'Enter your OpenAI API key and select the model for AI features.', 'ai-seo-tools' ) . '
'; } /** * Renders the API key input field. */ public function render_api_key_field(): void { $options = get_option( $this->option_name ); $api_key = $options['openai_api_key'] ?? ''; ?>', '' ); ?>
default_vision_models ) {
$warning_message = esc_html__( 'Warning: Could not fetch models from API, showing default list. Please ensure API key is correct and saved.', 'ai-seo-tools' );
echo '
' . esc_html( $warning_message ) . '';
}
// End the warning PHP block ?>
' . esc_html__('Controls the maximum length of AI-generated content for one post. Higher values allow longer posts but may cost more and take longer. Default: 10000. No hard maximum, but very high values may be limited by the AI model.', 'ai-seo-tools') . '
'; } /** * Renders the content refresh rewrite strength select field. */ public function render_content_refresh_rewrite_strength_field(): void { $options = get_option( $this->option_name ); $strength = isset($options['content_refresh_rewrite_strength']) ? $options['content_refresh_rewrite_strength'] : 'maximal'; echo ' '; echo '' . esc_html__('Controls how aggressively the AI rewrites your post. Default: Maximal.', 'ai-seo-tools') . '
'; } /** * Checks if the Alt Text Generator module is enabled. * * @return bool True if enabled, false otherwise. */ private function is_alt_text_module_enabled(): bool { $options = get_option($this->option_name); return ($options['enable_alt_text_module'] ?? '1') === '1'; } /** * Checks if the Content Refresh module is enabled. * * @return bool True if enabled, false otherwise. */ private function is_content_refresh_module_enabled(): bool { $options = get_option($this->option_name); return ($options['enable_content_refresh_module'] ?? '1') === '1'; } /** * Renders the main admin page HTML with tabs. */ public function render_settings_page(): void { // Generate nonce for tab navigation $tab_nonce = wp_create_nonce( 'ai_seo_tab_navigation' ); // Determine the active tab with flexible nonce verification $active_tab = 'settings'; // default if ( isset( $_GET['tab'] ) ) { $requested_tab = sanitize_key( wp_unslash( $_GET['tab'] ) ); // Check different types of nonce depending on the context $nonce_provided = isset( $_GET['_wpnonce'] ); $nonce_valid = false; if ( $nonce_provided ) { $provided_nonce = sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ); // Check for tab navigation nonce $tab_nonce_valid = wp_verify_nonce( $provided_nonce, 'ai_seo_tab_navigation' ); // Check for content refresh nonce (for pagination/filtering) $content_refresh_nonce_valid = wp_verify_nonce( $provided_nonce, 'ai_seo_content_refresh_view' ); // Accept either nonce type $nonce_valid = $tab_nonce_valid || $content_refresh_nonce_valid; } // Accept tab if no nonce provided (first load) OR if any valid nonce if ( ! $nonce_provided || $nonce_valid ) { // Validate the requested tab is one of the allowed values $allowed_tabs = [ 'settings', 'alt_text_generator', 'content_refresh', 'auto_tagging' ]; if ( in_array( $requested_tab, $allowed_tabs, true ) ) { $active_tab = $requested_tab; } } } $show_alt_text_tab = $this->is_alt_text_module_enabled(); $show_content_refresh_tab = $this->is_content_refresh_module_enabled(); // Show auto-tagging tab if enabled $show_auto_tagging_tab = $this->is_auto_tagging_module_enabled(); // If the current tab is hidden, fallback to settings if ( ($active_tab === 'alt_text_generator' && !$show_alt_text_tab) || ($active_tab === 'content_refresh' && !$show_content_refresh_tab) || ($active_tab === 'auto_tagging' && !$show_auto_tagging_tab) ) { $active_tab = 'settings'; } ?>Many site owners upload images without providing descriptive alt text or optimizing filenames, which hurts both accessibility and SEO. Over half of website homepages have images with missing alternative text, leaving visually impaired users in the dark and missing an SEO opportunity (search engines use alt text to understand images). Writing alt text for every image is tedious, and many people simply forget or don\'t know how to write a good description. This is a pain point for bloggers, e-commerce (lots of product images), and anyone mindful of SEO and accessibility compliance.
'; echo ''; // Ensure the Alt Text module class is loaded if the module is active. if ( ! class_exists( 'AI_SEO_Tools_Alt_Text' ) ) { // Attempt to load it if it wasn't loaded automatically (e.g., module was just enabled) $module_file = AI_SEO_TOOLS_PATH . 'modules/alt-text/class-ai-seo-tools-alt-text.php'; if (file_exists($module_file)) { require_once $module_file; } else { echo '' . esc_html__('Alt Text module file not found. Cannot display statistics.', 'ai-seo-tools') . '
' . esc_html__('Alt Text module class not found after loading file. Cannot display statistics.', 'ai-seo-tools') . '
' . esc_html__( 'No images found in the Media Library.', 'ai-seo-tools' ) . '
'; } echo '' . sprintf( /* translators: %d: Number of images without alt text. */ esc_html__( 'Found %d images without alt text.', 'ai-seo-tools' ), esc_html( $stats['without_alt'] ) ) . '
'; // Limit Input echo '' . esc_html__( 'All images in your Media Library already have alt text. Well done!', 'ai-seo-tools' ) . '
'; } echo 'Websites struggle to keep content up-to-date and SEO-friendly over time. Old blog posts can become outdated, hurting search rankings, but manually auditing and updating dozens or hundreds of posts is time-consuming. This module uses generative AI to analyze existing posts and suggest updates or rewrites for outdated sections, recommend low-competition keywords, and auto-generate meta descriptions or summaries. It provides a semi-automated content refresh process to keep posts relevant and improve rankings.
'; echo '| ' . esc_html__('Title', 'ai-seo-tools') . ' | '; echo '' . esc_html__('Last Updated', 'ai-seo-tools') . ' | '; echo '' . esc_html__('Word Count', 'ai-seo-tools') . ' | '; echo '' . esc_html__('Outdated Score', 'ai-seo-tools') . ' | '; echo '' . esc_html__('Actions', 'ai-seo-tools') . ' | '; echo '
|---|---|---|---|---|
' . esc_html($original_content) . ' ' . esc_html($row['title']) . ' | ';
echo '' . esc_html($row['last_updated']) . ' | '; echo '' . esc_html($row['word_count']) . ' | '; // Badge for Outdated Score $badge_color = $row['score'] === 'High' ? '#e74c3c' : '#27ae60'; $badge_text_color = '#fff'; $badge_class = $row['score'] === 'High' ? 'ai-seo-badge--high' : 'ai-seo-badge--low'; echo '' . esc_html( $row['score'] ) . ' | '; echo ''; echo ' '; echo ''; echo ' | '; echo '
' . esc_html__('Posts are considered outdated if last updated more than 6 months ago or missing a meta description. You can analyze and update any post with AI suggestions.', 'ai-seo-tools') . '
'; } /** * Renders the description for the Bulk Processing section. */ public function render_bulk_section(): void { echo '' . esc_html__( 'Configure how the bulk generation process runs.', 'ai-seo-tools' ) . '
'; } /** * Renders the input field for bulk processing delay. */ public function render_bulk_delay_field(): void { $options = get_option( $this->option_name ); $delay = $options['bulk_processing_delay'] ?? 20; // Default 20 ?>option_name ); $detail_level = $options['image_detail_level'] ?? 'low'; // Default 'low' ?>
settings_page_slug !== $hook ) { return; } wp_enqueue_script( 'ai-seo-tools-settings', // Handle for the script AI_SEO_TOOLS_URL . 'assets/js/ai-seo-settings.js', // Path to the new JS file array( 'jquery' ), AI_SEO_TOOLS_VERSION, true // Load in footer ); // Localize script with necessary data. wp_localize_script( 'ai-seo-tools-settings', 'aiSeoSettings', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'ai_seo_content_refresh_nonce' ), 'bulk_start_nonce' => wp_create_nonce( 'ai_seo_bulk_alt_start_nonce' ), 'bulk_status_nonce' => wp_create_nonce( 'ai_seo_bulk_alt_status_nonce' ), 'bulk_stop_nonce' => wp_create_nonce( 'ai_seo_bulk_alt_stop_nonce' ), 'get_stats_nonce' => wp_create_nonce( 'ai_seo_get_stats_nonce' ), 'get_details_nonce' => wp_create_nonce( 'ai_seo_get_image_details_nonce' ), 'refreshing_text' => esc_html__( 'Refreshing...', 'ai-seo-tools' ), 'refreshed_text' => esc_html__( 'List updated.', 'ai-seo-tools' ), 'error_text' => esc_html__( 'Error updating list.', 'ai-seo-tools' ), 'bulk_start_error' => esc_html__( 'Error starting generation.', 'ai-seo-tools' ), 'bulk_status_error' => esc_html__( 'Error fetching status.', 'ai-seo-tools' ), 'bulk_stop_error' => esc_html__( 'Error stopping generation.', 'ai-seo-tools' ), 'bulk_stopped' => esc_html__( 'Bulk generation stopped by user.', 'ai-seo-tools' ), 'bulk_complete' => esc_html__( 'Bulk generation complete!', 'ai-seo-tools' ), 'bulk_processing' => esc_html__( 'Processed {processed} of {total} images.', 'ai-seo-tools' ), 'bulk_processing_with_errors' => esc_html__( 'Processed {processed} of {total}. Encountered {errors} errors (see logs).', 'ai-seo-tools' ), // Auto-Tagging nonces and messages 'bulk_tags_start_nonce' => wp_create_nonce( 'ai_seo_bulk_tags_start_nonce' ), 'bulk_tags_status_nonce' => wp_create_nonce( 'ai_seo_bulk_tags_status_nonce' ), 'bulk_tags_stop_nonce' => wp_create_nonce( 'ai_seo_bulk_tags_stop_nonce' ), 'bulk_tags_processing' => esc_html__( 'Processed {processed} of {total} posts.', 'ai-seo-tools' ), 'bulk_tags_complete_msg' => esc_html__( 'Bulk tagging complete!', 'ai-seo-tools' ), 'bulk_tags_stopped_msg' => esc_html__( 'Bulk tagging stopped by user.', 'ai-seo-tools' ), // Bulk Append nonces 'bulk_append_tags_start_nonce' => wp_create_nonce( 'ai_seo_bulk_append_tags_start_nonce' ), 'bulk_append_tags_status_nonce' => wp_create_nonce( 'ai_seo_bulk_append_tags_status_nonce' ), 'bulk_append_tags_stop_nonce' => wp_create_nonce( 'ai_seo_bulk_append_tags_stop_nonce' ), // Bulk Regenerate nonces 'bulk_regenerate_tags_start_nonce' => wp_create_nonce( 'ai_seo_bulk_regenerate_tags_start_nonce' ), 'bulk_regenerate_tags_status_nonce' => wp_create_nonce( 'ai_seo_bulk_regenerate_tags_status_nonce' ), 'bulk_regenerate_tags_stop_nonce' => wp_create_nonce( 'ai_seo_bulk_regenerate_tags_stop_nonce' ), // Nonce and confirmation for clearing all tags 'clear_all_tags_nonce' => wp_create_nonce( 'ai_seo_clear_all_tags_nonce' ), 'confirm_clear_all_tags' => esc_html__( 'Are you sure you want to clear all tags for all posts?', 'ai-seo-tools' ), ) ); } /** * Handles the AJAX request to refresh the OpenAI models list. */ public function handle_ajax_refresh_models(): void { check_ajax_referer( 'ai_seo_refresh_models_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Permission denied.', 'ai-seo-tools' ) ], 403 ); } // Get current API key $options = get_option( $this->option_name ); $api_key = $options['openai_api_key'] ?? null; if ( empty( $api_key ) ) { wp_send_json_error( [ 'message' => esc_html__( 'API Key is not set.', 'ai-seo-tools' ) ], 400 ); } // Clear the cache first $this->clear_models_cache(); // Fetch fresh models $fetched_models = $this->fetch_openai_models( $api_key ); if ( is_wp_error( $fetched_models ) ) { // Send back the WP_Error message wp_send_json_error( [ 'message' => $fetched_models->get_error_message() ], 500 ); } if ( empty( $fetched_models ) ) { wp_send_json_error( [ 'message' => esc_html__( 'No models returned by API.', 'ai-seo-tools' ) ], 500 ); } // Cache the newly fetched models set_transient( self::MODELS_TRANSIENT_KEY, $fetched_models, 0 ); // Send success response with the models list wp_send_json_success( [ 'models' => $fetched_models ] ); } /** * Checks if the Auto Tagging module is enabled. * * @return bool True if enabled, false otherwise. */ private function is_auto_tagging_module_enabled(): bool { $options = get_option( $this->option_name ); return ( $options['enable_auto_tagging_module'] ?? '1' ) === '1'; } /** * Renders the content for the Auto Tagging tab. */ private function render_auto_tagging_tab_content(): void { // Explanation block echo '
' . esc_html__( 'Automated tagging enriches metadata, improves internal linking, and reduces manual effort for editors. Generate semantically relevant tags based on post content.', 'ai-seo-tools' ) . '
'; echo '' . esc_html__( 'Auto Tagging module file not found.', 'ai-seo-tools' ) . '
' . sprintf( esc_html__( 'Found %d posts without tags.', 'ai-seo-tools' ), esc_html( $stats['without_tags'] ) ) . '
'; echo '' . esc_html__( 'All published posts already have tags.', 'ai-seo-tools' ) . '
'; } echo '' . sprintf( esc_html__( 'Found %d posts with existing tags.', 'ai-seo-tools' ), esc_html( $stats['with_tags'] ) ) . '
'; echo '' . esc_html__( 'No posts eligible for append (no posts with existing tags).', 'ai-seo-tools' ) . '
'; } echo '' . sprintf( esc_html__( 'Found %d posts with existing tags.', 'ai-seo-tools' ), esc_html( $stats['with_tags'] ) ) . '
'; echo '' . esc_html__( 'No posts found for regeneration.', 'ai-seo-tools' ) . '
'; } echo '' . esc_html__( 'Configure default settings for the Auto Tagging module.', 'ai-seo-tools' ) . '
'; } /** * Renders the Max Tags per Post field. */ public function render_auto_tagging_max_tags_field(): void { $options = get_option( $this->option_name ); $value = $options['auto_tagging_max_tags'] ?? 5; ?> option_name ); $value = $options['auto_tagging_confidence_threshold'] ?? 0.75; ?> option_name ); $value = $options['auto_tagging_stop_words'] ?? ''; ?> option_name ); $is_enabled = ( $options['enable_auto_tagging_module'] ?? '1' ) === '1'; ?>