includes(); add_action( 'wp_ajax_qcld_grok_response', array( $this, 'grok_response_callback' ) ); add_action( 'wp_ajax_nopriv_qcld_grok_response', array( $this, 'grok_response_callback' ) ); add_action('wp_ajax_qcld_stream_grokai', array($this, 'qcld_stream_grok_callback')); add_action( 'wp_ajax_qcld_grok_add_collection', array( $this, 'qcld_grok_add_collection_callback' ) ); add_action('wp_ajax_qcld_grok_collectionlist', array($this, 'qcld_grok_collectionlist_callback')); add_action('wp_ajax_nopriv_qcld_stream_grokai', array($this, 'qcld_stream_grok_callback')); add_action('wp_ajax_qcld_grok_remove_collection', array($this, 'qcld_grok_remove_collection_callback')); add_action( 'wp_ajax_qcld_grok_settings_option', array( $this, 'qcld_grok_settings_option_callback' ) ); add_action( 'wp_ajax_qcld_grok_file_upload', array( $this, 'qcld_grok_file_upload_callback' ) ); if ( is_admin() && ! empty( $_GET['page'] ) && ( ( $_GET['page'] == 'openai-panel_dashboard' ) || ( $_GET['page'] == 'openai-panel_file' ) || ( $_GET['page'] == 'wpbot_openAi' ) || ( $_GET['page'] == 'openai-panel_help' ) ) ) {// phpcs:ignore WordPress.Security.NonceVerification.Missing add_action( 'admin_enqueue_scripts', array( $this, 'qcld_wb_chatbot_grok_admin_scripts' ) ); } // add_action('wp_enqueue_scripts', array($this, 'qcld_wb_chatbot_grok_scripts')); } public function qcld_wb_chatbot_grok_admin_scripts() { wp_register_script( 'qcld-wp-chatbot-grok-admin', QCLD_wpCHATBOT_PLUGIN_URL . 'includes/integration/grok/js/qcld-wp-grok-admin.js', array( 'jquery' ), QCLD_wpCHATBOT_VERSION, true ); // Localize the script with necessary data. wp_localize_script( 'qcld-wp-chatbot-grok-admin', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'ajax_nonce' => wp_create_nonce( 'wp_chatbot' ), 'grok_api_key' => get_option( 'qcld_grok_api_key' ), 'grok_model' => get_option( 'qcld_grok_model' ), 'grok_enabled' => get_option( 'qcld_grok_enabled' ), 'qcld_grok_rag_enabled' => get_option( 'qcld_grok_rag_enabled' ), 'qcld_grok_append_content' => get_option( 'qcld_grok_append_content' ), 'qcld_grok_prepend_content' => get_option( 'qcld_grok_prepend_content' ), ) ); wp_enqueue_script( 'qcld-wp-chatbot-grok-admin' ); } /** * Define wpbot Constants. * * @return void * @since 1.0.0 */ public function includes() { require_once QCLD_wpCHATBOT_PLUGIN_DIR_PATH . 'includes/Parsedown.php'; require_once QCLD_wpCHATBOT_PLUGIN_DIR_PATH . 'includes/class-common-function.php'; } public function qcld_grok_settings_option_callback() { $nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); if ( ! wp_verify_nonce( $nonce, 'wp_chatbot' ) || ! current_user_can( 'manage_options' ) ) { wp_send_json( array( 'success' => false, 'msg' => esc_html__( 'Failed in Security check', 'chatbot'), ) ); wp_die(); } else { $grok_api_key = sanitize_text_field( wp_unslash($_POST['grok_api_key']) ?? '' ); $grok_model = sanitize_text_field( wp_unslash($_POST['grok_model']) ?? '' ); $grok_enabled = sanitize_text_field(wp_unslash($_POST['grok_enabled'])); $qcld_grok_page_suggestion_enabled = sanitize_text_field(wp_unslash($_POST['qcld_grok_page_suggestion_enabled'])); $qcld_grok_append_content = sanitize_text_field(wp_unslash($_POST['qcld_grok_append_content'])) ?? ''; $qcld_grok_prepend_content = sanitize_text_field(wp_unslash($_POST['qcld_grok_prepend_content'])) ?? ''; $grok_rag_enabled = sanitize_text_field(wp_unslash($_POST['grok_rag_enabled'])) ?? ''; $qcld_grok_system_content = sanitize_text_field(wp_unslash($_POST['qcld_grok_system_content'])) ?? ''; $grok_stream_enabled = sanitize_text_field(wp_unslash($_POST['grok_stream_enabled'])) ?? ''; $grok_management_api_key = sanitize_text_field( wp_unslash($_POST['grok_management_api_key']) ?? '' ); $grok_collection_id = sanitize_text_field( wp_unslash($_POST['grok_collection_id']) ?? '' ); if ( $grok_management_api_key != '' ) { update_option( 'qcld_grok_management_api_key', $grok_management_api_key ); } if ( $grok_rag_enabled != '' ) { update_option( 'qcld_grok_rag_enabled', $grok_rag_enabled ); } if ( $grok_stream_enabled != '' ) { update_option( 'qcld_grok_stream_enabled', $grok_stream_enabled ); } if ( $grok_api_key != '' ) { update_option( 'qcld_grok_api_key', $grok_api_key ); } if ( $grok_model != '' ) { update_option( 'qcld_grok_model', $grok_model ); } if ( $grok_enabled != '' ) { update_option( 'qcld_grok_enabled', $grok_enabled ); } if ( $grok_collection_id != '' ) { update_option( 'qcld_grok_collection_id', $grok_collection_id ); } if ( $grok_enabled == '1' ) { update_option( 'ai_enabled', 0 ); update_option( 'qcld_ollama_enabled', 0 ); update_option('qcld_gemini_enabled', 0); update_option( 'qcld_openrouter_enabled', 0 ); update_option('disable_wp_chatbot_site_search',1); } if ( $qcld_grok_system_content != '' ) { update_option( 'qcld_grok_system_content', $qcld_grok_system_content ); } update_option( 'qcld_grok_page_suggestion_enabled', $qcld_grok_page_suggestion_enabled ); if ( isset( $_POST['openai_post_type'] ) ) { $openai_post_types = is_array( $_POST['openai_post_type'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['openai_post_type'] ) ) : sanitize_text_field( wp_unslash( $_POST['openai_post_type'] ) ); update_option( 'qcld_openai_relevant_post', $openai_post_types ); } update_option( 'qcld_grok_append_content', $qcld_grok_append_content ); update_option( 'qcld_grok_prepend_content', $qcld_grok_prepend_content ); } wp_send_json( $grok_enabled ); } public function grok_response_callback() { if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wp_chatbot' ) ) { wp_send_json_error([ 'status' => 'error', 'message' => esc_html__( 'Security check failed. Unauthorized request.', 'chatbot' ) ]); wp_die(); } if (get_option('is_rate_limiting_enabled') == '1') { do_action('rate_limit_checker'); } $grok_api_key = get_option( 'qcld_grok_api_key' ); $keyword = isset($_POST['keyword']) ? sanitize_text_field( wp_unslash($_POST['keyword']) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing $grok_system_content = get_option( 'qcld_grok_system_content' ); // AI Interactive Form if (get_option('enable_ai_interactive_form') == '1') { $saved_ai_forms = get_option('wpbot_ai_forms', array()); if (!empty($saved_ai_forms) && is_array($saved_ai_forms)) { $grok_system_content .= "\n\nYou must handle the following interactive forms when the user asks for them:\n"; foreach ($saved_ai_forms as $form) { $grok_system_content .= "\nForm Title: " . $form['title'] . "\nInstructions: " . $form['prompt'] . "\n"; } $grok_system_content .= "\n\nCRITICAL INSTRUCTIONS FOR INTERACTIVE FORMS:\n"; $grok_system_content .= "When a user triggers an interactive form, you must act as a step-by-step data collection agent.\n"; $grok_system_content .= "1. DO NOT ask all questions at once. Ask exactly ONE question at a time.\n"; $grok_system_content .= "2. Wait for the user's response before asking the next question.\n"; $grok_system_content .= "3. Once all necessary information is collected for the form, you MUST output a final JSON block summarizing the collected data. The keys inside the \"data\" object MUST be dynamically named based on the specific questions you asked during the form collection (e.g., \"Full Name\", \"Company Size\", \"Email\", etc.). The final JSON block must be wrapped EXACTLY in these delimiters:\n"; $grok_system_content .= "__AI_FORM_DATA__{ \"form_title\": \"