| @@ -105,10 +105,12 @@ | ||
| 105 | 105 | // Now that we have a session, do the deferred work |
| 106 | 106 | refreshNonceIfNeeded(); |
| 107 | 107 | trackOriginatingPage(); |
| 108 | 108 | |
| 109 | - // Note: loadChatHistory is handled by showChatContainerForBot with loader UI, | |
| 110 | - // so we do NOT call it here to avoid a race condition. | |
| 109 | + var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 110 | + if (chatPersistenceEnabled && mxchatChat.email_collection_enabled !== 'on') { | |
| 111 | + loadChatHistory(botId); | |
| 112 | + } | |
| 111 | 113 | |
| 112 | 114 | return instance.sessionId; |
| 113 | 115 | }, |
| 114 | 116 | |
| @@ -2290,19 +2292,9 @@ | ||
| 2290 | 2292 | var content = message.content; |
| 2291 | 2293 | content = content.replace(/\\'/g, "'").replace(/\\"/g, '"'); |
| 2292 | 2294 | content = decodeHTMLEntities(content); |
| 2293 | 2295 | |
| 2294 | - // Skip linkify for messages containing structured HTML | |
| 2295 | - // (forms, product cards, galleries, etc.) to avoid | |
| 2296 | - // markdown formatting corrupting HTML attributes | |
| 2297 | - // (e.g. underscores in name="field_name" becoming <em> tags) | |
| 2298 | - if (content.includes("mxchat-product-card") || | |
| 2299 | - content.includes("mxchat-image-gallery") || | |
| 2300 | - content.includes("mxchat-featured-products") || | |
| 2301 | - content.includes("<form") || | |
| 2302 | - content.includes("<input") || | |
| 2303 | - content.includes("<select") || | |
| 2304 | - content.includes("<textarea")) { | |
| 2296 | + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) { | |
| 2305 | 2297 | messageElement.html(content); |
| 2306 | 2298 | } else { |
| 2307 | 2299 | var formattedContent = linkify(content); |
| 2308 | 2300 | messageElement.html(formattedContent); |
| @@ -2636,12 +2628,8 @@ | ||
| 2636 | 2628 | var instance = MxChatInstances.get(botId); |
| 2637 | 2629 | if (emailBlocker && !instance.emailCheckDone) { |
| 2638 | 2630 | instance.emailCheckDone = true; |
| 2639 | 2631 | resolveEmailState(botId); |
| 2640 | - } else if (!emailBlocker) { | |
| 2641 | - // No email collection — still route through showChatContainerForBot | |
| 2642 | - // so the loader is shown while chat history loads | |
| 2643 | - showChatContainerForBot(botId); | |
| 2644 | 2632 | } |
| 2645 | 2633 | } else { |
| 2646 | 2634 | $chatbot.removeClass('visible').addClass('hidden'); |
| 2647 | 2635 | $(this).removeClass('hidden'); |
| @@ -2863,59 +2851,8 @@ | ||
| 2863 | 2851 | }); |
| 2864 | 2852 | |
| 2865 | 2853 | |
| 2866 | 2854 | // ==================================== |
| 2867 | -// INIT LOADER & CHAT CONTAINER HELPERS | |
| 2868 | -// ==================================== | |
| 2869 | -// These must be outside the email collection block so they're always available | |
| 2870 | -// (used by persistence loading even when email collection is off) | |
| 2871 | - | |
| 2872 | -function showInitLoader(botId) { | |
| 2873 | - var loader = getElementDOM(botId, 'mxchat-init-loader'); | |
| 2874 | - if (loader) loader.style.display = 'flex'; | |
| 2875 | -} | |
| 2876 | - | |
| 2877 | -function hideInitLoader(botId) { | |
| 2878 | - var loader = getElementDOM(botId, 'mxchat-init-loader'); | |
| 2879 | - if (loader) loader.style.display = 'none'; | |
| 2880 | -} | |
| 2881 | - | |
| 2882 | -function showEmailFormForBot(botId) { | |
| 2883 | - hideInitLoader(botId); | |
| 2884 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 2885 | - var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 2886 | - if (emailBlocker) emailBlocker.style.display = 'flex'; | |
| 2887 | - if (chatContainer) chatContainer.style.display = 'none'; | |
| 2888 | -} | |
| 2889 | - | |
| 2890 | -function showChatContainerForBot(botId) { | |
| 2891 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 2892 | - var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 2893 | - if (emailBlocker) emailBlocker.style.display = 'none'; | |
| 2894 | - | |
| 2895 | - var instance = MxChatInstances.get(botId); | |
| 2896 | - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on'; | |
| 2897 | - | |
| 2898 | - // If persistence is on and history hasn't loaded yet, show loader | |
| 2899 | - // while history loads to prevent flash of empty chat | |
| 2900 | - if (chatPersistenceEnabled && !instance.chatHistoryLoaded) { | |
| 2901 | - if (chatContainer) chatContainer.style.display = 'none'; | |
| 2902 | - showInitLoader(botId); | |
| 2903 | - loadChatHistory(botId, function() { | |
| 2904 | - hideInitLoader(botId); | |
| 2905 | - if (chatContainer) chatContainer.style.display = 'flex'; | |
| 2906 | - scrollToBottom(botId, true); | |
| 2907 | - }); | |
| 2908 | - } else { | |
| 2909 | - hideInitLoader(botId); | |
| 2910 | - if (chatContainer) chatContainer.style.display = 'flex'; | |
| 2911 | - if (typeof loadChatHistory === 'function') { | |
| 2912 | - loadChatHistory(botId); | |
| 2913 | - } | |
| 2914 | - } | |
| 2915 | -} | |
| 2916 | - | |
| 2917 | -// ==================================== | |
| 2918 | 2855 | // EMAIL COLLECTION SETUP - MULTI-INSTANCE VERSION |
| 2919 | 2856 | // ==================================== |
| 2920 | 2857 | // Only run email collection setup if it's enabled |
| 2921 | 2858 | if (mxchatChat && mxchatChat.email_collection_enabled === 'on') { |
| @@ -2951,8 +2888,40 @@ | ||
| 2951 | 2888 | `; |
| 2952 | 2889 | document.head.appendChild(style); |
| 2953 | 2890 | } |
| 2954 | 2891 | |
| 2892 | + // Helper functions for email collection (multi-instance aware) | |
| 2893 | + function showEmailFormForBot(botId) { | |
| 2894 | + var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 2895 | + var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 2896 | + if (emailBlocker) emailBlocker.style.display = 'flex'; | |
| 2897 | + if (chatContainer) chatContainer.style.display = 'none'; | |
| 2898 | + } | |
| 2899 | + | |
| 2900 | + function showChatContainerForBot(botId) { | |
| 2901 | + var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 2902 | + var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 2903 | + if (emailBlocker) emailBlocker.style.display = 'none'; | |
| 2904 | + | |
| 2905 | + var instance = MxChatInstances.get(botId); | |
| 2906 | + var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on'; | |
| 2907 | + | |
| 2908 | + // If persistence is on and history hasn't loaded yet, keep container | |
| 2909 | + // hidden until history loads to prevent flash of empty chat | |
| 2910 | + if (chatPersistenceEnabled && !instance.chatHistoryLoaded) { | |
| 2911 | + if (chatContainer) chatContainer.style.display = 'none'; | |
| 2912 | + loadChatHistory(botId, function() { | |
| 2913 | + if (chatContainer) chatContainer.style.display = 'flex'; | |
| 2914 | + scrollToBottom(botId, true); | |
| 2915 | + }); | |
| 2916 | + } else { | |
| 2917 | + if (chatContainer) chatContainer.style.display = 'flex'; | |
| 2918 | + if (typeof loadChatHistory === 'function') { | |
| 2919 | + loadChatHistory(botId); | |
| 2920 | + } | |
| 2921 | + } | |
| 2922 | + } | |
| 2923 | + | |
| 2955 | 2924 | function isValidEmailAddress(email) { |
| 2956 | 2925 | const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 2957 | 2926 | return emailRegex.test(email.trim()) && email.length <= 254; |
| 2958 | 2927 | } |
| @@ -3090,14 +3059,13 @@ | ||
| 3090 | 3059 | |
| 3091 | 3060 | function checkSessionAndEmailForBot(botId) { |
| 3092 | 3061 | const sessionId = MxChatInstances.ensureSession(botId); |
| 3093 | 3062 | |
| 3094 | - // Hide both panels while we check — show loader instead | |
| 3063 | + // Hide both panels while we check — prevents flash of wrong state | |
| 3095 | 3064 | var emailBlocker = getElementDOM(botId, 'email-blocker'); |
| 3096 | 3065 | var chatContainer = getElementDOM(botId, 'chat-container'); |
| 3097 | 3066 | if (emailBlocker) emailBlocker.style.display = 'none'; |
| 3098 | 3067 | if (chatContainer) chatContainer.style.display = 'none'; |
| 3099 | - showInitLoader(botId); | |
| 3100 | 3068 | |
| 3101 | 3069 | fetch(mxchatChat.ajax_url, { |
| 3102 | 3070 | method: 'POST', |
| 3103 | 3071 | headers: { |
| @@ -3277,8 +3245,9 @@ | ||
| 3277 | 3245 | $('.mxchat-chatbot-wrapper').each(function() { |
| 3278 | 3246 | var botId = $(this).data('bot-id') || 'default'; |
| 3279 | 3247 | var emailBlocker = getElementDOM(botId, 'email-blocker'); |
| 3280 | 3248 | |
| 3249 | + // Only check if email blocker exists for this bot | |
| 3281 | 3250 | if (emailBlocker) { |
| 3282 | 3251 | if (isEmbeddedBot(botId)) { |
| 3283 | 3252 | // Embedded bots are always visible — check now |
| 3284 | 3253 | resolveEmailState(botId); |
| @@ -3283,15 +3252,8 @@ | ||
| 3283 | 3252 | // Embedded bots are always visible — check now |
| 3284 | 3253 | resolveEmailState(botId); |
| 3285 | 3254 | } |
| 3286 | 3255 | // Floating bots: handled in the widget open handler |
| 3287 | - } else if (isEmbeddedBot(botId)) { | |
| 3288 | - // Embedded bot, no email collection — load history with loader | |
| 3289 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 3290 | - if (chatPersistenceEnabled) { | |
| 3291 | - MxChatInstances.ensureSession(botId); | |
| 3292 | - showChatContainerForBot(botId); | |
| 3293 | - } | |
| 3294 | 3256 | } |
| 3295 | 3257 | }); |
| 3296 | 3258 | } |
| 3297 | 3259 | |
| @@ -3316,10 +3278,8 @@ | ||
| 3316 | 3278 | var instance = MxChatInstances.get(botId); |
| 3317 | 3279 | if (emailBlocker && !instance.emailCheckDone) { |
| 3318 | 3280 | instance.emailCheckDone = true; |
| 3319 | 3281 | resolveEmailState(botId); |
| 3320 | - } else if (!emailBlocker) { | |
| 3321 | - showChatContainerForBot(botId); | |
| 3322 | 3282 | } |
| 3323 | 3283 | } |
| 3324 | 3284 | }); |
| 3325 | 3285 | |