PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.0.8
MxChat – AI Chatbot & Content Generation for WordPress v1.0.8
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | includes/class-mxchat-public.php +148 -888 3.2.211.0.8 View file →
@@ -5,928 +5,188 @@
5 5
6 6 class MxChat_Public {
7 7 private $options;
8 8
9 - // True once any FLOATING instance has rendered on this request via the
10 - // shortcode (or the block wrapping it) — append_chatbot_to_body() then
11 - // yields, so an explicit floating placement never doubles up with the
12 - // Auto-Display instance (plan 95dd1e).
13 - private static $floating_instance_rendered = false;
14 -
15 9 public function __construct() {
16 - // Simply get the options without defining duplicated defaults
17 - $this->options = get_option('mxchat_options', array());
10 + $this->options = get_option('mxchat_options', $this->get_default_options()); // Ensure defaults are used
11 + add_action('wp_enqueue_scripts', array($this, 'register_public_scripts_styles'));
18 12 add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode'));
19 13 add_action('wp_footer', array($this, 'append_chatbot_to_body'));
20 14 }
21 15
22 -/**
23 - * UPDATED: Enhanced should_hide_chatbot method - only blocks auto-append, not shortcodes
24 - */
25 -private function should_hide_chatbot($context = 'auto') {
26 - global $post;
16 + private function get_default_options() {
17 + return array(
18 + 'user_message_bg_color' => '#fff',
19 + 'user_message_font_color' => '#212121',
20 + 'bot_message_bg_color' => '#212121',
21 + 'bot_message_font_color' => '#fff',
22 + 'top_bar_bg_color' => '#212121',
23 + 'send_button_font_color' => '#212121',
24 + 'close_button_color' => '#fff',
25 + 'chatbot_bg_color' => '#fff',
26 + 'chatbot_background_color' => '#212121',
27 + 'icon_color' => '#fff',
28 + 'chat_input_font_color' => '#212121',
29 + 'api_key' => '',
30 + '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:
31 + - Your name is [Chatbot Name]. Always introduce yourself as this name when appropriate.
32 + - 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.
33 + - When appropriate, highlight the benefits of [insert proper subject here]. Offer to guide visitors to relevant pages or provide them with more information.
34 + - 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.
35 + - Keep your responses short, concise, and to the point. Provide clear and direct answers suitable for a chatbot interaction.
36 + - 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.
37 + - 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.',
38 + 'model' => 'gpt-3.5-turbo',
39 + 'rate_limit' => '100',
40 + 'rate_limit_message' => 'Rate limit exceeded. Please try again later.',
41 + 'top_bar_title' => 'MxChat: Basic',
42 + 'intro_message' => 'Hello! How can I assist you today?',
43 + 'append_to_body' => 'on'
44 + );
45 + }
27 46
28 - if (!$post) {
29 - return false;
30 - }
47 + public function register_public_scripts_styles() {
48 + $chat_style_version = '1.0.8'; // Replace with your actual version
49 + $chat_script_version = '1.0.8'; // Replace with your actual version
31 50
32 - // Only hide if it's the auto-append context (floating="yes" from global setting)
33 - // Allow shortcodes to work regardless of this setting
34 - if ($context === 'auto') {
35 - // Check new visibility field first
36 - $visibility = get_post_meta($post->ID, '_mxchat_page_visibility', true);
37 - if ($visibility === 'hide') {
38 - return true;
39 - }
51 + // Correct path to the CSS file
52 + wp_register_style(
53 + 'mxchat-chat-css',
54 + plugin_dir_url(__FILE__) . '../css/chat-style.css', // Correct path to css directory
55 + array(),
56 + $chat_style_version
57 + );
40 58
41 - // Backward compat: check legacy field if new field not set
42 - if (empty($visibility)) {
43 - $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true);
44 - if ($hide_chatbot === '1') {
45 - return true;
46 - }
47 - }
59 + // Correct path to the JS file
60 + wp_register_script(
61 + 'mxchat-chat-js',
62 + plugin_dir_url(__FILE__) . '../js/chat-script.js', // Correct path to js directory
63 + array('jquery'), // Ensure jQuery is listed as a dependency
64 + $chat_script_version,
65 + true // Load in the footer
66 + );
48 67 }
49 68
50 - return false;
51 -}
69 + public function enqueue_public_scripts_styles() {
70 + wp_enqueue_style('mxchat-chat-css');
71 + wp_enqueue_script('mxchat-chat-js');
52 72
53 -/**
54 - * UPDATED: Enhanced append_chatbot_to_body method with context
55 - */
56 -public function append_chatbot_to_body() {
57 - // Only run on public pages
58 - if (is_admin() || wp_doing_ajax()) {
59 - return;
73 + // Localize script with necessary data, including nonce
74 + wp_localize_script('mxchat-chat-js', 'mxchatChat', array(
75 + 'ajax_url' => admin_url('admin-ajax.php'),
76 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'), // Correct nonce creation
77 + 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
78 + 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
79 + 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
80 + 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
81 + 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
82 + 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
83 + 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
84 + 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
85 + 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
86 + 'icon_color' => $this->options['icon_color'] ?? '#fff',
87 + 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121'
88 + ));
60 89 }
61 -
62 - // Check if auto-append chatbot should be hidden on this page
63 - if ($this->should_hide_chatbot('auto')) {
64 - return; // Don't show auto-appended chatbot
65 - }
66 90
67 - // An explicit floating placement (shortcode or the 95dd1e block) already
68 - // rendered during content — appending the auto instance would put two
69 - // floating widgets on the page with colliding element ids. The explicit
70 - // placement wins; content renders before wp_footer, so the flag is set
71 - // by the time this runs.
72 - if (self::$floating_instance_rendered) {
73 - return;
91 + public function append_chatbot_to_body() {
92 + $options = get_option('mxchat_options', $this->get_default_options()); // Use default options if not set
93 + if (isset($options['append_to_body']) && $options['append_to_body'] === 'on') {
94 + echo do_shortcode('[mxchat_chatbot floating="yes"]');
95 + }
74 96 }
75 97
76 - // Get the bot that should be displayed using new logic
77 - $bot_to_show = $this->get_display_bot();
78 -
79 - // Don't show chatbot if determination is false
80 - if ($bot_to_show === false) {
81 - return;
82 - }
83 -
84 - // Handle consent for display
85 - $consent_category = 'marketing';
86 - $has_consent = true;
87 -
88 - if (
89 - isset($this->options['complianz_toggle']) &&
90 - $this->options['complianz_toggle'] === 'on' &&
91 - function_exists('cmplz_has_consent')
92 - ) {
93 - $has_consent = cmplz_has_consent($consent_category);
94 - }
95 -
96 - // Display the appropriate chatbot (always floating for auto-append)
97 - if ($bot_to_show && $bot_to_show !== 'default') {
98 - // Show specific bot
99 - echo do_shortcode('[mxchat_chatbot floating="yes" bot_id="' . esc_attr($bot_to_show) . '" has_consent="' . ($has_consent ? 'yes' : 'no') . '"]');
100 - } else {
101 - // Show default bot
102 - echo do_shortcode('[mxchat_chatbot floating="yes" has_consent="' . ($has_consent ? 'yes' : 'no') . '"]');
103 - }
104 -}
105 -
106 98 private function mxchat_get_user_identifier() {
107 99 return sanitize_text_field($_SERVER['REMOTE_ADDR']);
108 100 }
109 101
110 -/**
111 - * UPDATED: Enhanced shortcode with context-aware hiding
112 - */
113 -public function render_chatbot_shortcode($atts) {
114 - // Smart asset loading safety net (plan-915355): if the opt-in enqueue gate
115 - // skipped assets on this request (shortcode invisible to has_shortcode —
116 - // builder-stored content, template files, widget areas), force the FULL
117 - // enqueue now, including the mxchatChat settings payload and the delayed
118 - // loader wiring. The integrator method is idempotent, so this is a no-op
119 - // when assets already went out at wp_enqueue_scripts time.
120 - if (self::is_smart_asset_loading_enabled() && !wp_style_is('mxchat-chat-css', 'enqueued')) {
121 - global $mxchat_integrator;
122 - if (isset($mxchat_integrator) && is_object($mxchat_integrator)
123 - && method_exists($mxchat_integrator, 'mxchat_enqueue_scripts_styles')) {
124 - $mxchat_integrator->mxchat_enqueue_scripts_styles(true);
125 - }
126 - }
102 + public function render_chatbot_shortcode($atts) {
103 + $this->enqueue_public_scripts_styles(); // Enqueue the styles and scripts here
127 104
128 - // UPDATED: Add bot_id parameter support and improve logic
129 - $attributes = shortcode_atts(array(
130 - 'floating' => 'yes',
131 - 'has_consent' => 'yes',
132 - 'bot_id' => '' // Support for multi-bot functionality - empty means auto-detect
133 - ), $atts);
134 -
135 - // Determine which bot to use
136 - $bot_id = $this->determine_bot_for_shortcode($attributes['bot_id']);
137 -
138 - // A floating shortcode always renders; what it does is suppress the LATER
139 - // auto-append instance (see append_chatbot_to_body), so a page never gets
140 - // two floating widgets. This replaced a dead empty branch here that
141 - // documented the intent but never suppressed anything (plan 95dd1e).
142 - if ($attributes['floating'] === 'yes') {
143 - self::$floating_instance_rendered = true;
144 - }
105 + $attributes = shortcode_atts(array(
106 + 'floating' => 'yes',
107 + ), $atts);
145 108
146 - // Non-floating shortcodes (floating="no") should NEVER be blocked by the hide setting
147 - // This allows embedded chatbots even when floating is hidden
148 -
149 - $is_floating = $attributes['floating'] === 'yes';
150 - $bot_id = sanitize_key($bot_id); // Sanitize bot ID
151 -
152 - // Rest of your existing shortcode rendering logic continues unchanged...
153 - // [All the existing HTML generation code remains the same]
154 -
155 - // Get bot-specific options if multi-bot add-on is active
156 - $bot_options = $this->get_bot_options($bot_id);
157 -
158 - // Use bot-specific options or fall back to default options
159 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
160 -
161 - // [Rest of your existing rendering code stays exactly the same]
162 - // Just remove the old should_hide_chatbot() check from the beginning
163 -
164 - // Check for Complianz consent if the toggle is enabled
165 - $initial_visibility = 'hidden';
166 - $additional_class = '';
167 -
168 - if (isset($current_options['complianz_toggle']) && $current_options['complianz_toggle'] === 'on') {
169 - $additional_class = ' no-consent';
170 - }
171 - $visibility_class = $initial_visibility . $additional_class;
172 -
173 - $theme_options = get_option('mxchat_theme_options', array());
174 - $custom_send_image = isset($theme_options['custom_send_button_image']) ? esc_url($theme_options['custom_send_button_image']) : '';
175 - $send_width = isset($theme_options['send_button_width']) ? intval($theme_options['send_button_width']) : 24;
176 - $send_height = isset($theme_options['send_button_height']) ? intval($theme_options['send_button_height']) : 24;
177 - $send_rotation = isset($theme_options['send_button_rotation']) ? intval($theme_options['send_button_rotation']) : 0;
109 + $is_floating = $attributes['floating'] === 'yes';
178 110
179 - // Check if an AI theme is active (global or bot-specific) - if so, skip inline color styles
180 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
181 - $bot_has_theme = isset($theme_options['bot_theme_assignments'][$bot_id]);
182 - $skip_inline_colors = $ai_theme_active || $bot_has_theme;
111 + $this->options = get_option('mxchat_options', $this->get_default_options()); // Use default options if not set
183 112
184 - // UPDATED: Use current_options instead of $this->options throughout
185 - $bg_color = $current_options['chatbot_background_color'] ?? '#fff';
186 - $user_message_bg_color = $current_options['user_message_bg_color'] ?? '#fff';
187 - $user_message_font_color = $current_options['user_message_font_color'] ?? '#212121';
188 - $bot_message_bg_color = $current_options['bot_message_bg_color'] ?? '#212121';
189 - $bot_message_font_color = $current_options['bot_message_font_color'] ?? '#fff';
190 - $top_bar_bg_color = $current_options['top_bar_bg_color'] ?? '#212121';
191 - $send_button_font_color = $current_options['send_button_font_color'] ?? '#212121';
192 - $intro_message = $current_options['intro_message'] ?? esc_html__('Hello! How can I assist you today?', 'mxchat');
193 - $top_bar_title = $current_options['top_bar_title'] ?? esc_html__('MxChat: Basic', 'mxchat');
194 - $chatbot_background_color = $current_options['chatbot_background_color'] ?? '#212121';
195 - $icon_color = $current_options['icon_color'] ?? '#fff';
196 - $chat_input_font_color = $current_options['chat_input_font_color'] ?? '#212121';
197 - $close_button_color = $current_options['close_button_color'] ?? '#fff';
198 - $chatbot_bg_color = $current_options['chatbot_bg_color'] ?? '#fff';
199 - $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_textarea_field(trim($current_options['pre_chat_message'])) : '';
200 - $user_id = sanitize_key($this->mxchat_get_user_identifier());
201 - $email_state = $this->determine_email_collection_state();
202 - $show_email_form = $email_state['show_email_form'];
203 - $user_email = $email_state['user_email'] ?? '';
204 - $user_name = $email_state['user_name'] ?? '';
205 - // Pre-chat dismissal now handled client-side via localStorage (zero server load)
206 - $input_copy = isset($current_options['input_copy']) ? esc_attr($current_options['input_copy']) : esc_attr__('How can I assist?', 'mxchat');
207 - $rate_limit_message = isset($current_options['rate_limit_message']) ? esc_attr($current_options['rate_limit_message']) : esc_attr__('Rate limit exceeded. Please try again later.', 'mxchat');
208 - $mode_indicator_bg_color = $current_options['mode_indicator_bg_color'] ?? '#212121';
209 - $mode_indicator_font_color = $current_options['mode_indicator_font_color'] ?? '#fff';
210 - $quick_questions_toggle_color = $current_options['quick_questions_toggle_color'] ?? '#212121';
113 + $bg_color = $this->options['chatbot_background_color'] ?? '#fff';
114 + $user_message_bg_color = $this->options['user_message_bg_color'] ?? '#fff';
115 + $user_message_font_color = $this->options['user_message_font_color'] ?? '#212121';
116 + $bot_message_bg_color = $this->options['bot_message_bg_color'] ?? '#212121';
117 + $bot_message_font_color = $this->options['bot_message_font_color'] ?? '#fff';
118 + $top_bar_bg_color = $this->options['top_bar_bg_color'] ?? '#212121';
119 + $send_button_font_color = $this->options['send_button_font_color'] ?? '#212121';
120 + $intro_message = $this->options['intro_message'] ?? 'Hello! How can I assist you today?';
121 + $top_bar_title = $this->options['top_bar_title'] ?? 'MxChat: Basic';
122 + $chatbot_background_color = $this->options['chatbot_background_color'] ?? '#212121';
123 + $icon_color = $this->options['icon_color'] ?? '#fff';
124 + $chat_input_font_color = $this->options['chat_input_font_color'] ?? '#212121';
125 + $close_button_color = $this->options['close_button_color'] ?? '#fff';
126 + $chatbot_bg_color = $this->options['chatbot_bg_color'] ?? '#fff';
127 + $pre_chat_message = isset($this->options['pre_chat_message']) ? sanitize_text_field(trim($this->options['pre_chat_message'])) : '';
128 + $user_id = sanitize_key($this->mxchat_get_user_identifier());
129 + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
211 130
212 - $privacy_toggle = isset($current_options['privacy_toggle']) && $current_options['privacy_toggle'] === 'on';
213 - $privacy_text = isset($current_options['privacy_text']) ? wp_kses_post($current_options['privacy_text']) : wp_kses_post(__('By chatting, you agree to our <a href="https://example.com/privacy-policy" target="_blank">privacy policy</a>.', 'mxchat'));
214 131
215 - $popular_question_1 = isset($current_options['popular_question_1']) ? esc_html($current_options['popular_question_1']) : '';
216 - $popular_question_2 = isset($current_options['popular_question_2']) ? esc_html($current_options['popular_question_2']) : '';
217 - $popular_question_3 = isset($current_options['popular_question_3']) ? esc_html($current_options['popular_question_3']) : '';
218 - $additional_questions = isset($current_options['additional_popular_questions']) ? $current_options['additional_popular_questions'] : [];
219 - $custom_icon = isset($current_options['custom_icon']) ? esc_url($current_options['custom_icon']) : '';
220 - $title_icon = isset($current_options['title_icon']) ? esc_url($current_options['title_icon']) : '';
221 - // AI agent text - if explicitly set to empty string, hide the indicator entirely
222 - $ai_agent_text = isset($current_options['ai_agent_text']) ? $current_options['ai_agent_text'] : __('AI Agent', 'mxchat');
132 + ob_start();
223 133
224 - $live_agent_message_bg_color = $current_options['live_agent_message_bg_color'] ?? '#212121';
225 - $live_agent_message_font_color = $current_options['live_agent_message_font_color'] ?? '#fff';
226 - $enable_email_block = isset($current_options['enable_email_block']) &&
227 - ($current_options['enable_email_block'] === '1' || $current_options['enable_email_block'] === 'on');
134 + // Check if floating attribute is set to 'yes' and wrap accordingly
135 + if ($is_floating) {
136 + echo '<div id="floating-chatbot" class="hidden">';
137 + }
228 138
229 - // Add name field variables
230 - $enable_name_field = isset($current_options['enable_name_field']) &&
231 - ($current_options['enable_name_field'] === '1' || $current_options['enable_name_field'] === 'on');
232 - $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
233 - esc_attr($current_options['name_field_placeholder']) :
234 - esc_attr__('Enter your name', 'mxchat');
139 + echo '<div id="mxchat-chatbot-wrapper">';
140 + echo ' <div class="chatbot-top-bar" id="exit-chat-button" style="background: ' . esc_attr($top_bar_bg_color) . ';">';
141 + echo ' <p class="chatbot-title" style="color: ' . esc_attr($close_button_color) . ';">' . esc_html($top_bar_title) . '</p>';
142 + echo ' <button class="exit-chat" type="button" aria-label="Minimize" style="color: ' . esc_attr($close_button_color) . ';">';
143 + echo ' <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" id="ic-minimize" style="fill: ' . esc_attr($close_button_color) . ';">';
144 + echo ' <path d="M0 0h24v24H0z" fill="none"></path>';
145 + echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>';
146 + echo ' </svg>';
147 + echo ' <span>Minimize</span>';
148 + echo ' </button>';
149 + echo ' </div>';
150 + echo ' <div id="mxchat-chatbot" style="background-color: ' . esc_attr($chatbot_bg_color) . '">';
151 + echo ' <div id="chat-container">';
152 + echo ' <div id="chat-box">';
153 + echo ' <div class="bot-message" style="background: ' . esc_attr($bot_message_bg_color) . '; color: ' . esc_attr($bot_message_font_color) . ';">';
154 + echo esc_html($intro_message);
155 + echo ' </div>';
156 + echo ' </div>';
157 + echo ' <div id="input-container">';
158 + echo ' <input type="text" id="chat-input" placeholder="How can I assist?" style="color: ' . esc_attr($chat_input_font_color) . ';">';
159 + echo ' <button id="send-button">';
160 + echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="fill: ' . esc_attr($send_button_font_color) . ';">';
161 + echo ' <path d="M498.1 5.6c10.1 7 15.4 19.1 13.5 31.2l-64 416c-1.5 9.7-7.4 18.2-16 23s-18.9 5.4-28 1.6L284 427.7l-68.5 74.1c-8.9 9.7-22.9 12.9-35.2 8.1S160 493.2 160 480V396.4c0-4 1.5-7.8 4.2-10.7L331.8 202.8c5.8-6.3 5.6-16-.4-22s-15.7-6.4-22-.7L106 360.8 17.7 316.6C7.1 311.3 .3 300.7 0 288.9s5.9-22.8 16.1-28.7l448-256c10.7-6.1 23.9-5.5 34 1.4z"/>';
162 + echo ' </svg>';
163 + echo ' </button>';
164 + echo ' </div>';
165 + echo ' <div class="chatbot-footer">';
166 + echo ' </div>';
167 + echo ' </div>';
168 + echo ' </div>';
169 + echo '</div>';
235 170
236 - // Consent checkbox (b062c4). Default OFF — with the toggle off this block
237 - // renders nothing and the form markup is byte-identical to before.
238 - $enable_consent_checkbox = isset($current_options['enable_consent_checkbox']) &&
239 - ($current_options['enable_consent_checkbox'] === '1' || $current_options['enable_consent_checkbox'] === 'on');
240 - $consent_checkbox_required = isset($current_options['consent_checkbox_required']) &&
241 - ($current_options['consent_checkbox_required'] === '1' || $current_options['consent_checkbox_required'] === 'on');
242 - // Owner content, kses'd through the shared allowlist at render time as well
243 - // as save time — the render is the security boundary the spec names.
244 - $consent_checkbox_label = MxChat_Utils::sanitize_consent_label(
245 - isset($current_options['consent_checkbox_label']) && $current_options['consent_checkbox_label'] !== ''
246 - ? $current_options['consent_checkbox_label']
247 - : __('I agree to the Privacy Policy.', 'mxchat')
248 - );
171 + if ($is_floating) {
172 + echo '</div>';
249 173
250 - ob_start();
251 -
252 - // Check if floating attribute is set to 'yes' and wrap accordingly
253 - if ($is_floating) {
254 - echo '<div id="floating-chatbot-' . esc_attr($bot_id) . '" class="floating-chatbot ' . $initial_visibility . $additional_class . '">';
255 - }
256 -
257 - // Add bot_id to the chatbot wrapper as a data attribute
258 - // data-nosnippet: keep the chat widget's UI copy (greeting, title, quick-question
259 - // prompts, privacy notice) out of Google search snippets. This wrapper renders in both
260 - // floating and inline/shortcode modes, so it covers all in-panel copy in one place.
261 - echo '<div id="mxchat-chatbot-wrapper-' . esc_attr($bot_id) . '" class="mxchat-chatbot-wrapper" data-nosnippet data-bot-id="' . esc_attr($bot_id) . '">';
262 -
263 - echo ' <div class="chatbot-top-bar" id="exit-chat-button-' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($top_bar_bg_color) . ';"') . '>';
264 - echo ' <div class="chatbot-title-container">';
265 - echo ' <div class="chatbot-title-group">';
266 - if (!empty($title_icon)) {
267 - echo ' <img src="' . esc_url($title_icon) . '" alt="" class="chatbot-title-icon">';
268 - }
269 - echo ' <p class="chatbot-title"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>' . esc_html($top_bar_title) . '</p>';
270 - echo ' </div>';
271 - // Only show mode indicator if ai_agent_text is not empty
272 - if (!empty(trim($ai_agent_text))) {
273 - echo '<span class="chat-mode-indicator" id="chat-mode-indicator-' . esc_attr($bot_id) . '" data-ai-text="' . esc_attr($ai_agent_text) . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($mode_indicator_font_color) . '; background-color: ' . esc_attr($mode_indicator_bg_color) . ';"') . '>' . esc_html($ai_agent_text) . '</span>';
274 - }
275 - echo ' </div>';
276 - // Overflow menu (3-dot) trigger + dropdown container.
277 - // Sibling of .exit-chat, rendered to its left. JS hides the trigger
278 - // if no menu items are enabled; outer click + Escape close the menu.
279 - echo ' <div class="mxchat-header-menu-wrap" data-bot-id="' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="--mxchat-menu-bg: ' . esc_attr($bot_message_bg_color) . '; --mxchat-menu-fg: ' . esc_attr($bot_message_font_color) . ';"') . '>';
280 - echo ' <button class="mxchat-header-btn mxchat-menu-trigger" type="button" aria-haspopup="menu" aria-expanded="false" aria-label="' . esc_attr__('More options', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
281 - echo ' <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">';
282 - echo ' <circle cx="12" cy="5" r="2"/><circle cx="12" cy="12" r="2"/><circle cx="12" cy="19" r="2"/>';
283 - echo ' </svg>';
284 - echo ' </button>';
285 - echo ' <div class="mxchat-header-menu" role="menu" aria-hidden="true" hidden></div>';
286 - echo ' </div>';
287 - echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Close', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
288 - echo ' <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" id="ic-close" aria-hidden="true">';
289 - echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
290 - echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
291 - echo ' </svg>';
292 - echo ' <span>' . esc_html__('Close', 'mxchat') . '</span>';
293 - echo ' </button>';
294 - echo ' </div>';
295 -
296 - // 3b) Main chatbot container
297 - echo ' <div id="mxchat-chatbot-' . esc_attr($bot_id) . '" class="mxchat-chatbot"' . ($skip_inline_colors ? '' : ' style="background-color: ' . esc_attr($chatbot_bg_color) . ';"') . '>';
298 -
299 - if ($enable_email_block) {
300 - echo '<div id="email-blocker-' . esc_attr($bot_id) . '" class="email-blocker" style="' . ($show_email_form ? '' : 'display: none;') . '">';
301 - echo ' <div class="email-blocker-header">';
302 -
303 - $header_html = isset($current_options['email_blocker_header_content'])
304 - ? $current_options['email_blocker_header_content']
305 - : '';
306 - echo wp_kses_post($header_html);
307 -
308 - echo ' </div>';
309 - echo ' <form id="email-collection-form-' . esc_attr($bot_id) . '" class="email-collection-form" method="POST" action="">';
310 -
311 - // Add name field if enabled
312 - if ($enable_name_field) {
313 - echo ' <label for="user-name-' . esc_attr($bot_id) . '" class="sr-only mxchat-name-label">' . esc_html__('Name', 'mxchat') . '</label>';
314 - echo ' <input type="text" id="user-name-' . esc_attr($bot_id) . '" name="user_name" class="mxchat-name-input" required placeholder="' . $name_field_placeholder . '" />';
315 - }
316 -
317 - echo ' <label for="user-email-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>';
318 - echo ' <input type="email" id="user-email-' . esc_attr($bot_id) . '" name="user_email" class="mxchat-email-input" required placeholder="' . esc_attr__('Enter your email address', 'mxchat') . '" />';
319 -
320 - // Consent checkbox row (b062c4). The native required attribute
321 - // is UX only — the save endpoint re-enforces it server-side.
322 - // Clicking an anchor inside the label activates the link, not
323 - // the checkbox (interactive descendants skip label activation).
324 - if ($enable_consent_checkbox) {
325 - echo ' <div class="mxchat-consent-row">';
326 - echo ' <input type="checkbox" id="user-consent-' . esc_attr($bot_id) . '" name="user_consent" value="1" class="mxchat-consent-checkbox"' . ($consent_checkbox_required ? ' required' : '') . ' />';
327 - echo ' <label for="user-consent-' . esc_attr($bot_id) . '" class="mxchat-consent-label">' . $consent_checkbox_label . '</label>';
328 - echo ' </div>';
329 - }
330 -
331 - echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">';
332 - $button_text = isset($current_options['email_blocker_button_text'])
333 - ? $current_options['email_blocker_button_text']
334 - : esc_html__('Start Chat', 'mxchat');
335 - echo esc_html($button_text);
336 - echo '</button>';
337 - echo ' </form>';
338 - echo '</div>';
339 - }
340 -
341 - echo ' <div id="mxchat-init-loader-' . esc_attr($bot_id) . '" class="mxchat-init-loader" style="display:none;">';
342 - echo ' <div class="mxchat-init-loader-dots">';
343 - echo ' <span class="dot"></span><span class="dot"></span><span class="dot"></span>';
344 - echo ' </div>';
345 - echo ' </div>';
346 -
347 - echo ' <div id="chat-container-' . esc_attr($bot_id) . '" class="chat-container" style="' . ($enable_email_block && $show_email_form ? 'display: none;' : '') . '">';
348 - // role="log" + polite live region: new messages are announced to
349 - // screen readers explicitly, so the arrival signal no longer rides
350 - // the post-reply input autofocus (suppressed on touch — plan 03799f).
351 - // additions-only + non-atomic, with aria-busy held on the streaming
352 - // bubble until [DONE] (chat-script.js) so a streamed reply is
353 - // announced once, complete — not per token (WCAG 4.1.3, plan 67f126).
354 - echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box" role="log" aria-live="polite" aria-atomic="false" aria-relevant="additions text" aria-label="' . esc_attr__('Chat messages', 'mxchat') . '">';
355 - // mxchat-intro-message marks the greeting bubble so nothing has to
356 - // infer it from position (plan a1a79b). Only this element ever
357 - // carries the class — reset and the {visitor_name} substitution
358 - // both key off it, and both used to assume the greeting was still
359 - // the first .bot-message in the box, which stops being true the
360 - // moment chat persistence rehydrates the transcript.
361 - echo ' <div class="bot-message mxchat-intro-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>';
362 - echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>';
363 - echo wp_kses_post($intro_message);
364 - echo ' </div>';
365 - echo ' </div>';
366 - echo ' </div>'; // end #chat-box
367 -
368 -
369 - // Replace the existing popular questions section with this:
370 - echo ' <div id="mxchat-popular-questions-' . esc_attr($bot_id) . '" class="mxchat-popular-questions">';
371 - echo ' <div class="mxchat-popular-questions-container">';
372 -
373 - // Collapse button (down arrow) - shows when open, centered at top
374 - echo ' <button class="questions-collapse-btn" aria-label="' . esc_attr__('Hide Quick Questions', 'mxchat') . '">';
375 - echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none"' . ($skip_inline_colors ? '' : ' stroke="' . esc_attr($quick_questions_toggle_color) . '"') . ' stroke-width="2">';
376 - echo ' <polyline points="6,9 12,15 18,9"></polyline>';
377 - echo ' </svg>';
378 - echo ' </button>';
379 -
380 - // Expand button (up arrow) - shows when collapsed
381 - echo ' <button class="questions-toggle-btn" aria-label="' . esc_attr__('Show Quick Questions', 'mxchat') . '">';
382 - echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none"' . ($skip_inline_colors ? '' : ' stroke="' . esc_attr($quick_questions_toggle_color) . '"') . ' stroke-width="2">';
383 - echo ' <polyline points="18,15 12,9 6,15"></polyline>';
384 - echo ' </svg>';
385 - echo ' </button>';
386 -
387 - if (!empty($popular_question_1)) {
388 - echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($popular_question_1) . '</button>';
389 - }
390 - if (!empty($popular_question_2)) {
391 - echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($popular_question_2) . '</button>';
392 - }
393 - if (!empty($popular_question_3)) {
394 - echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($popular_question_3) . '</button>';
395 - }
396 -
397 - if (!empty($additional_questions) && is_array($additional_questions)) {
398 - foreach ($additional_questions as $index => $question) {
399 - if (!empty($question)) {
400 - echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($question) . '</button>';
401 - }
402 - }
403 - }
404 -
405 - echo ' </div>';
406 - echo ' </div>';
407 -
408 - echo ' <div id="input-container-' . esc_attr($bot_id) . '" class="input-container">';
409 - // Max input length (plan a3fae2 part C) — global core setting, 0 = unlimited.
410 - // Hard-caps typing/paste client-side; the chat handler enforces it server-side too.
411 - $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
412 - $mxchat_maxlength_attr = $mxchat_max_input_length > 0 ? ' maxlength="' . esc_attr($mxchat_max_input_length) . '"' : '';
413 - // Visually-hidden label = the input's accessible name (WCAG 3.3.2,
414 - // plan 67f126). The placeholder stays as visible prompt copy but
415 - // cannot be the name — it vanishes on the first keystroke.
416 - echo ' <label for="chat-input-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Type your message', 'mxchat') . '</label>';
417 - echo ' <textarea id="chat-input-' . esc_attr($bot_id) . '" class="chat-input" dir="auto"' . $mxchat_maxlength_attr . ' placeholder="' . esc_attr($input_copy) . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($chat_input_font_color) . ';"') . '></textarea>';
418 - // Language-neutral character counter (plan 7091a2). Numbers only — no
419 - // translatable strings — so it reads correctly on every-language install.
420 - // Only rendered when a cap is set; hidden until ~80% of the cap, then
421 - // ramps neutral -> amber -> red. Decorative (aria-hidden); the textarea's
422 - // maxlength carries the real semantics for assistive tech.
423 - if ($mxchat_max_input_length > 0) {
424 - echo ' <div id="mxchat-char-counter-' . esc_attr($bot_id) . '" class="mxchat-char-counter" aria-hidden="true"><span class="mxchat-char-counter-current">0</span><span class="mxchat-char-counter-sep">/</span><span class="mxchat-char-counter-max">' . esc_html($mxchat_max_input_length) . '</span></div>';
425 - }
426 - echo ' <button id="send-button-' . esc_attr($bot_id) . '" class="send-button" aria-label="' . esc_attr__('Send message', 'mxchat') . '">';
427 - if (!empty($custom_send_image)) {
428 - echo ' <img src="' . esc_url($custom_send_image) . '" alt="' . esc_attr__('Send', 'mxchat') . '" style="width: ' . intval($send_width) . 'px; height: ' . intval($send_height) . 'px; transform: rotate(' . intval($send_rotation) . 'deg);" />';
429 - } else {
430 - $send_svg_style = 'width: ' . intval($send_width) . 'px; height: ' . intval($send_height) . 'px; transform: rotate(' . intval($send_rotation) . 'deg);';
431 - if (!$skip_inline_colors) {
432 - $send_svg_style = 'fill: ' . esc_attr($send_button_font_color) . '; ' . $send_svg_style;
433 - }
434 - echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="' . $send_svg_style . '">';
435 - echo ' <path d="M498.1 5.6c10.1 7 15.4 19.1 13.5 31.2l-64 416c-1.5 9.7-7.4 18.2-16 23s-18.9 5.4-28 1.6L284 427.7l-68.5 74.1c-8.9 9.7-22.9 12.9-35.2 8.1S160 493.2 160 480V396.4c0-4 1.5-7.8 4.2-10.7L331.8 202.8c5.8-6.3 5.6-16-.4-22s-15.7-6.4-22-.7L106 360.8 17.7 316.6C7.1 311.3 .3 300.7 0 288.9s5.9-22.8 16.1-28.7l448-256c10.7-6.1 23.9-5.5 34 1.4z"></path>';
436 - echo ' </svg>';
437 - }
438 - echo ' </button>';
439 - echo ' </div>';
440 -
441 - echo ' <div class="chat-toolbar">';
442 -
443 - // PDF Upload Button - wrapped in conditional using current_options
444 - $show_pdf_button = isset($current_options['show_pdf_upload_button']) ? $current_options['show_pdf_upload_button'] : 'on';
445 - if ($show_pdf_button === 'on') {
446 - echo ' <input type="file" id="pdf-upload-' . esc_attr($bot_id) . '" class="pdf-upload" accept=".pdf" style="display: none;">';
447 - echo ' <button id="pdf-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn pdf-upload-btn" title="' . esc_attr__('Upload PDF', 'mxchat') . '">';
448 - echo ' <!-- Icon from Font Awesome Free: https://fontawesome.com/license/free -->';
449 - echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" stroke="currentColor">';
450 - echo ' <path d="M64 464l48 0 0 48-48 0c-35.3 0-64-28.7-64-64L0 64C0 28.7 28.7 0 64 0L229.5 0c17 0 33.3 6.7 45.3 18.7l90.5 90.5c12 12 18.7 28.3 18.7 45.3L384 304l-48 0 0-144-80 0c-17.7 0-32-14.3-32-32l0-80L64 48c-8.8 0-16 7.2-16 16l0 384c0 8.8 7.2 16 16 16zM176 352l32 0c30.9 0 56 25.1 56 56s-25.1 56-56 56l-16 0 0 32c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-48 0-80c0-8.8 7.2-16 16-16zm32 80c13.3 0 24-10.7 24-24s-10.7-24-24-24l-16 0 0 48 16 0zm96-80l32 0c26.5 0 48 21.5 48 48l0 64c0 26.5-21.5 48-48 48l-32 0c-8.8 0-16-7.2-16-16l0-128c0-8.8 7.2-16 16-16zm32 128c8.8 0 16-7.2 16-16l0-64c0-8.8-7.2-16-16-16l-16 0 0 96 16 0zm80-112c0-8.8 7.2-16 16-16l48 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 32 32 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 48c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-64 0-64z"></path>';
451 - echo ' </svg>';
452 - echo ' </button>';
453 - }
454 -
455 - // Word Upload Button - wrapped in conditional using current_options
456 - $show_word_button = isset($current_options['show_word_upload_button']) ? $current_options['show_word_upload_button'] : 'on';
457 - if ($show_word_button === 'on') {
458 - echo ' <input type="file" id="word-upload-' . esc_attr($bot_id) . '" class="word-upload" accept=".docx" style="display: none;">';
459 - echo ' <button id="word-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn word-upload-btn" title="' . esc_attr__('Upload Word Document', 'mxchat') . '">';
460 - echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" stroke="currentColor">';
461 - echo ' <path d="M48 448L48 64c0-8.8 7.2-16 16-16l160 0 0 80c0 17.7 14.3 32 32 32l80 0 0 288c0 8.8-7.2 16-16 16L64 464c-8.8 0-16-7.2-16-16zM64 0C28.7 0 0 28.7 0 64L0 448c0 35.3 28.7 64 64 64l256 0c35.3 0 64-28.7 64-64l0-293.5c0-17-6.7-33.3-18.7-45.3L274.7 18.7C262.7 6.7 246.5 0 229.5 0L64 0zm55 241.1c-3.8-12.7-17.2-19.9-29.9-16.1s-19.9 17.2-16.1 29.9l48 160c3 10.2 12.4 17.1 23 17.1s19.9-7 23-17.1l25-83.4 25 83.4c3 10.2 12.4 17.1 23 17.1s19.9-7 23-17.1l48-160c3.8-12.7-3.4-26.1-16.1-29.9s-26.1 3.4-29.9 16.1l-25 83.4-25-83.4c-3-10.2-12.4-17.1-23-17.1s-19.9 7-23 17.1l-25 83.4-25-83.4z"/></svg>';
462 - echo ' </button>';
463 - }
464 -
465 - // File containers
466 - echo ' <div id="active-pdf-container-' . esc_attr($bot_id) . '" class="active-pdf-container" style="display: none;">';
467 - echo ' <span id="active-pdf-name-' . esc_attr($bot_id) . '" class="active-pdf-name"></span>';
468 - echo ' <button id="remove-pdf-btn-' . esc_attr($bot_id) . '" class="remove-pdf-btn" title="' . esc_attr__('Remove PDF', 'mxchat') . '">';
469 - echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2">';
470 - echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
471 - echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
472 - echo ' </svg>';
473 - echo ' </button>';
474 - echo ' </div>';
475 -
476 - echo ' <div id="active-word-container-' . esc_attr($bot_id) . '" class="active-word-container" style="display: none;">';
477 - echo ' <span id="active-word-name-' . esc_attr($bot_id) . '" class="active-word-name"></span>';
478 - echo ' <button id="remove-word-btn-' . esc_attr($bot_id) . '" class="remove-word-btn" title="' . esc_attr__('Remove Word Document', 'mxchat') . '">';
479 - echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2">';
480 - echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
481 - echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
482 - echo ' </svg>';
483 - echo ' </button>';
484 - echo ' </div>';
485 -
486 - // Perplexity Button
487 - if (apply_filters('mxchat_perplexity_should_show_logo', true)) {
488 - echo ' <button id="perplexity-search-btn-' . esc_attr($bot_id) . '" class="toolbar-btn perplexity-search-btn" title="' . esc_attr__('Search with Perplexity', 'mxchat-perplexity') . '">';
489 - echo ' <svg fill="currentColor" height="1em" viewBox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg">';
490 - echo ' <path d="M19.785 0v7.272H22.5V17.62h-2.935V24l-7.037-6.194v6.145h-1.091v-6.152L4.392 24v-6.465H1.5V7.188h2.884V0l7.053 6.494V.19h1.09v6.49L19.786 0zm-7.257 9.044v7.319l5.946 5.234V14.44l-5.946-5.397zm-1.099-.08l-5.946 5.398v7.235l5.946-5.234V8.965zm8.136 7.58h1.844V8.349H13.46l6.105 5.54v2.655zm-8.982-8.28H2.59v8.195h1.8v-2.576l6.192-5.62zM5.475 2.476v4.71h5.115l-5.115-4.71zm13.219 0l-5.115 4.71h5.115v-4.71z"></path>';
491 - echo ' </svg>';
492 - echo ' </button>';
493 - }
494 -
495 -
496 - // Image Analysis Button - hidden by default, controlled by MxChat Vision add-on
497 - echo ' <input type="file" id="image-upload-' . esc_attr($bot_id) . '" class="image-upload" accept="image/*" style="display: none;" multiple>';
498 - echo ' <button id="image-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn image-upload-btn" title="' . esc_attr__('Upload Image for Analysis', 'mxchat') . '" style="display: none;">';
499 - echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="fill: none !important;">';
500 - echo ' <path d="M14.5 4h-5L7 7H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-3l-2.5-3z" style="fill: none !important;"/>';
501 - echo ' <circle cx="12" cy="13" r="3" style="fill: none !important;"/>';
502 - echo ' </svg>';
503 - echo ' </button>';
504 -
505 -
506 - echo ' </div>';
507 -
508 - echo ' <div class="chatbot-footer">';
509 -
510 - // Output the privacy notice if enabled
511 - if ($privacy_toggle && !empty($privacy_text)) {
512 - echo '<p class="privacy-notice">' . $privacy_text . '</p>';
513 - }
514 -
515 - echo ' </div>';
516 - echo ' </div>';
517 - echo ' </div>';
174 + if (!empty($pre_chat_message) && !get_transient($transient_key)) {
175 + echo '<div id="pre-chat-message">';
176 + echo esc_html($pre_chat_message);
177 + echo '<button class="close-pre-chat-message" aria-label="Close">&times;</button>';
518 178 echo '</div>';
519 -
520 - if ($is_floating) {
521 - echo '</div>';
522 -
523 - if (!empty($pre_chat_message)) {
524 - // Rendered hidden by default — JS checkPreChatDismissal() handles show/hide via localStorage
525 - // data-nosnippet: the pre-chat teaser bubble is a sibling outside
526 - // .mxchat-chatbot-wrapper, so it needs its own marker to stay out of snippets.
527 - echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message" data-nosnippet style="display:none;">';
528 - echo nl2br(esc_html($pre_chat_message));
529 - echo '<button class="close-pre-chat-message" aria-label="' . esc_attr__('Close', 'mxchat') . '">&times;</button>';
530 - echo '</div>';
531 - }
532 -
533 - $is_initially_hidden = (strpos($visibility_class, 'hidden') !== false);
534 - $aria_expanded = $is_initially_hidden ? 'false' : 'true';
535 - echo '<div class="floating-chatbot-button ' . esc_attr($visibility_class) . '" id="floating-chatbot-button-' . esc_attr($bot_id) . '" role="button" tabindex="0" aria-label="' . esc_attr__('Open chat', 'mxchat') . '" aria-expanded="' . esc_attr($aria_expanded) . '" aria-controls="floating-chatbot-' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($chatbot_background_color) . '; color: ' . esc_attr($send_button_font_color) . ';"') . '>';
536 - echo '<div id="chat-notification-badge-' . esc_attr($bot_id) . '" class="chat-notification-badge" style="display: none; position: absolute; top: -8px; right: -8px; background-color: #ff4444; color: white; border-radius: 50%; padding: 4px 8px; font-size: 12px; font-weight: bold; z-index: 10001;">1</div>';
537 -
538 - if (!empty($custom_icon)) {
539 - echo '<img src="' . $custom_icon . '" alt="' . esc_attr__('Chatbot Icon', 'mxchat') . '" style="height: 48px; width: 48px; object-fit: contain;" />';
540 - } else {
541 - $widget_icon_style = 'height: 48px; width: 48px;';
542 - if (!$skip_inline_colors) {
543 - $widget_icon_style .= ' fill: ' . esc_attr($icon_color) . ';';
544 - }
545 - echo '<svg id="widget_icon_10" style="' . $widget_icon_style . '" viewBox="0 0 1120 1120" fill="none" xmlns="http://www.w3.org/2000/svg">';
546 - echo ' <path fill-rule="evenodd" clip-rule="evenodd" d="M252 434C252 372.144 302.144 322 364 322H770C831.856 322 882 372.144 882 434V614.459L804.595 585.816C802.551 585.06 800.94 583.449 800.184 581.405L763.003 480.924C760.597 474.424 751.403 474.424 748.997 480.924L711.816 581.405C711.06 583.449 709.449 585.06 707.405 585.816L606.924 622.997C600.424 625.403 600.424 634.597 606.924 637.003L707.405 674.184C709.449 674.94 711.06 676.551 711.816 678.595L740.459 756H629.927C629.648 756.476 629.337 756.945 628.993 757.404L578.197 825.082C572.597 832.543 561.403 832.543 555.803 825.082L505.007 757.404C504.663 756.945 504.352 756.476 504.073 756H364C302.144 756 252 705.856 252 644V434ZM633.501 471.462C632.299 468.212 627.701 468.212 626.499 471.462L619.252 491.046C618.874 492.068 618.068 492.874 617.046 493.252L597.462 500.499C594.212 501.701 594.212 506.299 597.462 507.501L617.046 514.748C618.068 515.126 618.874 515.932 619.252 516.954L626.499 536.538C627.701 539.788 632.299 539.788 633.501 536.538L640.748 516.954C641.126 515.932 641.932 515.126 642.954 514.748L662.538 507.501C665.788 506.299 665.788 501.701 662.538 500.499L642.954 493.252C641.932 492.874 641.126 492.068 640.748 491.046L633.501 471.462Z" ></path>';
547 - echo ' <path d="M771.545 755.99C832.175 755.17 881.17 706.175 881.99 645.545L804.595 674.184C802.551 674.94 800.94 676.551 800.184 678.595L771.545 755.99Z" ></path>';
548 - echo '</svg>';
549 - }
550 - echo '</div>';
551 - }
552 -
553 - return ob_get_clean();
554 - }
555 -
556 - /**
557 - * Get bot-specific options for multi-bot functionality
558 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
559 - */
560 - private function get_bot_options($bot_id = 'default') {
561 - // If default bot or multi-bot add-on not active, return empty (use default options)
562 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
563 - return array();
564 179 }
565 -
566 - //Hook for multi-bot add-on to provide bot-specific options
567 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
568 -
569 - return is_array($bot_options) ? $bot_options : array();
570 - }
571 -
572 - /**
573 - * Get bot-specific Pinecone configuration
574 - * Used in the knowledge retrieval functions
575 - */
576 - private function get_bot_pinecone_config($bot_id = 'default') {
577 - // If default bot or multi-bot add-on not active, use default Pinecone config
578 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
579 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
580 - return array(
581 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
582 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
583 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
584 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
585 - );
586 - }
587 -
588 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
589 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
590 -
591 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
592 - }
593 -
594 -
595 - private function determine_email_collection_state() {
596 - // Logged-in users skip the email form
597 - if (is_user_logged_in()) {
598 - $current_user = wp_get_current_user();
599 - return [
600 - 'show_email_form' => false,
601 - 'user_email' => $current_user->user_email,
602 - 'user_name' => $current_user->display_name ?: $current_user->first_name ?: ''
603 - ];
604 - }
605 180
606 - // For guests, default to showing the email form.
607 - // The session-based check happens client-side via AJAX since the
608 - // session ID lives in the browser cookie/localStorage.
609 - return [
610 - 'show_email_form' => true,
611 - 'user_email' => '',
612 - 'user_name' => ''
613 - ];
614 -}
615 -
616 -
617 - /**
618 - * NEW: Determine if and which chatbot should be displayed
619 - */
620 -private function get_display_bot() {
621 - // Delegates to the static so the enqueue-time gate (smart asset loading,
622 - // plan-915355) and this render-time decision share ONE code path and can
623 - // never disagree. The static reads mxchat_options fresh — same row this
624 - // instance loaded at construct.
625 - return self::compute_display_bot();
626 -}
627 -
628 -/**
629 - * Static single source of truth for the display decision (plan-915355).
630 - * Combines the per-page meta box, the append_to_body global toggle, and the
631 - * post-type include/exclude mode. Returns a bot id ('default' or specific)
632 - * when the auto-append widget will render on the current request, false when
633 - * it won't. Static (not a second instance) deliberately: MxChat_Public's
634 - * constructor registers a wp_footer action, so constructing a throwaway
635 - * instance would double-append the widget.
636 - */
637 -public static function compute_display_bot() {
638 - // The Elementor builder canvas is not a chat surface (plan 95dd1e part 2):
639 - // its preview iframe is a real front-end page load, so without this guard
640 - // the Auto-Display instance renders LIVE inside the canvas — front-end
641 - // chat scripts firing in the editor, able to start a session for the
642 - // person editing. Returning false here suppresses both the wp_footer
643 - // auto-append and the asset enqueue gate for preview requests only; the
644 - // placement widget's own canvas rendering is a static placeholder anyway.
645 - if (did_action('elementor/loaded')
646 - && class_exists('\Elementor\Plugin')
647 - && isset(\Elementor\Plugin::$instance->preview)
648 - && \Elementor\Plugin::$instance->preview
649 - && \Elementor\Plugin::$instance->preview->is_preview_mode()) {
650 - return false;
651 - }
652 -
653 - $options = get_option('mxchat_options', array());
654 - if (!is_array($options)) {
655 - $options = array();
656 - }
657 -
658 - // Get page-specific settings from meta box
659 - $page_setting = self::get_page_bot_setting();
660 -
661 - $global_autoshow = isset($options['append_to_body']) && $options['append_to_body'] === 'on';
662 - $global_default_bot = isset($options['default_bot']) ? $options['default_bot'] : 'default';
663 -
664 - // If page specifically hides chatbot, don't show anything
665 - if ($page_setting && $page_setting['action'] === 'hide') {
666 - return false;
667 - }
668 -
669 - // If page specifies a specific bot, use that
670 - if ($page_setting && $page_setting['action'] === 'show') {
671 - return $page_setting['bot_id'];
672 - }
673 -
674 - // Page setting is 'global' or no page setting exists
675 - // Check global auto-show setting
676 - if ($global_autoshow) {
677 - // Check post type visibility settings
678 - if (!self::should_show_on_current_post_type()) {
679 - return false;
181 + echo '<div class="hidden" id="floating-chatbot-button" style="background: ' . esc_attr($chatbot_background_color) . '; color: ' . esc_attr($send_button_font_color) . ';">';
182 + echo ' <svg id="widget_icon_10" style="height: 48px; width: 48px; fill: ' . esc_attr($icon_color) . '" viewBox="0 0 1120 1120" fill="none" xmlns="http://www.w3.org/2000/svg">';
183 + echo ' <path fill-rule="evenodd" clip-rule="evenodd" d="M252 434C252 372.144 302.144 322 364 322H770C831.856 322 882 372.144 882 434V614.459L804.595 585.816C802.551 585.06 800.94 583.449 800.184 581.405L763.003 480.924C760.597 474.424 751.403 474.424 748.997 480.924L711.816 581.405C711.06 583.449 709.449 585.06 707.405 585.816L606.924 622.997C600.424 625.403 600.424 634.597 606.924 637.003L707.405 674.184C709.449 674.94 711.06 676.551 711.816 678.595L740.459 756H629.927C629.648 756.476 629.337 756.945 628.993 757.404L578.197 825.082C572.597 832.543 561.403 832.543 555.803 825.082L505.007 757.404C504.663 756.945 504.352 756.476 504.073 756H364C302.144 756 252 705.856 252 644V434ZM633.501 471.462C632.299 468.212 627.701 468.212 626.499 471.462L619.252 491.046C618.874 492.068 618.068 492.874 617.046 493.252L597.462 500.499C594.212 501.701 594.212 506.299 597.462 507.501L617.046 514.748C618.068 515.126 618.874 515.932 619.252 516.954L626.499 536.538C627.701 539.788 632.299 539.788 633.501 536.538L640.748 516.954C641.126 515.932 641.932 515.126 642.954 514.748L662.538 507.501C665.788 506.299 665.788 501.701 662.538 500.499L642.954 493.252C641.932 492.874 641.126 492.068 640.748 491.046L633.501 471.462Z" ></path>';
184 + echo ' <path d="M771.545 755.99C832.175 755.17 881.17 706.175 881.99 645.545L804.595 674.184C802.551 674.94 800.94 676.551 800.184 678.595L771.545 755.99Z" ></path>';
185 + echo ' </svg>';
186 + echo '</div>';
680 187 }
681 188
682 - // Global auto-show is enabled, return the default bot
683 - return $global_default_bot;
189 + return ob_get_clean();
684 190 }
685 -
686 - // Global auto-show is disabled and no page-specific bot selected
687 - // Don't show chatbot (user should use shortcodes)
688 - return false;
689 191 }
690 -
691 -/**
692 - * Smart asset loading opt-in (plan-915355). Standalone option — deliberately
693 - * NOT a mxchat_options key, so it can never be stripped by mxchat_sanitize().
694 - * Default off: enqueue behavior is byte-identical to before until an owner
695 - * turns the toggle on.
696 - */
697 -public static function is_smart_asset_loading_enabled() {
698 - return get_option('mxchat_smart_asset_loading', 'off') === 'on';
699 -}
700 -
701 -/**
702 - * Will the chat widget render on the current request? (plan-915355)
703 - *
704 - * True when the auto-append decision resolves to a bot, OR the singular
705 - * post's content contains the [mxchat_chatbot] shortcode (first-chance
706 - * detection so shortcode pages keep head-loaded CSS — no FOUC). Computed
707 - * once per request and cached, so the wp_enqueue_scripts gate and any
708 - * add-on consulting this later in the same request always get one answer.
709 - *
710 - * Filter `mxchat_should_load_assets` is the force-load escape hatch for
711 - * headless/builder/custom-JS setups whose shortcode placement is invisible
712 - * to has_shortcode (builder-stored content, template files, widget areas).
713 - * Note the render-time safety net in render_chatbot_shortcode() still
714 - * force-loads assets whenever the shortcode actually renders — the filter
715 - * is only needed where even that net can't fire (e.g. markup assembled
716 - * outside WP rendering).
717 - */
718 -public static function should_load_assets() {
719 - static $cached = null;
720 - if ($cached !== null) {
721 - return $cached;
722 - }
723 -
724 - // Never gate admin/ajax requests — this decision is for front-end enqueues only.
725 - if (is_admin()) {
726 - $cached = true;
727 - return $cached;
728 - }
729 -
730 - $display_bot = self::compute_display_bot();
731 - $has_shortcode = false;
732 -
733 - if ($display_bot === false && is_singular()) {
734 - $post = get_post();
735 - if ($post && has_shortcode((string) $post->post_content, 'mxchat_chatbot')) {
736 - $has_shortcode = true;
737 - }
738 - // The mxchat/chatbot block (plan-95dd1e) stores a block comment, not
739 - // shortcode text, so has_shortcode can't see it — first-chance detect
740 - // it here for the same no-FOUC reason. The render-time safety net in
741 - // render_chatbot_shortcode() still covers template/widget placements.
742 - if ($post && !$has_shortcode && function_exists('has_block')
743 - && has_block('mxchat/chatbot', $post)) {
744 - $has_shortcode = true;
745 - }
746 - }
747 -
748 - $should = ($display_bot !== false) || $has_shortcode;
749 -
750 - $cached = (bool) apply_filters('mxchat_should_load_assets', $should, array(
751 - 'display_bot' => $display_bot,
752 - 'has_shortcode' => $has_shortcode,
753 - 'post_id' => get_the_ID(),
754 - ));
755 -
756 - return $cached;
757 -}
758 -
759 -/**
760 - * Check if chatbot should be shown on the current post type
761 - */
762 -private static function should_show_on_current_post_type() {
763 - $options = get_option('mxchat_options', array());
764 - if (!is_array($options)) {
765 - $options = array();
766 - }
767 - // Get visibility settings
768 - $mode = isset($options['post_type_visibility_mode']) ? $options['post_type_visibility_mode'] : 'all';
769 - $list = isset($options['post_type_visibility_list']) ? $options['post_type_visibility_list'] : array();
770 -
771 - // Ensure list is an array
772 - if (!is_array($list)) {
773 - $list = array();
774 - }
775 -
776 - // If mode is 'all', show on all post types
777 - if ($mode === 'all') {
778 - return true;
779 - }
780 -
781 - // Get current post type
782 - $current_post_type = self::get_current_post_type();
783 -
784 - // If we can't determine post type, default to showing
785 - if (empty($current_post_type)) {
786 - return true;
787 - }
788 -
789 - // Check based on mode
790 - if ($mode === 'include') {
791 - // Only show on selected post types
792 - return in_array($current_post_type, $list);
793 - } elseif ($mode === 'exclude') {
794 - // Hide on selected post types
795 - return !in_array($current_post_type, $list);
796 - }
797 -
798 - // Default to showing
799 - return true;
800 -}
801 -
802 -/**
803 - * Get the current post type
804 - */
805 -private static function get_current_post_type() {
806 - // Try to get from queried object first
807 - $queried_object = get_queried_object();
808 -
809 - if ($queried_object instanceof WP_Post) {
810 - return $queried_object->post_type;
811 - }
812 -
813 - // Try get_post_type()
814 - $post_type = get_post_type();
815 - if ($post_type) {
816 - return $post_type;
817 - }
818 -
819 - // Check if we're on an archive
820 - if (is_post_type_archive()) {
821 - return get_query_var('post_type');
822 - }
823 -
824 - // Check common archive types
825 - if (is_home() || is_single()) {
826 - return 'post';
827 - }
828 -
829 - if (is_page()) {
830 - return 'page';
831 - }
832 -
833 - return '';
834 -}
835 -
836 -/**
837 - * Get page-specific bot setting using new visibility field with backward compat
838 - */
839 -private static function get_page_bot_setting($post_id = null) {
840 - if (!$post_id) {
841 - $post_id = get_the_ID();
842 - }
843 -
844 - if (!$post_id) {
845 - return null;
846 - }
847 -
848 - // Check new visibility field first
849 - $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true);
850 -
851 - if ($visibility === 'hide') {
852 - return array('action' => 'hide');
853 - }
854 -
855 - if ($visibility === 'show') {
856 - $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
857 - $bot_id = !empty($selected_bot) ? $selected_bot : 'default';
858 - return array('action' => 'show', 'bot_id' => $bot_id);
859 - }
860 -
861 - // Backward compat: check legacy hide checkbox if no new field set
862 - if (empty($visibility)) {
863 - $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true);
864 - if ($hide_chatbot === '1') {
865 - return array('action' => 'hide');
866 - }
867 - }
868 -
869 - // Check if specific bot is selected (legacy path)
870 - $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
871 - if (!empty($selected_bot)) {
872 - return array('action' => 'show', 'bot_id' => $selected_bot);
873 - }
874 -
875 - // Use global setting
876 - return array('action' => 'global');
877 -}
878 -
879 -/**
880 - * NEW: Determine which bot to use for shortcode
881 - */
882 -private function determine_bot_for_shortcode($shortcode_bot_id) {
883 - // If bot_id explicitly provided in shortcode, use that
884 - if (!empty($shortcode_bot_id)) {
885 - return $shortcode_bot_id;
886 - }
887 -
888 - // No bot_id in shortcode, check page setting
889 - $page_setting = self::get_page_bot_setting();
890 - if ($page_setting && $page_setting['action'] === 'show') {
891 - return $page_setting['bot_id'];
892 - }
893 -
894 - // Fall back to default
895 - return 'default';
896 -}
897 -
898 -/**
899 - * NEW: Helper to check if auto-append is enabled globally
900 - */
901 -private function is_auto_append_enabled() {
902 - return isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
903 -}
904 -
905 -/**
906 - * UPDATED: Debug function with new context info
907 - */
908 -
909 -/**
910 - * NEW: Helper method to get available bots (for admin notices, etc.)
911 - */
912 -private function get_available_bots() {
913 - $bots = array('default' => __('Default Bot', 'mxchat'));
914 -
915 - // Check if multi-bot addon is active
916 - if (class_exists('MxChat_Multi_Bot_Manager')) {
917 - $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance();
918 - $available_bots = $multi_bot_manager->get_available_bots();
919 -
920 - // Add the available bots
921 - foreach ($available_bots as $bot_id => $bot_name) {
922 - $bots[$bot_id] = $bot_name;
923 - }
924 - }
925 -
926 - return $bots;
927 -}
928 -
929 -
930 -
931 -}
932 -?>
192 +?>