PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.1.7
MxChat – AI Chatbot & Content Generation for WordPress v3.1.7
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
mxchat-basic / includes / class-mxchat-public.php

class-mxchat-public.php in MxChat – AI Chatbot & Content Generation for WordPress 3.1.7, at includes/class-mxchat-public.php

803 lines 43.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) {
3 exit; // Exit if accessed directly
4 }
5
6 class MxChat_Public {
7 private $options;
8
9 public function __construct() {
10 // Simply get the options without defining duplicated defaults
11 $this->options = get_option('mxchat_options', array());
12 add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode'));
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'));
20 }
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
80 /**
81 * UPDATED: Enhanced should_hide_chatbot method - only blocks auto-append, not shortcodes
82 */
83 private function should_hide_chatbot($context = 'auto') {
84 global $post;
85
86 if (!$post) {
87 return false;
88 }
89
90 // Only hide if it's the auto-append context (floating="yes" from global setting)
91 // Allow shortcodes to work regardless of this setting
92 if ($context === 'auto') {
93 // Check new visibility field first
94 $visibility = get_post_meta($post->ID, '_mxchat_page_visibility', true);
95 if ($visibility === 'hide') {
96 return true;
97 }
98
99 // Backward compat: check legacy field if new field not set
100 if (empty($visibility)) {
101 $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true);
102 if ($hide_chatbot === '1') {
103 return true;
104 }
105 }
106 }
107
108 return false;
109 }
110
111 /**
112 * UPDATED: Enhanced append_chatbot_to_body method with context
113 */
114 public function append_chatbot_to_body() {
115 // Only run on public pages
116 if (is_admin() || wp_doing_ajax()) {
117 return;
118 }
119
120 // Check if auto-append chatbot should be hidden on this page
121 if ($this->should_hide_chatbot('auto')) {
122 return; // Don't show auto-appended chatbot
123 }
124
125 // Get the bot that should be displayed using new logic
126 $bot_to_show = $this->get_display_bot();
127
128 // Don't show chatbot if determination is false
129 if ($bot_to_show === false) {
130 return;
131 }
132
133 // Handle consent for display
134 $consent_category = 'marketing';
135 $has_consent = true;
136
137 if (
138 isset($this->options['complianz_toggle']) &&
139 $this->options['complianz_toggle'] === 'on' &&
140 function_exists('cmplz_has_consent')
141 ) {
142 $has_consent = cmplz_has_consent($consent_category);
143 }
144
145 // Display the appropriate chatbot (always floating for auto-append)
146 if ($bot_to_show && $bot_to_show !== 'default') {
147 // Show specific bot
148 echo do_shortcode('[mxchat_chatbot floating="yes" bot_id="' . esc_attr($bot_to_show) . '" has_consent="' . ($has_consent ? 'yes' : 'no') . '"]');
149 } else {
150 // Show default bot
151 echo do_shortcode('[mxchat_chatbot floating="yes" has_consent="' . ($has_consent ? 'yes' : 'no') . '"]');
152 }
153 }
154
155 private function mxchat_get_user_identifier() {
156 return sanitize_text_field($_SERVER['REMOTE_ADDR']);
157 }
158
159 /**
160 * UPDATED: Enhanced shortcode with context-aware hiding
161 */
162 public function render_chatbot_shortcode($atts) {
163 // UPDATED: Add bot_id parameter support and improve logic
164 $attributes = shortcode_atts(array(
165 'floating' => 'yes',
166 'has_consent' => 'yes',
167 'bot_id' => '' // Support for multi-bot functionality - empty means auto-detect
168 ), $atts);
169
170 // Determine which bot to use
171 $bot_id = $this->determine_bot_for_shortcode($attributes['bot_id']);
172
173 // UPDATED: Only check hiding for floating shortcodes that could conflict with auto-append
174 // Non-floating shortcodes should always work
175 if ($attributes['floating'] === 'yes') {
176 // For floating shortcodes, check if auto-append is hidden
177 // This prevents duplicate floating chatbots
178 if ($this->should_hide_chatbot('auto') && $this->is_auto_append_enabled()) {
179 // If auto-append is enabled but hidden on this page,
180 // allow the floating shortcode to work (user is overriding)
181 // But if auto-append is disabled globally, also allow shortcode
182 }
183 }
184
185 // Non-floating shortcodes (floating="no") should NEVER be blocked by the hide setting
186 // This allows embedded chatbots even when floating is hidden
187
188 $is_floating = $attributes['floating'] === 'yes';
189 $bot_id = sanitize_key($bot_id); // Sanitize bot ID
190
191 // Rest of your existing shortcode rendering logic continues unchanged...
192 // [All the existing HTML generation code remains the same]
193
194 // Get bot-specific options if multi-bot add-on is active
195 $bot_options = $this->get_bot_options($bot_id);
196
197 // Use bot-specific options or fall back to default options
198 $current_options = !empty($bot_options) ? $bot_options : $this->options;
199
200 // [Rest of your existing rendering code stays exactly the same]
201 // Just remove the old should_hide_chatbot() check from the beginning
202
203 // Check for Complianz consent if the toggle is enabled
204 $initial_visibility = 'hidden';
205 $additional_class = '';
206
207 if (isset($current_options['complianz_toggle']) && $current_options['complianz_toggle'] === 'on') {
208 $additional_class = ' no-consent';
209 }
210 $visibility_class = $initial_visibility . $additional_class;
211
212 $theme_options = get_option('mxchat_theme_options', array());
213 $custom_send_image = isset($theme_options['custom_send_button_image']) ? esc_url($theme_options['custom_send_button_image']) : '';
214 $send_width = isset($theme_options['send_button_width']) ? intval($theme_options['send_button_width']) : 24;
215 $send_height = isset($theme_options['send_button_height']) ? intval($theme_options['send_button_height']) : 24;
216 $send_rotation = isset($theme_options['send_button_rotation']) ? intval($theme_options['send_button_rotation']) : 0;
217
218 // Check if an AI theme is active (global or bot-specific) - if so, skip inline color styles
219 $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
220 $bot_has_theme = isset($theme_options['bot_theme_assignments'][$bot_id]);
221 $skip_inline_colors = $ai_theme_active || $bot_has_theme;
222
223 // UPDATED: Use current_options instead of $this->options throughout
224 $bg_color = $current_options['chatbot_background_color'] ?? '#fff';
225 $user_message_bg_color = $current_options['user_message_bg_color'] ?? '#fff';
226 $user_message_font_color = $current_options['user_message_font_color'] ?? '#212121';
227 $bot_message_bg_color = $current_options['bot_message_bg_color'] ?? '#212121';
228 $bot_message_font_color = $current_options['bot_message_font_color'] ?? '#fff';
229 $top_bar_bg_color = $current_options['top_bar_bg_color'] ?? '#212121';
230 $send_button_font_color = $current_options['send_button_font_color'] ?? '#212121';
231 $intro_message = $current_options['intro_message'] ?? esc_html__('Hello! How can I assist you today?', 'mxchat');
232 $top_bar_title = $current_options['top_bar_title'] ?? esc_html__('MxChat: Basic', 'mxchat');
233 $chatbot_background_color = $current_options['chatbot_background_color'] ?? '#212121';
234 $icon_color = $current_options['icon_color'] ?? '#fff';
235 $chat_input_font_color = $current_options['chat_input_font_color'] ?? '#212121';
236 $close_button_color = $current_options['close_button_color'] ?? '#fff';
237 $chatbot_bg_color = $current_options['chatbot_bg_color'] ?? '#fff';
238 $pre_chat_message = isset($current_options['pre_chat_message']) ? sanitize_textarea_field(trim($current_options['pre_chat_message'])) : '';
239 $user_id = sanitize_key($this->mxchat_get_user_identifier());
240 $email_state = $this->determine_email_collection_state();
241 $show_email_form = $email_state['show_email_form'];
242 $user_email = $email_state['user_email'] ?? '';
243 $user_name = $email_state['user_name'] ?? '';
244 // Pre-chat dismissal now handled client-side via localStorage (zero server load)
245 $input_copy = isset($current_options['input_copy']) ? esc_attr($current_options['input_copy']) : esc_attr__('How can I assist?', 'mxchat');
246 $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');
247 $mode_indicator_bg_color = $current_options['mode_indicator_bg_color'] ?? '#212121';
248 $mode_indicator_font_color = $current_options['mode_indicator_font_color'] ?? '#fff';
249 $quick_questions_toggle_color = $current_options['quick_questions_toggle_color'] ?? '#212121';
250
251 $privacy_toggle = isset($current_options['privacy_toggle']) && $current_options['privacy_toggle'] === 'on';
252 $privacy_text = isset($current_options['privacy_text']) ? wp_kses_post($current_options['privacy_text']) : wp_kses_post(__('By chatting, you agree to our <a href="https://example.com/privacy-policy" target="_blank">privacy policy</a>.', 'mxchat'));
253
254 $popular_question_1 = isset($current_options['popular_question_1']) ? esc_html($current_options['popular_question_1']) : '';
255 $popular_question_2 = isset($current_options['popular_question_2']) ? esc_html($current_options['popular_question_2']) : '';
256 $popular_question_3 = isset($current_options['popular_question_3']) ? esc_html($current_options['popular_question_3']) : '';
257 $additional_questions = isset($current_options['additional_popular_questions']) ? $current_options['additional_popular_questions'] : [];
258 $custom_icon = isset($current_options['custom_icon']) ? esc_url($current_options['custom_icon']) : '';
259 $title_icon = isset($current_options['title_icon']) ? esc_url($current_options['title_icon']) : '';
260 // AI agent text - if explicitly set to empty string, hide the indicator entirely
261 $ai_agent_text = isset($current_options['ai_agent_text']) ? $current_options['ai_agent_text'] : __('AI Agent', 'mxchat');
262
263 $live_agent_message_bg_color = $current_options['live_agent_message_bg_color'] ?? '#212121';
264 $live_agent_message_font_color = $current_options['live_agent_message_font_color'] ?? '#fff';
265 $enable_email_block = isset($current_options['enable_email_block']) &&
266 ($current_options['enable_email_block'] === '1' || $current_options['enable_email_block'] === 'on');
267
268 // Add name field variables
269 $enable_name_field = isset($current_options['enable_name_field']) &&
270 ($current_options['enable_name_field'] === '1' || $current_options['enable_name_field'] === 'on');
271 $name_field_placeholder = isset($current_options['name_field_placeholder']) ?
272 esc_attr($current_options['name_field_placeholder']) :
273 esc_attr__('Enter your name', 'mxchat');
274
275 ob_start();
276
277 // Check if floating attribute is set to 'yes' and wrap accordingly
278 if ($is_floating) {
279 echo '<div id="floating-chatbot-' . esc_attr($bot_id) . '" class="floating-chatbot ' . $initial_visibility . $additional_class . '">';
280 }
281
282 // Add bot_id to the chatbot wrapper as a data attribute
283 echo '<div id="mxchat-chatbot-wrapper-' . esc_attr($bot_id) . '" class="mxchat-chatbot-wrapper" data-bot-id="' . esc_attr($bot_id) . '">';
284
285 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) . ';"') . '>';
286 echo ' <div class="chatbot-title-container">';
287 echo ' <div class="chatbot-title-group">';
288 if (!empty($title_icon)) {
289 echo ' <img src="' . esc_url($title_icon) . '" alt="" class="chatbot-title-icon">';
290 }
291 echo ' <p class="chatbot-title"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>' . esc_html($top_bar_title) . '</p>';
292 echo ' </div>';
293 // Only show mode indicator if ai_agent_text is not empty
294 if (!empty(trim($ai_agent_text))) {
295 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>';
296 }
297 echo ' </div>';
298 echo ' <button class="exit-chat" type="button" aria-label="' . esc_attr__('Minimize', 'mxchat') . '"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($close_button_color) . ';"') . '>';
299 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) . ';"') . '>';
300 echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>';
301 echo ' </svg>';
302 echo ' <span>' . esc_html__('Minimize', 'mxchat') . '</span>';
303 echo ' </button>';
304 echo ' </div>';
305
306 // 3b) Main chatbot container
307 echo ' <div id="mxchat-chatbot-' . esc_attr($bot_id) . '" class="mxchat-chatbot"' . ($skip_inline_colors ? '' : ' style="background-color: ' . esc_attr($chatbot_bg_color) . ';"') . '>';
308
309 if ($enable_email_block) {
310 echo '<div id="email-blocker-' . esc_attr($bot_id) . '" class="email-blocker" style="' . ($show_email_form ? '' : 'display: none;') . '">';
311 echo ' <div class="email-blocker-header">';
312
313 $header_html = isset($current_options['email_blocker_header_content'])
314 ? $current_options['email_blocker_header_content']
315 : '';
316 echo wp_kses_post($header_html);
317
318 echo ' </div>';
319 echo ' <form id="email-collection-form-' . esc_attr($bot_id) . '" class="email-collection-form" method="POST" action="">';
320
321 // Add name field if enabled
322 if ($enable_name_field) {
323 echo ' <label for="user-name-' . esc_attr($bot_id) . '" class="sr-only mxchat-name-label">' . esc_html__('Name', 'mxchat') . '</label>';
324 echo ' <input type="text" id="user-name-' . esc_attr($bot_id) . '" name="user_name" class="mxchat-name-input" required placeholder="' . $name_field_placeholder . '" />';
325 }
326
327 echo ' <label for="user-email-' . esc_attr($bot_id) . '" class="sr-only">' . esc_html__('Email Address', 'mxchat') . '</label>';
328 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') . '" />';
329 echo '<button type="submit" id="email-submit-button-' . esc_attr($bot_id) . '" class="email-submit-button">';
330 $button_text = isset($current_options['email_blocker_button_text'])
331 ? $current_options['email_blocker_button_text']
332 : esc_html__('Start Chat', 'mxchat');
333 echo esc_html($button_text);
334 echo '</button>';
335 echo ' </form>';
336 echo '</div>';
337 }
338
339 echo ' <div id="chat-container-' . esc_attr($bot_id) . '" class="chat-container" style="' . ($enable_email_block && $show_email_form ? 'display: none;' : '') . '">';
340 echo ' <div id="chat-box-' . esc_attr($bot_id) . '" class="chat-box">';
341 echo ' <div class="bot-message"' . ($skip_inline_colors ? '' : ' style="background: ' . esc_attr($bot_message_bg_color) . ';"') . '>';
342 echo ' <div dir="auto"' . ($skip_inline_colors ? '' : ' style="color: ' . esc_attr($bot_message_font_color) . ';"') . '>';
343 echo wp_kses_post($intro_message);
344 echo ' </div>';
345 echo ' </div>';
346 echo ' </div>'; // end #chat-box
347
348
349 // Replace the existing popular questions section with this:
350 echo ' <div id="mxchat-popular-questions-' . esc_attr($bot_id) . '" class="mxchat-popular-questions">';
351 echo ' <div class="mxchat-popular-questions-container">';
352
353 // Collapse button (down arrow) - shows when open, centered at top
354 echo ' <button class="questions-collapse-btn" aria-label="' . esc_attr__('Hide Quick Questions', 'mxchat') . '">';
355 echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none"' . ($skip_inline_colors ? '' : ' stroke="' . esc_attr($quick_questions_toggle_color) . '"') . ' stroke-width="2">';
356 echo ' <polyline points="6,9 12,15 18,9"></polyline>';
357 echo ' </svg>';
358 echo ' </button>';
359
360 // Expand button (up arrow) - shows when collapsed
361 echo ' <button class="questions-toggle-btn" aria-label="' . esc_attr__('Show Quick Questions', 'mxchat') . '">';
362 echo ' <svg width="25" height="25" viewBox="0 0 24 24" fill="none"' . ($skip_inline_colors ? '' : ' stroke="' . esc_attr($quick_questions_toggle_color) . '"') . ' stroke-width="2">';
363 echo ' <polyline points="18,15 12,9 6,15"></polyline>';
364 echo ' </svg>';
365 echo ' </button>';
366
367 if (!empty($popular_question_1)) {
368 echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($popular_question_1) . '</button>';
369 }
370 if (!empty($popular_question_2)) {
371 echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($popular_question_2) . '</button>';
372 }
373 if (!empty($popular_question_3)) {
374 echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($popular_question_3) . '</button>';
375 }
376
377 if (!empty($additional_questions) && is_array($additional_questions)) {
378 foreach ($additional_questions as $index => $question) {
379 if (!empty($question)) {
380 echo '<button class="mxchat-popular-question" dir="auto">' . esc_html($question) . '</button>';
381 }
382 }
383 }
384
385 echo ' </div>';
386 echo ' </div>';
387
388 echo ' <div id="input-container-' . esc_attr($bot_id) . '" class="input-container">';
389 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>';
390 echo ' <button id="send-button-' . esc_attr($bot_id) . '" class="send-button" aria-label="' . esc_attr__('Send message', 'mxchat') . '">';
391 if (!empty($custom_send_image)) {
392 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);" />';
393 } else {
394 $send_svg_style = 'width: ' . intval($send_width) . 'px; height: ' . intval($send_height) . 'px; transform: rotate(' . intval($send_rotation) . 'deg);';
395 if (!$skip_inline_colors) {
396 $send_svg_style = 'fill: ' . esc_attr($send_button_font_color) . '; ' . $send_svg_style;
397 }
398 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="' . $send_svg_style . '">';
399 echo ' <path d="M498.1 5.6c10.1 7 15.4 19.1 13.5 31.2l-64 416c-1.5 9.7-7.4 18.2-16 23s-18.9 5.4-28 1.6L284 427.7l-68.5 74.1c-8.9 9.7-22.9 12.9-35.2 8.1S160 493.2 160 480V396.4c0-4 1.5-7.8 4.2-10.7L331.8 202.8c5.8-6.3 5.6-16-.4-22s-15.7-6.4-22-.7L106 360.8 17.7 316.6C7.1 311.3 .3 300.7 0 288.9s5.9-22.8 16.1-28.7l448-256c10.7-6.1 23.9-5.5 34 1.4z"></path>';
400 echo ' </svg>';
401 }
402 echo ' </button>';
403 echo ' </div>';
404
405 echo ' <div class="chat-toolbar">';
406
407 // PDF Upload Button - wrapped in conditional using current_options
408 $show_pdf_button = isset($current_options['show_pdf_upload_button']) ? $current_options['show_pdf_upload_button'] : 'on';
409 if ($show_pdf_button === 'on') {
410 echo ' <input type="file" id="pdf-upload-' . esc_attr($bot_id) . '" class="pdf-upload" accept=".pdf" style="display: none;">';
411 echo ' <button id="pdf-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn pdf-upload-btn" title="' . esc_attr__('Upload PDF', 'mxchat') . '">';
412 echo ' <!-- Icon from Font Awesome Free: https://fontawesome.com/license/free -->';
413 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" stroke="currentColor">';
414 echo ' <path d="M64 464l48 0 0 48-48 0c-35.3 0-64-28.7-64-64L0 64C0 28.7 28.7 0 64 0L229.5 0c17 0 33.3 6.7 45.3 18.7l90.5 90.5c12 12 18.7 28.3 18.7 45.3L384 304l-48 0 0-144-80 0c-17.7 0-32-14.3-32-32l0-80L64 48c-8.8 0-16 7.2-16 16l0 384c0 8.8 7.2 16 16 16zM176 352l32 0c30.9 0 56 25.1 56 56s-25.1 56-56 56l-16 0 0 32c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-48 0-80c0-8.8 7.2-16 16-16zm32 80c13.3 0 24-10.7 24-24s-10.7-24-24-24l-16 0 0 48 16 0zm96-80l32 0c26.5 0 48 21.5 48 48l0 64c0 26.5-21.5 48-48 48l-32 0c-8.8 0-16-7.2-16-16l0-128c0-8.8 7.2-16 16-16zm32 128c8.8 0 16-7.2 16-16l0-64c0-8.8-7.2-16-16-16l-16 0 0 96 16 0zm80-112c0-8.8 7.2-16 16-16l48 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 32 32 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 48c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-64 0-64z"></path>';
415 echo ' </svg>';
416 echo ' </button>';
417 }
418
419 // Word Upload Button - wrapped in conditional using current_options
420 $show_word_button = isset($current_options['show_word_upload_button']) ? $current_options['show_word_upload_button'] : 'on';
421 if ($show_word_button === 'on') {
422 echo ' <input type="file" id="word-upload-' . esc_attr($bot_id) . '" class="word-upload" accept=".docx" style="display: none;">';
423 echo ' <button id="word-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn word-upload-btn" title="' . esc_attr__('Upload Word Document', 'mxchat') . '">';
424 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" stroke="currentColor">';
425 echo ' <path d="M48 448L48 64c0-8.8 7.2-16 16-16l160 0 0 80c0 17.7 14.3 32 32 32l80 0 0 288c0 8.8-7.2 16-16 16L64 464c-8.8 0-16-7.2-16-16zM64 0C28.7 0 0 28.7 0 64L0 448c0 35.3 28.7 64 64 64l256 0c35.3 0 64-28.7 64-64l0-293.5c0-17-6.7-33.3-18.7-45.3L274.7 18.7C262.7 6.7 246.5 0 229.5 0L64 0zm55 241.1c-3.8-12.7-17.2-19.9-29.9-16.1s-19.9 17.2-16.1 29.9l48 160c3 10.2 12.4 17.1 23 17.1s19.9-7 23-17.1l25-83.4 25 83.4c3 10.2 12.4 17.1 23 17.1s19.9-7 23-17.1l48-160c3.8-12.7-3.4-26.1-16.1-29.9s-26.1 3.4-29.9 16.1l-25 83.4-25-83.4c-3-10.2-12.4-17.1-23-17.1s-19.9 7-23 17.1l-25 83.4-25-83.4z"/></svg>';
426 echo ' </button>';
427 }
428
429 // File containers
430 echo ' <div id="active-pdf-container-' . esc_attr($bot_id) . '" class="active-pdf-container" style="display: none;">';
431 echo ' <span id="active-pdf-name-' . esc_attr($bot_id) . '" class="active-pdf-name"></span>';
432 echo ' <button id="remove-pdf-btn-' . esc_attr($bot_id) . '" class="remove-pdf-btn" title="' . esc_attr__('Remove PDF', 'mxchat') . '">';
433 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2">';
434 echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
435 echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
436 echo ' </svg>';
437 echo ' </button>';
438 echo ' </div>';
439
440 echo ' <div id="active-word-container-' . esc_attr($bot_id) . '" class="active-word-container" style="display: none;">';
441 echo ' <span id="active-word-name-' . esc_attr($bot_id) . '" class="active-word-name"></span>';
442 echo ' <button id="remove-word-btn-' . esc_attr($bot_id) . '" class="remove-word-btn" title="' . esc_attr__('Remove Word Document', 'mxchat') . '">';
443 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2">';
444 echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
445 echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
446 echo ' </svg>';
447 echo ' </button>';
448 echo ' </div>';
449
450 // Perplexity Button
451 if (apply_filters('mxchat_perplexity_should_show_logo', true)) {
452 echo ' <button id="perplexity-search-btn-' . esc_attr($bot_id) . '" class="toolbar-btn perplexity-search-btn" title="' . esc_attr__('Search with Perplexity', 'mxchat-perplexity') . '">';
453 echo ' <svg fill="currentColor" height="1em" viewBox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg">';
454 echo ' <path d="M19.785 0v7.272H22.5V17.62h-2.935V24l-7.037-6.194v6.145h-1.091v-6.152L4.392 24v-6.465H1.5V7.188h2.884V0l7.053 6.494V.19h1.09v6.49L19.786 0zm-7.257 9.044v7.319l5.946 5.234V14.44l-5.946-5.397zm-1.099-.08l-5.946 5.398v7.235l5.946-5.234V8.965zm8.136 7.58h1.844V8.349H13.46l6.105 5.54v2.655zm-8.982-8.28H2.59v8.195h1.8v-2.576l6.192-5.62zM5.475 2.476v4.71h5.115l-5.115-4.71zm13.219 0l-5.115 4.71h5.115v-4.71z"></path>';
455 echo ' </svg>';
456 echo ' </button>';
457 }
458
459
460 // Image Analysis Button - hidden by default, controlled by MxChat Vision add-on
461 echo ' <input type="file" id="image-upload-' . esc_attr($bot_id) . '" class="image-upload" accept="image/*" style="display: none;" multiple>';
462 echo ' <button id="image-upload-btn-' . esc_attr($bot_id) . '" class="toolbar-btn image-upload-btn" title="' . esc_attr__('Upload Image for Analysis', 'mxchat') . '" style="display: none;">';
463 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="fill: none !important;">';
464 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;"/>';
465 echo ' <circle cx="12" cy="13" r="3" style="fill: none !important;"/>';
466 echo ' </svg>';
467 echo ' </button>';
468
469
470 echo ' </div>';
471
472 echo ' <div class="chatbot-footer">';
473
474 // Output the privacy notice if enabled
475 if ($privacy_toggle && !empty($privacy_text)) {
476 echo '<p class="privacy-notice">' . $privacy_text . '</p>';
477 }
478
479 echo ' </div>';
480 echo ' </div>';
481 echo ' </div>';
482 echo '</div>';
483
484 if ($is_floating) {
485 echo '</div>';
486
487 if (!empty($pre_chat_message)) {
488 // Rendered hidden by default — JS checkPreChatDismissal() handles show/hide via localStorage
489 echo '<div id="pre-chat-message-' . esc_attr($bot_id) . '" class="pre-chat-message" style="display:none;">';
490 echo nl2br(esc_html($pre_chat_message));
491 echo '<button class="close-pre-chat-message" aria-label="' . esc_attr__('Close', 'mxchat') . '">&times;</button>';
492 echo '</div>';
493 }
494
495 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) . ';"') . '>';
496 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>';
497
498 if (!empty($custom_icon)) {
499 echo '<img src="' . $custom_icon . '" alt="' . esc_attr__('Chatbot Icon', 'mxchat') . '" style="height: 48px; width: 48px; object-fit: contain;" />';
500 } else {
501 $widget_icon_style = 'height: 48px; width: 48px;';
502 if (!$skip_inline_colors) {
503 $widget_icon_style .= ' fill: ' . esc_attr($icon_color) . ';';
504 }
505 echo '<svg id="widget_icon_10" style="' . $widget_icon_style . '" viewBox="0 0 1120 1120" fill="none" xmlns="http://www.w3.org/2000/svg">';
506 echo ' <path fill-rule="evenodd" clip-rule="evenodd" d="M252 434C252 372.144 302.144 322 364 322H770C831.856 322 882 372.144 882 434V614.459L804.595 585.816C802.551 585.06 800.94 583.449 800.184 581.405L763.003 480.924C760.597 474.424 751.403 474.424 748.997 480.924L711.816 581.405C711.06 583.449 709.449 585.06 707.405 585.816L606.924 622.997C600.424 625.403 600.424 634.597 606.924 637.003L707.405 674.184C709.449 674.94 711.06 676.551 711.816 678.595L740.459 756H629.927C629.648 756.476 629.337 756.945 628.993 757.404L578.197 825.082C572.597 832.543 561.403 832.543 555.803 825.082L505.007 757.404C504.663 756.945 504.352 756.476 504.073 756H364C302.144 756 252 705.856 252 644V434ZM633.501 471.462C632.299 468.212 627.701 468.212 626.499 471.462L619.252 491.046C618.874 492.068 618.068 492.874 617.046 493.252L597.462 500.499C594.212 501.701 594.212 506.299 597.462 507.501L617.046 514.748C618.068 515.126 618.874 515.932 619.252 516.954L626.499 536.538C627.701 539.788 632.299 539.788 633.501 536.538L640.748 516.954C641.126 515.932 641.932 515.126 642.954 514.748L662.538 507.501C665.788 506.299 665.788 501.701 662.538 500.499L642.954 493.252C641.932 492.874 641.126 492.068 640.748 491.046L633.501 471.462Z" ></path>';
507 echo ' <path d="M771.545 755.99C832.175 755.17 881.17 706.175 881.99 645.545L804.595 674.184C802.551 674.94 800.94 676.551 800.184 678.595L771.545 755.99Z" ></path>';
508 echo '</svg>';
509 }
510 echo '</div>';
511 }
512
513 return ob_get_clean();
514 }
515
516 /**
517 * Get bot-specific options for multi-bot functionality
518 * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
519 */
520 private function get_bot_options($bot_id = 'default') {
521 // If default bot or multi-bot add-on not active, return empty (use default options)
522 if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
523 return array();
524 }
525
526 //Hook for multi-bot add-on to provide bot-specific options
527 $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
528
529 return is_array($bot_options) ? $bot_options : array();
530 }
531
532 /**
533 * Get bot-specific Pinecone configuration
534 * Used in the knowledge retrieval functions
535 */
536 private function get_bot_pinecone_config($bot_id = 'default') {
537 // If default bot or multi-bot add-on not active, use default Pinecone config
538 if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
539 $addon_options = get_option('mxchat_pinecone_addon_options', array());
540 return array(
541 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
542 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
543 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
544 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
545 );
546 }
547
548 // Hook for multi-bot add-on to provide bot-specific Pinecone config
549 $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
550
551 return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
552 }
553
554
555 private function determine_email_collection_state() {
556 // Logged-in users skip the email form
557 if (is_user_logged_in()) {
558 $current_user = wp_get_current_user();
559 return [
560 'show_email_form' => false,
561 'user_email' => $current_user->user_email,
562 'user_name' => $current_user->display_name ?: $current_user->first_name ?: ''
563 ];
564 }
565
566 // For guests, default to showing the email form.
567 // The session-based check happens client-side via AJAX since the
568 // session ID lives in the browser cookie/localStorage.
569 return [
570 'show_email_form' => true,
571 'user_email' => '',
572 'user_name' => ''
573 ];
574 }
575
576
577 /**
578 * NEW: Determine if and which chatbot should be displayed
579 */
580 private function get_display_bot() {
581 // Get page-specific settings from meta box
582 $page_setting = $this->get_page_bot_setting();
583
584 // Get global settings - FIXED: Check for 'on' instead of 'on'
585 $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
586 $global_default_bot = isset($this->options['default_bot']) ? $this->options['default_bot'] : 'default';
587
588 // If page specifically hides chatbot, don't show anything
589 if ($page_setting && $page_setting['action'] === 'hide') {
590 return false;
591 }
592
593 // If page specifies a specific bot, use that
594 if ($page_setting && $page_setting['action'] === 'show') {
595 return $page_setting['bot_id'];
596 }
597
598 // Page setting is 'global' or no page setting exists
599 // Check global auto-show setting
600 if ($global_autoshow) {
601 // Check post type visibility settings
602 if (!$this->should_show_on_current_post_type()) {
603 return false;
604 }
605
606 // Global auto-show is enabled, return the default bot
607 return $global_default_bot;
608 }
609
610 // Global auto-show is disabled and no page-specific bot selected
611 // Don't show chatbot (user should use shortcodes)
612 return false;
613 }
614
615 /**
616 * Check if chatbot should be shown on the current post type
617 */
618 private function should_show_on_current_post_type() {
619 // Get visibility settings
620 $mode = isset($this->options['post_type_visibility_mode']) ? $this->options['post_type_visibility_mode'] : 'all';
621 $list = isset($this->options['post_type_visibility_list']) ? $this->options['post_type_visibility_list'] : array();
622
623 // Ensure list is an array
624 if (!is_array($list)) {
625 $list = array();
626 }
627
628 // If mode is 'all', show on all post types
629 if ($mode === 'all') {
630 return true;
631 }
632
633 // Get current post type
634 $current_post_type = $this->get_current_post_type();
635
636 // If we can't determine post type, default to showing
637 if (empty($current_post_type)) {
638 return true;
639 }
640
641 // Check based on mode
642 if ($mode === 'include') {
643 // Only show on selected post types
644 return in_array($current_post_type, $list);
645 } elseif ($mode === 'exclude') {
646 // Hide on selected post types
647 return !in_array($current_post_type, $list);
648 }
649
650 // Default to showing
651 return true;
652 }
653
654 /**
655 * Get the current post type
656 */
657 private function get_current_post_type() {
658 // Try to get from queried object first
659 $queried_object = get_queried_object();
660
661 if ($queried_object instanceof WP_Post) {
662 return $queried_object->post_type;
663 }
664
665 // Try get_post_type()
666 $post_type = get_post_type();
667 if ($post_type) {
668 return $post_type;
669 }
670
671 // Check if we're on an archive
672 if (is_post_type_archive()) {
673 return get_query_var('post_type');
674 }
675
676 // Check common archive types
677 if (is_home() || is_single()) {
678 return 'post';
679 }
680
681 if (is_page()) {
682 return 'page';
683 }
684
685 return '';
686 }
687
688 /**
689 * Get page-specific bot setting using new visibility field with backward compat
690 */
691 private function get_page_bot_setting($post_id = null) {
692 if (!$post_id) {
693 $post_id = get_the_ID();
694 }
695
696 if (!$post_id) {
697 return null;
698 }
699
700 // Check new visibility field first
701 $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true);
702
703 if ($visibility === 'hide') {
704 return array('action' => 'hide');
705 }
706
707 if ($visibility === 'show') {
708 $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
709 $bot_id = !empty($selected_bot) ? $selected_bot : 'default';
710 return array('action' => 'show', 'bot_id' => $bot_id);
711 }
712
713 // Backward compat: check legacy hide checkbox if no new field set
714 if (empty($visibility)) {
715 $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true);
716 if ($hide_chatbot === '1') {
717 return array('action' => 'hide');
718 }
719 }
720
721 // Check if specific bot is selected (legacy path)
722 $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true);
723 if (!empty($selected_bot)) {
724 return array('action' => 'show', 'bot_id' => $selected_bot);
725 }
726
727 // Use global setting
728 return array('action' => 'global');
729 }
730
731 /**
732 * NEW: Determine which bot to use for shortcode
733 */
734 private function determine_bot_for_shortcode($shortcode_bot_id) {
735 // If bot_id explicitly provided in shortcode, use that
736 if (!empty($shortcode_bot_id)) {
737 return $shortcode_bot_id;
738 }
739
740 // No bot_id in shortcode, check page setting
741 $page_setting = $this->get_page_bot_setting();
742 if ($page_setting && $page_setting['action'] === 'show') {
743 return $page_setting['bot_id'];
744 }
745
746 // Fall back to default
747 return 'default';
748 }
749
750 /**
751 * NEW: Helper to check if auto-append is enabled globally
752 */
753 private function is_auto_append_enabled() {
754 return isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
755 }
756
757 /**
758 * UPDATED: Debug function with new context info
759 */
760 public function debug_display_logic() {
761 if (current_user_can('manage_options') && isset($_GET['mxchat_debug'])) {
762 $bot_to_show = $this->get_display_bot();
763 $page_setting = $this->get_page_bot_setting();
764 $global_autoshow = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on';
765 $hide_auto = $this->should_hide_chatbot('auto');
766
767 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;">';
768 echo '<h4 style="margin: 0 0 10px 0;">MxChat Debug Info</h4>';
769 echo '<p><strong>Raw append_to_body value:</strong> "' . ($this->options['append_to_body'] ?? 'NOT SET') . '"</p>';
770 echo '<p><strong>Page Setting:</strong><br><pre>' . print_r($page_setting, true) . '</pre></p>';
771 echo '<p><strong>Global Auto-show:</strong> ' . ($global_autoshow ? 'ON' : 'OFF') . '</p>';
772 echo '<p><strong>Hide Auto-Append:</strong> ' . ($hide_auto ? 'YES' : 'NO') . '</p>';
773 echo '<p><strong>Bot to Show:</strong> ' . ($bot_to_show === false ? 'NONE' : $bot_to_show) . '</p>';
774 echo '<p><strong>Shortcodes:</strong> floating="no" always works</p>';
775 echo '<p><small>Add ?mxchat_debug=1 to URL to see this</small></p>';
776 echo '</div>';
777 }
778 }
779
780 /**
781 * NEW: Helper method to get available bots (for admin notices, etc.)
782 */
783 private function get_available_bots() {
784 $bots = array('default' => __('Default Bot', 'mxchat'));
785
786 // Check if multi-bot addon is active
787 if (class_exists('MxChat_Multi_Bot_Manager')) {
788 $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance();
789 $available_bots = $multi_bot_manager->get_available_bots();
790
791 // Add the available bots
792 foreach ($available_bots as $bot_id => $bot_name) {
793 $bots[$bot_id] = $bot_name;
794 }
795 }
796
797 return $bots;
798 }
799
800
801
802 }
803 ?>