PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.0.2
MxChat – AI Chatbot & Content Generation for WordPress v3.0.2
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 +514 -1499 3.2.83.0.2 View file →
@@ -1,111 +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 3 // ====================================
109 4 // MULTI-INSTANCE MANAGEMENT SYSTEM
110 5 // ====================================
111 6
@@ -115,15 +10,11 @@
115 10
116 11 // Initialize an instance for a bot
117 12 init: function(botId) {
118 13 if (!this.instances[botId]) {
119 - // When persistence is OFF, track when this session started
120 - // so the AI only sees messages from this page load
121 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
122 -
123 14 this.instances[botId] = {
124 15 botId: botId,
125 - sessionId: null,
16 + sessionId: this.getChatSession(botId),
126 17 lastSeenMessageId: '',
127 18 notificationCheckInterval: null,
128 19 pollingInterval: null,
129 20 processedMessageIds: new Set(),
@@ -129,11 +20,9 @@
129 20 processedMessageIds: new Set(),
130 21 activePdfFile: null,
131 22 activeWordFile: null,
132 23 chatHistoryLoaded: false,
133 - isStreaming: false,
134 - // Fresh context timestamp - only used when persistence is OFF
135 - sessionStartTimestamp: chatPersistenceEnabled ? 0 : Date.now()
24 + isStreaming: false
136 25 };
137 26 }
138 27 return this.instances[botId];
139 28 },
@@ -148,77 +37,23 @@
148 37 return Object.keys(this.instances);
149 38 },
150 39
151 40 // Session management per bot
152 - // Returns existing session ID from cookie or localStorage (with in-memory fallback),
153 - // or null if none exists. Does NOT create a new session — use ensureSession() for that.
154 41 getChatSession: function(botId) {
155 42 var cookieName = 'mxchat_session_id_' + botId;
156 - var storageKey = 'mxchat_session_id_' + botId;
157 43 var sessionId = getCookie(cookieName);
158 44
159 - // Fallback to localStorage if cookie is missing (e.g. cleared by browser/consent)
160 45 if (!sessionId) {
161 - try { sessionId = localStorage.getItem(storageKey); } catch (e) {}
46 + sessionId = generateSessionId();
47 + this.setChatSession(botId, sessionId);
162 48 }
163 49
164 - // Fallback to in-memory instance when cookie AND localStorage are both blocked
165 - // (Safari ITP, strict tracking prevention, cross-origin iframes with partitioned
166 - // storage). Without this, ensureSession() can generate and store an ID that
167 - // getChatSession() then can't read back, causing null session_ids on send.
168 - if (!sessionId && this.instances[botId] && this.instances[botId].sessionId) {
169 - sessionId = this.instances[botId].sessionId;
170 - }
171 -
172 - // Guard against stored sentinel values that indicate earlier broken writes.
173 - if (sessionId === 'null' || sessionId === 'undefined') {
174 - sessionId = null;
175 - }
176 -
177 - // Re-sync cookie from localStorage if cookie was lost
178 - if (sessionId && !getCookie(cookieName)) {
179 - document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
180 - }
181 -
182 - return sessionId || null;
50 + return sessionId;
183 51 },
184 52
185 - // Lazy session initializer — called on first user interaction
186 - ensureSession: function(botId) {
187 - botId = botId || 'default';
188 - var instance = this.instances[botId] || this.init(botId);
189 -
190 - if (instance.sessionId) {
191 - return instance.sessionId;
192 - }
193 -
194 - // Check for existing session from cookie or localStorage
195 - var existingSession = this.getChatSession(botId);
196 -
197 - if (existingSession) {
198 - instance.sessionId = existingSession;
199 - } else {
200 - // Brand new session
201 - var newId = generateSessionId();
202 - this.setChatSession(botId, newId);
203 - instance.sessionId = newId;
204 - }
205 -
206 - // Now that we have a session, do the deferred work
207 - refreshNonceIfNeeded();
208 - trackOriginatingPage();
209 -
210 - // Note: loadChatHistory is handled by showChatContainerForBot with loader UI,
211 - // so we do NOT call it here to avoid a race condition.
212 -
213 - return instance.sessionId;
214 - },
215 -
216 53 setChatSession: function(botId, sessionId) {
217 54 var cookieName = 'mxchat_session_id_' + botId;
218 - var storageKey = 'mxchat_session_id_' + botId;
219 55 document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
220 - try { localStorage.setItem(storageKey, sessionId); } catch (e) {}
221 56 if (this.instances[botId]) {
222 57 this.instances[botId].sessionId = sessionId;
223 58 }
224 59 },
@@ -223,10 +58,8 @@
223 58 }
224 59 },
225 60
226 61 resetChatSession: function(botId) {
227 - // Clear old session from localStorage before setting new one
228 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
229 62 var newSessionId = generateSessionId();
230 63 this.setChatSession(botId, newSessionId);
231 64 var $chatBox = getElement(botId, 'chat-box');
232 65 if ($chatBox.length) {
@@ -235,20 +68,8 @@
235 68 if (this.instances[botId]) {
236 69 this.instances[botId].chatHistoryLoaded = false;
237 70 this.instances[botId].processedMessageIds = new Set();
238 71 }
239 - },
240 -
241 - // Silent reset — new session ID without clearing the chat UI
242 - // Used when IP changes mid-conversation so the user doesn't see messages vanish
243 - silentResetSession: function(botId) {
244 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
245 - var newSessionId = generateSessionId();
246 - this.setChatSession(botId, newSessionId);
247 - if (this.instances[botId]) {
248 - this.instances[botId].sessionId = newSessionId;
249 - }
250 - return newSessionId;
251 72 }
252 73 };
253 74
254 75 // ====================================
@@ -553,9 +374,8 @@
553 374
554 375 // Update your existing sendMessage function
555 376 function sendMessage(botId) {
556 377 botId = botId || 'default';
557 - MxChatInstances.ensureSession(botId);
558 378 var $chatInput = getElement(botId, 'chat-input');
559 379 var message = $chatInput.val();
560 380
561 381 // ADD PROMPT HOOK HERE
@@ -563,14 +383,10 @@
563 383 message = customMxChatFilter(message, "prompt");
564 384 }
565 385
566 386 if (message) {
567 - // Don't disable input in live agent mode - let users chat freely
568 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
569 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
570 - if (!isAgentMode) {
571 - disableChatInput(botId);
572 - }
387 + // Disable input while waiting for response
388 + disableChatInput(botId);
573 389
574 390 appendMessage("user", message, '', [], false, botId);
575 391 $chatInput.val('');
576 392 $chatInput.css('height', 'auto');
@@ -580,9 +396,9 @@
580 396 }
581 397 appendThinkingMessage(botId);
582 398 scrollToBottom(botId);
583 399
584 - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
400 + const currentModel = mxchatChat.model || 'gpt-4o';
585 401
586 402 // Check if streaming is enabled AND supported for this model
587 403 if (shouldUseStreaming(currentModel)) {
588 404 callMxChatStream(message, function(response) {
@@ -598,9 +414,8 @@
598 414
599 415 // Update your existing sendMessageToChatbot function
600 416 function sendMessageToChatbot(message, botId) {
601 417 botId = botId || 'default';
602 - MxChatInstances.ensureSession(botId);
603 418
604 419 // ADD PROMPT HOOK HERE
605 420 if (typeof customMxChatFilter === 'function') {
606 421 message = customMxChatFilter(message, "prompt");
@@ -605,14 +420,10 @@
605 420 if (typeof customMxChatFilter === 'function') {
606 421 message = customMxChatFilter(message, "prompt");
607 422 }
608 423
609 - // Don't disable input in live agent mode - let users chat freely
610 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
611 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
612 - if (!isAgentMode) {
613 - disableChatInput(botId);
614 - }
424 + // Disable input while waiting for response
425 + disableChatInput(botId);
615 426
616 427 var sessionId = getChatSession(botId);
617 428
618 429 if (hasQuickQuestions(botId)) {
@@ -620,9 +431,9 @@
620 431 }
621 432 appendThinkingMessage(botId);
622 433 scrollToBottom(botId);
623 434
624 - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
435 + const currentModel = mxchatChat.model || 'gpt-4o';
625 436
626 437 // Check if streaming is enabled AND supported for this model
627 438 if (shouldUseStreaming(currentModel)) {
628 439 callMxChatStream(message, function(response) {
@@ -697,44 +508,24 @@
697 508
698 509 // Get page context if contextual awareness is enabled
699 510 const pageContext = getPageContext();
700 511
701 - // Get instance for session start timestamp (used when persistence is OFF)
702 - var instance = MxChatInstances.get(botId);
703 -
704 - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent
705 - // and returns the guaranteed-present session id from the in-memory instance even when
706 - // cookie/localStorage writes are silently blocked by the browser.
707 - var sessionId = MxChatInstances.ensureSession(botId);
708 - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') {
709 - // Last-resort generation to ensure we never POST a null marker.
710 - sessionId = generateSessionId();
711 - MxChatInstances.setChatSession(botId, sessionId);
712 - }
713 -
714 - // Wait for the page-cache nonce refresh to complete before firing the
715 - // chat-send AJAX. On cached pages the inline mxchatChat.nonce is stale
716 - // until refreshNonceIfNeeded() returns; constructing ajaxData inside the
717 - // callback guarantees we read the fresh value. See plan-c5457f.
718 - refreshNonceIfNeeded(function() {
719 512 // Prepare AJAX data
720 513 const ajaxData = {
721 514 action: 'mxchat_handle_chat_request',
722 515 message: message,
723 - session_id: sessionId,
516 + session_id: getChatSession(botId),
724 517 nonce: mxchatChat.nonce,
725 518 current_page_url: window.location.href,
726 519 current_page_title: document.title,
727 - bot_id: botId,
728 - // Pass session start timestamp so AI context matches what user sees
729 - session_start_timestamp: instance.sessionStartTimestamp || 0
520 + bot_id: botId
730 521 };
731 -
522 +
732 523 // Add page context if available
733 524 if (pageContext) {
734 525 ajaxData.page_context = JSON.stringify(pageContext);
735 526 }
736 -
527 +
737 528 // CHECK FOR VISION FLAGS AND ADD THEM
738 529 if (window.mxchatVisionProcessed) {
739 530 ajaxData.vision_processed = true;
740 531 ajaxData.original_user_message = window.mxchatOriginalMessage || message;
@@ -743,9 +534,9 @@
743 534 window.mxchatVisionProcessed = false;
744 535 window.mxchatOriginalMessage = null;
745 536 window.mxchatVisionImagesCount = 0;
746 537 }
747 -
538 +
748 539 $.ajax({
749 540 url: mxchatChat.ajax_url,
750 541 type: 'POST',
751 542 dataType: 'json',
@@ -783,16 +574,23 @@
783 574 errorMessage = "An error occurred. Please try again or contact support.";
784 575 }
785 576
786 577 // Handle session reset action (IP changed, session expired, etc.)
787 - // Silent reset — keep chat UI intact, just get a new session and retry
788 578 if (response.data && response.data.action === 'reset_session') {
789 - MxChatInstances.silentResetSession(botId);
790 - // Re-send the original message with the new session (user message is already displayed)
579 + // Clear the old session and generate a new one
580 + resetChatSession(botId);
581 + // Remove the temporary loading message
582 + getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
583 + // Re-send the original message with the new session
791 584 var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
792 585 if (originalMessage) {
793 586 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
794 - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
587 + // Re-add the user message and thinking indicator
588 + appendMessage("user", originalMessage, '', [], false, botId);
589 + appendThinkingMessage(botId);
590 + scrollToBottom(botId);
591 + // Determine whether to use streaming
592 + const currentModel = mxchatChat.model || 'gpt-4o';
795 593 if (shouldUseStreaming(currentModel)) {
796 594 callMxChatStream(originalMessage, function(response) {
797 595 getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message');
798 596 }, botId);
@@ -849,11 +647,9 @@
849 647 }
850 648
851 649 // Check for live agent response
852 650 if (response.success && response.data && response.data.status === 'waiting_for_agent') {
853 - removeThinkingDots(botId);
854 651 updateChatModeIndicator('agent', botId);
855 - enableChatInput(botId);
856 652 return;
857 653 }
858 654
859 655 // Handle the message and show notification if chat is hidden
@@ -886,13 +682,9 @@
886 682 $badge.show();
887 683 }
888 684 }
889 685 } else {
890 - var emptyMsg = "I received an empty response. Please try again or contact support if this persists.";
891 - if (response.vectorstore_error) {
892 - emptyMsg = "I received an empty response. Debug info: " + response.vectorstore_error;
893 - }
894 - replaceLastMessage("bot", emptyMsg, '', [], botId);
686 + replaceLastMessage("bot", "I received an empty response. Please try again or contact support if this persists.", '', [], botId);
895 687 }
896 688
897 689 if (response.message_id) {
898 690 var instance = MxChatInstances.get(botId);
@@ -934,9 +726,8 @@
934 726
935 727 replaceLastMessage("bot", errorMessage, '', [], botId);
936 728 }
937 729 });
938 - }); // refreshNonceIfNeeded
939 730 }
940 731
941 732 function callMxChatStream(message, callback, botId) {
942 733 botId = botId || getMxChatBotId();
@@ -943,9 +734,9 @@
943 734
944 735 // Store the message in case we need to retry after session reset
945 736 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', message);
946 737
947 - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
738 + const currentModel = mxchatChat.model || 'gpt-4o';
948 739 if (!isStreamingSupported(currentModel)) {
949 740 callMxChat(message, callback, botId);
950 741 return;
951 742 }
@@ -952,35 +743,17 @@
952 743
953 744 // Get page context if contextual awareness is enabled
954 745 const pageContext = getPageContext();
955 746
956 - // Get instance for session start timestamp (used when persistence is OFF)
957 - var instance = MxChatInstances.get(botId);
958 -
959 - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any
960 - // non-string value via String(), so passing `null` would POST the literal string "null"
961 - // and land in the transcripts table as a ghost session. ensureSession() always returns
962 - // a real string even when cookies/localStorage are blocked.
963 - var streamSessionId = MxChatInstances.ensureSession(botId);
964 - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') {
965 - streamSessionId = generateSessionId();
966 - MxChatInstances.setChatSession(botId, streamSessionId);
967 - }
968 -
969 - // Wait for the page-cache nonce refresh before constructing formData (which
970 - // captures mxchatChat.nonce by value). Mirrors callMxChat's wrapping. See plan-c5457f.
971 - refreshNonceIfNeeded(function() {
972 747 const formData = new FormData();
973 748 formData.append('action', 'mxchat_stream_chat');
974 749 formData.append('message', message);
975 - formData.append('session_id', streamSessionId);
750 + formData.append('session_id', getChatSession(botId));
976 751 formData.append('nonce', mxchatChat.nonce);
977 752 formData.append('current_page_url', window.location.href);
978 753 formData.append('current_page_title', document.title);
979 754 formData.append('bot_id', botId);
980 - // Pass session start timestamp so AI context matches what user sees
981 - formData.append('session_start_timestamp', instance.sessionStartTimestamp || 0);
982 -
755 +
983 756 // Add page context if available
984 757 if (pageContext) {
985 758 formData.append('page_context', JSON.stringify(pageContext));
986 759 }
@@ -1070,19 +843,8 @@
1070 843 });
1071 844 return;
1072 845 }
1073 846
1074 - // Re-enable chat input when stream ends with content
1075 - enableChatInput(botId);
1076 -
1077 - // Scroll the user's last message to the top now that the
1078 - // bot's full reply has rendered (gives max reading room).
1079 - var $chatBoxDone = getElement(botId, 'chat-box');
1080 - var $lastUserMsgDone = $chatBoxDone.find('.user-message').last();
1081 - if ($lastUserMsgDone.length) {
1082 - scrollElementToTop($lastUserMsgDone, botId);
1083 - }
1084 -
1085 847 if (callback) {
1086 848 callback(accumulatedContent);
1087 849 }
1088 850 return;
@@ -1105,16 +867,8 @@
1105 867
1106 868 // Re-enable chat input after streaming completes
1107 869 enableChatInput(botId);
1108 870
1109 - // Scroll the user's last message to the top now
1110 - // that the bot's full reply has rendered.
1111 - var $chatBoxStreamDone = getElement(botId, 'chat-box');
1112 - var $lastUserMsgStreamDone = $chatBoxStreamDone.find('.user-message').last();
1113 - if ($lastUserMsgStreamDone.length) {
1114 - scrollElementToTop($lastUserMsgStreamDone, botId);
1115 - }
1116 -
1117 871 if (callback) {
1118 872 callback(accumulatedContent);
1119 873 }
1120 874 return;
@@ -1193,9 +947,8 @@
1193 947 getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1194 948 callMxChat(message, callback, botId);
1195 949 }
1196 950 });
1197 - }); // refreshNonceIfNeeded
1198 951 }
1199 952
1200 953 // Helper function to handle non-streaming responses
1201 954 function handleNonStreamResponse(data, callback, botId) {
@@ -1234,16 +987,21 @@
1234 987 errorMessage = "An error occurred. Please try again or contact support.";
1235 988 }
1236 989
1237 990 // Handle session reset action (IP changed, session expired, etc.)
1238 - // Silent reset — keep chat UI intact, just get a new session and retry
1239 991 if (data.data && data.data.action === 'reset_session') {
1240 - MxChatInstances.silentResetSession(botId);
1241 - // Re-send the original message with the new session (user message is already displayed)
992 + // Clear the old session and generate a new one
993 + resetChatSession(botId);
994 + // Re-send the original message with the new session
1242 995 var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
1243 996 if (originalMessage) {
1244 997 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
1245 - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
998 + // Re-add the user message and thinking indicator
999 + appendMessage("user", originalMessage, '', [], false, botId);
1000 + appendThinkingMessage(botId);
1001 + scrollToBottom(botId);
1002 + // Determine whether to use streaming
1003 + const currentModel = mxchatChat.model || 'gpt-4o';
1246 1004 if (shouldUseStreaming(currentModel)) {
1247 1005 callMxChatStream(originalMessage, callback, botId);
1248 1006 } else {
1249 1007 callMxChat(originalMessage, callback, botId);
@@ -1265,22 +1023,8 @@
1265 1023 }
1266 1024 return; // Exit early for errors
1267 1025 }
1268 1026
1269 - // Check for live agent response
1270 - if (data.success && data.data && data.data.status === 'waiting_for_agent') {
1271 - removeThinkingDots(botId);
1272 - // Also remove any leftover bot-message that lost its temporary-message class
1273 - var $chatBox = getElement(botId, 'chat-box');
1274 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
1275 - updateChatModeIndicator('agent', botId);
1276 - enableChatInput(botId);
1277 - if (callback) {
1278 - callback('');
1279 - }
1280 - return;
1281 - }
1282 -
1283 1027 // Handle different response formats
1284 1028 if (data.text || data.html || data.message) {
1285 1029
1286 1030 // Apply response hooks
@@ -1325,15 +1069,19 @@
1325 1069 }
1326 1070
1327 1071 // Enhanced updateChatModeIndicator function for immediate DOM updates
1328 1072 function updateChatModeIndicator(mode, botId) {
1073 + console.log('[MxChat] updateChatModeIndicator called with mode:', mode, 'botId:', botId);
1329 1074 botId = botId || 'default';
1330 1075 const indicator = getElementDOM(botId, 'chat-mode-indicator');
1076 + console.log('[MxChat] chat-mode-indicator element found:', !!indicator);
1331 1077 if (indicator) {
1332 1078 const oldText = indicator.textContent;
1079 + console.log('[MxChat] Current indicator text:', oldText, '-> changing to mode:', mode);
1333 1080
1334 1081 if (mode === 'agent') {
1335 1082 indicator.textContent = 'Live Agent';
1083 + console.log('[MxChat] Mode is agent, calling startPolling...');
1336 1084 startPolling(botId);
1337 1085 } else {
1338 1086 // Everything else is AI mode
1339 1087 const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
@@ -1404,12 +1152,9 @@
1404 1152 // Update the event handlers to use the correct function names (using event delegation)
1405 1153 // Use class-based selectors for multi-instance support
1406 1154 $(document).on('click', '.send-button', function() {
1407 1155 var botId = getBotIdFromElement(this);
1408 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1409 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1410 - disableChatInput(botId);
1411 - }
1156 + disableChatInput(botId);
1412 1157 sendMessage(botId);
1413 1158 });
1414 1159
1415 1160 // Override enter key handler (using event delegation)
@@ -1416,237 +1161,14 @@
1416 1161 $(document).on('keypress', '.chat-input', function(e) {
1417 1162 if (e.which == 13 && !e.shiftKey) {
1418 1163 e.preventDefault();
1419 1164 var botId = getBotIdFromElement(this);
1420 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1421 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1422 - disableChatInput(botId);
1423 - }
1165 + disableChatInput(botId);
1424 1166 sendMessage(botId);
1425 1167 }
1426 1168 });
1427 1169
1428 -// Builds the list of overflow-menu items for a given bot.
1429 -// Adding a future item is one push to this array — do NOT hardcode "only download."
1430 -function mxchatGetHeaderMenuItems(botId) {
1431 - var items = [];
1432 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1433 -
1434 - // The `print_button_*` keys still gate this item for back-compat with
1435 - // existing user options. The action is now a transcript download, not print.
1436 - if (settings.print_button_enabled === 'on') {
1437 - items.push({
1438 - id: 'download-transcript',
1439 - label: settings.print_button_label || 'Download Transcript',
1440 - 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>',
1441 - action: function() {
1442 - mxchatDownloadTranscript(botId);
1443 - }
1444 - });
1445 - }
1446 -
1447 - return items;
1448 -}
1449 -
1450 -// Builds a clean markdown transcript of the current conversation and triggers
1451 -// a file download. Used by the "Download Transcript" menu item.
1452 -function mxchatDownloadTranscript(botId) {
1453 - var $chatBox = getElement(botId, 'chat-box');
1454 - if (!$chatBox || !$chatBox.length) return;
1455 -
1456 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1457 - var headerTitle = settings.print_header_title || 'Chat transcript';
1458 - var now = new Date();
1459 - var stamp = now.toLocaleString();
1460 -
1461 - var lines = [];
1462 - lines.push('# ' + headerTitle);
1463 - lines.push('');
1464 - lines.push('Exported: ' + stamp);
1465 - lines.push('');
1466 - lines.push('---');
1467 - lines.push('');
1468 -
1469 - $chatBox.find('.user-message, .bot-message, .agent-message').each(function() {
1470 - var $msg = $(this);
1471 - // Skip thinking placeholders and any in-flight temporary messages.
1472 - if ($msg.find('.thinking-dots').length) return;
1473 - if ($msg.hasClass('temporary-message')) return;
1474 -
1475 - var sender;
1476 - if ($msg.hasClass('user-message')) sender = 'User';
1477 - else if ($msg.hasClass('agent-message')) sender = 'Live Agent';
1478 - else sender = 'AI Agent';
1479 -
1480 - // Strip interactive UI from the cloned message so we get the conversation text.
1481 - var $clone = $msg.clone();
1482 - $clone.find('.copy-button, .message-toolbar, .mxchat-copy, button, script, style').remove();
1483 - var text = $clone.text().replace(/ /g, ' ').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim();
1484 - if (!text) return;
1485 -
1486 - lines.push('**' + sender + '**');
1487 - lines.push('');
1488 - lines.push(text);
1489 - lines.push('');
1490 - });
1491 -
1492 - var content = lines.join('\n');
1493 - var iso = now.toISOString().replace(/[:.]/g, '-').slice(0, 19);
1494 - var fname = 'mxchat-transcript-' + iso + '.md';
1495 - var blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
1496 - var url = URL.createObjectURL(blob);
1497 - var a = document.createElement('a');
1498 - a.href = url;
1499 - a.download = fname;
1500 - a.style.display = 'none';
1501 - document.body.appendChild(a);
1502 - a.click();
1503 - setTimeout(function() {
1504 - if (a.parentNode) a.parentNode.removeChild(a);
1505 - URL.revokeObjectURL(url);
1506 - }, 100);
1507 -}
1508 -
1509 -// Reads the bot bubble's actual computed bg+fg and writes them as CSS vars
1510 -// on the menu wrap, so the dropdown matches whatever paints the bubble —
1511 -// saved options, AI theme CSS, or the mxchat-theme add-on.
1512 -function mxchatSyncMenuColors(botId, $wrap) {
1513 - if (!$wrap || !$wrap.length) return;
1514 - var $bot = $wrap.closest('.mxchat-chatbot-wrapper').find('.bot-message').not('.temporary-message').first();
1515 - if (!$bot.length) return;
1516 - var cs = window.getComputedStyle($bot[0]);
1517 - if (cs.backgroundColor && cs.backgroundColor !== 'rgba(0, 0, 0, 0)' && cs.backgroundColor !== 'transparent') {
1518 - $wrap[0].style.setProperty('--mxchat-menu-bg', cs.backgroundColor);
1519 - }
1520 - // Bot text color usually lives on a child div, not .bot-message itself.
1521 - var $textChild = $bot.find('[style*="color"]').first();
1522 - var fg = ($textChild.length ? window.getComputedStyle($textChild[0]).color : cs.color);
1523 - if (fg) $wrap[0].style.setProperty('--mxchat-menu-fg', fg);
1524 -}
1525 -
1526 -// One-time per-widget init: renders menu items, wires open/close,
1527 -// outside-click, Escape, and arrow-key navigation. If no items, hides the trigger.
1528 -function mxchatInitHeaderMenu(botId) {
1529 - var $wrap = $('.mxchat-header-menu-wrap[data-bot-id="' + botId + '"]').first();
1530 - if (!$wrap.length || $wrap.data('mxchatMenuReady')) return;
1531 -
1532 - var $trigger = $wrap.find('.mxchat-menu-trigger');
1533 - var $menu = $wrap.find('.mxchat-header-menu');
1534 - var items = mxchatGetHeaderMenuItems(botId);
1535 -
1536 - // Initial color sync — covers normal page load.
1537 - mxchatSyncMenuColors(botId, $wrap);
1538 -
1539 - if (!items.length) {
1540 - $trigger.hide();
1541 - $menu.hide();
1542 - $wrap.data('mxchatMenuReady', true);
1543 - return;
1544 - }
1545 -
1546 - // Build the menu items.
1547 - $menu.empty();
1548 - items.forEach(function(item, idx) {
1549 - var $btn = $('<button>', {
1550 - type: 'button',
1551 - 'class': 'mxchat-menu-item',
1552 - 'role': 'menuitem',
1553 - 'tabindex': '-1',
1554 - 'data-menu-id': item.id,
1555 - html: '<span class="mxchat-menu-item-icon">' + item.icon + '</span>' +
1556 - '<span class="mxchat-menu-item-label"></span>'
1557 - });
1558 - $btn.find('.mxchat-menu-item-label').text(item.label);
1559 - $btn.on('click', function(e) {
1560 - e.preventDefault();
1561 - e.stopPropagation();
1562 - closeMenu();
1563 - try { item.action(); } catch (err) { /* no-op */ }
1564 - });
1565 - $menu.append($btn);
1566 - });
1567 -
1568 - function openMenu() {
1569 - // Re-sync each open in case the active theme changed since init.
1570 - mxchatSyncMenuColors(botId, $wrap);
1571 - $menu.prop('hidden', false).attr('aria-hidden', 'false').addClass('is-open');
1572 - $trigger.attr('aria-expanded', 'true');
1573 - // Focus the first item for keyboard users
1574 - setTimeout(function() {
1575 - $menu.find('.mxchat-menu-item').first().attr('tabindex', '0').trigger('focus');
1576 - }, 0);
1577 - }
1578 - function closeMenu(returnFocus) {
1579 - $menu.prop('hidden', true).attr('aria-hidden', 'true').removeClass('is-open');
1580 - $trigger.attr('aria-expanded', 'false');
1581 - $menu.find('.mxchat-menu-item').attr('tabindex', '-1');
1582 - if (returnFocus) $trigger.trigger('focus');
1583 - }
1584 -
1585 - // Toggle on trigger click — stop propagation so the .chatbot-top-bar
1586 - // click-to-collapse handler does not fire.
1587 - $trigger.on('click', function(e) {
1588 - e.preventDefault();
1589 - e.stopPropagation();
1590 - if ($menu.hasClass('is-open')) closeMenu();
1591 - else openMenu();
1592 - });
1593 -
1594 - // Don't let clicks inside the menu bubble to the top-bar collapse handler.
1595 - $menu.on('click', function(e) {
1596 - e.stopPropagation();
1597 - });
1598 -
1599 - // Outside click closes the menu.
1600 - $(document).on('click.mxchatMenu-' + botId, function(e) {
1601 - if (!$menu.hasClass('is-open')) return;
1602 - if ($wrap.has(e.target).length || $wrap.is(e.target)) return;
1603 - closeMenu();
1604 - });
1605 -
1606 - // Keyboard: Escape closes and returns focus; arrow keys move focus; Enter activates.
1607 - $menu.on('keydown', '.mxchat-menu-item', function(e) {
1608 - var $items = $menu.find('.mxchat-menu-item');
1609 - var idx = $items.index(this);
1610 - if (e.key === 'Escape') {
1611 - e.preventDefault();
1612 - closeMenu(true);
1613 - } else if (e.key === 'ArrowDown') {
1614 - e.preventDefault();
1615 - var $next = $items.eq((idx + 1) % $items.length);
1616 - $items.attr('tabindex', '-1');
1617 - $next.attr('tabindex', '0').trigger('focus');
1618 - } else if (e.key === 'ArrowUp') {
1619 - e.preventDefault();
1620 - var $prev = $items.eq((idx - 1 + $items.length) % $items.length);
1621 - $items.attr('tabindex', '-1');
1622 - $prev.attr('tabindex', '0').trigger('focus');
1623 - } else if (e.key === 'Enter' || e.key === ' ') {
1624 - e.preventDefault();
1625 - $(this).trigger('click');
1626 - }
1627 - });
1628 - $trigger.on('keydown', function(e) {
1629 - if (e.key === 'Escape' && $menu.hasClass('is-open')) {
1630 - e.preventDefault();
1631 - closeMenu(true);
1632 - } else if ((e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') && !$menu.hasClass('is-open')) {
1633 - e.preventDefault();
1634 - openMenu();
1635 - }
1636 - });
1637 -
1638 - $wrap.data('mxchatMenuReady', true);
1639 -}
1640 -
1641 -// Initialize header menus for every rendered widget on DOM ready.
1642 -$(function() {
1643 - $('.mxchat-header-menu-wrap').each(function() {
1644 - var botId = $(this).data('bot-id');
1645 - if (botId) mxchatInitHeaderMenu(botId);
1646 - });
1647 -});
1648 -
1170 +
1649 1171 function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false, botId = 'default') {
1650 1172 try {
1651 1173 // Determine styles based on sender type
1652 1174 let messageClass, bgColor, fontColor;
@@ -1684,12 +1206,17 @@
1684 1206 'margin-bottom': '1em'
1685 1207 });
1686 1208 }
1687 1209
1688 - // Process the message content - always run linkify to convert markdown
1689 - // links and format text. linkify() handles existing HTML safely via
1690 - // negative lookaheads that skip URLs already inside <a> tags.
1691 - let fullMessage = linkify(messageText);
1210 + // Process the message content based on sender
1211 + let fullMessage;
1212 + if (sender === "user") {
1213 + // For user messages, apply linkify after sanitization
1214 + fullMessage = linkify(messageText);
1215 + } else {
1216 + // For bot/agent messages, preserve HTML
1217 + fullMessage = messageText;
1218 + }
1692 1219
1693 1220 // Add images if provided
1694 1221 if (images && images.length > 0) {
1695 1222 fullMessage += '<div class="image-gallery" dir="auto">';
@@ -1738,12 +1265,8 @@
1738 1265 if (lastUserMessage.length) {
1739 1266 scrollElementToTop(lastUserMessage, botId);
1740 1267 }
1741 1268 }
1742 -
1743 - if ((sender === "bot" || sender === "agent") && !isTemporary) {
1744 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
1745 - }
1746 1269 });
1747 1270
1748 1271 if (messageText.id) {
1749 1272 var instance = MxChatInstances.get(botId);
@@ -1828,12 +1351,26 @@
1828 1351 bgColor = botMessageBgColor;
1829 1352 fontColor = botMessageFontColor;
1830 1353 }
1831 1354
1832 - // Always run linkify to convert markdown links and format text.
1833 - // linkify() already handles existing HTML (its URL patterns use negative lookaheads
1834 - // to avoid double-processing URLs that are already inside <a> tags).
1835 - var fullMessage = linkify(responseText);
1355 + // FIXED: Only linkify if response doesn't already contain HTML links or tags
1356 + // This prevents double-processing of URLs that are already formatted as HTML
1357 + var fullMessage;
1358 + if (sender === "user") {
1359 + // Always linkify user messages (they're plain text)
1360 + fullMessage = linkify(responseText);
1361 + } else {
1362 + // For bot/agent messages, check if HTML already exists
1363 + if (responseText.includes('<a href=') || responseText.includes('</a>') ||
1364 + responseText.includes('<img') || responseText.includes('<div') ||
1365 + responseText.includes('<p>') || responseText.includes('<br>')) {
1366 + // Response already has HTML, don't process it
1367 + fullMessage = responseText;
1368 + } else {
1369 + // Plain text response, apply linkify
1370 + fullMessage = linkify(responseText);
1371 + }
1372 + }
1836 1373
1837 1374 if (responseHtml) {
1838 1375 // Only add line breaks if there's actual text content before the HTML
1839 1376 if (fullMessage && fullMessage.trim()) {
@@ -1890,12 +1427,8 @@
1890 1427 }
1891 1428
1892 1429 // Re-enable chat input after response is displayed
1893 1430 enableChatInput(botId);
1894 -
1895 - if (sender === "bot" || sender === "agent") {
1896 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
1897 - }
1898 1431 } else {
1899 1432 appendMessage(sender, responseText, responseHtml, images, false, botId);
1900 1433 // Re-enable chat input after response is displayed
1901 1434 enableChatInput(botId);
@@ -1904,15 +1437,8 @@
1904 1437
1905 1438
1906 1439 function appendThinkingMessage(botId) {
1907 1440 botId = botId || 'default';
1908 -
1909 - // Don't show thinking dots in live agent mode - message is just forwarded to a human
1910 - var indicator = getElementDOM(botId, 'chat-mode-indicator');
1911 - if (indicator && indicator.textContent === 'Live Agent') {
1912 - return;
1913 - }
1914 -
1915 1441 var $chatBox = getElement(botId, 'chat-box');
1916 1442
1917 1443 // Remove any existing thinking dots in this bot's chat first
1918 1444 $chatBox.find('.thinking-dots').remove();
@@ -1934,9 +1460,9 @@
1934 1460 '</div>' +
1935 1461 '</div>';
1936 1462
1937 1463 // Append the thinking dots to this bot's chat container - skip inline colors if AI theme is active
1938 - var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + '; color: ' + botMessageFontColor + ';"';
1464 + var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + ';"';
1939 1465 $chatBox.append('<div class="bot-message temporary-message"' + messageStyle + '>' + thinkingHtml + '</div>');
1940 1466 scrollToBottom(botId);
1941 1467 }
1942 1468
@@ -1942,11 +1468,9 @@
1942 1468
1943 1469 function removeThinkingDots(botId) {
1944 1470 botId = botId || 'default';
1945 1471 var $chatBox = getElement(botId, 'chat-box');
1946 - // Remove by temporary-message class first, then fall back to any bot-message containing thinking dots
1947 1472 $chatBox.find('.thinking-dots').closest('.temporary-message').remove();
1948 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
1949 1473 }
1950 1474
1951 1475 // ====================================
1952 1476 // TEXT FORMATTING & PROCESSING
@@ -1980,12 +1504,9 @@
1980 1504 processedText = formatTextStyling(processedText);
1981 1505
1982 1506 // Process code blocks BEFORE processing links
1983 1507 processedText = formatCodeBlocks(processedText);
1984 -
1985 - // Process markdown tables BEFORE converting newlines to paragraphs
1986 - processedText = formatMarkdownTables(processedText);
1987 -
1508 +
1988 1509 // NOW convert to paragraphs
1989 1510 processedText = convertNewlinesToBreaks(processedText);
1990 1511
1991 1512 // IMPORTANT: Handle citation-style brackets FIRST [URL]
@@ -1998,63 +1519,37 @@
1998 1519 // Return as a proper link without the brackets
1999 1520 return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2000 1521 });
2001 1522
2002 - // Process markdown links: [text](url) and [](url)
2003 - // Uses balanced parenthesis matching to handle URLs containing parens
2004 - // (e.g. PDF filenames with dates like (2025-08-28).pdf)
2005 - processedText = (function(input) {
2006 - var result = '';
2007 - var i = 0;
2008 - while (i < input.length) {
2009 - // Look for [ at current position
2010 - if (input[i] === '[') {
2011 - // Find closing ]
2012 - var closeBracket = input.indexOf(']', i + 1);
2013 - if (closeBracket === -1 || closeBracket + 1 >= input.length || input[closeBracket + 1] !== '(') {
2014 - result += input[i];
2015 - i++;
2016 - continue;
2017 - }
2018 - var linkText = input.substring(i + 1, closeBracket);
2019 - // Check if URL starts with http
2020 - var urlStart = closeBracket + 2;
2021 - if (!input.substring(urlStart).match(/^https?:\/\//)) {
2022 - result += input[i];
2023 - i++;
2024 - continue;
2025 - }
2026 - // Find balanced closing paren
2027 - var depth = 1;
2028 - var j = urlStart;
2029 - while (j < input.length && depth > 0) {
2030 - if (input[j] === '(') depth++;
2031 - else if (input[j] === ')') depth--;
2032 - if (depth > 0) j++;
2033 - }
2034 - if (depth !== 0) {
2035 - result += input[i];
2036 - i++;
2037 - continue;
2038 - }
2039 - var url = input.substring(urlStart, j);
2040 - var cleanUrl = url.replace(/[\].,;!?]+$/, '');
2041 - var encodedUrl = safeEncodeUrl(cleanUrl);
2042 - if (!linkText || !linkText.trim()) {
2043 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + cleanUrl + '</a>';
2044 - } else {
2045 - var safeText = sanitizeUserInput(linkText);
2046 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + safeText + '</a>';
2047 - }
2048 - i = j + 1; // Skip past the closing )
2049 - } else {
2050 - result += input[i];
2051 - i++;
2052 - }
1523 + // Process proper markdown links with text: [text](url)
1524 + // This MUST have non-empty text in the first brackets
1525 + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g;
1526 + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => {
1527 + // Make sure we have actual text (not just whitespace)
1528 + if (!text || !text.trim()) {
1529 + // If no text, treat the URL as the text
1530 + let cleanUrl = url.replace(/[.,;!?]+$/, '');
1531 + const safeUrl = safeEncodeUrl(cleanUrl);
1532 + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2053 1533 }
2054 - return result;
2055 - })(processedText);
1534 +
1535 + // Clean the URL
1536 + let cleanUrl = url.replace(/[\].,;!?]+$/, '');
1537 + const safeUrl = safeEncodeUrl(cleanUrl);
1538 + const safeText = sanitizeUserInput(text);
1539 + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`;
1540 + });
2056 1541
1542 + // Handle empty markdown links: [](url)
1543 + // This is a specific case where there's no text
1544 + const emptyMarkdownPattern = /\[\]\((https?:\/\/[^\s)]+)\)/g;
1545 + processedText = processedText.replace(emptyMarkdownPattern, (match, url) => {
1546 + let cleanUrl = url.replace(/[.,;!?]+$/, '');
1547 + const safeUrl = safeEncodeUrl(cleanUrl);
1548 + // Use the URL itself as the link text
1549 + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
1550 + });
1551 +
2057 1552 // Process phone numbers: [text](tel:number)
2058 1553 const phonePattern = /\[([^\]]+)\]\((tel:[\d+\-\s()]+)\)/g;
2059 1554 processedText = processedText.replace(phonePattern, (match, text, phone) => {
2060 1555 const safePhone = safeEncodeUrl(phone);
@@ -2206,78 +1701,9 @@
2206 1701 });
2207 1702
2208 1703 return text;
2209 1704 }
2210 -
2211 - function formatMarkdownTables(text) {
2212 - var lines = text.split('\n');
2213 - var result = [];
2214 - var i = 0;
2215 -
2216 - while (i < lines.length) {
2217 - // Check for a table: current line has pipes AND next line is a separator row
2218 - if (i + 1 < lines.length &&
2219 - lines[i].indexOf('|') !== -1 &&
2220 - /^\s*\|?[\s\-:]+(\|[\s\-:]+)+\|?\s*$/.test(lines[i + 1])) {
2221 -
2222 - var tableLines = [];
2223 - var headerLine = lines[i];
2224 - var separatorLine = lines[i + 1];
2225 - tableLines.push(headerLine);
2226 - tableLines.push(separatorLine);
2227 -
2228 - // Collect remaining table rows
2229 - var j = i + 2;
2230 - while (j < lines.length && lines[j].indexOf('|') !== -1 && lines[j].trim() !== '') {
2231 - tableLines.push(lines[j]);
2232 - j++;
2233 - }
2234 -
2235 - // Parse alignment from separator row
2236 - var sepCells = separatorLine.split('|').filter(function(c) { return c.trim() !== ''; });
2237 - var alignments = sepCells.map(function(cell) {
2238 - var trimmed = cell.trim();
2239 - if (trimmed.charAt(0) === ':' && trimmed.charAt(trimmed.length - 1) === ':') return 'center';
2240 - if (trimmed.charAt(trimmed.length - 1) === ':') return 'right';
2241 - return 'left';
2242 - });
2243 -
2244 - // Build HTML table
2245 - var html = '<div class="mxchat-table-wrapper"><table class="mxchat-table">';
2246 -
2247 - // Header row
2248 - var headerCells = tableLines[0].split('|').filter(function(c) { return c.trim() !== ''; });
2249 - html += '<thead><tr>';
2250 - headerCells.forEach(function(cell, idx) {
2251 - var align = alignments[idx] || 'left';
2252 - html += '<th style="text-align:' + align + '">' + cell.trim() + '</th>';
2253 - });
2254 - html += '</tr></thead>';
2255 -
2256 - // Body rows
2257 - html += '<tbody>';
2258 - for (var r = 2; r < tableLines.length; r++) {
2259 - var rowCells = tableLines[r].split('|').filter(function(c) { return c.trim() !== ''; });
2260 - html += '<tr>';
2261 - rowCells.forEach(function(cell, idx) {
2262 - var align = alignments[idx] || 'left';
2263 - html += '<td style="text-align:' + align + '">' + cell.trim() + '</td>';
2264 - });
2265 - html += '</tr>';
2266 - }
2267 - html += '</tbody></table></div>';
2268 -
2269 - result.push(html);
2270 - i = j;
2271 - } else {
2272 - result.push(lines[i]);
2273 - i++;
2274 - }
2275 - }
2276 -
2277 - return result.join('\n');
2278 - }
2279 -
1705 +
2280 1706 function sanitizeUserInput(text) {
2281 1707 const div = document.createElement('div');
2282 1708 div.textContent = text;
2283 1709 return div.innerHTML;
@@ -2348,14 +1774,13 @@
2348 1774 requestAnimationFrame(smoothScroll);
2349 1775 }
2350 1776 }
2351 1777
2352 - function scrollElementToTop(element, botId, topOffset) {
1778 + function scrollElementToTop(element, botId) {
2353 1779 botId = botId || 'default';
2354 - topOffset = (typeof topOffset === 'number') ? topOffset : 2;
2355 1780 var chatBox = getElement(botId, 'chat-box');
2356 1781 var elementTop = element.position().top + chatBox.scrollTop();
2357 - chatBox.animate({ scrollTop: Math.max(0, elementTop - topOffset) }, 500);
1782 + chatBox.animate({ scrollTop: elementTop }, 500);
2358 1783 }
2359 1784
2360 1785 function showChatWidget(botId) {
2361 1786 botId = botId || 'default';
@@ -2499,12 +1924,15 @@
2499 1924 // LIVE AGENT FUNCTIONALITY
2500 1925 // ====================================
2501 1926
2502 1927 function startPolling(botId) {
1928 + console.log('[MxChat] startPolling called for botId:', botId);
2503 1929 botId = botId || 'default';
2504 1930 var instance = MxChatInstances.get(botId);
2505 1931 // Clear any existing interval first
2506 1932 stopPolling(botId);
1933 + // Start new polling interval
1934 + console.log('[MxChat] Starting polling interval (5s) for botId:', botId);
2507 1935 instance.pollingInterval = setInterval(function() {
2508 1936 checkForAgentMessages(botId);
2509 1937 }, 5000);
2510 1938 }
@@ -2509,17 +1937,20 @@
2509 1937 }, 5000);
2510 1938 }
2511 1939
2512 1940 function stopPolling(botId) {
1941 + console.log('[MxChat] stopPolling called for botId:', botId);
2513 1942 botId = botId || 'default';
2514 1943 var instance = MxChatInstances.get(botId);
2515 1944 if (instance.pollingInterval) {
2516 1945 clearInterval(instance.pollingInterval);
2517 1946 instance.pollingInterval = null;
1947 + console.log('[MxChat] Polling stopped for botId:', botId);
2518 1948 }
2519 1949 }
2520 1950
2521 1951 function checkForAgentMessages(botId) {
1952 + console.log('[MxChat] checkForAgentMessages called for botId:', botId);
2522 1953 botId = botId || 'default';
2523 1954 var instance = MxChatInstances.get(botId);
2524 1955 const sessionId = getChatSession(botId);
2525 1956 $.ajax({
@@ -2545,12 +1976,8 @@
2545 1976 instance.processedMessageIds.add(message.id);
2546 1977 }
2547 1978 });
2548 1979
2549 - if (hasNewMessage) {
2550 - enableChatInput(botId);
2551 - }
2552 -
2553 1980 var $floatingChatbot = getElement(botId, 'floating-chatbot');
2554 1981 if (hasNewMessage && $floatingChatbot.hasClass('hidden')) {
2555 1982 showNotification(botId);
2556 1983 }
@@ -2556,13 +1983,8 @@
2556 1983 }
2557 1984
2558 1985 scrollToBottom(botId, true);
2559 1986 }
2560 -
2561 - // Handle chat mode transitions (e.g. agent ended chat via !endchat)
2562 - if (response.success && response.data?.chat_mode) {
2563 - updateChatModeIndicator(response.data.chat_mode, botId);
2564 - }
2565 1987 },
2566 1988 error: function (xhr, status, error) {
2567 1989 // Polling error - silently continue
2568 1990 }
@@ -2572,29 +1994,20 @@
2572 1994 // ====================================
2573 1995 // CHAT HISTORY & PERSISTENCE
2574 1996 // ====================================
2575 1997
2576 -function loadChatHistory(botId, onComplete) {
1998 +function loadChatHistory(botId) {
2577 1999 botId = botId || 'default';
2578 2000 var instance = MxChatInstances.get(botId);
2579 2001
2580 2002 // Prevent duplicate loading
2581 2003 if (instance.chatHistoryLoaded) {
2582 - if (onComplete) onComplete();
2583 2004 return;
2584 2005 }
2585 2006
2586 - // Use getChatSession which returns null if no session exists (does NOT create one)
2587 2007 var sessionId = getChatSession(botId);
2588 2008 var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
2589 2009
2590 - // No session yet — nothing to load. History will load after first message via ensureSession.
2591 - if (!sessionId) {
2592 - instance.chatHistoryLoaded = true;
2593 - if (onComplete) onComplete();
2594 - return;
2595 - }
2596 -
2597 2010 if (chatPersistenceEnabled && sessionId) {
2598 2011 $.ajax({
2599 2012 url: mxchatChat.ajax_url,
2600 2013 type: 'POST',
@@ -2605,12 +2018,11 @@
2605 2018 },
2606 2019 success: function(response) {
2607 2020 // Handle session reset (IP changed while user was away)
2608 2021 if (response.success === false && response.data && response.data.action === 'reset_session') {
2609 - // Silent reset — new session but don't clear UI
2610 - MxChatInstances.silentResetSession(botId);
2022 + // Silently reset session - user will start fresh
2023 + resetChatSession(botId);
2611 2024 instance.chatHistoryLoaded = true; // Prevent retry loop
2612 - if (onComplete) onComplete();
2613 2025 return;
2614 2026 }
2615 2027
2616 2028 // Check if the response indicates success
@@ -2666,19 +2078,9 @@
2666 2078 var content = message.content;
2667 2079 content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
2668 2080 content = decodeHTMLEntities(content);
2669 2081
2670 - // Skip linkify for messages containing structured HTML
2671 - // (forms, product cards, galleries, etc.) to avoid
2672 - // markdown formatting corrupting HTML attributes
2673 - // (e.g. underscores in name="field_name" becoming <em> tags)
2674 - if (content.includes("mxchat-product-card") ||
2675 - content.includes("mxchat-image-gallery") ||
2676 - content.includes("mxchat-featured-products") ||
2677 - content.includes("<form") ||
2678 - content.includes("<input") ||
2679 - content.includes("<select") ||
2680 - content.includes("<textarea")) {
2082 + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
2681 2083 messageElement.html(content);
2682 2084 } else {
2683 2085 var formattedContent = linkify(content);
2684 2086 messageElement.html(formattedContent);
@@ -2718,17 +2120,13 @@
2718 2120 instance.chatHistoryLoaded = true;
2719 2121 }
2720 2122 }
2721 2123 }
2722 - if (onComplete) onComplete();
2723 2124 },
2724 2125 error: function(xhr, status, error) {
2725 2126 // Error loading chat history - silently continue
2726 - if (onComplete) onComplete();
2727 2127 }
2728 2128 });
2729 - } else {
2730 - if (onComplete) onComplete();
2731 2129 }
2732 2130 }
2733 2131
2734 2132
@@ -2904,35 +2302,45 @@
2904 2302 // ====================================
2905 2303
2906 2304 function checkPreChatDismissal(botId) {
2907 2305 botId = botId || 'default';
2908 - try {
2909 - var dismissedAt = localStorage.getItem('mxchat_pre_chat_dismissed_' + botId);
2910 - if (dismissedAt) {
2911 - // Re-show after 24 hours
2912 - var elapsed = Date.now() - parseInt(dismissedAt, 10);
2913 - if (elapsed < 86400000) {
2306 + $.ajax({
2307 + url: mxchatChat.ajax_url,
2308 + type: 'POST',
2309 + data: {
2310 + action: 'mxchat_check_pre_chat_message_status',
2311 + _ajax_nonce: mxchatChat.nonce
2312 + },
2313 + success: function(response) {
2314 + if (response.success && !response.data.dismissed) {
2315 + getElement(botId, 'pre-chat-message').fadeIn(250);
2316 + } else {
2914 2317 getElement(botId, 'pre-chat-message').hide();
2915 - return;
2916 2318 }
2917 - // Expired — clear and show again
2918 - localStorage.removeItem('mxchat_pre_chat_dismissed_' + botId);
2319 + },
2320 + error: function() {
2321 + // Error checking pre-chat dismissal - silently continue
2919 2322 }
2920 - getElement(botId, 'pre-chat-message').fadeIn(250);
2921 - } catch (e) {
2922 - // localStorage unavailable — show the message
2923 - getElement(botId, 'pre-chat-message').fadeIn(250);
2924 - }
2323 + });
2925 2324 }
2926 2325
2927 2326 function handlePreChatDismissal(botId) {
2928 2327 botId = botId || 'default';
2929 2328 getElement(botId, 'pre-chat-message').fadeOut(200);
2930 - try {
2931 - localStorage.setItem('mxchat_pre_chat_dismissed_' + botId, String(Date.now()));
2932 - } catch (e) {
2933 - // localStorage unavailable — dismissal won't persist
2934 - }
2329 + $.ajax({
2330 + url: mxchatChat.ajax_url,
2331 + type: 'POST',
2332 + data: {
2333 + action: 'mxchat_dismiss_pre_chat_message',
2334 + _ajax_nonce: mxchatChat.nonce
2335 + },
2336 + success: function() {
2337 + $('#pre-chat-message').hide();
2338 + },
2339 + error: function() {
2340 + // Error dismissing pre-chat message - silently continue
2341 + }
2342 + });
2935 2343 }
2936 2344
2937 2345
2938 2346 // ====================================
@@ -2987,14 +2395,9 @@
2987 2395 collapseQuickQuestions(botId);
2988 2396 });
2989 2397
2990 2398 // Chatbot visibility toggle handlers - use class selector for multi-instance support
2991 - // Handles click + Enter/Space keypresses for keyboard accessibility (WCAG 2.1 SC 2.1.1).
2992 - $(document).on('click keydown', '.floating-chatbot-button', function(e) {
2993 - if (e.type === 'keydown') {
2994 - if (e.key !== 'Enter' && e.key !== ' ' && e.key !== 'Spacebar') return;
2995 - e.preventDefault();
2996 - }
2399 + $(document).on('click', '.floating-chatbot-button', function() {
2997 2400 var botId = getBotIdFromElement(this);
2998 2401 var $chatbot = getElement(botId, 'floating-chatbot');
2999 2402 var $badge = getElement(botId, 'chat-notification-badge');
3000 2403 var $preChat = getElement(botId, 'pre-chat-message');
@@ -2999,83 +2402,35 @@
2999 2402 var $badge = getElement(botId, 'chat-notification-badge');
3000 2403 var $preChat = getElement(botId, 'pre-chat-message');
3001 2404
3002 2405 if ($chatbot.hasClass('hidden')) {
3003 - $chatbot.removeClass('hidden').addClass('visible')
3004 - .attr('aria-modal', 'true').attr('role', 'dialog');
3005 - $(this).addClass('hidden').attr('aria-expanded', 'true');
2406 + $chatbot.removeClass('hidden').addClass('visible');
2407 + $(this).addClass('hidden');
3006 2408 $badge.hide(); // Hide notification when opening chat
3007 2409 disableScroll();
3008 2410 $preChat.fadeOut(250);
3009 -
3010 - // Load chat history for returning visitors (persistence)
3011 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3012 - if (chatPersistenceEnabled) {
3013 - MxChatInstances.ensureSession(botId);
3014 - }
3015 -
3016 - // Deferred email check — only on first widget open
3017 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3018 - var instance = MxChatInstances.get(botId);
3019 - if (emailBlocker && !instance.emailCheckDone) {
3020 - instance.emailCheckDone = true;
3021 - resolveEmailState(botId);
3022 - } else if (!emailBlocker) {
3023 - // No email collection — still route through showChatContainerForBot
3024 - // so the loader is shown while chat history loads
3025 - showChatContainerForBot(botId);
3026 - }
3027 -
3028 - // Move keyboard focus into the message input after the open transition.
3029 - setTimeout(function() {
3030 - var chatInput = getElementDOM(botId, 'chat-input');
3031 - if (chatInput && !chatInput.disabled) {
3032 - try { chatInput.focus({ preventScroll: true }); } catch (err) { chatInput.focus(); }
3033 - }
3034 - }, 300);
3035 2411 } else {
3036 - $chatbot.removeClass('visible').addClass('hidden').removeAttr('aria-modal');
3037 - $(this).removeClass('hidden').attr('aria-expanded', 'false');
2412 + $chatbot.removeClass('visible').addClass('hidden');
2413 + $(this).removeClass('hidden');
3038 2414 enableScroll();
3039 2415 checkPreChatDismissal(botId);
3040 2416 }
3041 2417 });
3042 2418
3043 - // Allow clicking anywhere on the title bar to close the chatbot.
3044 - // Returns keyboard focus to the launcher so keyboard users don't get
3045 - // stranded at <body> (WCAG SC 2.4.3 Focus Order). :focus-visible is
3046 - // heuristic-based so mouse-triggered close won't show a focus ring.
2419 + // Allow clicking anywhere on the title bar to close the chatbot
3047 2420 $(document).on('click', '.chatbot-top-bar', function() {
3048 2421 var botId = getBotIdFromElement(this);
3049 - getElement(botId, 'floating-chatbot').addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3050 - var $launcher = getElement(botId, 'floating-chatbot-button');
3051 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
2422 + getElement(botId, 'floating-chatbot').addClass('hidden').removeClass('visible');
2423 + getElement(botId, 'floating-chatbot-button').removeClass('hidden');
3052 2424 enableScroll();
3053 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3054 2425 });
3055 2426
3056 - // Global Escape-key handler — closes any visible chat widget and
3057 - // returns focus to its launcher. Standard modal-dismissal pattern;
3058 - // pairs with aria-modal="true" set on the widget when it opens.
3059 - $(document).on('keydown', function(e) {
3060 - if (e.key !== 'Escape' && e.key !== 'Esc') return;
3061 - var $visible = $('.floating-chatbot.visible');
3062 - if (!$visible.length) return;
3063 - e.preventDefault();
3064 - $visible.each(function() {
3065 - var botId = getBotIdFromElement(this);
3066 - $(this).addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3067 - var $launcher = getElement(botId, 'floating-chatbot-button');
3068 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
3069 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3070 - });
3071 - enableScroll();
3072 - });
3073 -
3074 2427 $(document).on('click', '.close-pre-chat-message', function(e) {
3075 2428 e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
3076 2429 var botId = getBotIdFromElement(this);
3077 - handlePreChatDismissal(botId);
2430 + getElement(botId, 'pre-chat-message').fadeOut(200, function() {
2431 + $(this).remove();
2432 + });
3078 2433 });
3079 2434
3080 2435
3081 2436 // PDF upload button handlers - use class selector
@@ -3116,10 +2471,8 @@
3116 2471 const sendBtn = document.getElementById('send-button');
3117 2472 const originalBtnContent = uploadBtn.innerHTML;
3118 2473
3119 2474 try {
3120 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3121 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3122 2475 const formData = new FormData();
3123 2476 formData.append('action', 'mxchat_upload_pdf');
3124 2477 formData.append('pdf_file', file);
3125 2478 formData.append('session_id', sessionId);
@@ -3183,10 +2536,8 @@
3183 2536 const sendBtn = document.getElementById('send-button');
3184 2537 const originalBtnContent = uploadBtn.innerHTML;
3185 2538
3186 2539 try {
3187 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3188 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3189 2540 const formData = new FormData();
3190 2541 formData.append('action', 'mxchat_upload_word');
3191 2542 formData.append('word_file', file);
3192 2543 formData.append('session_id', sessionId);
@@ -3280,437 +2631,380 @@
3280 2631 });
3281 2632
3282 2633
3283 2634 // ====================================
3284 -// INIT LOADER & CHAT CONTAINER HELPERS
2635 +// EMAIL COLLECTION SETUP - FIXED VERSION
3285 2636 // ====================================
3286 -// These must be outside the email collection block so they're always available
3287 -// (used by persistence loading even when email collection is off)
3288 -
3289 -function showInitLoader(botId) {
3290 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3291 - if (loader) loader.style.display = 'flex';
3292 -}
3293 -
3294 -function hideInitLoader(botId) {
3295 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3296 - if (loader) loader.style.display = 'none';
3297 -}
3298 -
3299 -function showEmailFormForBot(botId) {
3300 - hideInitLoader(botId);
3301 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3302 - var chatContainer = getElementDOM(botId, 'chat-container');
3303 - if (emailBlocker) emailBlocker.style.display = 'flex';
3304 - if (chatContainer) chatContainer.style.display = 'none';
3305 -}
3306 -
3307 -function showChatContainerForBot(botId) {
3308 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3309 - var chatContainer = getElementDOM(botId, 'chat-container');
3310 - if (emailBlocker) emailBlocker.style.display = 'none';
3311 -
3312 - var instance = MxChatInstances.get(botId);
3313 - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
3314 -
3315 - // If persistence is on and history hasn't loaded yet, show loader
3316 - // while history loads to prevent flash of empty chat
3317 - if (chatPersistenceEnabled && !instance.chatHistoryLoaded) {
3318 - if (chatContainer) chatContainer.style.display = 'none';
3319 - showInitLoader(botId);
3320 - loadChatHistory(botId, function() {
3321 - hideInitLoader(botId);
3322 - if (chatContainer) chatContainer.style.display = 'flex';
3323 - scrollToBottom(botId, true);
3324 - });
3325 - } else {
3326 - hideInitLoader(botId);
3327 - if (chatContainer) chatContainer.style.display = 'flex';
3328 - if (typeof loadChatHistory === 'function') {
3329 - loadChatHistory(botId);
3330 - }
3331 - }
3332 -}
3333 -
3334 -// ====================================
3335 -// EMAIL COLLECTION SETUP - MULTI-INSTANCE VERSION
3336 -// ====================================
3337 2637 // Only run email collection setup if it's enabled
3338 2638 if (mxchatChat && mxchatChat.email_collection_enabled === 'on') {
2639 + // Email collection form setup and handlers
2640 + const emailForm = document.getElementById('email-collection-form');
2641 + const emailBlocker = document.getElementById('email-blocker');
2642 + const chatbotWrapper = document.getElementById('chat-container');
3339 2643
3340 - // Track submitting state per bot
3341 - const emailSubmittingState = {};
2644 + if (emailForm && emailBlocker && chatbotWrapper) {
2645 +
2646 + // Add loading state management
2647 + let isSubmitting = false;
2648 +
2649 + // Optimized UI transition functions
2650 + function showEmailForm() {
2651 + emailBlocker.style.display = 'flex';
2652 + chatbotWrapper.style.display = 'none';
2653 + }
3342 2654
3343 - // Add CSS animations for email form (once globally)
3344 - if (!document.getElementById('email-error-styles')) {
3345 - const style = document.createElement('style');
3346 - style.id = 'email-error-styles';
3347 - style.textContent = `
3348 - @keyframes fadeInError {
3349 - from { opacity: 0; transform: translateY(-5px); }
3350 - to { opacity: 1; transform: translateY(0); }
2655 + function showChatContainer() {
2656 + // Show chat immediately without delay
2657 + emailBlocker.style.display = 'none';
2658 + chatbotWrapper.style.display = 'flex';
2659 +
2660 + // Load chat history only after showing chat container
2661 + if (typeof loadChatHistory === 'function') {
2662 + loadChatHistory();
3351 2663 }
3352 - .email-input-shake {
3353 - animation: shake 0.5s ease-in-out;
3354 - }
3355 - @keyframes shake {
3356 - 0%, 100% { transform: translateX(0); }
3357 - 25% { transform: translateX(-5px); }
3358 - 75% { transform: translateX(5px); }
3359 - }
3360 - @keyframes spin {
3361 - from { transform: rotate(0deg); }
3362 - to { transform: rotate(360deg); }
3363 - }
3364 - .email-spinner {
3365 - display: inline-block;
3366 - vertical-align: middle;
3367 - }
3368 - `;
3369 - document.head.appendChild(style);
3370 - }
2664 + }
3371 2665
3372 - function isValidEmailAddress(email) {
3373 - const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
3374 - return emailRegex.test(email.trim()) && email.length <= 254;
3375 - }
2666 + // Enhanced email validation
2667 + function isValidEmail(email) {
2668 + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
2669 + return emailRegex.test(email.trim()) && email.length <= 254; // RFC 5321 limit
2670 + }
3376 2671
3377 - function isValidNameInput(name) {
3378 - return name && name.trim().length >= 2 && name.trim().length <= 100;
3379 - }
2672 + // Enhanced name validation
2673 + function isValidName(name) {
2674 + return name && name.trim().length >= 2 && name.trim().length <= 100;
2675 + }
3380 2676
3381 - /**
3382 - * Replace {visitor_name} placeholder in intro message with actual visitor name
3383 - * @param {string} botId - The bot instance ID
3384 - * @param {string} visitorName - The visitor's name to insert
3385 - */
3386 - function replaceVisitorNamePlaceholder(botId, visitorName) {
3387 - var chatBox = getElementDOM(botId, 'chat-box');
3388 - if (!chatBox) return;
3389 -
3390 - // Find the first bot message (intro message)
3391 - var introMessage = chatBox.querySelector('.bot-message');
3392 - if (!introMessage) return;
3393 -
3394 - var messageContent = introMessage.querySelector('div[dir="auto"]');
3395 - if (!messageContent) return;
3396 -
3397 - var html = messageContent.innerHTML;
3398 -
3399 - // Replace {visitor_name} placeholder (case-insensitive)
3400 - if (visitorName && visitorName.trim()) {
3401 - // Escape HTML to prevent XSS
3402 - var safeName = $('<div>').text(visitorName.trim()).html();
3403 - html = html.replace(/\{visitor_name\}/gi, safeName);
3404 - } else {
3405 - // Remove placeholder and clean up spacing if no name provided
3406 - html = html.replace(/\{visitor_name\}/gi, '');
3407 - // Clean up any double spaces that might result
3408 - html = html.replace(/\s{2,}/g, ' ').trim();
2677 + // Show loading state with spinner
2678 + function setSubmissionState(loading) {
2679 + const submitButton = document.getElementById('email-submit-button');
2680 + const emailInput = document.getElementById('user-email');
2681 + const nameInput = document.getElementById('user-name');
2682 +
2683 + if (loading) {
2684 + isSubmitting = true;
2685 + if (submitButton) submitButton.disabled = true;
2686 + if (emailInput) emailInput.disabled = true;
2687 + if (nameInput) nameInput.disabled = true;
2688 +
2689 + // Store original content and add spinner
2690 + if (submitButton && !submitButton.getAttribute('data-original-html')) {
2691 + submitButton.setAttribute('data-original-html', submitButton.innerHTML);
2692 +
2693 + // Add loading spinner while keeping original text
2694 + const originalText = submitButton.textContent;
2695 + submitButton.innerHTML = `
2696 + <svg class="email-spinner" style="width: 16px; height: 16px; margin-right: 8px; animation: spin 1s linear infinite;" viewBox="0 0 24 24">
2697 + <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416">
2698 + <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/>
2699 + <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/>
2700 + </circle>
2701 + </svg>
2702 + ${originalText}
2703 + `;
2704 +
2705 + submitButton.style.opacity = '0.8';
2706 + }
2707 + } else {
2708 + isSubmitting = false;
2709 + if (submitButton) submitButton.disabled = false;
2710 + if (emailInput) emailInput.disabled = false;
2711 + if (nameInput) nameInput.disabled = false;
2712 +
2713 + // Restore original content
2714 + if (submitButton) {
2715 + const originalHtml = submitButton.getAttribute('data-original-html');
2716 + if (originalHtml) {
2717 + submitButton.innerHTML = originalHtml;
2718 + }
2719 + submitButton.style.opacity = '1';
2720 + }
2721 + }
3409 2722 }
3410 2723
3411 - messageContent.innerHTML = html;
3412 - }
3413 -
3414 - function setEmailSubmissionState(botId, loading) {
3415 - var submitButton = getElementDOM(botId, 'email-submit-button');
3416 - var emailInput = getElementDOM(botId, 'user-email');
3417 - var nameInput = getElementDOM(botId, 'user-name');
3418 -
3419 - if (loading) {
3420 - emailSubmittingState[botId] = true;
3421 - if (submitButton) submitButton.disabled = true;
3422 - if (emailInput) emailInput.disabled = true;
3423 - if (nameInput) nameInput.disabled = true;
3424 -
3425 - if (submitButton && !submitButton.getAttribute('data-original-html')) {
3426 - submitButton.setAttribute('data-original-html', submitButton.innerHTML);
3427 - const originalText = submitButton.textContent;
3428 - submitButton.innerHTML = `
3429 - <svg class="email-spinner" style="width: 16px; height: 16px; margin-right: 8px; animation: spin 1s linear infinite;" viewBox="0 0 24 24">
3430 - <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416">
3431 - <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/>
3432 - <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/>
3433 - </circle>
3434 - </svg>
3435 - ${originalText}
2724 + // Error display functions
2725 + function showEmailError(message) {
2726 + clearEmailError();
2727 +
2728 + const errorDiv = document.createElement('div');
2729 + errorDiv.className = 'email-error';
2730 + errorDiv.style.cssText = `
2731 + color: #e74c3c;
2732 + font-size: 12px;
2733 + margin-top: 8px;
2734 + padding: 4px 0;
2735 + animation: fadeInError 0.3s ease;
2736 + `;
2737 + errorDiv.textContent = message;
2738 +
2739 + // Add CSS animation if not already present
2740 + if (!document.getElementById('email-error-styles')) {
2741 + const style = document.createElement('style');
2742 + style.id = 'email-error-styles';
2743 + style.textContent = `
2744 + @keyframes fadeInError {
2745 + from { opacity: 0; transform: translateY(-5px); }
2746 + to { opacity: 1; transform: translateY(0); }
2747 + }
2748 + .email-input-shake {
2749 + animation: shake 0.5s ease-in-out;
2750 + }
2751 + @keyframes shake {
2752 + 0%, 100% { transform: translateX(0); }
2753 + 25% { transform: translateX(-5px); }
2754 + 75% { transform: translateX(5px); }
2755 + }
2756 + @keyframes spin {
2757 + from { transform: rotate(0deg); }
2758 + to { transform: rotate(360deg); }
2759 + }
2760 + .email-spinner {
2761 + display: inline-block;
2762 + vertical-align: middle;
2763 + }
3436 2764 `;
3437 - submitButton.style.opacity = '0.8';
2765 + document.head.appendChild(style);
3438 2766 }
3439 - } else {
3440 - emailSubmittingState[botId] = false;
3441 - if (submitButton) submitButton.disabled = false;
3442 - if (emailInput) emailInput.disabled = false;
3443 - if (nameInput) nameInput.disabled = false;
3444 -
3445 - if (submitButton) {
3446 - const originalHtml = submitButton.getAttribute('data-original-html');
3447 - if (originalHtml) {
3448 - submitButton.innerHTML = originalHtml;
3449 - }
3450 - submitButton.style.opacity = '1';
2767 +
2768 + emailForm.appendChild(errorDiv);
2769 +
2770 + // Add shake animation to inputs
2771 + const emailInput = document.getElementById('user-email');
2772 + const nameInput = document.getElementById('user-name');
2773 +
2774 + if (emailInput) {
2775 + emailInput.classList.add('email-input-shake');
2776 + setTimeout(() => {
2777 + emailInput.classList.remove('email-input-shake');
2778 + }, 500);
3451 2779 }
2780 +
2781 + if (nameInput) {
2782 + nameInput.classList.add('email-input-shake');
2783 + setTimeout(() => {
2784 + nameInput.classList.remove('email-input-shake');
2785 + }, 500);
2786 + }
3452 2787 }
3453 - }
3454 2788
3455 - function showEmailError(botId, message) {
3456 - clearEmailError(botId);
3457 -
3458 - var emailForm = getElementDOM(botId, 'email-collection-form');
3459 - if (!emailForm) return;
3460 -
3461 - const errorDiv = document.createElement('div');
3462 - errorDiv.className = 'email-error';
3463 - errorDiv.style.cssText = `
3464 - color: #e74c3c;
3465 - font-size: 12px;
3466 - margin-top: 8px;
3467 - padding: 4px 0;
3468 - animation: fadeInError 0.3s ease;
3469 - `;
3470 - errorDiv.textContent = message;
3471 - emailForm.appendChild(errorDiv);
3472 -
3473 - // Add shake animation to inputs
3474 - var emailInput = getElementDOM(botId, 'user-email');
3475 - var nameInput = getElementDOM(botId, 'user-name');
3476 -
3477 - if (emailInput) {
3478 - emailInput.classList.add('email-input-shake');
3479 - setTimeout(() => emailInput.classList.remove('email-input-shake'), 500);
3480 - }
3481 - if (nameInput) {
3482 - nameInput.classList.add('email-input-shake');
3483 - setTimeout(() => nameInput.classList.remove('email-input-shake'), 500);
3484 - }
3485 - }
3486 -
3487 - function clearEmailError(botId) {
3488 - var emailForm = getElementDOM(botId, 'email-collection-form');
3489 - if (emailForm) {
2789 + function clearEmailError() {
3490 2790 const existingErrors = emailForm.querySelectorAll('.email-error');
3491 2791 existingErrors.forEach(error => error.remove());
3492 2792 }
3493 - }
3494 2793
3495 - // Resolve email state using server-side data when available, AJAX fallback otherwise
3496 - function resolveEmailState(botId) {
3497 - if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) {
3498 - if (mxchatChat.initial_email_state.show_email_form) {
3499 - showEmailFormForBot(botId);
3500 - } else {
3501 - showChatContainerForBot(botId);
3502 - }
3503 - } else {
3504 - checkSessionAndEmailForBot(botId);
3505 - }
3506 - }
2794 + // MAIN FORM SUBMIT HANDLER
2795 + // Remove any existing event listeners first
2796 + emailForm.removeEventListener('submit', handleFormSubmit);
3507 2797
3508 - function checkSessionAndEmailForBot(botId) {
3509 - const sessionId = MxChatInstances.ensureSession(botId);
2798 + // Add the form submit handler
2799 + emailForm.addEventListener('submit', handleFormSubmit);
3510 2800
3511 - // Hide both panels while we check — show loader instead
3512 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3513 - var chatContainer = getElementDOM(botId, 'chat-container');
3514 - if (emailBlocker) emailBlocker.style.display = 'none';
3515 - if (chatContainer) chatContainer.style.display = 'none';
3516 - showInitLoader(botId);
2801 + function handleFormSubmit(event) {
2802 + event.preventDefault();
2803 + event.stopPropagation();
3517 2804
3518 - fetch(mxchatChat.ajax_url, {
3519 - method: 'POST',
3520 - headers: {
3521 - 'Content-Type': 'application/x-www-form-urlencoded',
3522 - },
3523 - body: new URLSearchParams({
3524 - action: 'mxchat_check_email_provided',
3525 - session_id: sessionId,
3526 - nonce: mxchatChat.nonce,
3527 - })
3528 - })
3529 - .then((response) => {
3530 - if (!response.ok) {
3531 - throw new Error(`HTTP error! status: ${response.status}`);
2805 + // Prevent double submission
2806 + if (isSubmitting) {
2807 + return false;
3532 2808 }
3533 - return response.json();
3534 - })
3535 - .then((data) => {
3536 - if (data.success) {
3537 - if (data.data.logged_in || data.data.email) {
3538 - showChatContainerForBot(botId);
3539 - } else {
3540 - showEmailFormForBot(botId);
3541 - }
3542 - } else {
3543 - showEmailFormForBot(botId);
3544 - }
3545 - })
3546 - .catch((error) => {
3547 - showEmailFormForBot(botId);
3548 - });
3549 - }
3550 2809
3551 - // Event delegation for email form submission
3552 - $(document).on('submit', '.email-collection-form', function(e) {
3553 - e.preventDefault();
3554 - e.stopPropagation();
2810 + const userEmail = document.getElementById('user-email').value.trim();
2811 + const nameInput = document.getElementById('user-name');
2812 + const userName = nameInput ? nameInput.value.trim() : '';
2813 + const sessionId = getChatSession();
3555 2814
3556 - var botId = getBotIdFromElement(this);
2815 + // Validate email before submission
2816 + if (!userEmail) {
2817 + showEmailError('Please enter your email address.');
2818 + return false;
2819 + }
3557 2820
3558 - // Prevent double submission
3559 - if (emailSubmittingState[botId]) {
3560 - return false;
3561 - }
2821 + if (!isValidEmail(userEmail)) {
2822 + showEmailError('Please enter a valid email address.');
2823 + return false;
2824 + }
3562 2825
3563 - var emailInput = getElementDOM(botId, 'user-email');
3564 - var nameInput = getElementDOM(botId, 'user-name');
3565 - var userEmail = emailInput ? emailInput.value.trim() : '';
3566 - var userName = nameInput ? nameInput.value.trim() : '';
3567 - var sessionId = MxChatInstances.ensureSession(botId);
2826 + // Validate name if field exists
2827 + if (nameInput && !isValidName(userName)) {
2828 + showEmailError('Please enter a valid name (2-100 characters).');
2829 + return false;
2830 + }
3568 2831
3569 - // Validate email
3570 - if (!userEmail) {
3571 - showEmailError(botId, 'Please enter your email address.');
3572 - return false;
3573 - }
2832 + // Clear any existing errors
2833 + clearEmailError();
2834 + setSubmissionState(true);
3574 2835
3575 - if (!isValidEmailAddress(userEmail)) {
3576 - showEmailError(botId, 'Please enter a valid email address.');
3577 - return false;
3578 - }
2836 + // Prepare form data with optional name
2837 + const formData = new URLSearchParams({
2838 + action: 'mxchat_handle_save_email_and_response',
2839 + email: userEmail,
2840 + session_id: sessionId,
2841 + nonce: mxchatChat.nonce,
2842 + });
3579 2843
3580 - // Validate name if field exists and has content
3581 - if (nameInput && userName && !isValidNameInput(userName)) {
3582 - showEmailError(botId, 'Please enter a valid name (2-100 characters).');
3583 - return false;
3584 - }
2844 + // Add name to form data if provided
2845 + if (userName) {
2846 + formData.append('name', userName);
2847 + }
3585 2848
3586 - clearEmailError(botId);
3587 - setEmailSubmissionState(botId, true);
2849 + fetch(mxchatChat.ajax_url, {
2850 + method: 'POST',
2851 + headers: {
2852 + 'Content-Type': 'application/x-www-form-urlencoded',
2853 + },
2854 + body: formData
2855 + })
2856 + .then((response) => {
2857 + if (!response.ok) {
2858 + throw new Error(`HTTP error! status: ${response.status}`);
2859 + }
2860 + return response.json();
2861 + })
2862 + .then((data) => {
2863 + setSubmissionState(false);
3588 2864
3589 - // Prepare form data
3590 - const formData = new URLSearchParams({
3591 - action: 'mxchat_handle_save_email_and_response',
3592 - email: userEmail,
3593 - session_id: sessionId,
3594 - nonce: mxchatChat.nonce,
3595 - });
2865 + if (data.success) {
2866 + // Show chat immediately
2867 + showChatContainer();
3596 2868
3597 - if (userName) {
3598 - formData.append('name', userName);
2869 + // Handle bot response if provided
2870 + if (data.message && typeof appendMessage === 'function') {
2871 + setTimeout(() => {
2872 + appendMessage('bot', data.message);
2873 + if (typeof scrollToBottom === 'function') {
2874 + scrollToBottom();
2875 + }
2876 + }, 100);
2877 + }
2878 + } else {
2879 + showEmailError(data.message || 'Failed to save email. Please try again.');
2880 + }
2881 + })
2882 + .catch((error) => {
2883 + setSubmissionState(false);
2884 + showEmailError('An error occurred. Please try again.');
2885 + });
2886 +
2887 + return false; // Extra prevention
3599 2888 }
3600 2889
3601 - fetch(mxchatChat.ajax_url, {
3602 - method: 'POST',
3603 - headers: {
3604 - 'Content-Type': 'application/x-www-form-urlencoded',
3605 - },
3606 - body: formData
3607 - })
3608 - .then((response) => {
3609 - if (!response.ok) {
3610 - throw new Error(`HTTP error! status: ${response.status}`);
3611 - }
3612 - return response.json();
3613 - })
3614 - .then((data) => {
3615 - setEmailSubmissionState(botId, false);
2890 + // Real-time email validation
2891 + const emailInput = document.getElementById('user-email');
2892 + if (emailInput) {
2893 + let validationTimeout;
2894 +
2895 + emailInput.addEventListener('input', function() {
2896 + // Clear previous validation timeout
2897 + if (validationTimeout) {
2898 + clearTimeout(validationTimeout);
2899 + }
2900 +
2901 + // Debounce validation
2902 + validationTimeout = setTimeout(() => {
2903 + const email = this.value.trim();
2904 + clearEmailError();
2905 +
2906 + if (email && !isValidEmail(email)) {
2907 + showEmailError('Please enter a valid email address.');
2908 + }
2909 + }, 500);
2910 + });
3616 2911
3617 - if (data.success) {
3618 - showChatContainerForBot(botId);
2912 + // Handle Enter key
2913 + emailInput.addEventListener('keypress', function(e) {
2914 + if (e.key === 'Enter' && !isSubmitting) {
2915 + e.preventDefault();
2916 + emailForm.dispatchEvent(new Event('submit'));
2917 + }
2918 + });
2919 + }
3619 2920
3620 - // Replace {visitor_name} placeholder in intro message with actual name
3621 - if (userName) {
3622 - replaceVisitorNamePlaceholder(botId, userName);
3623 - } else {
3624 - // Remove placeholder if no name provided
3625 - replaceVisitorNamePlaceholder(botId, '');
2921 + // Real-time name validation
2922 + const nameInput = document.getElementById('user-name');
2923 + if (nameInput) {
2924 + let nameValidationTimeout;
2925 +
2926 + nameInput.addEventListener('input', function() {
2927 + // Clear previous validation timeout
2928 + if (nameValidationTimeout) {
2929 + clearTimeout(nameValidationTimeout);
3626 2930 }
2931 +
2932 + // Debounce validation
2933 + nameValidationTimeout = setTimeout(() => {
2934 + const name = this.value.trim();
2935 + clearEmailError();
2936 +
2937 + if (name && !isValidName(name)) {
2938 + showEmailError('Name must be between 2 and 100 characters.');
2939 + }
2940 + }, 500);
2941 + });
3627 2942
3628 - if (data.message && typeof appendMessage === 'function') {
3629 - setTimeout(() => {
3630 - appendMessage('bot', data.message, '', [], false, botId);
3631 - if (typeof scrollToBottom === 'function') {
3632 - scrollToBottom(botId);
3633 - }
3634 - }, 100);
2943 + // Handle Enter key
2944 + nameInput.addEventListener('keypress', function(e) {
2945 + if (e.key === 'Enter' && !isSubmitting) {
2946 + e.preventDefault();
2947 + emailForm.dispatchEvent(new Event('submit'));
3635 2948 }
2949 + });
2950 + }
2951 +
2952 + // Initial state check
2953 + if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) {
2954 + const emailState = mxchatChat.initial_email_state;
2955 + if (emailState.show_email_form) {
2956 + showEmailForm();
3636 2957 } else {
3637 - showEmailError(botId, data.message || 'Failed to save email. Please try again.');
2958 + showChatContainer();
3638 2959 }
3639 - })
3640 - .catch((error) => {
3641 - setEmailSubmissionState(botId, false);
3642 - showEmailError(botId, 'An error occurred. Please try again.');
3643 - });
3644 -
3645 - return false;
3646 - });
3647 -
3648 - // Real-time email validation using event delegation
3649 - $(document).on('input', '.mxchat-email-input', function() {
3650 - var botId = getBotIdFromElement(this);
3651 - var $input = $(this);
3652 -
3653 - // Clear previous timeout
3654 - clearTimeout($input.data('validationTimeout'));
3655 -
3656 - // Debounce validation
3657 - var timeout = setTimeout(() => {
3658 - var email = this.value.trim();
3659 - clearEmailError(botId);
3660 -
3661 - if (email && !isValidEmailAddress(email)) {
3662 - showEmailError(botId, 'Please enter a valid email address.');
3663 - }
3664 - }, 500);
3665 -
3666 - $input.data('validationTimeout', timeout);
3667 - });
3668 -
3669 - // Handle Enter key in email input
3670 - $(document).on('keypress', '.mxchat-email-input', function(e) {
3671 - if (e.key === 'Enter') {
3672 - e.preventDefault();
3673 - var botId = getBotIdFromElement(this);
3674 - if (!emailSubmittingState[botId]) {
3675 - $(this).closest('.email-collection-form').submit();
3676 - }
2960 + } else {
2961 + // Check email status via AJAX
2962 + setTimeout(checkSessionAndEmail, 100);
3677 2963 }
3678 - });
3679 2964
3680 - // Handle Enter key in name input
3681 - $(document).on('keypress', '.mxchat-name-input', function(e) {
3682 - if (e.key === 'Enter') {
3683 - e.preventDefault();
3684 - var botId = getBotIdFromElement(this);
3685 - if (!emailSubmittingState[botId]) {
3686 - $(this).closest('.email-collection-form').submit();
3687 - }
2965 + // Check if email exists for the current session
2966 + function checkSessionAndEmail() {
2967 + const sessionId = getChatSession();
2968 +
2969 + fetch(mxchatChat.ajax_url, {
2970 + method: 'POST',
2971 + headers: {
2972 + 'Content-Type': 'application/x-www-form-urlencoded',
2973 + },
2974 + body: new URLSearchParams({
2975 + action: 'mxchat_check_email_provided',
2976 + session_id: sessionId,
2977 + nonce: mxchatChat.nonce,
2978 + })
2979 + })
2980 + .then((response) => {
2981 + if (!response.ok) {
2982 + throw new Error(`HTTP error! status: ${response.status}`);
2983 + }
2984 + return response.json();
2985 + })
2986 + .then((data) => {
2987 + if (data.success) {
2988 + if (data.data.logged_in || data.data.email) {
2989 + showChatContainer();
2990 + } else {
2991 + showEmailForm();
2992 + }
2993 + } else {
2994 + // On error, default to showing email form
2995 + showEmailForm();
2996 + }
2997 + })
2998 + .catch((error) => {
2999 + // Email check failed - default to email form
3000 + showEmailForm();
3001 + });
3688 3002 }
3689 - });
3690 3003
3691 - // Initialize email check for all bot instances
3692 - // For floating bots: defer until widget is opened (zero passive AJAX)
3693 - // For embedded bots: check immediately since the form is visible
3694 - $('.mxchat-chatbot-wrapper').each(function() {
3695 - var botId = $(this).data('bot-id') || 'default';
3696 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3697 -
3698 - if (emailBlocker) {
3699 - if (isEmbeddedBot(botId)) {
3700 - // Embedded bots are always visible — check now
3701 - resolveEmailState(botId);
3702 - }
3703 - // Floating bots: handled in the widget open handler
3704 - } else if (isEmbeddedBot(botId)) {
3705 - // Embedded bot, no email collection — load history with loader
3706 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3707 - if (chatPersistenceEnabled) {
3708 - MxChatInstances.ensureSession(botId);
3709 - showChatContainerForBot(botId);
3710 - }
3711 - }
3712 - });
3004 + } else {
3005 + // Email collection is enabled but essential elements are missing - silently continue
3006 + }
3713 3007 }
3714 3008
3715 3009 // Open chatbot when pre-chat message is clicked - use class selector for multi-instance
3716 3010 $(document).on('click', '.pre-chat-message', function() {
@@ -3718,32 +3012,39 @@
3718 3012 var $chatbot = getElement(botId, 'floating-chatbot');
3719 3013 if ($chatbot.hasClass('hidden')) {
3720 3014 $chatbot.removeClass('hidden').addClass('visible');
3721 3015 getElement(botId, 'floating-chatbot-button').addClass('hidden');
3722 - handlePreChatDismissal(botId);
3016 + $(this).fadeOut(250); // Hide pre-chat message
3723 3017 disableScroll(); // Disable scroll when chatbot opens
3018 + }
3019 + });
3724 3020
3725 - // Load chat history for returning visitors (persistence)
3726 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3727 - if (chatPersistenceEnabled) {
3728 - MxChatInstances.ensureSession(botId);
3729 - }
3021 + // Dismiss pre-chat message via close button - handled by event delegation above at line ~2376
3022 + // This is a fallback for legacy support
3023 + $(document).on('click', '.close-pre-chat-message', function() {
3024 + var botId = getBotIdFromElement(this);
3025 + var $preChat = getElement(botId, 'pre-chat-message');
3026 + $preChat.fadeOut(200); // Hide the message
3730 3027
3731 - // Deferred email check — only on first widget open
3732 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3733 - var instance = MxChatInstances.get(botId);
3734 - if (emailBlocker && !instance.emailCheckDone) {
3735 - instance.emailCheckDone = true;
3736 - resolveEmailState(botId);
3737 - } else if (!emailBlocker) {
3738 - showChatContainerForBot(botId);
3028 + // Send an AJAX request to set the transient flag for 24 hours
3029 + $.ajax({
3030 + url: mxchatChat.ajax_url,
3031 + type: 'POST',
3032 + data: {
3033 + action: 'mxchat_dismiss_pre_chat_message',
3034 + _ajax_nonce: mxchatChat.nonce
3035 + },
3036 + success: function() {
3037 + // Ensure the message is hidden after dismissal
3038 + $preChat.hide();
3039 + },
3040 + error: function() {
3041 + // Error dismissing pre-chat message - silently continue
3739 3042 }
3740 - }
3043 + });
3741 3044 });
3742 3045
3743 - // Legacy duplicate close handler removed — handled by single event delegation above
3744 3046
3745 -
3746 3047 function hasQuickQuestions(botId) {
3747 3048 botId = botId || 'default';
3748 3049 var questionsContainer = getElementDOM(botId, 'mxchat-popular-questions');
3749 3050 if (!questionsContainer) return false;
@@ -3879,11 +3180,18 @@
3879 3180 });
3880 3181
3881 3182 // Initialize when document is ready
3882 3183 setFullHeight();
3184 + trackOriginatingPage();
3883 3185
3884 - // Note: trackOriginatingPage() and loadChatHistory() are now deferred
3885 - // until the user's first interaction via MxChatInstances.ensureSession()
3186 + // Only load chat history if email collection is disabled
3187 + if (mxchatChat.email_collection_enabled !== 'on') {
3188 + // Load history for all instances
3189 + $('.mxchat-chatbot-wrapper').each(function() {
3190 + var botId = $(this).data('bot-id') || 'default';
3191 + loadChatHistory(botId);
3192 + });
3193 + }
3886 3194
3887 3195 // Initialize chat visibility for all instances
3888 3196 $('.mxchat-chatbot-wrapper').each(function() {
3889 3197 var botId = $(this).data('bot-id') || 'default';
@@ -3936,299 +3244,6 @@
3936 3244 }, 2000);
3937 3245 });
3938 3246 }
3939 3247 }
3940 -});
3941 -
3942 -// ============================================================================
3943 -// SATISFACTION RATING (v3.2.6)
3944 -// ============================================================================
3945 -// Per-session 👍/👎 prompt that appears in the chat-box after 60s of user
3946 -// inactivity following a bot reply. One prompt per session, deduped via
3947 -// localStorage. Disabled site-wide when mxchatChat.satisfaction_rating_enabled
3948 -// is exactly false (default ON).
3949 -jQuery(function($) {
3950 - if (typeof mxchatChat === 'undefined') return;
3951 - if (mxchatChat.satisfaction_rating_enabled === false || mxchatChat.satisfaction_rating_enabled === 'off') return;
3952 -
3953 - // wp_localize_script stringifies ints, so accept both number and numeric string.
3954 - var idleRaw = mxchatChat.satisfaction_rating_idle_seconds;
3955 - var idleSeconds = (typeof idleRaw === 'number') ? idleRaw : parseInt(idleRaw, 10);
3956 - if (!isFinite(idleSeconds)) idleSeconds = 60;
3957 - if (idleSeconds < 5) idleSeconds = 5;
3958 - if (idleSeconds > 600) idleSeconds = 600;
3959 - var IDLE_MS = idleSeconds * 1000;
3960 - var MIN_BOT_REPLIES = 2;
3961 - var ratingState = {};
3962 -
3963 - function getState(botId) {
3964 - if (!ratingState[botId]) {
3965 - ratingState[botId] = { idleTimer: null, botReplies: 0, promptShown: false, dismissed: false };
3966 - }
3967 - return ratingState[botId];
3968 - }
3969 -
3970 - function getSessionId(botId) {
3971 - if (typeof MxChatInstances !== 'undefined' && MxChatInstances.getChatSession) {
3972 - return MxChatInstances.getChatSession(botId);
3973 - }
3974 - return null;
3975 - }
3976 -
3977 - function isAlreadyRated(sessionId) {
3978 - if (!sessionId) return false;
3979 - try { return localStorage.getItem('mxchat_rated:' + sessionId) === '1'; } catch (e) { return false; }
3980 - }
3981 -
3982 - function markRated(sessionId) {
3983 - if (!sessionId) return;
3984 - try { localStorage.setItem('mxchat_rated:' + sessionId, '1'); } catch (e) {}
3985 - }
3986 -
3987 - function esc(s) {
3988 - return String(s == null ? '' : s)
3989 - .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
3990 - .replace(/"/g, '&quot;').replace(/'/g, '&#039;');
3991 - }
3992 -
3993 - // Mirror shouldSkipInlineColors so rating bubbles defer to AI-theme CSS.
3994 - function ratingSkipInlineColors(botId) {
3995 - if (mxchatChat.skip_inline_colors) return true;
3996 - var botAssignments = mxchatChat.bot_theme_assignments || {};
3997 - return botAssignments.hasOwnProperty(botId);
3998 - }
3999 -
4000 - function botBubbleStyleAttr(botId) {
4001 - if (ratingSkipInlineColors(botId)) return '';
4002 - var bg = mxchatChat.bot_message_bg_color;
4003 - var fg = mxchatChat.bot_message_font_color;
4004 - if (!bg && !fg) return '';
4005 - return ' style="background-color: ' + esc(bg || '') + '; color: ' + esc(fg || '') + ';"';
4006 - }
4007 -
4008 - // Reads the rating bubble's actual computed fg+bg (whatever paints it —
4009 - // the inline color pickers OR the mxchat-theme AI customizer's injected CSS)
4010 - // and paints the filled "Send" pill so it fills with the bot font color and
4011 - // labels in the bubble bg. Mirrors mxchatSyncMenuColors(~:1512) for the read.
4012 - // We paint the submit button DIRECTLY (inline longhand) rather than relying
4013 - // on the CSS rule's var()s: Chromium resolves an INHERITED custom property
4014 - // unreliably inside a descendant's `background`, so a bubble-level var would
4015 - // silently fall back to the literal (white-block bug all over again). Inline
4016 - // longhand always wins. Same transparent-guard as the menu so we never paint
4017 - // a see-through value — in that case the CSS literal fallbacks keep it legible.
4018 - function syncRatingBubbleColors(botId) {
4019 - var $chatBox = getChatBoxByBotId(botId);
4020 - if (!$chatBox || !$chatBox.length) return;
4021 - var bubbleEl = $chatBox.find('.mxchat-rating-bot-bubble').last()[0];
4022 - if (!bubbleEl) return;
4023 - var cs = window.getComputedStyle(bubbleEl);
4024 - var fg = cs.color;
4025 - var bg = cs.backgroundColor;
4026 - var hasFg = fg && fg !== 'rgba(0, 0, 0, 0)' && fg !== 'transparent';
4027 - var hasBg = bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent';
4028 - // Expose on the bubble too, for any inheriting styles / future use.
4029 - if (hasFg) bubbleEl.style.setProperty('--mxchat-bot-fg', fg);
4030 - if (hasBg) bubbleEl.style.setProperty('--mxchat-bot-bg', bg);
4031 - // Paint the Send pill directly — the part that actually fixes the bug.
4032 - var submitEl = bubbleEl.querySelector('.mxchat-rating-submit');
4033 - if (submitEl) {
4034 - if (hasFg) submitEl.style.backgroundColor = fg; // fill = bot font color
4035 - if (hasBg) submitEl.style.color = bg; // label = bubble background
4036 - }
4037 - }
4038 -
4039 - function copy(key) {
4040 - var c = mxchatChat.satisfaction_rating_copy || {};
4041 - var d = {
4042 - question: 'Was this helpful?',
4043 - helpful: 'Helpful',
4044 - not_helpful: 'Not helpful',
4045 - dismiss: 'Dismiss',
4046 - thanks: 'Thanks! Anything we should improve? (optional)',
4047 - placeholder: 'Tell us what could be better…',
4048 - send: 'Send',
4049 - skip: 'Skip',
4050 - saved: 'Thanks for the feedback.'
4051 - };
4052 - return c[key] || d[key];
4053 - }
4054 -
4055 - function thumbUpSvg() {
4056 - 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>';
4057 - }
4058 - function thumbDownSvg() {
4059 - 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>';
4060 - }
4061 -
4062 - function buildPromptHtml(botId) {
4063 - var styleAttr = botBubbleStyleAttr(botId);
4064 - return ''
4065 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4066 - + '<div class="mxchat-rating-prompt" data-bot-id="' + esc(botId) + '" role="group" aria-label="' + esc(copy('question')) + '">'
4067 - + '<div class="mxchat-rating-question">' + esc(copy('question')) + '</div>'
4068 - + '<div class="mxchat-rating-actions">'
4069 - + '<span class="mxchat-rating-buttons">'
4070 - + '<button type="button" class="mxchat-rating-btn" data-rating="1" aria-label="' + esc(copy('helpful')) + '">' + thumbUpSvg() + '</button>'
4071 - + '<button type="button" class="mxchat-rating-btn" data-rating="-1" aria-label="' + esc(copy('not_helpful')) + '">' + thumbDownSvg() + '</button>'
4072 - + '</span>'
4073 - + '<button type="button" class="mxchat-rating-dismiss" aria-label="' + esc(copy('dismiss')) + '">×</button>'
4074 - + '</div>'
4075 - + '</div>'
4076 - + '</div>';
4077 - }
4078 -
4079 - function buildFeedbackHtml(botId, rating) {
4080 - var styleAttr = botBubbleStyleAttr(botId);
4081 - return ''
4082 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4083 - + '<div class="mxchat-rating-feedback" data-bot-id="' + esc(botId) + '" data-rating="' + esc(String(rating)) + '">'
4084 - + '<div class="mxchat-rating-feedback-label">' + esc(copy('thanks')) + '</div>'
4085 - + '<textarea class="mxchat-rating-feedback-input" maxlength="500" placeholder="' + esc(copy('placeholder')) + '" rows="2"></textarea>'
4086 - + '<div class="mxchat-rating-feedback-actions">'
4087 - + '<button type="button" class="mxchat-rating-skip">' + esc(copy('skip')) + '</button>'
4088 - + '<button type="button" class="mxchat-rating-submit">' + esc(copy('send')) + '</button>'
4089 - + '</div>'
4090 - + '</div>'
4091 - + '</div>';
4092 - }
4093 -
4094 - function buildSavedHtml(botId) {
4095 - var styleAttr = botBubbleStyleAttr(botId);
4096 - return ''
4097 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4098 - + '<div class="mxchat-rating-saved">' + esc(copy('saved')) + '</div>'
4099 - + '</div>';
4100 - }
4101 -
4102 - function getChatBoxByBotId(botId) {
4103 - var $byId = $('#chat-box-' + botId);
4104 - if ($byId.length) return $byId.first();
4105 - return $('.chat-box').first();
4106 - }
4107 -
4108 - function scrollChatBoxToBottom($chatBox) {
4109 - if (!$chatBox || !$chatBox.length) return;
4110 - $chatBox.scrollTop($chatBox[0].scrollHeight);
4111 - }
4112 -
4113 - function showPrompt(botId) {
4114 - var s = getState(botId);
4115 - if (s.promptShown || s.dismissed) return;
4116 - var sessionId = getSessionId(botId);
4117 - if (!sessionId) return;
4118 - if (isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4119 - var $chatBox = getChatBoxByBotId(botId);
4120 - if (!$chatBox.length) return;
4121 - if ($chatBox.find('.mxchat-rating-prompt').length) { s.promptShown = true; return; }
4122 - $chatBox.append(buildPromptHtml(botId));
4123 - syncRatingBubbleColors(botId);
4124 - s.promptShown = true;
4125 - scrollChatBoxToBottom($chatBox);
4126 - }
4127 -
4128 - function submitRating(botId, rating, feedback) {
4129 - var sessionId = getSessionId(botId);
4130 - if (!sessionId) return;
4131 - $.post(mxchatChat.ajax_url, {
4132 - action: 'mxchat_save_rating',
4133 - session_id: sessionId,
4134 - bot_id: botId,
4135 - rating: rating,
4136 - feedback: feedback || ''
4137 - });
4138 - markRated(sessionId);
4139 - }
4140 -
4141 - function onBotReply(botId) {
4142 - var s = getState(botId);
4143 - s.botReplies += 1;
4144 - if (s.promptShown || s.dismissed) return;
4145 - var sessionId = getSessionId(botId);
4146 - if (sessionId && isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4147 - if (s.botReplies < MIN_BOT_REPLIES) return;
4148 - if (s.idleTimer) clearTimeout(s.idleTimer);
4149 - s.idleTimer = setTimeout(function() { showPrompt(botId); }, IDLE_MS);
4150 - }
4151 -
4152 - function onUserMessage(botId) {
4153 - var s = getState(botId);
4154 - if (s.idleTimer) { clearTimeout(s.idleTimer); s.idleTimer = null; }
4155 - }
4156 -
4157 - function botIdFromChatBox(el) {
4158 - var id = el && el.id ? el.id : '';
4159 - return id.indexOf('chat-box-') === 0 ? id.substring('chat-box-'.length) : 'default';
4160 - }
4161 -
4162 - function setupObserver(chatBox) {
4163 - var botId = botIdFromChatBox(chatBox);
4164 - try {
4165 - var observer = new MutationObserver(function(mutations) {
4166 - mutations.forEach(function(m) {
4167 - for (var i = 0; i < m.addedNodes.length; i++) {
4168 - var node = m.addedNodes[i];
4169 - if (!node || node.nodeType !== 1) continue;
4170 - var $n = $(node);
4171 - if ($n.hasClass('mxchat-rating-bot-bubble') || $n.hasClass('mxchat-rating-prompt') || $n.hasClass('mxchat-rating-feedback') || $n.hasClass('mxchat-rating-saved')) continue;
4172 - 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)
4173 - else if ($n.hasClass('user-message')) onUserMessage(botId);
4174 - }
4175 - });
4176 - });
4177 - observer.observe(chatBox, { childList: true });
4178 - } catch (e) { /* noop */ }
4179 - }
4180 -
4181 - $('.chat-box').each(function() { setupObserver(this); });
4182 -
4183 - $(document).on('click', '.mxchat-rating-btn', function(e) {
4184 - e.preventDefault();
4185 - var $btn = $(this);
4186 - var $prompt = $btn.closest('.mxchat-rating-prompt');
4187 - var $wrap = $btn.closest('.mxchat-rating-bot-bubble');
4188 - var botId = $prompt.data('bot-id') || 'default';
4189 - var rating = parseInt($btn.attr('data-rating'), 10);
4190 - if (rating !== 1 && rating !== -1) return;
4191 - submitRating(botId, rating, '');
4192 - ($wrap.length ? $wrap : $prompt).replaceWith(buildFeedbackHtml(botId, rating));
4193 - syncRatingBubbleColors(botId);
4194 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4195 - });
4196 -
4197 - $(document).on('click', '.mxchat-rating-dismiss', function(e) {
4198 - e.preventDefault();
4199 - var $prompt = $(this).closest('.mxchat-rating-prompt');
4200 - var $wrap = $(this).closest('.mxchat-rating-bot-bubble');
4201 - var botId = $prompt.data('bot-id') || 'default';
4202 - var s = getState(botId);
4203 - s.dismissed = true;
4204 - markRated(getSessionId(botId));
4205 - ($wrap.length ? $wrap : $prompt).remove();
4206 - });
4207 -
4208 - function closeFeedback($fb) {
4209 - var botId = $fb.data('bot-id') || 'default';
4210 - var $wrap = $fb.closest('.mxchat-rating-bot-bubble');
4211 - ($wrap.length ? $wrap : $fb).replaceWith(buildSavedHtml(botId));
4212 - syncRatingBubbleColors(botId);
4213 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4214 - }
4215 -
4216 - $(document).on('click', '.mxchat-rating-skip', function(e) {
4217 - e.preventDefault();
4218 - closeFeedback($(this).closest('.mxchat-rating-feedback'));
4219 - });
4220 -
4221 - $(document).on('click', '.mxchat-rating-submit', function(e) {
4222 - e.preventDefault();
4223 - var $fb = $(this).closest('.mxchat-rating-feedback');
4224 - var botId = $fb.data('bot-id') || 'default';
4225 - var rating = parseInt($fb.attr('data-rating'), 10);
4226 - if (rating !== 1 && rating !== -1) { closeFeedback($fb); return; }
4227 - var text = String($fb.find('.mxchat-rating-feedback-input').val() || '').trim();
4228 - if (text !== '') {
4229 - submitRating(botId, rating, text);
4230 - }
4231 - closeFeedback($fb);
4232 - });
4233 3248 });
4234 3249