| @@ -60,39 +60,18 @@ | ||
| 60 | 60 | return Object.keys(this.instances); |
| 61 | 61 | }, |
| 62 | 62 | |
| 63 | 63 | // Session management per bot |
| 64 | - // Returns existing session ID from cookie or localStorage (with in-memory fallback), | |
| 65 | - // or null if none exists. Does NOT create a new session — use ensureSession() for that. | |
| 66 | 64 | getChatSession: function(botId) { |
| 67 | 65 | var cookieName = 'mxchat_session_id_' + botId; |
| 68 | - var storageKey = 'mxchat_session_id_' + botId; | |
| 69 | 66 | var sessionId = getCookie(cookieName); |
| 70 | 67 | |
| 71 | - // Fallback to localStorage if cookie is missing (e.g. cleared by browser/consent) | |
| 72 | 68 | if (!sessionId) { |
| 73 | - try { sessionId = localStorage.getItem(storageKey); } catch (e) {} | |
| 69 | + sessionId = generateSessionId(); | |
| 70 | + this.setChatSession(botId, sessionId); | |
| 74 | 71 | } |
| 75 | 72 | |
| 76 | - // Fallback to in-memory instance when cookie AND localStorage are both blocked | |
| 77 | - // (Safari ITP, strict tracking prevention, cross-origin iframes with partitioned | |
| 78 | - // storage). Without this, ensureSession() can generate and store an ID that | |
| 79 | - // getChatSession() then can't read back, causing null session_ids on send. | |
| 80 | - if (!sessionId && this.instances[botId] && this.instances[botId].sessionId) { | |
| 81 | - sessionId = this.instances[botId].sessionId; | |
| 82 | - } | |
| 83 | - | |
| 84 | - // Guard against stored sentinel values that indicate earlier broken writes. | |
| 85 | - if (sessionId === 'null' || sessionId === 'undefined') { | |
| 86 | - sessionId = null; | |
| 87 | - } | |
| 88 | - | |
| 89 | - // Re-sync cookie from localStorage if cookie was lost | |
| 90 | - if (sessionId && !getCookie(cookieName)) { | |
| 91 | - document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax"; | |
| 92 | - } | |
| 93 | - | |
| 94 | - return sessionId || null; | |
| 73 | + return sessionId; | |
| 95 | 74 | }, |
| 96 | 75 | |
| 97 | 76 | // Lazy session initializer — called on first user interaction |
| 98 | 77 | ensureSession: function(botId) { |
| @@ -102,10 +81,11 @@ | ||
| 102 | 81 | if (instance.sessionId) { |
| 103 | 82 | return instance.sessionId; |
| 104 | 83 | } |
| 105 | 84 | |
| 106 | - // Check for existing session from cookie or localStorage | |
| 107 | - var existingSession = this.getChatSession(botId); | |
| 85 | + // Check if a cookie already exists from a prior visit | |
| 86 | + var cookieName = 'mxchat_session_id_' + botId; | |
| 87 | + var existingSession = getCookie(cookieName); | |
| 108 | 88 | |
| 109 | 89 | if (existingSession) { |
| 110 | 90 | instance.sessionId = existingSession; |
| 111 | 91 | } else { |
| @@ -118,10 +98,12 @@ | ||
| 118 | 98 | // Now that we have a session, do the deferred work |
| 119 | 99 | refreshNonceIfNeeded(); |
| 120 | 100 | trackOriginatingPage(); |
| 121 | 101 | |
| 122 | - // Note: loadChatHistory is handled by showChatContainerForBot with loader UI, | |
| 123 | - // so we do NOT call it here to avoid a race condition. | |
| 102 | + var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 103 | + if (chatPersistenceEnabled && mxchatChat.email_collection_enabled !== 'on') { | |
| 104 | + loadChatHistory(botId); | |
| 105 | + } | |
| 124 | 106 | |
| 125 | 107 | return instance.sessionId; |
| 126 | 108 | }, |
| 127 | 109 | |
| @@ -126,11 +108,9 @@ | ||
| 126 | 108 | }, |
| 127 | 109 | |
| 128 | 110 | setChatSession: function(botId, sessionId) { |
| 129 | 111 | var cookieName = 'mxchat_session_id_' + botId; |
| 130 | - var storageKey = 'mxchat_session_id_' + botId; | |
| 131 | 112 | document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax"; |
| 132 | - try { localStorage.setItem(storageKey, sessionId); } catch (e) {} | |
| 133 | 113 | if (this.instances[botId]) { |
| 134 | 114 | this.instances[botId].sessionId = sessionId; |
| 135 | 115 | } |
| 136 | 116 | }, |
| @@ -135,10 +115,8 @@ | ||
| 135 | 115 | } |
| 136 | 116 | }, |
| 137 | 117 | |
| 138 | 118 | resetChatSession: function(botId) { |
| 139 | - // Clear old session from localStorage before setting new one | |
| 140 | - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {} | |
| 141 | 119 | var newSessionId = generateSessionId(); |
| 142 | 120 | this.setChatSession(botId, newSessionId); |
| 143 | 121 | var $chatBox = getElement(botId, 'chat-box'); |
| 144 | 122 | if ($chatBox.length) { |
| @@ -147,20 +125,8 @@ | ||
| 147 | 125 | if (this.instances[botId]) { |
| 148 | 126 | this.instances[botId].chatHistoryLoaded = false; |
| 149 | 127 | this.instances[botId].processedMessageIds = new Set(); |
| 150 | 128 | } |
| 151 | - }, | |
| 152 | - | |
| 153 | - // Silent reset — new session ID without clearing the chat UI | |
| 154 | - // Used when IP changes mid-conversation so the user doesn't see messages vanish | |
| 155 | - silentResetSession: function(botId) { | |
| 156 | - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {} | |
| 157 | - var newSessionId = generateSessionId(); | |
| 158 | - this.setChatSession(botId, newSessionId); | |
| 159 | - if (this.instances[botId]) { | |
| 160 | - this.instances[botId].sessionId = newSessionId; | |
| 161 | - } | |
| 162 | - return newSessionId; | |
| 163 | 129 | } |
| 164 | 130 | }; |
| 165 | 131 | |
| 166 | 132 | // ==================================== |
| @@ -612,23 +578,13 @@ | ||
| 612 | 578 | |
| 613 | 579 | // Get instance for session start timestamp (used when persistence is OFF) |
| 614 | 580 | var instance = MxChatInstances.get(botId); |
| 615 | 581 | |
| 616 | - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent | |
| 617 | - // and returns the guaranteed-present session id from the in-memory instance even when | |
| 618 | - // cookie/localStorage writes are silently blocked by the browser. | |
| 619 | - var sessionId = MxChatInstances.ensureSession(botId); | |
| 620 | - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') { | |
| 621 | - // Last-resort generation to ensure we never POST a null marker. | |
| 622 | - sessionId = generateSessionId(); | |
| 623 | - MxChatInstances.setChatSession(botId, sessionId); | |
| 624 | - } | |
| 625 | - | |
| 626 | 582 | // Prepare AJAX data |
| 627 | 583 | const ajaxData = { |
| 628 | 584 | action: 'mxchat_handle_chat_request', |
| 629 | 585 | message: message, |
| 630 | - session_id: sessionId, | |
| 586 | + session_id: getChatSession(botId), | |
| 631 | 587 | nonce: mxchatChat.nonce, |
| 632 | 588 | current_page_url: window.location.href, |
| 633 | 589 | current_page_title: document.title, |
| 634 | 590 | bot_id: botId, |
| @@ -690,16 +646,23 @@ | ||
| 690 | 646 | errorMessage = "An error occurred. Please try again or contact support."; |
| 691 | 647 | } |
| 692 | 648 | |
| 693 | 649 | // Handle session reset action (IP changed, session expired, etc.) |
| 694 | - // Silent reset — keep chat UI intact, just get a new session and retry | |
| 695 | 650 | if (response.data && response.data.action === 'reset_session') { |
| 696 | - MxChatInstances.silentResetSession(botId); | |
| 697 | - // Re-send the original message with the new session (user message is already displayed) | |
| 651 | + // Clear the old session and generate a new one | |
| 652 | + resetChatSession(botId); | |
| 653 | + // Remove the temporary loading message | |
| 654 | + getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove(); | |
| 655 | + // Re-send the original message with the new session | |
| 698 | 656 | var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message'); |
| 699 | 657 | if (originalMessage) { |
| 700 | 658 | getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null); |
| 701 | - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 659 | + // Re-add the user message and thinking indicator | |
| 660 | + appendMessage("user", originalMessage, '', [], false, botId); | |
| 661 | + appendThinkingMessage(botId); | |
| 662 | + scrollToBottom(botId); | |
| 663 | + // Determine whether to use streaming | |
| 664 | + const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 702 | 665 | if (shouldUseStreaming(currentModel)) { |
| 703 | 666 | callMxChatStream(originalMessage, function(response) { |
| 704 | 667 | getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message'); |
| 705 | 668 | }, botId); |
| @@ -861,22 +824,12 @@ | ||
| 861 | 824 | |
| 862 | 825 | // Get instance for session start timestamp (used when persistence is OFF) |
| 863 | 826 | var instance = MxChatInstances.get(botId); |
| 864 | 827 | |
| 865 | - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any | |
| 866 | - // non-string value via String(), so passing `null` would POST the literal string "null" | |
| 867 | - // and land in the transcripts table as a ghost session. ensureSession() always returns | |
| 868 | - // a real string even when cookies/localStorage are blocked. | |
| 869 | - var streamSessionId = MxChatInstances.ensureSession(botId); | |
| 870 | - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') { | |
| 871 | - streamSessionId = generateSessionId(); | |
| 872 | - MxChatInstances.setChatSession(botId, streamSessionId); | |
| 873 | - } | |
| 874 | - | |
| 875 | 828 | const formData = new FormData(); |
| 876 | 829 | formData.append('action', 'mxchat_stream_chat'); |
| 877 | 830 | formData.append('message', message); |
| 878 | - formData.append('session_id', streamSessionId); | |
| 831 | + formData.append('session_id', getChatSession(botId)); | |
| 879 | 832 | formData.append('nonce', mxchatChat.nonce); |
| 880 | 833 | formData.append('current_page_url', window.location.href); |
| 881 | 834 | formData.append('current_page_title', document.title); |
| 882 | 835 | formData.append('bot_id', botId); |
| @@ -976,16 +929,8 @@ | ||
| 976 | 929 | |
| 977 | 930 | // Re-enable chat input when stream ends with content |
| 978 | 931 | enableChatInput(botId); |
| 979 | 932 | |
| 980 | - // Scroll the user's last message to the top now that the | |
| 981 | - // bot's full reply has rendered (gives max reading room). | |
| 982 | - var $chatBoxDone = getElement(botId, 'chat-box'); | |
| 983 | - var $lastUserMsgDone = $chatBoxDone.find('.user-message').last(); | |
| 984 | - if ($lastUserMsgDone.length) { | |
| 985 | - scrollElementToTop($lastUserMsgDone, botId); | |
| 986 | - } | |
| 987 | - | |
| 988 | 933 | if (callback) { |
| 989 | 934 | callback(accumulatedContent); |
| 990 | 935 | } |
| 991 | 936 | return; |
| @@ -1008,16 +953,8 @@ | ||
| 1008 | 953 | |
| 1009 | 954 | // Re-enable chat input after streaming completes |
| 1010 | 955 | enableChatInput(botId); |
| 1011 | 956 | |
| 1012 | - // Scroll the user's last message to the top now | |
| 1013 | - // that the bot's full reply has rendered. | |
| 1014 | - var $chatBoxStreamDone = getElement(botId, 'chat-box'); | |
| 1015 | - var $lastUserMsgStreamDone = $chatBoxStreamDone.find('.user-message').last(); | |
| 1016 | - if ($lastUserMsgStreamDone.length) { | |
| 1017 | - scrollElementToTop($lastUserMsgStreamDone, botId); | |
| 1018 | - } | |
| 1019 | - | |
| 1020 | 957 | if (callback) { |
| 1021 | 958 | callback(accumulatedContent); |
| 1022 | 959 | } |
| 1023 | 960 | return; |
| @@ -1136,16 +1073,21 @@ | ||
| 1136 | 1073 | errorMessage = "An error occurred. Please try again or contact support."; |
| 1137 | 1074 | } |
| 1138 | 1075 | |
| 1139 | 1076 | // Handle session reset action (IP changed, session expired, etc.) |
| 1140 | - // Silent reset — keep chat UI intact, just get a new session and retry | |
| 1141 | 1077 | if (data.data && data.data.action === 'reset_session') { |
| 1142 | - MxChatInstances.silentResetSession(botId); | |
| 1143 | - // Re-send the original message with the new session (user message is already displayed) | |
| 1078 | + // Clear the old session and generate a new one | |
| 1079 | + resetChatSession(botId); | |
| 1080 | + // Re-send the original message with the new session | |
| 1144 | 1081 | var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message'); |
| 1145 | 1082 | if (originalMessage) { |
| 1146 | 1083 | getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null); |
| 1147 | - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 1084 | + // Re-add the user message and thinking indicator | |
| 1085 | + appendMessage("user", originalMessage, '', [], false, botId); | |
| 1086 | + appendThinkingMessage(botId); | |
| 1087 | + scrollToBottom(botId); | |
| 1088 | + // Determine whether to use streaming | |
| 1089 | + const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 1148 | 1090 | if (shouldUseStreaming(currentModel)) { |
| 1149 | 1091 | callMxChatStream(originalMessage, callback, botId); |
| 1150 | 1092 | } else { |
| 1151 | 1093 | callMxChat(originalMessage, callback, botId); |
| @@ -1327,247 +1269,8 @@ | ||
| 1327 | 1269 | } |
| 1328 | 1270 | }); |
| 1329 | 1271 | |
| 1330 | 1272 | |
| 1331 | -// Tags the chat-box transcript so the print stylesheet can target it, | |
| 1332 | -// and lazily initializes the header overflow menu for this bot if the | |
| 1333 | -// markup is present but not yet wired (covers dynamically-rendered widgets). | |
| 1334 | -// Idempotent; safe to call on every appended message. | |
| 1335 | -function mxchatEnsurePrintRoot(botId) { | |
| 1336 | - var $chatBox = getElement(botId, 'chat-box'); | |
| 1337 | - if (!$chatBox || !$chatBox.length) return; | |
| 1338 | - $chatBox.addClass('mxchat-conversation-print-root'); | |
| 1339 | - if (!$chatBox.attr('data-print-title')) { | |
| 1340 | - var nowStr = new Date().toLocaleString(); | |
| 1341 | - var headerTitle = ((typeof mxchatChat !== 'undefined' && mxchatChat.print_header_title) || 'Chat transcript') + ' — ' + nowStr; | |
| 1342 | - $chatBox.attr('data-print-title', headerTitle); | |
| 1343 | - } | |
| 1344 | - if (typeof mxchatInitHeaderMenu === 'function') { | |
| 1345 | - mxchatInitHeaderMenu(botId); | |
| 1346 | - } | |
| 1347 | -} | |
| 1348 | - | |
| 1349 | -// Builds the list of overflow-menu items for a given bot. | |
| 1350 | -// Adding a future item is one push to this array — do NOT hardcode "only download." | |
| 1351 | -function mxchatGetHeaderMenuItems(botId) { | |
| 1352 | - var items = []; | |
| 1353 | - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {}; | |
| 1354 | - | |
| 1355 | - // The `print_button_*` keys still gate this item for back-compat with | |
| 1356 | - // existing user options. The action is now a transcript download, not print. | |
| 1357 | - if (settings.print_button_enabled === 'on') { | |
| 1358 | - items.push({ | |
| 1359 | - id: 'download-transcript', | |
| 1360 | - label: settings.print_button_label || 'Download Transcript', | |
| 1361 | - 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>', | |
| 1362 | - action: function() { | |
| 1363 | - mxchatDownloadTranscript(botId); | |
| 1364 | - } | |
| 1365 | - }); | |
| 1366 | - } | |
| 1367 | - | |
| 1368 | - return items; | |
| 1369 | -} | |
| 1370 | - | |
| 1371 | -// Builds a clean markdown transcript of the current conversation and triggers | |
| 1372 | -// a file download. Used by the "Download Transcript" menu item. | |
| 1373 | -function mxchatDownloadTranscript(botId) { | |
| 1374 | - var $chatBox = getElement(botId, 'chat-box'); | |
| 1375 | - if (!$chatBox || !$chatBox.length) return; | |
| 1376 | - | |
| 1377 | - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {}; | |
| 1378 | - var headerTitle = settings.print_header_title || 'Chat transcript'; | |
| 1379 | - var now = new Date(); | |
| 1380 | - var stamp = now.toLocaleString(); | |
| 1381 | - | |
| 1382 | - var lines = []; | |
| 1383 | - lines.push('# ' + headerTitle); | |
| 1384 | - lines.push(''); | |
| 1385 | - lines.push('Exported: ' + stamp); | |
| 1386 | - lines.push(''); | |
| 1387 | - lines.push('---'); | |
| 1388 | - lines.push(''); | |
| 1389 | - | |
| 1390 | - $chatBox.find('.user-message, .bot-message, .agent-message').each(function() { | |
| 1391 | - var $msg = $(this); | |
| 1392 | - // Skip thinking placeholders and any in-flight temporary messages. | |
| 1393 | - if ($msg.find('.thinking-dots').length) return; | |
| 1394 | - if ($msg.hasClass('temporary-message')) return; | |
| 1395 | - | |
| 1396 | - var sender; | |
| 1397 | - if ($msg.hasClass('user-message')) sender = 'User'; | |
| 1398 | - else if ($msg.hasClass('agent-message')) sender = 'Live Agent'; | |
| 1399 | - else sender = 'AI Agent'; | |
| 1400 | - | |
| 1401 | - // Strip interactive UI from the cloned message so we get the conversation text. | |
| 1402 | - var $clone = $msg.clone(); | |
| 1403 | - $clone.find('.copy-button, .message-toolbar, .mxchat-copy, button, script, style').remove(); | |
| 1404 | - var text = $clone.text().replace(/ /g, ' ').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim(); | |
| 1405 | - if (!text) return; | |
| 1406 | - | |
| 1407 | - lines.push('**' + sender + '**'); | |
| 1408 | - lines.push(''); | |
| 1409 | - lines.push(text); | |
| 1410 | - lines.push(''); | |
| 1411 | - }); | |
| 1412 | - | |
| 1413 | - var content = lines.join('\n'); | |
| 1414 | - var iso = now.toISOString().replace(/[:.]/g, '-').slice(0, 19); | |
| 1415 | - var fname = 'mxchat-transcript-' + iso + '.md'; | |
| 1416 | - var blob = new Blob([content], { type: 'text/markdown;charset=utf-8' }); | |
| 1417 | - var url = URL.createObjectURL(blob); | |
| 1418 | - var a = document.createElement('a'); | |
| 1419 | - a.href = url; | |
| 1420 | - a.download = fname; | |
| 1421 | - a.style.display = 'none'; | |
| 1422 | - document.body.appendChild(a); | |
| 1423 | - a.click(); | |
| 1424 | - setTimeout(function() { | |
| 1425 | - if (a.parentNode) a.parentNode.removeChild(a); | |
| 1426 | - URL.revokeObjectURL(url); | |
| 1427 | - }, 100); | |
| 1428 | -} | |
| 1429 | - | |
| 1430 | -// Reads the bot bubble's actual computed bg+fg and writes them as CSS vars | |
| 1431 | -// on the menu wrap, so the dropdown matches whatever paints the bubble — | |
| 1432 | -// saved options, AI theme CSS, or the mxchat-theme add-on. | |
| 1433 | -function mxchatSyncMenuColors(botId, $wrap) { | |
| 1434 | - if (!$wrap || !$wrap.length) return; | |
| 1435 | - var $bot = $wrap.closest('.mxchat-chatbot-wrapper').find('.bot-message').not('.temporary-message').first(); | |
| 1436 | - if (!$bot.length) return; | |
| 1437 | - var cs = window.getComputedStyle($bot[0]); | |
| 1438 | - if (cs.backgroundColor && cs.backgroundColor !== 'rgba(0, 0, 0, 0)' && cs.backgroundColor !== 'transparent') { | |
| 1439 | - $wrap[0].style.setProperty('--mxchat-menu-bg', cs.backgroundColor); | |
| 1440 | - } | |
| 1441 | - // Bot text color usually lives on a child div, not .bot-message itself. | |
| 1442 | - var $textChild = $bot.find('[style*="color"]').first(); | |
| 1443 | - var fg = ($textChild.length ? window.getComputedStyle($textChild[0]).color : cs.color); | |
| 1444 | - if (fg) $wrap[0].style.setProperty('--mxchat-menu-fg', fg); | |
| 1445 | -} | |
| 1446 | - | |
| 1447 | -// One-time per-widget init: renders menu items, wires open/close, | |
| 1448 | -// outside-click, Escape, and arrow-key navigation. If no items, hides the trigger. | |
| 1449 | -function mxchatInitHeaderMenu(botId) { | |
| 1450 | - var $wrap = $('.mxchat-header-menu-wrap[data-bot-id="' + botId + '"]').first(); | |
| 1451 | - if (!$wrap.length || $wrap.data('mxchatMenuReady')) return; | |
| 1452 | - | |
| 1453 | - var $trigger = $wrap.find('.mxchat-menu-trigger'); | |
| 1454 | - var $menu = $wrap.find('.mxchat-header-menu'); | |
| 1455 | - var items = mxchatGetHeaderMenuItems(botId); | |
| 1456 | - | |
| 1457 | - // Initial color sync — covers normal page load. | |
| 1458 | - mxchatSyncMenuColors(botId, $wrap); | |
| 1459 | - | |
| 1460 | - if (!items.length) { | |
| 1461 | - $trigger.hide(); | |
| 1462 | - $menu.hide(); | |
| 1463 | - $wrap.data('mxchatMenuReady', true); | |
| 1464 | - return; | |
| 1465 | - } | |
| 1466 | - | |
| 1467 | - // Build the menu items. | |
| 1468 | - $menu.empty(); | |
| 1469 | - items.forEach(function(item, idx) { | |
| 1470 | - var $btn = $('<button>', { | |
| 1471 | - type: 'button', | |
| 1472 | - 'class': 'mxchat-menu-item', | |
| 1473 | - 'role': 'menuitem', | |
| 1474 | - 'tabindex': '-1', | |
| 1475 | - 'data-menu-id': item.id, | |
| 1476 | - html: '<span class="mxchat-menu-item-icon">' + item.icon + '</span>' + | |
| 1477 | - '<span class="mxchat-menu-item-label"></span>' | |
| 1478 | - }); | |
| 1479 | - $btn.find('.mxchat-menu-item-label').text(item.label); | |
| 1480 | - $btn.on('click', function(e) { | |
| 1481 | - e.preventDefault(); | |
| 1482 | - e.stopPropagation(); | |
| 1483 | - closeMenu(); | |
| 1484 | - try { item.action(); } catch (err) { /* no-op */ } | |
| 1485 | - }); | |
| 1486 | - $menu.append($btn); | |
| 1487 | - }); | |
| 1488 | - | |
| 1489 | - function openMenu() { | |
| 1490 | - // Re-sync each open in case the active theme changed since init. | |
| 1491 | - mxchatSyncMenuColors(botId, $wrap); | |
| 1492 | - $menu.prop('hidden', false).attr('aria-hidden', 'false').addClass('is-open'); | |
| 1493 | - $trigger.attr('aria-expanded', 'true'); | |
| 1494 | - // Focus the first item for keyboard users | |
| 1495 | - setTimeout(function() { | |
| 1496 | - $menu.find('.mxchat-menu-item').first().attr('tabindex', '0').trigger('focus'); | |
| 1497 | - }, 0); | |
| 1498 | - } | |
| 1499 | - function closeMenu(returnFocus) { | |
| 1500 | - $menu.prop('hidden', true).attr('aria-hidden', 'true').removeClass('is-open'); | |
| 1501 | - $trigger.attr('aria-expanded', 'false'); | |
| 1502 | - $menu.find('.mxchat-menu-item').attr('tabindex', '-1'); | |
| 1503 | - if (returnFocus) $trigger.trigger('focus'); | |
| 1504 | - } | |
| 1505 | - | |
| 1506 | - // Toggle on trigger click — stop propagation so the .chatbot-top-bar | |
| 1507 | - // click-to-collapse handler does not fire. | |
| 1508 | - $trigger.on('click', function(e) { | |
| 1509 | - e.preventDefault(); | |
| 1510 | - e.stopPropagation(); | |
| 1511 | - if ($menu.hasClass('is-open')) closeMenu(); | |
| 1512 | - else openMenu(); | |
| 1513 | - }); | |
| 1514 | - | |
| 1515 | - // Don't let clicks inside the menu bubble to the top-bar collapse handler. | |
| 1516 | - $menu.on('click', function(e) { | |
| 1517 | - e.stopPropagation(); | |
| 1518 | - }); | |
| 1519 | - | |
| 1520 | - // Outside click closes the menu. | |
| 1521 | - $(document).on('click.mxchatMenu-' + botId, function(e) { | |
| 1522 | - if (!$menu.hasClass('is-open')) return; | |
| 1523 | - if ($wrap.has(e.target).length || $wrap.is(e.target)) return; | |
| 1524 | - closeMenu(); | |
| 1525 | - }); | |
| 1526 | - | |
| 1527 | - // Keyboard: Escape closes and returns focus; arrow keys move focus; Enter activates. | |
| 1528 | - $menu.on('keydown', '.mxchat-menu-item', function(e) { | |
| 1529 | - var $items = $menu.find('.mxchat-menu-item'); | |
| 1530 | - var idx = $items.index(this); | |
| 1531 | - if (e.key === 'Escape') { | |
| 1532 | - e.preventDefault(); | |
| 1533 | - closeMenu(true); | |
| 1534 | - } else if (e.key === 'ArrowDown') { | |
| 1535 | - e.preventDefault(); | |
| 1536 | - var $next = $items.eq((idx + 1) % $items.length); | |
| 1537 | - $items.attr('tabindex', '-1'); | |
| 1538 | - $next.attr('tabindex', '0').trigger('focus'); | |
| 1539 | - } else if (e.key === 'ArrowUp') { | |
| 1540 | - e.preventDefault(); | |
| 1541 | - var $prev = $items.eq((idx - 1 + $items.length) % $items.length); | |
| 1542 | - $items.attr('tabindex', '-1'); | |
| 1543 | - $prev.attr('tabindex', '0').trigger('focus'); | |
| 1544 | - } else if (e.key === 'Enter' || e.key === ' ') { | |
| 1545 | - e.preventDefault(); | |
| 1546 | - $(this).trigger('click'); | |
| 1547 | - } | |
| 1548 | - }); | |
| 1549 | - $trigger.on('keydown', function(e) { | |
| 1550 | - if (e.key === 'Escape' && $menu.hasClass('is-open')) { | |
| 1551 | - e.preventDefault(); | |
| 1552 | - closeMenu(true); | |
| 1553 | - } else if ((e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') && !$menu.hasClass('is-open')) { | |
| 1554 | - e.preventDefault(); | |
| 1555 | - openMenu(); | |
| 1556 | - } | |
| 1557 | - }); | |
| 1558 | - | |
| 1559 | - $wrap.data('mxchatMenuReady', true); | |
| 1560 | -} | |
| 1561 | - | |
| 1562 | -// Initialize header menus for every rendered widget on DOM ready. | |
| 1563 | -$(function() { | |
| 1564 | - $('.mxchat-header-menu-wrap').each(function() { | |
| 1565 | - var botId = $(this).data('bot-id'); | |
| 1566 | - if (botId) mxchatInitHeaderMenu(botId); | |
| 1567 | - }); | |
| 1568 | -}); | |
| 1569 | - | |
| 1570 | 1273 | function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false, botId = 'default') { |
| 1571 | 1274 | try { |
| 1572 | 1275 | // Determine styles based on sender type |
| 1573 | 1276 | let messageClass, bgColor, fontColor; |
| @@ -1605,12 +1308,17 @@ | ||
| 1605 | 1308 | 'margin-bottom': '1em' |
| 1606 | 1309 | }); |
| 1607 | 1310 | } |
| 1608 | 1311 | |
| 1609 | - // Process the message content - always run linkify to convert markdown | |
| 1610 | - // links and format text. linkify() handles existing HTML safely via | |
| 1611 | - // negative lookaheads that skip URLs already inside <a> tags. | |
| 1612 | - let fullMessage = linkify(messageText); | |
| 1312 | + // Process the message content based on sender | |
| 1313 | + let fullMessage; | |
| 1314 | + if (sender === "user") { | |
| 1315 | + // For user messages, apply linkify after sanitization | |
| 1316 | + fullMessage = linkify(messageText); | |
| 1317 | + } else { | |
| 1318 | + // For bot/agent messages, preserve HTML | |
| 1319 | + fullMessage = messageText; | |
| 1320 | + } | |
| 1613 | 1321 | |
| 1614 | 1322 | // Add images if provided |
| 1615 | 1323 | if (images && images.length > 0) { |
| 1616 | 1324 | fullMessage += '<div class="image-gallery" dir="auto">'; |
| @@ -1659,12 +1367,8 @@ | ||
| 1659 | 1367 | if (lastUserMessage.length) { |
| 1660 | 1368 | scrollElementToTop(lastUserMessage, botId); |
| 1661 | 1369 | } |
| 1662 | 1370 | } |
| 1663 | - | |
| 1664 | - if ((sender === "bot" || sender === "agent") && !isTemporary) { | |
| 1665 | - mxchatEnsurePrintRoot(botId); | |
| 1666 | - } | |
| 1667 | 1371 | }); |
| 1668 | 1372 | |
| 1669 | 1373 | if (messageText.id) { |
| 1670 | 1374 | var instance = MxChatInstances.get(botId); |
| @@ -1749,12 +1453,26 @@ | ||
| 1749 | 1453 | bgColor = botMessageBgColor; |
| 1750 | 1454 | fontColor = botMessageFontColor; |
| 1751 | 1455 | } |
| 1752 | 1456 | |
| 1753 | - // Always run linkify to convert markdown links and format text. | |
| 1754 | - // linkify() already handles existing HTML (its URL patterns use negative lookaheads | |
| 1755 | - // to avoid double-processing URLs that are already inside <a> tags). | |
| 1756 | - var fullMessage = linkify(responseText); | |
| 1457 | + // FIXED: Only linkify if response doesn't already contain HTML links or tags | |
| 1458 | + // This prevents double-processing of URLs that are already formatted as HTML | |
| 1459 | + var fullMessage; | |
| 1460 | + if (sender === "user") { | |
| 1461 | + // Always linkify user messages (they're plain text) | |
| 1462 | + fullMessage = linkify(responseText); | |
| 1463 | + } else { | |
| 1464 | + // For bot/agent messages, check if HTML already exists | |
| 1465 | + if (responseText.includes('<a href=') || responseText.includes('</a>') || | |
| 1466 | + responseText.includes('<img') || responseText.includes('<div') || | |
| 1467 | + responseText.includes('<p>') || responseText.includes('<br>')) { | |
| 1468 | + // Response already has HTML, don't process it | |
| 1469 | + fullMessage = responseText; | |
| 1470 | + } else { | |
| 1471 | + // Plain text response, apply linkify | |
| 1472 | + fullMessage = linkify(responseText); | |
| 1473 | + } | |
| 1474 | + } | |
| 1757 | 1475 | |
| 1758 | 1476 | if (responseHtml) { |
| 1759 | 1477 | // Only add line breaks if there's actual text content before the HTML |
| 1760 | 1478 | if (fullMessage && fullMessage.trim()) { |
| @@ -1811,12 +1529,8 @@ | ||
| 1811 | 1529 | } |
| 1812 | 1530 | |
| 1813 | 1531 | // Re-enable chat input after response is displayed |
| 1814 | 1532 | enableChatInput(botId); |
| 1815 | - | |
| 1816 | - if (sender === "bot" || sender === "agent") { | |
| 1817 | - mxchatEnsurePrintRoot(botId); | |
| 1818 | - } | |
| 1819 | 1533 | } else { |
| 1820 | 1534 | appendMessage(sender, responseText, responseHtml, images, false, botId); |
| 1821 | 1535 | // Re-enable chat input after response is displayed |
| 1822 | 1536 | enableChatInput(botId); |
| @@ -1919,63 +1633,37 @@ | ||
| 1919 | 1633 | // Return as a proper link without the brackets |
| 1920 | 1634 | return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`; |
| 1921 | 1635 | }); |
| 1922 | 1636 | |
| 1923 | - // Process markdown links: [text](url) and [](url) | |
| 1924 | - // Uses balanced parenthesis matching to handle URLs containing parens | |
| 1925 | - // (e.g. PDF filenames with dates like (2025-08-28).pdf) | |
| 1926 | - processedText = (function(input) { | |
| 1927 | - var result = ''; | |
| 1928 | - var i = 0; | |
| 1929 | - while (i < input.length) { | |
| 1930 | - // Look for [ at current position | |
| 1931 | - if (input[i] === '[') { | |
| 1932 | - // Find closing ] | |
| 1933 | - var closeBracket = input.indexOf(']', i + 1); | |
| 1934 | - if (closeBracket === -1 || closeBracket + 1 >= input.length || input[closeBracket + 1] !== '(') { | |
| 1935 | - result += input[i]; | |
| 1936 | - i++; | |
| 1937 | - continue; | |
| 1938 | - } | |
| 1939 | - var linkText = input.substring(i + 1, closeBracket); | |
| 1940 | - // Check if URL starts with http | |
| 1941 | - var urlStart = closeBracket + 2; | |
| 1942 | - if (!input.substring(urlStart).match(/^https?:\/\//)) { | |
| 1943 | - result += input[i]; | |
| 1944 | - i++; | |
| 1945 | - continue; | |
| 1946 | - } | |
| 1947 | - // Find balanced closing paren | |
| 1948 | - var depth = 1; | |
| 1949 | - var j = urlStart; | |
| 1950 | - while (j < input.length && depth > 0) { | |
| 1951 | - if (input[j] === '(') depth++; | |
| 1952 | - else if (input[j] === ')') depth--; | |
| 1953 | - if (depth > 0) j++; | |
| 1954 | - } | |
| 1955 | - if (depth !== 0) { | |
| 1956 | - result += input[i]; | |
| 1957 | - i++; | |
| 1958 | - continue; | |
| 1959 | - } | |
| 1960 | - var url = input.substring(urlStart, j); | |
| 1961 | - var cleanUrl = url.replace(/[\].,;!?]+$/, ''); | |
| 1962 | - var encodedUrl = safeEncodeUrl(cleanUrl); | |
| 1963 | - if (!linkText || !linkText.trim()) { | |
| 1964 | - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + cleanUrl + '</a>'; | |
| 1965 | - } else { | |
| 1966 | - var safeText = sanitizeUserInput(linkText); | |
| 1967 | - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + safeText + '</a>'; | |
| 1968 | - } | |
| 1969 | - i = j + 1; // Skip past the closing ) | |
| 1970 | - } else { | |
| 1971 | - result += input[i]; | |
| 1972 | - i++; | |
| 1973 | - } | |
| 1637 | + // Process proper markdown links with text: [text](url) | |
| 1638 | + // This MUST have non-empty text in the first brackets | |
| 1639 | + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g; | |
| 1640 | + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => { | |
| 1641 | + // Make sure we have actual text (not just whitespace) | |
| 1642 | + if (!text || !text.trim()) { | |
| 1643 | + // If no text, treat the URL as the text | |
| 1644 | + let cleanUrl = url.replace(/[.,;!?]+$/, ''); | |
| 1645 | + const safeUrl = safeEncodeUrl(cleanUrl); | |
| 1646 | + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`; | |
| 1974 | 1647 | } |
| 1975 | - return result; | |
| 1976 | - })(processedText); | |
| 1648 | + | |
| 1649 | + // Clean the URL | |
| 1650 | + let cleanUrl = url.replace(/[\].,;!?]+$/, ''); | |
| 1651 | + const safeUrl = safeEncodeUrl(cleanUrl); | |
| 1652 | + const safeText = sanitizeUserInput(text); | |
| 1653 | + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`; | |
| 1654 | + }); | |
| 1977 | 1655 | |
| 1656 | + // Handle empty markdown links: [](url) | |
| 1657 | + // This is a specific case where there's no text | |
| 1658 | + const emptyMarkdownPattern = /\[\]\((https?:\/\/[^\s)]+)\)/g; | |
| 1659 | + processedText = processedText.replace(emptyMarkdownPattern, (match, url) => { | |
| 1660 | + let cleanUrl = url.replace(/[.,;!?]+$/, ''); | |
| 1661 | + const safeUrl = safeEncodeUrl(cleanUrl); | |
| 1662 | + // Use the URL itself as the link text | |
| 1663 | + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`; | |
| 1664 | + }); | |
| 1665 | + | |
| 1978 | 1666 | // Process phone numbers: [text](tel:number) |
| 1979 | 1667 | const phonePattern = /\[([^\]]+)\]\((tel:[\d+\-\s()]+)\)/g; |
| 1980 | 1668 | processedText = processedText.replace(phonePattern, (match, text, phone) => { |
| 1981 | 1669 | const safePhone = safeEncodeUrl(phone); |
| @@ -2269,14 +1957,13 @@ | ||
| 2269 | 1957 | requestAnimationFrame(smoothScroll); |
| 2270 | 1958 | } |
| 2271 | 1959 | } |
| 2272 | 1960 | |
| 2273 | - function scrollElementToTop(element, botId, topOffset) { | |
| 1961 | + function scrollElementToTop(element, botId) { | |
| 2274 | 1962 | botId = botId || 'default'; |
| 2275 | - topOffset = (typeof topOffset === 'number') ? topOffset : 2; | |
| 2276 | 1963 | var chatBox = getElement(botId, 'chat-box'); |
| 2277 | 1964 | var elementTop = element.position().top + chatBox.scrollTop(); |
| 2278 | - chatBox.animate({ scrollTop: Math.max(0, elementTop - topOffset) }, 500); | |
| 1965 | + chatBox.animate({ scrollTop: elementTop }, 500); | |
| 2279 | 1966 | } |
| 2280 | 1967 | |
| 2281 | 1968 | function showChatWidget(botId) { |
| 2282 | 1969 | botId = botId || 'default'; |
| @@ -2503,19 +2190,11 @@ | ||
| 2503 | 2190 | if (onComplete) onComplete(); |
| 2504 | 2191 | return; |
| 2505 | 2192 | } |
| 2506 | 2193 | |
| 2507 | - // Use getChatSession which returns null if no session exists (does NOT create one) | |
| 2508 | 2194 | var sessionId = getChatSession(botId); |
| 2509 | 2195 | var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on'; |
| 2510 | 2196 | |
| 2511 | - // No session yet — nothing to load. History will load after first message via ensureSession. | |
| 2512 | - if (!sessionId) { | |
| 2513 | - instance.chatHistoryLoaded = true; | |
| 2514 | - if (onComplete) onComplete(); | |
| 2515 | - return; | |
| 2516 | - } | |
| 2517 | - | |
| 2518 | 2197 | if (chatPersistenceEnabled && sessionId) { |
| 2519 | 2198 | $.ajax({ |
| 2520 | 2199 | url: mxchatChat.ajax_url, |
| 2521 | 2200 | type: 'POST', |
| @@ -2526,10 +2205,10 @@ | ||
| 2526 | 2205 | }, |
| 2527 | 2206 | success: function(response) { |
| 2528 | 2207 | // Handle session reset (IP changed while user was away) |
| 2529 | 2208 | if (response.success === false && response.data && response.data.action === 'reset_session') { |
| 2530 | - // Silent reset — new session but don't clear UI | |
| 2531 | - MxChatInstances.silentResetSession(botId); | |
| 2209 | + // Silently reset session - user will start fresh | |
| 2210 | + resetChatSession(botId); | |
| 2532 | 2211 | instance.chatHistoryLoaded = true; // Prevent retry loop |
| 2533 | 2212 | if (onComplete) onComplete(); |
| 2534 | 2213 | return; |
| 2535 | 2214 | } |
| @@ -2587,19 +2266,9 @@ | ||
| 2587 | 2266 | var content = message.content; |
| 2588 | 2267 | content = content.replace(/\\'/g, "'").replace(/\\"/g, '"'); |
| 2589 | 2268 | content = decodeHTMLEntities(content); |
| 2590 | 2269 | |
| 2591 | - // Skip linkify for messages containing structured HTML | |
| 2592 | - // (forms, product cards, galleries, etc.) to avoid | |
| 2593 | - // markdown formatting corrupting HTML attributes | |
| 2594 | - // (e.g. underscores in name="field_name" becoming <em> tags) | |
| 2595 | - if (content.includes("mxchat-product-card") || | |
| 2596 | - content.includes("mxchat-image-gallery") || | |
| 2597 | - content.includes("mxchat-featured-products") || | |
| 2598 | - content.includes("<form") || | |
| 2599 | - content.includes("<input") || | |
| 2600 | - content.includes("<select") || | |
| 2601 | - content.includes("<textarea")) { | |
| 2270 | + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) { | |
| 2602 | 2271 | messageElement.html(content); |
| 2603 | 2272 | } else { |
| 2604 | 2273 | var formattedContent = linkify(content); |
| 2605 | 2274 | messageElement.html(formattedContent); |
| @@ -2826,20 +2495,14 @@ | ||
| 2826 | 2495 | |
| 2827 | 2496 | function checkPreChatDismissal(botId) { |
| 2828 | 2497 | botId = botId || 'default'; |
| 2829 | 2498 | try { |
| 2830 | - var dismissedAt = localStorage.getItem('mxchat_pre_chat_dismissed_' + botId); | |
| 2831 | - if (dismissedAt) { | |
| 2832 | - // Re-show after 24 hours | |
| 2833 | - var elapsed = Date.now() - parseInt(dismissedAt, 10); | |
| 2834 | - if (elapsed < 86400000) { | |
| 2835 | - getElement(botId, 'pre-chat-message').hide(); | |
| 2836 | - return; | |
| 2837 | - } | |
| 2838 | - // Expired — clear and show again | |
| 2839 | - localStorage.removeItem('mxchat_pre_chat_dismissed_' + botId); | |
| 2499 | + var dismissed = localStorage.getItem('mxchat_pre_chat_dismissed_' + botId); | |
| 2500 | + if (!dismissed) { | |
| 2501 | + getElement(botId, 'pre-chat-message').fadeIn(250); | |
| 2502 | + } else { | |
| 2503 | + getElement(botId, 'pre-chat-message').hide(); | |
| 2840 | 2504 | } |
| 2841 | - getElement(botId, 'pre-chat-message').fadeIn(250); | |
| 2842 | 2505 | } catch (e) { |
| 2843 | 2506 | // localStorage unavailable — show the message |
| 2844 | 2507 | getElement(botId, 'pre-chat-message').fadeIn(250); |
| 2845 | 2508 | } |
| @@ -2848,9 +2511,9 @@ | ||
| 2848 | 2511 | function handlePreChatDismissal(botId) { |
| 2849 | 2512 | botId = botId || 'default'; |
| 2850 | 2513 | getElement(botId, 'pre-chat-message').fadeOut(200); |
| 2851 | 2514 | try { |
| 2852 | - localStorage.setItem('mxchat_pre_chat_dismissed_' + botId, String(Date.now())); | |
| 2515 | + localStorage.setItem('mxchat_pre_chat_dismissed_' + botId, '1'); | |
| 2853 | 2516 | } catch (e) { |
| 2854 | 2517 | // localStorage unavailable — dismissal won't persist |
| 2855 | 2518 | } |
| 2856 | 2519 | } |
| @@ -2921,14 +2584,8 @@ | ||
| 2921 | 2584 | $badge.hide(); // Hide notification when opening chat |
| 2922 | 2585 | disableScroll(); |
| 2923 | 2586 | $preChat.fadeOut(250); |
| 2924 | 2587 | |
| 2925 | - // Load chat history for returning visitors (persistence) | |
| 2926 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 2927 | - if (chatPersistenceEnabled) { | |
| 2928 | - MxChatInstances.ensureSession(botId); | |
| 2929 | - } | |
| 2930 | - | |
| 2931 | 2588 | // Deferred email check — only on first widget open |
| 2932 | 2589 | var emailBlocker = getElementDOM(botId, 'email-blocker'); |
| 2933 | 2590 | var instance = MxChatInstances.get(botId); |
| 2934 | 2591 | if (emailBlocker && !instance.emailCheckDone) { |
| @@ -2933,12 +2590,8 @@ | ||
| 2933 | 2590 | var instance = MxChatInstances.get(botId); |
| 2934 | 2591 | if (emailBlocker && !instance.emailCheckDone) { |
| 2935 | 2592 | instance.emailCheckDone = true; |
| 2936 | 2593 | resolveEmailState(botId); |
| 2937 | - } else if (!emailBlocker) { | |
| 2938 | - // No email collection — still route through showChatContainerForBot | |
| 2939 | - // so the loader is shown while chat history loads | |
| 2940 | - showChatContainerForBot(botId); | |
| 2941 | 2594 | } |
| 2942 | 2595 | } else { |
| 2943 | 2596 | $chatbot.removeClass('visible').addClass('hidden'); |
| 2944 | 2597 | $(this).removeClass('hidden'); |
| @@ -2957,9 +2610,11 @@ | ||
| 2957 | 2610 | |
| 2958 | 2611 | $(document).on('click', '.close-pre-chat-message', function(e) { |
| 2959 | 2612 | e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click |
| 2960 | 2613 | var botId = getBotIdFromElement(this); |
| 2961 | - handlePreChatDismissal(botId); | |
| 2614 | + getElement(botId, 'pre-chat-message').fadeOut(200, function() { | |
| 2615 | + $(this).remove(); | |
| 2616 | + }); | |
| 2962 | 2617 | }); |
| 2963 | 2618 | |
| 2964 | 2619 | |
| 2965 | 2620 | // PDF upload button handlers - use class selector |
| @@ -3160,59 +2815,8 @@ | ||
| 3160 | 2815 | }); |
| 3161 | 2816 | |
| 3162 | 2817 | |
| 3163 | 2818 | // ==================================== |
| 3164 | -// INIT LOADER & CHAT CONTAINER HELPERS | |
| 3165 | -// ==================================== | |
| 3166 | -// These must be outside the email collection block so they're always available | |
| 3167 | -// (used by persistence loading even when email collection is off) | |
| 3168 | - | |
| 3169 | -function showInitLoader(botId) { | |
| 3170 | - var loader = getElementDOM(botId, 'mxchat-init-loader'); | |
| 3171 | - if (loader) loader.style.display = 'flex'; | |
| 3172 | -} | |
| 3173 | - | |
| 3174 | -function hideInitLoader(botId) { | |
| 3175 | - var loader = getElementDOM(botId, 'mxchat-init-loader'); | |
| 3176 | - if (loader) loader.style.display = 'none'; | |
| 3177 | -} | |
| 3178 | - | |
| 3179 | -function showEmailFormForBot(botId) { | |
| 3180 | - hideInitLoader(botId); | |
| 3181 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 3182 | - var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 3183 | - if (emailBlocker) emailBlocker.style.display = 'flex'; | |
| 3184 | - if (chatContainer) chatContainer.style.display = 'none'; | |
| 3185 | -} | |
| 3186 | - | |
| 3187 | -function showChatContainerForBot(botId) { | |
| 3188 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 3189 | - var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 3190 | - if (emailBlocker) emailBlocker.style.display = 'none'; | |
| 3191 | - | |
| 3192 | - var instance = MxChatInstances.get(botId); | |
| 3193 | - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on'; | |
| 3194 | - | |
| 3195 | - // If persistence is on and history hasn't loaded yet, show loader | |
| 3196 | - // while history loads to prevent flash of empty chat | |
| 3197 | - if (chatPersistenceEnabled && !instance.chatHistoryLoaded) { | |
| 3198 | - if (chatContainer) chatContainer.style.display = 'none'; | |
| 3199 | - showInitLoader(botId); | |
| 3200 | - loadChatHistory(botId, function() { | |
| 3201 | - hideInitLoader(botId); | |
| 3202 | - if (chatContainer) chatContainer.style.display = 'flex'; | |
| 3203 | - scrollToBottom(botId, true); | |
| 3204 | - }); | |
| 3205 | - } else { | |
| 3206 | - hideInitLoader(botId); | |
| 3207 | - if (chatContainer) chatContainer.style.display = 'flex'; | |
| 3208 | - if (typeof loadChatHistory === 'function') { | |
| 3209 | - loadChatHistory(botId); | |
| 3210 | - } | |
| 3211 | - } | |
| 3212 | -} | |
| 3213 | - | |
| 3214 | -// ==================================== | |
| 3215 | 2819 | // EMAIL COLLECTION SETUP - MULTI-INSTANCE VERSION |
| 3216 | 2820 | // ==================================== |
| 3217 | 2821 | // Only run email collection setup if it's enabled |
| 3218 | 2822 | if (mxchatChat && mxchatChat.email_collection_enabled === 'on') { |
| @@ -3248,8 +2852,40 @@ | ||
| 3248 | 2852 | `; |
| 3249 | 2853 | document.head.appendChild(style); |
| 3250 | 2854 | } |
| 3251 | 2855 | |
| 2856 | + // Helper functions for email collection (multi-instance aware) | |
| 2857 | + function showEmailFormForBot(botId) { | |
| 2858 | + var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 2859 | + var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 2860 | + if (emailBlocker) emailBlocker.style.display = 'flex'; | |
| 2861 | + if (chatContainer) chatContainer.style.display = 'none'; | |
| 2862 | + } | |
| 2863 | + | |
| 2864 | + function showChatContainerForBot(botId) { | |
| 2865 | + var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 2866 | + var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 2867 | + if (emailBlocker) emailBlocker.style.display = 'none'; | |
| 2868 | + | |
| 2869 | + var instance = MxChatInstances.get(botId); | |
| 2870 | + var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on'; | |
| 2871 | + | |
| 2872 | + // If persistence is on and history hasn't loaded yet, keep container | |
| 2873 | + // hidden until history loads to prevent flash of empty chat | |
| 2874 | + if (chatPersistenceEnabled && !instance.chatHistoryLoaded) { | |
| 2875 | + if (chatContainer) chatContainer.style.display = 'none'; | |
| 2876 | + loadChatHistory(botId, function() { | |
| 2877 | + if (chatContainer) chatContainer.style.display = 'flex'; | |
| 2878 | + scrollToBottom(botId, true); | |
| 2879 | + }); | |
| 2880 | + } else { | |
| 2881 | + if (chatContainer) chatContainer.style.display = 'flex'; | |
| 2882 | + if (typeof loadChatHistory === 'function') { | |
| 2883 | + loadChatHistory(botId); | |
| 2884 | + } | |
| 2885 | + } | |
| 2886 | + } | |
| 2887 | + | |
| 3252 | 2888 | function isValidEmailAddress(email) { |
| 3253 | 2889 | const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 3254 | 2890 | return emailRegex.test(email.trim()) && email.length <= 254; |
| 3255 | 2891 | } |
| @@ -3385,16 +3021,15 @@ | ||
| 3385 | 3021 | } |
| 3386 | 3022 | } |
| 3387 | 3023 | |
| 3388 | 3024 | function checkSessionAndEmailForBot(botId) { |
| 3389 | - const sessionId = MxChatInstances.ensureSession(botId); | |
| 3025 | + const sessionId = getChatSession(botId); | |
| 3390 | 3026 | |
| 3391 | - // Hide both panels while we check — show loader instead | |
| 3027 | + // Hide both panels while we check — prevents flash of wrong state | |
| 3392 | 3028 | var emailBlocker = getElementDOM(botId, 'email-blocker'); |
| 3393 | 3029 | var chatContainer = getElementDOM(botId, 'chat-container'); |
| 3394 | 3030 | if (emailBlocker) emailBlocker.style.display = 'none'; |
| 3395 | 3031 | if (chatContainer) chatContainer.style.display = 'none'; |
| 3396 | - showInitLoader(botId); | |
| 3397 | 3032 | |
| 3398 | 3033 | fetch(mxchatChat.ajax_url, { |
| 3399 | 3034 | method: 'POST', |
| 3400 | 3035 | headers: { |
| @@ -3443,9 +3078,9 @@ | ||
| 3443 | 3078 | var emailInput = getElementDOM(botId, 'user-email'); |
| 3444 | 3079 | var nameInput = getElementDOM(botId, 'user-name'); |
| 3445 | 3080 | var userEmail = emailInput ? emailInput.value.trim() : ''; |
| 3446 | 3081 | var userName = nameInput ? nameInput.value.trim() : ''; |
| 3447 | - var sessionId = MxChatInstances.ensureSession(botId); | |
| 3082 | + var sessionId = getChatSession(botId); | |
| 3448 | 3083 | |
| 3449 | 3084 | // Validate email |
| 3450 | 3085 | if (!userEmail) { |
| 3451 | 3086 | showEmailError(botId, 'Please enter your email address.'); |
| @@ -3574,8 +3209,9 @@ | ||
| 3574 | 3209 | $('.mxchat-chatbot-wrapper').each(function() { |
| 3575 | 3210 | var botId = $(this).data('bot-id') || 'default'; |
| 3576 | 3211 | var emailBlocker = getElementDOM(botId, 'email-blocker'); |
| 3577 | 3212 | |
| 3213 | + // Only check if email blocker exists for this bot | |
| 3578 | 3214 | if (emailBlocker) { |
| 3579 | 3215 | if (isEmbeddedBot(botId)) { |
| 3580 | 3216 | // Embedded bots are always visible — check now |
| 3581 | 3217 | resolveEmailState(botId); |
| @@ -3580,15 +3216,8 @@ | ||
| 3580 | 3216 | // Embedded bots are always visible — check now |
| 3581 | 3217 | resolveEmailState(botId); |
| 3582 | 3218 | } |
| 3583 | 3219 | // Floating bots: handled in the widget open handler |
| 3584 | - } else if (isEmbeddedBot(botId)) { | |
| 3585 | - // Embedded bot, no email collection — load history with loader | |
| 3586 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 3587 | - if (chatPersistenceEnabled) { | |
| 3588 | - MxChatInstances.ensureSession(botId); | |
| 3589 | - showChatContainerForBot(botId); | |
| 3590 | - } | |
| 3591 | 3220 | } |
| 3592 | 3221 | }); |
| 3593 | 3222 | } |
| 3594 | 3223 | |
| @@ -3598,17 +3227,11 @@ | ||
| 3598 | 3227 | var $chatbot = getElement(botId, 'floating-chatbot'); |
| 3599 | 3228 | if ($chatbot.hasClass('hidden')) { |
| 3600 | 3229 | $chatbot.removeClass('hidden').addClass('visible'); |
| 3601 | 3230 | getElement(botId, 'floating-chatbot-button').addClass('hidden'); |
| 3602 | - handlePreChatDismissal(botId); | |
| 3231 | + $(this).fadeOut(250); // Hide pre-chat message | |
| 3603 | 3232 | disableScroll(); // Disable scroll when chatbot opens |
| 3604 | 3233 | |
| 3605 | - // Load chat history for returning visitors (persistence) | |
| 3606 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 3607 | - if (chatPersistenceEnabled) { | |
| 3608 | - MxChatInstances.ensureSession(botId); | |
| 3609 | - } | |
| 3610 | - | |
| 3611 | 3234 | // Deferred email check — only on first widget open |
| 3612 | 3235 | var emailBlocker = getElementDOM(botId, 'email-blocker'); |
| 3613 | 3236 | var instance = MxChatInstances.get(botId); |
| 3614 | 3237 | if (emailBlocker && !instance.emailCheckDone) { |
| @@ -3613,15 +3236,36 @@ | ||
| 3613 | 3236 | var instance = MxChatInstances.get(botId); |
| 3614 | 3237 | if (emailBlocker && !instance.emailCheckDone) { |
| 3615 | 3238 | instance.emailCheckDone = true; |
| 3616 | 3239 | resolveEmailState(botId); |
| 3617 | - } else if (!emailBlocker) { | |
| 3618 | - showChatContainerForBot(botId); | |
| 3619 | 3240 | } |
| 3620 | 3241 | } |
| 3621 | 3242 | }); |
| 3622 | 3243 | |
| 3623 | - // Legacy duplicate close handler removed — handled by single event delegation above | |
| 3244 | + // Dismiss pre-chat message via close button - handled by event delegation above at line ~2376 | |
| 3245 | + // This is a fallback for legacy support | |
| 3246 | + $(document).on('click', '.close-pre-chat-message', function() { | |
| 3247 | + var botId = getBotIdFromElement(this); | |
| 3248 | + var $preChat = getElement(botId, 'pre-chat-message'); | |
| 3249 | + $preChat.fadeOut(200); // Hide the message | |
| 3250 | + | |
| 3251 | + // Send an AJAX request to set the transient flag for 24 hours | |
| 3252 | + $.ajax({ | |
| 3253 | + url: mxchatChat.ajax_url, | |
| 3254 | + type: 'POST', | |
| 3255 | + data: { | |
| 3256 | + action: 'mxchat_dismiss_pre_chat_message', | |
| 3257 | + _ajax_nonce: mxchatChat.nonce | |
| 3258 | + }, | |
| 3259 | + success: function() { | |
| 3260 | + // Ensure the message is hidden after dismissal | |
| 3261 | + $preChat.hide(); | |
| 3262 | + }, | |
| 3263 | + error: function() { | |
| 3264 | + // Error dismissing pre-chat message - silently continue | |
| 3265 | + } | |
| 3266 | + }); | |
| 3267 | + }); | |
| 3624 | 3268 | |
| 3625 | 3269 | |
| 3626 | 3270 | function hasQuickQuestions(botId) { |
| 3627 | 3271 | botId = botId || 'default'; |