PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.0.6
MxChat – AI Chatbot & Content Generation for WordPress v3.0.6
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | includes/class-mxchat-public.php +160 -295 3.2.203.0.6 View file →
@@ -5,49 +5,97 @@
5 5
6 6 class MxChat_Public {
7 7 private $options;
8 8
9 - // True once any FLOATING instance has rendered on this request via the
10 - // shortcode (or the block wrapping it) — append_chatbot_to_body() then
11 - // yields, so an explicit floating placement never doubles up with the
12 - // Auto-Display instance (plan 95dd1e).
13 - private static $floating_instance_rendered = false;
14 -
15 9 public function __construct() {
16 10 // Simply get the options without defining duplicated defaults
17 11 $this->options = get_option('mxchat_options', array());
18 12 add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode'));
19 13 add_action('wp_footer', array($this, 'append_chatbot_to_body'));
14 +
15 + // Initialize testing panel for admins
16 + add_action('wp_enqueue_scripts', array($this, 'enqueue_testing_assets'));
17 +
18 + // Add debug hook
19 + add_action('wp_footer', array($this, 'debug_display_logic'));
20 20 }
21 21
22 + /**
23 + * Enqueue testing panel assets for admin users
24 + */
25 + public function enqueue_testing_assets() {
26 + // Check if frontend debugger is enabled in settings
27 + $options = get_option('mxchat_options', array());
28 + $show_debugger = isset($options['show_frontend_debugger']) ? $options['show_frontend_debugger'] : 'on';
29 +
30 + // Only load if setting is enabled AND (user is admin or testing parameter is present)
31 + if ($show_debugger === 'on' && (current_user_can('administrator') || isset($_GET['mxchat_test']))) {
32 +
33 + // Get plugin URL for assets
34 + $plugin_url = plugin_dir_url(dirname(__FILE__));
35 +
36 + // Enqueue test panel CSS
37 + wp_enqueue_style(
38 + 'mxchat-test-panel',
39 + $plugin_url . 'css/test-panel.css',
40 + array(),
41 + '2.5.2'
42 + );
43 +
44 + // Enqueue test panel JS
45 + wp_enqueue_script(
46 + 'mxchat-test-panel',
47 + $plugin_url . 'js/test-panel.js',
48 + array('jquery'),
49 + '2.5.2',
50 + true
51 + );
52 +
53 + // Pass data to JavaScript
54 + wp_localize_script('mxchat-test-panel', 'mxchatTestData', array(
55 + 'ajaxUrl' => admin_url('admin-ajax.php'),
56 + 'nonce' => wp_create_nonce('mxchat_test_nonce'),
57 + 'isAdmin' => current_user_can('administrator'),
58 + 'testingEnabled' => true
59 + ));
60 +
61 + // Add JavaScript flag to enable testing
62 + add_action('wp_footer', array($this, 'add_testing_flag'));
63 + }
64 + }
65 +
66 + /**
67 + * Add JavaScript flag to enable testing panel
68 + */
69 + public function add_testing_flag() {
70 + echo '<script>window.mxchatTestingEnabled = true;</script>';
71 + }
72 +
73 + /**
74 + * Check if testing mode should be enabled
75 + */
76 + private function is_testing_mode_enabled() {
77 + return (current_user_can('administrator') || isset($_GET['mxchat_test']));
78 + }
79 +
22 80 /**
23 81 * UPDATED: Enhanced should_hide_chatbot method - only blocks auto-append, not shortcodes
24 82 */
25 83 private function should_hide_chatbot($context = 'auto') {
26 84 global $post;
27 -
85 +
28 86 if (!$post) {
29 87 return false;
30 88 }
31 -
89 +
90 + $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true);
91 +
32 92 // Only hide if it's the auto-append context (floating="yes" from global setting)
33 93 // Allow shortcodes to work regardless of this setting
34 - if ($context === 'auto') {
35 - // Check new visibility field first
36 - $visibility = get_post_meta($post->ID, '_mxchat_page_visibility', true);
37 - if ($visibility === 'hide') {
38 - return true;
39 - }
40 -
41 - // Backward compat: check legacy field if new field not set
42 - if (empty($visibility)) {
43 - $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true);
44 - if ($hide_chatbot === '1') {
45 - return true;
46 - }
47 - }
94 + if ($context === 'auto' && $hide_chatbot === '1') {
95 + return true;
48 96 }
49 -
97 +
50 98 return false;
51 99 }
52 100
53 101 /**
@@ -62,18 +110,9 @@
62 110 // Check if auto-append chatbot should be hidden on this page
63 111 if ($this->should_hide_chatbot('auto')) {
64 112 return; // Don't show auto-appended chatbot
65 113 }
66 -
67 - // An explicit floating placement (shortcode or the 95dd1e block) already
68 - // rendered during content — appending the auto instance would put two
69 - // floating widgets on the page with colliding element ids. The explicit
70 - // placement wins; content renders before wp_footer, so the flag is set
71 - // by the time this runs.
72 - if (self::$floating_instance_rendered) {
73 - return;
74 - }
75 -
114 +
76 115 // Get the bot that should be displayed using new logic
77 116 $bot_to_show = $this->get_display_bot();
78 117
79 118 // Don't show chatbot if determination is false
@@ -110,22 +149,8 @@
110 149 /**
111 150 * UPDATED: Enhanced shortcode with context-aware hiding
112 151 */
113 152 public function render_chatbot_shortcode($atts) {
114 - // Smart asset loading safety net (plan-915355): if the opt-in enqueue gate
115 - // skipped assets on this request (shortcode invisible to has_shortcode —
116 - // builder-stored content, template files, widget areas), force the FULL
117 - // enqueue now, including the mxchatChat settings payload and the delayed
118 - // loader wiring. The integrator method is idempotent, so this is a no-op
119 - // when assets already went out at wp_enqueue_scripts time.
120 - if (self::is_smart_asset_loading_enabled() && !wp_style_is('mxchat-chat-css', 'enqueued')) {
121 - global $mxchat_integrator;
122 - if (isset($mxchat_integrator) && is_object($mxchat_integrator)
123 - && method_exists($mxchat_integrator, 'mxchat_enqueue_scripts_styles')) {
124 - $mxchat_integrator->mxchat_enqueue_scripts_styles(true);
125 - }
126 - }
127 -
128 153 // UPDATED: Add bot_id parameter support and improve logic
129 154 $attributes = shortcode_atts(array(
130 155 'floating' => 'yes',
131 156 'has_consent' => 'yes',
@@ -134,16 +159,20 @@
134 159
135 160 // Determine which bot to use
136 161 $bot_id = $this->determine_bot_for_shortcode($attributes['bot_id']);
137 162
138 - // A floating shortcode always renders; what it does is suppress the LATER
139 - // auto-append instance (see append_chatbot_to_body), so a page never gets
140 - // two floating widgets. This replaced a dead empty branch here that
141 - // documented the intent but never suppressed anything (plan 95dd1e).
163 + // UPDATED: Only check hiding for floating shortcodes that could conflict with auto-append
164 + // Non-floating shortcodes should always work
142 165 if ($attributes['floating'] === 'yes') {
143 - self::$floating_instance_rendered = true;
166 + // For floating shortcodes, check if auto-append is hidden
167 + // This prevents duplicate floating chatbots
168 + if ($this->should_hide_chatbot('auto') && $this->is_auto_append_enabled()) {
169 + // If auto-append is enabled but hidden on this page,
170 + // allow the floating shortcode to work (user is overriding)
171 + // But if auto-append is disabled globally, also allow shortcode
172 + }
144 173 }
145 -
174 +
146 175 // Non-floating shortcodes (floating="no") should NEVER be blocked by the hide setting
147 176 // This allows embedded chatbots even when floating is hidden
148 177
149 178 $is_floating = $attributes['floating'] === 'yes';
@@ -195,15 +224,15 @@
195 224 $icon_color = $current_options['icon_color'] ?? '#fff';
196 225 $chat_input_font_color = $current_options['chat_input_font_color'] ?? '#212121';
197 226 $close_button_color = $current_options['close_button_color'] ?? '#fff';
198 227 $chatbot_bg_color = $current_options['chatbot_bg_color'] ?? '#fff';
199 - $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_textarea_field(trim($current_options['pre_chat_message'])) : '';
228 + $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_text_field(trim($current_options['pre_chat_message'])) : '';
200 229 $user_id = sanitize_key($this->mxchat_get_user_identifier());
201 230 $email_state = $this->determine_email_collection_state();
202 231 $show_email_form = $email_state['show_email_form'];
203 232 $user_email = $email_state['user_email'] ?? '';
204 233 $user_name = $email_state['user_name'] ?? '';
205 - // Pre-chat dismissal now handled client-side via localStorage (zero server load)
234 + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
206 235 $input_copy = isset($current_options['input_copy']) ? esc_attr($current_options['input_copy']) : esc_attr__('How can I assist?', 'mxchat');
207 236 $rate_limit_message = isset($current_options['rate_limit_message']) ? esc_attr($current_options['rate_limit_message']) : esc_attr__('Rate limit exceeded. Please try again later.', 'mxchat');
208 237 $mode_indicator_bg_color = $current_options['mode_indicator_bg_color'] ?? '#212121';
209 238 $mode_indicator_font_color = $current_options['mode_indicator_font_color'] ?? '#fff';
@@ -226,28 +255,14 @@
226 255 $enable_email_block = isset($current_options['enable_email_block']) &&
227 256 ($current_options['enable_email_block'] === '1' || $current_options['enable_email_block'] === 'on');
228 257
229 258 // Add name field variables
230 - $enable_name_field = isset($current_options['enable_name_field']) &&
259 + $enable_name_field = isset($current_options['enable_name_field']) &&
231 260 ($current_options['enable_name_field'] === '1' || $current_options['enable_name_field'] === 'on');
232 - $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
233 - esc_attr($current_options['name_field_placeholder']) :
261 + $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
262 + esc_attr($current_options['name_field_placeholder']) :
234 263 esc_attr__('Enter your name', 'mxchat');
235 264
236 - // Consent checkbox (b062c4). Default OFF — with the toggle off this block
237 - // renders nothing and the form markup is byte-identical to before.
238 - $enable_consent_checkbox = isset($current_options['enable_consent_checkbox']) &&
239 - ($current_options['enable_consent_checkbox'] === '1' || $current_options['enable_consent_checkbox'] === 'on');
240 - $consent_checkbox_required = isset($current_options['consent_checkbox_required']) &&
241 - ($current_options['consent_checkbox_required'] === '1' || $current_options['consent_checkbox_required'] === 'on');
242 - // Owner content, kses'd through the shared allowlist at render time as well
243 - // as save time — the render is the security boundary the spec names.
244 - $consent_checkbox_label = MxChat_Utils::sanitize_consent_label(
245 - isset($current_options['consent_checkbox_label']) && $current_options['consent_checkbox_label'] !== ''
246 - ? $current_options['consent_checkbox_label']
247 - : __('I agree to the Privacy Policy.', 'mxchat')
248 - );
249 -
250 265 ob_start();
251 266
252 267 // Check if floating attribute is set to 'yes' and wrap accordingly
253 268 if ($is_floating) {
@@ -254,12 +269,9 @@
254 269 echo '<div id="floating-chatbot-' . esc_attr($bot_id) . '" class="floating-chatbot ' . $initial_visibility . $additional_class . '">';
255 270 }
256 271
257 272 // Add bot_id to the chatbot wrapper as a data attribute
258 - // data-nosnippet: keep the chat widget's UI copy (greeting, title, quick-question
259 - // prompts, privacy notice) out of Google search snippets. This wrapper renders in both
260 - // floating and inline/shortcode modes, so it covers all in-panel copy in one place.
261 - echo '<div id="mxchat-chatbot-wrapper-' . esc_attr($bot_id) . '" class="mxchat-chatbot-wrapper" data-nosnippet data-bot-id="' . esc_attr($bot_id) . '">';
273 + echo '<div id="mxchat-chatbot-wrapper-' . esc_attr($bot_id) . '" class="mxchat-chatbot-wrapper" data-bot-id="' . esc_attr($bot_id) . '">';
262 274
263 275 echo ' <div class="chatbot-top-bar" id="exit-chat-button-' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($top_bar_bg_color) . ';"') . '>';
264 276 echo ' <div class="chatbot-title-container">';
265 277 echo ' <div class="chatbot-title-group">';
@@ -272,25 +284,14 @@
272 284 if (!empty(trim($ai_agent_text))) {
273 285 echo '<span class="chat-mode-indicator" id="chat-mode-indicator-' . esc_attr($bot_id) . '" data-ai-text="' . esc_attr($ai_agent_text) . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($mode_indicator_font_color) . '; background-color: ' . esc_attr($mode_indicator_bg_color) . ';"') . '>' . esc_html($ai_agent_text) . '</span>';
274 286 }
275 287 echo ' </div>';
276 - // Overflow menu (3-dot) trigger + dropdown container.
277 - // Sibling of .exit-chat, rendered to its left. JS hides the trigger
278 - // if no menu items are enabled; outer click + Escape close the menu.
279 - echo ' <div class="mxchat-header-menu-wrap" data-bot-id="' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="--mxchat-menu-bg: ' . esc_attr($bot_message_bg_color) . '; --mxchat-menu-fg: ' . esc_attr($bot_message_font_color) . ';"') . '>';
280 - echo ' <button class="mxchat-header-btn mxchat-menu-trigger" type="button" aria-haspopup="menu" aria-expanded="false" aria-label="' . esc_attr__('More options', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
281 - echo ' <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">';
282 - echo ' <circle cx="12" cy="5" r="2"/><circle cx="12" cy="12" r="2"/><circle cx="12" cy="19" r="2"/>';
283 - echo ' </svg>';
284 - echo ' </button>';
285 - echo ' <div class="mxchat-header-menu" role="menu" aria-hidden="true" hidden></div>';
286 - echo ' </div>';
287 - echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Close', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
288 - echo ' <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" id="ic-close" aria-hidden="true">';
289 - echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
290 - echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
288 + echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Minimize', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
289 + echo ' <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" id="ic-minimize"' . ($skip_inline_colors ? '' : ' style="fill: ' . esc_attr($close_button_color) . ';"') . '>';
290 + echo ' <path d="M0 0h24v24H0z" fill="none"></path>';
291 + echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>';
291 292 echo ' </svg>';
292 - echo ' <span>' . esc_html__('Close', 'mxchat') . '</span>';
293 + echo ' <span>' . esc_html__('Minimize', 'mxchat') . '</span>';
293 294 echo ' </button>';
294 295 echo ' </div>';
295 296
296 297 // 3b) Main chatbot container
@@ -315,20 +316,8 @@
315 316 }
316 317
317 318 echo ' <label for="user-email-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>';
318 319 echo ' <input type="email" id="user-email-' . esc_attr($bot_id) . '" name="user_email" class="mxchat-email-input" required placeholder="' . esc_attr__('Enter your email address', 'mxchat') . '" />';
319 -
320 - // Consent checkbox row (b062c4). The native required attribute
321 - // is UX only — the save endpoint re-enforces it server-side.
322 - // Clicking an anchor inside the label activates the link, not
323 - // the checkbox (interactive descendants skip label activation).
324 - if ($enable_consent_checkbox) {
325 - echo ' <div class="mxchat-consent-row">';
326 - echo ' <input type="checkbox" id="user-consent-' . esc_attr($bot_id) . '" name="user_consent" value="1" class="mxchat-consent-checkbox"' . ($consent_checkbox_required ? ' required' : '') . ' />';
327 - echo ' <label for="user-consent-' . esc_attr($bot_id) . '" class="mxchat-consent-label">' . $consent_checkbox_label . '</label>';
328 - echo ' </div>';
329 - }
330 -
331 320 echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">';
332 321 $button_text = isset($current_options['email_blocker_button_text'])
333 322 ? $current_options['email_blocker_button_text']
334 323 : esc_html__('Start Chat', 'mxchat');
@@ -337,22 +326,10 @@
337 326 echo ' </form>';
338 327 echo '</div>';
339 328 }
340 329
341 - echo ' <div id="mxchat-init-loader-' . esc_attr($bot_id) . '" class="mxchat-init-loader" style="display:none;">';
342 - echo ' <div class="mxchat-init-loader-dots">';
343 - echo ' <span class="dot"></span><span class="dot"></span><span class="dot"></span>';
344 - echo ' </div>';
345 - echo ' </div>';
346 -
347 330 echo ' <div id="chat-container-' . esc_attr($bot_id) . '" class="chat-container" style="' . ($enable_email_block && $show_email_form ? 'display: none;' : '') . '">';
348 - // role="log" + polite live region: new messages are announced to
349 - // screen readers explicitly, so the arrival signal no longer rides
350 - // the post-reply input autofocus (suppressed on touch — plan 03799f).
351 - // additions-only + non-atomic, with aria-busy held on the streaming
352 - // bubble until [DONE] (chat-script.js) so a streamed reply is
353 - // announced once, complete — not per token (WCAG 4.1.3, plan 67f126).
354 - echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box" role="log" aria-live="polite" aria-atomic="false" aria-relevant="additions text" aria-label="' . esc_attr__('Chat messages', 'mxchat') . '">';
331 + echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box">';
355 332 echo ' <div class="bot-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>';
356 333 echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>';
357 334 echo wp_kses_post($intro_message);
358 335 echo ' </div>';
@@ -399,25 +376,9 @@
399 376 echo ' </div>';
400 377 echo ' </div>';
401 378
402 379 echo ' <div id="input-container-' . esc_attr($bot_id) . '" class="input-container">';
403 - // Max input length (plan a3fae2 part C) — global core setting, 0 = unlimited.
404 - // Hard-caps typing/paste client-side; the chat handler enforces it server-side too.
405 - $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
406 - $mxchat_maxlength_attr = $mxchat_max_input_length > 0 ? ' maxlength="' . esc_attr($mxchat_max_input_length) . '"' : '';
407 - // Visually-hidden label = the input's accessible name (WCAG 3.3.2,
408 - // plan 67f126). The placeholder stays as visible prompt copy but
409 - // cannot be the name — it vanishes on the first keystroke.
410 - echo ' <label for="chat-input-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Type your message', 'mxchat') . '</label>';
411 - echo ' <textarea id="chat-input-' . esc_attr($bot_id) . '" class="chat-input" dir="auto"' . $mxchat_maxlength_attr . ' placeholder="' . esc_attr($input_copy) . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($chat_input_font_color) . ';"') . '></textarea>';
412 - // Language-neutral character counter (plan 7091a2). Numbers only — no
413 - // translatable strings — so it reads correctly on every-language install.
414 - // Only rendered when a cap is set; hidden until ~80% of the cap, then
415 - // ramps neutral -> amber -> red. Decorative (aria-hidden); the textarea's
416 - // maxlength carries the real semantics for assistive tech.
417 - if ($mxchat_max_input_length > 0) {
418 - echo ' <div id="mxchat-char-counter-' . esc_attr($bot_id) . '" class="mxchat-char-counter" aria-hidden="true"><span class="mxchat-char-counter-current">0</span><span class="mxchat-char-counter-sep">/</span><span class="mxchat-char-counter-max">' . esc_html($mxchat_max_input_length) . '</span></div>';
419 - }
380 + echo ' <textarea id="chat-input-' . esc_attr($bot_id) . '" class="chat-input" dir="auto" placeholder="' . esc_attr($input_copy) . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($chat_input_font_color) . ';"') . '></textarea>';
420 381 echo ' <button id="send-button-' . esc_attr($bot_id) . '" class="send-button" aria-label="' . esc_attr__('Send message', 'mxchat') . '">';
421 382 if (!empty($custom_send_image)) {
422 383 echo ' <img src="' . esc_url($custom_send_image) . '" alt="' . esc_attr__('Send', 'mxchat') . '" style="width: ' . intval($send_width) . 'px; height: ' . intval($send_height) . 'px; transform: rotate(' . intval($send_rotation) . 'deg);" />';
423 384 } else {
@@ -513,21 +474,16 @@
513 474
514 475 if ($is_floating) {
515 476 echo '</div>';
516 477
517 - if (!empty($pre_chat_message)) {
518 - // Rendered hidden by default — JS checkPreChatDismissal() handles show/hide via localStorage
519 - // data-nosnippet: the pre-chat teaser bubble is a sibling outside
520 - // .mxchat-chatbot-wrapper, so it needs its own marker to stay out of snippets.
521 - echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message" data-nosnippet style="display:none;">';
522 - echo nl2br(esc_html($pre_chat_message));
478 + if (!empty($pre_chat_message) && !get_transient($transient_key)) {
479 + echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message">';
480 + echo esc_html($pre_chat_message);
523 481 echo '<button class="close-pre-chat-message" aria-label="' . esc_attr__('Close', 'mxchat') . '">&times;</button>';
524 482 echo '</div>';
525 483 }
526 484
527 - $is_initially_hidden = (strpos($visibility_class, 'hidden') !== false);
528 - $aria_expanded = $is_initially_hidden ? 'false' : 'true';
529 - echo '<div class="floating-chatbot-button ' . esc_attr($visibility_class) . '" id="floating-chatbot-button-' . esc_attr($bot_id) . '" role="button" tabindex="0" aria-label="' . esc_attr__('Open chat', 'mxchat') . '" aria-expanded="' . esc_attr($aria_expanded) . '" aria-controls="floating-chatbot-' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($chatbot_background_color) . '; color: ' . esc_attr($send_button_font_color) . ';"') . '>';
485 + echo '<div class="floating-chatbot-button ' . esc_attr($visibility_class) . '" id="floating-chatbot-button-' . esc_attr($bot_id) . '"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($chatbot_background_color) . '; color: ' . esc_attr($send_button_font_color) . ';"') . '>';
530 486 echo '<div id="chat-notification-badge-' . esc_attr($bot_id) . '" class="chat-notification-badge" style="display: none; position: absolute; top: -8px; right: -8px; background-color: #ff4444; color: white; border-radius: 50%; padding: 4px 8px; font-size: 12px; font-weight: bold; z-index: 10001;">1</div>';
531 487
532 488 if (!empty($custom_icon)) {
533 489 echo '<img src="' . $custom_icon . '" alt="' . esc_attr__('Chatbot Icon', 'mxchat') . '" style="height: 48px; width: 48px; object-fit: contain;" />';
@@ -586,9 +542,12 @@
586 542 }
587 543
588 544
589 545 private function determine_email_collection_state() {
590 - // Logged-in users skip the email form
546 + // Get session ID
547 + $session_id = 'mxchat_chat_' . wp_generate_uuid4(); // You'll need to use your actual session generation logic
548 +
549 + // Check if user is logged in first
591 550 if (is_user_logged_in()) {
592 551 $current_user = wp_get_current_user();
593 552 return [
594 553 'show_email_form' => false,
@@ -595,16 +554,26 @@
595 554 'user_email' => $current_user->user_email,
596 555 'user_name' => $current_user->display_name ?: $current_user->first_name ?: ''
597 556 ];
598 557 }
599 -
600 - // For guests, default to showing the email form.
601 - // The session-based check happens client-side via AJAX since the
602 - // session ID lives in the browser cookie/localStorage.
558 +
559 + // Check stored email for session
560 + $stored_email = get_option("mxchat_email_{$session_id}", '');
561 + $stored_name = get_option("mxchat_name_{$session_id}", '');
562 +
563 + // Check if name is required
564 + $name_field_enabled = isset($this->options['enable_name_field']) &&
565 + ($this->options['enable_name_field'] === '1' || $this->options['enable_name_field'] === 'on');
566 +
567 + $has_required_info = !empty($stored_email);
568 + if ($name_field_enabled) {
569 + $has_required_info = $has_required_info && !empty($stored_name);
570 + }
571 +
603 572 return [
604 - 'show_email_form' => true,
605 - 'user_email' => '',
606 - 'user_name' => ''
573 + 'show_email_form' => !$has_required_info,
574 + 'user_email' => $stored_email,
575 + 'user_name' => $stored_name
607 576 ];
608 577 }
609 578
610 579
@@ -611,50 +580,14 @@
611 580 /**
612 581 * NEW: Determine if and which chatbot should be displayed
613 582 */
614 583 private function get_display_bot() {
615 - // Delegates to the static so the enqueue-time gate (smart asset loading,
616 - // plan-915355) and this render-time decision share ONE code path and can
617 - // never disagree. The static reads mxchat_options fresh — same row this
618 - // instance loaded at construct.
619 - return self::compute_display_bot();
620 -}
621 -
622 -/**
623 - * Static single source of truth for the display decision (plan-915355).
624 - * Combines the per-page meta box, the append_to_body global toggle, and the
625 - * post-type include/exclude mode. Returns a bot id ('default' or specific)
626 - * when the auto-append widget will render on the current request, false when
627 - * it won't. Static (not a second instance) deliberately: MxChat_Public's
628 - * constructor registers a wp_footer action, so constructing a throwaway
629 - * instance would double-append the widget.
630 - */
631 -public static function compute_display_bot() {
632 - // The Elementor builder canvas is not a chat surface (plan 95dd1e part 2):
633 - // its preview iframe is a real front-end page load, so without this guard
634 - // the Auto-Display instance renders LIVE inside the canvas — front-end
635 - // chat scripts firing in the editor, able to start a session for the
636 - // person editing. Returning false here suppresses both the wp_footer
637 - // auto-append and the asset enqueue gate for preview requests only; the
638 - // placement widget's own canvas rendering is a static placeholder anyway.
639 - if (did_action('elementor/loaded')
640 - && class_exists('\Elementor\Plugin')
641 - && isset(\Elementor\Plugin::$instance->preview)
642 - && \Elementor\Plugin::$instance->preview
643 - && \Elementor\Plugin::$instance->preview->is_preview_mode()) {
644 - return false;
645 - }
646 -
647 - $options = get_option('mxchat_options', array());
648 - if (!is_array($options)) {
649 - $options = array();
650 - }
651 -
652 584 // Get page-specific settings from meta box
653 - $page_setting = self::get_page_bot_setting();
585 + $page_setting = $this->get_page_bot_setting();
654 586
655 - $global_autoshow = isset($options['append_to_body']) && $options['append_to_body'] === 'on';
656 - $global_default_bot = isset($options['default_bot']) ? $options['default_bot'] : 'default';
587 + // Get global settings - FIXED: Check for 'on' instead of 'on'
588 + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
589 + $global_default_bot = isset($this->options['default_bot']) ? $this->options['default_bot'] : 'default';
657 590
658 591 // If page specifically hides chatbot, don't show anything
659 592 if ($page_setting && $page_setting['action'] === 'hide') {
660 593 return false;
@@ -668,9 +601,9 @@
668 601 // Page setting is 'global' or no page setting exists
669 602 // Check global auto-show setting
670 603 if ($global_autoshow) {
671 604 // Check post type visibility settings
672 - if (!self::should_show_on_current_post_type()) {
605 + if (!$this->should_show_on_current_post_type()) {
673 606 return false;
674 607 }
675 608
676 609 // Global auto-show is enabled, return the default bot
@@ -682,86 +615,14 @@
682 615 return false;
683 616 }
684 617
685 618 /**
686 - * Smart asset loading opt-in (plan-915355). Standalone option — deliberately
687 - * NOT a mxchat_options key, so it can never be stripped by mxchat_sanitize().
688 - * Default off: enqueue behavior is byte-identical to before until an owner
689 - * turns the toggle on.
690 - */
691 -public static function is_smart_asset_loading_enabled() {
692 - return get_option('mxchat_smart_asset_loading', 'off') === 'on';
693 -}
694 -
695 -/**
696 - * Will the chat widget render on the current request? (plan-915355)
697 - *
698 - * True when the auto-append decision resolves to a bot, OR the singular
699 - * post's content contains the [mxchat_chatbot] shortcode (first-chance
700 - * detection so shortcode pages keep head-loaded CSS — no FOUC). Computed
701 - * once per request and cached, so the wp_enqueue_scripts gate and any
702 - * add-on consulting this later in the same request always get one answer.
703 - *
704 - * Filter `mxchat_should_load_assets` is the force-load escape hatch for
705 - * headless/builder/custom-JS setups whose shortcode placement is invisible
706 - * to has_shortcode (builder-stored content, template files, widget areas).
707 - * Note the render-time safety net in render_chatbot_shortcode() still
708 - * force-loads assets whenever the shortcode actually renders — the filter
709 - * is only needed where even that net can't fire (e.g. markup assembled
710 - * outside WP rendering).
711 - */
712 -public static function should_load_assets() {
713 - static $cached = null;
714 - if ($cached !== null) {
715 - return $cached;
716 - }
717 -
718 - // Never gate admin/ajax requests — this decision is for front-end enqueues only.
719 - if (is_admin()) {
720 - $cached = true;
721 - return $cached;
722 - }
723 -
724 - $display_bot = self::compute_display_bot();
725 - $has_shortcode = false;
726 -
727 - if ($display_bot === false && is_singular()) {
728 - $post = get_post();
729 - if ($post && has_shortcode((string) $post->post_content, 'mxchat_chatbot')) {
730 - $has_shortcode = true;
731 - }
732 - // The mxchat/chatbot block (plan-95dd1e) stores a block comment, not
733 - // shortcode text, so has_shortcode can't see it — first-chance detect
734 - // it here for the same no-FOUC reason. The render-time safety net in
735 - // render_chatbot_shortcode() still covers template/widget placements.
736 - if ($post && !$has_shortcode && function_exists('has_block')
737 - && has_block('mxchat/chatbot', $post)) {
738 - $has_shortcode = true;
739 - }
740 - }
741 -
742 - $should = ($display_bot !== false) || $has_shortcode;
743 -
744 - $cached = (bool) apply_filters('mxchat_should_load_assets', $should, array(
745 - 'display_bot' => $display_bot,
746 - 'has_shortcode' => $has_shortcode,
747 - 'post_id' => get_the_ID(),
748 - ));
749 -
750 - return $cached;
751 -}
752 -
753 -/**
754 619 * Check if chatbot should be shown on the current post type
755 620 */
756 -private static function should_show_on_current_post_type() {
757 - $options = get_option('mxchat_options', array());
758 - if (!is_array($options)) {
759 - $options = array();
760 - }
621 +private function should_show_on_current_post_type() {
761 622 // Get visibility settings
762 - $mode = isset($options['post_type_visibility_mode']) ? $options['post_type_visibility_mode'] : 'all';
763 - $list = isset($options['post_type_visibility_list']) ? $options['post_type_visibility_list'] : array();
623 + $mode = isset($this->options['post_type_visibility_mode']) ? $this->options['post_type_visibility_mode'] : 'all';
624 + $list = isset($this->options['post_type_visibility_list']) ? $this->options['post_type_visibility_list'] : array();
764 625
765 626 // Ensure list is an array
766 627 if (!is_array($list)) {
767 628 $list = array();
@@ -772,9 +633,9 @@
772 633 return true;
773 634 }
774 635
775 636 // Get current post type
776 - $current_post_type = self::get_current_post_type();
637 + $current_post_type = $this->get_current_post_type();
777 638
778 639 // If we can't determine post type, default to showing
779 640 if (empty($current_post_type)) {
780 641 return true;
@@ -795,9 +656,9 @@
795 656
796 657 /**
797 658 * Get the current post type
798 659 */
799 -private static function get_current_post_type() {
660 +private function get_current_post_type() {
800 661 // Try to get from queried object first
801 662 $queried_object = get_queried_object();
802 663
803 664 if ($queried_object instanceof WP_Post) {
@@ -827,46 +688,31 @@
827 688 return '';
828 689 }
829 690
830 691 /**
831 - * Get page-specific bot setting using new visibility field with backward compat
692 + * NEW: Get page-specific bot setting
832 693 */
833 -private static function get_page_bot_setting($post_id = null) {
694 +private function get_page_bot_setting($post_id = null) {
834 695 if (!$post_id) {
835 696 $post_id = get_the_ID();
836 697 }
837 -
698 +
838 699 if (!$post_id) {
839 700 return null;
840 701 }
841 -
842 - // Check new visibility field first
843 - $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true);
844 -
845 - if ($visibility === 'hide') {
702 +
703 + // Check if chatbot is hidden on this page
704 + $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true);
705 + if ($hide_chatbot === '1') {
846 706 return array('action' => 'hide');
847 707 }
848 -
849 - if ($visibility === 'show') {
850 - $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
851 - $bot_id = !empty($selected_bot) ? $selected_bot : 'default';
852 - return array('action' => 'show', 'bot_id' => $bot_id);
853 - }
854 -
855 - // Backward compat: check legacy hide checkbox if no new field set
856 - if (empty($visibility)) {
857 - $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true);
858 - if ($hide_chatbot === '1') {
859 - return array('action' => 'hide');
860 - }
861 - }
862 -
863 - // Check if specific bot is selected (legacy path)
708 +
709 + // Check if specific bot is selected
864 710 $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
865 711 if (!empty($selected_bot)) {
866 712 return array('action' => 'show', 'bot_id' => $selected_bot);
867 713 }
868 -
714 +
869 715 // Use global setting
870 716 return array('action' => 'global');
871 717 }
872 718
@@ -879,9 +725,9 @@
879 725 return $shortcode_bot_id;
880 726 }
881 727
882 728 // No bot_id in shortcode, check page setting
883 - $page_setting = self::get_page_bot_setting();
729 + $page_setting = $this->get_page_bot_setting();
884 730 if ($page_setting && $page_setting['action'] === 'show') {
885 731 return $page_setting['bot_id'];
886 732 }
887 733
@@ -898,8 +744,27 @@
898 744
899 745 /**
900 746 * UPDATED: Debug function with new context info
901 747 */
748 +public function debug_display_logic() {
749 + if (current_user_can('manage_options') && isset($_GET['mxchat_debug'])) {
750 + $bot_to_show = $this->get_display_bot();
751 + $page_setting = $this->get_page_bot_setting();
752 + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
753 + $hide_auto = $this->should_hide_chatbot('auto');
754 +
755 + echo '<div style="position: fixed; top: 50px; right: 20px; background: white; border: 2px solid red; padding: 10px; z-index: 9999; max-width: 300px; font-size: 12px;">';
756 + echo '<h4 style="margin: 0 0 10px 0;">MxChat Debug Info</h4>';
757 + echo '<p><strong>Raw append_to_body value:</strong> "' . ($this->options['append_to_body'] ?? 'NOT SET') . '"</p>';
758 + echo '<p><strong>Page Setting:</strong><br><pre>' . print_r($page_setting, true) . '</pre></p>';
759 + echo '<p><strong>Global Auto-show:</strong> ' . ($global_autoshow ? 'ON' : 'OFF') . '</p>';
760 + echo '<p><strong>Hide Auto-Append:</strong> ' . ($hide_auto ? 'YES' : 'NO') . '</p>';
761 + echo '<p><strong>Bot to Show:</strong> ' . ($bot_to_show === false ? 'NONE' : $bot_to_show) . '</p>';
762 + echo '<p><strong>Shortcodes:</strong> floating="no" always works</p>';
763 + echo '<p><small>Add ?mxchat_debug=1 to URL to see this</small></p>';
764 + echo '</div>';
765 + }
766 +}
902 767
903 768 /**
904 769 * NEW: Helper method to get available bots (for admin notices, etc.)
905 770 */