PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.24
MxChat – AI Chatbot & Content Generation for WordPress v3.2.24
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 +254 -25 3.2.19 → 3.2.24 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
@@ -119,20 +134,16 @@
119 134
120 135 // Determine which bot to use
121 136 $bot_id = $this->determine_bot_for_shortcode($attributes['bot_id']);
122 137
123 - // UPDATED: Only check hiding for floating shortcodes that could conflict with auto-append
124 - // 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).
125 142 if ($attributes['floating'] === 'yes') {
126 - // For floating shortcodes, check if auto-append is hidden
127 - // This prevents duplicate floating chatbots
128 - if ($this->should_hide_chatbot('auto') && $this->is_auto_append_enabled()) {
129 - // If auto-append is enabled but hidden on this page,
130 - // allow the floating shortcode to work (user is overriding)
131 - // But if auto-append is disabled globally, also allow shortcode
132 - }
143 + self::$floating_instance_rendered = true;
133 144 }
134 -
145 +
135 146 // Non-floating shortcodes (floating="no") should NEVER be blocked by the hide setting
136 147 // This allows embedded chatbots even when floating is hidden
137 148
138 149 $is_floating = $attributes['floating'] === 'yes';
@@ -215,14 +226,49 @@
215 226 $enable_email_block = isset($current_options['enable_email_block']) &&
216 227 ($current_options['enable_email_block'] === '1' || $current_options['enable_email_block'] === 'on');
217 228
218 229 // Add name field variables
219 - $enable_name_field = isset($current_options['enable_name_field']) &&
230 + $enable_name_field = isset($current_options['enable_name_field']) &&
220 231 ($current_options['enable_name_field'] === '1' || $current_options['enable_name_field'] === 'on');
221 - $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
222 - 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']) :
223 234 esc_attr__('Enter your name', 'mxchat');
224 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 +
225 271 ob_start();
226 272
227 273 // Check if floating attribute is set to 'yes' and wrap accordingly
228 274 if ($is_floating) {
@@ -285,13 +331,29 @@
285 331
286 332 // Add name field if enabled
287 333 if ($enable_name_field) {
288 334 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 . '" />';
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 . '" />';
290 336 }
291 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.
292 342 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') . '" />';
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 +
294 356 echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">';
295 357 $button_text = isset($current_options['email_blocker_button_text'])
296 358 ? $current_options['email_blocker_button_text']
297 359 : esc_html__('Start Chat', 'mxchat');
@@ -307,10 +369,22 @@
307 369 echo ' </div>';
308 370 echo ' </div>';
309 371
310 372 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) . ';"') . '>';
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) . ';"') . '>';
313 387 echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>';
314 388 echo wp_kses_post($intro_message);
315 389 echo ' </div>';
316 390 echo ' </div>';
@@ -360,8 +434,12 @@
360 434 // Max input length (plan a3fae2 part C) — global core setting, 0 = unlimited.
361 435 // Hard-caps typing/paste client-side; the chat handler enforces it server-side too.
362 436 $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
363 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>';
364 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>';
365 443 // Language-neutral character counter (plan 7091a2). Numbers only — no
366 444 // translatable strings — so it reads correctly on every-language install.
367 445 // Only rendered when a cap is set; hidden until ~80% of the cap, then
@@ -447,14 +525,22 @@
447 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;"/>';
448 526 echo ' <circle cx="12" cy="13" r="3" style="fill: none !important;"/>';
449 527 echo ' </svg>';
450 528 echo ' </button>';
451 -
452 -
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 +
453 539 echo ' </div>';
454 -
540 +
455 541 echo ' <div class="chatbot-footer">';
456 -
542 +
457 543 // Output the privacy notice if enabled
458 544 if ($privacy_toggle && !empty($privacy_text)) {
459 545 echo '<p class="privacy-notice">' . $privacy_text . '</p>';
460 546 }
@@ -500,10 +586,116 @@
500 586 return ob_get_clean();
501 587 }
502 588
503 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 + /**
504 696 * Get bot-specific options for multi-bot functionality
505 - * 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
506 698 */
507 699 private function get_bot_options($bot_id = 'default') {
508 700 // If default bot or multi-bot add-on not active, return empty (use default options)
509 701 if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
@@ -539,13 +731,27 @@
539 731 }
540 732
541 733
542 734 private function determine_email_collection_state() {
543 - // 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.
544 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 +
545 751 $current_user = wp_get_current_user();
546 752 return [
547 - 'show_email_form' => false,
753 + 'show_email_form' => $show_for_logged_in,
548 754 'user_email' => $current_user->user_email,
549 755 'user_name' => $current_user->display_name ?: $current_user->first_name ?: ''
550 756 ];
551 757 }
@@ -581,8 +787,23 @@
581 787 * constructor registers a wp_footer action, so constructing a throwaway
582 788 * instance would double-append the widget.
583 789 */
584 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 +
585 806 $options = get_option('mxchat_options', array());
586 807 if (!is_array($options)) {
587 808 $options = array();
588 809 }
@@ -664,8 +885,16 @@
664 885
665 886 if ($display_bot === false && is_singular()) {
666 887 $post = get_post();
667 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)) {
668 897 $has_shortcode = true;
669 898 }
670 899 }
671 900