PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.23
MxChat – AI Chatbot & Content Generation for WordPress v3.2.23
3.2.25 3.2.24 3.2.23 3.2.22 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 All 156 releases
← All changes | includes/class-mxchat-public.php +378 -38 3.2.9 → 3.2.23 View file →
@@ -5,8 +5,14 @@
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 +
9 15 public function __construct() {
10 16 // Simply get the options without defining duplicated defaults
11 17 $this->options = get_option('mxchat_options', array());
12 18 add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode'));
@@ -56,9 +62,18 @@
56 62 // Check if auto-append chatbot should be hidden on this page
57 63 if ($this->should_hide_chatbot('auto')) {
58 64 return; // Don't show auto-appended chatbot
59 65 }
60 -
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 +
61 76 // Get the bot that should be displayed using new logic
62 77 $bot_to_show = $this->get_display_bot();
63 78
64 79 // Don't show chatbot if determination is false
@@ -95,8 +110,22 @@
95 110 /**
96 111 * UPDATED: Enhanced shortcode with context-aware hiding
97 112 */
98 113 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 +
99 128 // UPDATED: Add bot_id parameter support and improve logic
100 129 $attributes = shortcode_atts(array(
101 130 'floating' => 'yes',
102 131 'has_consent' => 'yes',
@@ -105,20 +134,16 @@
105 134
106 135 // Determine which bot to use
107 136 $bot_id = $this->determine_bot_for_shortcode($attributes['bot_id']);
108 137
109 - // UPDATED: Only check hiding for floating shortcodes that could conflict with auto-append
110 - // Non-floating shortcodes should always work
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).
111 142 if ($attributes['floating'] === 'yes') {
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 - }
143 + self::$floating_instance_rendered = true;
119 144 }
120 -
145 +
121 146 // Non-floating shortcodes (floating="no") should NEVER be blocked by the hide setting
122 147 // This allows embedded chatbots even when floating is hidden
123 148
124 149 $is_floating = $attributes['floating'] === 'yes';
@@ -201,14 +226,49 @@
201 226 $enable_email_block = isset($current_options['enable_email_block']) &&
202 227 ($current_options['enable_email_block'] === '1' || $current_options['enable_email_block'] === 'on');
203 228
204 229 // Add name field variables
205 - $enable_name_field = isset($current_options['enable_name_field']) &&
230 + $enable_name_field = isset($current_options['enable_name_field']) &&
206 231 ($current_options['enable_name_field'] === '1' || $current_options['enable_name_field'] === 'on');
207 - $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
208 - esc_attr($current_options['name_field_placeholder']) :
232 + $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
233 + esc_attr($current_options['name_field_placeholder']) :
209 234 esc_attr__('Enter your name', 'mxchat');
210 235
236 + // Require Email Address (c0cfaf). Default ON — absent key means required,
237 + // so existing installs render the same markup they always have.
238 + $require_email_field = ($current_options['lead_capture_require_email_toggle'] ?? 'on') === 'on';
239 +
240 + // Pre-fill the form from the profile when a signed-in visitor is being
241 + // shown it (c0cfaf). Emitted ONLY for a logged-in request, so the markup a
242 + // guest gets — the copy a page cache actually stores and re-serves — never
243 + // carries anyone's address. WordPress already marks logged-in responses
244 + // uncacheable, and such a page carries per-user data from other plugins
245 + // regardless.
246 + $prefill_email_attr = '';
247 + $prefill_name_attr = '';
248 + if (is_user_logged_in()) {
249 + if ($user_email !== '') {
250 + $prefill_email_attr = ' value="' . esc_attr($user_email) . '"';
251 + }
252 + if ($user_name !== '') {
253 + $prefill_name_attr = ' value="' . esc_attr($user_name) . '"';
254 + }
255 + }
256 +
257 + // Consent checkbox (b062c4). Default OFF — with the toggle off this block
258 + // renders nothing and the form markup is byte-identical to before.
259 + $enable_consent_checkbox = isset($current_options['enable_consent_checkbox']) &&
260 + ($current_options['enable_consent_checkbox'] === '1' || $current_options['enable_consent_checkbox'] === 'on');
261 + $consent_checkbox_required = isset($current_options['consent_checkbox_required']) &&
262 + ($current_options['consent_checkbox_required'] === '1' || $current_options['consent_checkbox_required'] === 'on');
263 + // Owner content, kses'd through the shared allowlist at render time as well
264 + // as save time — the render is the security boundary the spec names.
265 + $consent_checkbox_label = MxChat_Utils::sanitize_consent_label(
266 + isset($current_options['consent_checkbox_label']) && $current_options['consent_checkbox_label'] !== ''
267 + ? $current_options['consent_checkbox_label']
268 + : __('I agree to the Privacy Policy.', 'mxchat')
269 + );
270 +
211 271 ob_start();
212 272
213 273 // Check if floating attribute is set to 'yes' and wrap accordingly
214 274 if ($is_floating) {
@@ -271,13 +331,29 @@
271 331
272 332 // Add name field if enabled
273 333 if ($enable_name_field) {
274 334 echo ' <label for="user-name-' . esc_attr($bot_id) . '" class="sr-only mxchat-name-label">' . esc_html__('Name', 'mxchat') . '</label>';
275 - echo ' <input type="text" id="user-name-' . esc_attr($bot_id) . '" name="user_name" class="mxchat-name-input" required placeholder="' . $name_field_placeholder . '" />';
335 + echo ' <input type="text" id="user-name-' . esc_attr($bot_id) . '" name="user_name" class="mxchat-name-input" required' . $prefill_name_attr . ' placeholder="' . $name_field_placeholder . '" />';
276 336 }
277 337
338 + // The email field drops its required attribute when the owner
339 + // has made it optional (c0cfaf). Like the consent checkbox,
340 + // the attribute is UX only — the save endpoint re-enforces the
341 + // rule server-side, so a direct POST cannot bypass it.
278 342 echo ' <label for="user-email-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>';
279 - 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') . '" />';
343 + echo ' <input type="email" id="user-email-' . esc_attr($bot_id) . '" name="user_email" class="mxchat-email-input"' . ($require_email_field ? ' required' : '') . $prefill_email_attr . ' placeholder="' . ($require_email_field ? esc_attr__('Enter your email address', 'mxchat') : esc_attr__('Enter your email address (optional)', 'mxchat')) . '" />';
344 +
345 + // Consent checkbox row (b062c4). The native required attribute
346 + // is UX only — the save endpoint re-enforces it server-side.
347 + // Clicking an anchor inside the label activates the link, not
348 + // the checkbox (interactive descendants skip label activation).
349 + if ($enable_consent_checkbox) {
350 + echo ' <div class="mxchat-consent-row">';
351 + echo ' <input type="checkbox" id="user-consent-' . esc_attr($bot_id) . '" name="user_consent" value="1" class="mxchat-consent-checkbox"' . ($consent_checkbox_required ? ' required' : '') . ' />';
352 + echo ' <label for="user-consent-' . esc_attr($bot_id) . '" class="mxchat-consent-label">' . $consent_checkbox_label . '</label>';
353 + echo ' </div>';
354 + }
355 +
280 356 echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">';
281 357 $button_text = isset($current_options['email_blocker_button_text'])
282 358 ? $current_options['email_blocker_button_text']
283 359 : esc_html__('Start Chat', 'mxchat');
@@ -293,10 +369,22 @@
293 369 echo ' </div>';
294 370 echo ' </div>';
295 371
296 372 echo ' <div id="chat-container-' . esc_attr($bot_id) . '" class="chat-container" style="' . ($enable_email_block && $show_email_form ? 'display: none;' : '') . '">';
297 - echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box">';
298 - echo ' <div class="bot-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>';
373 + // role="log" + polite live region: new messages are announced to
374 + // screen readers explicitly, so the arrival signal no longer rides
375 + // the post-reply input autofocus (suppressed on touch — plan 03799f).
376 + // additions-only + non-atomic, with aria-busy held on the streaming
377 + // bubble until [DONE] (chat-script.js) so a streamed reply is
378 + // announced once, complete — not per token (WCAG 4.1.3, plan 67f126).
379 + 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') . '">';
380 + // mxchat-intro-message marks the greeting bubble so nothing has to
381 + // infer it from position (plan a1a79b). Only this element ever
382 + // carries the class — reset and the {visitor_name} substitution
383 + // both key off it, and both used to assume the greeting was still
384 + // the first .bot-message in the box, which stops being true the
385 + // moment chat persistence rehydrates the transcript.
386 + echo ' <div class="bot-message mxchat-intro-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>';
299 387 echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>';
300 388 echo wp_kses_post($intro_message);
301 389 echo ' </div>';
302 390 echo ' </div>';
@@ -342,9 +430,25 @@
342 430 echo ' </div>';
343 431 echo ' </div>';
344 432
345 433 echo ' <div id="input-container-' . esc_attr($bot_id) . '" class="input-container">';
346 - 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>';
434 + // Max input length (plan a3fae2 part C) — global core setting, 0 = unlimited.
435 + // Hard-caps typing/paste client-side; the chat handler enforces it server-side too.
436 + $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
437 + $mxchat_maxlength_attr = $mxchat_max_input_length > 0 ? ' maxlength="' . esc_attr($mxchat_max_input_length) . '"' : '';
438 + // Visually-hidden label = the input's accessible name (WCAG 3.3.2,
439 + // plan 67f126). The placeholder stays as visible prompt copy but
440 + // cannot be the name — it vanishes on the first keystroke.
441 + echo ' <label for="chat-input-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Type your message', 'mxchat') . '</label>';
442 + 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>';
443 + // Language-neutral character counter (plan 7091a2). Numbers only — no
444 + // translatable strings — so it reads correctly on every-language install.
445 + // Only rendered when a cap is set; hidden until ~80% of the cap, then
446 + // ramps neutral -> amber -> red. Decorative (aria-hidden); the textarea's
447 + // maxlength carries the real semantics for assistive tech.
448 + if ($mxchat_max_input_length > 0) {
449 + 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>';
450 + }
347 451 echo ' <button id="send-button-' . esc_attr($bot_id) . '" class="send-button" aria-label="' . esc_attr__('Send message', 'mxchat') . '">';
348 452 if (!empty($custom_send_image)) {
349 453 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);" />';
350 454 } else {
@@ -421,14 +525,22 @@
421 525 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;"/>';
422 526 echo ' <circle cx="12" cy="13" r="3" style="fill: none !important;"/>';
423 527 echo ' </svg>';
424 528 echo ' </button>';
425 -
426 -
529 +
530 + // Toolbar items API (plan 65c9b6). Add-ons put their buttons here
531 + // through the mxchat_chat_toolbar_items filter instead of polling
532 + // the DOM after load. Only asked for when the toolbar is on: an item
533 + // nobody can see is work nobody should do (the Woo cart badge used
534 + // to fetch a count for a hidden toolbar on every page view).
535 + if (isset($this->options['chat_toolbar_toggle']) && $this->options['chat_toolbar_toggle'] === 'on') {
536 + echo self::render_toolbar_items($bot_id);
537 + }
538 +
427 539 echo ' </div>';
428 -
540 +
429 541 echo ' <div class="chatbot-footer">';
430 -
542 +
431 543 // Output the privacy notice if enabled
432 544 if ($privacy_toggle && !empty($privacy_text)) {
433 545 echo '<p class="privacy-notice">' . $privacy_text . '</p>';
434 546 }
@@ -474,10 +586,116 @@
474 586 return ob_get_clean();
475 587 }
476 588
477 589 /**
590 + * Render the add-on items of the chat toolbar (plan-mxchat-20260916-65c9b6).
591 + *
592 + * `mxchat_chat_toolbar_items` receives an empty array and the bot id and
593 + * returns items shaped ['id' => 'woo-cart', 'html' => '<button …>…</button>',
594 + * 'order' => 10]. Items are sorted by order (ties keep filter order), keyed
595 + * by id so a later callback can replace an earlier one, and the html is run
596 + * through wp_kses with the post allow-list plus the SVG shapes a toolbar
597 + * icon needs (filterable via mxchat_chat_toolbar_items_allowed_html).
598 + * Add-ons must not add the dropdowns / panels their button opens here —
599 + * the toolbar sits inside an overflow-hidden widget; append those to body
600 + * from JS on first use, the way the Woo cart does.
601 + *
602 + * @param string $bot_id
603 + * @return string HTML, empty when no add-on contributed anything
604 + */
605 + public static function render_toolbar_items($bot_id) {
606 + $items = apply_filters('mxchat_chat_toolbar_items', array(), $bot_id);
607 + if (!is_array($items) || empty($items)) {
608 + return '';
609 + }
610 +
611 + $clean = array();
612 + $seq = 0;
613 + foreach ($items as $key => $item) {
614 + if (!is_array($item) || empty($item['html']) || !is_string($item['html'])) {
615 + continue;
616 + }
617 + $id = isset($item['id']) ? sanitize_key($item['id']) : (is_string($key) ? sanitize_key($key) : '');
618 + if ($id === '') {
619 + continue;
620 + }
621 + $clean[$id] = array(
622 + 'html' => $item['html'],
623 + 'order' => isset($item['order']) ? (int) $item['order'] : 10,
624 + 'seq' => $seq++,
625 + );
626 + }
627 + if (empty($clean)) {
628 + return '';
629 + }
630 +
631 + uasort($clean, function ($a, $b) {
632 + if ($a['order'] === $b['order']) {
633 + return $a['seq'] <=> $b['seq'];
634 + }
635 + return $a['order'] <=> $b['order'];
636 + });
637 +
638 + $allowed = self::toolbar_items_allowed_html();
639 + $out = '';
640 + foreach ($clean as $id => $item) {
641 + $html = trim(wp_kses($item['html'], $allowed));
642 + if ($html === '') {
643 + continue;
644 + }
645 + $out .= ' <!-- toolbar item: ' . esc_html($id) . ' -->' . "\n";
646 + $out .= ' ' . $html . "\n";
647 + }
648 + return $out;
649 + }
650 +
651 + /**
652 + * Allow-list for toolbar item markup: everything wp_kses_post allows (button,
653 + * span, div, aria-* and data-* are in there) plus inline SVG icon shapes.
654 + */
655 + private static function toolbar_items_allowed_html() {
656 + $allowed = wp_kses_allowed_html('post');
657 + $shape = array(
658 + 'fill' => true,
659 + 'stroke' => true,
660 + 'stroke-width' => true,
661 + 'stroke-linecap' => true,
662 + 'stroke-linejoin' => true,
663 + 'transform' => true,
664 + 'style' => true,
665 + 'class' => true,
666 + );
667 + $allowed['svg'] = array_merge($shape, array(
668 + 'xmlns' => true,
669 + 'viewbox' => true,
670 + 'width' => true,
671 + 'height' => true,
672 + 'aria-hidden' => true,
673 + 'focusable' => true,
674 + 'role' => true,
675 + ));
676 + $allowed['path'] = array_merge($shape, array('d' => true));
677 + $allowed['circle'] = array_merge($shape, array('cx' => true, 'cy' => true, 'r' => true));
678 + $allowed['line'] = array_merge($shape, array('x1' => true, 'y1' => true, 'x2' => true, 'y2' => true));
679 + $allowed['polyline'] = array_merge($shape, array('points' => true));
680 + $allowed['polygon'] = array_merge($shape, array('points' => true));
681 + $allowed['rect'] = array_merge($shape, array('x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true));
682 + $allowed['g'] = $shape;
683 + if (!isset($allowed['button'])) {
684 + $allowed['button'] = array();
685 + }
686 + $allowed['button'] = array_merge($allowed['button'], array(
687 + 'type' => true,
688 + 'disabled' => true,
689 + 'aria-haspopup' => true,
690 + 'aria-pressed' => true,
691 + ));
692 + return apply_filters('mxchat_chat_toolbar_items_allowed_html', $allowed);
693 + }
694 +
695 + /**
478 696 * Get bot-specific options for multi-bot functionality
479 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
697 +* Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
480 698 */
481 699 private function get_bot_options($bot_id = 'default') {
482 700 // If default bot or multi-bot add-on not active, return empty (use default options)
483 701 if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
@@ -513,13 +731,27 @@
513 731 }
514 732
515 733
516 734 private function determine_email_collection_state() {
517 - // Logged-in users skip the email form
735 + // Logged-in users skip the email form, UNLESS the owner has turned on
736 + // "Also Show for Logged-In Users" (c0cfaf). On a membership or LMS site
737 + // every visitor is signed in, so the short-circuit meant lead capture —
738 + // and the consent checkbox — never appeared at all.
739 + //
740 + // Deliberately no PII in this decision: the returned profile values are
741 + // NOT rendered into the markup, because this page HTML is cacheable and
742 + // a cached form carrying one user's address would serve it to the next
743 + // visitor. Pre-fill travels on the nocache'd AJAX check instead
744 + // (mxchat_check_email_provided). Both logged-in and guest requests render
745 + // the same empty form, so the cached copy is correct for either.
518 746 if (is_user_logged_in()) {
747 + $options = get_option('mxchat_options', array());
748 + $show_for_logged_in = isset($options['lead_capture_logged_in_toggle'])
749 + && $options['lead_capture_logged_in_toggle'] === 'on';
750 +
519 751 $current_user = wp_get_current_user();
520 752 return [
521 - 'show_email_form' => false,
753 + 'show_email_form' => $show_for_logged_in,
522 754 'user_email' => $current_user->user_email,
523 755 'user_name' => $current_user->display_name ?: $current_user->first_name ?: ''
524 756 ];
525 757 }
@@ -538,14 +770,50 @@
538 770 /**
539 771 * NEW: Determine if and which chatbot should be displayed
540 772 */
541 773 private function get_display_bot() {
774 + // Delegates to the static so the enqueue-time gate (smart asset loading,
775 + // plan-915355) and this render-time decision share ONE code path and can
776 + // never disagree. The static reads mxchat_options fresh — same row this
777 + // instance loaded at construct.
778 + return self::compute_display_bot();
779 +}
780 +
781 +/**
782 + * Static single source of truth for the display decision (plan-915355).
783 + * Combines the per-page meta box, the append_to_body global toggle, and the
784 + * post-type include/exclude mode. Returns a bot id ('default' or specific)
785 + * when the auto-append widget will render on the current request, false when
786 + * it won't. Static (not a second instance) deliberately: MxChat_Public's
787 + * constructor registers a wp_footer action, so constructing a throwaway
788 + * instance would double-append the widget.
789 + */
790 +public static function compute_display_bot() {
791 + // The Elementor builder canvas is not a chat surface (plan 95dd1e part 2):
792 + // its preview iframe is a real front-end page load, so without this guard
793 + // the Auto-Display instance renders LIVE inside the canvas — front-end
794 + // chat scripts firing in the editor, able to start a session for the
795 + // person editing. Returning false here suppresses both the wp_footer
796 + // auto-append and the asset enqueue gate for preview requests only; the
797 + // placement widget's own canvas rendering is a static placeholder anyway.
798 + if (did_action('elementor/loaded')
799 + && class_exists('\Elementor\Plugin')
800 + && isset(\Elementor\Plugin::$instance->preview)
801 + && \Elementor\Plugin::$instance->preview
802 + && \Elementor\Plugin::$instance->preview->is_preview_mode()) {
803 + return false;
804 + }
805 +
806 + $options = get_option('mxchat_options', array());
807 + if (!is_array($options)) {
808 + $options = array();
809 + }
810 +
542 811 // Get page-specific settings from meta box
543 - $page_setting = $this->get_page_bot_setting();
812 + $page_setting = self::get_page_bot_setting();
544 813
545 - // Get global settings - FIXED: Check for 'on' instead of 'on'
546 - $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
547 - $global_default_bot = isset($this->options['default_bot']) ? $this->options['default_bot'] : 'default';
814 + $global_autoshow = isset($options['append_to_body']) && $options['append_to_body'] === 'on';
815 + $global_default_bot = isset($options['default_bot']) ? $options['default_bot'] : 'default';
548 816
549 817 // If page specifically hides chatbot, don't show anything
550 818 if ($page_setting && $page_setting['action'] === 'hide') {
551 819 return false;
@@ -559,9 +827,9 @@
559 827 // Page setting is 'global' or no page setting exists
560 828 // Check global auto-show setting
561 829 if ($global_autoshow) {
562 830 // Check post type visibility settings
563 - if (!$this->should_show_on_current_post_type()) {
831 + if (!self::should_show_on_current_post_type()) {
564 832 return false;
565 833 }
566 834
567 835 // Global auto-show is enabled, return the default bot
@@ -573,14 +841,86 @@
573 841 return false;
574 842 }
575 843
576 844 /**
845 + * Smart asset loading opt-in (plan-915355). Standalone option — deliberately
846 + * NOT a mxchat_options key, so it can never be stripped by mxchat_sanitize().
847 + * Default off: enqueue behavior is byte-identical to before until an owner
848 + * turns the toggle on.
849 + */
850 +public static function is_smart_asset_loading_enabled() {
851 + return get_option('mxchat_smart_asset_loading', 'off') === 'on';
852 +}
853 +
854 +/**
855 + * Will the chat widget render on the current request? (plan-915355)
856 + *
857 + * True when the auto-append decision resolves to a bot, OR the singular
858 + * post's content contains the [mxchat_chatbot] shortcode (first-chance
859 + * detection so shortcode pages keep head-loaded CSS — no FOUC). Computed
860 + * once per request and cached, so the wp_enqueue_scripts gate and any
861 + * add-on consulting this later in the same request always get one answer.
862 + *
863 + * Filter `mxchat_should_load_assets` is the force-load escape hatch for
864 + * headless/builder/custom-JS setups whose shortcode placement is invisible
865 + * to has_shortcode (builder-stored content, template files, widget areas).
866 + * Note the render-time safety net in render_chatbot_shortcode() still
867 + * force-loads assets whenever the shortcode actually renders — the filter
868 + * is only needed where even that net can't fire (e.g. markup assembled
869 + * outside WP rendering).
870 + */
871 +public static function should_load_assets() {
872 + static $cached = null;
873 + if ($cached !== null) {
874 + return $cached;
875 + }
876 +
877 + // Never gate admin/ajax requests — this decision is for front-end enqueues only.
878 + if (is_admin()) {
879 + $cached = true;
880 + return $cached;
881 + }
882 +
883 + $display_bot = self::compute_display_bot();
884 + $has_shortcode = false;
885 +
886 + if ($display_bot === false && is_singular()) {
887 + $post = get_post();
888 + if ($post && has_shortcode((string) $post->post_content, 'mxchat_chatbot')) {
889 + $has_shortcode = true;
890 + }
891 + // The mxchat/chatbot block (plan-95dd1e) stores a block comment, not
892 + // shortcode text, so has_shortcode can't see it — first-chance detect
893 + // it here for the same no-FOUC reason. The render-time safety net in
894 + // render_chatbot_shortcode() still covers template/widget placements.
895 + if ($post && !$has_shortcode && function_exists('has_block')
896 + && has_block('mxchat/chatbot', $post)) {
897 + $has_shortcode = true;
898 + }
899 + }
900 +
901 + $should = ($display_bot !== false) || $has_shortcode;
902 +
903 + $cached = (bool) apply_filters('mxchat_should_load_assets', $should, array(
904 + 'display_bot' => $display_bot,
905 + 'has_shortcode' => $has_shortcode,
906 + 'post_id' => get_the_ID(),
907 + ));
908 +
909 + return $cached;
910 +}
911 +
912 +/**
577 913 * Check if chatbot should be shown on the current post type
578 914 */
579 -private function should_show_on_current_post_type() {
915 +private static function should_show_on_current_post_type() {
916 + $options = get_option('mxchat_options', array());
917 + if (!is_array($options)) {
918 + $options = array();
919 + }
580 920 // Get visibility settings
581 - $mode = isset($this->options['post_type_visibility_mode']) ? $this->options['post_type_visibility_mode'] : 'all';
582 - $list = isset($this->options['post_type_visibility_list']) ? $this->options['post_type_visibility_list'] : array();
921 + $mode = isset($options['post_type_visibility_mode']) ? $options['post_type_visibility_mode'] : 'all';
922 + $list = isset($options['post_type_visibility_list']) ? $options['post_type_visibility_list'] : array();
583 923
584 924 // Ensure list is an array
585 925 if (!is_array($list)) {
586 926 $list = array();
@@ -591,9 +931,9 @@
591 931 return true;
592 932 }
593 933
594 934 // Get current post type
595 - $current_post_type = $this->get_current_post_type();
935 + $current_post_type = self::get_current_post_type();
596 936
597 937 // If we can't determine post type, default to showing
598 938 if (empty($current_post_type)) {
599 939 return true;
@@ -614,9 +954,9 @@
614 954
615 955 /**
616 956 * Get the current post type
617 957 */
618 -private function get_current_post_type() {
958 +private static function get_current_post_type() {
619 959 // Try to get from queried object first
620 960 $queried_object = get_queried_object();
621 961
622 962 if ($queried_object instanceof WP_Post) {
@@ -648,9 +988,9 @@
648 988
649 989 /**
650 990 * Get page-specific bot setting using new visibility field with backward compat
651 991 */
652 -private function get_page_bot_setting($post_id = null) {
992 +private static function get_page_bot_setting($post_id = null) {
653 993 if (!$post_id) {
654 994 $post_id = get_the_ID();
655 995 }
656 996
@@ -698,9 +1038,9 @@
698 1038 return $shortcode_bot_id;
699 1039 }
700 1040
701 1041 // No bot_id in shortcode, check page setting
702 - $page_setting = $this->get_page_bot_setting();
1042 + $page_setting = self::get_page_bot_setting();
703 1043 if ($page_setting && $page_setting['action'] === 'show') {
704 1044 return $page_setting['bot_id'];
705 1045 }
706 1046