0; // Force database upgrade and clear caches if editing if ($is_edit) { // Clear any cached data wp_cache_delete('a1ai_chatbot_' . $chatbot_id, 'a1ai'); wp_cache_delete('a1ai_all_chatbots', 'a1ai'); // Force database upgrade check $current_db_version = get_option('a1ai_db_version', '0.0.0'); if (version_compare($current_db_version, A1AI_VERSION, '<')) { // Trigger upgrade process $a1ai = new A1AI(); $a1ai->check_database_upgrade(); } } // Verify nonce if in edit mode if ($is_edit && (!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_key($_GET['_wpnonce']), 'a1ai_chatbot_action'))) { wp_die(esc_html__('Security check failed.', 'a1ai-chatbot')); } $chatbot = $is_edit ? $data_manager->get_chatbot($chatbot_id) : null; // If editing but chatbot doesn't exist, show error if ($is_edit && !$chatbot) { wp_die(esc_html__('Chatbot not found.', 'a1ai-chatbot')); } // Set default values $chatbot_name = $is_edit ? (isset($chatbot['name']) ? $chatbot['name'] : '') : ''; $settings = $is_edit ? (isset($chatbot['settings']) && is_array($chatbot['settings']) ? $chatbot['settings'] : array()) : array(); // Ensure settings is always an array and handle potential unserialize issues if (!is_array($settings)) { $settings = array(); } // Load system prompt and custom instructions from dedicated tables if editing if ($is_edit && $chatbot_id > 0) { // Get system prompt from custom instructions table $system_instructions = $data_manager->get_custom_instructions($chatbot_id, 'system'); if (!empty($system_instructions) && is_array($system_instructions)) { foreach ($system_instructions as $instruction) { if ($instruction['instruction_name'] === 'System Prompt' && !empty($instruction['instruction_content'])) { $settings['system_prompt'] = $instruction['instruction_content']; break; } } } // Get general custom instructions from dedicated table $custom_instructions = $data_manager->get_custom_instructions($chatbot_id, 'general'); if (!empty($custom_instructions) && is_array($custom_instructions)) { // Combine all custom instructions into one text $instructions_text = ''; foreach ($custom_instructions as $instruction) { if (!empty($instruction['instruction_content'])) { $instructions_text .= $instruction['instruction_content'] . "\n\n"; } } if (!empty($instructions_text)) { $settings['custom_instructions'] = trim($instructions_text); } } } // Ensure all settings have default values $default_settings = array( 'custom_instructions' => '', 'system_prompt' => '', 'welcome_message' => __('Hello! How can I help you today?', 'a1ai-chatbot'), 'llm_provider' => '', // empty = inherit a1ai_llm_provider 'model' => get_option('a1ai_default_model', 'gpt-4o'), 'temperature' => get_option('a1ai_default_temperature', 0.5), 'max_tokens' => get_option('a1ai_default_max_tokens', 1000), 'chat_bubble_text' => __('Chat with us!', 'a1ai-chatbot'), 'chatbot_title' => __('Chat Support', 'a1ai-chatbot'), 'placeholder' => __('Type your message here...', 'a1ai-chatbot'), 'notification_email' => '', 'gdpr_enabled' => false, 'gdpr_consent_text' => __('By clicking "I Agree", you consent to the use of AI-powered chat services and acknowledge that your conversations may be processed to provide you with assistance. You can withdraw your consent at any time by closing this chat.', 'a1ai-chatbot'), 'primary_color' => '#2271b1', 'font_size' => 'medium', 'chat_width' => '350px', 'chat_height' => '500px', // Conversation Settings 'primary_action' => '', 'secondary_actions' => '', 'conversation_stages' => '', 'action_guidance_tone' => 'moderate', 'repetition_threshold' => '6', 'message_history_limit' => 20, 'variation_strategies' => '', 'conversation_tracking' => false, 'action_tracking' => false, // Voice input (per-chatbot; only used when upgrade plugin is active) 'voice_enabled' => true, 'voice_language' => '', ); // Merge existing settings with defaults to ensure all keys exist $settings = array_merge($default_settings, $settings); // Helper function to safely get array values function a1ai_get_setting($settings, $key, $default = '') { if (!is_array($settings)) { return $default; } return isset($settings[$key]) ? $settings[$key] : $default; } $title = $is_edit ? sprintf( /* translators: %s: Name of the chatbot */ __('Edit Chatbot: %s', 'a1ai-chatbot'), $chatbot_name ) : __('Add New Chatbot', 'a1ai-chatbot'); ?>
' . esc_html__('Settings', 'a1ai-chatbot') . '' ); ?>