PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.0.6
MxChat – AI Chatbot & Content Generation for WordPress v3.0.6
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 +161 -302 3.2.213.0.6 View file →
@@ -5,49 +5,97 @@
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 10 // Simply get the options without defining duplicated defaults
17 11 $this->options = get_option('mxchat_options', array());
18 12 add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode'));
19 13 add_action('wp_footer', array($this, 'append_chatbot_to_body'));
14 +
15 + // Initialize testing panel for admins
16 + add_action('wp_enqueue_scripts', array($this, 'enqueue_testing_assets'));
17 +
18 + // Add debug hook
19 + add_action('wp_footer', array($this, 'debug_display_logic'));
20 20 }
21 21
22 + /**
23 + * Enqueue testing panel assets for admin users
24 + */
25 + public function enqueue_testing_assets() {
26 + // Check if frontend debugger is enabled in settings
27 + $options = get_option('mxchat_options', array());
28 + $show_debugger = isset($options['show_frontend_debugger']) ? $options['show_frontend_debugger'] : 'on';
29 +
30 + // Only load if setting is enabled AND (user is admin or testing parameter is present)
31 + if ($show_debugger === 'on' && (current_user_can('administrator') || isset($_GET['mxchat_test']))) {
32 +
33 + // Get plugin URL for assets
34 + $plugin_url = plugin_dir_url(dirname(__FILE__));
35 +
36 + // Enqueue test panel CSS
37 + wp_enqueue_style(
38 + 'mxchat-test-panel',
39 + $plugin_url . 'css/test-panel.css',
40 + array(),
41 + '2.5.2'
42 + );
43 +
44 + // Enqueue test panel JS
45 + wp_enqueue_script(
46 + 'mxchat-test-panel',
47 + $plugin_url . 'js/test-panel.js',
48 + array('jquery'),
49 + '2.5.2',
50 + true
51 + );
52 +
53 + // Pass data to JavaScript
54 + wp_localize_script('mxchat-test-panel', 'mxchatTestData', array(
55 + 'ajaxUrl' => admin_url('admin-ajax.php'),
56 + 'nonce' => wp_create_nonce('mxchat_test_nonce'),
57 + 'isAdmin' => current_user_can('administrator'),
58 + 'testingEnabled' => true
59 + ));
60 +
61 + // Add JavaScript flag to enable testing
62 + add_action('wp_footer', array($this, 'add_testing_flag'));
63 + }
64 + }
65 +
66 + /**
67 + * Add JavaScript flag to enable testing panel
68 + */
69 + public function add_testing_flag() {
70 + echo '<script>window.mxchatTestingEnabled = true;</script>';
71 + }
72 +
73 + /**
74 + * Check if testing mode should be enabled
75 + */
76 + private function is_testing_mode_enabled() {
77 + return (current_user_can('administrator') || isset($_GET['mxchat_test']));
78 + }
79 +
22 80 /**
23 81 * UPDATED: Enhanced should_hide_chatbot method - only blocks auto-append, not shortcodes
24 82 */
25 83 private function should_hide_chatbot($context = 'auto') {
26 84 global $post;
27 -
85 +
28 86 if (!$post) {
29 87 return false;
30 88 }
31 -
89 +
90 + $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true);
91 +
32 92 // Only hide if it's the auto-append context (floating="yes" from global setting)
33 93 // 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 - }
40 -
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 - }
94 + if ($context === 'auto' && $hide_chatbot === '1') {
95 + return true;
48 96 }
49 -
97 +
50 98 return false;
51 99 }
52 100
53 101 /**
@@ -62,18 +110,9 @@
62 110 // Check if auto-append chatbot should be hidden on this page
63 111 if ($this->should_hide_chatbot('auto')) {
64 112 return; // Don't show auto-appended chatbot
65 113 }
66 -
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;
74 - }
75 -
114 +
76 115 // Get the bot that should be displayed using new logic
77 116 $bot_to_show = $this->get_display_bot();
78 117
79 118 // Don't show chatbot if determination is false
@@ -110,22 +149,8 @@
110 149 /**
111 150 * UPDATED: Enhanced shortcode with context-aware hiding
112 151 */
113 152 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 - }
127 -
128 153 // UPDATED: Add bot_id parameter support and improve logic
129 154 $attributes = shortcode_atts(array(
130 155 'floating' => 'yes',
131 156 'has_consent' => 'yes',
@@ -134,16 +159,20 @@
134 159
135 160 // Determine which bot to use
136 161 $bot_id = $this->determine_bot_for_shortcode($attributes['bot_id']);
137 162
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).
163 + // UPDATED: Only check hiding for floating shortcodes that could conflict with auto-append
164 + // Non-floating shortcodes should always work
142 165 if ($attributes['floating'] === 'yes') {
143 - self::$floating_instance_rendered = true;
166 + // For floating shortcodes, check if auto-append is hidden
167 + // This prevents duplicate floating chatbots
168 + if ($this->should_hide_chatbot('auto') && $this->is_auto_append_enabled()) {
169 + // If auto-append is enabled but hidden on this page,
170 + // allow the floating shortcode to work (user is overriding)
171 + // But if auto-append is disabled globally, also allow shortcode
172 + }
144 173 }
145 -
174 +
146 175 // Non-floating shortcodes (floating="no") should NEVER be blocked by the hide setting
147 176 // This allows embedded chatbots even when floating is hidden
148 177
149 178 $is_floating = $attributes['floating'] === 'yes';
@@ -195,15 +224,15 @@
195 224 $icon_color = $current_options['icon_color'] ?? '#fff';
196 225 $chat_input_font_color = $current_options['chat_input_font_color'] ?? '#212121';
197 226 $close_button_color = $current_options['close_button_color'] ?? '#fff';
198 227 $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'])) : '';
228 + $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_text_field(trim($current_options['pre_chat_message'])) : '';
200 229 $user_id = sanitize_key($this->mxchat_get_user_identifier());
201 230 $email_state = $this->determine_email_collection_state();
202 231 $show_email_form = $email_state['show_email_form'];
203 232 $user_email = $email_state['user_email'] ?? '';
204 233 $user_name = $email_state['user_name'] ?? '';
205 - // Pre-chat dismissal now handled client-side via localStorage (zero server load)
234 + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
206 235 $input_copy = isset($current_options['input_copy']) ? esc_attr($current_options['input_copy']) : esc_attr__('How can I assist?', 'mxchat');
207 236 $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 237 $mode_indicator_bg_color = $current_options['mode_indicator_bg_color'] ?? '#212121';
209 238 $mode_indicator_font_color = $current_options['mode_indicator_font_color'] ?? '#fff';
@@ -226,28 +255,14 @@
226 255 $enable_email_block = isset($current_options['enable_email_block']) &&
227 256 ($current_options['enable_email_block'] === '1' || $current_options['enable_email_block'] === 'on');
228 257
229 258 // Add name field variables
230 - $enable_name_field = isset($current_options['enable_name_field']) &&
259 + $enable_name_field = isset($current_options['enable_name_field']) &&
231 260 ($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']) :
261 + $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
262 + esc_attr($current_options['name_field_placeholder']) :
234 263 esc_attr__('Enter your name', 'mxchat');
235 264
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 - );
249 -
250 265 ob_start();
251 266
252 267 // Check if floating attribute is set to 'yes' and wrap accordingly
253 268 if ($is_floating) {
@@ -254,12 +269,9 @@
254 269 echo '<div id="floating-chatbot-' . esc_attr($bot_id) . '" class="floating-chatbot ' . $initial_visibility . $additional_class . '">';
255 270 }
256 271
257 272 // 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) . '">';
273 + echo '<div id="mxchat-chatbot-wrapper-' . esc_attr($bot_id) . '" class="mxchat-chatbot-wrapper" data-bot-id="' . esc_attr($bot_id) . '">';
262 274
263 275 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 276 echo ' <div class="chatbot-title-container">';
265 277 echo ' <div class="chatbot-title-group">';
@@ -272,25 +284,14 @@
272 284 if (!empty(trim($ai_agent_text))) {
273 285 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 286 }
275 287 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>';
288 + echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Minimize', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
289 + echo ' <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" id="ic-minimize"' . ($skip_inline_colors ? '' : ' style="fill: ' . esc_attr($close_button_color) . ';"') . '>';
290 + echo ' <path d="M0 0h24v24H0z" fill="none"></path>';
291 + echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>';
291 292 echo ' </svg>';
292 - echo ' <span>' . esc_html__('Close', 'mxchat') . '</span>';
293 + echo ' <span>' . esc_html__('Minimize', 'mxchat') . '</span>';
293 294 echo ' </button>';
294 295 echo ' </div>';
295 296
296 297 // 3b) Main chatbot container
@@ -315,20 +316,8 @@
315 316 }
316 317
317 318 echo ' <label for="user-email-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>';
318 319 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 320 echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">';
332 321 $button_text = isset($current_options['email_blocker_button_text'])
333 322 ? $current_options['email_blocker_button_text']
334 323 : esc_html__('Start Chat', 'mxchat');
@@ -337,29 +326,11 @@
337 326 echo ' </form>';
338 327 echo '</div>';
339 328 }
340 329
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 330 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) . ';"') . '>';
331 + echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box">';
332 + echo ' <div class="bot-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>';
362 333 echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>';
363 334 echo wp_kses_post($intro_message);
364 335 echo ' </div>';
365 336 echo ' </div>';
@@ -405,25 +376,9 @@
405 376 echo ' </div>';
406 377 echo ' </div>';
407 378
408 379 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 - }
380 + echo ' <textarea id="chat-input-' . esc_attr($bot_id) . '" class="chat-input" dir="auto" placeholder="' . esc_attr($input_copy) . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($chat_input_font_color) . ';"') . '></textarea>';
426 381 echo ' <button id="send-button-' . esc_attr($bot_id) . '" class="send-button" aria-label="' . esc_attr__('Send message', 'mxchat') . '">';
427 382 if (!empty($custom_send_image)) {
428 383 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 384 } else {
@@ -519,21 +474,16 @@
519 474
520 475 if ($is_floating) {
521 476 echo '</div>';
522 477
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));
478 + if (!empty($pre_chat_message) && !get_transient($transient_key)) {
479 + echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message">';
480 + echo esc_html($pre_chat_message);
529 481 echo '<button class="close-pre-chat-message" aria-label="' . esc_attr__('Close', 'mxchat') . '">&times;</button>';
530 482 echo '</div>';
531 483 }
532 484
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) . ';"') . '>';
485 + echo '<div class="floating-chatbot-button ' . esc_attr($visibility_class) . '" id="floating-chatbot-button-' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($chatbot_background_color) . '; color: ' . esc_attr($send_button_font_color) . ';"') . '>';
536 486 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 487
538 488 if (!empty($custom_icon)) {
539 489 echo '<img src="' . $custom_icon . '" alt="' . esc_attr__('Chatbot Icon', 'mxchat') . '" style="height: 48px; width: 48px; object-fit: contain;" />';
@@ -592,9 +542,12 @@
592 542 }
593 543
594 544
595 545 private function determine_email_collection_state() {
596 - // Logged-in users skip the email form
546 + // Get session ID
547 + $session_id = 'mxchat_chat_' . wp_generate_uuid4(); // You'll need to use your actual session generation logic
548 +
549 + // Check if user is logged in first
597 550 if (is_user_logged_in()) {
598 551 $current_user = wp_get_current_user();
599 552 return [
600 553 'show_email_form' => false,
@@ -601,16 +554,26 @@
601 554 'user_email' => $current_user->user_email,
602 555 'user_name' => $current_user->display_name ?: $current_user->first_name ?: ''
603 556 ];
604 557 }
605 -
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.
558 +
559 + // Check stored email for session
560 + $stored_email = get_option("mxchat_email_{$session_id}", '');
561 + $stored_name = get_option("mxchat_name_{$session_id}", '');
562 +
563 + // Check if name is required
564 + $name_field_enabled = isset($this->options['enable_name_field']) &&
565 + ($this->options['enable_name_field'] === '1' || $this->options['enable_name_field'] === 'on');
566 +
567 + $has_required_info = !empty($stored_email);
568 + if ($name_field_enabled) {
569 + $has_required_info = $has_required_info && !empty($stored_name);
570 + }
571 +
609 572 return [
610 - 'show_email_form' => true,
611 - 'user_email' => '',
612 - 'user_name' => ''
573 + 'show_email_form' => !$has_required_info,
574 + 'user_email' => $stored_email,
575 + 'user_name' => $stored_name
613 576 ];
614 577 }
615 578
616 579
@@ -617,50 +580,14 @@
617 580 /**
618 581 * NEW: Determine if and which chatbot should be displayed
619 582 */
620 583 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 584 // Get page-specific settings from meta box
659 - $page_setting = self::get_page_bot_setting();
585 + $page_setting = $this->get_page_bot_setting();
660 586
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';
587 + // Get global settings - FIXED: Check for 'on' instead of 'on'
588 + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
589 + $global_default_bot = isset($this->options['default_bot']) ? $this->options['default_bot'] : 'default';
663 590
664 591 // If page specifically hides chatbot, don't show anything
665 592 if ($page_setting && $page_setting['action'] === 'hide') {
666 593 return false;
@@ -674,9 +601,9 @@
674 601 // Page setting is 'global' or no page setting exists
675 602 // Check global auto-show setting
676 603 if ($global_autoshow) {
677 604 // Check post type visibility settings
678 - if (!self::should_show_on_current_post_type()) {
605 + if (!$this->should_show_on_current_post_type()) {
679 606 return false;
680 607 }
681 608
682 609 // Global auto-show is enabled, return the default bot
@@ -688,86 +615,14 @@
688 615 return false;
689 616 }
690 617
691 618 /**
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 619 * Check if chatbot should be shown on the current post type
761 620 */
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 - }
621 +private function should_show_on_current_post_type() {
767 622 // 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();
623 + $mode = isset($this->options['post_type_visibility_mode']) ? $this->options['post_type_visibility_mode'] : 'all';
624 + $list = isset($this->options['post_type_visibility_list']) ? $this->options['post_type_visibility_list'] : array();
770 625
771 626 // Ensure list is an array
772 627 if (!is_array($list)) {
773 628 $list = array();
@@ -778,9 +633,9 @@
778 633 return true;
779 634 }
780 635
781 636 // Get current post type
782 - $current_post_type = self::get_current_post_type();
637 + $current_post_type = $this->get_current_post_type();
783 638
784 639 // If we can't determine post type, default to showing
785 640 if (empty($current_post_type)) {
786 641 return true;
@@ -801,9 +656,9 @@
801 656
802 657 /**
803 658 * Get the current post type
804 659 */
805 -private static function get_current_post_type() {
660 +private function get_current_post_type() {
806 661 // Try to get from queried object first
807 662 $queried_object = get_queried_object();
808 663
809 664 if ($queried_object instanceof WP_Post) {
@@ -833,46 +688,31 @@
833 688 return '';
834 689 }
835 690
836 691 /**
837 - * Get page-specific bot setting using new visibility field with backward compat
692 + * NEW: Get page-specific bot setting
838 693 */
839 -private static function get_page_bot_setting($post_id = null) {
694 +private function get_page_bot_setting($post_id = null) {
840 695 if (!$post_id) {
841 696 $post_id = get_the_ID();
842 697 }
843 -
698 +
844 699 if (!$post_id) {
845 700 return null;
846 701 }
847 -
848 - // Check new visibility field first
849 - $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true);
850 -
851 - if ($visibility === 'hide') {
702 +
703 + // Check if chatbot is hidden on this page
704 + $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true);
705 + if ($hide_chatbot === '1') {
852 706 return array('action' => 'hide');
853 707 }
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)
708 +
709 + // Check if specific bot is selected
870 710 $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
871 711 if (!empty($selected_bot)) {
872 712 return array('action' => 'show', 'bot_id' => $selected_bot);
873 713 }
874 -
714 +
875 715 // Use global setting
876 716 return array('action' => 'global');
877 717 }
878 718
@@ -885,9 +725,9 @@
885 725 return $shortcode_bot_id;
886 726 }
887 727
888 728 // No bot_id in shortcode, check page setting
889 - $page_setting = self::get_page_bot_setting();
729 + $page_setting = $this->get_page_bot_setting();
890 730 if ($page_setting && $page_setting['action'] === 'show') {
891 731 return $page_setting['bot_id'];
892 732 }
893 733
@@ -904,8 +744,27 @@
904 744
905 745 /**
906 746 * UPDATED: Debug function with new context info
907 747 */
748 +public function debug_display_logic() {
749 + if (current_user_can('manage_options') && isset($_GET['mxchat_debug'])) {
750 + $bot_to_show = $this->get_display_bot();
751 + $page_setting = $this->get_page_bot_setting();
752 + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
753 + $hide_auto = $this->should_hide_chatbot('auto');
754 +
755 + echo '<div style="position: fixed; top: 50px; right: 20px; background: white; border: 2px solid red; padding: 10px; z-index: 9999; max-width: 300px; font-size: 12px;">';
756 + echo '<h4 style="margin: 0 0 10px 0;">MxChat Debug Info</h4>';
757 + echo '<p><strong>Raw append_to_body value:</strong> "' . ($this->options['append_to_body'] ?? 'NOT SET') . '"</p>';
758 + echo '<p><strong>Page Setting:</strong><br><pre>' . print_r($page_setting, true) . '</pre></p>';
759 + echo '<p><strong>Global Auto-show:</strong> ' . ($global_autoshow ? 'ON' : 'OFF') . '</p>';
760 + echo '<p><strong>Hide Auto-Append:</strong> ' . ($hide_auto ? 'YES' : 'NO') . '</p>';
761 + echo '<p><strong>Bot to Show:</strong> ' . ($bot_to_show === false ? 'NONE' : $bot_to_show) . '</p>';
762 + echo '<p><strong>Shortcodes:</strong> floating="no" always works</p>';
763 + echo '<p><small>Add ?mxchat_debug=1 to URL to see this</small></p>';
764 + echo '</div>';
765 + }
766 +}
908 767
909 768 /**
910 769 * NEW: Helper method to get available bots (for admin notices, etc.)
911 770 */