PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.4.6
MxChat – AI Chatbot & Content Generation for WordPress v2.4.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 +191 -355 3.2.182.4.6 View file →
@@ -10,38 +10,88 @@
10 10 // Simply get the options without defining duplicated defaults
11 11 $this->options = get_option('mxchat_options', array());
12 12 add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode'));
13 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'));
14 20 }
15 21
22 + /**
23 + * Enqueue testing panel assets for admin users
24 + */
25 + public function enqueue_testing_assets() {
26 + // Only load for admin users or if testing parameter is present
27 + if (current_user_can('administrator') || isset($_GET['mxchat_test'])) {
28 +
29 + // Get plugin URL for assets
30 + $plugin_url = plugin_dir_url(dirname(__FILE__));
31 +
32 + // Enqueue test panel CSS
33 + wp_enqueue_style(
34 + 'mxchat-test-panel',
35 + $plugin_url . 'css/test-panel.css',
36 + array(),
37 + '1.0.0'
38 + );
39 +
40 + // Enqueue test panel JS
41 + wp_enqueue_script(
42 + 'mxchat-test-panel',
43 + $plugin_url . 'js/test-panel.js',
44 + array('jquery'),
45 + '1.0.0',
46 + true
47 + );
48 +
49 + // Pass data to JavaScript
50 + wp_localize_script('mxchat-test-panel', 'mxchatTestData', array(
51 + 'ajaxUrl' => admin_url('admin-ajax.php'),
52 + 'nonce' => wp_create_nonce('mxchat_test_nonce'),
53 + 'isAdmin' => current_user_can('administrator'),
54 + 'testingEnabled' => true
55 + ));
56 +
57 + // Add JavaScript flag to enable testing
58 + add_action('wp_footer', array($this, 'add_testing_flag'));
59 + }
60 + }
61 +
62 + /**
63 + * Add JavaScript flag to enable testing panel
64 + */
65 + public function add_testing_flag() {
66 + echo '<script>window.mxchatTestingEnabled = true;</script>';
67 + }
68 +
69 + /**
70 + * Check if testing mode should be enabled
71 + */
72 + private function is_testing_mode_enabled() {
73 + return (current_user_can('administrator') || isset($_GET['mxchat_test']));
74 + }
75 +
16 76 /**
17 77 * UPDATED: Enhanced should_hide_chatbot method - only blocks auto-append, not shortcodes
18 78 */
19 79 private function should_hide_chatbot($context = 'auto') {
20 80 global $post;
21 -
81 +
22 82 if (!$post) {
23 83 return false;
24 84 }
25 -
85 +
86 + $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true);
87 +
26 88 // Only hide if it's the auto-append context (floating="yes" from global setting)
27 89 // Allow shortcodes to work regardless of this setting
28 - if ($context === 'auto') {
29 - // Check new visibility field first
30 - $visibility = get_post_meta($post->ID, '_mxchat_page_visibility', true);
31 - if ($visibility === 'hide') {
32 - return true;
33 - }
34 -
35 - // Backward compat: check legacy field if new field not set
36 - if (empty($visibility)) {
37 - $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true);
38 - if ($hide_chatbot === '1') {
39 - return true;
40 - }
41 - }
90 + if ($context === 'auto' && $hide_chatbot === '1') {
91 + return true;
42 92 }
43 -
93 +
44 94 return false;
45 95 }
46 96
47 97 /**
@@ -95,22 +145,8 @@
95 145 /**
96 146 * UPDATED: Enhanced shortcode with context-aware hiding
97 147 */
98 148 public function render_chatbot_shortcode($atts) {
99 - // Smart asset loading safety net (plan-915355): if the opt-in enqueue gate
100 - // skipped assets on this request (shortcode invisible to has_shortcode —
101 - // builder-stored content, template files, widget areas), force the FULL
102 - // enqueue now, including the mxchatChat settings payload and the delayed
103 - // loader wiring. The integrator method is idempotent, so this is a no-op
104 - // when assets already went out at wp_enqueue_scripts time.
105 - if (self::is_smart_asset_loading_enabled() && !wp_style_is('mxchat-chat-css', 'enqueued')) {
106 - global $mxchat_integrator;
107 - if (isset($mxchat_integrator) && is_object($mxchat_integrator)
108 - && method_exists($mxchat_integrator, 'mxchat_enqueue_scripts_styles')) {
109 - $mxchat_integrator->mxchat_enqueue_scripts_styles(true);
110 - }
111 - }
112 -
113 149 // UPDATED: Add bot_id parameter support and improve logic
114 150 $attributes = shortcode_atts(array(
115 151 'floating' => 'yes',
116 152 'has_consent' => 'yes',
@@ -163,14 +199,9 @@
163 199 $custom_send_image = isset($theme_options['custom_send_button_image']) ? esc_url($theme_options['custom_send_button_image']) : '';
164 200 $send_width = isset($theme_options['send_button_width']) ? intval($theme_options['send_button_width']) : 24;
165 201 $send_height = isset($theme_options['send_button_height']) ? intval($theme_options['send_button_height']) : 24;
166 202 $send_rotation = isset($theme_options['send_button_rotation']) ? intval($theme_options['send_button_rotation']) : 0;
167 -
168 - // Check if an AI theme is active (global or bot-specific) - if so, skip inline color styles
169 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
170 - $bot_has_theme = isset($theme_options['bot_theme_assignments'][$bot_id]);
171 - $skip_inline_colors = $ai_theme_active || $bot_has_theme;
172 -
203 +
173 204 // UPDATED: Use current_options instead of $this->options throughout
174 205 $bg_color = $current_options['chatbot_background_color'] ?? '#fff';
175 206 $user_message_bg_color = $current_options['user_message_bg_color'] ?? '#fff';
176 207 $user_message_font_color = $current_options['user_message_font_color'] ?? '#212121';
@@ -184,15 +215,15 @@
184 215 $icon_color = $current_options['icon_color'] ?? '#fff';
185 216 $chat_input_font_color = $current_options['chat_input_font_color'] ?? '#212121';
186 217 $close_button_color = $current_options['close_button_color'] ?? '#fff';
187 218 $chatbot_bg_color = $current_options['chatbot_bg_color'] ?? '#fff';
188 - $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_textarea_field(trim($current_options['pre_chat_message'])) : '';
219 + $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_text_field(trim($current_options['pre_chat_message'])) : '';
189 220 $user_id = sanitize_key($this->mxchat_get_user_identifier());
190 221 $email_state = $this->determine_email_collection_state();
191 222 $show_email_form = $email_state['show_email_form'];
192 223 $user_email = $email_state['user_email'] ?? '';
193 224 $user_name = $email_state['user_name'] ?? '';
194 - // Pre-chat dismissal now handled client-side via localStorage (zero server load)
225 + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
195 226 $input_copy = isset($current_options['input_copy']) ? esc_attr($current_options['input_copy']) : esc_attr__('How can I assist?', 'mxchat');
196 227 $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');
197 228 $mode_indicator_bg_color = $current_options['mode_indicator_bg_color'] ?? '#212121';
198 229 $mode_indicator_font_color = $current_options['mode_indicator_font_color'] ?? '#fff';
@@ -206,10 +237,9 @@
206 237 $popular_question_3 = isset($current_options['popular_question_3']) ? esc_html($current_options['popular_question_3']) : '';
207 238 $additional_questions = isset($current_options['additional_popular_questions']) ? $current_options['additional_popular_questions'] : [];
208 239 $custom_icon = isset($current_options['custom_icon']) ? esc_url($current_options['custom_icon']) : '';
209 240 $title_icon = isset($current_options['title_icon']) ? esc_url($current_options['title_icon']) : '';
210 - // AI agent text - if explicitly set to empty string, hide the indicator entirely
211 - $ai_agent_text = isset($current_options['ai_agent_text']) ? $current_options['ai_agent_text'] : __('AI Agent', 'mxchat');
241 + $ai_agent_text = isset($current_options['ai_agent_text']) ? esc_html($current_options['ai_agent_text']) : esc_html__('AI Agent', 'mxchat');
212 242
213 243 $live_agent_message_bg_color = $current_options['live_agent_message_bg_color'] ?? '#212121';
214 244 $live_agent_message_font_color = $current_options['live_agent_message_font_color'] ?? '#fff';
215 245 $enable_email_block = isset($current_options['enable_email_block']) &&
@@ -225,74 +255,57 @@
225 255 ob_start();
226 256
227 257 // Check if floating attribute is set to 'yes' and wrap accordingly
228 258 if ($is_floating) {
229 - echo '<div id="floating-chatbot-' . esc_attr($bot_id) . '" class="floating-chatbot ' . $initial_visibility . $additional_class . '">';
259 + echo '<div id="floating-chatbot" class="' . $initial_visibility . $additional_class . '">';
230 260 }
231 261
232 262 // Add bot_id to the chatbot wrapper as a data attribute
233 - // data-nosnippet: keep the chat widget's UI copy (greeting, title, quick-question
234 - // prompts, privacy notice) out of Google search snippets. This wrapper renders in both
235 - // floating and inline/shortcode modes, so it covers all in-panel copy in one place.
236 - echo '<div id="mxchat-chatbot-wrapper-' . esc_attr($bot_id) . '" class="mxchat-chatbot-wrapper" data-nosnippet data-bot-id="' . esc_attr($bot_id) . '">';
263 + echo '<div id="mxchat-chatbot-wrapper" data-bot-id="' . esc_attr($bot_id) . '">';
237 264
238 - 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) . ';"') . '>';
265 + echo ' <div class="chatbot-top-bar" id="exit-chat-button" style="background: ' . esc_attr($top_bar_bg_color) . ';">';
239 266 echo ' <div class="chatbot-title-container">';
240 267 echo ' <div class="chatbot-title-group">';
241 268 if (!empty($title_icon)) {
242 269 echo ' <img src="' . esc_url($title_icon) . '" alt="" class="chatbot-title-icon">';
243 270 }
244 - echo ' <p class="chatbot-title"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>' . esc_html($top_bar_title) . '</p>';
271 + echo ' <p class="chatbot-title" style="color: ' . esc_attr($close_button_color) . ';">' . esc_html($top_bar_title) . '</p>';
245 272 echo ' </div>';
246 - // Only show mode indicator if ai_agent_text is not empty
247 - if (!empty(trim($ai_agent_text))) {
248 - 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>';
249 - }
273 + echo '<span class="chat-mode-indicator" id="chat-mode-indicator" data-ai-text="' . $ai_agent_text . '" style="color: ' . esc_attr($mode_indicator_font_color) . '; background-color: ' . esc_attr($mode_indicator_bg_color) . ';">' . $ai_agent_text . '</span>';
250 274 echo ' </div>';
251 - // Overflow menu (3-dot) trigger + dropdown container.
252 - // Sibling of .exit-chat, rendered to its left. JS hides the trigger
253 - // if no menu items are enabled; outer click + Escape close the menu.
254 - 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) . ';"') . '>';
255 - 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) . ';"') . '>';
256 - echo ' <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">';
257 - echo ' <circle cx="12" cy="5" r="2"/><circle cx="12" cy="12" r="2"/><circle cx="12" cy="19" r="2"/>';
258 - echo ' </svg>';
259 - echo ' </button>';
260 - echo ' <div class="mxchat-header-menu" role="menu" aria-hidden="true" hidden></div>';
261 - echo ' </div>';
262 - echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Close', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
263 - 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">';
264 - echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
265 - echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
275 + echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Minimize', 'mxchat') . '" style="color: ' . esc_attr($close_button_color) . ';">';
276 + echo ' <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" id="ic-minimize" style="fill: ' . esc_attr($close_button_color) . ';">';
277 + echo ' <path d="M0 0h24v24H0z" fill="none"></path>';
278 + echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>';
266 279 echo ' </svg>';
267 - echo ' <span>' . esc_html__('Close', 'mxchat') . '</span>';
280 + echo ' <span>' . esc_html__('Minimize', 'mxchat') . '</span>';
268 281 echo ' </button>';
269 282 echo ' </div>';
270 -
283 +
271 284 // 3b) Main chatbot container
272 - echo ' <div id="mxchat-chatbot-' . esc_attr($bot_id) . '" class="mxchat-chatbot"' . ($skip_inline_colors ? '' : ' style="background-color: ' . esc_attr($chatbot_bg_color) . ';"') . '>';
285 + echo ' <div id="mxchat-chatbot" style="background-color: ' . esc_attr($chatbot_bg_color) . ';">';
273 286
274 287 if ($enable_email_block) {
275 - echo '<div id="email-blocker-' . esc_attr($bot_id) . '" class="email-blocker" style="' . ($show_email_form ? '' : 'display: none;') . '">';
288 + echo '<div id="email-blocker" class="email-blocker" style="' . ($show_email_form ? '' : 'display: none;') . '">';
276 289 echo ' <div class="email-blocker-header">';
277 -
290 +
278 291 $header_html = isset($current_options['email_blocker_header_content'])
279 292 ? $current_options['email_blocker_header_content']
280 293 : '';
281 294 echo wp_kses_post($header_html);
282 -
295 +
283 296 echo ' </div>';
284 - echo ' <form id="email-collection-form-' . esc_attr($bot_id) . '" class="email-collection-form" method="POST" action="">';
285 -
297 + echo ' <form id="email-collection-form" method="POST" action="">';
298 +
286 299 // Add name field if enabled
287 300 if ($enable_name_field) {
288 - echo ' <label for="user-name-' . esc_attr($bot_id) . '" class="sr-only mxchat-name-label">' . esc_html__('Name', 'mxchat') . '</label>';
289 - echo ' <input type="text" id="user-name-' . esc_attr($bot_id) . '" name="user_name" class="mxchat-name-input" required placeholder="' . $name_field_placeholder . '" />';
301 + echo ' <label for="user-name" class="sr-only mxchat-name-label">' . esc_html__('Name', 'mxchat') . '</label>';
302 + echo ' <input type="text" id="user-name" name="user_name" class="mxchat-name-input" required placeholder="' . $name_field_placeholder . '" />';
290 303 }
291 -
292 - echo ' <label for="user-email-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>';
293 - 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') . '" />';
294 - echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">';
304 +
305 + echo ' <label for="user-email" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>';
306 + echo ' <input type="email" id="user-email" name="user_email" required placeholder="' . esc_attr__('Enter your email address', 'mxchat') . '" />';
307 + echo '<button type="submit" id="email-submit-button">';
295 308 $button_text = isset($current_options['email_blocker_button_text'])
296 309 ? $current_options['email_blocker_button_text']
297 310 : esc_html__('Start Chat', 'mxchat');
298 311 echo esc_html($button_text);
@@ -300,19 +313,13 @@
300 313 echo ' </form>';
301 314 echo '</div>';
302 315 }
303 316
304 - echo ' <div id="mxchat-init-loader-' . esc_attr($bot_id) . '" class="mxchat-init-loader" style="display:none;">';
305 - echo ' <div class="mxchat-init-loader-dots">';
306 - echo ' <span class="dot"></span><span class="dot"></span><span class="dot"></span>';
307 - echo ' </div>';
308 - echo ' </div>';
309 -
310 - echo ' <div id="chat-container-' . esc_attr($bot_id) . '" class="chat-container" style="' . ($enable_email_block && $show_email_form ? 'display: none;' : '') . '">';
311 - echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box">';
312 - echo ' <div class="bot-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>';
313 - echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>';
314 - echo wp_kses_post($intro_message);
317 + echo ' <div id="chat-container" style="' . ($enable_email_block && $show_email_form ? 'display: none;' : '') . '">';
318 + echo ' <div id="chat-box">';
319 + echo ' <div class="bot-message" style="background: ' . esc_attr($bot_message_bg_color) . ';">';
320 + echo ' <div dir="auto" style="color: ' . esc_attr($bot_message_font_color) . ';">';
321 + echo wp_kses_post($intro_message);
315 322 echo ' </div>';
316 323 echo ' </div>';
317 324 echo ' </div>'; // end #chat-box
318 325
@@ -317,21 +324,21 @@
317 324 echo ' </div>'; // end #chat-box
318 325
319 326
320 327 // Replace the existing popular questions section with this:
321 - echo ' <div id="mxchat-popular-questions-' . esc_attr($bot_id) . '" class="mxchat-popular-questions">';
328 + echo ' <div id="mxchat-popular-questions">';
322 329 echo ' <div class="mxchat-popular-questions-container">';
323 330
324 331 // Collapse button (down arrow) - shows when open, centered at top
325 332 echo ' <button class="questions-collapse-btn" aria-label="' . esc_attr__('Hide Quick Questions', 'mxchat') . '">';
326 - echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none"' . ($skip_inline_colors ? '' : ' stroke="' . esc_attr($quick_questions_toggle_color) . '"') . ' stroke-width="2">';
333 + echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none" stroke="' . esc_attr($quick_questions_toggle_color) . '" stroke-width="2">';
327 334 echo ' <polyline points="6,9 12,15 18,9"></polyline>';
328 335 echo ' </svg>';
329 336 echo ' </button>';
330 -
337 +
331 338 // Expand button (up arrow) - shows when collapsed
332 339 echo ' <button class="questions-toggle-btn" aria-label="' . esc_attr__('Show Quick Questions', 'mxchat') . '">';
333 - echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none"' . ($skip_inline_colors ? '' : ' stroke="' . esc_attr($quick_questions_toggle_color) . '"') . ' stroke-width="2">';
340 + echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none" stroke="' . esc_attr($quick_questions_toggle_color) . '" stroke-width="2">';
334 341 echo ' <polyline points="18,15 12,9 6,15"></polyline>';
335 342 echo ' </svg>';
336 343 echo ' </button>';
337 344
@@ -355,31 +362,15 @@
355 362
356 363 echo ' </div>';
357 364 echo ' </div>';
358 365
359 - echo ' <div id="input-container-' . esc_attr($bot_id) . '" class="input-container">';
360 - // Max input length (plan a3fae2 part C) — global core setting, 0 = unlimited.
361 - // Hard-caps typing/paste client-side; the chat handler enforces it server-side too.
362 - $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
363 - $mxchat_maxlength_attr = $mxchat_max_input_length > 0 ? ' maxlength="' . esc_attr($mxchat_max_input_length) . '"' : '';
364 - 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>';
365 - // Language-neutral character counter (plan 7091a2). Numbers only — no
366 - // translatable strings — so it reads correctly on every-language install.
367 - // Only rendered when a cap is set; hidden until ~80% of the cap, then
368 - // ramps neutral -> amber -> red. Decorative (aria-hidden); the textarea's
369 - // maxlength carries the real semantics for assistive tech.
370 - if ($mxchat_max_input_length > 0) {
371 - 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>';
372 - }
373 - echo ' <button id="send-button-' . esc_attr($bot_id) . '" class="send-button" aria-label="' . esc_attr__('Send message', 'mxchat') . '">';
366 + echo ' <div id="input-container">';
367 + echo ' <textarea id="chat-input" dir="auto" placeholder="' . esc_attr($input_copy) . '" style="color: ' . esc_attr($chat_input_font_color) . ';"></textarea>';
368 + echo ' <button id="send-button">';
374 369 if (!empty($custom_send_image)) {
375 370 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);" />';
376 371 } else {
377 - $send_svg_style = 'width: ' . intval($send_width) . 'px; height: ' . intval($send_height) . 'px; transform: rotate(' . intval($send_rotation) . 'deg);';
378 - if (!$skip_inline_colors) {
379 - $send_svg_style = 'fill: ' . esc_attr($send_button_font_color) . '; ' . $send_svg_style;
380 - }
381 - echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="' . $send_svg_style . '">';
372 + echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="fill: ' . esc_attr($send_button_font_color) . '; width: ' . intval($send_width) . 'px; height: ' . intval($send_height) . 'px; transform: rotate(' . intval($send_rotation) . 'deg);">';
382 373 echo ' <path d="M498.1 5.6c10.1 7 15.4 19.1 13.5 31.2l-64 416c-1.5 9.7-7.4 18.2-16 23s-18.9 5.4-28 1.6L284 427.7l-68.5 74.1c-8.9 9.7-22.9 12.9-35.2 8.1S160 493.2 160 480V396.4c0-4 1.5-7.8 4.2-10.7L331.8 202.8c5.8-6.3 5.6-16-.4-22s-15.7-6.4-22-.7L106 360.8 17.7 316.6C7.1 311.3 .3 300.7 0 288.9s5.9-22.8 16.1-28.7l448-256c10.7-6.1 23.9-5.5 34 1.4z"></path>';
383 374 echo ' </svg>';
384 375 }
385 376 echo ' </button>';
@@ -389,10 +380,10 @@
389 380
390 381 // PDF Upload Button - wrapped in conditional using current_options
391 382 $show_pdf_button = isset($current_options['show_pdf_upload_button']) ? $current_options['show_pdf_upload_button'] : 'on';
392 383 if ($show_pdf_button === 'on') {
393 - echo ' <input type="file" id="pdf-upload-' . esc_attr($bot_id) . '" class="pdf-upload" accept=".pdf" style="display: none;">';
394 - echo ' <button id="pdf-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn pdf-upload-btn" title="' . esc_attr__('Upload PDF', 'mxchat') . '">';
384 + echo ' <input type="file" id="pdf-upload" accept=".pdf" style="display: none;">';
385 + echo ' <button id="pdf-upload-btn" class="toolbar-btn" title="' . esc_attr__('Upload PDF', 'mxchat') . '">';
395 386 echo ' <!-- Icon from Font Awesome Free: https://fontawesome.com/license/free -->';
396 387 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" stroke="currentColor">';
397 388 echo ' <path d="M64 464l48 0 0 48-48 0c-35.3 0-64-28.7-64-64L0 64C0 28.7 28.7 0 64 0L229.5 0c17 0 33.3 6.7 45.3 18.7l90.5 90.5c12 12 18.7 28.3 18.7 45.3L384 304l-48 0 0-144-80 0c-17.7 0-32-14.3-32-32l0-80L64 48c-8.8 0-16 7.2-16 16l0 384c0 8.8 7.2 16 16 16zM176 352l32 0c30.9 0 56 25.1 56 56s-25.1 56-56 56l-16 0 0 32c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-48 0-80c0-8.8 7.2-16 16-16zm32 80c13.3 0 24-10.7 24-24s-10.7-24-24-24l-16 0 0 48 16 0zm96-80l32 0c26.5 0 48 21.5 48 48l0 64c0 26.5-21.5 48-48 48l-32 0c-8.8 0-16-7.2-16-16l0-128c0-8.8 7.2-16 16-16zm32 128c8.8 0 16-7.2 16-16l0-64c0-8.8-7.2-16-16-16l-16 0 0 96 16 0zm80-112c0-8.8 7.2-16 16-16l48 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 32 32 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 48c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-64 0-64z"></path>';
398 389 echo ' </svg>';
@@ -401,19 +392,19 @@
401 392
402 393 // Word Upload Button - wrapped in conditional using current_options
403 394 $show_word_button = isset($current_options['show_word_upload_button']) ? $current_options['show_word_upload_button'] : 'on';
404 395 if ($show_word_button === 'on') {
405 - echo ' <input type="file" id="word-upload-' . esc_attr($bot_id) . '" class="word-upload" accept=".docx" style="display: none;">';
406 - echo ' <button id="word-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn word-upload-btn" title="' . esc_attr__('Upload Word Document', 'mxchat') . '">';
396 + echo ' <input type="file" id="word-upload" accept=".docx" style="display: none;">';
397 + echo ' <button id="word-upload-btn" class="toolbar-btn" title="' . esc_attr__('Upload Word Document', 'mxchat') . '">';
407 398 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" stroke="currentColor">';
408 399 echo ' <path d="M48 448L48 64c0-8.8 7.2-16 16-16l160 0 0 80c0 17.7 14.3 32 32 32l80 0 0 288c0 8.8-7.2 16-16 16L64 464c-8.8 0-16-7.2-16-16zM64 0C28.7 0 0 28.7 0 64L0 448c0 35.3 28.7 64 64 64l256 0c35.3 0 64-28.7 64-64l0-293.5c0-17-6.7-33.3-18.7-45.3L274.7 18.7C262.7 6.7 246.5 0 229.5 0L64 0zm55 241.1c-3.8-12.7-17.2-19.9-29.9-16.1s-19.9 17.2-16.1 29.9l48 160c3 10.2 12.4 17.1 23 17.1s19.9-7 23-17.1l25-83.4 25 83.4c3 10.2 12.4 17.1 23 17.1s19.9-7 23-17.1l48-160c3.8-12.7-3.4-26.1-16.1-29.9s-26.1 3.4-29.9 16.1l-25 83.4-25-83.4c-3-10.2-12.4-17.1-23-17.1s-19.9 7-23 17.1l-25 83.4-25-83.4z"/></svg>';
409 400 echo ' </button>';
410 401 }
411 402
412 - // File containers
413 - echo ' <div id="active-pdf-container-' . esc_attr($bot_id) . '" class="active-pdf-container" style="display: none;">';
414 - echo ' <span id="active-pdf-name-' . esc_attr($bot_id) . '" class="active-pdf-name"></span>';
415 - echo ' <button id="remove-pdf-btn-' . esc_attr($bot_id) . '" class="remove-pdf-btn" title="' . esc_attr__('Remove PDF', 'mxchat') . '">';
403 + // File containers (unchanged)
404 + echo ' <div id="active-pdf-container" class="active-pdf-container" style="display: none;">';
405 + echo ' <span id="active-pdf-name" class="active-pdf-name"></span>';
406 + echo ' <button id="remove-pdf-btn" class="remove-pdf-btn" title="' . esc_attr__('Remove PDF', 'mxchat') . '">';
416 407 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2">';
417 408 echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
418 409 echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
419 410 echo ' </svg>';
@@ -418,12 +409,12 @@
418 409 echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
419 410 echo ' </svg>';
420 411 echo ' </button>';
421 412 echo ' </div>';
422 -
423 - echo ' <div id="active-word-container-' . esc_attr($bot_id) . '" class="active-word-container" style="display: none;">';
424 - echo ' <span id="active-word-name-' . esc_attr($bot_id) . '" class="active-word-name"></span>';
425 - echo ' <button id="remove-word-btn-' . esc_attr($bot_id) . '" class="remove-word-btn" title="' . esc_attr__('Remove Word Document', 'mxchat') . '">';
413 +
414 + echo ' <div id="active-word-container" class="active-pdf-container" style="display: none;">';
415 + echo ' <span id="active-word-name" class="active-pdf-name"></span>';
416 + echo ' <button id="remove-word-btn" class="remove-pdf-btn" title="' . esc_attr__('Remove Word Document', 'mxchat') . '">';
426 417 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2">';
427 418 echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
428 419 echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
429 420 echo ' </svg>';
@@ -429,11 +420,11 @@
429 420 echo ' </svg>';
430 421 echo ' </button>';
431 422 echo ' </div>';
432 423
433 - // Perplexity Button
424 + // Perplexity Button (unchanged except for the conditional)
434 425 if (apply_filters('mxchat_perplexity_should_show_logo', true)) {
435 - echo ' <button id="perplexity-search-btn-' . esc_attr($bot_id) . '" class="toolbar-btn perplexity-search-btn" title="' . esc_attr__('Search with Perplexity', 'mxchat-perplexity') . '">';
426 + echo ' <button id="perplexity-search-btn" class="toolbar-btn" title="' . esc_attr__('Search with Perplexity', 'mxchat-perplexity') . '">';
436 427 echo ' <svg fill="currentColor" height="1em" viewBox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg">';
437 428 echo ' <path d="M19.785 0v7.272H22.5V17.62h-2.935V24l-7.037-6.194v6.145h-1.091v-6.152L4.392 24v-6.465H1.5V7.188h2.884V0l7.053 6.494V.19h1.09v6.49L19.786 0zm-7.257 9.044v7.319l5.946 5.234V14.44l-5.946-5.397zm-1.099-.08l-5.946 5.398v7.235l5.946-5.234V8.965zm8.136 7.58h1.844V8.349H13.46l6.105 5.54v2.655zm-8.982-8.28H2.59v8.195h1.8v-2.576l6.192-5.62zM5.475 2.476v4.71h5.115l-5.115-4.71zm13.219 0l-5.115 4.71h5.115v-4.71z"></path>';
438 429 echo ' </svg>';
439 430 echo ' </button>';
@@ -440,10 +431,10 @@
440 431 }
441 432
442 433
443 434 // Image Analysis Button - hidden by default, controlled by MxChat Vision add-on
444 - echo ' <input type="file" id="image-upload-' . esc_attr($bot_id) . '" class="image-upload" accept="image/*" style="display: none;" multiple>';
445 - echo ' <button id="image-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn image-upload-btn" title="' . esc_attr__('Upload Image for Analysis', 'mxchat') . '" style="display: none;">';
435 + echo ' <input type="file" id="image-upload" accept="image/*" style="display: none;" multiple>';
436 + echo ' <button id="image-upload-btn" class="toolbar-btn" title="' . esc_attr__('Upload Image for Analysis', 'mxchat') . '" style="display: none;">';
446 437 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="fill: none !important;">';
447 438 echo ' <path d="M14.5 4h-5L7 7H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-3l-2.5-3z" style="fill: none !important;"/>';
448 439 echo ' <circle cx="12" cy="13" r="3" style="fill: none !important;"/>';
449 440 echo ' </svg>';
@@ -466,31 +457,22 @@
466 457
467 458 if ($is_floating) {
468 459 echo '</div>';
469 460
470 - if (!empty($pre_chat_message)) {
471 - // Rendered hidden by default — JS checkPreChatDismissal() handles show/hide via localStorage
472 - // data-nosnippet: the pre-chat teaser bubble is a sibling outside
473 - // .mxchat-chatbot-wrapper, so it needs its own marker to stay out of snippets.
474 - echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message" data-nosnippet style="display:none;">';
475 - echo nl2br(esc_html($pre_chat_message));
461 + if (!empty($pre_chat_message) && !get_transient($transient_key)) {
462 + echo '<div id="pre-chat-message">';
463 + echo esc_html($pre_chat_message);
476 464 echo '<button class="close-pre-chat-message" aria-label="' . esc_attr__('Close', 'mxchat') . '">&times;</button>';
477 465 echo '</div>';
478 466 }
479 -
480 - $is_initially_hidden = (strpos($visibility_class, 'hidden') !== false);
481 - $aria_expanded = $is_initially_hidden ? 'false' : 'true';
482 - 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) . ';"') . '>';
483 - 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>';
484 -
467 +
468 + echo '<div class="' . esc_attr($visibility_class) . '" id="floating-chatbot-button" style="background: ' . esc_attr($chatbot_background_color) . '; color: ' . esc_attr($send_button_font_color) . ';">';
469 + echo '<div id="chat-notification-badge" 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>';
470 +
485 471 if (!empty($custom_icon)) {
486 472 echo '<img src="' . $custom_icon . '" alt="' . esc_attr__('Chatbot Icon', 'mxchat') . '" style="height: 48px; width: 48px; object-fit: contain;" />';
487 473 } else {
488 - $widget_icon_style = 'height: 48px; width: 48px;';
489 - if (!$skip_inline_colors) {
490 - $widget_icon_style .= ' fill: ' . esc_attr($icon_color) . ';';
491 - }
492 - echo '<svg id="widget_icon_10" style="' . $widget_icon_style . '" viewBox="0 0 1120 1120" fill="none" xmlns="http://www.w3.org/2000/svg">';
474 + echo '<svg id="widget_icon_10" style="height: 48px; width: 48px; fill: ' . esc_attr($icon_color) . '" viewBox="0 0 1120 1120" fill="none" xmlns="http://www.w3.org/2000/svg">';
493 475 echo ' <path fill-rule="evenodd" clip-rule="evenodd" d="M252 434C252 372.144 302.144 322 364 322H770C831.856 322 882 372.144 882 434V614.459L804.595 585.816C802.551 585.06 800.94 583.449 800.184 581.405L763.003 480.924C760.597 474.424 751.403 474.424 748.997 480.924L711.816 581.405C711.06 583.449 709.449 585.06 707.405 585.816L606.924 622.997C600.424 625.403 600.424 634.597 606.924 637.003L707.405 674.184C709.449 674.94 711.06 676.551 711.816 678.595L740.459 756H629.927C629.648 756.476 629.337 756.945 628.993 757.404L578.197 825.082C572.597 832.543 561.403 832.543 555.803 825.082L505.007 757.404C504.663 756.945 504.352 756.476 504.073 756H364C302.144 756 252 705.856 252 644V434ZM633.501 471.462C632.299 468.212 627.701 468.212 626.499 471.462L619.252 491.046C618.874 492.068 618.068 492.874 617.046 493.252L597.462 500.499C594.212 501.701 594.212 506.299 597.462 507.501L617.046 514.748C618.068 515.126 618.874 515.932 619.252 516.954L626.499 536.538C627.701 539.788 632.299 539.788 633.501 536.538L640.748 516.954C641.126 515.932 641.932 515.126 642.954 514.748L662.538 507.501C665.788 506.299 665.788 501.701 662.538 500.499L642.954 493.252C641.932 492.874 641.126 492.068 640.748 491.046L633.501 471.462Z" ></path>';
494 476 echo ' <path d="M771.545 755.99C832.175 755.17 881.17 706.175 881.99 645.545L804.595 674.184C802.551 674.94 800.94 676.551 800.184 678.595L771.545 755.99Z" ></path>';
495 477 echo '</svg>';
496 478 }
@@ -539,9 +521,12 @@
539 521 }
540 522
541 523
542 524 private function determine_email_collection_state() {
543 - // Logged-in users skip the email form
525 + // Get session ID
526 + $session_id = 'mxchat_chat_' . wp_generate_uuid4(); // You'll need to use your actual session generation logic
527 +
528 + // Check if user is logged in first
544 529 if (is_user_logged_in()) {
545 530 $current_user = wp_get_current_user();
546 531 return [
547 532 'show_email_form' => false,
@@ -548,16 +533,26 @@
548 533 'user_email' => $current_user->user_email,
549 534 'user_name' => $current_user->display_name ?: $current_user->first_name ?: ''
550 535 ];
551 536 }
552 -
553 - // For guests, default to showing the email form.
554 - // The session-based check happens client-side via AJAX since the
555 - // session ID lives in the browser cookie/localStorage.
537 +
538 + // Check stored email for session
539 + $stored_email = get_option("mxchat_email_{$session_id}", '');
540 + $stored_name = get_option("mxchat_name_{$session_id}", '');
541 +
542 + // Check if name is required
543 + $name_field_enabled = isset($this->options['enable_name_field']) &&
544 + ($this->options['enable_name_field'] === '1' || $this->options['enable_name_field'] === 'on');
545 +
546 + $has_required_info = !empty($stored_email);
547 + if ($name_field_enabled) {
548 + $has_required_info = $has_required_info && !empty($stored_name);
549 + }
550 +
556 551 return [
557 - 'show_email_form' => true,
558 - 'user_email' => '',
559 - 'user_name' => ''
552 + 'show_email_form' => !$has_required_info,
553 + 'user_email' => $stored_email,
554 + 'user_name' => $stored_name
560 555 ];
561 556 }
562 557
563 558
@@ -564,58 +559,32 @@
564 559 /**
565 560 * NEW: Determine if and which chatbot should be displayed
566 561 */
567 562 private function get_display_bot() {
568 - // Delegates to the static so the enqueue-time gate (smart asset loading,
569 - // plan-915355) and this render-time decision share ONE code path and can
570 - // never disagree. The static reads mxchat_options fresh — same row this
571 - // instance loaded at construct.
572 - return self::compute_display_bot();
573 -}
574 -
575 -/**
576 - * Static single source of truth for the display decision (plan-915355).
577 - * Combines the per-page meta box, the append_to_body global toggle, and the
578 - * post-type include/exclude mode. Returns a bot id ('default' or specific)
579 - * when the auto-append widget will render on the current request, false when
580 - * it won't. Static (not a second instance) deliberately: MxChat_Public's
581 - * constructor registers a wp_footer action, so constructing a throwaway
582 - * instance would double-append the widget.
583 - */
584 -public static function compute_display_bot() {
585 - $options = get_option('mxchat_options', array());
586 - if (!is_array($options)) {
587 - $options = array();
588 - }
589 -
590 563 // Get page-specific settings from meta box
591 - $page_setting = self::get_page_bot_setting();
592 -
593 - $global_autoshow = isset($options['append_to_body']) && $options['append_to_body'] === 'on';
594 - $global_default_bot = isset($options['default_bot']) ? $options['default_bot'] : 'default';
595 -
564 + $page_setting = $this->get_page_bot_setting();
565 +
566 + // Get global settings - FIXED: Check for 'on' instead of 'on'
567 + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
568 + $global_default_bot = isset($this->options['default_bot']) ? $this->options['default_bot'] : 'default';
569 +
596 570 // If page specifically hides chatbot, don't show anything
597 571 if ($page_setting && $page_setting['action'] === 'hide') {
598 572 return false;
599 573 }
600 -
574 +
601 575 // If page specifies a specific bot, use that
602 576 if ($page_setting && $page_setting['action'] === 'show') {
603 577 return $page_setting['bot_id'];
604 578 }
605 -
579 +
606 580 // Page setting is 'global' or no page setting exists
607 581 // Check global auto-show setting
608 582 if ($global_autoshow) {
609 - // Check post type visibility settings
610 - if (!self::should_show_on_current_post_type()) {
611 - return false;
612 - }
613 -
614 583 // Global auto-show is enabled, return the default bot
615 584 return $global_default_bot;
616 585 }
617 -
586 +
618 587 // Global auto-show is disabled and no page-specific bot selected
619 588 // Don't show chatbot (user should use shortcodes)
620 589 return false;
621 590 }
@@ -620,183 +589,31 @@
620 589 return false;
621 590 }
622 591
623 592 /**
624 - * Smart asset loading opt-in (plan-915355). Standalone option — deliberately
625 - * NOT a mxchat_options key, so it can never be stripped by mxchat_sanitize().
626 - * Default off: enqueue behavior is byte-identical to before until an owner
627 - * turns the toggle on.
593 + * NEW: Get page-specific bot setting
628 594 */
629 -public static function is_smart_asset_loading_enabled() {
630 - return get_option('mxchat_smart_asset_loading', 'off') === 'on';
631 -}
632 -
633 -/**
634 - * Will the chat widget render on the current request? (plan-915355)
635 - *
636 - * True when the auto-append decision resolves to a bot, OR the singular
637 - * post's content contains the [mxchat_chatbot] shortcode (first-chance
638 - * detection so shortcode pages keep head-loaded CSS — no FOUC). Computed
639 - * once per request and cached, so the wp_enqueue_scripts gate and any
640 - * add-on consulting this later in the same request always get one answer.
641 - *
642 - * Filter `mxchat_should_load_assets` is the force-load escape hatch for
643 - * headless/builder/custom-JS setups whose shortcode placement is invisible
644 - * to has_shortcode (builder-stored content, template files, widget areas).
645 - * Note the render-time safety net in render_chatbot_shortcode() still
646 - * force-loads assets whenever the shortcode actually renders — the filter
647 - * is only needed where even that net can't fire (e.g. markup assembled
648 - * outside WP rendering).
649 - */
650 -public static function should_load_assets() {
651 - static $cached = null;
652 - if ($cached !== null) {
653 - return $cached;
654 - }
655 -
656 - // Never gate admin/ajax requests — this decision is for front-end enqueues only.
657 - if (is_admin()) {
658 - $cached = true;
659 - return $cached;
660 - }
661 -
662 - $display_bot = self::compute_display_bot();
663 - $has_shortcode = false;
664 -
665 - if ($display_bot === false && is_singular()) {
666 - $post = get_post();
667 - if ($post && has_shortcode((string) $post->post_content, 'mxchat_chatbot')) {
668 - $has_shortcode = true;
669 - }
670 - }
671 -
672 - $should = ($display_bot !== false) || $has_shortcode;
673 -
674 - $cached = (bool) apply_filters('mxchat_should_load_assets', $should, array(
675 - 'display_bot' => $display_bot,
676 - 'has_shortcode' => $has_shortcode,
677 - 'post_id' => get_the_ID(),
678 - ));
679 -
680 - return $cached;
681 -}
682 -
683 -/**
684 - * Check if chatbot should be shown on the current post type
685 - */
686 -private static function should_show_on_current_post_type() {
687 - $options = get_option('mxchat_options', array());
688 - if (!is_array($options)) {
689 - $options = array();
690 - }
691 - // Get visibility settings
692 - $mode = isset($options['post_type_visibility_mode']) ? $options['post_type_visibility_mode'] : 'all';
693 - $list = isset($options['post_type_visibility_list']) ? $options['post_type_visibility_list'] : array();
694 -
695 - // Ensure list is an array
696 - if (!is_array($list)) {
697 - $list = array();
698 - }
699 -
700 - // If mode is 'all', show on all post types
701 - if ($mode === 'all') {
702 - return true;
703 - }
704 -
705 - // Get current post type
706 - $current_post_type = self::get_current_post_type();
707 -
708 - // If we can't determine post type, default to showing
709 - if (empty($current_post_type)) {
710 - return true;
711 - }
712 -
713 - // Check based on mode
714 - if ($mode === 'include') {
715 - // Only show on selected post types
716 - return in_array($current_post_type, $list);
717 - } elseif ($mode === 'exclude') {
718 - // Hide on selected post types
719 - return !in_array($current_post_type, $list);
720 - }
721 -
722 - // Default to showing
723 - return true;
724 -}
725 -
726 -/**
727 - * Get the current post type
728 - */
729 -private static function get_current_post_type() {
730 - // Try to get from queried object first
731 - $queried_object = get_queried_object();
732 -
733 - if ($queried_object instanceof WP_Post) {
734 - return $queried_object->post_type;
735 - }
736 -
737 - // Try get_post_type()
738 - $post_type = get_post_type();
739 - if ($post_type) {
740 - return $post_type;
741 - }
742 -
743 - // Check if we're on an archive
744 - if (is_post_type_archive()) {
745 - return get_query_var('post_type');
746 - }
747 -
748 - // Check common archive types
749 - if (is_home() || is_single()) {
750 - return 'post';
751 - }
752 -
753 - if (is_page()) {
754 - return 'page';
755 - }
756 -
757 - return '';
758 -}
759 -
760 -/**
761 - * Get page-specific bot setting using new visibility field with backward compat
762 - */
763 -private static function get_page_bot_setting($post_id = null) {
595 +private function get_page_bot_setting($post_id = null) {
764 596 if (!$post_id) {
765 597 $post_id = get_the_ID();
766 598 }
767 -
599 +
768 600 if (!$post_id) {
769 601 return null;
770 602 }
771 -
772 - // Check new visibility field first
773 - $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true);
774 -
775 - if ($visibility === 'hide') {
603 +
604 + // Check if chatbot is hidden on this page
605 + $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true);
606 + if ($hide_chatbot === '1') {
776 607 return array('action' => 'hide');
777 608 }
778 -
779 - if ($visibility === 'show') {
780 - $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
781 - $bot_id = !empty($selected_bot) ? $selected_bot : 'default';
782 - return array('action' => 'show', 'bot_id' => $bot_id);
783 - }
784 -
785 - // Backward compat: check legacy hide checkbox if no new field set
786 - if (empty($visibility)) {
787 - $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true);
788 - if ($hide_chatbot === '1') {
789 - return array('action' => 'hide');
790 - }
791 - }
792 -
793 - // Check if specific bot is selected (legacy path)
609 +
610 + // Check if specific bot is selected
794 611 $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
795 612 if (!empty($selected_bot)) {
796 613 return array('action' => 'show', 'bot_id' => $selected_bot);
797 614 }
798 -
615 +
799 616 // Use global setting
800 617 return array('action' => 'global');
801 618 }
802 619
@@ -809,9 +626,9 @@
809 626 return $shortcode_bot_id;
810 627 }
811 628
812 629 // No bot_id in shortcode, check page setting
813 - $page_setting = self::get_page_bot_setting();
630 + $page_setting = $this->get_page_bot_setting();
814 631 if ($page_setting && $page_setting['action'] === 'show') {
815 632 return $page_setting['bot_id'];
816 633 }
817 634
@@ -828,8 +645,27 @@
828 645
829 646 /**
830 647 * UPDATED: Debug function with new context info
831 648 */
649 +public function debug_display_logic() {
650 + if (current_user_can('manage_options') && isset($_GET['mxchat_debug'])) {
651 + $bot_to_show = $this->get_display_bot();
652 + $page_setting = $this->get_page_bot_setting();
653 + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
654 + $hide_auto = $this->should_hide_chatbot('auto');
655 +
656 + 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;">';
657 + echo '<h4 style="margin: 0 0 10px 0;">MxChat Debug Info</h4>';
658 + echo '<p><strong>Raw append_to_body value:</strong> "' . ($this->options['append_to_body'] ?? 'NOT SET') . '"</p>';
659 + echo '<p><strong>Page Setting:</strong><br><pre>' . print_r($page_setting, true) . '</pre></p>';
660 + echo '<p><strong>Global Auto-show:</strong> ' . ($global_autoshow ? 'ON' : 'OFF') . '</p>';
661 + echo '<p><strong>Hide Auto-Append:</strong> ' . ($hide_auto ? 'YES' : 'NO') . '</p>';
662 + echo '<p><strong>Bot to Show:</strong> ' . ($bot_to_show === false ? 'NONE' : $bot_to_show) . '</p>';
663 + echo '<p><strong>Shortcodes:</strong> floating="no" always works</p>';
664 + echo '<p><small>Add ?mxchat_debug=1 to URL to see this</small></p>';
665 + echo '</div>';
666 + }
667 +}
832 668
833 669 /**
834 670 * NEW: Helper method to get available bots (for admin notices, etc.)
835 671 */