options = get_option('mxchat_options'); $this->chat_count = get_option('mxchat_chat_count', 0); $this->is_activated = $this->is_license_active(); // Initialize default options if they are not set if (!$this->options) { $this->initialize_default_options(); } // Add admin menu and initialize settings add_action('admin_menu', array($this, 'mxchat_add_plugin_page')); add_action('admin_init', array($this, 'mxchat_page_init')); add_action('admin_enqueue_scripts', array($this, 'mxchat_enqueue_admin_assets')); add_action('wp_ajax_mxchat_delete_chat_history', array($this, 'mxchat_delete_chat_history')); add_action('admin_post_mxchat_submit_content', array($this, 'mxchat_handle_content_submission')); add_action('admin_post_mxchat_delete_prompt', array($this, 'mxchat_handle_delete_prompt')); add_action('wp_ajax_mxchat_fetch_chat_history', array($this, 'mxchat_fetch_chat_history')); add_action('wp_ajax_nopriv_mxchat_fetch_chat_history', array($this, 'mxchat_fetch_chat_history')); add_action('admin_post_mxchat_submit_sitemap', array($this, 'mxchat_handle_sitemap_submission')); add_action('wp_footer', array($this, 'mxchat_append_chatbot_to_body')); add_action('admin_head-mxchat-prompts', array($this, 'mxchat_enqueue_admin_assets')); add_action('admin_head-toplevel_page_mxchat-max', array($this, 'mxchat_enqueue_admin_assets')); add_action('wp_ajax_mxchat_activate_license', array($this, 'mxchat_handle_activate_license')); add_action('admin_notices', array($this, 'mxchat_display_admin_notice')); } // Method to check if the license is active private function is_license_active() { $license_status = get_option('mxchat_license_status', 'inactive'); return $license_status === 'active'; } // Initialize default options private function initialize_default_options() { $default_options = array( 'api_key' => '', 'system_prompt_instructions' => '[EXAMPLE INSTRUCTIONS] You are an AI Chatbot assistant for this website. The primary subject you should focus on is [insert proper subject here]. Your main goal is to assist visitors with questions related to this specific topic. Here are some key things to keep in mind: - Your name is [Chatbot Name]. Always introduce yourself as this name when appropriate. - Stay focused on topics related to [insert proper subject here]. If a visitor asks about an unrelated topic, politely redirect the conversation to how you can assist them with this subject. If there is an exception topic (e.g., "parking") that you should assist with, you may do so if instructed. - When appropriate, highlight the benefits of [insert proper subject here]. Offer to guide visitors to relevant pages or provide them with more information. - If a visitor asks for a purchase link or further information, provide them with this link: [Insert Purchase Link Here]. Always ensure that the link is relevant and directly related to the website\'s offerings. - Keep your responses short, concise, and to the point. Provide clear and direct answers suitable for a chatbot interaction. - If you reference specific content, provide a hyperlink to the relevant page using hypertext. Avoid including links that do not directly relate to the content or answer the visitor\'s query. - Provide answers based on the knowledge available to you. If you do not have an answer to a specific question, let the visitor know that you don’t have the information and suggest where they might find it or offer to help with something else.', 'model' => 'gpt-3.5-turbo', 'rate_limit' => '100', 'rate_limit_message' => 'Rate limit exceeded. Please try again later.', 'top_bar_title' => 'MxChat', 'intro_message' => 'Hello! How can I assist you today?', 'append_to_body' => 'on', 'close_button_color' => '#fff', 'chatbot_bg_color' => '#fff', 'user_message_bg_color' => '#fff', 'user_message_font_color' => '#212121', 'bot_message_bg_color' => '#212121', 'bot_message_font_color' => '#fff', 'top_bar_bg_color' => '#212121', 'send_button_font_color' => '#212121', 'chat_input_font_color' => '#212121', 'chatbot_background_color' => '#212121', 'icon_color' => '#fff', 'enable_woocommerce_integration' => '0', ); // Merge existing options with defaults $existing_options = get_option('mxchat_options', array()); $merged_options = wp_parse_args($existing_options, $default_options); // Update the options if they have changed if ($existing_options !== $merged_options) { update_option('mxchat_options', $merged_options); } // Update the $this->options property $this->options = $merged_options; } public function mxchat_add_plugin_page() { // Main menu page add_menu_page( 'MxChat Settings', 'MxChat', 'manage_options', 'mxchat-max', array($this, 'mxchat_create_admin_page'), 'dashicons-testimonial', 6 ); // Submenu page for Knowledge add_submenu_page( 'mxchat-max', 'Prompts', 'Knowledge', 'manage_options', 'mxchat-prompts', array($this, 'mxchat_create_prompts_page') ); // Submenu page for Chat Transcripts add_submenu_page( 'mxchat-max', // Corrected parent slug to match the main menu 'Chat Transcripts', 'Transcripts', 'manage_options', 'mxchat-transcripts', array($this, 'mxchat_create_transcripts_page') // Prefixed function name with mxchat_ ); // Submenu page for Activation Key add_submenu_page( 'mxchat-max', 'Pro Upgrade', 'Pro Upgrade', 'manage_options', 'mxchat-activation', array($this, 'mxchat_create_activation_page') ); } public function mxchat_handle_content_submission() { // Check if the form was submitted and the user has sufficient permissions if (!isset($_POST['submit_content']) || !current_user_can('manage_options')) { return; } // Verify the nonce field for security $nonce = isset($_POST['mxchat_submit_content_nonce']) ? sanitize_text_field(wp_unslash($_POST['mxchat_submit_content_nonce'])) : ''; if (!wp_verify_nonce($nonce, 'mxchat_submit_content_action')) { wp_die('Nonce verification failed.'); } // Sanitize the content input $article_content = sanitize_textarea_field($_POST['article_content']); // Generate the embedding vector for the content $embedding_vector = $this->mxchat_generate_embedding($article_content); global $wpdb; $table_name = $wpdb->prefix . 'mxchat_system_prompt_content'; // Check if the 'source_url' column exists in the table and add it if it doesn't if ($wpdb->get_var($wpdb->prepare("SHOW COLUMNS FROM {$table_name} LIKE %s", 'source_url')) != 'source_url') { // Use wpdb::query and a prepared statement to avoid SQL injection $wpdb->query($wpdb->prepare("ALTER TABLE {$table_name} ADD source_url VARCHAR(255) DEFAULT ''")); } if (is_array($embedding_vector)) { // Serialize the embedding vector before storing it $embedding_vector_serialized = serialize($embedding_vector); // Insert the content and embedding vector into the database, using a prepared statement $inserted = $wpdb->insert( $table_name, array( 'article_content' => $article_content, 'embedding_vector' => $embedding_vector_serialized, 'source_url' => '', // Empty string as a placeholder for now, or pass a valid URL if applicable ), array( '%s', // Format for article_content (string) '%s', // Format for embedding_vector (serialized string) '%s', // Format for source_url (string) ) ); if ($inserted === false) { error_log('Error inserting content: ' . $wpdb->last_error); set_transient('mxchat_admin_notice', 'Error inserting content into the database. Please try again.', 30); } else { set_transient('mxchat_admin_notice', 'Content successfully submitted!', 30); } } else { error_log('Embedding generation failed for article content: ' . $article_content); set_transient('mxchat_admin_notice', 'Embedding generation failed. Please ensure your API key is correct and try again.', 30); } // Redirect after setting the transient wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts'))); exit; } public function mxchat_display_admin_notice() { // Get the message from the transient $message = get_transient('mxchat_admin_notice'); if ($message) { echo '
' . esc_html($message) . '
'; echo '| ID | Article Content | URL | Actions |
|---|---|---|---|
| id); ?> | article_content))); ?> | source_url)): ?> source_url); ?> N/A | Delete |
| No prompts found. | |||
Enable to append the chat widget directly to the body element or use shortcode: [mxchat_chatbot floating="yes"]
'; } public function mxchat_enqueue_admin_assets() { wp_enqueue_style('wp-color-picker'); // Get the plugin version or file modification time for cache busting $plugin_version = '1.0.8'; // Replace this with your plugin's version // File paths $color_picker_js_path = plugin_dir_path(__FILE__) . '../js/my-color-picker.js'; $embedding_check_js_path = plugin_dir_path(__FILE__) . '../js/embedding-check.js'; $admin_css_path = plugin_dir_path(__FILE__) . '../css/admin-style.css'; $transcripts_js_path = plugin_dir_path(__FILE__) . '../js/mxchat_transcripts.js'; // Check if files exist and get modification times $color_picker_version = file_exists($color_picker_js_path) ? filemtime($color_picker_js_path) : $plugin_version; $embedding_check_version = file_exists($embedding_check_js_path) ? filemtime($embedding_check_js_path) : $plugin_version; $admin_css_version = file_exists($admin_css_path) ? filemtime($admin_css_path) : $plugin_version; $transcripts_js_version = file_exists($transcripts_js_path) ? filemtime($transcripts_js_path) : $plugin_version; // Enqueue scripts and styles with corrected paths wp_enqueue_script( 'mxchat-color-picker', plugin_dir_url(__FILE__) . '../js/my-color-picker.js', array('wp-color-picker'), $color_picker_version, true ); wp_enqueue_script( 'mxchat-embedding-check', plugin_dir_url(__FILE__) . '../js/embedding-check.js', array(), $embedding_check_version, true ); wp_enqueue_script( 'mxchat-transcripts-js', plugin_dir_url(__FILE__) . '../js/mxchat_transcripts.js', array('jquery'), $transcripts_js_version, true ); wp_enqueue_script( 'mxchat-admin-js', plugin_dir_url(__FILE__) . '../js/mxchat-admin.js', array('jquery'), $plugin_version, true ); wp_localize_script('mxchat-admin-js', 'mxchatAdmin', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('mxchat_activate_license_nonce'), )); wp_enqueue_style( 'mxchat-admin-css', plugin_dir_url(__FILE__) . '../css/admin-style.css', array(), $admin_css_version ); // Use wp_json_encode for localizing script wp_localize_script('mxchat-color-picker', 'mxchatStyleSettings', array( 'ajax_url' => admin_url('admin-ajax.php'), 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff', 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121', 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121', 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff', 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121', 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121', 'close_button_color' => $this->options['close_button_color'] ?? '#fff', 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121', 'icon_color' => $this->options['icon_color'] ?? '#fff', 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121' )); } public function mxchat_sanitize($input) { $new_input = array(); if (isset($input['api_key'])) { $new_input['api_key'] = sanitize_text_field($input['api_key']); } if (isset($input['enable_woocommerce_integration'])) { $new_input['enable_woocommerce_integration'] = isset($input['enable_woocommerce_integration']) && $input['enable_woocommerce_integration'] === '1' ? '1' : '0'; } if (isset($input['system_prompt_instructions'])) { $new_input['system_prompt_instructions'] = sanitize_textarea_field($input['system_prompt_instructions']); } if (isset($input['mxchat_pro_email'])) { $new_input['mxchat_pro_email'] = sanitize_email($input['mxchat_pro_email']); } if (isset($input['mxchat_activation_key'])) { $new_input['mxchat_activation_key'] = sanitize_text_field($input['mxchat_activation_key']); } if (isset($input['append_to_body'])) { $new_input['append_to_body'] = $input['append_to_body'] === 'on' ? 'on' : 'off'; } if (isset($input['top_bar_title'])) { $new_input['top_bar_title'] = sanitize_text_field($input['top_bar_title']); } if (isset($input['intro_message'])) { $new_input['intro_message'] = sanitize_text_field($input['intro_message']); } if (isset($input['rate_limit_message'])) { $new_input['rate_limit_message'] = sanitize_text_field($input['rate_limit_message']); } if (isset($input['pre_chat_message'])) { $new_input['pre_chat_message'] = sanitize_textarea_field($input['pre_chat_message']); } if (isset($input['model'])) { $allowed_models = array( 'gpt-4o', 'gpt-4o-mini', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo', ); if (in_array($input['model'], $allowed_models)) { $new_input['model'] = sanitize_text_field($input['model']); } } // Sanitize new pro features if (isset($input['close_button_color'])) { $new_input['close_button_color'] = sanitize_hex_color($input['close_button_color']); } if (isset($input['chatbot_bg_color'])) { $new_input['chatbot_bg_color'] = sanitize_hex_color($input['chatbot_bg_color']); } if (isset($input['woocommerce_consumer_key'])) { $new_input['woocommerce_consumer_key'] = sanitize_text_field($input['woocommerce_consumer_key']); } if (isset($input['woocommerce_consumer_secret'])) { $new_input['woocommerce_consumer_secret'] = sanitize_text_field($input['woocommerce_consumer_secret']); } if (isset($input['user_message_bg_color'])) { $new_input['user_message_bg_color'] = sanitize_hex_color($input['user_message_bg_color']); } if (isset($input['user_message_font_color'])) { $new_input['user_message_font_color'] = sanitize_hex_color($input['user_message_font_color']); } if (isset($input['bot_message_bg_color'])) { $new_input['bot_message_bg_color'] = sanitize_hex_color($input['bot_message_bg_color']); } if (isset($input['bot_message_font_color'])) { $new_input['bot_message_font_color'] = sanitize_hex_color($input['bot_message_font_color']); } if (isset($input['top_bar_bg_color'])) { $new_input['top_bar_bg_color'] = sanitize_hex_color($input['top_bar_bg_color']); } if (isset($input['send_button_font_color'])) { $new_input['send_button_font_color'] = sanitize_hex_color($input['send_button_font_color']); } if (isset($input['chatbot_background_color'])) { $new_input['chatbot_background_color'] = sanitize_hex_color($input['chatbot_background_color']); } if (isset($input['icon_color'])) { $new_input['icon_color'] = sanitize_hex_color($input['icon_color']); } if (isset($input['chat_input_font_color'])) { $new_input['chat_input_font_color'] = sanitize_hex_color($input['chat_input_font_color']); } return $new_input; } // Method to append the chatbot to the body public function mxchat_append_chatbot_to_body() { $options = get_option('mxchat_options'); if (isset($options['append_to_body']) && $options['append_to_body'] === 'on') { echo do_shortcode('[mxchat_chatbot floating="yes"]'); } } private function mxchat_extract_main_content($html) { $dom = new DOMDocument; libxml_use_internal_errors(true); // Suppress HTML parsing errors @$dom->loadHTML($html); libxml_clear_errors(); $xpath = new DOMXPath($dom); // Simplified selectors focusing on common content areas $selectors = [ '//article', '//*[@id="content"]', '//*[@class="entry-content"]', '//main', ]; foreach ($selectors as $selector) { $nodes = $xpath->query($selector); if ($nodes->length > 0) { $content = ''; foreach ($nodes as $node) { $content .= $dom->saveHTML($node); } return $content; } } // Fallback: Return the entire body content if no specific selector matches $body = $dom->getElementsByTagName('body'); return $body->length > 0 ? $dom->saveHTML($body->item(0)) : $html; } public function mxchat_handle_sitemap_submission() { if (!isset($_POST['submit_sitemap']) || !current_user_can('manage_options')) { return; } check_admin_referer('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce'); $sitemap_url = esc_url_raw($_POST['sitemap_url']); $response = wp_remote_get($sitemap_url); if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { set_transient('mxchat_admin_notice', 'Failed to fetch the sitemap. Please check the URL and try again.', 30); wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts'))); exit; } $sitemap_content = wp_remote_retrieve_body($response); $xml = simplexml_load_string($sitemap_content); if ($xml === false) { set_transient('mxchat_admin_notice', 'Invalid sitemap XML. Please provide a valid sitemap.', 30); wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts'))); exit; } $embedding_success = true; // Flag to track if all embeddings are successful foreach ($xml->url as $url_element) { $page_url = (string)$url_element->loc; $page_response = wp_remote_get($page_url); if (is_wp_error($page_response) || wp_remote_retrieve_response_code($page_response) !== 200) { continue; } $page_html = wp_remote_retrieve_body($page_response); $page_content = $this->mxchat_extract_main_content($page_html); $sanitized_content = $this->mxchat_sanitize_content_for_api($page_content); if (!empty($sanitized_content)) { $embedding_vector = $this->mxchat_generate_embedding($sanitized_content); if (is_array($embedding_vector)) { MxChat_Utils::submit_content_to_db($sanitized_content, $page_url, $this->options['api_key']); } else { $embedding_success = false; // Set flag to false if any embedding fails } } } if ($embedding_success) { set_transient('mxchat_admin_notice', 'Sitemap content successfully submitted!', 30); } else { set_transient('mxchat_admin_notice', 'Some content failed to embed. Please check your API key and try again.', 30); } wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts'))); exit; } private function mxchat_sanitize_content_for_api($content) { // Remove script, style tags, and HTML comments $content = preg_replace('/