admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('businessbot_integration_nonce'), 'test_connection_label' => __('Test Connection', 'ai-chat-assistant-for-business'), 'testing_label' => __('Testing...', 'ai-chat-assistant-for-business'), 'save_success' => __('API key saved', 'ai-chat-assistant-for-business'), 'copy_success' => __('API key copied', 'ai-chat-assistant-for-business'), ]); } function businessbot_chat_assist_test_connection_ajax() { check_ajax_referer('businessbot_integration_nonce', '_ajax_nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(__('You are not allowed to do this action.', 'ai-chat-assistant-for-business')); } $api_key = isset($_POST['api_key']) ? sanitize_text_field(wp_unslash($_POST['api_key'])) : ''; $api_key = trim($api_key); if ('' === $api_key) { update_option('businessbot_api_last_test', 'error'); update_option('businessbot_api_last_test_message', __('API key missing. Please enter your key and test again.', 'ai-chat-assistant-for-business')); update_option('businessbot_api_last_test_model', ''); update_option('businessbot_api_last_test_http_code', 0); update_option('businessbot_api_last_test_error', __('API key missing.', 'ai-chat-assistant-for-business')); update_option('businessbot_api_last_test_time', time()); wp_send_json_error(__('API key missing. Please enter your key and test again.', 'ai-chat-assistant-for-business')); } $payload = [ 'contents' => [ [ 'role' => 'user', 'parts' => [ ['text' => 'Reply with exactly: OK'], ], ], ], ]; $model_chain = function_exists('businessbot_chat_assist_build_runtime_model_chain') ? businessbot_chat_assist_build_runtime_model_chain($api_key) : ['gemini-2.5-flash', 'gemini-2.0-flash']; $max_attempts = max(1, count($model_chain)); $attempts = 0; foreach ($model_chain as $model) { $attempts++; if ($attempts > $max_attempts) { break; } $result = function_exists('businessbot_chat_assist_call_gemini_model') ? businessbot_chat_assist_call_gemini_model($api_key, $model, $payload) : ['success' => false, 'retry_next' => true]; if (function_exists('businessbot_chat_assist_debug_log')) { businessbot_chat_assist_debug_log('test_connection_attempt', [ 'attempt' => $attempts, 'max_attempts' => $max_attempts, 'model' => $model, 'success' => !empty($result['success']) ? 'yes' : 'no', 'retry_next' => !empty($result['retry_next']) ? 'yes' : 'no', 'status_code' => (string) ($result['status_code'] ?? ''), 'error_message' => (string) ($result['error_message'] ?? ''), ]); } if (!empty($result['success'])) { update_option('businessbot_api_last_test', 'success'); update_option('businessbot_api_last_test_message', __('Connection successful. AI is working.', 'ai-chat-assistant-for-business')); update_option('businessbot_api_last_test_model', sanitize_text_field($result['model'] ?? $model)); update_option('businessbot_api_last_test_http_code', (int) ($result['status_code'] ?? 200)); update_option('businessbot_api_last_test_error', ''); update_option('businessbot_api_last_test_time', time()); wp_send_json_success(__('Connection successful. AI is working.', 'ai-chat-assistant-for-business')); } update_option('businessbot_api_last_test_model', sanitize_text_field($result['model'] ?? $model)); update_option('businessbot_api_last_test_http_code', (int) ($result['status_code'] ?? 0)); update_option('businessbot_api_last_test_error', sanitize_text_field($result['error_message'] ?? __('Unknown error.', 'ai-chat-assistant-for-business'))); update_option('businessbot_api_last_test_time', time()); if (isset($result['retry_next']) && !$result['retry_next']) { break; } } update_option('businessbot_api_last_test', 'error'); update_option('businessbot_api_last_test_message', __('Invalid API key or network issue.', 'ai-chat-assistant-for-business')); wp_send_json_error(__('Invalid API key or network issue.', 'ai-chat-assistant-for-business')); } function businessbot_chat_assist_integration_page() { $api_key = get_option('businessbot_api_key'); $test_state = get_option('businessbot_api_last_test', ''); $last_test_model = get_option('businessbot_api_last_test_model', ''); $last_test_http_code = (int) get_option('businessbot_api_last_test_http_code', 0); $last_test_error = get_option('businessbot_api_last_test_error', ''); $last_test_time = (int) get_option('businessbot_api_last_test_time', 0); $status = 'not_connected'; $status_text = __('Not Connected', 'ai-chat-assistant-for-business'); $status_message = __('API key missing or invalid. Chatbot will not respond.', 'ai-chat-assistant-for-business'); if (!empty($api_key) && 'success' === $test_state) { $status = 'connected'; $status_text = __('Connected', 'ai-chat-assistant-for-business'); $status_message = __('Your AI is successfully connected and ready.', 'ai-chat-assistant-for-business'); } elseif (!empty($api_key) && 'error' !== $test_state) { $status = 'not_set'; $status_text = __('Not Tested', 'ai-chat-assistant-for-business'); $status_message = __('API key is saved. Run Test Connection to confirm it is working.', 'ai-chat-assistant-for-business'); } elseif (!empty($api_key) && 'error' === $test_state) { $status = 'not_connected'; $status_text = __('Not Connected', 'ai-chat-assistant-for-business'); $status_message = __('API key missing or invalid. Chatbot will not respond.', 'ai-chat-assistant-for-business'); } $masked_key = ''; if (!empty($api_key)) { $key_len = strlen($api_key); $masked_key = ($key_len > 8) ? substr($api_key, 0, 4) . str_repeat('*', max(6, $key_len - 8)) . substr($api_key, -4) : str_repeat('*', $key_len); } ?>

0 ? (string) $last_test_http_code : __('N/A', 'ai-chat-assistant-for-business')); ?>