PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.6.0
MxChat – AI Chatbot & Content Generation for WordPress v1.6.0
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 1.6.0, at includes/class-mxchat-public.php

307 lines 20.3 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 $this->options = get_option('mxchat_options', $this->get_default_options());
11 add_shortcode('mxchat_chatbot', array($this, 'render_chatbot_shortcode'));
12 add_action('wp_footer', array($this, 'append_chatbot_to_body'));
13 }
14
15 private function get_default_options() {
16 return array(
17 'user_message_bg_color' => '#fff',
18 'user_message_font_color' => '#212121',
19 'bot_message_bg_color' => '#212121',
20 'bot_message_font_color' => '#fff',
21 'live_agent_message_bg_color' => '#212121',
22 'live_agent_message_font_color' => '#fff',
23 'top_bar_bg_color' => '#212121',
24 'send_button_font_color' => '#212121',
25 'close_button_color' => '#fff',
26 'chatbot_bg_color' => '#fff',
27 'chatbot_background_color' => '#212121',
28 'icon_color' => '#fff',
29 'chat_input_font_color' => '#212121',
30 'api_key' => '',
31 'system_prompt_instructions' => '[EXAMPLE INSTRUCTIONS] You are an AI Chatbot assistant for this website. The primary subject you should focus on is [insert proper subject here]. Your main goal is to assist visitors with questions related to this specific topic. Here are some key things to keep in mind:
32 - Your name is [Chatbot Name]. Always introduce yourself as this name when appropriate.
33 - Stay focused on topics related to [insert proper subject here]. If a visitor asks about an unrelated topic, politely redirect the conversation to how you can assist them with this subject. If there is an exception topic (e.g., "parking") that you should assist with, you may do so if instructed.
34 - When appropriate, highlight the benefits of [insert proper subject here]. Offer to guide visitors to relevant pages or provide them with more information.
35 - If a visitor asks for a purchase link or further information, provide them with this link: [Insert Purchase Link Here]. Always ensure that the link is relevant and directly related to the website\'s offerings.
36 - Keep your responses short, concise, and to the point. Provide clear and direct answers suitable for a chatbot interaction.
37 - If you reference specific content, provide a hyperlink to the relevant page using hypertext. Avoid including links that do not directly relate to the content or answer the visitor\'s query.
38 - Provide answers based on the knowledge available to you. If you do not have an answer to a specific question, let the visitor know that you don’t have the information and suggest where they might find it or offer to help with something else.',
39 'model' => 'gpt-3.5-turbo',
40 'rate_limit' => '100',
41 'rate_limit_message' => 'Rate limit exceeded. Please try again later.',
42 'top_bar_title' => 'MxChat: Basic',
43 'intro_message' => 'Hello! How can I assist you today?',
44 'append_to_body' => 'on',
45 'enable_email_block' => '', // Default to disabled
46
47 );
48 }
49
50 public function append_chatbot_to_body() {
51 $options = get_option('mxchat_options', $this->get_default_options());
52 $consent_category = 'marketing';
53
54 if (isset($options['append_to_body']) && $options['append_to_body'] === 'on') {
55 // Store consent status but always render
56 $has_consent = true;
57
58 if (
59 isset($options['complianz_toggle']) &&
60 $options['complianz_toggle'] === 'on' &&
61 function_exists('cmplz_has_consent')
62 ) {
63 $has_consent = cmplz_has_consent($consent_category);
64 }
65
66 // Add consent status as a parameter to the shortcode
67 echo do_shortcode('[mxchat_chatbot floating="yes" has_consent="' . ($has_consent ? 'yes' : 'no') . '"]');
68 }
69 }
70
71 private function mxchat_get_user_identifier() {
72 return sanitize_text_field($_SERVER['REMOTE_ADDR']);
73 }
74
75 public function render_chatbot_shortcode($atts) {
76 $attributes = shortcode_atts(array(
77 'floating' => 'yes',
78 'has_consent' => 'yes' // Add default
79 ), $atts);
80
81 $is_floating = $attributes['floating'] === 'yes';
82 $this->options = get_option('mxchat_options', $this->get_default_options());
83
84 // Check for Complianz consent if the toggle is enabled
85 $initial_visibility = 'hidden';
86 $additional_class = '';
87
88 if (isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on') {
89 // If Complianz is enabled, add no-consent class by default
90 $additional_class = ' no-consent';
91 }
92 $visibility_class = $initial_visibility . $additional_class;
93
94
95 $bg_color = $this->options['chatbot_background_color'] ?? '#fff';
96 $user_message_bg_color = $this->options['user_message_bg_color'] ?? '#fff';
97 $user_message_font_color = $this->options['user_message_font_color'] ?? '#212121';
98 $bot_message_bg_color = $this->options['bot_message_bg_color'] ?? '#212121';
99 $bot_message_font_color = $this->options['bot_message_font_color'] ?? '#fff';
100 $top_bar_bg_color = $this->options['top_bar_bg_color'] ?? '#212121';
101 $send_button_font_color = $this->options['send_button_font_color'] ?? '#212121';
102 $intro_message = $this->options['intro_message'] ?? 'Hello! How can I assist you today?';
103 $top_bar_title = $this->options['top_bar_title'] ?? 'MxChat: Basic';
104 $chatbot_background_color = $this->options['chatbot_background_color'] ?? '#212121';
105 $icon_color = $this->options['icon_color'] ?? '#fff';
106 $chat_input_font_color = $this->options['chat_input_font_color'] ?? '#212121';
107 $close_button_color = $this->options['close_button_color'] ?? '#fff';
108 $chatbot_bg_color = $this->options['chatbot_bg_color'] ?? '#fff';
109 $pre_chat_message = isset($this->options['pre_chat_message']) ? sanitize_text_field(trim($this->options['pre_chat_message'])) : '';
110 $user_id = sanitize_key($this->mxchat_get_user_identifier());
111 $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
112 $input_copy = isset($this->options['input_copy']) ? esc_attr($this->options['input_copy']) : 'How can I assist?';
113 $rate_limit_message = isset($this->options['rate_limit_message']) ? esc_attr($this->options['rate_limit_message']) : 'Rate limit exceeded. Please try again later.';
114
115 $privacy_toggle = isset($this->options['privacy_toggle']) && $this->options['privacy_toggle'] === 'on';
116 $privacy_text = isset($this->options['privacy_text']) ? wp_kses_post($this->options['privacy_text']) : 'By chatting, you agree to our <a href="https://example.com/privacy-policy" target="_blank">privacy policy</a>.';
117
118 $popular_question_1 = isset($this->options['popular_question_1']) ? esc_html($this->options['popular_question_1']) : '';
119 $popular_question_2 = isset($this->options['popular_question_2']) ? esc_html($this->options['popular_question_2']) : '';
120 $popular_question_3 = isset($this->options['popular_question_3']) ? esc_html($this->options['popular_question_3']) : '';
121 $additional_questions = isset($this->options['additional_popular_questions']) ? $this->options['additional_popular_questions'] : [];
122 $custom_icon = isset($this->options['custom_icon']) ? esc_url($this->options['custom_icon']) : '';
123 $title_icon = isset($this->options['title_icon']) ? esc_url($this->options['title_icon']) : '';
124
125
126 $live_agent_message_bg_color = $this->options['live_agent_message_bg_color'] ?? '#212121';
127 $live_agent_message_font_color = $this->options['live_agent_message_font_color'] ?? '#fff';
128 $enable_email_block = isset($this->options['enable_email_block']) &&
129 ($this->options['enable_email_block'] === '1' || $this->options['enable_email_block'] === 'on');
130
131 ob_start();
132
133 // Check if floating attribute is set to 'yes' and wrap accordingly
134 if ($is_floating) {
135 echo '<div id="floating-chatbot" class="' . $initial_visibility . $additional_class . '">';
136 }
137
138 echo '<div id="mxchat-chatbot-wrapper">';
139 echo ' <div class="chatbot-top-bar" id="exit-chat-button" style="background: ' . esc_attr($top_bar_bg_color) . ';">';
140 echo ' <div class="chatbot-title-container">';
141 echo ' <div class="chatbot-title-group">';
142 if (!empty($title_icon)) {
143 echo ' <img src="' . esc_url($title_icon) . '" alt="" class="chatbot-title-icon">';
144 }
145 echo ' <p class="chatbot-title" style="color: ' . esc_attr($close_button_color) . ';">' . esc_html($top_bar_title) . '</p>';
146 echo ' </div>';
147 echo ' <span class="chat-mode-indicator" id="chat-mode-indicator" style="color: ' . esc_attr($close_button_color) . ';">AI Agent</span>';
148 echo ' </div>';
149 echo ' <button class="exit-chat" type="button" aria-label="Minimize" style="color: ' . esc_attr($close_button_color) . ';">';
150
151 echo ' <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" id="ic-minimize" style="fill: ' . esc_attr($close_button_color) . ';">';
152 echo ' <path d="M0 0h24v24H0z" fill="none"></path>';
153 echo ' <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"></path>';
154 echo ' </svg>';
155 echo ' <span>Minimize</span>';
156 echo ' </button>';
157 echo ' </div>';
158
159 // 3b) Main chatbot container
160 echo ' <div id="mxchat-chatbot" style="background-color: ' . esc_attr($chatbot_bg_color) . ';">';
161
162 if ($enable_email_block) {
163 echo '<div id="email-blocker" class="email-blocker">';
164 echo ' <div class="email-blocker-header">';
165
166 // Safely echo the user-provided HTML
167 // (We rely on wp_kses_post() from the sanitize step to ensure it's safe)
168 $header_html = isset($this->options['email_blocker_header_content'])
169 ? $this->options['email_blocker_header_content']
170 : '';
171 echo wp_kses_post($header_html);
172
173 echo ' </div>';
174 echo ' <form id="email-collection-form">';
175 echo ' <label for="user-email" class="sr-only">Email Address</label>';
176 echo ' <input type="email" id="user-email" name="user_email" required placeholder="Enter your email address" />';
177 echo '<button type="submit" id="email-submit-button">';
178 // If not set, fallback to default. Typically, $this->options includes defaults merged in.
179 $button_text = isset($this->options['email_blocker_button_text'])
180 ? $this->options['email_blocker_button_text']
181 : 'Start Chat'; // fallback just in case
182 echo esc_html($button_text);
183 echo '</button>';
184 echo ' </form>';
185 echo '</div>';
186 }
187
188 echo ' <div id="chat-container" style="' . ($enable_email_block ? 'display: none;' : '') . '">';
189 echo ' <div id="chat-box">';
190 echo ' <div class="bot-message" style="background: '
191 . esc_attr($bot_message_bg_color) . '; color: '
192 . esc_attr($bot_message_font_color) . ';">';
193 echo nl2br(esc_html($intro_message));
194 echo ' </div>';
195 echo ' </div>'; // end #chat-box
196
197 // Add the popular questions section
198 echo ' <div id="mxchat-popular-questions">';
199 echo ' <div class="mxchat-popular-questions-container">';
200
201 if (!empty($popular_question_1)) {
202 echo '<button class="mxchat-popular-question">' . esc_html($popular_question_1) . '</button>';
203 }
204 if (!empty($popular_question_2)) {
205 echo '<button class="mxchat-popular-question">' . esc_html($popular_question_2) . '</button>';
206 }
207 if (!empty($popular_question_3)) {
208 echo '<button class="mxchat-popular-question">' . esc_html($popular_question_3) . '</button>';
209 }
210
211 if (!empty($additional_questions) && is_array($additional_questions)) {
212 foreach ($additional_questions as $index => $question) {
213 if (!empty($question)) {
214 echo '<button class="mxchat-popular-question">' . esc_html($question) . '</button>';
215 }
216 }
217 }
218
219 echo ' </div>';
220 echo ' </div>';
221
222
223 echo ' <div id="input-container">';
224 echo ' <textarea id="chat-input" placeholder="' . esc_attr($input_copy) . '" style="color: ' . esc_attr($chat_input_font_color) . ';"></textarea>';
225 echo ' <button id="send-button">';
226 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="fill: ' . esc_attr($send_button_font_color) . ';">';
227 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"/>';
228 echo ' </svg>';
229 echo ' </button>';
230 echo ' </div>';
231 echo ' <div class="chat-toolbar">';
232 echo ' <input type="file" id="pdf-upload" accept=".pdf" style="display: none;">';
233 echo ' <button id="pdf-upload-btn" class="toolbar-btn" title="Upload PDF">';
234 echo ' <!-- Icon from Font Awesome Free: https://fontawesome.com/license/free -->';
235 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" stroke="currentColor">';
236 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>';
237 echo ' </svg>';
238 echo ' </button>';
239
240 echo ' <input type="file" id="word-upload" accept=".docx" style="display: none;">';
241 echo ' <button id="word-upload-btn" class="toolbar-btn" title="Upload Word Document">';
242 echo ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" stroke="currentColor">';
243 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>';
244 echo ' </button>';
245
246 echo ' <div id="active-pdf-container" class="active-pdf-container" style="display: none;">';
247 echo ' <span id="active-pdf-name" class="active-pdf-name"></span>';
248 echo ' <button id="remove-pdf-btn" class="remove-pdf-btn" title="Remove PDF">';
249 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">';
250 echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
251 echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
252 echo ' </svg>';
253 echo ' </button>';
254 echo ' </div>';
255
256 echo ' <div id="active-word-container" class="active-pdf-container" style="display: none;">';
257 echo ' <span id="active-word-name" class="active-pdf-name"></span>';
258 echo ' <button id="remove-word-btn" class="remove-pdf-btn" title="Remove Word Document">';
259 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">';
260 echo ' <line x1="18" y1="6" x2="6" y2="18"></line>';
261 echo ' <line x1="6" y1="6" x2="18" y2="18"></line>';
262 echo ' </svg>';
263 echo ' </button>';
264 echo ' </div>';
265 echo ' </div>';
266 echo ' <div class="chatbot-footer">';
267
268 // Output the privacy notice if enabled
269 if ($privacy_toggle && !empty($privacy_text)) {
270 echo '<p class="privacy-notice">' . $privacy_text . '</p>';
271 }
272
273 echo ' </div>';
274 echo ' </div>';
275 echo ' </div>';
276 echo '</div>';
277
278 if ($is_floating) {
279 echo '</div>';
280
281 if (!empty($pre_chat_message) && !get_transient($transient_key)) {
282 echo '<div id="pre-chat-message">';
283 echo esc_html($pre_chat_message);
284 echo '<button class="close-pre-chat-message" aria-label="Close">&times;</button>';
285 echo '</div>';
286 }
287
288 echo '<div class="' . esc_attr($visibility_class) . '" id="floating-chatbot-button" style="background: ' . esc_attr($chatbot_background_color) . '; color: ' . esc_attr($send_button_font_color) . ';">';
289 if (!empty($custom_icon)) {
290 echo '<img src="' . $custom_icon . '" alt="Chatbot Icon" style="height: 48px; width: 48px; object-fit: contain;" />';
291 } else {
292 echo '<svg id="widget_icon_10" style="height: 48px; width: 48px; fill: ' . esc_attr($icon_color) . '" viewBox="0 0 1120 1120" fill="none" xmlns="http://www.w3.org/2000/svg">';
293 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>';
294 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>';
295 echo '</svg>';
296 }
297 echo '</div>';
298
299 }
300
301 return ob_get_clean();
302 }
303
304
305 }
306 ?>
307