| @@ -5,14 +5,8 @@ | ||
| 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')); |
| @@ -62,18 +56,9 @@ | ||
| 62 | 56 | // Check if auto-append chatbot should be hidden on this page |
| 63 | 57 | if ($this->should_hide_chatbot('auto')) { |
| 64 | 58 | return; // Don't show auto-appended chatbot |
| 65 | 59 | } |
| 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 | - | |
| 60 | + | |
| 76 | 61 | // Get the bot that should be displayed using new logic |
| 77 | 62 | $bot_to_show = $this->get_display_bot(); |
| 78 | 63 | |
| 79 | 64 | // Don't show chatbot if determination is false |
| @@ -110,22 +95,8 @@ | ||
| 110 | 95 | /** |
| 111 | 96 | * UPDATED: Enhanced shortcode with context-aware hiding |
| 112 | 97 | */ |
| 113 | 98 | 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 | 99 | // UPDATED: Add bot_id parameter support and improve logic |
| 129 | 100 | $attributes = shortcode_atts(array( |
| 130 | 101 | 'floating' => 'yes', |
| 131 | 102 | 'has_consent' => 'yes', |
| @@ -134,16 +105,20 @@ | ||
| 134 | 105 | |
| 135 | 106 | // Determine which bot to use |
| 136 | 107 | $bot_id = $this->determine_bot_for_shortcode($attributes['bot_id']); |
| 137 | 108 | |
| 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). | |
| 109 | + // UPDATED: Only check hiding for floating shortcodes that could conflict with auto-append | |
| 110 | + // Non-floating shortcodes should always work | |
| 142 | 111 | if ($attributes['floating'] === 'yes') { |
| 143 | - self::$floating_instance_rendered = true; | |
| 112 | + // For floating shortcodes, check if auto-append is hidden | |
| 113 | + // This prevents duplicate floating chatbots | |
| 114 | + if ($this->should_hide_chatbot('auto') && $this->is_auto_append_enabled()) { | |
| 115 | + // If auto-append is enabled but hidden on this page, | |
| 116 | + // allow the floating shortcode to work (user is overriding) | |
| 117 | + // But if auto-append is disabled globally, also allow shortcode | |
| 118 | + } | |
| 144 | 119 | } |
| 145 | - | |
| 120 | + | |
| 146 | 121 | // Non-floating shortcodes (floating="no") should NEVER be blocked by the hide setting |
| 147 | 122 | // This allows embedded chatbots even when floating is hidden |
| 148 | 123 | |
| 149 | 124 | $is_floating = $attributes['floating'] === 'yes'; |
| @@ -226,28 +201,14 @@ | ||
| 226 | 201 | $enable_email_block = isset($current_options['enable_email_block']) && |
| 227 | 202 | ($current_options['enable_email_block'] === '1' || $current_options['enable_email_block'] === 'on'); |
| 228 | 203 | |
| 229 | 204 | // Add name field variables |
| 230 | - $enable_name_field = isset($current_options['enable_name_field']) && | |
| 205 | + $enable_name_field = isset($current_options['enable_name_field']) && | |
| 231 | 206 | ($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']) : | |
| 207 | + $name_field_placeholder = isset($current_options['name_field_placeholder']) ? | |
| 208 | + esc_attr($current_options['name_field_placeholder']) : | |
| 234 | 209 | esc_attr__('Enter your name', 'mxchat'); |
| 235 | 210 | |
| 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 | 211 | ob_start(); |
| 251 | 212 | |
| 252 | 213 | // Check if floating attribute is set to 'yes' and wrap accordingly |
| 253 | 214 | if ($is_floating) { |
| @@ -254,12 +215,9 @@ | ||
| 254 | 215 | echo '<div id="floating-chatbot-' . esc_attr($bot_id) . '" class="floating-chatbot ' . $initial_visibility . $additional_class . '">'; |
| 255 | 216 | } |
| 256 | 217 | |
| 257 | 218 | // 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) . '">'; | |
| 219 | + echo '<div id="mxchat-chatbot-wrapper-' . esc_attr($bot_id) . '" class="mxchat-chatbot-wrapper" data-bot-id="' . esc_attr($bot_id) . '">'; | |
| 262 | 220 | |
| 263 | 221 | 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 | 222 | echo ' <div class="chatbot-title-container">'; |
| 265 | 223 | echo ' <div class="chatbot-title-group">'; |
| @@ -272,25 +230,13 @@ | ||
| 272 | 230 | if (!empty(trim($ai_agent_text))) { |
| 273 | 231 | 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 | 232 | } |
| 275 | 233 | 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>'; | |
| 234 | + echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Minimize', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>'; | |
| 235 | + 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) . ';"') . '>'; | |
| 236 | + echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>'; | |
| 291 | 237 | echo ' </svg>'; |
| 292 | - echo ' <span>' . esc_html__('Close', 'mxchat') . '</span>'; | |
| 238 | + echo ' <span>' . esc_html__('Minimize', 'mxchat') . '</span>'; | |
| 293 | 239 | echo ' </button>'; |
| 294 | 240 | echo ' </div>'; |
| 295 | 241 | |
| 296 | 242 | // 3b) Main chatbot container |
| @@ -315,20 +261,8 @@ | ||
| 315 | 261 | } |
| 316 | 262 | |
| 317 | 263 | echo ' <label for="user-email-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>'; |
| 318 | 264 | 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 | 265 | echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">'; |
| 332 | 266 | $button_text = isset($current_options['email_blocker_button_text']) |
| 333 | 267 | ? $current_options['email_blocker_button_text'] |
| 334 | 268 | : esc_html__('Start Chat', 'mxchat'); |
| @@ -344,22 +278,10 @@ | ||
| 344 | 278 | echo ' </div>'; |
| 345 | 279 | echo ' </div>'; |
| 346 | 280 | |
| 347 | 281 | 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') . '">'; | |
| 355 | - // mxchat-intro-message marks the greeting bubble so nothing has to | |
| 356 | - // infer it from position (plan a1a79b). Only this element ever | |
| 357 | - // carries the class — reset and the {visitor_name} substitution | |
| 358 | - // both key off it, and both used to assume the greeting was still | |
| 359 | - // the first .bot-message in the box, which stops being true the | |
| 360 | - // moment chat persistence rehydrates the transcript. | |
| 361 | - echo ' <div class="bot-message mxchat-intro-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>'; | |
| 282 | + echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box">'; | |
| 283 | + echo ' <div class="bot-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>'; | |
| 362 | 284 | echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>'; |
| 363 | 285 | echo wp_kses_post($intro_message); |
| 364 | 286 | echo ' </div>'; |
| 365 | 287 | echo ' </div>'; |
| @@ -405,25 +327,9 @@ | ||
| 405 | 327 | echo ' </div>'; |
| 406 | 328 | echo ' </div>'; |
| 407 | 329 | |
| 408 | 330 | echo ' <div id="input-container-' . esc_attr($bot_id) . '" class="input-container">'; |
| 409 | - // Max input length (plan a3fae2 part C) — global core setting, 0 = unlimited. | |
| 410 | - // Hard-caps typing/paste client-side; the chat handler enforces it server-side too. | |
| 411 | - $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0; | |
| 412 | - $mxchat_maxlength_attr = $mxchat_max_input_length > 0 ? ' maxlength="' . esc_attr($mxchat_max_input_length) . '"' : ''; | |
| 413 | - // Visually-hidden label = the input's accessible name (WCAG 3.3.2, | |
| 414 | - // plan 67f126). The placeholder stays as visible prompt copy but | |
| 415 | - // cannot be the name — it vanishes on the first keystroke. | |
| 416 | - echo ' <label for="chat-input-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Type your message', 'mxchat') . '</label>'; | |
| 417 | - 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>'; | |
| 418 | - // Language-neutral character counter (plan 7091a2). Numbers only — no | |
| 419 | - // translatable strings — so it reads correctly on every-language install. | |
| 420 | - // Only rendered when a cap is set; hidden until ~80% of the cap, then | |
| 421 | - // ramps neutral -> amber -> red. Decorative (aria-hidden); the textarea's | |
| 422 | - // maxlength carries the real semantics for assistive tech. | |
| 423 | - if ($mxchat_max_input_length > 0) { | |
| 424 | - 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>'; | |
| 425 | - } | |
| 331 | + 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>'; | |
| 426 | 332 | echo ' <button id="send-button-' . esc_attr($bot_id) . '" class="send-button" aria-label="' . esc_attr__('Send message', 'mxchat') . '">'; |
| 427 | 333 | if (!empty($custom_send_image)) { |
| 428 | 334 | 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);" />'; |
| 429 | 335 | } else { |
| @@ -521,19 +427,15 @@ | ||
| 521 | 427 | echo '</div>'; |
| 522 | 428 | |
| 523 | 429 | if (!empty($pre_chat_message)) { |
| 524 | 430 | // Rendered hidden by default — JS checkPreChatDismissal() handles show/hide via localStorage |
| 525 | - // data-nosnippet: the pre-chat teaser bubble is a sibling outside | |
| 526 | - // .mxchat-chatbot-wrapper, so it needs its own marker to stay out of snippets. | |
| 527 | - echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message" data-nosnippet style="display:none;">'; | |
| 431 | + echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message" style="display:none;">'; | |
| 528 | 432 | echo nl2br(esc_html($pre_chat_message)); |
| 529 | 433 | echo '<button class="close-pre-chat-message" aria-label="' . esc_attr__('Close', 'mxchat') . '">×</button>'; |
| 530 | 434 | echo '</div>'; |
| 531 | 435 | } |
| 532 | 436 | |
| 533 | - $is_initially_hidden = (strpos($visibility_class, 'hidden') !== false); | |
| 534 | - $aria_expanded = $is_initially_hidden ? 'false' : 'true'; | |
| 535 | - 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) . ';"') . '>'; | |
| 437 | + 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) . ';"') . '>'; | |
| 536 | 438 | 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>'; |
| 537 | 439 | |
| 538 | 440 | if (!empty($custom_icon)) { |
| 539 | 441 | echo '<img src="' . $custom_icon . '" alt="' . esc_attr__('Chatbot Icon', 'mxchat') . '" style="height: 48px; width: 48px; object-fit: contain;" />'; |
| @@ -617,50 +519,14 @@ | ||
| 617 | 519 | /** |
| 618 | 520 | * NEW: Determine if and which chatbot should be displayed |
| 619 | 521 | */ |
| 620 | 522 | private function get_display_bot() { |
| 621 | - // Delegates to the static so the enqueue-time gate (smart asset loading, | |
| 622 | - // plan-915355) and this render-time decision share ONE code path and can | |
| 623 | - // never disagree. The static reads mxchat_options fresh — same row this | |
| 624 | - // instance loaded at construct. | |
| 625 | - return self::compute_display_bot(); | |
| 626 | -} | |
| 627 | - | |
| 628 | -/** | |
| 629 | - * Static single source of truth for the display decision (plan-915355). | |
| 630 | - * Combines the per-page meta box, the append_to_body global toggle, and the | |
| 631 | - * post-type include/exclude mode. Returns a bot id ('default' or specific) | |
| 632 | - * when the auto-append widget will render on the current request, false when | |
| 633 | - * it won't. Static (not a second instance) deliberately: MxChat_Public's | |
| 634 | - * constructor registers a wp_footer action, so constructing a throwaway | |
| 635 | - * instance would double-append the widget. | |
| 636 | - */ | |
| 637 | -public static function compute_display_bot() { | |
| 638 | - // The Elementor builder canvas is not a chat surface (plan 95dd1e part 2): | |
| 639 | - // its preview iframe is a real front-end page load, so without this guard | |
| 640 | - // the Auto-Display instance renders LIVE inside the canvas — front-end | |
| 641 | - // chat scripts firing in the editor, able to start a session for the | |
| 642 | - // person editing. Returning false here suppresses both the wp_footer | |
| 643 | - // auto-append and the asset enqueue gate for preview requests only; the | |
| 644 | - // placement widget's own canvas rendering is a static placeholder anyway. | |
| 645 | - if (did_action('elementor/loaded') | |
| 646 | - && class_exists('\Elementor\Plugin') | |
| 647 | - && isset(\Elementor\Plugin::$instance->preview) | |
| 648 | - && \Elementor\Plugin::$instance->preview | |
| 649 | - && \Elementor\Plugin::$instance->preview->is_preview_mode()) { | |
| 650 | - return false; | |
| 651 | - } | |
| 652 | - | |
| 653 | - $options = get_option('mxchat_options', array()); | |
| 654 | - if (!is_array($options)) { | |
| 655 | - $options = array(); | |
| 656 | - } | |
| 657 | - | |
| 658 | 523 | // Get page-specific settings from meta box |
| 659 | - $page_setting = self::get_page_bot_setting(); | |
| 524 | + $page_setting = $this->get_page_bot_setting(); | |
| 660 | 525 | |
| 661 | - $global_autoshow = isset($options['append_to_body']) && $options['append_to_body'] === 'on'; | |
| 662 | - $global_default_bot = isset($options['default_bot']) ? $options['default_bot'] : 'default'; | |
| 526 | + // Get global settings - FIXED: Check for 'on' instead of 'on' | |
| 527 | + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on'; | |
| 528 | + $global_default_bot = isset($this->options['default_bot']) ? $this->options['default_bot'] : 'default'; | |
| 663 | 529 | |
| 664 | 530 | // If page specifically hides chatbot, don't show anything |
| 665 | 531 | if ($page_setting && $page_setting['action'] === 'hide') { |
| 666 | 532 | return false; |
| @@ -674,9 +540,9 @@ | ||
| 674 | 540 | // Page setting is 'global' or no page setting exists |
| 675 | 541 | // Check global auto-show setting |
| 676 | 542 | if ($global_autoshow) { |
| 677 | 543 | // Check post type visibility settings |
| 678 | - if (!self::should_show_on_current_post_type()) { | |
| 544 | + if (!$this->should_show_on_current_post_type()) { | |
| 679 | 545 | return false; |
| 680 | 546 | } |
| 681 | 547 | |
| 682 | 548 | // Global auto-show is enabled, return the default bot |
| @@ -688,86 +554,14 @@ | ||
| 688 | 554 | return false; |
| 689 | 555 | } |
| 690 | 556 | |
| 691 | 557 | /** |
| 692 | - * Smart asset loading opt-in (plan-915355). Standalone option — deliberately | |
| 693 | - * NOT a mxchat_options key, so it can never be stripped by mxchat_sanitize(). | |
| 694 | - * Default off: enqueue behavior is byte-identical to before until an owner | |
| 695 | - * turns the toggle on. | |
| 696 | - */ | |
| 697 | -public static function is_smart_asset_loading_enabled() { | |
| 698 | - return get_option('mxchat_smart_asset_loading', 'off') === 'on'; | |
| 699 | -} | |
| 700 | - | |
| 701 | -/** | |
| 702 | - * Will the chat widget render on the current request? (plan-915355) | |
| 703 | - * | |
| 704 | - * True when the auto-append decision resolves to a bot, OR the singular | |
| 705 | - * post's content contains the [mxchat_chatbot] shortcode (first-chance | |
| 706 | - * detection so shortcode pages keep head-loaded CSS — no FOUC). Computed | |
| 707 | - * once per request and cached, so the wp_enqueue_scripts gate and any | |
| 708 | - * add-on consulting this later in the same request always get one answer. | |
| 709 | - * | |
| 710 | - * Filter `mxchat_should_load_assets` is the force-load escape hatch for | |
| 711 | - * headless/builder/custom-JS setups whose shortcode placement is invisible | |
| 712 | - * to has_shortcode (builder-stored content, template files, widget areas). | |
| 713 | - * Note the render-time safety net in render_chatbot_shortcode() still | |
| 714 | - * force-loads assets whenever the shortcode actually renders — the filter | |
| 715 | - * is only needed where even that net can't fire (e.g. markup assembled | |
| 716 | - * outside WP rendering). | |
| 717 | - */ | |
| 718 | -public static function should_load_assets() { | |
| 719 | - static $cached = null; | |
| 720 | - if ($cached !== null) { | |
| 721 | - return $cached; | |
| 722 | - } | |
| 723 | - | |
| 724 | - // Never gate admin/ajax requests — this decision is for front-end enqueues only. | |
| 725 | - if (is_admin()) { | |
| 726 | - $cached = true; | |
| 727 | - return $cached; | |
| 728 | - } | |
| 729 | - | |
| 730 | - $display_bot = self::compute_display_bot(); | |
| 731 | - $has_shortcode = false; | |
| 732 | - | |
| 733 | - if ($display_bot === false && is_singular()) { | |
| 734 | - $post = get_post(); | |
| 735 | - if ($post && has_shortcode((string) $post->post_content, 'mxchat_chatbot')) { | |
| 736 | - $has_shortcode = true; | |
| 737 | - } | |
| 738 | - // The mxchat/chatbot block (plan-95dd1e) stores a block comment, not | |
| 739 | - // shortcode text, so has_shortcode can't see it — first-chance detect | |
| 740 | - // it here for the same no-FOUC reason. The render-time safety net in | |
| 741 | - // render_chatbot_shortcode() still covers template/widget placements. | |
| 742 | - if ($post && !$has_shortcode && function_exists('has_block') | |
| 743 | - && has_block('mxchat/chatbot', $post)) { | |
| 744 | - $has_shortcode = true; | |
| 745 | - } | |
| 746 | - } | |
| 747 | - | |
| 748 | - $should = ($display_bot !== false) || $has_shortcode; | |
| 749 | - | |
| 750 | - $cached = (bool) apply_filters('mxchat_should_load_assets', $should, array( | |
| 751 | - 'display_bot' => $display_bot, | |
| 752 | - 'has_shortcode' => $has_shortcode, | |
| 753 | - 'post_id' => get_the_ID(), | |
| 754 | - )); | |
| 755 | - | |
| 756 | - return $cached; | |
| 757 | -} | |
| 758 | - | |
| 759 | -/** | |
| 760 | 558 | * Check if chatbot should be shown on the current post type |
| 761 | 559 | */ |
| 762 | -private static function should_show_on_current_post_type() { | |
| 763 | - $options = get_option('mxchat_options', array()); | |
| 764 | - if (!is_array($options)) { | |
| 765 | - $options = array(); | |
| 766 | - } | |
| 560 | +private function should_show_on_current_post_type() { | |
| 767 | 561 | // Get visibility settings |
| 768 | - $mode = isset($options['post_type_visibility_mode']) ? $options['post_type_visibility_mode'] : 'all'; | |
| 769 | - $list = isset($options['post_type_visibility_list']) ? $options['post_type_visibility_list'] : array(); | |
| 562 | + $mode = isset($this->options['post_type_visibility_mode']) ? $this->options['post_type_visibility_mode'] : 'all'; | |
| 563 | + $list = isset($this->options['post_type_visibility_list']) ? $this->options['post_type_visibility_list'] : array(); | |
| 770 | 564 | |
| 771 | 565 | // Ensure list is an array |
| 772 | 566 | if (!is_array($list)) { |
| 773 | 567 | $list = array(); |
| @@ -778,9 +572,9 @@ | ||
| 778 | 572 | return true; |
| 779 | 573 | } |
| 780 | 574 | |
| 781 | 575 | // Get current post type |
| 782 | - $current_post_type = self::get_current_post_type(); | |
| 576 | + $current_post_type = $this->get_current_post_type(); | |
| 783 | 577 | |
| 784 | 578 | // If we can't determine post type, default to showing |
| 785 | 579 | if (empty($current_post_type)) { |
| 786 | 580 | return true; |
| @@ -801,9 +595,9 @@ | ||
| 801 | 595 | |
| 802 | 596 | /** |
| 803 | 597 | * Get the current post type |
| 804 | 598 | */ |
| 805 | -private static function get_current_post_type() { | |
| 599 | +private function get_current_post_type() { | |
| 806 | 600 | // Try to get from queried object first |
| 807 | 601 | $queried_object = get_queried_object(); |
| 808 | 602 | |
| 809 | 603 | if ($queried_object instanceof WP_Post) { |
| @@ -835,9 +629,9 @@ | ||
| 835 | 629 | |
| 836 | 630 | /** |
| 837 | 631 | * Get page-specific bot setting using new visibility field with backward compat |
| 838 | 632 | */ |
| 839 | -private static function get_page_bot_setting($post_id = null) { | |
| 633 | +private function get_page_bot_setting($post_id = null) { | |
| 840 | 634 | if (!$post_id) { |
| 841 | 635 | $post_id = get_the_ID(); |
| 842 | 636 | } |
| 843 | 637 | |
| @@ -885,9 +679,9 @@ | ||
| 885 | 679 | return $shortcode_bot_id; |
| 886 | 680 | } |
| 887 | 681 | |
| 888 | 682 | // No bot_id in shortcode, check page setting |
| 889 | - $page_setting = self::get_page_bot_setting(); | |
| 683 | + $page_setting = $this->get_page_bot_setting(); | |
| 890 | 684 | if ($page_setting && $page_setting['action'] === 'show') { |
| 891 | 685 | return $page_setting['bot_id']; |
| 892 | 686 | } |
| 893 | 687 | |