options = get_option('mxchat_options', $this->get_default_options()); // Ensure defaults are used add_action('wp_enqueue_scripts', array($this, 'register_public_scripts_styles')); add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode')); add_action('wp_footer', array($this, 'append_chatbot_to_body')); } private function get_default_options() { return array( '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', 'close_button_color' => '#fff', 'chatbot_bg_color' => '#fff', 'chatbot_background_color' => '#212121', 'icon_color' => '#fff', 'chat_input_font_color' => '#212121', '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: Basic', 'intro_message' => 'Hello! How can I assist you today?', 'append_to_body' => 'on' ); } public function register_public_scripts_styles() { $chat_style_version = '1.3'; // Replace with your actual version $chat_script_version = '1.3'; // Replace with your actual version // Correct path to the CSS file wp_register_style( 'mxchat-chat-css', plugin_dir_url(__FILE__) . '../css/chat-style.css', // Correct path to css directory array(), $chat_style_version ); // Correct path to the JS file wp_register_script( 'mxchat-chat-js', plugin_dir_url(__FILE__) . '../js/chat-script.js', // Correct path to js directory array('jquery'), // Ensure jQuery is listed as a dependency $chat_script_version, true // Load in the footer ); } public function enqueue_public_scripts_styles() { wp_enqueue_style('mxchat-chat-css'); wp_enqueue_script('mxchat-chat-js', plugin_dir_url(__FILE__) . 'js/chat-script.js', array('jquery', 'complianz'), '1.0.0', true); // Fetch options from the database $this->options = get_option('mxchat_options'); // Localize script with necessary data, including nonce wp_localize_script('mxchat-chat-js', 'mxchatChat', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('mxchat_chat_nonce'), // Correct nonce creation 'link_target' => isset($this->options['link_target_toggle']) ? $this->options['link_target_toggle'] : 'off', 'rate_limit_message' => isset($this->options['rate_limit_message']) ? $this->options['rate_limit_message'] : 'Rate limit exceeded. Please try again later.', 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on' ? true : false, 'user_message_bg_color' => isset($this->options['user_message_bg_color']) ? $this->options['user_message_bg_color'] : '#fff', 'user_message_font_color' => isset($this->options['user_message_font_color']) ? $this->options['user_message_font_color'] : '#212121', 'bot_message_bg_color' => isset($this->options['bot_message_bg_color']) ? $this->options['bot_message_bg_color'] : '#212121', 'bot_message_font_color' => isset($this->options['bot_message_font_color']) ? $this->options['bot_message_font_color'] : '#fff', 'top_bar_bg_color' => isset($this->options['top_bar_bg_color']) ? $this->options['top_bar_bg_color'] : '#212121', 'send_button_font_color' => isset($this->options['send_button_font_color']) ? $this->options['send_button_font_color'] : '#212121', 'close_button_color' => isset($this->options['close_button_color']) ? $this->options['close_button_color'] : '#fff', 'chatbot_background_color' => isset($this->options['chatbot_background_color']) ? $this->options['chatbot_background_color'] : '#212121', 'chatbot_bg_color' => isset($this->options['chatbot_bg_color']) ? $this->options['chatbot_bg_color'] : '#fff', 'icon_color' => isset($this->options['icon_color']) ? $this->options['icon_color'] : '#fff', 'chat_input_font_color' => isset($this->options['chat_input_font_color']) ? $this->options['chat_input_font_color'] : '#212121', // Add chat_persistence_toggle to the array 'chat_persistence_toggle' => isset($this->options['chat_persistence_toggle']) ? $this->options['chat_persistence_toggle'] : 'off', )); } public function append_chatbot_to_body() { $options = get_option('mxchat_options', $this->get_default_options()); // Use default options if not set if (isset($options['append_to_body']) && $options['append_to_body'] === 'on') { echo do_shortcode('[mxchat_chatbot floating="yes"]'); } } private function mxchat_get_user_identifier() { return sanitize_text_field($_SERVER['REMOTE_ADDR']); } public function render_chatbot_shortcode($atts) { $this->enqueue_public_scripts_styles(); // Enqueue the styles and scripts here $attributes = shortcode_atts(array( 'floating' => 'yes', ), $atts); $is_floating = $attributes['floating'] === 'yes'; $this->options = get_option('mxchat_options', $this->get_default_options()); // Use default options if not set $bg_color = $this->options['chatbot_background_color'] ?? '#fff'; $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'; $intro_message = $this->options['intro_message'] ?? 'Hello! How can I assist you today?'; $top_bar_title = $this->options['top_bar_title'] ?? 'MxChat: Basic'; $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'; $close_button_color = $this->options['close_button_color'] ?? '#fff'; $chatbot_bg_color = $this->options['chatbot_bg_color'] ?? '#fff'; $pre_chat_message = isset($this->options['pre_chat_message']) ? sanitize_text_field(trim($this->options['pre_chat_message'])) : ''; $user_id = sanitize_key($this->mxchat_get_user_identifier()); $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id; $input_copy = isset($this->options['input_copy']) ? esc_attr($this->options['input_copy']) : 'How can I assist?'; $rate_limit_message = isset($this->options['rate_limit_message']) ? esc_attr($this->options['rate_limit_message']) : 'Rate limit exceeded. Please try again later.'; $privacy_toggle = isset($this->options['privacy_toggle']) && $this->options['privacy_toggle'] === 'on'; $privacy_text = isset($this->options['privacy_text']) ? wp_kses_post($this->options['privacy_text']) : 'By chatting, you agree to our privacy policy.'; $popular_question_1 = isset($this->options['popular_question_1']) ? esc_html($this->options['popular_question_1']) : ''; $popular_question_2 = isset($this->options['popular_question_2']) ? esc_html($this->options['popular_question_2']) : ''; $popular_question_3 = isset($this->options['popular_question_3']) ? esc_html($this->options['popular_question_3']) : ''; ob_start(); // Check if floating attribute is set to 'yes' and wrap accordingly if ($is_floating) { echo ''; if (!empty($pre_chat_message) && !get_transient($transient_key)) { echo '
'; echo esc_html($pre_chat_message); echo ''; echo '
'; } echo ''; } return ob_get_clean(); } } ?>