| @@ -10,38 +10,92 @@ | ||
| 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 | + // Check if frontend debugger is enabled in settings | |
| 27 | + $options = get_option('mxchat_options', array()); | |
| 28 | + $show_debugger = isset($options['show_frontend_debugger']) ? $options['show_frontend_debugger'] : 'on'; | |
| 29 | + | |
| 30 | + // Only load if setting is enabled AND (user is admin or testing parameter is present) | |
| 31 | + if ($show_debugger === 'on' && (current_user_can('administrator') || isset($_GET['mxchat_test']))) { | |
| 32 | + | |
| 33 | + // Get plugin URL for assets | |
| 34 | + $plugin_url = plugin_dir_url(dirname(__FILE__)); | |
| 35 | + | |
| 36 | + // Enqueue test panel CSS | |
| 37 | + wp_enqueue_style( | |
| 38 | + 'mxchat-test-panel', | |
| 39 | + $plugin_url . 'css/test-panel.css', | |
| 40 | + array(), | |
| 41 | + '2.5.2' | |
| 42 | + ); | |
| 43 | + | |
| 44 | + // Enqueue test panel JS | |
| 45 | + wp_enqueue_script( | |
| 46 | + 'mxchat-test-panel', | |
| 47 | + $plugin_url . 'js/test-panel.js', | |
| 48 | + array('jquery'), | |
| 49 | + '2.5.2', | |
| 50 | + true | |
| 51 | + ); | |
| 52 | + | |
| 53 | + // Pass data to JavaScript | |
| 54 | + wp_localize_script('mxchat-test-panel', 'mxchatTestData', array( | |
| 55 | + 'ajaxUrl' => admin_url('admin-ajax.php'), | |
| 56 | + 'nonce' => wp_create_nonce('mxchat_test_nonce'), | |
| 57 | + 'isAdmin' => current_user_can('administrator'), | |
| 58 | + 'testingEnabled' => true | |
| 59 | + )); | |
| 60 | + | |
| 61 | + // Add JavaScript flag to enable testing | |
| 62 | + add_action('wp_footer', array($this, 'add_testing_flag')); | |
| 63 | + } | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Add JavaScript flag to enable testing panel | |
| 68 | + */ | |
| 69 | + public function add_testing_flag() { | |
| 70 | + echo '<script>window.mxchatTestingEnabled = true;</script>'; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Check if testing mode should be enabled | |
| 75 | + */ | |
| 76 | + private function is_testing_mode_enabled() { | |
| 77 | + return (current_user_can('administrator') || isset($_GET['mxchat_test'])); | |
| 78 | + } | |
| 79 | + | |
| 16 | 80 | /** |
| 17 | 81 | * UPDATED: Enhanced should_hide_chatbot method - only blocks auto-append, not shortcodes |
| 18 | 82 | */ |
| 19 | 83 | private function should_hide_chatbot($context = 'auto') { |
| 20 | 84 | global $post; |
| 21 | - | |
| 85 | + | |
| 22 | 86 | if (!$post) { |
| 23 | 87 | return false; |
| 24 | 88 | } |
| 25 | - | |
| 89 | + | |
| 90 | + $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true); | |
| 91 | + | |
| 26 | 92 | // Only hide if it's the auto-append context (floating="yes" from global setting) |
| 27 | 93 | // 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 | - } | |
| 94 | + if ($context === 'auto' && $hide_chatbot === '1') { | |
| 95 | + return true; | |
| 42 | 96 | } |
| 43 | - | |
| 97 | + | |
| 44 | 98 | return false; |
| 45 | 99 | } |
| 46 | 100 | |
| 47 | 101 | /** |
| @@ -170,15 +224,15 @@ | ||
| 170 | 224 | $icon_color = $current_options['icon_color'] ?? '#fff'; |
| 171 | 225 | $chat_input_font_color = $current_options['chat_input_font_color'] ?? '#212121'; |
| 172 | 226 | $close_button_color = $current_options['close_button_color'] ?? '#fff'; |
| 173 | 227 | $chatbot_bg_color = $current_options['chatbot_bg_color'] ?? '#fff'; |
| 174 | - $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_textarea_field(trim($current_options['pre_chat_message'])) : ''; | |
| 228 | + $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_text_field(trim($current_options['pre_chat_message'])) : ''; | |
| 175 | 229 | $user_id = sanitize_key($this->mxchat_get_user_identifier()); |
| 176 | 230 | $email_state = $this->determine_email_collection_state(); |
| 177 | 231 | $show_email_form = $email_state['show_email_form']; |
| 178 | 232 | $user_email = $email_state['user_email'] ?? ''; |
| 179 | 233 | $user_name = $email_state['user_name'] ?? ''; |
| 180 | - // Pre-chat dismissal now handled client-side via localStorage (zero server load) | |
| 234 | + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id; | |
| 181 | 235 | $input_copy = isset($current_options['input_copy']) ? esc_attr($current_options['input_copy']) : esc_attr__('How can I assist?', 'mxchat'); |
| 182 | 236 | $rate_limit_message = isset($current_options['rate_limit_message']) ? esc_attr($current_options['rate_limit_message']) : esc_attr__('Rate limit exceeded. Please try again later.', 'mxchat'); |
| 183 | 237 | $mode_indicator_bg_color = $current_options['mode_indicator_bg_color'] ?? '#212121'; |
| 184 | 238 | $mode_indicator_font_color = $current_options['mode_indicator_font_color'] ?? '#fff'; |
| @@ -232,8 +286,9 @@ | ||
| 232 | 286 | } |
| 233 | 287 | echo ' </div>'; |
| 234 | 288 | echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Minimize', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>'; |
| 235 | 289 | echo ' <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" id="ic-minimize"' . ($skip_inline_colors ? '' : ' style="fill: ' . esc_attr($close_button_color) . ';"') . '>'; |
| 290 | + echo ' <path d="M0 0h24v24H0z" fill="none"></path>'; | |
| 236 | 291 | echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>'; |
| 237 | 292 | echo ' </svg>'; |
| 238 | 293 | echo ' <span>' . esc_html__('Minimize', 'mxchat') . '</span>'; |
| 239 | 294 | echo ' </button>'; |
| @@ -271,14 +326,8 @@ | ||
| 271 | 326 | echo ' </form>'; |
| 272 | 327 | echo '</div>'; |
| 273 | 328 | } |
| 274 | 329 | |
| 275 | - echo ' <div id="mxchat-init-loader-' . esc_attr($bot_id) . '" class="mxchat-init-loader" style="display:none;">'; | |
| 276 | - echo ' <div class="mxchat-init-loader-dots">'; | |
| 277 | - echo ' <span class="dot"></span><span class="dot"></span><span class="dot"></span>'; | |
| 278 | - echo ' </div>'; | |
| 279 | - echo ' </div>'; | |
| 280 | - | |
| 281 | 330 | echo ' <div id="chat-container-' . esc_attr($bot_id) . '" class="chat-container" style="' . ($enable_email_block && $show_email_form ? 'display: none;' : '') . '">'; |
| 282 | 331 | echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box">'; |
| 283 | 332 | echo ' <div class="bot-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>'; |
| 284 | 333 | echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>'; |
| @@ -425,12 +474,11 @@ | ||
| 425 | 474 | |
| 426 | 475 | if ($is_floating) { |
| 427 | 476 | echo '</div>'; |
| 428 | 477 | |
| 429 | - if (!empty($pre_chat_message)) { | |
| 430 | - // Rendered hidden by default — JS checkPreChatDismissal() handles show/hide via localStorage | |
| 431 | - echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message" style="display:none;">'; | |
| 432 | - echo nl2br(esc_html($pre_chat_message)); | |
| 478 | + if (!empty($pre_chat_message) && !get_transient($transient_key)) { | |
| 479 | + echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message">'; | |
| 480 | + echo esc_html($pre_chat_message); | |
| 433 | 481 | echo '<button class="close-pre-chat-message" aria-label="' . esc_attr__('Close', 'mxchat') . '">×</button>'; |
| 434 | 482 | echo '</div>'; |
| 435 | 483 | } |
| 436 | 484 | |
| @@ -494,9 +542,12 @@ | ||
| 494 | 542 | } |
| 495 | 543 | |
| 496 | 544 | |
| 497 | 545 | private function determine_email_collection_state() { |
| 498 | - // Logged-in users skip the email form | |
| 546 | + // Get session ID | |
| 547 | + $session_id = 'mxchat_chat_' . wp_generate_uuid4(); // You'll need to use your actual session generation logic | |
| 548 | + | |
| 549 | + // Check if user is logged in first | |
| 499 | 550 | if (is_user_logged_in()) { |
| 500 | 551 | $current_user = wp_get_current_user(); |
| 501 | 552 | return [ |
| 502 | 553 | 'show_email_form' => false, |
| @@ -503,16 +554,26 @@ | ||
| 503 | 554 | 'user_email' => $current_user->user_email, |
| 504 | 555 | 'user_name' => $current_user->display_name ?: $current_user->first_name ?: '' |
| 505 | 556 | ]; |
| 506 | 557 | } |
| 507 | - | |
| 508 | - // For guests, default to showing the email form. | |
| 509 | - // The session-based check happens client-side via AJAX since the | |
| 510 | - // session ID lives in the browser cookie/localStorage. | |
| 558 | + | |
| 559 | + // Check stored email for session | |
| 560 | + $stored_email = get_option("mxchat_email_{$session_id}", ''); | |
| 561 | + $stored_name = get_option("mxchat_name_{$session_id}", ''); | |
| 562 | + | |
| 563 | + // Check if name is required | |
| 564 | + $name_field_enabled = isset($this->options['enable_name_field']) && | |
| 565 | + ($this->options['enable_name_field'] === '1' || $this->options['enable_name_field'] === 'on'); | |
| 566 | + | |
| 567 | + $has_required_info = !empty($stored_email); | |
| 568 | + if ($name_field_enabled) { | |
| 569 | + $has_required_info = $has_required_info && !empty($stored_name); | |
| 570 | + } | |
| 571 | + | |
| 511 | 572 | return [ |
| 512 | - 'show_email_form' => true, | |
| 513 | - 'user_email' => '', | |
| 514 | - 'user_name' => '' | |
| 573 | + 'show_email_form' => !$has_required_info, | |
| 574 | + 'user_email' => $stored_email, | |
| 575 | + 'user_name' => $stored_name | |
| 515 | 576 | ]; |
| 516 | 577 | } |
| 517 | 578 | |
| 518 | 579 | |
| @@ -627,46 +688,31 @@ | ||
| 627 | 688 | return ''; |
| 628 | 689 | } |
| 629 | 690 | |
| 630 | 691 | /** |
| 631 | - * Get page-specific bot setting using new visibility field with backward compat | |
| 692 | + * NEW: Get page-specific bot setting | |
| 632 | 693 | */ |
| 633 | 694 | private function get_page_bot_setting($post_id = null) { |
| 634 | 695 | if (!$post_id) { |
| 635 | 696 | $post_id = get_the_ID(); |
| 636 | 697 | } |
| 637 | - | |
| 698 | + | |
| 638 | 699 | if (!$post_id) { |
| 639 | 700 | return null; |
| 640 | 701 | } |
| 641 | - | |
| 642 | - // Check new visibility field first | |
| 643 | - $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true); | |
| 644 | - | |
| 645 | - if ($visibility === 'hide') { | |
| 702 | + | |
| 703 | + // Check if chatbot is hidden on this page | |
| 704 | + $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true); | |
| 705 | + if ($hide_chatbot === '1') { | |
| 646 | 706 | return array('action' => 'hide'); |
| 647 | 707 | } |
| 648 | - | |
| 649 | - if ($visibility === 'show') { | |
| 650 | - $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true); | |
| 651 | - $bot_id = !empty($selected_bot) ? $selected_bot : 'default'; | |
| 652 | - return array('action' => 'show', 'bot_id' => $bot_id); | |
| 653 | - } | |
| 654 | - | |
| 655 | - // Backward compat: check legacy hide checkbox if no new field set | |
| 656 | - if (empty($visibility)) { | |
| 657 | - $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true); | |
| 658 | - if ($hide_chatbot === '1') { | |
| 659 | - return array('action' => 'hide'); | |
| 660 | - } | |
| 661 | - } | |
| 662 | - | |
| 663 | - // Check if specific bot is selected (legacy path) | |
| 708 | + | |
| 709 | + // Check if specific bot is selected | |
| 664 | 710 | $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true); |
| 665 | 711 | if (!empty($selected_bot)) { |
| 666 | 712 | return array('action' => 'show', 'bot_id' => $selected_bot); |
| 667 | 713 | } |
| 668 | - | |
| 714 | + | |
| 669 | 715 | // Use global setting |
| 670 | 716 | return array('action' => 'global'); |
| 671 | 717 | } |
| 672 | 718 | |
| @@ -698,8 +744,27 @@ | ||
| 698 | 744 | |
| 699 | 745 | /** |
| 700 | 746 | * UPDATED: Debug function with new context info |
| 701 | 747 | */ |
| 748 | +public function debug_display_logic() { | |
| 749 | + if (current_user_can('manage_options') && isset($_GET['mxchat_debug'])) { | |
| 750 | + $bot_to_show = $this->get_display_bot(); | |
| 751 | + $page_setting = $this->get_page_bot_setting(); | |
| 752 | + $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on'; | |
| 753 | + $hide_auto = $this->should_hide_chatbot('auto'); | |
| 754 | + | |
| 755 | + echo '<div style="position: fixed; top: 50px; right: 20px; background: white; border: 2px solid red; padding: 10px; z-index: 9999; max-width: 300px; font-size: 12px;">'; | |
| 756 | + echo '<h4 style="margin: 0 0 10px 0;">MxChat Debug Info</h4>'; | |
| 757 | + echo '<p><strong>Raw append_to_body value:</strong> "' . ($this->options['append_to_body'] ?? 'NOT SET') . '"</p>'; | |
| 758 | + echo '<p><strong>Page Setting:</strong><br><pre>' . print_r($page_setting, true) . '</pre></p>'; | |
| 759 | + echo '<p><strong>Global Auto-show:</strong> ' . ($global_autoshow ? 'ON' : 'OFF') . '</p>'; | |
| 760 | + echo '<p><strong>Hide Auto-Append:</strong> ' . ($hide_auto ? 'YES' : 'NO') . '</p>'; | |
| 761 | + echo '<p><strong>Bot to Show:</strong> ' . ($bot_to_show === false ? 'NONE' : $bot_to_show) . '</p>'; | |
| 762 | + echo '<p><strong>Shortcodes:</strong> floating="no" always works</p>'; | |
| 763 | + echo '<p><small>Add ?mxchat_debug=1 to URL to see this</small></p>'; | |
| 764 | + echo '</div>'; | |
| 765 | + } | |
| 766 | +} | |
| 702 | 767 | |
| 703 | 768 | /** |
| 704 | 769 | * NEW: Helper method to get available bots (for admin notices, etc.) |
| 705 | 770 | */ |