PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.0.9
MxChat – AI Chatbot & Content Generation for WordPress v3.0.9
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 | js/chat-script.js +281 -1505 3.2.93.0.9 View file →
@@ -1,166 +1,6 @@
1 1 jQuery(document).ready(function($) {
2 2
3 - // Nonce refresh — v2 (plan-6a68c9).
4 - //
5 - // The widget no longer relies on a nonce embedded in inline cached HTML.
6 - // Before each chat-send / stream-send / upload, we call the REST endpoint
7 - // GET /wp-json/mxchat/v1/nonce and use the freshly-issued value. The
8 - // endpoint creates the nonce with action `mxchat_chat_send`; the server-side
9 - // verifier ALSO still accepts the legacy `mxchat_chat_nonce` action for a
10 - // 30-day backwards-compat window so cached pages still in users' browsers
11 - // (which carry the legacy inline-localized nonce) keep working.
12 - //
13 - // Cache: a single module-scoped slot. TTL 12h conservatively (WP nonces are
14 - // 24h but we refetch at half-life so a freshly-cached-page user never sees
15 - // a borderline-stale nonce).
16 - var cachedFreshNonce = null;
17 - var cachedFreshNonceFetchedAt = 0;
18 - var NONCE_TTL_MS = 12 * 60 * 60 * 1000;
19 - var nonceRefreshState = 'idle'; // 'idle' | 'pending' | 'done'
20 - var nonceRefreshCallbacks = [];
21 -
22 - function getRestNonceUrl() {
23 - if (typeof mxchatChat !== 'undefined' && mxchatChat.rest_url) {
24 - return mxchatChat.rest_url.replace(/\/+$/, '') + '/nonce';
25 - }
26 - // Fallback: derive from current origin if mxchatChat.rest_url isn't set.
27 - return window.location.origin + '/wp-json/mxchat/v1/nonce';
28 - }
29 -
30 - function fetchFreshNonceFromRest() {
31 - return fetch(getRestNonceUrl(), {
32 - credentials: 'same-origin',
33 - headers: { 'Accept': 'application/json' }
34 - }).then(function (resp) {
35 - if (!resp.ok) {
36 - throw new Error('REST nonce fetch failed: ' + resp.status);
37 - }
38 - return resp.json();
39 - }).then(function (data) {
40 - if (data && data.nonce) {
41 - return data.nonce;
42 - }
43 - throw new Error('REST nonce response had no nonce field.');
44 - });
45 - }
46 -
47 - /**
48 - * withFreshNonce(cb) — invoke cb() after ensuring mxchatChat.nonce is fresh.
49 - * Tries REST endpoint first (cache-bypass design); falls back to the legacy
50 - * admin-ajax refresh path if REST is unavailable. Idempotent — concurrent
51 - * calls share the same in-flight refresh.
52 - */
53 - function withFreshNonce(callback) {
54 - if (typeof mxchatChat === 'undefined') {
55 - if (callback) callback();
56 - return;
57 - }
58 - var now = Date.now();
59 - if (cachedFreshNonce && (now - cachedFreshNonceFetchedAt) < NONCE_TTL_MS) {
60 - mxchatChat.nonce = cachedFreshNonce;
61 - if (callback) callback();
62 - return;
63 - }
64 - if (callback) nonceRefreshCallbacks.push(callback);
65 - if (nonceRefreshState === 'pending') return;
66 - nonceRefreshState = 'pending';
67 -
68 - var resolved = function (nonce) {
69 - if (nonce) {
70 - cachedFreshNonce = nonce;
71 - cachedFreshNonceFetchedAt = Date.now();
72 - mxchatChat.nonce = nonce;
73 - }
74 - nonceRefreshState = 'done';
75 - var pending = nonceRefreshCallbacks;
76 - nonceRefreshCallbacks = [];
77 - pending.forEach(function (cb) { try { cb(); } catch (e) {} });
78 - };
79 -
80 - fetchFreshNonceFromRest()
81 - .then(resolved)
82 - .catch(function () {
83 - // Fallback to the legacy admin-ajax refresh path (issued with the
84 - // old action `mxchat_chat_nonce`; the server still accepts both
85 - // during the compat window).
86 - if (mxchatChat.ajax_url) {
87 - $.post(mxchatChat.ajax_url, { action: 'mxchat_refresh_nonce' })
88 - .done(function (res) {
89 - if (res && res.success && res.data && res.data.nonce) {
90 - resolved(res.data.nonce);
91 - return;
92 - }
93 - resolved(null);
94 - })
95 - .fail(function () { resolved(null); });
96 - } else {
97 - resolved(null);
98 - }
99 - });
100 - }
101 -
102 - // Backwards-compat alias — every existing caller in this file (and any
103 - // out-of-tree consumer that hit this internal API) keeps working unchanged.
104 - function refreshNonceIfNeeded(callback) {
105 - return withFreshNonce(callback);
106 - }
107 -
108 - // Dynamic-settings refresh (plan-32db95).
109 - //
110 - // Every widget setting ships inline in cached page HTML, so behind a
111 - // full-page cache the site owner can't purge (host cache, CDN, the
112 - // browser itself) a toggled setting looks broken until the cache turns
113 - // over. Same distrust-cached-HTML reasoning as the per-request nonce:
114 - // on the FIRST widget open per page load we ask the nonce endpoint for
115 - // the current behavior-gate settings (?with_settings=1), merge them over
116 - // mxchatChat, and rebuild the header menu. Colors are NOT refreshed —
117 - // they're server-inline-styled, so a runtime swap would visibly flash.
118 - // On any failure we keep the inline values silently (nonce-fallback
119 - // posture). At most one request per page load, only if a widget opens.
120 - var dynamicSettingsState = 'idle'; // 'idle' | 'pending' | 'done'
121 -
122 - function mxchatRefreshDynamicSettings() {
123 - if (dynamicSettingsState !== 'idle') return;
124 - if (typeof mxchatChat === 'undefined') return;
125 - dynamicSettingsState = 'pending';
126 -
127 - var applied = function (data) {
128 - dynamicSettingsState = 'done';
129 - if (!data) return; // endpoint unavailable — inline values stand.
130 - if (data.nonce) {
131 - // Seed the nonce cache too: saves the first send's REST
132 - // round-trip and keeps us under the endpoint's rate limit.
133 - cachedFreshNonce = data.nonce;
134 - cachedFreshNonceFetchedAt = Date.now();
135 - mxchatChat.nonce = data.nonce;
136 - }
137 - if (data.settings && typeof data.settings === 'object') {
138 - $.extend(mxchatChat, data.settings);
139 - mxchatRebuildHeaderMenus();
140 - }
141 - };
142 -
143 - fetch(getRestNonceUrl() + '?with_settings=1', {
144 - credentials: 'same-origin',
145 - headers: { 'Accept': 'application/json' }
146 - }).then(function (resp) {
147 - if (!resp.ok) throw new Error('settings refresh failed: ' + resp.status);
148 - return resp.json();
149 - }).then(applied).catch(function () {
150 - // Fallback: legacy admin-ajax refresh path, same as withFreshNonce.
151 - if (mxchatChat.ajax_url) {
152 - $.post(mxchatChat.ajax_url, { action: 'mxchat_refresh_nonce', with_settings: 1 })
153 - .done(function (res) {
154 - applied(res && res.success && res.data ? res.data : null);
155 - })
156 - .fail(function () { applied(null); });
157 - } else {
158 - applied(null);
159 - }
160 - });
161 - }
162 -
163 3 // ====================================
164 4 // MULTI-INSTANCE MANAGEMENT SYSTEM
165 5 // ====================================
166 6
@@ -176,9 +16,9 @@
176 16 var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
177 17
178 18 this.instances[botId] = {
179 19 botId: botId,
180 - sessionId: null,
20 + sessionId: this.getChatSession(botId),
181 21 lastSeenMessageId: '',
182 22 notificationCheckInterval: null,
183 23 pollingInterval: null,
184 24 processedMessageIds: new Set(),
@@ -203,77 +43,23 @@
203 43 return Object.keys(this.instances);
204 44 },
205 45
206 46 // Session management per bot
207 - // Returns existing session ID from cookie or localStorage (with in-memory fallback),
208 - // or null if none exists. Does NOT create a new session — use ensureSession() for that.
209 47 getChatSession: function(botId) {
210 48 var cookieName = 'mxchat_session_id_' + botId;
211 - var storageKey = 'mxchat_session_id_' + botId;
212 49 var sessionId = getCookie(cookieName);
213 50
214 - // Fallback to localStorage if cookie is missing (e.g. cleared by browser/consent)
215 51 if (!sessionId) {
216 - try { sessionId = localStorage.getItem(storageKey); } catch (e) {}
52 + sessionId = generateSessionId();
53 + this.setChatSession(botId, sessionId);
217 54 }
218 55
219 - // Fallback to in-memory instance when cookie AND localStorage are both blocked
220 - // (Safari ITP, strict tracking prevention, cross-origin iframes with partitioned
221 - // storage). Without this, ensureSession() can generate and store an ID that
222 - // getChatSession() then can't read back, causing null session_ids on send.
223 - if (!sessionId && this.instances[botId] && this.instances[botId].sessionId) {
224 - sessionId = this.instances[botId].sessionId;
225 - }
226 -
227 - // Guard against stored sentinel values that indicate earlier broken writes.
228 - if (sessionId === 'null' || sessionId === 'undefined') {
229 - sessionId = null;
230 - }
231 -
232 - // Re-sync cookie from localStorage if cookie was lost
233 - if (sessionId && !getCookie(cookieName)) {
234 - document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
235 - }
236 -
237 - return sessionId || null;
56 + return sessionId;
238 57 },
239 58
240 - // Lazy session initializer — called on first user interaction
241 - ensureSession: function(botId) {
242 - botId = botId || 'default';
243 - var instance = this.instances[botId] || this.init(botId);
244 -
245 - if (instance.sessionId) {
246 - return instance.sessionId;
247 - }
248 -
249 - // Check for existing session from cookie or localStorage
250 - var existingSession = this.getChatSession(botId);
251 -
252 - if (existingSession) {
253 - instance.sessionId = existingSession;
254 - } else {
255 - // Brand new session
256 - var newId = generateSessionId();
257 - this.setChatSession(botId, newId);
258 - instance.sessionId = newId;
259 - }
260 -
261 - // Now that we have a session, do the deferred work
262 - refreshNonceIfNeeded();
263 - trackOriginatingPage();
264 -
265 - // Note: loadChatHistory is handled by showChatContainerForBot with loader UI,
266 - // so we do NOT call it here to avoid a race condition.
267 -
268 - return instance.sessionId;
269 - },
270 -
271 59 setChatSession: function(botId, sessionId) {
272 60 var cookieName = 'mxchat_session_id_' + botId;
273 - var storageKey = 'mxchat_session_id_' + botId;
274 61 document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
275 - try { localStorage.setItem(storageKey, sessionId); } catch (e) {}
276 62 if (this.instances[botId]) {
277 63 this.instances[botId].sessionId = sessionId;
278 64 }
279 65 },
@@ -278,10 +64,8 @@
278 64 }
279 65 },
280 66
281 67 resetChatSession: function(botId) {
282 - // Clear old session from localStorage before setting new one
283 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
284 68 var newSessionId = generateSessionId();
285 69 this.setChatSession(botId, newSessionId);
286 70 var $chatBox = getElement(botId, 'chat-box');
287 71 if ($chatBox.length) {
@@ -290,20 +74,8 @@
290 74 if (this.instances[botId]) {
291 75 this.instances[botId].chatHistoryLoaded = false;
292 76 this.instances[botId].processedMessageIds = new Set();
293 77 }
294 - },
295 -
296 - // Silent reset — new session ID without clearing the chat UI
297 - // Used when IP changes mid-conversation so the user doesn't see messages vanish
298 - silentResetSession: function(botId) {
299 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
300 - var newSessionId = generateSessionId();
301 - this.setChatSession(botId, newSessionId);
302 - if (this.instances[botId]) {
303 - this.instances[botId].sessionId = newSessionId;
304 - }
305 - return newSessionId;
306 78 }
307 79 };
308 80
309 81 // ====================================
@@ -603,106 +375,13 @@
603 375 sendButton.disabled = false;
604 376 sendButton.style.opacity = '1';
605 377 sendButton.style.pointerEvents = 'auto';
606 378 }
607 - // Every completion path re-enables input, so this is the single restore
608 - // point for the streaming Stop affordance (no-op when not in stop mode).
609 - mxchatRestoreSendButton(botId);
610 379 }
611 380
612 -// --- Streaming Stop control -------------------------------------------------
613 -// One live stream handle per bot instance, so Stop on one widget never aborts
614 -// another bot on the same page.
615 -var mxchatActiveStreams = {};
616 -// Original send-button markup, captured once per bot the first time the Stop
617 -// state is shown (never captured while already in stop mode, so a rapid
618 -// stop-then-resend can't save the stop glyph as the "original").
619 -var mxchatSendMarkup = {};
620 -
621 -function mxchatShowStopButton(botId) {
622 - var btn = getElementDOM(botId, 'send-button');
623 - if (!btn) return;
624 - if (!btn.classList.contains('mxchat-stop-mode')) {
625 - mxchatSendMarkup[botId] = {
626 - html: btn.innerHTML,
627 - label: btn.getAttribute('aria-label')
628 - };
629 - }
630 -
631 - // Mirror the send icon's rendered size + color so the stop glyph looks
632 - // native, including custom send images/colors and theme overrides.
633 - var child = btn.querySelector('svg, img');
634 - var size = 25;
635 - var color = '';
636 - if (child) {
637 - var rect = child.getBoundingClientRect();
638 - if (rect.width) {
639 - size = Math.round(Math.min(rect.width, rect.height));
640 - }
641 - var cs = window.getComputedStyle(child);
642 - color = (child.tagName.toLowerCase() === 'svg' ? cs.fill : cs.color) || '';
643 - }
644 - var stopLabel = (typeof mxchatChat !== 'undefined' && mxchatChat.stop_button_label) || 'Stop response';
645 - btn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" style="width:' + size + 'px;height:' + size + 'px;' + (color ? 'fill:' + color + ';' : '') + '"><rect x="5" y="5" width="14" height="14" rx="3"></rect></svg>';
646 - // An add-on's DIRECT send-button handler (e.g. mxchat-vision's rebind) can
647 - // start a stream synchronously while the originating click is still
648 - // bubbling up to our delegated handler. Without this guard, that handler
649 - // reads the just-added stop-mode class as a user Stop press and aborts the
650 - // brand-new stream — the user's message renders but no reply ever fires
651 - // (plan-4bba64 silent message loss). The flag only spans the current event
652 - // dispatch: cleared on the next macrotask, long before a real Stop click.
653 - btn.__mxchatStopJustShown = true;
654 - setTimeout(function () { btn.__mxchatStopJustShown = false; }, 0);
655 - btn.classList.add('mxchat-stop-mode');
656 - btn.setAttribute('aria-label', stopLabel);
657 - btn.setAttribute('title', stopLabel);
658 - // disableChatInput() ran when the turn was sent; the Stop control itself
659 - // must stay clickable while the textarea remains disabled.
660 - btn.disabled = false;
661 - btn.style.opacity = '1';
662 - btn.style.pointerEvents = 'auto';
663 -}
664 -
665 -function mxchatRestoreSendButton(botId) {
666 - var btn = getElementDOM(botId, 'send-button');
667 - var saved = mxchatSendMarkup[botId];
668 - if (!btn || !btn.classList.contains('mxchat-stop-mode') || !saved) return;
669 - btn.innerHTML = saved.html;
670 - btn.classList.remove('mxchat-stop-mode');
671 - btn.removeAttribute('title');
672 - if (saved.label) {
673 - btn.setAttribute('aria-label', saved.label);
674 - }
675 -}
676 -
677 -function mxchatStopStreaming(botId) {
678 - var entry = mxchatActiveStreams[botId];
679 - if (!entry || !entry.controller) return;
680 - entry.aborted = true;
681 - try { entry.controller.abort(); } catch (e) {}
682 -}
683 -
684 -// Returns true when a stream rejection came from an intentional Stop click:
685 -// keep the partial text as the turn's answer — no error UI, no fallback resend.
686 -function mxchatHandleStreamAbort(botId, accumulatedContent, callback) {
687 - var entry = mxchatActiveStreams[botId];
688 - if (!entry || !entry.aborted) return false;
689 - delete mxchatActiveStreams[botId];
690 - if (!accumulatedContent) {
691 - // Stopped before the first chunk: drop the thinking bubble, no orphan message.
692 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
693 - }
694 - enableChatInput(botId); // also restores the send icon
695 - if (callback) {
696 - callback(accumulatedContent || '');
697 - }
698 - return true;
699 -}
700 -
701 381 // Update your existing sendMessage function
702 382 function sendMessage(botId) {
703 383 botId = botId || 'default';
704 - MxChatInstances.ensureSession(botId);
705 384 var $chatInput = getElement(botId, 'chat-input');
706 385 var message = $chatInput.val();
707 386
708 387 // ADD PROMPT HOOK HERE
@@ -710,14 +389,10 @@
710 389 message = customMxChatFilter(message, "prompt");
711 390 }
712 391
713 392 if (message) {
714 - // Don't disable input in live agent mode - let users chat freely
715 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
716 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
717 - if (!isAgentMode) {
718 - disableChatInput(botId);
719 - }
393 + // Disable input while waiting for response
394 + disableChatInput(botId);
720 395
721 396 appendMessage("user", message, '', [], false, botId);
722 397 $chatInput.val('');
723 398 $chatInput.css('height', 'auto');
@@ -745,9 +420,8 @@
745 420
746 421 // Update your existing sendMessageToChatbot function
747 422 function sendMessageToChatbot(message, botId) {
748 423 botId = botId || 'default';
749 - MxChatInstances.ensureSession(botId);
750 424
751 425 // ADD PROMPT HOOK HERE
752 426 if (typeof customMxChatFilter === 'function') {
753 427 message = customMxChatFilter(message, "prompt");
@@ -752,14 +426,10 @@
752 426 if (typeof customMxChatFilter === 'function') {
753 427 message = customMxChatFilter(message, "prompt");
754 428 }
755 429
756 - // Don't disable input in live agent mode - let users chat freely
757 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
758 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
759 - if (!isAgentMode) {
760 - disableChatInput(botId);
761 - }
430 + // Disable input while waiting for response
431 + disableChatInput(botId);
762 432
763 433 var sessionId = getChatSession(botId);
764 434
765 435 if (hasQuickQuestions(botId)) {
@@ -838,15 +508,8 @@
838 508
839 509 function callMxChat(message, callback, botId) {
840 510 botId = botId || getMxChatBotId();
841 511
842 - // Streaming fallbacks land here: drop any leftover stream handle and
843 - // return the button to its send state (no-op for plain non-stream turns).
844 - if (mxchatActiveStreams[botId]) {
845 - delete mxchatActiveStreams[botId];
846 - }
847 - mxchatRestoreSendButton(botId);
848 -
849 512 // Store the message in case we need to retry after session reset
850 513 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', message);
851 514
852 515 // Get page context if contextual awareness is enabled
@@ -854,28 +517,13 @@
854 517
855 518 // Get instance for session start timestamp (used when persistence is OFF)
856 519 var instance = MxChatInstances.get(botId);
857 520
858 - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent
859 - // and returns the guaranteed-present session id from the in-memory instance even when
860 - // cookie/localStorage writes are silently blocked by the browser.
861 - var sessionId = MxChatInstances.ensureSession(botId);
862 - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') {
863 - // Last-resort generation to ensure we never POST a null marker.
864 - sessionId = generateSessionId();
865 - MxChatInstances.setChatSession(botId, sessionId);
866 - }
867 -
868 - // Wait for the page-cache nonce refresh to complete before firing the
869 - // chat-send AJAX. On cached pages the inline mxchatChat.nonce is stale
870 - // until refreshNonceIfNeeded() returns; constructing ajaxData inside the
871 - // callback guarantees we read the fresh value. See plan-c5457f.
872 - refreshNonceIfNeeded(function() {
873 521 // Prepare AJAX data
874 522 const ajaxData = {
875 523 action: 'mxchat_handle_chat_request',
876 524 message: message,
877 - session_id: sessionId,
525 + session_id: getChatSession(botId),
878 526 nonce: mxchatChat.nonce,
879 527 current_page_url: window.location.href,
880 528 current_page_title: document.title,
881 529 bot_id: botId,
@@ -881,14 +529,14 @@
881 529 bot_id: botId,
882 530 // Pass session start timestamp so AI context matches what user sees
883 531 session_start_timestamp: instance.sessionStartTimestamp || 0
884 532 };
885 -
533 +
886 534 // Add page context if available
887 535 if (pageContext) {
888 536 ajaxData.page_context = JSON.stringify(pageContext);
889 537 }
890 -
538 +
891 539 // CHECK FOR VISION FLAGS AND ADD THEM
892 540 if (window.mxchatVisionProcessed) {
893 541 ajaxData.vision_processed = true;
894 542 ajaxData.original_user_message = window.mxchatOriginalMessage || message;
@@ -897,9 +545,9 @@
897 545 window.mxchatVisionProcessed = false;
898 546 window.mxchatOriginalMessage = null;
899 547 window.mxchatVisionImagesCount = 0;
900 548 }
901 -
549 +
902 550 $.ajax({
903 551 url: mxchatChat.ajax_url,
904 552 type: 'POST',
905 553 dataType: 'json',
@@ -937,16 +585,23 @@
937 585 errorMessage = "An error occurred. Please try again or contact support.";
938 586 }
939 587
940 588 // Handle session reset action (IP changed, session expired, etc.)
941 - // Silent reset — keep chat UI intact, just get a new session and retry
942 589 if (response.data && response.data.action === 'reset_session') {
943 - MxChatInstances.silentResetSession(botId);
944 - // Re-send the original message with the new session (user message is already displayed)
590 + // Clear the old session and generate a new one
591 + resetChatSession(botId);
592 + // Remove the temporary loading message
593 + getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
594 + // Re-send the original message with the new session
945 595 var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
946 596 if (originalMessage) {
947 597 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
948 - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
598 + // Re-add the user message and thinking indicator
599 + appendMessage("user", originalMessage, '', [], false, botId);
600 + appendThinkingMessage(botId);
601 + scrollToBottom(botId);
602 + // Determine whether to use streaming
603 + const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
949 604 if (shouldUseStreaming(currentModel)) {
950 605 callMxChatStream(originalMessage, function(response) {
951 606 getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message');
952 607 }, botId);
@@ -1003,11 +658,9 @@
1003 658 }
1004 659
1005 660 // Check for live agent response
1006 661 if (response.success && response.data && response.data.status === 'waiting_for_agent') {
1007 - removeThinkingDots(botId);
1008 662 updateChatModeIndicator('agent', botId);
1009 - enableChatInput(botId);
1010 663 return;
1011 664 }
1012 665
1013 666 // Handle the message and show notification if chat is hidden
@@ -1040,13 +693,9 @@
1040 693 $badge.show();
1041 694 }
1042 695 }
1043 696 } else {
1044 - var emptyMsg = "I received an empty response. Please try again or contact support if this persists.";
1045 - if (response.vectorstore_error) {
1046 - emptyMsg = "I received an empty response. Debug info: " + response.vectorstore_error;
1047 - }
1048 - replaceLastMessage("bot", emptyMsg, '', [], botId);
697 + replaceLastMessage("bot", "I received an empty response. Please try again or contact support if this persists.", '', [], botId);
1049 698 }
1050 699
1051 700 if (response.message_id) {
1052 701 var instance = MxChatInstances.get(botId);
@@ -1088,9 +737,8 @@
1088 737
1089 738 replaceLastMessage("bot", errorMessage, '', [], botId);
1090 739 }
1091 740 });
1092 - }); // refreshNonceIfNeeded
1093 741 }
1094 742
1095 743 function callMxChatStream(message, callback, botId) {
1096 744 botId = botId || getMxChatBotId();
@@ -1109,25 +757,12 @@
1109 757
1110 758 // Get instance for session start timestamp (used when persistence is OFF)
1111 759 var instance = MxChatInstances.get(botId);
1112 760
1113 - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any
1114 - // non-string value via String(), so passing `null` would POST the literal string "null"
1115 - // and land in the transcripts table as a ghost session. ensureSession() always returns
1116 - // a real string even when cookies/localStorage are blocked.
1117 - var streamSessionId = MxChatInstances.ensureSession(botId);
1118 - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') {
1119 - streamSessionId = generateSessionId();
1120 - MxChatInstances.setChatSession(botId, streamSessionId);
1121 - }
1122 -
1123 - // Wait for the page-cache nonce refresh before constructing formData (which
1124 - // captures mxchatChat.nonce by value). Mirrors callMxChat's wrapping. See plan-c5457f.
1125 - refreshNonceIfNeeded(function() {
1126 761 const formData = new FormData();
1127 762 formData.append('action', 'mxchat_stream_chat');
1128 763 formData.append('message', message);
1129 - formData.append('session_id', streamSessionId);
764 + formData.append('session_id', getChatSession(botId));
1130 765 formData.append('nonce', mxchatChat.nonce);
1131 766 formData.append('current_page_url', window.location.href);
1132 767 formData.append('current_page_title', document.title);
1133 768 formData.append('bot_id', botId);
@@ -1153,20 +788,12 @@
1153 788 let accumulatedContent = '';
1154 789 let testingDataReceived = false;
1155 790 let streamingStarted = false;
1156 791
1157 - // Abortable stream: a fresh controller per turn, keyed by bot instance.
1158 - // The Stop control (send button swapped in place) aborts both the read
1159 - // loop and the underlying request.
1160 - var streamControl = { controller: new AbortController(), aborted: false };
1161 - mxchatActiveStreams[botId] = streamControl;
1162 - mxchatShowStopButton(botId);
1163 -
1164 792 fetch(mxchatChat.ajax_url, {
1165 793 method: 'POST',
1166 794 body: formData,
1167 - credentials: 'same-origin',
1168 - signal: streamControl.controller.signal
795 + credentials: 'same-origin'
1169 796 })
1170 797 .then(response => {
1171 798 // Store the response for potential fallback handling
1172 799 const responseClone = response.clone();
@@ -1235,16 +862,8 @@
1235 862
1236 863 // Re-enable chat input when stream ends with content
1237 864 enableChatInput(botId);
1238 865
1239 - // Scroll the user's last message to the top now that the
1240 - // bot's full reply has rendered (gives max reading room).
1241 - var $chatBoxDone = getElement(botId, 'chat-box');
1242 - var $lastUserMsgDone = $chatBoxDone.find('.user-message').last();
1243 - if ($lastUserMsgDone.length) {
1244 - scrollElementToTop($lastUserMsgDone, botId);
1245 - }
1246 -
1247 866 if (callback) {
1248 867 callback(accumulatedContent);
1249 868 }
1250 869 return;
@@ -1267,16 +886,8 @@
1267 886
1268 887 // Re-enable chat input after streaming completes
1269 888 enableChatInput(botId);
1270 889
1271 - // Scroll the user's last message to the top now
1272 - // that the bot's full reply has rendered.
1273 - var $chatBoxStreamDone = getElement(botId, 'chat-box');
1274 - var $lastUserMsgStreamDone = $chatBoxStreamDone.find('.user-message').last();
1275 - if ($lastUserMsgStreamDone.length) {
1276 - scrollElementToTop($lastUserMsgStreamDone, botId);
1277 - }
1278 -
1279 890 if (callback) {
1280 891 callback(accumulatedContent);
1281 892 }
1282 893 return;
@@ -1333,9 +944,8 @@
1333 944 }
1334 945
1335 946 processStream();
1336 947 }).catch(streamError => {
1337 - if (mxchatHandleStreamAbort(botId, accumulatedContent, callback)) return;
1338 948 getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1339 949 callMxChat(message, callback, botId);
1340 950 });
1341 951 }
@@ -1342,9 +952,8 @@
1342 952
1343 953 processStream();
1344 954 })
1345 955 .catch(error => {
1346 - if (mxchatHandleStreamAbort(botId, accumulatedContent, callback)) return;
1347 956 // Check if we have server error data with chat mode
1348 957 if (error && error.isServerError && error.data) {
1349 958 // Check for chat mode in error data
1350 959 if (error.data.chat_mode) {
@@ -1357,9 +966,8 @@
1357 966 getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1358 967 callMxChat(message, callback, botId);
1359 968 }
1360 969 });
1361 - }); // refreshNonceIfNeeded
1362 970 }
1363 971
1364 972 // Helper function to handle non-streaming responses
1365 973 function handleNonStreamResponse(data, callback, botId) {
@@ -1398,16 +1006,21 @@
1398 1006 errorMessage = "An error occurred. Please try again or contact support.";
1399 1007 }
1400 1008
1401 1009 // Handle session reset action (IP changed, session expired, etc.)
1402 - // Silent reset — keep chat UI intact, just get a new session and retry
1403 1010 if (data.data && data.data.action === 'reset_session') {
1404 - MxChatInstances.silentResetSession(botId);
1405 - // Re-send the original message with the new session (user message is already displayed)
1011 + // Clear the old session and generate a new one
1012 + resetChatSession(botId);
1013 + // Re-send the original message with the new session
1406 1014 var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
1407 1015 if (originalMessage) {
1408 1016 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
1409 - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
1017 + // Re-add the user message and thinking indicator
1018 + appendMessage("user", originalMessage, '', [], false, botId);
1019 + appendThinkingMessage(botId);
1020 + scrollToBottom(botId);
1021 + // Determine whether to use streaming
1022 + const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
1410 1023 if (shouldUseStreaming(currentModel)) {
1411 1024 callMxChatStream(originalMessage, callback, botId);
1412 1025 } else {
1413 1026 callMxChat(originalMessage, callback, botId);
@@ -1429,22 +1042,8 @@
1429 1042 }
1430 1043 return; // Exit early for errors
1431 1044 }
1432 1045
1433 - // Check for live agent response
1434 - if (data.success && data.data && data.data.status === 'waiting_for_agent') {
1435 - removeThinkingDots(botId);
1436 - // Also remove any leftover bot-message that lost its temporary-message class
1437 - var $chatBox = getElement(botId, 'chat-box');
1438 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
1439 - updateChatModeIndicator('agent', botId);
1440 - enableChatInput(botId);
1441 - if (callback) {
1442 - callback('');
1443 - }
1444 - return;
1445 - }
1446 -
1447 1046 // Handle different response formats
1448 1047 if (data.text || data.html || data.message) {
1449 1048
1450 1049 // Apply response hooks
@@ -1489,15 +1088,19 @@
1489 1088 }
1490 1089
1491 1090 // Enhanced updateChatModeIndicator function for immediate DOM updates
1492 1091 function updateChatModeIndicator(mode, botId) {
1092 + console.log('[MxChat] updateChatModeIndicator called with mode:', mode, 'botId:', botId);
1493 1093 botId = botId || 'default';
1494 1094 const indicator = getElementDOM(botId, 'chat-mode-indicator');
1095 + console.log('[MxChat] chat-mode-indicator element found:', !!indicator);
1495 1096 if (indicator) {
1496 1097 const oldText = indicator.textContent;
1098 + console.log('[MxChat] Current indicator text:', oldText, '-> changing to mode:', mode);
1497 1099
1498 1100 if (mode === 'agent') {
1499 1101 indicator.textContent = 'Live Agent';
1102 + console.log('[MxChat] Mode is agent, calling startPolling...');
1500 1103 startPolling(botId);
1501 1104 } else {
1502 1105 // Everything else is AI mode
1503 1106 const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
@@ -1568,21 +1171,9 @@
1568 1171 // Update the event handlers to use the correct function names (using event delegation)
1569 1172 // Use class-based selectors for multi-instance support
1570 1173 $(document).on('click', '.send-button', function() {
1571 1174 var botId = getBotIdFromElement(this);
1572 - // While a response is streaming the button is a Stop control.
1573 - if (this.classList.contains('mxchat-stop-mode')) {
1574 - // Same click that just started this stream (an add-on's direct handler
1575 - // ran before this delegated one) — not a Stop press. See
1576 - // mxchatShowStopButton for the full story (plan-4bba64).
1577 - if (this.__mxchatStopJustShown) return;
1578 - mxchatStopStreaming(botId);
1579 - return;
1580 - }
1581 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1582 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1583 - disableChatInput(botId);
1584 - }
1175 + disableChatInput(botId);
1585 1176 sendMessage(botId);
1586 1177 });
1587 1178
1588 1179 // Override enter key handler (using event delegation)
@@ -1589,295 +1180,14 @@
1589 1180 $(document).on('keypress', '.chat-input', function(e) {
1590 1181 if (e.which == 13 && !e.shiftKey) {
1591 1182 e.preventDefault();
1592 1183 var botId = getBotIdFromElement(this);
1593 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1594 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1595 - disableChatInput(botId);
1596 - }
1184 + disableChatInput(botId);
1597 1185 sendMessage(botId);
1598 1186 }
1599 1187 });
1600 1188
1601 -// Builds the list of overflow-menu items for a given bot.
1602 -// Adding a future item is one push to this array — do NOT hardcode "only download."
1603 -function mxchatGetHeaderMenuItems(botId) {
1604 - var items = [];
1605 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1606 -
1607 - // The `print_button_*` keys still gate this item for back-compat with
1608 - // existing user options. The action is now a transcript download, not print.
1609 - if (settings.print_button_enabled === 'on') {
1610 - items.push({
1611 - id: 'download-transcript',
1612 - label: settings.print_button_label || 'Download Transcript',
1613 - icon: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>',
1614 - action: function() {
1615 - mxchatDownloadTranscript(botId);
1616 - }
1617 - });
1618 - }
1619 -
1620 - return items;
1621 -}
1622 -
1623 -// Builds a clean markdown transcript of the current conversation and triggers
1624 -// a file download. Used by the "Download Transcript" menu item.
1625 -function mxchatDownloadTranscript(botId) {
1626 - var $chatBox = getElement(botId, 'chat-box');
1627 - if (!$chatBox || !$chatBox.length) return;
1628 -
1629 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1630 - var headerTitle = settings.print_header_title || 'Chat transcript';
1631 - var now = new Date();
1632 - var stamp = now.toLocaleString();
1633 -
1634 - var lines = [];
1635 - lines.push('# ' + headerTitle);
1636 - lines.push('');
1637 - lines.push('Exported: ' + stamp);
1638 - lines.push('');
1639 - lines.push('---');
1640 - lines.push('');
1641 -
1642 - $chatBox.find('.user-message, .bot-message, .agent-message').each(function() {
1643 - var $msg = $(this);
1644 - // Skip thinking placeholders and any in-flight temporary messages.
1645 - if ($msg.find('.thinking-dots').length) return;
1646 - if ($msg.hasClass('temporary-message')) return;
1647 -
1648 - var sender;
1649 - if ($msg.hasClass('user-message')) sender = 'User';
1650 - else if ($msg.hasClass('agent-message')) sender = 'Live Agent';
1651 - else sender = 'AI Agent';
1652 -
1653 - // Strip interactive UI from the cloned message so we get the conversation text.
1654 - var $clone = $msg.clone();
1655 - $clone.find('.copy-button, .message-toolbar, .mxchat-copy, button, script, style').remove();
1656 - var text = $clone.text().replace(/ /g, ' ').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim();
1657 - if (!text) return;
1658 -
1659 - lines.push('**' + sender + '**');
1660 - lines.push('');
1661 - lines.push(text);
1662 - lines.push('');
1663 - });
1664 -
1665 - var content = lines.join('\n');
1666 - var iso = now.toISOString().replace(/[:.]/g, '-').slice(0, 19);
1667 - var fname = 'mxchat-transcript-' + iso + '.md';
1668 - var blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
1669 - var url = URL.createObjectURL(blob);
1670 - var a = document.createElement('a');
1671 - a.href = url;
1672 - a.download = fname;
1673 - a.style.display = 'none';
1674 - document.body.appendChild(a);
1675 - a.click();
1676 - setTimeout(function() {
1677 - if (a.parentNode) a.parentNode.removeChild(a);
1678 - URL.revokeObjectURL(url);
1679 - }, 100);
1680 -}
1681 -
1682 -// Reads the bot bubble's actual computed bg+fg and writes them as CSS vars
1683 -// on the menu wrap, so the dropdown matches whatever paints the bubble —
1684 -// saved options, AI theme CSS, or the mxchat-theme add-on.
1685 -function mxchatSyncMenuColors(botId, $wrap) {
1686 - if (!$wrap || !$wrap.length) return;
1687 - var $bot = $wrap.closest('.mxchat-chatbot-wrapper').find('.bot-message').not('.temporary-message').first();
1688 - if (!$bot.length) return;
1689 - var cs = window.getComputedStyle($bot[0]);
1690 - if (cs.backgroundColor && cs.backgroundColor !== 'rgba(0, 0, 0, 0)' && cs.backgroundColor !== 'transparent') {
1691 - $wrap[0].style.setProperty('--mxchat-menu-bg', cs.backgroundColor);
1692 - }
1693 - // Bot text color usually lives on a child div, not .bot-message itself.
1694 - var $textChild = $bot.find('[style*="color"]').first();
1695 - var fg = ($textChild.length ? window.getComputedStyle($textChild[0]).color : cs.color);
1696 - if (fg) $wrap[0].style.setProperty('--mxchat-menu-fg', fg);
1697 -}
1698 -
1699 -// Renders (or re-renders) the item list for one menu wrap. Split out of
1700 -// mxchatInitHeaderMenu so the dynamic-settings merge (plan-32db95) can
1701 -// rebuild items + trigger visibility WITHOUT re-binding the one-time
1702 -// open/close/keyboard wiring. closeMenu is passed in by the init closure;
1703 -// a rebuild before init (never happens, but harmless) just skips it.
1704 -function mxchatRenderHeaderMenuItems(botId, $wrap, closeMenuFn) {
1705 - var $trigger = $wrap.find('.mxchat-menu-trigger');
1706 - var $menu = $wrap.find('.mxchat-header-menu');
1707 - var items = mxchatGetHeaderMenuItems(botId);
1708 -
1709 - $menu.empty();
1710 -
1711 - if (!items.length) {
1712 - $trigger.hide();
1713 - $menu.hide();
1714 - return;
1715 - }
1716 -
1717 - // Clear any inline display:none a previous zero-item render left behind —
1718 - // open/close visibility is governed by the hidden prop + is-open class.
1719 - $trigger.css('display', '');
1720 - $menu.css('display', '');
1721 -
1722 - items.forEach(function(item, idx) {
1723 - var $btn = $('<button>', {
1724 - type: 'button',
1725 - 'class': 'mxchat-menu-item',
1726 - 'role': 'menuitem',
1727 - 'tabindex': '-1',
1728 - 'data-menu-id': item.id,
1729 - html: '<span class="mxchat-menu-item-icon">' + item.icon + '</span>' +
1730 - '<span class="mxchat-menu-item-label"></span>'
1731 - });
1732 - $btn.find('.mxchat-menu-item-label').text(item.label);
1733 - $btn.on('click', function(e) {
1734 - e.preventDefault();
1735 - e.stopPropagation();
1736 - if (closeMenuFn) closeMenuFn();
1737 - try { item.action(); } catch (err) { /* no-op */ }
1738 - });
1739 - $menu.append($btn);
1740 - });
1741 -}
1742 -
1743 -// Re-render every menu on the page after a dynamic-settings merge
1744 -// (multi-bot: each wrap re-reads its items). An OPEN menu is left alone —
1745 -// swapping items under the user mid-interaction yanks focus — and the
1746 -// rebuild runs when it closes instead (closeMenu checks the pending flag).
1747 -function mxchatRebuildHeaderMenus() {
1748 - $('.mxchat-header-menu-wrap').each(function() {
1749 - var $wrap = $(this);
1750 - var botId = $wrap.data('bot-id');
1751 - if (!botId) return;
1752 - if (!$wrap.data('mxchatMenuReady')) {
1753 - mxchatInitHeaderMenu(botId);
1754 - return;
1755 - }
1756 - if ($wrap.find('.mxchat-header-menu').hasClass('is-open')) {
1757 - $wrap.data('mxchatMenuRebuildPending', true);
1758 - return;
1759 - }
1760 - mxchatRenderHeaderMenuItems(botId, $wrap, $wrap.data('mxchatMenuClose'));
1761 - });
1762 -}
1763 -
1764 -// One-time per-widget init: renders menu items, wires open/close,
1765 -// outside-click, Escape, and arrow-key navigation. If no items, hides the
1766 -// trigger. Wiring happens even when there are zero items at init, so a
1767 -// later dynamic-settings rebuild that adds items has a working trigger.
1768 -function mxchatInitHeaderMenu(botId) {
1769 - var $wrap = $('.mxchat-header-menu-wrap[data-bot-id="' + botId + '"]').first();
1770 - if (!$wrap.length || $wrap.data('mxchatMenuReady')) return;
1771 -
1772 - var $trigger = $wrap.find('.mxchat-menu-trigger');
1773 - var $menu = $wrap.find('.mxchat-header-menu');
1774 -
1775 - // Initial color sync — covers normal page load.
1776 - mxchatSyncMenuColors(botId, $wrap);
1777 -
1778 - function openMenu() {
1779 - // Re-sync each open in case the active theme changed since init.
1780 - mxchatSyncMenuColors(botId, $wrap);
1781 - $menu.prop('hidden', false).attr('aria-hidden', 'false').addClass('is-open');
1782 - $trigger.attr('aria-expanded', 'true');
1783 - // Focus the first item for keyboard users
1784 - setTimeout(function() {
1785 - $menu.find('.mxchat-menu-item').first().attr('tabindex', '0').trigger('focus');
1786 - }, 0);
1787 - }
1788 - function closeMenu(returnFocus) {
1789 - $menu.prop('hidden', true).attr('aria-hidden', 'true').removeClass('is-open');
1790 - $trigger.attr('aria-expanded', 'false');
1791 - $menu.find('.mxchat-menu-item').attr('tabindex', '-1');
1792 - if (returnFocus) $trigger.trigger('focus');
1793 - // A dynamic-settings rebuild that arrived while the menu was open
1794 - // was deferred (mxchatRebuildHeaderMenus) — run it now.
1795 - if ($wrap.data('mxchatMenuRebuildPending')) {
1796 - $wrap.removeData('mxchatMenuRebuildPending');
1797 - mxchatRenderHeaderMenuItems(botId, $wrap, closeMenu);
1798 - }
1799 - }
1800 -
1801 - // Toggle on trigger click — stop propagation so the .chatbot-top-bar
1802 - // click-to-collapse handler does not fire.
1803 - $trigger.on('click', function(e) {
1804 - e.preventDefault();
1805 - e.stopPropagation();
1806 - if ($menu.hasClass('is-open')) closeMenu();
1807 - else openMenu();
1808 - });
1809 -
1810 - // Don't let clicks inside the menu bubble to the top-bar collapse handler.
1811 - $menu.on('click', function(e) {
1812 - e.stopPropagation();
1813 - });
1814 -
1815 - // Outside click closes the menu.
1816 - $(document).on('click.mxchatMenu-' + botId, function(e) {
1817 - if (!$menu.hasClass('is-open')) return;
1818 - if ($wrap.has(e.target).length || $wrap.is(e.target)) return;
1819 - closeMenu();
1820 - });
1821 -
1822 - // Keyboard: Escape closes and returns focus; arrow keys move focus; Enter activates.
1823 - $menu.on('keydown', '.mxchat-menu-item', function(e) {
1824 - var $items = $menu.find('.mxchat-menu-item');
1825 - var idx = $items.index(this);
1826 - if (e.key === 'Escape') {
1827 - e.preventDefault();
1828 - closeMenu(true);
1829 - } else if (e.key === 'ArrowDown') {
1830 - e.preventDefault();
1831 - var $next = $items.eq((idx + 1) % $items.length);
1832 - $items.attr('tabindex', '-1');
1833 - $next.attr('tabindex', '0').trigger('focus');
1834 - } else if (e.key === 'ArrowUp') {
1835 - e.preventDefault();
1836 - var $prev = $items.eq((idx - 1 + $items.length) % $items.length);
1837 - $items.attr('tabindex', '-1');
1838 - $prev.attr('tabindex', '0').trigger('focus');
1839 - } else if (e.key === 'Enter' || e.key === ' ') {
1840 - e.preventDefault();
1841 - $(this).trigger('click');
1842 - }
1843 - });
1844 - $trigger.on('keydown', function(e) {
1845 - if (e.key === 'Escape' && $menu.hasClass('is-open')) {
1846 - e.preventDefault();
1847 - closeMenu(true);
1848 - } else if ((e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') && !$menu.hasClass('is-open')) {
1849 - e.preventDefault();
1850 - openMenu();
1851 - }
1852 - });
1853 -
1854 - // Expose closeMenu for out-of-closure re-renders (mxchatRebuildHeaderMenus),
1855 - // then do the initial item render.
1856 - $wrap.data('mxchatMenuClose', closeMenu);
1857 - mxchatRenderHeaderMenuItems(botId, $wrap, closeMenu);
1858 -
1859 - $wrap.data('mxchatMenuReady', true);
1860 -}
1861 -
1862 -// Initialize header menus for every rendered widget on DOM ready.
1863 -$(function() {
1864 - $('.mxchat-header-menu-wrap').each(function() {
1865 - var botId = $(this).data('bot-id');
1866 - if (botId) mxchatInitHeaderMenu(botId);
1867 - });
1868 -
1869 - // Embedded (non-floating) widgets are open from the moment the page
1870 - // renders — refresh dynamic settings at init (plan-32db95). Floating
1871 - // widgets refresh on first launcher open instead.
1872 - var hasEmbeddedWidget = $('.mxchat-chatbot-wrapper').filter(function() {
1873 - return !$(this).closest('.floating-chatbot').length;
1874 - }).length > 0;
1875 - if (hasEmbeddedWidget) {
1876 - mxchatRefreshDynamicSettings();
1877 - }
1878 -});
1879 -
1189 +
1880 1190 function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false, botId = 'default') {
1881 1191 try {
1882 1192 // Determine styles based on sender type
1883 1193 let messageClass, bgColor, fontColor;
@@ -1915,12 +1225,17 @@
1915 1225 'margin-bottom': '1em'
1916 1226 });
1917 1227 }
1918 1228
1919 - // Process the message content - always run linkify to convert markdown
1920 - // links and format text. linkify() handles existing HTML safely via
1921 - // negative lookaheads that skip URLs already inside <a> tags.
1922 - let fullMessage = linkify(messageText);
1229 + // Process the message content based on sender
1230 + let fullMessage;
1231 + if (sender === "user") {
1232 + // For user messages, apply linkify after sanitization
1233 + fullMessage = linkify(messageText);
1234 + } else {
1235 + // For bot/agent messages, preserve HTML
1236 + fullMessage = messageText;
1237 + }
1923 1238
1924 1239 // Add images if provided
1925 1240 if (images && images.length > 0) {
1926 1241 fullMessage += '<div class="image-gallery" dir="auto">';
@@ -1969,12 +1284,8 @@
1969 1284 if (lastUserMessage.length) {
1970 1285 scrollElementToTop(lastUserMessage, botId);
1971 1286 }
1972 1287 }
1973 -
1974 - if ((sender === "bot" || sender === "agent") && !isTemporary) {
1975 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
1976 - }
1977 1288 });
1978 1289
1979 1290 if (messageText.id) {
1980 1291 var instance = MxChatInstances.get(botId);
@@ -2059,12 +1370,26 @@
2059 1370 bgColor = botMessageBgColor;
2060 1371 fontColor = botMessageFontColor;
2061 1372 }
2062 1373
2063 - // Always run linkify to convert markdown links and format text.
2064 - // linkify() already handles existing HTML (its URL patterns use negative lookaheads
2065 - // to avoid double-processing URLs that are already inside <a> tags).
2066 - var fullMessage = linkify(responseText);
1374 + // FIXED: Only linkify if response doesn't already contain HTML links or tags
1375 + // This prevents double-processing of URLs that are already formatted as HTML
1376 + var fullMessage;
1377 + if (sender === "user") {
1378 + // Always linkify user messages (they're plain text)
1379 + fullMessage = linkify(responseText);
1380 + } else {
1381 + // For bot/agent messages, check if HTML already exists
1382 + if (responseText.includes('<a href=') || responseText.includes('</a>') ||
1383 + responseText.includes('<img') || responseText.includes('<div') ||
1384 + responseText.includes('<p>') || responseText.includes('<br>')) {
1385 + // Response already has HTML, don't process it
1386 + fullMessage = responseText;
1387 + } else {
1388 + // Plain text response, apply linkify
1389 + fullMessage = linkify(responseText);
1390 + }
1391 + }
2067 1392
2068 1393 if (responseHtml) {
2069 1394 // Only add line breaks if there's actual text content before the HTML
2070 1395 if (fullMessage && fullMessage.trim()) {
@@ -2121,12 +1446,8 @@
2121 1446 }
2122 1447
2123 1448 // Re-enable chat input after response is displayed
2124 1449 enableChatInput(botId);
2125 -
2126 - if (sender === "bot" || sender === "agent") {
2127 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
2128 - }
2129 1450 } else {
2130 1451 appendMessage(sender, responseText, responseHtml, images, false, botId);
2131 1452 // Re-enable chat input after response is displayed
2132 1453 enableChatInput(botId);
@@ -2135,15 +1456,8 @@
2135 1456
2136 1457
2137 1458 function appendThinkingMessage(botId) {
2138 1459 botId = botId || 'default';
2139 -
2140 - // Don't show thinking dots in live agent mode - message is just forwarded to a human
2141 - var indicator = getElementDOM(botId, 'chat-mode-indicator');
2142 - if (indicator && indicator.textContent === 'Live Agent') {
2143 - return;
2144 - }
2145 -
2146 1460 var $chatBox = getElement(botId, 'chat-box');
2147 1461
2148 1462 // Remove any existing thinking dots in this bot's chat first
2149 1463 $chatBox.find('.thinking-dots').remove();
@@ -2165,9 +1479,9 @@
2165 1479 '</div>' +
2166 1480 '</div>';
2167 1481
2168 1482 // Append the thinking dots to this bot's chat container - skip inline colors if AI theme is active
2169 - var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + '; color: ' + botMessageFontColor + ';"';
1483 + var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + ';"';
2170 1484 $chatBox.append('<div class="bot-message temporary-message"' + messageStyle + '>' + thinkingHtml + '</div>');
2171 1485 scrollToBottom(botId);
2172 1486 }
2173 1487
@@ -2173,11 +1487,9 @@
2173 1487
2174 1488 function removeThinkingDots(botId) {
2175 1489 botId = botId || 'default';
2176 1490 var $chatBox = getElement(botId, 'chat-box');
2177 - // Remove by temporary-message class first, then fall back to any bot-message containing thinking dots
2178 1491 $chatBox.find('.thinking-dots').closest('.temporary-message').remove();
2179 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
2180 1492 }
2181 1493
2182 1494 // ====================================
2183 1495 // TEXT FORMATTING & PROCESSING
@@ -2211,12 +1523,9 @@
2211 1523 processedText = formatTextStyling(processedText);
2212 1524
2213 1525 // Process code blocks BEFORE processing links
2214 1526 processedText = formatCodeBlocks(processedText);
2215 -
2216 - // Process markdown tables BEFORE converting newlines to paragraphs
2217 - processedText = formatMarkdownTables(processedText);
2218 -
1527 +
2219 1528 // NOW convert to paragraphs
2220 1529 processedText = convertNewlinesToBreaks(processedText);
2221 1530
2222 1531 // IMPORTANT: Handle citation-style brackets FIRST [URL]
@@ -2229,63 +1538,37 @@
2229 1538 // Return as a proper link without the brackets
2230 1539 return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2231 1540 });
2232 1541
2233 - // Process markdown links: [text](url) and [](url)
2234 - // Uses balanced parenthesis matching to handle URLs containing parens
2235 - // (e.g. PDF filenames with dates like (2025-08-28).pdf)
2236 - processedText = (function(input) {
2237 - var result = '';
2238 - var i = 0;
2239 - while (i < input.length) {
2240 - // Look for [ at current position
2241 - if (input[i] === '[') {
2242 - // Find closing ]
2243 - var closeBracket = input.indexOf(']', i + 1);
2244 - if (closeBracket === -1 || closeBracket + 1 >= input.length || input[closeBracket + 1] !== '(') {
2245 - result += input[i];
2246 - i++;
2247 - continue;
2248 - }
2249 - var linkText = input.substring(i + 1, closeBracket);
2250 - // Check if URL starts with http
2251 - var urlStart = closeBracket + 2;
2252 - if (!input.substring(urlStart).match(/^https?:\/\//)) {
2253 - result += input[i];
2254 - i++;
2255 - continue;
2256 - }
2257 - // Find balanced closing paren
2258 - var depth = 1;
2259 - var j = urlStart;
2260 - while (j < input.length && depth > 0) {
2261 - if (input[j] === '(') depth++;
2262 - else if (input[j] === ')') depth--;
2263 - if (depth > 0) j++;
2264 - }
2265 - if (depth !== 0) {
2266 - result += input[i];
2267 - i++;
2268 - continue;
2269 - }
2270 - var url = input.substring(urlStart, j);
2271 - var cleanUrl = url.replace(/[\].,;!?]+$/, '');
2272 - var encodedUrl = safeEncodeUrl(cleanUrl);
2273 - if (!linkText || !linkText.trim()) {
2274 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + cleanUrl + '</a>';
2275 - } else {
2276 - var safeText = sanitizeUserInput(linkText);
2277 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + safeText + '</a>';
2278 - }
2279 - i = j + 1; // Skip past the closing )
2280 - } else {
2281 - result += input[i];
2282 - i++;
2283 - }
1542 + // Process proper markdown links with text: [text](url)
1543 + // This MUST have non-empty text in the first brackets
1544 + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g;
1545 + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => {
1546 + // Make sure we have actual text (not just whitespace)
1547 + if (!text || !text.trim()) {
1548 + // If no text, treat the URL as the text
1549 + let cleanUrl = url.replace(/[.,;!?]+$/, '');
1550 + const safeUrl = safeEncodeUrl(cleanUrl);
1551 + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2284 1552 }
2285 - return result;
2286 - })(processedText);
1553 +
1554 + // Clean the URL
1555 + let cleanUrl = url.replace(/[\].,;!?]+$/, '');
1556 + const safeUrl = safeEncodeUrl(cleanUrl);
1557 + const safeText = sanitizeUserInput(text);
1558 + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`;
1559 + });
2287 1560
1561 + // Handle empty markdown links: [](url)
1562 + // This is a specific case where there's no text
1563 + const emptyMarkdownPattern = /\[\]\((https?:\/\/[^\s)]+)\)/g;
1564 + processedText = processedText.replace(emptyMarkdownPattern, (match, url) => {
1565 + let cleanUrl = url.replace(/[.,;!?]+$/, '');
1566 + const safeUrl = safeEncodeUrl(cleanUrl);
1567 + // Use the URL itself as the link text
1568 + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
1569 + });
1570 +
2288 1571 // Process phone numbers: [text](tel:number)
2289 1572 const phonePattern = /\[([^\]]+)\]\((tel:[\d+\-\s()]+)\)/g;
2290 1573 processedText = processedText.replace(phonePattern, (match, text, phone) => {
2291 1574 const safePhone = safeEncodeUrl(phone);
@@ -2437,78 +1720,9 @@
2437 1720 });
2438 1721
2439 1722 return text;
2440 1723 }
2441 -
2442 - function formatMarkdownTables(text) {
2443 - var lines = text.split('\n');
2444 - var result = [];
2445 - var i = 0;
2446 -
2447 - while (i < lines.length) {
2448 - // Check for a table: current line has pipes AND next line is a separator row
2449 - if (i + 1 < lines.length &&
2450 - lines[i].indexOf('|') !== -1 &&
2451 - /^\s*\|?[\s\-:]+(\|[\s\-:]+)+\|?\s*$/.test(lines[i + 1])) {
2452 -
2453 - var tableLines = [];
2454 - var headerLine = lines[i];
2455 - var separatorLine = lines[i + 1];
2456 - tableLines.push(headerLine);
2457 - tableLines.push(separatorLine);
2458 -
2459 - // Collect remaining table rows
2460 - var j = i + 2;
2461 - while (j < lines.length && lines[j].indexOf('|') !== -1 && lines[j].trim() !== '') {
2462 - tableLines.push(lines[j]);
2463 - j++;
2464 - }
2465 -
2466 - // Parse alignment from separator row
2467 - var sepCells = separatorLine.split('|').filter(function(c) { return c.trim() !== ''; });
2468 - var alignments = sepCells.map(function(cell) {
2469 - var trimmed = cell.trim();
2470 - if (trimmed.charAt(0) === ':' && trimmed.charAt(trimmed.length - 1) === ':') return 'center';
2471 - if (trimmed.charAt(trimmed.length - 1) === ':') return 'right';
2472 - return 'left';
2473 - });
2474 -
2475 - // Build HTML table
2476 - var html = '<div class="mxchat-table-wrapper"><table class="mxchat-table">';
2477 -
2478 - // Header row
2479 - var headerCells = tableLines[0].split('|').filter(function(c) { return c.trim() !== ''; });
2480 - html += '<thead><tr>';
2481 - headerCells.forEach(function(cell, idx) {
2482 - var align = alignments[idx] || 'left';
2483 - html += '<th style="text-align:' + align + '">' + cell.trim() + '</th>';
2484 - });
2485 - html += '</tr></thead>';
2486 -
2487 - // Body rows
2488 - html += '<tbody>';
2489 - for (var r = 2; r < tableLines.length; r++) {
2490 - var rowCells = tableLines[r].split('|').filter(function(c) { return c.trim() !== ''; });
2491 - html += '<tr>';
2492 - rowCells.forEach(function(cell, idx) {
2493 - var align = alignments[idx] || 'left';
2494 - html += '<td style="text-align:' + align + '">' + cell.trim() + '</td>';
2495 - });
2496 - html += '</tr>';
2497 - }
2498 - html += '</tbody></table></div>';
2499 -
2500 - result.push(html);
2501 - i = j;
2502 - } else {
2503 - result.push(lines[i]);
2504 - i++;
2505 - }
2506 - }
2507 -
2508 - return result.join('\n');
2509 - }
2510 -
1724 +
2511 1725 function sanitizeUserInput(text) {
2512 1726 const div = document.createElement('div');
2513 1727 div.textContent = text;
2514 1728 return div.innerHTML;
@@ -2579,14 +1793,13 @@
2579 1793 requestAnimationFrame(smoothScroll);
2580 1794 }
2581 1795 }
2582 1796
2583 - function scrollElementToTop(element, botId, topOffset) {
1797 + function scrollElementToTop(element, botId) {
2584 1798 botId = botId || 'default';
2585 - topOffset = (typeof topOffset === 'number') ? topOffset : 2;
2586 1799 var chatBox = getElement(botId, 'chat-box');
2587 1800 var elementTop = element.position().top + chatBox.scrollTop();
2588 - chatBox.animate({ scrollTop: Math.max(0, elementTop - topOffset) }, 500);
1801 + chatBox.animate({ scrollTop: elementTop }, 500);
2589 1802 }
2590 1803
2591 1804 function showChatWidget(botId) {
2592 1805 botId = botId || 'default';
@@ -2730,12 +1943,15 @@
2730 1943 // LIVE AGENT FUNCTIONALITY
2731 1944 // ====================================
2732 1945
2733 1946 function startPolling(botId) {
1947 + console.log('[MxChat] startPolling called for botId:', botId);
2734 1948 botId = botId || 'default';
2735 1949 var instance = MxChatInstances.get(botId);
2736 1950 // Clear any existing interval first
2737 1951 stopPolling(botId);
1952 + // Start new polling interval
1953 + console.log('[MxChat] Starting polling interval (5s) for botId:', botId);
2738 1954 instance.pollingInterval = setInterval(function() {
2739 1955 checkForAgentMessages(botId);
2740 1956 }, 5000);
2741 1957 }
@@ -2740,17 +1956,20 @@
2740 1956 }, 5000);
2741 1957 }
2742 1958
2743 1959 function stopPolling(botId) {
1960 + console.log('[MxChat] stopPolling called for botId:', botId);
2744 1961 botId = botId || 'default';
2745 1962 var instance = MxChatInstances.get(botId);
2746 1963 if (instance.pollingInterval) {
2747 1964 clearInterval(instance.pollingInterval);
2748 1965 instance.pollingInterval = null;
1966 + console.log('[MxChat] Polling stopped for botId:', botId);
2749 1967 }
2750 1968 }
2751 1969
2752 1970 function checkForAgentMessages(botId) {
1971 + console.log('[MxChat] checkForAgentMessages called for botId:', botId);
2753 1972 botId = botId || 'default';
2754 1973 var instance = MxChatInstances.get(botId);
2755 1974 const sessionId = getChatSession(botId);
2756 1975 $.ajax({
@@ -2776,12 +1995,8 @@
2776 1995 instance.processedMessageIds.add(message.id);
2777 1996 }
2778 1997 });
2779 1998
2780 - if (hasNewMessage) {
2781 - enableChatInput(botId);
2782 - }
2783 -
2784 1999 var $floatingChatbot = getElement(botId, 'floating-chatbot');
2785 2000 if (hasNewMessage && $floatingChatbot.hasClass('hidden')) {
2786 2001 showNotification(botId);
2787 2002 }
@@ -2787,13 +2002,8 @@
2787 2002 }
2788 2003
2789 2004 scrollToBottom(botId, true);
2790 2005 }
2791 -
2792 - // Handle chat mode transitions (e.g. agent ended chat via !endchat)
2793 - if (response.success && response.data?.chat_mode) {
2794 - updateChatModeIndicator(response.data.chat_mode, botId);
2795 - }
2796 2006 },
2797 2007 error: function (xhr, status, error) {
2798 2008 // Polling error - silently continue
2799 2009 }
@@ -2803,29 +2013,20 @@
2803 2013 // ====================================
2804 2014 // CHAT HISTORY & PERSISTENCE
2805 2015 // ====================================
2806 2016
2807 -function loadChatHistory(botId, onComplete) {
2017 +function loadChatHistory(botId) {
2808 2018 botId = botId || 'default';
2809 2019 var instance = MxChatInstances.get(botId);
2810 2020
2811 2021 // Prevent duplicate loading
2812 2022 if (instance.chatHistoryLoaded) {
2813 - if (onComplete) onComplete();
2814 2023 return;
2815 2024 }
2816 2025
2817 - // Use getChatSession which returns null if no session exists (does NOT create one)
2818 2026 var sessionId = getChatSession(botId);
2819 2027 var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
2820 2028
2821 - // No session yet — nothing to load. History will load after first message via ensureSession.
2822 - if (!sessionId) {
2823 - instance.chatHistoryLoaded = true;
2824 - if (onComplete) onComplete();
2825 - return;
2826 - }
2827 -
2828 2029 if (chatPersistenceEnabled && sessionId) {
2829 2030 $.ajax({
2830 2031 url: mxchatChat.ajax_url,
2831 2032 type: 'POST',
@@ -2836,12 +2037,11 @@
2836 2037 },
2837 2038 success: function(response) {
2838 2039 // Handle session reset (IP changed while user was away)
2839 2040 if (response.success === false && response.data && response.data.action === 'reset_session') {
2840 - // Silent reset — new session but don't clear UI
2841 - MxChatInstances.silentResetSession(botId);
2041 + // Silently reset session - user will start fresh
2042 + resetChatSession(botId);
2842 2043 instance.chatHistoryLoaded = true; // Prevent retry loop
2843 - if (onComplete) onComplete();
2844 2044 return;
2845 2045 }
2846 2046
2847 2047 // Check if the response indicates success
@@ -2897,19 +2097,9 @@
2897 2097 var content = message.content;
2898 2098 content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
2899 2099 content = decodeHTMLEntities(content);
2900 2100
2901 - // Skip linkify for messages containing structured HTML
2902 - // (forms, product cards, galleries, etc.) to avoid
2903 - // markdown formatting corrupting HTML attributes
2904 - // (e.g. underscores in name="field_name" becoming <em> tags)
2905 - if (content.includes("mxchat-product-card") ||
2906 - content.includes("mxchat-image-gallery") ||
2907 - content.includes("mxchat-featured-products") ||
2908 - content.includes("<form") ||
2909 - content.includes("<input") ||
2910 - content.includes("<select") ||
2911 - content.includes("<textarea")) {
2101 + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
2912 2102 messageElement.html(content);
2913 2103 } else {
2914 2104 var formattedContent = linkify(content);
2915 2105 messageElement.html(formattedContent);
@@ -2949,17 +2139,13 @@
2949 2139 instance.chatHistoryLoaded = true;
2950 2140 }
2951 2141 }
2952 2142 }
2953 - if (onComplete) onComplete();
2954 2143 },
2955 2144 error: function(xhr, status, error) {
2956 2145 // Error loading chat history - silently continue
2957 - if (onComplete) onComplete();
2958 2146 }
2959 2147 });
2960 - } else {
2961 - if (onComplete) onComplete();
2962 2148 }
2963 2149 }
2964 2150
2965 2151
@@ -3023,10 +2209,10 @@
3023 2209 .then(data => {
3024 2210 if (data.success) {
3025 2211 container.style.display = 'none';
3026 2212 nameElement.textContent = '';
3027 - instance.activePdfFile = null;
3028 - appendMessage('bot', 'PDF removed.', '', [], false, botId);
2213 + activePdfFile = null;
2214 + appendMessage('bot', 'PDF removed.');
3029 2215 }
3030 2216 })
3031 2217 .catch(error => {
3032 2218 // Error removing PDF - silently continue
@@ -3032,16 +2218,14 @@
3032 2218 // Error removing PDF - silently continue
3033 2219 });
3034 2220 }
3035 2221
3036 - function removeActiveWord(botId) {
3037 - botId = botId || 'default';
3038 - var instance = MxChatInstances.get(botId);
3039 - const container = getElementDOM(botId, 'active-word-container');
3040 - const nameElement = getElementDOM(botId, 'active-word-name');
3041 -
3042 - if (!container || !nameElement || !instance.activeWordFile) return;
3043 -
2222 + function removeActiveWord() {
2223 + const container = document.getElementById('active-word-container');
2224 + const nameElement = document.getElementById('active-word-name');
2225 +
2226 + if (!container || !nameElement || !activeWordFile) return;
2227 +
3044 2228 fetch(mxchatChat.ajax_url, {
3045 2229 method: 'POST',
3046 2230 headers: {
3047 2231 'Content-Type': 'application/x-www-form-urlencoded',
@@ -3047,9 +2231,9 @@
3047 2231 'Content-Type': 'application/x-www-form-urlencoded',
3048 2232 },
3049 2233 body: new URLSearchParams({
3050 2234 'action': 'mxchat_remove_word',
3051 - 'session_id': getChatSession(botId),
2235 + 'session_id': sessionId,
3052 2236 'nonce': mxchatChat.nonce
3053 2237 })
3054 2238 })
3055 2239 .then(response => response.json())
@@ -3056,10 +2240,10 @@
3056 2240 .then(data => {
3057 2241 if (data.success) {
3058 2242 container.style.display = 'none';
3059 2243 nameElement.textContent = '';
3060 - instance.activeWordFile = null;
3061 - appendMessage('bot', 'Word document removed.', '', [], false, botId);
2244 + activeWordFile = null;
2245 + appendMessage('bot', 'Word document removed.');
3062 2246 }
3063 2247 })
3064 2248 .catch(error => {
3065 2249 // Error removing Word document - silently continue
@@ -3137,35 +2321,45 @@
3137 2321 // ====================================
3138 2322
3139 2323 function checkPreChatDismissal(botId) {
3140 2324 botId = botId || 'default';
3141 - try {
3142 - var dismissedAt = localStorage.getItem('mxchat_pre_chat_dismissed_' + botId);
3143 - if (dismissedAt) {
3144 - // Re-show after 24 hours
3145 - var elapsed = Date.now() - parseInt(dismissedAt, 10);
3146 - if (elapsed < 86400000) {
2325 + $.ajax({
2326 + url: mxchatChat.ajax_url,
2327 + type: 'POST',
2328 + data: {
2329 + action: 'mxchat_check_pre_chat_message_status',
2330 + _ajax_nonce: mxchatChat.nonce
2331 + },
2332 + success: function(response) {
2333 + if (response.success && !response.data.dismissed) {
2334 + getElement(botId, 'pre-chat-message').fadeIn(250);
2335 + } else {
3147 2336 getElement(botId, 'pre-chat-message').hide();
3148 - return;
3149 2337 }
3150 - // Expired — clear and show again
3151 - localStorage.removeItem('mxchat_pre_chat_dismissed_' + botId);
2338 + },
2339 + error: function() {
2340 + // Error checking pre-chat dismissal - silently continue
3152 2341 }
3153 - getElement(botId, 'pre-chat-message').fadeIn(250);
3154 - } catch (e) {
3155 - // localStorage unavailable — show the message
3156 - getElement(botId, 'pre-chat-message').fadeIn(250);
3157 - }
2342 + });
3158 2343 }
3159 2344
3160 2345 function handlePreChatDismissal(botId) {
3161 2346 botId = botId || 'default';
3162 2347 getElement(botId, 'pre-chat-message').fadeOut(200);
3163 - try {
3164 - localStorage.setItem('mxchat_pre_chat_dismissed_' + botId, String(Date.now()));
3165 - } catch (e) {
3166 - // localStorage unavailable — dismissal won't persist
3167 - }
2348 + $.ajax({
2349 + url: mxchatChat.ajax_url,
2350 + type: 'POST',
2351 + data: {
2352 + action: 'mxchat_dismiss_pre_chat_message',
2353 + _ajax_nonce: mxchatChat.nonce
2354 + },
2355 + success: function() {
2356 + $('#pre-chat-message').hide();
2357 + },
2358 + error: function() {
2359 + // Error dismissing pre-chat message - silently continue
2360 + }
2361 + });
3168 2362 }
3169 2363
3170 2364
3171 2365 // ====================================
@@ -3220,14 +2414,9 @@
3220 2414 collapseQuickQuestions(botId);
3221 2415 });
3222 2416
3223 2417 // Chatbot visibility toggle handlers - use class selector for multi-instance support
3224 - // Handles click + Enter/Space keypresses for keyboard accessibility (WCAG 2.1 SC 2.1.1).
3225 - $(document).on('click keydown', '.floating-chatbot-button', function(e) {
3226 - if (e.type === 'keydown') {
3227 - if (e.key !== 'Enter' && e.key !== ' ' && e.key !== 'Spacebar') return;
3228 - e.preventDefault();
3229 - }
2418 + $(document).on('click', '.floating-chatbot-button', function() {
3230 2419 var botId = getBotIdFromElement(this);
3231 2420 var $chatbot = getElement(botId, 'floating-chatbot');
3232 2421 var $badge = getElement(botId, 'chat-notification-badge');
3233 2422 var $preChat = getElement(botId, 'pre-chat-message');
@@ -3232,88 +2421,35 @@
3232 2421 var $badge = getElement(botId, 'chat-notification-badge');
3233 2422 var $preChat = getElement(botId, 'pre-chat-message');
3234 2423
3235 2424 if ($chatbot.hasClass('hidden')) {
3236 - $chatbot.removeClass('hidden').addClass('visible')
3237 - .attr('aria-modal', 'true').attr('role', 'dialog');
3238 - $(this).addClass('hidden').attr('aria-expanded', 'true');
2425 + $chatbot.removeClass('hidden').addClass('visible');
2426 + $(this).addClass('hidden');
3239 2427 $badge.hide(); // Hide notification when opening chat
3240 2428 disableScroll();
3241 2429 $preChat.fadeOut(250);
3242 -
3243 - // First open per page load: re-fetch behavior settings in case
3244 - // this page's inline values came from a stale full-page cache
3245 - // (plan-32db95). Idempotent — later opens are a no-op.
3246 - mxchatRefreshDynamicSettings();
3247 -
3248 - // Load chat history for returning visitors (persistence)
3249 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3250 - if (chatPersistenceEnabled) {
3251 - MxChatInstances.ensureSession(botId);
3252 - }
3253 -
3254 - // Deferred email check — only on first widget open
3255 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3256 - var instance = MxChatInstances.get(botId);
3257 - if (emailBlocker && !instance.emailCheckDone) {
3258 - instance.emailCheckDone = true;
3259 - resolveEmailState(botId);
3260 - } else if (!emailBlocker) {
3261 - // No email collection — still route through showChatContainerForBot
3262 - // so the loader is shown while chat history loads
3263 - showChatContainerForBot(botId);
3264 - }
3265 -
3266 - // Move keyboard focus into the message input after the open transition.
3267 - setTimeout(function() {
3268 - var chatInput = getElementDOM(botId, 'chat-input');
3269 - if (chatInput && !chatInput.disabled) {
3270 - try { chatInput.focus({ preventScroll: true }); } catch (err) { chatInput.focus(); }
3271 - }
3272 - }, 300);
3273 2430 } else {
3274 - $chatbot.removeClass('visible').addClass('hidden').removeAttr('aria-modal');
3275 - $(this).removeClass('hidden').attr('aria-expanded', 'false');
2431 + $chatbot.removeClass('visible').addClass('hidden');
2432 + $(this).removeClass('hidden');
3276 2433 enableScroll();
3277 2434 checkPreChatDismissal(botId);
3278 2435 }
3279 2436 });
3280 2437
3281 - // Allow clicking anywhere on the title bar to close the chatbot.
3282 - // Returns keyboard focus to the launcher so keyboard users don't get
3283 - // stranded at <body> (WCAG SC 2.4.3 Focus Order). :focus-visible is
3284 - // heuristic-based so mouse-triggered close won't show a focus ring.
2438 + // Allow clicking anywhere on the title bar to close the chatbot
3285 2439 $(document).on('click', '.chatbot-top-bar', function() {
3286 2440 var botId = getBotIdFromElement(this);
3287 - getElement(botId, 'floating-chatbot').addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3288 - var $launcher = getElement(botId, 'floating-chatbot-button');
3289 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
2441 + getElement(botId, 'floating-chatbot').addClass('hidden').removeClass('visible');
2442 + getElement(botId, 'floating-chatbot-button').removeClass('hidden');
3290 2443 enableScroll();
3291 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3292 2444 });
3293 2445
3294 - // Global Escape-key handler — closes any visible chat widget and
3295 - // returns focus to its launcher. Standard modal-dismissal pattern;
3296 - // pairs with aria-modal="true" set on the widget when it opens.
3297 - $(document).on('keydown', function(e) {
3298 - if (e.key !== 'Escape' && e.key !== 'Esc') return;
3299 - var $visible = $('.floating-chatbot.visible');
3300 - if (!$visible.length) return;
3301 - e.preventDefault();
3302 - $visible.each(function() {
3303 - var botId = getBotIdFromElement(this);
3304 - $(this).addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3305 - var $launcher = getElement(botId, 'floating-chatbot-button');
3306 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
3307 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3308 - });
3309 - enableScroll();
3310 - });
3311 -
3312 2446 $(document).on('click', '.close-pre-chat-message', function(e) {
3313 2447 e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
3314 2448 var botId = getBotIdFromElement(this);
3315 - handlePreChatDismissal(botId);
2449 + getElement(botId, 'pre-chat-message').fadeOut(200, function() {
2450 + $(this).remove();
2451 + });
3316 2452 });
3317 2453
3318 2454
3319 2455 // PDF upload button handlers - use class selector
@@ -3329,20 +2465,17 @@
3329 2465 var wordInput = getElementDOM(botId, 'word-upload');
3330 2466 if (wordInput) wordInput.click();
3331 2467 });
3332 2468
3333 - // PDF file input change handler - delegated, bot-aware (was bound to stale un-suffixed id 'pdf-upload')
3334 - $(document).on('change', '.pdf-upload', async function(e) {
3335 - var botId = getBotIdFromElement(this);
3336 - var instance = MxChatInstances.get(botId);
3337 - const file = this.files[0];
3338 - const sessionId = MxChatInstances.ensureSession(botId);
3339 -
2469 + // PDF file input change handler
2470 + addSafeEventListener('pdf-upload', 'change', async function(e) {
2471 + const file = e.target.files[0];
2472 +
3340 2473 if (!file || file.type !== 'application/pdf') {
3341 2474 alert('Please select a valid PDF file.');
3342 2475 return;
3343 2476 }
3344 -
2477 +
3345 2478 if (!sessionId) {
3346 2479 alert('Error: No session ID found');
3347 2480 return;
3348 2481 }
@@ -3350,49 +2483,47 @@
3350 2483 if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
3351 2484 alert('Error: Ajax configuration missing');
3352 2485 return;
3353 2486 }
3354 -
2487 +
3355 2488 // Disable buttons and show loading state
3356 - const uploadBtn = getElementDOM(botId, 'pdf-upload-btn');
3357 - const sendBtn = getElementDOM(botId, 'send-button');
3358 - if (!uploadBtn) return;
2489 + const uploadBtn = document.getElementById('pdf-upload-btn');
2490 + const sendBtn = document.getElementById('send-button');
3359 2491 const originalBtnContent = uploadBtn.innerHTML;
3360 -
2492 +
3361 2493 try {
3362 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3363 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3364 2494 const formData = new FormData();
3365 2495 formData.append('action', 'mxchat_upload_pdf');
3366 2496 formData.append('pdf_file', file);
3367 2497 formData.append('session_id', sessionId);
3368 2498 formData.append('nonce', mxchatChat.nonce);
3369 -
2499 +
3370 2500 uploadBtn.disabled = true;
3371 - if (sendBtn) sendBtn.disabled = true;
2501 + sendBtn.disabled = true;
3372 2502 uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
3373 2503 <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
3374 2504 </svg>`;
3375 -
2505 +
3376 2506 const response = await fetch(mxchatChat.ajax_url, {
3377 2507 method: 'POST',
3378 2508 body: formData
3379 2509 });
3380 -
2510 +
3381 2511 const data = await response.json();
3382 -
2512 +
3383 2513 if (data.success) {
3384 2514 // Hide popular questions if they exist
3385 - if (hasQuickQuestions(botId)) {
3386 - collapseQuickQuestions(botId);
2515 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
2516 + if (hasQuickQuestions()) {
2517 + collapseQuickQuestions();
3387 2518 }
3388 -
2519 +
3389 2520 // Show the active PDF name
3390 - showActivePdf(data.data.filename, botId);
3391 -
3392 - appendMessage('bot', data.data.message, '', [], false, botId);
3393 - scrollToBottom(botId);
3394 - instance.activePdfFile = data.data.filename;
2521 + showActivePdf(data.data.filename);
2522 +
2523 + appendMessage('bot', data.data.message);
2524 + scrollToBottom();
2525 + activePdfFile = data.data.filename;
3395 2526 } else {
3396 2527 alert('Failed to upload PDF. Please try again.');
3397 2528 }
3398 2529 } catch (error) {
@@ -3398,76 +2529,66 @@
3398 2529 } catch (error) {
3399 2530 alert('Error uploading file. Please try again.');
3400 2531 } finally {
3401 2532 uploadBtn.disabled = false;
3402 - if (sendBtn) sendBtn.disabled = false;
2533 + sendBtn.disabled = false;
3403 2534 uploadBtn.innerHTML = originalBtnContent;
3404 2535 this.value = ''; // Reset file input
3405 2536 }
3406 2537 });
3407 2538
3408 - // Word file input change handler - delegated, bot-aware (was bound to stale un-suffixed id 'word-upload')
3409 - $(document).on('change', '.word-upload', async function(e) {
3410 - var botId = getBotIdFromElement(this);
3411 - var instance = MxChatInstances.get(botId);
3412 - const file = this.files[0];
3413 - const sessionId = MxChatInstances.ensureSession(botId);
3414 -
2539 + // Word file input change handler
2540 + addSafeEventListener('word-upload', 'change', async function(e) {
2541 + const file = e.target.files[0];
2542 +
3415 2543 if (!file || file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
3416 2544 alert('Please select a valid Word document (.docx).');
3417 2545 return;
3418 2546 }
3419 -
2547 +
3420 2548 if (!sessionId) {
3421 2549 alert('Error: No session ID found');
3422 2550 return;
3423 2551 }
3424 2552
3425 - if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
3426 - alert('Error: Ajax configuration missing');
3427 - return;
3428 - }
3429 -
3430 2553 // Disable buttons and show loading state
3431 - const uploadBtn = getElementDOM(botId, 'word-upload-btn');
3432 - const sendBtn = getElementDOM(botId, 'send-button');
3433 - if (!uploadBtn) return;
2554 + const uploadBtn = document.getElementById('word-upload-btn');
2555 + const sendBtn = document.getElementById('send-button');
3434 2556 const originalBtnContent = uploadBtn.innerHTML;
3435 -
2557 +
3436 2558 try {
3437 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3438 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3439 2559 const formData = new FormData();
3440 2560 formData.append('action', 'mxchat_upload_word');
3441 2561 formData.append('word_file', file);
3442 2562 formData.append('session_id', sessionId);
3443 2563 formData.append('nonce', mxchatChat.nonce);
3444 -
2564 +
3445 2565 uploadBtn.disabled = true;
3446 - if (sendBtn) sendBtn.disabled = true;
2566 + sendBtn.disabled = true;
3447 2567 uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
3448 2568 <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
3449 2569 </svg>`;
3450 -
2570 +
3451 2571 const response = await fetch(mxchatChat.ajax_url, {
3452 2572 method: 'POST',
3453 2573 body: formData
3454 2574 });
3455 -
2575 +
3456 2576 const data = await response.json();
3457 -
2577 +
3458 2578 if (data.success) {
3459 2579 // Hide popular questions if they exist
3460 - if (hasQuickQuestions(botId)) {
3461 - collapseQuickQuestions(botId);
2580 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
2581 + if (hasQuickQuestions()) {
2582 + collapseQuickQuestions();
3462 2583 }
3463 -
2584 +
3464 2585 // Show the active Word document name
3465 - showActiveWord(data.data.filename, botId);
3466 -
3467 - appendMessage('bot', data.data.message, '', [], false, botId);
3468 - scrollToBottom(botId);
3469 - instance.activeWordFile = data.data.filename;
2586 + showActiveWord(data.data.filename);
2587 +
2588 + appendMessage('bot', data.data.message);
2589 + scrollToBottom();
2590 + activeWordFile = data.data.filename;
3470 2591 } else {
3471 2592 alert('Failed to upload Word document. Please try again.');
3472 2593 }
3473 2594 } catch (error) {
@@ -3473,25 +2594,25 @@
3473 2594 } catch (error) {
3474 2595 alert('Error uploading file. Please try again.');
3475 2596 } finally {
3476 2597 uploadBtn.disabled = false;
3477 - if (sendBtn) sendBtn.disabled = false;
2598 + sendBtn.disabled = false;
3478 2599 uploadBtn.innerHTML = originalBtnContent;
3479 2600 this.value = ''; // Reset file input
3480 2601 }
3481 2602 });
3482 2603
3483 - // Remove button click handlers - delegated, bot-aware (were bound to stale un-suffixed ids)
3484 - $(document).on('click', '.remove-pdf-btn', function(e) {
2604 + // Remove button click handlers
2605 + document.getElementById('remove-pdf-btn')?.addEventListener('click', function(e) {
3485 2606 e.preventDefault();
3486 2607 e.stopPropagation();
3487 - removeActivePdf(getBotIdFromElement(this));
2608 + removeActivePdf();
3488 2609 });
3489 -
3490 - $(document).on('click', '.remove-word-btn', function(e) {
2610 +
2611 + document.getElementById('remove-word-btn')?.addEventListener('click', function(e) {
3491 2612 e.preventDefault();
3492 2613 e.stopPropagation();
3493 - removeActiveWord(getBotIdFromElement(this));
2614 + removeActiveWord();
3494 2615 });
3495 2616
3496 2617 // Window resize handlers
3497 2618 $(window).on('resize orientationchange', function() {
@@ -3529,59 +2650,8 @@
3529 2650 });
3530 2651
3531 2652
3532 2653 // ====================================
3533 -// INIT LOADER & CHAT CONTAINER HELPERS
3534 -// ====================================
3535 -// These must be outside the email collection block so they're always available
3536 -// (used by persistence loading even when email collection is off)
3537 -
3538 -function showInitLoader(botId) {
3539 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3540 - if (loader) loader.style.display = 'flex';
3541 -}
3542 -
3543 -function hideInitLoader(botId) {
3544 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3545 - if (loader) loader.style.display = 'none';
3546 -}
3547 -
3548 -function showEmailFormForBot(botId) {
3549 - hideInitLoader(botId);
3550 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3551 - var chatContainer = getElementDOM(botId, 'chat-container');
3552 - if (emailBlocker) emailBlocker.style.display = 'flex';
3553 - if (chatContainer) chatContainer.style.display = 'none';
3554 -}
3555 -
3556 -function showChatContainerForBot(botId) {
3557 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3558 - var chatContainer = getElementDOM(botId, 'chat-container');
3559 - if (emailBlocker) emailBlocker.style.display = 'none';
3560 -
3561 - var instance = MxChatInstances.get(botId);
3562 - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
3563 -
3564 - // If persistence is on and history hasn't loaded yet, show loader
3565 - // while history loads to prevent flash of empty chat
3566 - if (chatPersistenceEnabled && !instance.chatHistoryLoaded) {
3567 - if (chatContainer) chatContainer.style.display = 'none';
3568 - showInitLoader(botId);
3569 - loadChatHistory(botId, function() {
3570 - hideInitLoader(botId);
3571 - if (chatContainer) chatContainer.style.display = 'flex';
3572 - scrollToBottom(botId, true);
3573 - });
3574 - } else {
3575 - hideInitLoader(botId);
3576 - if (chatContainer) chatContainer.style.display = 'flex';
3577 - if (typeof loadChatHistory === 'function') {
3578 - loadChatHistory(botId);
3579 - }
3580 - }
3581 -}
3582 -
3583 -// ====================================
3584 2654 // EMAIL COLLECTION SETUP - MULTI-INSTANCE VERSION
3585 2655 // ====================================
3586 2656 // Only run email collection setup if it's enabled
3587 2657 if (mxchatChat && mxchatChat.email_collection_enabled === 'on') {
@@ -3617,8 +2687,28 @@
3617 2687 `;
3618 2688 document.head.appendChild(style);
3619 2689 }
3620 2690
2691 + // Helper functions for email collection (multi-instance aware)
2692 + function showEmailFormForBot(botId) {
2693 + var emailBlocker = getElementDOM(botId, 'email-blocker');
2694 + var chatContainer = getElementDOM(botId, 'chat-container');
2695 + if (emailBlocker) emailBlocker.style.display = 'flex';
2696 + if (chatContainer) chatContainer.style.display = 'none';
2697 + }
2698 +
2699 + function showChatContainerForBot(botId) {
2700 + var emailBlocker = getElementDOM(botId, 'email-blocker');
2701 + var chatContainer = getElementDOM(botId, 'chat-container');
2702 + if (emailBlocker) emailBlocker.style.display = 'none';
2703 + if (chatContainer) chatContainer.style.display = 'flex';
2704 +
2705 + // Load chat history for this bot
2706 + if (typeof loadChatHistory === 'function') {
2707 + loadChatHistory(botId);
2708 + }
2709 + }
2710 +
3621 2711 function isValidEmailAddress(email) {
3622 2712 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
3623 2713 return emailRegex.test(email.trim()) && email.length <= 254;
3624 2714 }
@@ -3740,31 +2830,11 @@
3740 2830 existingErrors.forEach(error => error.remove());
3741 2831 }
3742 2832 }
3743 2833
3744 - // Resolve email state using server-side data when available, AJAX fallback otherwise
3745 - function resolveEmailState(botId) {
3746 - if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) {
3747 - if (mxchatChat.initial_email_state.show_email_form) {
3748 - showEmailFormForBot(botId);
3749 - } else {
3750 - showChatContainerForBot(botId);
3751 - }
3752 - } else {
3753 - checkSessionAndEmailForBot(botId);
3754 - }
3755 - }
3756 -
3757 2834 function checkSessionAndEmailForBot(botId) {
3758 - const sessionId = MxChatInstances.ensureSession(botId);
2835 + const sessionId = getChatSession(botId);
3759 2836
3760 - // Hide both panels while we check — show loader instead
3761 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3762 - var chatContainer = getElementDOM(botId, 'chat-container');
3763 - if (emailBlocker) emailBlocker.style.display = 'none';
3764 - if (chatContainer) chatContainer.style.display = 'none';
3765 - showInitLoader(botId);
3766 -
3767 2837 fetch(mxchatChat.ajax_url, {
3768 2838 method: 'POST',
3769 2839 headers: {
3770 2840 'Content-Type': 'application/x-www-form-urlencoded',
@@ -3812,9 +2882,9 @@
3812 2882 var emailInput = getElementDOM(botId, 'user-email');
3813 2883 var nameInput = getElementDOM(botId, 'user-name');
3814 2884 var userEmail = emailInput ? emailInput.value.trim() : '';
3815 2885 var userName = nameInput ? nameInput.value.trim() : '';
3816 - var sessionId = MxChatInstances.ensureSession(botId);
2886 + var sessionId = getChatSession(botId);
3817 2887
3818 2888 // Validate email
3819 2889 if (!userEmail) {
3820 2890 showEmailError(botId, 'Please enter your email address.');
@@ -3937,27 +3007,25 @@
3937 3007 }
3938 3008 });
3939 3009
3940 3010 // Initialize email check for all bot instances
3941 - // For floating bots: defer until widget is opened (zero passive AJAX)
3942 - // For embedded bots: check immediately since the form is visible
3943 3011 $('.mxchat-chatbot-wrapper').each(function() {
3944 3012 var botId = $(this).data('bot-id') || 'default';
3945 3013 var emailBlocker = getElementDOM(botId, 'email-blocker');
3946 3014
3015 + // Only check if email blocker exists for this bot
3947 3016 if (emailBlocker) {
3948 - if (isEmbeddedBot(botId)) {
3949 - // Embedded bots are always visible — check now
3950 - resolveEmailState(botId);
3017 + if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) {
3018 + if (mxchatChat.initial_email_state.show_email_form) {
3019 + showEmailFormForBot(botId);
3020 + } else {
3021 + showChatContainerForBot(botId);
3022 + }
3023 + } else {
3024 + setTimeout(function() {
3025 + checkSessionAndEmailForBot(botId);
3026 + }, 100);
3951 3027 }
3952 - // Floating bots: handled in the widget open handler
3953 - } else if (isEmbeddedBot(botId)) {
3954 - // Embedded bot, no email collection — load history with loader
3955 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3956 - if (chatPersistenceEnabled) {
3957 - MxChatInstances.ensureSession(botId);
3958 - showChatContainerForBot(botId);
3959 - }
3960 3028 }
3961 3029 });
3962 3030 }
3963 3031
@@ -3967,32 +3035,39 @@
3967 3035 var $chatbot = getElement(botId, 'floating-chatbot');
3968 3036 if ($chatbot.hasClass('hidden')) {
3969 3037 $chatbot.removeClass('hidden').addClass('visible');
3970 3038 getElement(botId, 'floating-chatbot-button').addClass('hidden');
3971 - handlePreChatDismissal(botId);
3039 + $(this).fadeOut(250); // Hide pre-chat message
3972 3040 disableScroll(); // Disable scroll when chatbot opens
3041 + }
3042 + });
3973 3043
3974 - // Load chat history for returning visitors (persistence)
3975 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3976 - if (chatPersistenceEnabled) {
3977 - MxChatInstances.ensureSession(botId);
3978 - }
3044 + // Dismiss pre-chat message via close button - handled by event delegation above at line ~2376
3045 + // This is a fallback for legacy support
3046 + $(document).on('click', '.close-pre-chat-message', function() {
3047 + var botId = getBotIdFromElement(this);
3048 + var $preChat = getElement(botId, 'pre-chat-message');
3049 + $preChat.fadeOut(200); // Hide the message
3979 3050
3980 - // Deferred email check — only on first widget open
3981 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3982 - var instance = MxChatInstances.get(botId);
3983 - if (emailBlocker && !instance.emailCheckDone) {
3984 - instance.emailCheckDone = true;
3985 - resolveEmailState(botId);
3986 - } else if (!emailBlocker) {
3987 - showChatContainerForBot(botId);
3051 + // Send an AJAX request to set the transient flag for 24 hours
3052 + $.ajax({
3053 + url: mxchatChat.ajax_url,
3054 + type: 'POST',
3055 + data: {
3056 + action: 'mxchat_dismiss_pre_chat_message',
3057 + _ajax_nonce: mxchatChat.nonce
3058 + },
3059 + success: function() {
3060 + // Ensure the message is hidden after dismissal
3061 + $preChat.hide();
3062 + },
3063 + error: function() {
3064 + // Error dismissing pre-chat message - silently continue
3988 3065 }
3989 - }
3066 + });
3990 3067 });
3991 3068
3992 - // Legacy duplicate close handler removed — handled by single event delegation above
3993 3069
3994 -
3995 3070 function hasQuickQuestions(botId) {
3996 3071 botId = botId || 'default';
3997 3072 var questionsContainer = getElementDOM(botId, 'mxchat-popular-questions');
3998 3073 if (!questionsContainer) return false;
@@ -4128,11 +3203,18 @@
4128 3203 });
4129 3204
4130 3205 // Initialize when document is ready
4131 3206 setFullHeight();
3207 + trackOriginatingPage();
4132 3208
4133 - // Note: trackOriginatingPage() and loadChatHistory() are now deferred
4134 - // until the user's first interaction via MxChatInstances.ensureSession()
3209 + // Only load chat history if email collection is disabled
3210 + if (mxchatChat.email_collection_enabled !== 'on') {
3211 + // Load history for all instances
3212 + $('.mxchat-chatbot-wrapper').each(function() {
3213 + var botId = $(this).data('bot-id') || 'default';
3214 + loadChatHistory(botId);
3215 + });
3216 + }
4135 3217
4136 3218 // Initialize chat visibility for all instances
4137 3219 $('.mxchat-chatbot-wrapper').each(function() {
4138 3220 var botId = $(this).data('bot-id') || 'default';
@@ -4185,312 +3267,6 @@
4185 3267 }, 2000);
4186 3268 });
4187 3269 }
4188 3270 }
4189 -});
4190 -
4191 -// ============================================================================
4192 -// SATISFACTION RATING (v3.2.6)
4193 -// ============================================================================
4194 -// Per-session 👍/👎 prompt that appears in the chat-box after 60s of user
4195 -// inactivity following a bot reply. One prompt per session, deduped via
4196 -// localStorage. Runs ONLY when the satisfaction_rating_enabled option is on —
4197 -// the option (default off) is authoritative.
4198 -jQuery(function($) {
4199 - if (typeof mxchatChat === 'undefined') return;
4200 - // wp_localize_script stringifies scalars: a PHP boolean false arrives as
4201 - // '' and true as '1', so this must be an explicit-enable allowlist — the
4202 - // old "disabled when exactly false/'off'" check let '' through and the
4203 - // bubble rendered on sites with the option off/unset (plan-4bba64). PHP
4204 - // now emits 'on'/'off' strings; true/'1'/1 keep cached pre-fix HTML
4205 - // (boolean-true localizations) working.
4206 - // NOTE (plan-32db95): this gate reads the INLINE value at DOM ready and is
4207 - // deliberately NOT re-evaluated after the widget's dynamic-settings refresh
4208 - // merges fresh values over mxchatChat (that merge fires on first widget
4209 - // open, after this module has already decided). Re-evaluating would mean
4210 - // restructuring the whole module to late-bind its listeners — not worth it
4211 - // for a prompt that is at worst stale for one page load on a cached page.
4212 - var sre = mxchatChat.satisfaction_rating_enabled;
4213 - if (sre !== 'on' && sre !== true && sre !== '1' && sre !== 1) return;
4214 -
4215 - // wp_localize_script stringifies ints, so accept both number and numeric string.
4216 - var idleRaw = mxchatChat.satisfaction_rating_idle_seconds;
4217 - var idleSeconds = (typeof idleRaw === 'number') ? idleRaw : parseInt(idleRaw, 10);
4218 - if (!isFinite(idleSeconds)) idleSeconds = 60;
4219 - if (idleSeconds < 5) idleSeconds = 5;
4220 - if (idleSeconds > 600) idleSeconds = 600;
4221 - var IDLE_MS = idleSeconds * 1000;
4222 - var MIN_BOT_REPLIES = 2;
4223 - var ratingState = {};
4224 -
4225 - function getState(botId) {
4226 - if (!ratingState[botId]) {
4227 - ratingState[botId] = { idleTimer: null, botReplies: 0, promptShown: false, dismissed: false };
4228 - }
4229 - return ratingState[botId];
4230 - }
4231 -
4232 - function getSessionId(botId) {
4233 - if (typeof MxChatInstances !== 'undefined' && MxChatInstances.getChatSession) {
4234 - return MxChatInstances.getChatSession(botId);
4235 - }
4236 - return null;
4237 - }
4238 -
4239 - function isAlreadyRated(sessionId) {
4240 - if (!sessionId) return false;
4241 - try { return localStorage.getItem('mxchat_rated:' + sessionId) === '1'; } catch (e) { return false; }
4242 - }
4243 -
4244 - function markRated(sessionId) {
4245 - if (!sessionId) return;
4246 - try { localStorage.setItem('mxchat_rated:' + sessionId, '1'); } catch (e) {}
4247 - }
4248 -
4249 - function esc(s) {
4250 - return String(s == null ? '' : s)
4251 - .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
4252 - .replace(/"/g, '&quot;').replace(/'/g, '&#039;');
4253 - }
4254 -
4255 - // Mirror shouldSkipInlineColors so rating bubbles defer to AI-theme CSS.
4256 - function ratingSkipInlineColors(botId) {
4257 - if (mxchatChat.skip_inline_colors) return true;
4258 - var botAssignments = mxchatChat.bot_theme_assignments || {};
4259 - return botAssignments.hasOwnProperty(botId);
4260 - }
4261 -
4262 - function botBubbleStyleAttr(botId) {
4263 - if (ratingSkipInlineColors(botId)) return '';
4264 - var bg = mxchatChat.bot_message_bg_color;
4265 - var fg = mxchatChat.bot_message_font_color;
4266 - if (!bg && !fg) return '';
4267 - return ' style="background-color: ' + esc(bg || '') + '; color: ' + esc(fg || '') + ';"';
4268 - }
4269 -
4270 - // Reads the rating bubble's actual computed fg+bg (whatever paints it —
4271 - // the inline color pickers OR the mxchat-theme AI customizer's injected CSS)
4272 - // and paints the filled "Send" pill so it fills with the bot font color and
4273 - // labels in the bubble bg. Mirrors mxchatSyncMenuColors(~:1512) for the read.
4274 - // We paint the submit button DIRECTLY (inline longhand) rather than relying
4275 - // on the CSS rule's var()s: Chromium resolves an INHERITED custom property
4276 - // unreliably inside a descendant's `background`, so a bubble-level var would
4277 - // silently fall back to the literal (white-block bug all over again). Inline
4278 - // longhand always wins. Same transparent-guard as the menu so we never paint
4279 - // a see-through value — in that case the CSS literal fallbacks keep it legible.
4280 - function syncRatingBubbleColors(botId) {
4281 - var $chatBox = getChatBoxByBotId(botId);
4282 - if (!$chatBox || !$chatBox.length) return;
4283 - var bubbleEl = $chatBox.find('.mxchat-rating-bot-bubble').last()[0];
4284 - if (!bubbleEl) return;
4285 - var cs = window.getComputedStyle(bubbleEl);
4286 - var fg = cs.color;
4287 - var bg = cs.backgroundColor;
4288 - var hasFg = fg && fg !== 'rgba(0, 0, 0, 0)' && fg !== 'transparent';
4289 - var hasBg = bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent';
4290 - // Expose on the bubble too, for any inheriting styles / future use.
4291 - if (hasFg) bubbleEl.style.setProperty('--mxchat-bot-fg', fg);
4292 - if (hasBg) bubbleEl.style.setProperty('--mxchat-bot-bg', bg);
4293 - // Paint the Send pill directly — the part that actually fixes the bug.
4294 - var submitEl = bubbleEl.querySelector('.mxchat-rating-submit');
4295 - if (submitEl) {
4296 - if (hasFg) submitEl.style.backgroundColor = fg; // fill = bot font color
4297 - if (hasBg) submitEl.style.color = bg; // label = bubble background
4298 - }
4299 - }
4300 -
4301 - function copy(key) {
4302 - var c = mxchatChat.satisfaction_rating_copy || {};
4303 - var d = {
4304 - question: 'Was this helpful?',
4305 - helpful: 'Helpful',
4306 - not_helpful: 'Not helpful',
4307 - dismiss: 'Dismiss',
4308 - thanks: 'Thanks! Anything we should improve? (optional)',
4309 - placeholder: 'Tell us what could be better…',
4310 - send: 'Send',
4311 - skip: 'Skip',
4312 - saved: 'Thanks for the feedback.'
4313 - };
4314 - return c[key] || d[key];
4315 - }
4316 -
4317 - function thumbUpSvg() {
4318 - return '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" aria-hidden="true"><path d="M7.493 18.75c-.425 0-.82-.236-.975-.632A7.48 7.48 0 0 1 6 15.375c0-1.75.599-3.358 1.602-4.634.151-.192.373-.309.6-.397.473-.183.89-.514 1.212-.924a9.042 9.042 0 0 1 2.861-2.4c.723-.384 1.35-.956 1.653-1.715a4.498 4.498 0 0 0 .322-1.672V2.75A.75.75 0 0 1 15 2a2.25 2.25 0 0 1 2.25 2.25c0 1.152-.26 2.243-.723 3.218-.266.558.107 1.282.725 1.282h3.126c1.026 0 1.945.694 2.054 1.715.045.422.068.85.068 1.285a11.95 11.95 0 0 1-2.649 7.521c-.388.482-.987.729-1.605.729H14.23c-.483 0-.964-.078-1.423-.23l-3.114-1.04a4.501 4.501 0 0 0-1.423-.23h-.777Z"/><path d="M2.331 10.977a11.969 11.969 0 0 0-.831 4.398 12 12 0 0 0 .52 3.507c.26.85 1.084 1.368 1.973 1.368H4.9c.445 0 .72-.498.523-.898a8.963 8.963 0 0 1-.924-3.977c0-1.708.476-3.305 1.302-4.666.245-.403-.028-.959-.5-.959H4.25c-.832 0-1.612.453-1.918 1.227Z"/></svg>';
4319 - }
4320 - function thumbDownSvg() {
4321 - return '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" aria-hidden="true"><path d="M15.73 5.25h1.035A7.465 7.465 0 0 1 18 9.375a7.465 7.465 0 0 1-1.235 4.125h-.148c-.806 0-1.534.446-2.031 1.08a9.04 9.04 0 0 1-2.861 2.4c-.723.384-1.35.956-1.653 1.715a4.498 4.498 0 0 0-.322 1.672V21a.75.75 0 0 1-.75.75 2.25 2.25 0 0 1-2.25-2.25c0-1.152.26-2.243.723-3.218.266-.558-.107-1.282-.725-1.282H3.622c-1.026 0-1.945-.694-2.054-1.715A12.137 12.137 0 0 1 1.5 12c0-2.848.992-5.464 2.649-7.521C4.537 3.997 5.136 3.75 5.754 3.75h4.541c.483 0 .964.078 1.423.23l3.114 1.04c.46.152.94.23 1.423.23Z"/><path d="M21.669 13.023c.536-1.362.831-2.845.831-4.398 0-1.22-.182-2.398-.52-3.507-.26-.85-1.084-1.368-1.973-1.368H19.1c-.445 0-.72.498-.523.898.591 1.2.924 2.55.924 3.977a8.958 8.958 0 0 1-1.302 4.666c-.245.403.028.959.5.959h1.053c.832 0 1.612-.453 1.918-1.227Z"/></svg>';
4322 - }
4323 -
4324 - function buildPromptHtml(botId) {
4325 - var styleAttr = botBubbleStyleAttr(botId);
4326 - return ''
4327 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4328 - + '<div class="mxchat-rating-prompt" data-bot-id="' + esc(botId) + '" role="group" aria-label="' + esc(copy('question')) + '">'
4329 - + '<div class="mxchat-rating-question">' + esc(copy('question')) + '</div>'
4330 - + '<div class="mxchat-rating-actions">'
4331 - + '<span class="mxchat-rating-buttons">'
4332 - + '<button type="button" class="mxchat-rating-btn" data-rating="1" aria-label="' + esc(copy('helpful')) + '">' + thumbUpSvg() + '</button>'
4333 - + '<button type="button" class="mxchat-rating-btn" data-rating="-1" aria-label="' + esc(copy('not_helpful')) + '">' + thumbDownSvg() + '</button>'
4334 - + '</span>'
4335 - + '<button type="button" class="mxchat-rating-dismiss" aria-label="' + esc(copy('dismiss')) + '">×</button>'
4336 - + '</div>'
4337 - + '</div>'
4338 - + '</div>';
4339 - }
4340 -
4341 - function buildFeedbackHtml(botId, rating) {
4342 - var styleAttr = botBubbleStyleAttr(botId);
4343 - return ''
4344 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4345 - + '<div class="mxchat-rating-feedback" data-bot-id="' + esc(botId) + '" data-rating="' + esc(String(rating)) + '">'
4346 - + '<div class="mxchat-rating-feedback-label">' + esc(copy('thanks')) + '</div>'
4347 - + '<textarea class="mxchat-rating-feedback-input" maxlength="500" placeholder="' + esc(copy('placeholder')) + '" rows="2"></textarea>'
4348 - + '<div class="mxchat-rating-feedback-actions">'
4349 - + '<button type="button" class="mxchat-rating-skip">' + esc(copy('skip')) + '</button>'
4350 - + '<button type="button" class="mxchat-rating-submit">' + esc(copy('send')) + '</button>'
4351 - + '</div>'
4352 - + '</div>'
4353 - + '</div>';
4354 - }
4355 -
4356 - function buildSavedHtml(botId) {
4357 - var styleAttr = botBubbleStyleAttr(botId);
4358 - return ''
4359 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4360 - + '<div class="mxchat-rating-saved">' + esc(copy('saved')) + '</div>'
4361 - + '</div>';
4362 - }
4363 -
4364 - function getChatBoxByBotId(botId) {
4365 - var $byId = $('#chat-box-' + botId);
4366 - if ($byId.length) return $byId.first();
4367 - return $('.chat-box').first();
4368 - }
4369 -
4370 - function scrollChatBoxToBottom($chatBox) {
4371 - if (!$chatBox || !$chatBox.length) return;
4372 - $chatBox.scrollTop($chatBox[0].scrollHeight);
4373 - }
4374 -
4375 - function showPrompt(botId) {
4376 - var s = getState(botId);
4377 - if (s.promptShown || s.dismissed) return;
4378 - var sessionId = getSessionId(botId);
4379 - if (!sessionId) return;
4380 - if (isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4381 - var $chatBox = getChatBoxByBotId(botId);
4382 - if (!$chatBox.length) return;
4383 - if ($chatBox.find('.mxchat-rating-prompt').length) { s.promptShown = true; return; }
4384 - $chatBox.append(buildPromptHtml(botId));
4385 - syncRatingBubbleColors(botId);
4386 - s.promptShown = true;
4387 - scrollChatBoxToBottom($chatBox);
4388 - }
4389 -
4390 - function submitRating(botId, rating, feedback) {
4391 - var sessionId = getSessionId(botId);
4392 - if (!sessionId) return;
4393 - $.post(mxchatChat.ajax_url, {
4394 - action: 'mxchat_save_rating',
4395 - session_id: sessionId,
4396 - bot_id: botId,
4397 - rating: rating,
4398 - feedback: feedback || ''
4399 - });
4400 - markRated(sessionId);
4401 - }
4402 -
4403 - function onBotReply(botId) {
4404 - var s = getState(botId);
4405 - s.botReplies += 1;
4406 - if (s.promptShown || s.dismissed) return;
4407 - var sessionId = getSessionId(botId);
4408 - if (sessionId && isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4409 - if (s.botReplies < MIN_BOT_REPLIES) return;
4410 - if (s.idleTimer) clearTimeout(s.idleTimer);
4411 - s.idleTimer = setTimeout(function() { showPrompt(botId); }, IDLE_MS);
4412 - }
4413 -
4414 - function onUserMessage(botId) {
4415 - var s = getState(botId);
4416 - if (s.idleTimer) { clearTimeout(s.idleTimer); s.idleTimer = null; }
4417 - }
4418 -
4419 - function botIdFromChatBox(el) {
4420 - var id = el && el.id ? el.id : '';
4421 - return id.indexOf('chat-box-') === 0 ? id.substring('chat-box-'.length) : 'default';
4422 - }
4423 -
4424 - function setupObserver(chatBox) {
4425 - var botId = botIdFromChatBox(chatBox);
4426 - try {
4427 - var observer = new MutationObserver(function(mutations) {
4428 - mutations.forEach(function(m) {
4429 - for (var i = 0; i < m.addedNodes.length; i++) {
4430 - var node = m.addedNodes[i];
4431 - if (!node || node.nodeType !== 1) continue;
4432 - var $n = $(node);
4433 - if ($n.hasClass('mxchat-rating-bot-bubble') || $n.hasClass('mxchat-rating-prompt') || $n.hasClass('mxchat-rating-feedback') || $n.hasClass('mxchat-rating-saved')) continue;
4434 - if ($n.hasClass('bot-message')) onBotReply(botId); // count at insert time — streaming providers append with .temporary-message first, then remove later (childList observer can't see attr changes)
4435 - else if ($n.hasClass('user-message')) onUserMessage(botId);
4436 - }
4437 - });
4438 - });
4439 - observer.observe(chatBox, { childList: true });
4440 - } catch (e) { /* noop */ }
4441 - }
4442 -
4443 - $('.chat-box').each(function() { setupObserver(this); });
4444 -
4445 - $(document).on('click', '.mxchat-rating-btn', function(e) {
4446 - e.preventDefault();
4447 - var $btn = $(this);
4448 - var $prompt = $btn.closest('.mxchat-rating-prompt');
4449 - var $wrap = $btn.closest('.mxchat-rating-bot-bubble');
4450 - var botId = $prompt.data('bot-id') || 'default';
4451 - var rating = parseInt($btn.attr('data-rating'), 10);
4452 - if (rating !== 1 && rating !== -1) return;
4453 - submitRating(botId, rating, '');
4454 - ($wrap.length ? $wrap : $prompt).replaceWith(buildFeedbackHtml(botId, rating));
4455 - syncRatingBubbleColors(botId);
4456 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4457 - });
4458 -
4459 - $(document).on('click', '.mxchat-rating-dismiss', function(e) {
4460 - e.preventDefault();
4461 - var $prompt = $(this).closest('.mxchat-rating-prompt');
4462 - var $wrap = $(this).closest('.mxchat-rating-bot-bubble');
4463 - var botId = $prompt.data('bot-id') || 'default';
4464 - var s = getState(botId);
4465 - s.dismissed = true;
4466 - markRated(getSessionId(botId));
4467 - ($wrap.length ? $wrap : $prompt).remove();
4468 - });
4469 -
4470 - function closeFeedback($fb) {
4471 - var botId = $fb.data('bot-id') || 'default';
4472 - var $wrap = $fb.closest('.mxchat-rating-bot-bubble');
4473 - ($wrap.length ? $wrap : $fb).replaceWith(buildSavedHtml(botId));
4474 - syncRatingBubbleColors(botId);
4475 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4476 - }
4477 -
4478 - $(document).on('click', '.mxchat-rating-skip', function(e) {
4479 - e.preventDefault();
4480 - closeFeedback($(this).closest('.mxchat-rating-feedback'));
4481 - });
4482 -
4483 - $(document).on('click', '.mxchat-rating-submit', function(e) {
4484 - e.preventDefault();
4485 - var $fb = $(this).closest('.mxchat-rating-feedback');
4486 - var botId = $fb.data('bot-id') || 'default';
4487 - var rating = parseInt($fb.attr('data-rating'), 10);
4488 - if (rating !== 1 && rating !== -1) { closeFeedback($fb); return; }
4489 - var text = String($fb.find('.mxchat-rating-feedback-input').val() || '').trim();
4490 - if (text !== '') {
4491 - submitRating(botId, rating, text);
4492 - }
4493 - closeFeedback($fb);
4494 - });
4495 3271 });
4496 3272