PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.5.8
MxChat – AI Chatbot & Content Generation for WordPress v1.5.8
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
← All changes | includes/class-mxchat-admin.php +3241 -9408 3.2.161.5.8 View file →
@@ -6,31 +6,13 @@
6 6 class MxChat_Admin {
7 7 private $options;
8 8 private $chat_count;
9 9 private $is_activated;
10 - private $knowledge_manager;
11 10
12 - /**
13 - * Get whether the license is activated
14 - * @return bool
15 - */
16 - public function is_activated() {
17 - return $this->is_activated;
18 - }
19 -
20 - /**
21 - * Get plugin options
22 - * @return array
23 - */
24 - public function get_options() {
25 - return $this->options;
26 - }
27 -
28 - public function __construct($knowledge_manager = null) {
11 + public function __construct() {
29 12 $this->options = get_option('mxchat_options');
30 13 $this->chat_count = get_option('mxchat_chat_count', 0);
31 14 $this->is_activated = $this->is_license_active();
32 - $this->knowledge_manager = $knowledge_manager;
33 15
34 16 // Initialize default options if they are not set
35 17 if (!$this->options) {
36 18 $this->initialize_default_options();
@@ -37,401 +19,100 @@
37 19 }
38 20
39 21 // Add admin menu and initialize settings
40 22 add_action('admin_menu', array($this, 'mxchat_add_plugin_page'));
41 - // Pro & Extensions registers at priority 30 so it always lands last in
42 - // the sidebar — below API Access (priority 20) and below any other
43 - // submenu that hooks at default priority 10.
44 - add_action('admin_menu', array($this, 'mxchat_add_pro_extensions_page'), 30);
45 - // Onboarding visibility — runs LAST so it can remove the submenu after every
46 - // other add_submenu_page() call. The page stays reachable by direct URL.
47 - add_action('admin_menu', array($this, 'mxchat_apply_onboarding_visibility'), 999);
48 23 add_action('admin_init', array($this, 'mxchat_page_init'));
49 - add_action('admin_init', array($this, 'mxchat_prompts_page_init'));
50 24 add_action('admin_enqueue_scripts', array($this, 'mxchat_enqueue_admin_assets'));
51 - // Add body class for the Onboarding wizard (plan-905439) so the
52 - // chrome-surgery CSS in admin-onboarding-wizard.css can scope its
53 - // WP-sidebar collapse to this page only.
54 - add_filter('admin_body_class', array($this, 'mxchat_add_onboarding_body_class'));
55 25 add_action('wp_ajax_mxchat_delete_chat_history', array($this, 'mxchat_delete_chat_history'));
26 + add_action('admin_post_mxchat_submit_content', array($this, 'mxchat_handle_content_submission'));
56 27 add_action('admin_post_mxchat_delete_prompt', array($this, 'mxchat_handle_delete_prompt'));
57 28 add_action('wp_ajax_mxchat_fetch_chat_history', array($this, 'mxchat_fetch_chat_history'));
58 29 add_action('wp_ajax_nopriv_mxchat_fetch_chat_history', array($this, 'mxchat_fetch_chat_history'));
59 - add_action('wp_ajax_mxchat_fetch_conversation', array($this, 'mxchat_fetch_conversation'));
30 + add_action('admin_post_mxchat_submit_sitemap', array($this, 'mxchat_handle_sitemap_submission'));
60 31 add_action('wp_footer', array($this, 'mxchat_append_chatbot_to_body'));
61 32 add_action('admin_head-mxchat-prompts', array($this, 'mxchat_enqueue_admin_assets'));
62 33 add_action('admin_head-toplevel_page_mxchat-max', array($this, 'mxchat_enqueue_admin_assets'));
34 + add_action('wp_ajax_mxchat_activate_license', array($this, 'mxchat_handle_activate_license'));
63 35 add_action('admin_notices', array($this, 'mxchat_display_admin_notice'));
36 + // Add the AJAX handler for logged-in users
37 + add_action('wp_ajax_mxchat_save_inline_prompt', array($this, 'mxchat_save_inline_prompt'));
64 38 add_action('admin_post_mxchat_delete_all_prompts', array($this, 'mxchat_handle_delete_all_prompts'));
39 +
40 + // Define admin_post actions for form submission handling
65 41 add_action('admin_post_mxchat_add_intent', array($this, 'mxchat_handle_add_intent'));
66 42 add_action('admin_post_mxchat_delete_intent', array($this, 'mxchat_handle_delete_intent'));
67 - add_action('admin_post_mxchat_edit_intent', array($this, 'mxchat_handle_edit_intent'));
68 - add_action('wp_ajax_mxchat_export_transcripts', array($this, 'export_chat_transcripts'));
69 -
70 - // Leads tab (inside Transcripts)
71 - add_action('wp_ajax_mxchat_fetch_leads', array($this, 'mxchat_fetch_leads'));
72 - add_action('wp_ajax_mxchat_delete_leads', array($this, 'mxchat_delete_leads'));
73 - add_action('wp_ajax_mxchat_export_leads', array($this, 'mxchat_export_leads'));
74 -
75 - add_action('admin_init', array($this, 'mxchat_transcripts_page_init'));
76 - add_action('wp_ajax_dismiss_live_agent_notice', array($this, 'dismiss_live_agent_notice'));
77 - add_action('wp_ajax_dismiss_theme_migration_notice', array($this, 'dismiss_theme_migration_notice'));
78 - add_action('mxchat_cleanup_old_transcripts', array($this, 'cleanup_old_transcripts'));
79 - // Self-heal: the cleanup event is otherwise only scheduled at activation or
80 - // when the retention setting CHANGES — if it is ever lost (deactivate cycle,
81 - // cron-option wipe, DB restore) nothing re-registers it and retention goes
82 - // silently inert while the UI still says it is on (plan-bc08a6).
83 - add_action('admin_init', array($this, 'ensure_transcript_cleanup_scheduled'));
84 -
85 - add_action('admin_init', array($this, 'register_pinecone_settings'));
86 - add_action('admin_init', array($this, 'register_openai_vectorstore_settings'));
87 -
88 - add_action('admin_notices', array($this, 'display_admin_notices'));
89 -
90 - add_action('wp_ajax_mxchat_test_streaming_actual', [$this, 'mxchat_handle_test_streaming_actual']);
91 - add_action('wp_ajax_mxchat_test_streaming', [$this, 'mxchat_handle_test_streaming']); // Keep existing as fallback
92 - add_action('wp_ajax_mxchat_test_vectorstore_connection', array($this, 'mxchat_test_vectorstore_connection'));
93 -
94 - // wp_ajax_mxchat_save_selected_bot is registered (and handled) in
95 - // admin/class-ajax-handler.php — a duplicate registration here pointed
96 - // at a method this class never defined, a load-order-dependent fatal
97 - // waiting to fire (plan 52bcad).
98 - add_action('wp_ajax_mxchat_fetch_openrouter_models', array($this, 'fetch_openrouter_models'));
99 - add_action('wp_ajax_mxchat_get_rag_context', array($this, 'mxchat_get_rag_context'));
100 -
101 - // Actions page AJAX handlers
102 - add_action('wp_ajax_mxchat_fetch_actions_list', array($this, 'mxchat_fetch_actions_list'));
103 - add_action('wp_ajax_mxchat_toggle_action_status', array($this, 'mxchat_toggle_action_status'));
104 - add_action('wp_ajax_mxchat_bulk_delete_actions', array($this, 'mxchat_bulk_delete_actions'));
105 - add_action('wp_ajax_mxchat_add_intent_ajax', array($this, 'mxchat_add_intent_ajax'));
106 - add_action('wp_ajax_mxchat_edit_intent_ajax', array($this, 'mxchat_edit_intent_ajax'));
107 - add_action('wp_ajax_mxchat_delete_intent_ajax', array($this, 'mxchat_delete_intent_ajax'));
108 - add_action('wp_ajax_mxchat_add_phrase', array($this, 'mxchat_add_phrase_ajax'));
109 - add_action('wp_ajax_mxchat_delete_phrase', array($this, 'mxchat_delete_phrase_ajax'));
110 - add_action('wp_ajax_mxchat_get_phrases', array($this, 'mxchat_get_phrases_ajax'));
111 - add_action('wp_ajax_mxchat_delete_legacy_phrases', array($this, 'mxchat_delete_legacy_phrases_ajax'));
112 -
113 - // Slack test connection
114 - add_action('wp_ajax_mxchat_test_slack_connection', array($this, 'mxchat_test_slack_connection'));
115 -
116 - // Translation handlers
117 - add_action('wp_ajax_mxchat_translate_messages', array($this, 'mxchat_translate_messages'));
118 - add_action('wp_ajax_mxchat_get_transcript_translation', array($this, 'mxchat_get_transcript_translation'));
119 -
120 - // 3.2.3: Embedding model switch protection
121 - add_action('wp_ajax_mxchat_check_embedding_switch', array($this, 'mxchat_check_embedding_switch_ajax'));
122 - add_action('wp_ajax_mxchat_dismiss_embedding_mismatch', array($this, 'mxchat_dismiss_embedding_mismatch_ajax'));
123 - add_action('wp_ajax_mxchat_dismiss_telegram_secret_notice', array($this, 'mxchat_dismiss_telegram_secret_notice_ajax'));
124 - add_action('admin_notices', array($this, 'mxchat_embedding_mismatch_notice'));
125 - add_action('admin_notices', array($this, 'mxchat_telegram_secret_notice'));
43 + add_action('admin_post_mxchat_update_intent_threshold', array($this, 'mxchat_handle_update_intent_threshold'));
44 + add_action('admin_post_mxchat_stop_processing', array($this, 'mxchat_stop_processing'));
45 +add_action('admin_post_mxchat_edit_intent', array($this, 'handle_edit_intent'));
126 46 }
127 47
128 - /**
129 - * Nudge admins to configure a Telegram webhook secret (plan-0c17b5).
130 - *
131 - * Shown only when a Telegram bot token is configured (integration active)
132 - * AND no webhook secret is set. Without a secret the webhook falls back to a
133 - * Telegram source-IP check — safer than the old fail-open, but a real secret
134 - * is the recommended protection. Dismissible; does not block anything.
135 - */
136 - public function mxchat_telegram_secret_notice() {
137 - if (!current_user_can('manage_options')) {
138 - return;
139 - }
140 - $options = get_option('mxchat_options', array());
141 - $has_token = !empty($options['telegram_bot_token']);
142 - $has_secret = !empty($options['telegram_webhook_secret']);
143 - if (!$has_token || $has_secret) {
144 - return;
145 - }
146 - if (get_option('mxchat_dismissed_telegram_secret_notice', '') === '1') {
147 - return;
148 - }
149 - $settings_url = admin_url('admin.php?page=mxchat-max');
150 - ?>
151 - <div class="notice notice-warning is-dismissible mxchat-telegram-secret-notice">
152 - <p><strong><?php esc_html_e('MxChat: Telegram webhook is running without a secret token', 'mxchat'); ?></strong></p>
153 - <p>
154 - <?php esc_html_e('Your Telegram integration is active but no webhook secret is configured. Requests are currently verified only by origin IP. For full protection, set a webhook secret token in the Telegram settings and re-register your webhook.', 'mxchat'); ?>
155 - </p>
156 - <p>
157 - <a href="<?php echo esc_url($settings_url); ?>" class="button button-primary"><?php esc_html_e('Open Telegram settings', 'mxchat'); ?></a>
158 - </p>
159 - </div>
160 - <script>
161 - (function(){
162 - var n = document.querySelector('.mxchat-telegram-secret-notice');
163 - if (!n) return;
164 - n.addEventListener('click', function(e){
165 - if (e.target && e.target.classList.contains('notice-dismiss')) {
166 - var d = new FormData();
167 - d.append('action', 'mxchat_dismiss_telegram_secret_notice');
168 - d.append('nonce', '<?php echo esc_js(wp_create_nonce('mxchat_dismiss_telegram_secret')); ?>');
169 - fetch('<?php echo esc_url(admin_url('admin-ajax.php')); ?>', { method: 'POST', body: d, credentials: 'same-origin' });
170 - }
171 - });
172 - })();
173 - </script>
174 - <?php
48 + // Method to check if the license is active
49 + private function is_license_active() {
50 + $license_status = get_option('mxchat_license_status', 'inactive');
51 + return $license_status === 'active';
175 52 }
176 53
177 - /**
178 - * AJAX: persist dismissal of the Telegram-secret admin notice (plan-0c17b5).
179 - */
180 - public function mxchat_dismiss_telegram_secret_notice_ajax() {
181 - if (!current_user_can('manage_options')
182 - || !check_ajax_referer('mxchat_dismiss_telegram_secret', 'nonce', false)) {
183 - wp_send_json_error();
184 - }
185 - update_option('mxchat_dismissed_telegram_secret_notice', '1', 'no');
186 - wp_send_json_success();
187 - }
188 -
189 - /**
190 - * 3.2.3: Preflight check before allowing the embedding model dropdown to
191 - * switch. Pure option comparison — no counts, no DB queries beyond the
192 - * cached options. The dialog is shown whenever the user has previously
193 - * embedded with a different model than the one they're switching to.
194 - */
195 - public function mxchat_check_embedding_switch_ajax() {
196 - check_ajax_referer('mxchat_admin_nonce', 'security');
197 - if (!current_user_can('manage_options')) {
198 - wp_send_json_error(__('Unauthorized', 'mxchat'));
199 - }
200 -
201 - $new_model = isset($_POST['new_model']) ? sanitize_text_field(wp_unslash($_POST['new_model'])) : '';
202 - $active_model = MxChat_Utils::get_active_embedding_model();
203 -
204 - $is_mismatch = !empty($active_model) && !empty($new_model) && $active_model !== $new_model;
205 -
206 - $active_dims = MxChat_Utils::embedding_model_dimensions($active_model);
207 - $new_dims = MxChat_Utils::embedding_model_dimensions($new_model);
208 -
209 - wp_send_json_success(array(
210 - 'is_mismatch' => $is_mismatch,
211 - 'active_model' => $active_model,
212 - 'active_label' => MxChat_Utils::embedding_model_label($active_model),
213 - 'new_model' => $new_model,
214 - 'new_label' => MxChat_Utils::embedding_model_label($new_model),
215 - 'dims_differ' => ($active_dims > 0 && $new_dims > 0 && $active_dims !== $new_dims),
216 - 'active_dims' => $active_dims,
217 - 'new_dims' => $new_dims,
218 - ));
219 - }
220 -
221 - /**
222 - * 3.2.3: Dismiss the persistent mismatch banner. Tied to the active+selected
223 - * pair so the banner reappears on the next switch event.
224 - */
225 - public function mxchat_dismiss_embedding_mismatch_ajax() {
226 - check_ajax_referer('mxchat_admin_nonce', 'security');
227 - if (!current_user_can('manage_options')) {
228 - wp_send_json_error(__('Unauthorized', 'mxchat'));
229 - }
230 -
231 - $options = get_option('mxchat_options', array());
232 - $selected = $options['embedding_model'] ?? '';
233 - $active = MxChat_Utils::get_active_embedding_model();
234 - update_option('mxchat_dismissed_embedding_mismatch', $active . '|' . $selected, false);
235 - wp_send_json_success();
236 - }
237 -
238 - /**
239 - * 3.2.3: Persistent admin banner shown whenever the active embedding model
240 - * (last used to actually embed something) differs from the currently
241 - * selected model. Pure option comparison — no DB queries on every page
242 - * load. The banner auto-clears once both match again, i.e. after a delete
243 - * + re-embed cycle.
244 - */
245 - public function mxchat_embedding_mismatch_notice() {
246 - if (!current_user_can('manage_options')) {
247 - return;
248 - }
249 -
250 - $options = get_option('mxchat_options', array());
251 - $selected = $options['embedding_model'] ?? '';
252 - $active = MxChat_Utils::get_active_embedding_model();
253 -
254 - if (empty($active) || empty($selected) || $active === $selected) {
255 - return;
256 - }
257 -
258 - $dismissed = get_option('mxchat_dismissed_embedding_mismatch', '');
259 - if ($dismissed === $active . '|' . $selected) {
260 - return;
261 - }
262 -
263 - $active_label = MxChat_Utils::embedding_model_label($active);
264 - $selected_label = MxChat_Utils::embedding_model_label($selected);
265 - $active_dims = MxChat_Utils::embedding_model_dimensions($active);
266 - $selected_dims = MxChat_Utils::embedding_model_dimensions($selected);
267 - $dims_differ = ($active_dims > 0 && $selected_dims > 0 && $active_dims !== $selected_dims);
268 -
269 - $kb_url = admin_url('admin.php?page=mxchat-prompts');
270 - $actions_url = admin_url('admin.php?page=mxchat-actions');
271 -
272 - ?>
273 - <div class="notice notice-error is-dismissible mxchat-embedding-mismatch-notice"
274 - data-active="<?php echo esc_attr($active); ?>"
275 - data-selected="<?php echo esc_attr($selected); ?>">
276 - <p><strong><?php esc_html_e('MxChat: Embedding model mismatch detected', 'mxchat'); ?></strong></p>
277 - <p>
278 - <?php
279 - printf(
280 - /* translators: 1: previously-used model name, 2: currently-selected model name */
281 - esc_html__('Your knowledge base and actions were embedded with %1$s, but %2$s is now selected. Similarity matching will return inaccurate or empty results until you delete all existing embeddings and re-embed your content with the new model.', 'mxchat'),
282 - '<code>' . esc_html($active_label) . '</code>',
283 - '<code>' . esc_html($selected_label) . '</code>'
284 - );
285 - ?>
286 - </p>
287 - <?php if ($dims_differ) : ?>
288 - <p>
289 - <strong><?php esc_html_e('Dimension mismatch:', 'mxchat'); ?></strong>
290 - <?php
291 - printf(
292 - /* translators: 1: old dim count, 2: new dim count */
293 - esc_html__('Existing vectors are %1$d-dimensional but the new model produces %2$d-dimensional vectors. If you use Pinecone, your index will reject queries entirely until re-embedded.', 'mxchat'),
294 - (int) $active_dims,
295 - (int) $selected_dims
296 - );
297 - ?>
298 - </p>
299 - <?php endif; ?>
300 - <p>
301 - <?php esc_html_e('To fix this:', 'mxchat'); ?>
302 - <a href="<?php echo esc_url($kb_url); ?>"><?php esc_html_e('Delete all knowledge base entries', 'mxchat'); ?></a> ·
303 - <a href="<?php echo esc_url($actions_url); ?>"><?php esc_html_e('Delete all actions', 'mxchat'); ?></a> ·
304 - <?php esc_html_e('then re-import / re-add them with the new model selected.', 'mxchat'); ?>
305 - </p>
306 - </div>
307 - <script>
308 - (function($){
309 - $(document).on('click', '.mxchat-embedding-mismatch-notice .notice-dismiss', function(){
310 - $.post(ajaxurl, {
311 - action: 'mxchat_dismiss_embedding_mismatch',
312 - security: '<?php echo esc_js(wp_create_nonce('mxchat_admin_nonce')); ?>'
313 - });
314 - });
315 - })(jQuery);
316 - </script>
317 - <?php
318 - }
319 -
320 -private function is_license_active() {
321 - $license_status = get_option('mxchat_license_status', 'inactive');
322 - return ($license_status === 'active');
323 -}
324 -
54 + // Initialize default options
325 55 private function initialize_default_options() {
326 56 $default_options = array(
327 57 'api_key' => '',
328 58 'xai_api_key' => '',
329 59 'claude_api_key' => '',
330 - 'deepseek_api_key' => '',
331 - 'voyage_api_key' => '',
332 - 'gemini_api_key' => '',
333 - 'enable_streaming_toggle' => 'off',
334 - 'enable_web_search' => 'off',
335 - 'embedding_model' => 'text-embedding-ada-002',
336 - 'system_prompt_instructions' => 'You are an AI Chatbot assistant for this website. Your main goal is to assist visitors with questions and provide helpful information. Here are your key guidelines:
337 -
338 - # Response Style - CRITICALLY IMPORTANT
339 - - MAXIMUM LENGTH: 1-3 short sentences per response
340 - - Ultra-concise: Get straight to the answer with no filler
341 - - No introductions like "Sure!" or "I\'d be happy to help"
342 - - No phrases like "based on my knowledge" or "according to information"
343 - - No explanatory text before giving the answer
344 - - No summaries or repetition
345 - - Hyperlink all URLs
346 - - Respond in user\'s language
347 - - Minor chit chat or conversation is okay, but try to keep it focused on [insert topic]
348 -
349 - # Knowledge Base Requirements - PREVENT HALLUCINATIONS
350 - - ONLY answer questions using information explicitly provided in OFFICIAL KNOWLEDGE DATABASE CONTENT sections marked with ===== delimiters
351 - - If required information is NOT in the knowledge database: "I don\'t have enough information in my knowledge base to answer that question accurately."
352 - - NEVER invent or hallucinate URLs, links, product specs, procedures, dates, statistics, names, contacts, or company information
353 - - When knowledge base information is unclear or contradictory, acknowledge the limitation rather than guessing
354 - - Better to admit insufficient information than provide inaccurate answers',
355 - 'model' => esc_html__('gpt-5.6-sol', 'mxchat'),
356 - 'rate_limit_logged_out' => esc_html__('100', 'mxchat'),
357 - 'role_rate_limits' => array(),
358 - 'rate_limit_message' => esc_html__('Rate limit exceeded. Please try again later.', 'mxchat'),
60 + '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:
61 + - Your name is [Chatbot Name].
62 + - 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.
63 + - Do not make up links to pages. Only use specific links you see in your knowledgebase.
64 + - Keep your responses short, concise, and to the point. Provide clear and direct answers suitable for a chatbot interaction.
65 + - 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.',
66 + 'model' => 'gpt-4o',
67 + 'rate_limit_logged_in' => '100',
68 + 'rate_limit_logged_out' => '100',
69 + 'rate_limit_message' => 'Rate limit exceeded. Please try again later.',
359 70 'enable_email_block' => '',
360 - 'email_blocker_header_content' => __("<h2>Welcome to Our Chat!</h2>\n<p>Let's get started. Enter your email to begin chatting with us.</p>", 'mxchat'),
361 - 'email_blocker_button_text' => esc_html__('Start Chat', 'mxchat'),
362 - 'enable_name_field' => 'off', // NEW
363 - 'name_field_placeholder' => esc_html__('Enter your name', 'mxchat'), // NEW
364 - 'top_bar_title' => esc_html__('MxChat', 'mxchat'),
365 - 'intro_message' => __('Hello! How can I assist you today?', 'mxchat'),
366 - 'ai_agent_text' => esc_html__('AI Agent', 'mxchat'),
367 - 'input_copy' => esc_html__('How can I assist?', 'mxchat'),
368 - 'append_to_body' => esc_html__('off', 'mxchat'),
369 - 'post_type_visibility_mode' => 'all', // 'all', 'include', 'exclude'
370 - 'post_type_visibility_list' => array(), // Array of post type slugs
371 - 'contextual_awareness_toggle' => 'off',
372 - 'citation_links_toggle' => 'on',
373 - 'satisfaction_rating_enabled' => 'off',
374 - 'satisfaction_rating_idle_seconds' => 60,
375 - 'satisfaction_rating_question' => '',
376 - 'satisfaction_rating_thanks' => '',
377 - 'satisfaction_rating_placeholder' => '',
378 - 'satisfaction_rating_saved' => '',
379 - 'close_button_color' => esc_html__('#fff', 'mxchat'),
380 - 'chatbot_bg_color' => esc_html__('#fff', 'mxchat'),
381 - 'user_message_bg_color' => esc_html__('#fff', 'mxchat'),
382 - 'user_message_font_color' => esc_html__('#212121', 'mxchat'),
383 - 'bot_message_bg_color' => esc_html__('#212121', 'mxchat'),
384 - 'bot_message_font_color' => esc_html__('#fff', 'mxchat'),
385 - 'top_bar_bg_color' => esc_html__('#212121', 'mxchat'),
386 - 'send_button_font_color' => esc_html__('#212121', 'mxchat'),
387 - 'chat_input_font_color' => esc_html__('#212121', 'mxchat'),
388 - 'chatbot_background_color' => esc_html__('#212121', 'mxchat'),
389 - 'icon_color' => esc_html__('#fff', 'mxchat'),
390 - 'enable_woocommerce_integration' => esc_html__('0', 'mxchat'),
391 - 'link_target_toggle' => esc_html__('off', 'mxchat'),
392 - 'pre_chat_message' => esc_html__('Hey there! Ask me anything!', 'mxchat'),
71 + 'email_blocker_header_content' => "<h2>Welcome to Our Chat!</h2>\n<p>Let's get started. Enter your email to begin chatting with us.</p>",
72 + 'email_blocker_button_text' => 'Start Chat',
73 + 'top_bar_title' => 'MxChat',
74 + 'intro_message' => 'Hello! How can I assist you today?',
75 + 'input_copy' => 'How can I assist?',
76 + 'append_to_body' => 'off',
77 + 'close_button_color' => '#fff',
78 + 'chatbot_bg_color' => '#fff',
79 + 'user_message_bg_color' => '#fff',
80 + 'user_message_font_color' => '#212121',
81 + 'bot_message_bg_color' => '#212121',
82 + 'bot_message_font_color' => '#fff',
83 + 'top_bar_bg_color' => '#212121',
84 + 'send_button_font_color' => '#212121',
85 + 'chat_input_font_color' => '#212121',
86 + 'chatbot_background_color' => '#212121',
87 + 'icon_color' => '#fff',
88 + 'enable_woocommerce_integration' => '0',
89 + 'link_target_toggle' => 'off',
90 + 'pre_chat_message' => 'Hey there! Ask me anything!',
393 91
394 92 // New fields for Loops Integration
395 93 'loops_api_key' => '',
396 94 'loops_mailing_list' => '',
397 - 'triggered_phrase_response' => __('Would you like to join our mailing list? Please provide your email below.', 'mxchat'),
398 - 'email_capture_response' => __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat'),
95 + 'triggered_phrase_response' => 'Would you like to join our mailing list? Please provide your email below.',
96 + 'email_capture_response' => 'Thank you for providing your email! You\'ve been added to our list.',
399 97 'popular_question_1' => '',
400 98 'popular_question_2' => '',
401 99 'popular_question_3' => '',
402 - 'pdf_intent_trigger_text' => __("Please provide the URL to the PDF you'd like to discuss.", 'mxchat'),
403 - 'pdf_intent_success_text' => __("I've processed the PDF. What questions do you have about it?", 'mxchat'),
404 - 'pdf_intent_error_text' => __("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat'),
100 + 'pdf_intent_trigger_text' => "Please provide the URL to the PDF you'd like to discuss.",
101 + 'pdf_intent_success_text' => "I've processed the PDF. What questions do you have about it?",
102 + 'pdf_intent_error_text' => "Sorry, I couldn't process the PDF. Please ensure it's a valid file.",
405 103 'pdf_max_pages' => 69,
406 - 'show_pdf_upload_button' => 'on',
407 - 'show_word_upload_button' => 'on',
408 104
409 - // Live Agent Integration (Slack)
105 + // Live Agent Integration
410 106 'live_agent_webhook_url' => '',
411 107 'live_agent_secret_key' => '',
412 108 'live_agent_bot_token' => '',
413 - 'live_agent_message_bg_color' => esc_html__('#ffffff', 'mxchat'),
414 - 'live_agent_message_font_color' => esc_html__('#333333', 'mxchat'),
415 -
416 - // Telegram Integration
417 - 'telegram_status' => 'off',
418 - 'telegram_bot_token' => '',
419 - 'telegram_group_id' => '',
420 - 'telegram_webhook_secret' => '',
421 - 'telegram_notification_message' => __("I've notified a support agent. Please allow a moment for them to respond. If you'd like to continue with AI, just type \"Switch to AI\" at any time.", 'mxchat'),
422 - 'telegram_away_message' => __("I just checked, and it looks like our support team isn't personally available at the moment. If you'd like, you can leave your email address, and they'll get back to you as soon as possible. In the meantime, you can keep chatting with me — just let me know how I can help!", 'mxchat'),
423 -
424 - 'chat_toolbar_toggle' => esc_html__('off', 'mxchat'),
425 - 'mode_indicator_bg_color' => esc_html__('#767676', 'mxchat'),
426 - 'mode_indicator_font_color' => esc_html__('#ffffff', 'mxchat'),
427 - 'toolbar_icon_color' => esc_html__('#212121', 'mxchat'),
428 -
429 - // Optimization settings
430 - 'script_loading_strategy' => 'default',
431 -
432 - // Debug settings
433 - 'debug_mode' => 'off',
109 + 'live_agent_message_bg_color' => '#ffffff',
110 + 'live_agent_message_font_color' => '#333333',
111 + 'chat_toolbar_toggle' => 'off',
112 + 'mode_indicator_bg_color' => '#767676',
113 + 'mode_indicator_font_color' => '#ffffff',
114 + 'toolbar_icon_color' => '#212121',
434 115 );
435 116
436 117
437 118 // Merge existing options with defaults
@@ -442,683 +123,134 @@
442 123 if ($existing_options !== $merged_options) {
443 124 update_option('mxchat_options', $merged_options);
444 125 }
445 126
446 - // Add default limits for each role
447 - $roles = wp_roles()->get_names();
448 - foreach ($roles as $role_id => $role_name) {
449 - $default_options['role_rate_limits'][$role_id] = esc_html__('100', 'mxchat');
450 - }
451 -
452 - return $default_options;
453 -
454 127 // Update the $this->options property
455 128 $this->options = $merged_options;
456 129 }
457 130
458 -public function mxchat_add_plugin_page() {
459 - // Onboarding lifecycle helpers (admin_init redirect + ajax handlers live in the file).
460 - require_once plugin_dir_path(__FILE__) . 'admin-onboarding-page.php';
461 131
462 - // Main menu page — `mxchat-max` remains the parent slug for every MxChat submenu
463 - // (Settings, Knowledge, Transcripts, …). Hitting `?page=mxchat-max` directly now
464 - // dispatches to the Onboarding page (or Settings if the user has dismissed onboarding).
465 - add_menu_page(
466 - esc_html__('MxChat', 'mxchat'),
467 - esc_html__('MxChat', 'mxchat'),
468 - 'manage_options',
469 - 'mxchat-max',
470 - array($this, 'mxchat_create_dashboard_page'),
471 - 'dashicons-testimonial',
472 - 6
473 - );
474 132
475 - // Onboarding submenu — first child under MxChat (plan-d14e89).
476 - // First registration uses menu_slug === parent slug 'mxchat-max' →
477 - // WP-canonical override of the auto-duplicate "MxChat" entry. Result: the
478 - // first child shows as "Onboarding" instead of a redundant pair.
479 - add_submenu_page(
480 - 'mxchat-max',
481 - esc_html__('MxChat Onboarding', 'mxchat'),
482 - esc_html__('Onboarding', 'mxchat'),
483 - 'manage_options',
484 - 'mxchat-max',
485 - array($this, 'mxchat_create_dashboard_page')
486 - );
487 - // Hidden route for `?page=mxchat-onboarding` (Settings "Show again" link
488 - // + legacy redirects still target this slug). Parent === null keeps it
489 - // out of the menu while remaining accessible by URL.
490 - add_submenu_page(
491 - null,
492 - esc_html__('MxChat Onboarding', 'mxchat'),
493 - esc_html__('Onboarding', 'mxchat'),
494 - 'manage_options',
495 - 'mxchat-onboarding',
496 - array($this, 'mxchat_create_dashboard_page')
497 - );
133 + public function mxchat_add_plugin_page() {
134 + // Main menu page
135 + add_menu_page(
136 + 'MxChat Settings',
137 + 'MxChat',
138 + 'manage_options',
139 + 'mxchat-max',
140 + array($this, 'mxchat_create_admin_page'),
141 + 'dashicons-testimonial',
142 + 6
143 + );
498 144
499 - // Settings submenu — same callback as before, just at a new slug.
500 - add_submenu_page(
501 - 'mxchat-max',
502 - esc_html__('MxChat Settings', 'mxchat'),
503 - esc_html__('Settings', 'mxchat'),
504 - 'manage_options',
505 - 'mxchat-settings',
506 - array($this, 'mxchat_create_admin_page')
507 - );
145 + // Submenu page for Knowledge
146 + add_submenu_page(
147 + 'mxchat-max',
148 + 'Prompts',
149 + 'Knowledge',
150 + 'manage_options',
151 + 'mxchat-prompts',
152 + array($this, 'mxchat_create_prompts_page')
153 + );
508 154
509 - // Submenu page for Knowledge
510 - add_submenu_page(
511 - 'mxchat-max',
512 - esc_html__('Prompts', 'mxchat'),
513 - esc_html__('Knowledge', 'mxchat'),
514 - 'manage_options',
515 - 'mxchat-prompts',
516 - array($this, 'mxchat_create_prompts_page')
517 - );
155 + // Submenu page for Chat Transcripts
156 + add_submenu_page(
157 + 'mxchat-max', // Corrected parent slug to match the main menu
158 + 'Chat Transcripts',
159 + 'Transcripts',
160 + 'manage_options',
161 + 'mxchat-transcripts',
162 + array($this, 'mxchat_create_transcripts_page') // Prefixed function name with mxchat_
163 + );
518 164
165 + // Submenu page for Intents
519 166 add_submenu_page(
520 - 'mxchat-max',
521 - esc_html__('Chat Transcripts', 'mxchat'),
522 - esc_html__('Transcripts', 'mxchat'),
523 - 'manage_options',
524 - 'mxchat-transcripts',
525 - array($this, 'mxchat_create_transcripts_page')
167 + 'mxchat-max', // Parent slug
168 + 'MxChat Intents', // Page title
169 + 'Intents', // Menu title
170 + 'manage_options', // Capability
171 + 'mxchat-intents', // Menu slug
172 + array($this, 'mxchat_intents_page_html') // Callback function
526 173 );
527 174
528 - add_submenu_page(
529 - 'mxchat-max',
530 - esc_html__('MxChat Actions', 'mxchat'),
531 - esc_html__('Actions', 'mxchat'),
532 - 'manage_options',
533 - 'mxchat-actions',
534 - array($this, 'mxchat_actions_page_html')
535 - );
175 + // Submenu page for Activation Key
176 + add_submenu_page(
177 + 'mxchat-max',
178 + 'Pro Upgrade',
179 + 'Pro Upgrade',
180 + 'manage_options',
181 + 'mxchat-activation',
182 + array($this, 'mxchat_create_activation_page')
183 + );
536 184
537 - // Content Generator page
538 - add_submenu_page(
539 - 'mxchat-max',
540 - esc_html__('Content', 'mxchat'),
541 - esc_html__('Content', 'mxchat'),
542 - 'manage_options',
543 - 'mxchat-content',
544 - array($this, 'mxchat_create_content_page')
545 - );
546 185
547 186 }
548 187
549 -/**
550 - * Register the Pro & Extensions submenu on a later admin_menu priority so it
551 - * always renders as the bottom-most item in the MxChat sidebar — below
552 - * configuration pages like API Access (priority 20).
553 - */
554 -public function mxchat_add_pro_extensions_page() {
555 - add_submenu_page(
556 - 'mxchat-max',
557 - esc_html__('Pro & Extensions', 'mxchat'),
558 - esc_html__('Pro & Extensions', 'mxchat'),
559 - 'manage_options',
560 - 'mxchat-activation',
561 - array($this, 'mxchat_create_activation_page')
562 - );
563 -}
564 188
565 -public function mxchat_create_addons_page() {
566 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-addons.php';
567 - $addons_page = new MxChat_Addons();
568 - $addons_page->render_page();
569 -}
570 -
571 -/**
572 - * Render the Content Generator admin page
573 - */
574 -public function mxchat_create_content_page() {
575 - require_once plugin_dir_path(__FILE__) . 'admin-content-page.php';
576 - mxchat_render_content_page($this);
577 -}
578 -
579 -/**
580 - * Test actual streaming functionality in the WordPress environment
581 - */
582 -public function mxchat_handle_test_streaming_actual() {
583 - check_ajax_referer('mxchat_test_streaming_nonce', 'nonce');
584 -
585 - // A nonce is not authorization (plan-mxchat-20260731-c63fb6). This handler
586 - // reads the site's provider keys and spends them on an outbound call, so it
587 - // is billable abuse for any logged-in user who obtained the nonce.
588 - if (!current_user_can('manage_options')) {
589 - wp_send_json_error(['message' => esc_html__('Unauthorized', 'mxchat')], 403);
590 - }
591 -
592 - // Check if headers have already been sent
593 - if (headers_sent()) {
594 - wp_send_json_error(['message' => 'Headers already sent - streaming not possible']);
189 +public function mxchat_handle_content_submission() {
190 + // Check if the form was submitted and the user has sufficient permissions
191 + if (!isset($_POST['submit_content']) || !current_user_can('manage_options')) {
595 192 return;
596 193 }
597 194
598 - // Check for required functions
599 - if (!function_exists('curl_init')) {
600 - wp_send_json_error(['message' => 'cURL not available - streaming requires cURL']);
601 - return;
195 + // Verify the nonce field for security
196 + $nonce = isset($_POST['mxchat_submit_content_nonce']) ? sanitize_text_field(wp_unslash($_POST['mxchat_submit_content_nonce'])) : '';
197 + if (!wp_verify_nonce($nonce, 'mxchat_submit_content_action')) {
198 + wp_die('Nonce verification failed.');
602 199 }
603 200
604 - // Get user's selected model and API key
605 - $options = get_option('mxchat_options', []);
606 - $selected_model = $options['model'] ?? 'gpt-5.6-sol';
201 + // Sanitize the content input
202 + $article_content = sanitize_textarea_field($_POST['article_content']);
607 203
608 - // Get the provider from the model
609 - $model_parts = explode('-', $selected_model);
610 - $provider = strtolower($model_parts[0]);
204 + // Sanitize the URL input (optional URL)
205 + $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : ''; // Default to empty if not provided
611 206
612 - // Get the appropriate API key
613 - $api_key = '';
614 - switch ($provider) {
615 - case 'gpt':
616 - case 'o1':
617 - $api_key = $options['api_key'] ?? '';
618 - break;
619 - case 'claude':
620 - $api_key = $options['claude_api_key'] ?? '';
621 - break;
622 - case 'grok':
623 - $api_key = $options['xai_api_key'] ?? '';
624 - break;
625 - case 'deepseek':
626 - $api_key = $options['deepseek_api_key'] ?? '';
627 - break;
628 - case 'gemini':
629 - $api_key = $options['gemini_api_key'] ?? '';
630 - break;
631 - default:
632 - // Default to OpenAI for unknown models
633 - $api_key = $options['api_key'] ?? '';
634 - $provider = 'gpt';
635 - break;
636 - }
207 + // Generate the embedding vector for the content
208 + $embedding_vector = $this->mxchat_generate_embedding($article_content);
637 209
638 - if (empty($api_key)) {
639 - wp_send_json_error(['message' => "API key not configured for {$provider} provider"]);
640 - return;
641 - }
210 + global $wpdb;
211 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
642 212
643 - // Test streaming with the selected model and provider
644 - try {
645 - $this->perform_streaming_test($provider, $selected_model, $api_key);
646 - } catch (Exception $e) {
647 - wp_send_json_error(['message' => 'Streaming test exception: ' . $e->getMessage()]);
213 + // Check if the 'source_url' column exists in the table and add it if it doesn't
214 + if ($wpdb->get_var($wpdb->prepare("SHOW COLUMNS FROM {$table_name} LIKE %s", 'source_url')) != 'source_url') {
215 + // Use wpdb::query and a prepared statement to avoid SQL injection
216 + $wpdb->query($wpdb->prepare("ALTER TABLE {$table_name} ADD source_url VARCHAR(255) DEFAULT ''"));
648 217 }
649 -}
650 218
651 -/**
652 - * Perform the actual streaming test
653 - */
654 -private function perform_streaming_test($provider, $model, $api_key) {
655 - // Set streaming headers
656 - header('Content-Type: text/event-stream');
657 - header('Cache-Control: no-cache');
658 - header('X-Accel-Buffering: no'); // Disable nginx buffering
219 + if (is_array($embedding_vector)) {
220 + // Serialize the embedding vector before storing it
221 + $embedding_vector_serialized = serialize($embedding_vector);
659 222
660 - // Prepare test message
661 - $test_message = "Please respond with exactly: 'Streaming test successful!' - send this as a short response for testing.";
662 -
663 - // Configure API request based on provider
664 - $url = '';
665 - $headers = [];
666 - $body = [];
667 -
668 - switch ($provider) {
669 - case 'gpt':
670 - case 'o1':
671 - $url = 'https://api.openai.com/v1/chat/completions';
672 - $headers = [
673 - 'Content-Type: application/json',
674 - 'Authorization: Bearer ' . $api_key
675 - ];
676 - $body = [
677 - 'model' => $model,
678 - 'messages' => [['role' => 'user', 'content' => $test_message]],
679 - 'max_tokens' => 50,
680 - 'temperature' => 0.3,
681 - 'stream' => true
682 - ];
683 - break;
684 -
685 - case 'claude':
686 - $url = 'https://api.anthropic.com/v1/messages';
687 - $headers = [
688 - 'Content-Type: application/json',
689 - 'x-api-key: ' . $api_key,
690 - 'anthropic-version: 2023-06-01'
691 - ];
692 - $body = [
693 - 'model' => $model,
694 - 'messages' => [['role' => 'user', 'content' => $test_message]],
695 - 'max_tokens' => 50,
696 - 'temperature' => 0.3,
697 - 'stream' => true
698 - ];
699 - // Claude flagships from Opus 4.7 onward reject the temperature
700 - // param outright (400). Reuse the catalog's decision — this path
701 - // previously sent temperature unconditionally, so the streaming
702 - // test was broken for Opus 4.7/4.8, Fable 5 and Sonnet 5.
703 - if (class_exists('MxChat_Model_Catalog')
704 - && method_exists('MxChat_Model_Catalog', 'supports_temperature')
705 - && !MxChat_Model_Catalog::supports_temperature($model)) {
706 - unset($body['temperature']);
707 - }
708 - break;
709 -
710 - case 'grok':
711 - $url = 'https://api.x.ai/v1/chat/completions';
712 - $headers = [
713 - 'Content-Type: application/json',
714 - 'Authorization: Bearer ' . $api_key
715 - ];
716 - $body = [
717 - 'model' => $model,
718 - 'messages' => [['role' => 'user', 'content' => $test_message]],
719 - 'max_tokens' => 50,
720 - 'temperature' => 0.3,
721 - 'stream' => true
722 - ];
723 - break;
724 -
725 - case 'deepseek':
726 - $url = 'https://api.deepseek.com/v1/chat/completions';
727 - $headers = [
728 - 'Content-Type: application/json',
729 - 'Authorization: Bearer ' . $api_key
730 - ];
731 - $body = [
732 - 'model' => $model,
733 - 'messages' => [['role' => 'user', 'content' => $test_message]],
734 - 'max_tokens' => 50,
735 - 'temperature' => 0.3,
736 - 'stream' => true,
737 - // DeepSeek V4 defaults to thinking mode ON — reasoning would
738 - // consume the 50-token test budget and return no visible text.
739 - 'thinking' => ['type' => 'disabled']
740 - ];
741 - break;
742 -
743 - default:
744 - echo "data: " . json_encode(['error' => 'Unsupported provider for streaming test: ' . $provider]) . "\n\n";
745 - flush();
746 - return;
747 - }
748 -
749 - // Initialize cURL
750 - $ch = curl_init();
751 - curl_setopt($ch, CURLOPT_URL, $url);
752 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
753 - curl_setopt($ch, CURLOPT_POST, true);
754 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
755 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
756 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
757 - curl_setopt($ch, CURLOPT_TIMEOUT, 30);
758 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use ($provider) {
759 - return $this->process_streaming_test_data($data, $provider);
760 - });
761 -
762 - // Send initial test message
763 - echo "data: " . json_encode(['content' => '[Starting streaming test...]']) . "\n\n";
764 - flush();
765 -
766 - $result = curl_exec($ch);
767 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
768 - $curl_error = curl_error($ch);
769 - curl_close($ch);
770 -
771 - if ($curl_error) {
772 - echo "data: " . json_encode(['error' => 'cURL Error: ' . $curl_error]) . "\n\n";
773 - flush();
774 - return;
775 - }
776 -
777 - if ($http_code !== 200) {
778 - echo "data: " . json_encode(['error' => 'API returned HTTP ' . $http_code]) . "\n\n";
779 - flush();
780 - return;
781 - }
782 -
783 - // Send completion signal
784 - echo "data: [DONE]\n\n";
785 - flush();
786 -}
787 -
788 -/**
789 - * Process streaming data for the test
790 - */
791 -private function process_streaming_test_data($data, $provider) {
792 - static $chunk_count = 0;
793 -
794 - $lines = explode("\n", $data);
795 -
796 - foreach ($lines as $line) {
797 - if (trim($line) === '') {
798 - continue;
799 - }
800 -
801 - // Handle different provider formats
802 - if ($provider === 'claude') {
803 - // Claude uses event: and data: format
804 - if (strpos($line, 'data: ') === 0) {
805 - $json_str = substr($line, 6);
806 - $json = json_decode($json_str, true);
807 -
808 - if (isset($json['type']) && $json['type'] === 'content_block_delta') {
809 - if (isset($json['delta']['text'])) {
810 - $chunk_count++;
811 - echo "data: " . json_encode([
812 - 'content' => $json['delta']['text'],
813 - 'test_chunk' => $chunk_count
814 - ]) . "\n\n";
815 - flush();
816 - }
817 - }
818 - }
819 - } else {
820 - // OpenAI, X.AI, DeepSeek format
821 - if (strpos($line, 'data: ') === 0) {
822 - $json_str = substr($line, 6);
823 -
824 - if ($json_str === '[DONE]') {
825 - // Don't echo [DONE] here, let the main function handle it
826 - continue;
827 - }
828 -
829 - $json = json_decode($json_str, true);
830 - if (isset($json['choices'][0]['delta']['content'])) {
831 - $chunk_count++;
832 - echo "data: " . json_encode([
833 - 'content' => $json['choices'][0]['delta']['content'],
834 - 'test_chunk' => $chunk_count
835 - ]) . "\n\n";
836 - flush();
837 - }
838 - }
839 - }
840 - }
841 -
842 - return strlen($data);
843 -}
844 -
845 -/**
846 - * Updated version of your existing test method (keep this as a fallback)
847 - */
848 -public function mxchat_handle_test_streaming() {
849 - check_ajax_referer('mxchat_test_streaming_nonce', 'nonce');
850 -
851 - // plan-mxchat-20260731-c63fb6 — the delegate below checks this too, but
852 - // fail here so the alias never becomes a bypass if that call is refactored.
853 - if (!current_user_can('manage_options')) {
854 - wp_send_json_error(['message' => esc_html__('Unauthorized', 'mxchat')], 403);
855 - }
856 -
857 - // Use the actual streaming test instead
858 - $this->mxchat_handle_test_streaming_actual();
859 -}
860 -
861 -
862 -public function register_pinecone_settings() {
863 - register_setting(
864 - 'mxchat_pinecone_addon_options',
865 - 'mxchat_pinecone_addon_options',
866 - array(
867 - 'type' => 'array',
868 - 'sanitize_callback' => array($this, 'sanitize_pinecone_settings'),
869 - 'default' => array(
870 - 'mxchat_use_pinecone' => '0',
871 - 'mxchat_pinecone_api_key' => '',
872 - 'mxchat_pinecone_host' => '',
873 - 'mxchat_pinecone_index' => '',
874 - 'mxchat_pinecone_environment' => ''
875 - )
876 - )
877 - );
878 -}
879 -
880 -public function sanitize_pinecone_settings($input) {
881 - $sanitized = array();
882 -
883 - $sanitized['mxchat_use_pinecone'] = isset($input['mxchat_use_pinecone']) ? '1' : '0';
884 - $sanitized['mxchat_pinecone_api_key'] = sanitize_text_field($input['mxchat_pinecone_api_key'] ?? '');
885 - $sanitized['mxchat_pinecone_host'] = sanitize_text_field($input['mxchat_pinecone_host'] ?? '');
886 - $sanitized['mxchat_pinecone_index'] = sanitize_text_field($input['mxchat_pinecone_index'] ?? '');
887 - $sanitized['mxchat_pinecone_environment'] = sanitize_text_field($input['mxchat_pinecone_environment'] ?? '');
888 -
889 - // Remove https:// from host if present
890 - $sanitized['mxchat_pinecone_host'] = str_replace(['https://', 'http://'], '', $sanitized['mxchat_pinecone_host']);
891 -
892 - return $sanitized;
893 -}
894 -
895 -public function register_openai_vectorstore_settings() {
896 - register_setting(
897 - 'mxchat_openai_vectorstore_options',
898 - 'mxchat_openai_vectorstore_options',
899 - array(
900 - 'type' => 'array',
901 - 'sanitize_callback' => array($this, 'sanitize_openai_vectorstore_settings'),
902 - 'default' => array(
903 - 'mxchat_use_openai_vectorstore' => '0',
904 - 'mxchat_vectorstore_ids' => '',
905 - 'mxchat_vectorstore_max_results' => 5
906 - )
907 - )
908 - );
909 -}
910 -
911 -public function sanitize_openai_vectorstore_settings($input) {
912 - $sanitized = array();
913 -
914 - $sanitized['mxchat_use_openai_vectorstore'] = isset($input['mxchat_use_openai_vectorstore']) ? '1' : '0';
915 - $sanitized['mxchat_vectorstore_ids'] = sanitize_text_field($input['mxchat_vectorstore_ids'] ?? '');
916 - $sanitized['mxchat_vectorstore_max_results'] = absint($input['mxchat_vectorstore_max_results'] ?? 5);
917 -
918 - // Ensure max results is within reasonable range
919 - if ($sanitized['mxchat_vectorstore_max_results'] < 1) {
920 - $sanitized['mxchat_vectorstore_max_results'] = 1;
921 - }
922 - if ($sanitized['mxchat_vectorstore_max_results'] > 20) {
923 - $sanitized['mxchat_vectorstore_max_results'] = 20;
924 - }
925 -
926 - return $sanitized;
927 -}
928 -
929 -/**
930 - * AJAX handler to test OpenAI Vector Store connection
931 - */
932 -public function mxchat_test_vectorstore_connection() {
933 - check_ajax_referer('mxchat_admin_nonce', 'nonce');
934 -
935 - if (!current_user_can('manage_options')) {
936 - wp_send_json_error(array('message' => __('Permission denied.', 'mxchat')));
937 - return;
938 - }
939 -
940 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
941 - $vectorstore_ids = $vectorstore_options['mxchat_vectorstore_ids'] ?? '';
942 - $mxchat_options = get_option('mxchat_options', array());
943 - $api_key = $mxchat_options['api_key'] ?? '';
944 -
945 - if (empty($api_key)) {
946 - wp_send_json_error(array('message' => __('OpenAI API key is not configured.', 'mxchat')));
947 - return;
948 - }
949 -
950 - if (empty($vectorstore_ids)) {
951 - wp_send_json_error(array('message' => __('No Vector Store ID configured.', 'mxchat')));
952 - return;
953 - }
954 -
955 - // Get the first Vector Store ID for testing
956 - $ids_array = array_map('trim', explode(',', $vectorstore_ids));
957 - $test_id = $ids_array[0];
958 -
959 - // Test by retrieving the Vector Store info
960 - $response = wp_remote_get(
961 - 'https://api.openai.com/v1/vector_stores/' . $test_id,
962 - array(
963 - 'headers' => array(
964 - 'Authorization' => 'Bearer ' . $api_key,
965 - 'Content-Type' => 'application/json',
966 - 'OpenAI-Beta' => 'assistants=v2'
223 + // Insert the content, embedding vector, and source URL into the database, using a prepared statement
224 + $inserted = $wpdb->insert(
225 + $table_name,
226 + array(
227 + 'article_content' => $article_content,
228 + 'embedding_vector' => $embedding_vector_serialized,
229 + 'source_url' => $article_url, // Insert the URL, empty string if not provided
967 230 ),
968 - 'timeout' => 30
969 - )
970 - );
971 -
972 - if (is_wp_error($response)) {
973 - wp_send_json_error(array('message' => __('Connection failed: ', 'mxchat') . $response->get_error_message()));
974 - return;
975 - }
976 -
977 - $status_code = wp_remote_retrieve_response_code($response);
978 - $body = json_decode(wp_remote_retrieve_body($response), true);
979 -
980 - if ($status_code === 200 && isset($body['id'])) {
981 - $file_count = $body['file_counts']['completed'] ?? 0;
982 - $name = $body['name'] ?? $test_id;
983 - wp_send_json_success(array(
984 - 'message' => sprintf(
985 - __('Connected successfully! Vector Store: %s (%d files)', 'mxchat'),
986 - esc_html($name),
987 - $file_count
231 + array(
232 + '%s', // Format for article_content (string)
233 + '%s', // Format for embedding_vector (serialized string)
234 + '%s', // Format for source_url (string)
988 235 )
989 - ));
990 - } elseif ($status_code === 404) {
991 - wp_send_json_error(array('message' => __('Vector Store not found. Please check the ID.', 'mxchat')));
992 - } elseif ($status_code === 401) {
993 - wp_send_json_error(array('message' => __('Invalid API key.', 'mxchat')));
994 - } else {
995 - $error_message = $body['error']['message'] ?? __('Unknown error occurred.', 'mxchat');
996 - wp_send_json_error(array('message' => $error_message));
997 - }
998 -}
999 -
1000 -/**
1001 - * AJAX handler to test Slack connection and validate scopes
1002 - */
1003 -public function mxchat_test_slack_connection() {
1004 - check_ajax_referer('mxchat_admin_nonce', 'nonce');
1005 -
1006 - if (!current_user_can('manage_options')) {
1007 - wp_send_json_error(array('message' => __('Permission denied.', 'mxchat')));
1008 - return;
1009 - }
1010 -
1011 - $bot_token = $this->options['live_agent_bot_token'] ?? '';
1012 -
1013 - if (empty($bot_token)) {
1014 - wp_send_json_error(array('message' => __('Slack Bot Token is not configured. Please enter your token and save settings first.', 'mxchat')));
1015 - return;
1016 - }
1017 -
1018 - // Test 1: Validate bot token with auth.test
1019 - $auth_response = wp_remote_post('https://slack.com/api/auth.test', array(
1020 - 'headers' => array(
1021 - 'Authorization' => 'Bearer ' . $bot_token,
1022 - 'Content-Type' => 'application/json'
1023 - ),
1024 - 'timeout' => 15
1025 - ));
1026 -
1027 - if (is_wp_error($auth_response)) {
1028 - wp_send_json_error(array('message' => __('Connection failed: ', 'mxchat') . $auth_response->get_error_message()));
1029 - return;
1030 - }
1031 -
1032 - $auth_body = json_decode(wp_remote_retrieve_body($auth_response), true);
1033 -
1034 - if (!isset($auth_body['ok']) || !$auth_body['ok']) {
1035 - $error = $auth_body['error'] ?? 'unknown_error';
1036 - $error_messages = array(
1037 - 'invalid_auth' => __('Invalid bot token. Please check your token starts with xoxb-', 'mxchat'),
1038 - 'not_authed' => __('No authentication token provided.', 'mxchat'),
1039 - 'account_inactive' => __('The Slack workspace has been deactivated.', 'mxchat'),
1040 - 'token_revoked' => __('The bot token has been revoked. Please generate a new one.', 'mxchat'),
1041 236 );
1042 - $message = $error_messages[$error] ?? sprintf(__('Authentication failed: %s', 'mxchat'), $error);
1043 - wp_send_json_error(array('message' => $message));
1044 - return;
1045 - }
1046 237
1047 - $team_name = $auth_body['team'] ?? 'Unknown Workspace';
1048 - $bot_name = $auth_body['user'] ?? 'Unknown Bot';
238 + if ($inserted === false) {
239 + //error_log('Error inserting content: ' . $wpdb->last_error);
240 + set_transient('mxchat_admin_notice_error', 'Error inserting content into the database. Please try again.', 30);
241 + } else {
242 + set_transient('mxchat_admin_notice_success', 'Content successfully submitted!', 30);
243 + }
1049 244
1050 - // Test 2: Check if we can list channels (tests channels:read scope)
1051 - $channels_response = wp_remote_post('https://slack.com/api/conversations.list', array(
1052 - 'headers' => array(
1053 - 'Authorization' => 'Bearer ' . $bot_token,
1054 - 'Content-Type' => 'application/json'
1055 - ),
1056 - 'body' => json_encode(array('limit' => 1)),
1057 - 'timeout' => 15
1058 - ));
1059 -
1060 - $channels_body = json_decode(wp_remote_retrieve_body($channels_response), true);
1061 - $can_read_channels = isset($channels_body['ok']) && $channels_body['ok'];
1062 -
1063 - // Test 3: Check if we can create channels (tests channels:manage scope)
1064 - // We'll just check the error message without actually creating
1065 - $create_response = wp_remote_post('https://slack.com/api/conversations.create', array(
1066 - 'headers' => array(
1067 - 'Authorization' => 'Bearer ' . $bot_token,
1068 - 'Content-Type' => 'application/json'
1069 - ),
1070 - 'body' => json_encode(array('name' => 'mxchat-test-' . time(), 'is_private' => false)),
1071 - 'timeout' => 15
1072 - ));
1073 -
1074 - $create_body = json_decode(wp_remote_retrieve_body($create_response), true);
1075 -
1076 - // If channel was created, delete it immediately
1077 - if (isset($create_body['ok']) && $create_body['ok'] && isset($create_body['channel']['id'])) {
1078 - wp_remote_post('https://slack.com/api/conversations.archive', array(
1079 - 'headers' => array(
1080 - 'Authorization' => 'Bearer ' . $bot_token,
1081 - 'Content-Type' => 'application/json'
1082 - ),
1083 - 'body' => json_encode(array('channel' => $create_body['channel']['id'])),
1084 - 'timeout' => 15
1085 - ));
1086 - $can_create_channels = true;
1087 245 } else {
1088 - $create_error = $create_body['error'] ?? '';
1089 - // name_taken means we have permission but channel exists
1090 - $can_create_channels = ($create_error === 'name_taken' || (isset($create_body['ok']) && $create_body['ok']));
1091 -
1092 - // Check for missing scope errors
1093 - if ($create_error === 'missing_scope') {
1094 - $can_create_channels = false;
1095 - }
246 + //error_log('Embedding generation failed for article content: ' . $article_content);
247 + set_transient('mxchat_admin_notice_error', 'Embedding generation failed. Please ensure your API key is correct and try again.', 30);
1096 248 }
1097 249
1098 - // Build result message
1099 - $results = array();
1100 - $results[] = sprintf(__('Workspace: %s', 'mxchat'), esc_html($team_name));
1101 - $results[] = sprintf(__('Bot: %s', 'mxchat'), esc_html($bot_name));
1102 - $results[] = '';
1103 - $results[] = ($can_read_channels ? '✓' : '✗') . ' ' . __('channels:read - List channels', 'mxchat');
1104 - $results[] = ($can_create_channels ? '✓' : '✗') . ' ' . __('channels:manage - Create channels', 'mxchat');
1105 -
1106 - $missing_scopes = array();
1107 - if (!$can_read_channels) $missing_scopes[] = 'channels:read';
1108 - if (!$can_create_channels) $missing_scopes[] = 'channels:manage';
1109 -
1110 - if (!empty($missing_scopes)) {
1111 - wp_send_json_error(array(
1112 - 'message' => implode("\n", $results),
1113 - 'missing_scopes' => $missing_scopes,
1114 - 'partial' => true
1115 - ));
1116 - } else {
1117 - wp_send_json_success(array(
1118 - 'message' => implode("\n", $results)
1119 - ));
1120 - }
250 + // Redirect after setting the transient
251 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
252 + exit;
1121 253 }
1122 254
1123 255 public function mxchat_display_admin_notice() {
1124 256 // Success notice
@@ -1125,9 +257,9 @@
1125 257 if ($message = get_transient('mxchat_admin_notice_success')) {
1126 258 ?>
1127 259 <div class="notice notice-success is-dismissible">
1128 260 <p><?php echo esc_html($message); ?></p>
1129 - <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php echo esc_html__('Dismiss this notice.', 'mxchat'); ?></span></button>
261 + <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
1130 262 </div>
1131 263 <?php
1132 264 delete_transient('mxchat_admin_notice_success'); // Clear the transient after displaying
1133 265 }
@@ -1136,9 +268,9 @@
1136 268 if ($message = get_transient('mxchat_admin_notice_error')) {
1137 269 ?>
1138 270 <div class="notice notice-error is-dismissible">
1139 271 <p><?php echo esc_html($message); ?></p>
1140 - <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php echo esc_html__('Dismiss this notice.', 'mxchat'); ?></span></button>
272 + <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
1141 273 </div>
1142 274 <?php
1143 275 delete_transient('mxchat_admin_notice_error'); // Clear the transient after displaying
1144 276 }
@@ -1143,5016 +275,1405 @@
1143 275 delete_transient('mxchat_admin_notice_error'); // Clear the transient after displaying
1144 276 }
1145 277 }
1146 278
1147 -public function show_live_agent_disabled_banner() {
1148 - $show_disabled_notice = get_option('mxchat_show_live_agent_disabled_notice', false);
1149 279
1150 - if ($show_disabled_notice) {
1151 - ?>
1152 - <div class="mxchat-live-agent-disabled-notice" id="mxchat-disabled-notice">
1153 - <div class="mxchat-pro-notification">
1154 - <button type="button" class="mxchat-dismiss-btn" onclick="dismissLiveAgentNotice()" aria-label="Dismiss notification">
1155 - <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
1156 - <line x1="18" y1="6" x2="6" y2="18"></line>
1157 - <line x1="6" y1="6" x2="18" y2="18"></line>
1158 - </svg>
1159 - </button>
1160 - <div class="mxchat-live-agent-content">
1161 - <h3>🔧 Live Agent Integration Updated!</h3>
1162 - <p>We've temporarily disabled your Live Agent integration due to recent enhancements that have made it much better! You can easily turn it back on by going to <strong>Toolbar & Components → Live Agent Settings</strong> and reviewing the new configuration options.</p>
1163 - </div>
1164 - </div>
1165 - </div>
1166 - <?php
1167 - }
1168 -}
1169 -/**
1170 - * Render the Onboarding page. Delegates to the procedural renderer in
1171 - * includes/admin-onboarding-page.php. Wired to both `?page=mxchat-max`
1172 - * (legacy top-level URL) and `?page=mxchat-onboarding` (the canonical
1173 - * Onboarding submenu).
1174 - *
1175 - * When the user has dismissed onboarding and lands on `mxchat-max` (the
1176 - * legacy URL, since the Onboarding submenu has been removed), redirect to
1177 - * Settings instead — the page is "graduated" and we shouldn't dump them
1178 - * back onto it. They can still navigate here directly via the unhide link.
1179 - */
1180 -public function mxchat_create_dashboard_page() {
1181 - require_once plugin_dir_path(__FILE__) . 'admin-onboarding-page.php';
1182 280
1183 - $current = isset($_GET['page']) ? sanitize_key($_GET['page']) : '';
1184 - if ($current === 'mxchat-max' && function_exists('mxchat_onboarding_is_dismissed') && mxchat_onboarding_is_dismissed()) {
1185 - wp_safe_redirect(admin_url('admin.php?page=mxchat-settings'));
1186 - exit;
1187 - }
1188 281
1189 - if (function_exists('mxchat_render_onboarding_page')) {
1190 - mxchat_render_onboarding_page();
1191 - return;
1192 - }
1193 - // Defensive: if the include failed to load, fall back to the old Settings page
1194 - // so the top-level menu never lands on an empty screen.
1195 - $this->mxchat_create_admin_page();
1196 -}
1197 282
1198 -/**
1199 - * Hide the Onboarding submenu when the user has dismissed it (either
1200 - * manually or via auto-graduation). The page itself remains routable so
1201 - * the Settings "Show MxChat Onboarding again" link can navigate back to it.
1202 - */
1203 -public function mxchat_apply_onboarding_visibility() {
1204 - if (!function_exists('mxchat_onboarding_is_dismissed')) {
1205 - return;
1206 - }
1207 - if (mxchat_onboarding_is_dismissed()) {
1208 - // The first MxChat child is the same-slug-as-parent registration
1209 - // (slug 'mxchat-max', labelled "Onboarding") added in plan-d14e89.
1210 - // Remove it so the menu opens straight to Settings after dismiss.
1211 - remove_submenu_page('mxchat-max', 'mxchat-max');
1212 - }
1213 -}
1214 -
1215 283 public function mxchat_create_admin_page() {
1216 - $this->add_live_agent_nonce();
1217 - $this->add_theme_migration_nonce();
284 + ?>
285 + <div class="wrap mxchat-admin">
286 + <?php if (!$this->is_activated): ?>
287 + <div class="mxchat-pro-banner">
288 +<p>
289 + Get lifetime access to MxChat Pro for just $69.97! Act now - this will soon become an annual subscription.
290 + <a href="https://mxchat.ai/" target="_blank">Upgrade to Pro today!</a>
291 +</p>
1218 292
1219 - // Include and render the new sidebar-based settings page
1220 - require_once plugin_dir_path(__FILE__) . 'admin-settings-page.php';
1221 - mxchat_render_settings_page($this);
1222 -}
293 + </div>
1223 294
1224 -public function dismiss_live_agent_notice() {
1225 - // Add debugging
1226 - //error_log('dismiss_live_agent_notice called');
1227 - //error_log('POST data: ' . print_r($_POST, true));
295 + <?php endif; ?>
1228 296
1229 - // Verify nonce
1230 - if (!wp_verify_nonce($_POST['nonce'], 'dismiss_live_agent_notice')) {
1231 - //error_log('Nonce verification failed');
1232 - wp_die('Security check failed');
1233 - }
297 + <div class="mxchat-agents-banner">
298 +<p>
299 + Exciting Update: For a limited time, Pro users now get access to <a href="https://mxchat.ai/agents/" target="_blank">MxChat AI Agents</a> – a revolutionary way to test and deploy your MxChat chatbot!
300 +</p>
1234 301
1235 - // Remove the notice flag
1236 - $deleted = delete_option('mxchat_show_live_agent_disabled_notice');
1237 - //error_log('Option deleted: ' . ($deleted ? 'yes' : 'no'));
1238 -
1239 - wp_send_json_success();
1240 -}
1241 -public function add_live_agent_nonce() {
1242 - if (get_option('mxchat_show_live_agent_disabled_notice', false)) {
1243 - // Make sure your admin script is enqueued and localize the data
1244 - wp_localize_script('mxchat-admin-js', 'mxchatLiveAgent', array(
1245 - 'nonce' => wp_create_nonce('dismiss_live_agent_notice'),
1246 - 'ajaxurl' => admin_url('admin-ajax.php')
1247 - ));
1248 - }
1249 -}
1250 -
1251 -/**
1252 - * Show theme migration notice for Pro users with AI-generated themes
1253 - * Only shown once - dismissible and stored in options
1254 - */
1255 -public function show_theme_migration_banner() {
1256 - // Only show if Pro is activated
1257 - if (!$this->is_activated) {
1258 - return;
1259 - }
1260 -
1261 - // Check if notice should be shown
1262 - $show_notice = get_option('mxchat_show_theme_migration_notice', false);
1263 -
1264 - if ($show_notice) {
1265 - ?>
1266 - <div class="mxchat-theme-migration-notice" id="mxchat-theme-migration-notice">
1267 - <div class="mxchat-pro-notification">
1268 - <button type="button" class="mxchat-dismiss-btn" onclick="dismissThemeMigrationNotice()" aria-label="Dismiss notification">
1269 - <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
1270 - <line x1="18" y1="6" x2="6" y2="18"></line>
1271 - <line x1="6" y1="6" x2="18" y2="18"></line>
1272 - </svg>
1273 - </button>
1274 - <div class="mxchat-theme-migration-content">
1275 - <h3>🎨 AI Theme Migration Required</h3>
1276 - <p>If you're using an AI-generated chatbot theme, you'll need to migrate it to match the new CSS structure. Go to <strong>Theme Settings</strong>, select your theme from the sidebar, and click the <strong>Migrate</strong> button.</p>
1277 - </div>
302 + </div>
303 + <h2 class="admin-title">Mx<span class="admin-emphasis">Chat</span></h2>
304 + <h2 class="mxchat-nav-tab-wrapper">
305 + <a href="#chatbot" class="mxchat-nav-tab mxchat-nav-tab-active" data-tab="chatbot">Chatbot</a>
306 + <a href="#embed" class="mxchat-nav-tab" data-tab="embed">Integrations</a>
307 + <a href="#theme" class="mxchat-nav-tab" data-tab="theme">Theme</a>
308 + <a href="#general" class="mxchat-nav-tab" data-tab="general">FAQ</a>
309 + </h2>
310 + <form method="post" action="options.php">
311 + <?php settings_fields('mxchat_option_group'); ?>
312 + <div id="chatbot" class="mxchat-tab-content active">
313 + <?php do_settings_sections('mxchat-chatbot'); ?>
1278 314 </div>
1279 - </div>
1280 - <?php
1281 - }
1282 -}
1283 315
1284 -/**
1285 - * Dismiss theme migration notice via AJAX
1286 - */
1287 -public function dismiss_theme_migration_notice() {
1288 - // Verify nonce
1289 - if (!wp_verify_nonce($_POST['nonce'], 'dismiss_theme_migration_notice')) {
1290 - wp_die('Security check failed');
1291 - }
1292 316
1293 - // Remove the notice flag
1294 - delete_option('mxchat_show_theme_migration_notice');
1295 317
1296 - wp_send_json_success();
1297 -}
318 +<div id="embed" class="mxchat-tab-content">
319 + <div class="mxchat-settings-section">
320 + <h2>WooCommerce Settings</h2>
321 + <table class="form-table">
322 + <?php do_settings_fields('mxchat-embed', 'mxchat_woocommerce_section'); ?>
323 + </table>
324 + </div>
1298 325
1299 -/**
1300 - * Add nonce for theme migration notice dismiss
1301 - */
1302 -public function add_theme_migration_nonce() {
1303 - if (get_option('mxchat_show_theme_migration_notice', false) && $this->is_activated) {
1304 - wp_localize_script('mxchat-admin-js', 'mxchatThemeMigration', array(
1305 - 'nonce' => wp_create_nonce('dismiss_theme_migration_notice'),
1306 - 'ajaxurl' => admin_url('admin-ajax.php')
1307 - ));
1308 - }
1309 -}
326 + <div class="section-divider"></div> <!-- Divider -->
1310 327
1311 -public function mxchat_create_transcripts_page() {
1312 - global $wpdb;
1313 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
328 + <div class="mxchat-settings-section">
329 + <h2>Loops Settings</h2>
330 + <table class="form-table">
331 + <?php do_settings_fields('mxchat-embed', 'mxchat_loops_section'); ?>
332 + </table>
333 + </div>
1314 334
1315 - // Get basic stats
1316 - $total_chats = $wpdb->get_var("SELECT COUNT(DISTINCT session_id) FROM $table_name") ?: 0;
1317 - $total_messages = $wpdb->get_var("SELECT COUNT(*) FROM $table_name") ?: 0;
1318 335
1319 - // Count unique users with detailed breakdown
1320 - $total_users = $wpdb->get_var("
1321 - SELECT COUNT(DISTINCT
1322 - CASE
1323 - WHEN user_email != '' AND user_email IS NOT NULL THEN user_email
1324 - WHEN user_id != 0 THEN CONCAT('user_', user_id)
1325 - WHEN user_identifier NOT LIKE 'Tech-Savvy User'
1326 - AND user_identifier NOT LIKE 'Detail-Oriented User'
1327 - AND user_identifier NOT LIKE 'Language Learner'
1328 - AND user_identifier NOT LIKE 'Casual Browser'
1329 - AND user_identifier NOT LIKE 'Policy Enforcer'
1330 - AND user_identifier NOT LIKE 'Researcher'
1331 - AND user_identifier NOT LIKE 'Loyalty Member'
1332 - AND user_identifier NOT LIKE 'Gift Buyer'
1333 - AND user_identifier NOT LIKE 'Parent or Caregiver'
1334 - THEN user_identifier
1335 - ELSE session_id
1336 - END
1337 - )
1338 - FROM $table_name
1339 - WHERE role != 'assistant'
1340 - ");
336 + <div class="section-divider"></div> <!-- Divider -->
1341 337
1342 - // Get user type breakdown
1343 - $registered_users = $wpdb->get_var("
1344 - SELECT COUNT(DISTINCT user_email)
1345 - FROM $table_name
1346 - WHERE user_email != '' AND user_email IS NOT NULL
1347 - ");
338 + <!-- Brave Search Settings Section -->
339 + <div class="mxchat-settings-section">
340 + <h2>Brave Search Settings</h2>
341 + <table class="form-table">
342 + <?php do_settings_fields('mxchat-embed', 'mxchat_brave_section'); ?>
343 + </table>
344 + </div>
1348 345
1349 - $guest_users = $wpdb->get_var("
1350 - SELECT COUNT(DISTINCT user_identifier)
1351 - FROM $table_name
1352 - WHERE (user_email = '' OR user_email IS NULL)
1353 - AND role != 'assistant'
1354 - AND user_identifier NOT LIKE 'Tech-Savvy User'
1355 - AND user_identifier NOT LIKE 'Detail-Oriented User'
1356 - AND user_identifier NOT LIKE 'Language Learner'
1357 - AND user_identifier NOT LIKE 'Casual Browser'
1358 - AND user_identifier NOT LIKE 'Policy Enforcer'
1359 - AND user_identifier NOT LIKE 'Researcher'
1360 - AND user_identifier NOT LIKE 'Loyalty Member'
1361 - AND user_identifier NOT LIKE 'Gift Buyer'
1362 - AND user_identifier NOT LIKE 'Parent or Caregiver'
1363 - ");
346 + <div class="section-divider"></div> <!-- Divider -->
1364 347
1365 - // Get agent test messages count
1366 - $agent_tests = $wpdb->get_var("
1367 - SELECT COUNT(DISTINCT session_id)
1368 - FROM $table_name
1369 - WHERE user_identifier IN (
1370 - 'Tech-Savvy User',
1371 - 'Detail-Oriented User',
1372 - 'Language Learner',
1373 - 'Casual Browser',
1374 - 'Policy Enforcer',
1375 - 'Researcher',
1376 - 'Loyalty Member',
1377 - 'Gift Buyer',
1378 - 'Parent or Caregiver'
1379 - )
1380 - ");
1381 - // Get activity metrics
1382 - $today_chats = $wpdb->get_var("
1383 - SELECT COUNT(DISTINCT session_id)
1384 - FROM $table_name
1385 - WHERE DATE(timestamp) = CURDATE()
1386 - ");
1387 -
1388 - $week_chats = $wpdb->get_var("
1389 - SELECT COUNT(DISTINCT session_id)
1390 - FROM $table_name
1391 - WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 7 DAY)
1392 - ");
1393 -
1394 - $month_chats = $wpdb->get_var("
1395 - SELECT COUNT(DISTINCT session_id)
1396 - FROM $table_name
1397 - WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 30 DAY)
1398 - ");
1399 -
1400 - // Get daily chat data for last 7 days
1401 - $daily_stats = $wpdb->get_results("
1402 - SELECT
1403 - DATE(timestamp) as date,
1404 - COUNT(DISTINCT session_id) as chats,
1405 - COUNT(*) as messages
1406 - FROM $table_name
1407 - WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 7 DAY)
1408 - GROUP BY DATE(timestamp)
1409 - ORDER BY date ASC
1410 - ");
1411 -
1412 - // Get average messages per chat
1413 - $avg_messages = $wpdb->get_var("
1414 - SELECT AVG(message_count)
1415 - FROM (
1416 - SELECT session_id, COUNT(*) as message_count
1417 - FROM $table_name
1418 - GROUP BY session_id
1419 - ) as chat_counts
1420 - ");
1421 - $avg_messages = $avg_messages ? round($avg_messages, 1) : 0;
1422 -
1423 - // Get busiest hour
1424 - $busiest_hour = $wpdb->get_row("
1425 - SELECT HOUR(timestamp) as hour, COUNT(DISTINCT session_id) as chat_count
1426 - FROM $table_name
1427 - GROUP BY HOUR(timestamp)
1428 - ORDER BY chat_count DESC
1429 - LIMIT 1
1430 - ");
1431 -
1432 - // Prepare chart data
1433 - $chart_labels = array();
1434 - $chart_chats = array();
1435 - $chart_messages = array();
1436 -
1437 - // Fill last 7 days with data
1438 - for ($i = 6; $i >= 0; $i--) {
1439 - $date = date('Y-m-d', strtotime("-$i days"));
1440 - $day_name = date('D', strtotime("-$i days"));
1441 - $chart_labels[] = $day_name;
1442 -
1443 - $found = false;
1444 - foreach ($daily_stats as $stat) {
1445 - if ($stat->date === $date) {
1446 - $chart_chats[] = (int)$stat->chats;
1447 - $chart_messages[] = (int)$stat->messages;
1448 - $found = true;
1449 - break;
1450 - }
1451 - }
1452 - if (!$found) {
1453 - $chart_chats[] = 0;
1454 - $chart_messages[] = 0;
1455 - }
1456 - }
348 + <!-- Chat with PDF Intent Settings Section -->
349 +<div class="mxchat-settings-section">
350 + <h2>Toolbar Settings & Intents</h2>
351 + <table class="form-table">
352 + <?php do_settings_fields('mxchat-embed', 'mxchat_pdf_intent_section'); ?>
353 + </table>
354 +</div>
1457 355
1458 - // Satisfaction rating rollup — last 30 days, grouped by bot (plan-a5b006).
1459 - $satisfaction_stats = $this->get_satisfaction_rating_stats(30);
356 +<div class="section-divider"></div> <!-- Divider -->
357 +<!-- Live Agent Settings Section -->
358 +<div class="mxchat-settings-section">
359 + <h2>Live Agent Settings</h2>
360 +<p>Visit our <a href="https://mxchat.ai/documentation/#slack_integration" target="_blank">documentation page</a> to set up live agent transfer via Slack.</p>
361 + <table class="form-table">
362 + <?php do_settings_fields('mxchat-embed', 'mxchat_live_agent_section'); ?>
363 + </table>
364 +</div>
1460 365
1461 - // Prepare page data for the template
1462 - $page_data = array(
1463 - 'total_chats' => $total_chats,
1464 - 'total_messages' => $total_messages,
1465 - 'total_users' => $total_users,
1466 - 'registered_users' => $registered_users,
1467 - 'guest_users' => $guest_users,
1468 - 'agent_tests' => $agent_tests,
1469 - 'today_chats' => $today_chats,
1470 - 'week_chats' => $week_chats,
1471 - 'month_chats' => $month_chats,
1472 - 'avg_messages' => $avg_messages,
1473 - 'busiest_hour' => $busiest_hour,
1474 - 'chart_labels' => $chart_labels,
1475 - 'chart_chats' => $chart_chats,
1476 - 'chart_messages' => $chart_messages,
1477 - 'satisfaction_stats' => $satisfaction_stats,
1478 - );
366 +</div>
1479 367
1480 - // Include and render the new template
1481 - require_once plugin_dir_path(__FILE__) . 'admin-transcripts-page.php';
1482 - mxchat_render_transcripts_page($this, $page_data);
1483 -}
1484 368
1485 -/**
1486 - * Per-bot satisfaction rating rollup over the last $days days. Used by the
1487 - * Satisfaction card on the Transcripts dashboard (plan-a5b006).
1488 - *
1489 - * @param int $days Window in days.
1490 - * @return array Each entry: ['bot_id', 'total', 'positive', 'negative', 'positive_pct', 'negative_pct'].
1491 - */
1492 -public function get_satisfaction_rating_stats($days = 30) {
1493 - global $wpdb;
1494 - $table = $wpdb->prefix . 'mxchat_session_ratings';
1495 - if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) !== $table) {
1496 - return array();
1497 - }
1498 - $days = max(1, (int) $days);
1499 - $rows = $wpdb->get_results($wpdb->prepare(
1500 - "SELECT bot_id,
1501 - COUNT(*) AS total,
1502 - SUM(CASE WHEN rating_value = 1 THEN 1 ELSE 0 END) AS positive,
1503 - SUM(CASE WHEN rating_value = -1 THEN 1 ELSE 0 END) AS negative
1504 - FROM {$table}
1505 - WHERE created_at >= DATE_SUB(NOW(), INTERVAL %d DAY)
1506 - GROUP BY bot_id
1507 - ORDER BY total DESC",
1508 - $days
1509 - ));
1510 - $out = array();
1511 - foreach ((array) $rows as $row) {
1512 - $total = (int) $row->total;
1513 - $positive = (int) $row->positive;
1514 - $negative = (int) $row->negative;
1515 - $out[] = array(
1516 - 'bot_id' => $row->bot_id ?: 'default',
1517 - 'total' => $total,
1518 - 'positive' => $positive,
1519 - 'negative' => $negative,
1520 - 'positive_pct' => $total > 0 ? (int) round(($positive / $total) * 100) : 0,
1521 - 'negative_pct' => $total > 0 ? (int) round(($negative / $total) * 100) : 0,
1522 - );
1523 - }
1524 - return $out;
1525 -}
1526 369
1527 -/**
1528 - * Get chart data for transcripts page
1529 - * Used by both page render and script localization
1530 - *
1531 - * @return array Chart data with labels, chats, and messages arrays
1532 - */
1533 -private function get_transcripts_chart_data() {
1534 - global $wpdb;
1535 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1536 370
1537 - // Get daily chat data for last 7 days - same query as mxchat_transcripts_page()
1538 - $daily_stats = $wpdb->get_results("
1539 - SELECT
1540 - DATE(timestamp) as date,
1541 - COUNT(DISTINCT session_id) as chats,
1542 - COUNT(*) as messages
1543 - FROM $table_name
1544 - WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 7 DAY)
1545 - GROUP BY DATE(timestamp)
1546 - ORDER BY date ASC
1547 - ");
1548 371
1549 - // Prepare chart data
1550 - $chart_labels = array();
1551 - $chart_chats = array();
1552 - $chart_messages = array();
372 + <div id="theme" class="mxchat-tab-content">
373 + <?php do_settings_sections('mxchat-theme'); ?>
374 + </div>
375 + <div id="general" class="mxchat-tab-content">
376 + <?php do_settings_sections('mxchat-general'); ?>
377 +<p>
378 + If you’re having trouble with setup or getting the responses you need, we encourage you to review our
379 + <a href="https://mxchat.ai/documentation/" target="_blank" rel="noopener noreferrer">documentation</a> or
380 + <a href="https://wordpress.org/support/plugin/mxchat-basic/" target="_blank" rel="noopener noreferrer">create a support ticket</a>.
381 +</p>
382 +<p>
383 + If you like our plugin, please consider <a href="https://wordpress.org/plugins/mxchat-basic/#reviews" target="_blank" rel="noopener noreferrer">leaving us a review</a>.
384 +</p>
1553 385
1554 - // Fill last 7 days with data - same logic as mxchat_transcripts_page()
1555 - for ($i = 6; $i >= 0; $i--) {
1556 - $date = date('Y-m-d', strtotime("-$i days"));
1557 - $day_name = date('D', strtotime("-$i days"));
1558 - $chart_labels[] = $day_name;
1559 -
1560 - $found = false;
1561 - if ($daily_stats) {
1562 - foreach ($daily_stats as $stat) {
1563 - if ($stat->date === $date) {
1564 - $chart_chats[] = (int)$stat->chats;
1565 - $chart_messages[] = (int)$stat->messages;
1566 - $found = true;
1567 - break;
1568 - }
1569 - }
1570 - }
1571 - if (!$found) {
1572 - $chart_chats[] = 0;
1573 - $chart_messages[] = 0;
1574 - }
1575 - }
1576 -
1577 - return array(
1578 - 'labels' => $chart_labels,
1579 - 'chats' => $chart_chats,
1580 - 'messages' => $chart_messages
1581 - );
1582 -}
1583 -
1584 -public function mxchat_transcripts_notification_section_callback() {
1585 - echo '<p>' . esc_html__('Configure email notifications for new chat transcripts. You will receive an email notification when a new chat session begins.', 'mxchat') . '</p>';
1586 -}
1587 -public function mxchat_enable_notifications_callback() {
1588 - $options = get_option('mxchat_transcripts_options', array());
1589 - $enabled = isset($options['mxchat_enable_notifications']) ? $options['mxchat_enable_notifications'] : 0;
1590 - ?>
1591 - <label for="mxchat_enable_notifications">
1592 - <input type="checkbox" id="mxchat_enable_notifications"
1593 - name="mxchat_transcripts_options[mxchat_enable_notifications]"
1594 - value="1" <?php checked(1, $enabled); ?>>
1595 - <?php esc_html_e('Send email notification when a new chat session starts', 'mxchat'); ?>
1596 - </label>
1597 - <p class="description">
1598 - <?php esc_html_e('Enable this option to receive email notifications for new chat sessions.', 'mxchat'); ?>
386 +<div class="faq-item">
387 + <h3>How does the Claude API integration work?</h3>
388 + <p>
389 + The Claude API, provided by Anthropic, allows for intelligent, context-aware chatbot responses in MxChat. To use it, you will need both an OpenAI API key and a Claude API key. This is necessary because the system utilizes OpenAI for vector embedding in the Retrieval-Augmented Generation (RAG) process, as Claude does not currently offer an embedding API.
1599 390 </p>
1600 - <?php
1601 -}
1602 -public function mxchat_notification_email_callback() {
1603 - $options = get_option('mxchat_transcripts_options', array());
1604 - $email = isset($options['mxchat_notification_email']) ? $options['mxchat_notification_email'] : get_option('admin_email');
1605 - ?>
1606 - <input type="email" id="mxchat_notification_email"
1607 - name="mxchat_transcripts_options[mxchat_notification_email]"
1608 - value="<?php echo esc_attr($email); ?>"
1609 - class="regular-text">
1610 - <p class="description">
1611 - <?php esc_html_e('Enter the email address where notifications should be sent. Defaults to the admin email address.', 'mxchat'); ?>
391 + <p>
392 + When using the Claude API, your custom content is sent to Claude for generating responses, while the embeddings are processed through OpenAI. This ensures your chatbot provides accurate and engaging responses while maintaining knowledge relevant to your website.
1612 393 </p>
1613 - <?php
1614 -}
394 + <p>
395 + You can obtain your Claude API key by signing up on the <a href="https://www.anthropic.com/api" target="_blank" rel="noopener">Anthropic API page</a>.
396 + </p>
397 +</div>
1615 398
1616 -public function mxchat_auto_delete_transcripts_callback() {
1617 - $options = get_option('mxchat_transcripts_options', array());
1618 - $interval = isset($options['mxchat_auto_delete_transcripts']) ? $options['mxchat_auto_delete_transcripts'] : 'never';
1619 - ?>
1620 - <select id="mxchat_auto_delete_transcripts"
1621 - name="mxchat_transcripts_options[mxchat_auto_delete_transcripts]">
1622 - <option value="never" <?php selected($interval, 'never'); ?>>
1623 - <?php esc_html_e('Never (Keep All Transcripts)', 'mxchat'); ?>
1624 - </option>
1625 - <option value="1week" <?php selected($interval, '1week'); ?>>
1626 - <?php esc_html_e('After 1 Week', 'mxchat'); ?>
1627 - </option>
1628 - <option value="2weeks" <?php selected($interval, '2weeks'); ?>>
1629 - <?php esc_html_e('After 2 Weeks', 'mxchat'); ?>
1630 - </option>
1631 - <option value="1month" <?php selected($interval, '1month'); ?>>
1632 - <?php esc_html_e('After 1 Month', 'mxchat'); ?>
1633 - </option>
1634 - </select>
1635 - <p class="description">
1636 - <?php esc_html_e('Automatically delete old chat transcripts after the selected time period. This helps manage database size and privacy.', 'mxchat'); ?>
399 +<div class="faq-item">
400 + <h3>How does the X.AI API integration work?</h3>
401 + <p>
402 + The X.AI API, released on 10.21.24, is currently in beta. To use it, you will need both an OpenAI API key and an X.AI API key. This is because OpenAI handles vector embeddings in the Retrieval-Augmented Generation (RAG) process, as X.AI does not yet provide an embedding API.
1637 403 </p>
1638 - <?php
1639 -}
404 + <p>
405 + Custom content is sent to the X.AI API for generating responses, while OpenAI processes the embeddings. This allows the chatbot to provide advanced responses while maintaining context from your website. You can get your X.AI API key from the <a href="https://docs.x.ai/docs" target="_blank" rel="noopener">X.AI API documentation</a>.
406 + </p>
407 +</div>
1640 408
1641 -/**
1642 - * Custom retention-days input. When > 0 it overrides the bucket dropdown above
1643 - * and deletes transcripts older than the given number of days. Set to 0 to fall
1644 - * back to the dropdown (or "Never" if the dropdown is also Never).
1645 - *
1646 - * Devs can override the final day count via the `mxchat_transcript_retention_days`
1647 - * filter — runs in `cleanup_old_transcripts()` after this option is read.
1648 - *
1649 - * (plan-mxchat-20260509-9b80b1)
1650 - */
1651 -public function mxchat_retention_days_callback() {
1652 - $options = get_option('mxchat_transcripts_options', array());
1653 - $days = isset($options['mxchat_retention_days']) ? (int) $options['mxchat_retention_days'] : 0;
1654 - ?>
1655 - <input type="number"
1656 - id="mxchat_retention_days"
1657 - name="mxchat_transcripts_options[mxchat_retention_days]"
1658 - value="<?php echo esc_attr($days); ?>"
1659 - min="0"
1660 - max="3650"
1661 - step="1"
1662 - style="width: 90px;" />
1663 - <p class="description">
1664 - <?php esc_html_e('Number of days to retain transcripts. When set to a value greater than 0, this overrides the dropdown above. Set to 0 to use the dropdown. Maximum 3650 (10 years). A daily wp-cron task removes anything older, cascading to translations and click-tracking rows.', 'mxchat'); ?>
1665 - <br>
1666 - <code>apply_filters( 'mxchat_transcript_retention_days', $days )</code>
1667 - <?php esc_html_e('lets developers override the final day count programmatically.', 'mxchat'); ?>
409 +<div class="faq-item">
410 + <h3>Do I need an OpenAI API key to use the chatbot?</h3>
411 + <p>
412 + Yes, you will need an OpenAI API key to power the chatbot. You can obtain an API key by signing up on the <a href="https://platform.openai.com/signup" target="_blank" rel="noopener">OpenAI platform</a>. After signing up, you must add credits to your account—typically, $5 in credits is sufficient to get started.
1668 413 </p>
1669 - <?php
1670 -}
1671 -
1672 -public function mxchat_auto_email_transcript_callback() {
1673 - $options = get_option('mxchat_transcripts_options', array());
1674 - $enabled = isset($options['mxchat_auto_email_transcript_enabled']) ? $options['mxchat_auto_email_transcript_enabled'] : 0;
1675 - $delay = isset($options['mxchat_auto_email_transcript_delay']) ? $options['mxchat_auto_email_transcript_delay'] : '30';
1676 - $require_contact = isset($options['mxchat_auto_email_transcript_require_contact']) ? $options['mxchat_auto_email_transcript_require_contact'] : 0;
1677 - ?>
1678 - <label>
1679 - <input type="checkbox"
1680 - id="mxchat_auto_email_transcript_enabled"
1681 - name="mxchat_transcripts_options[mxchat_auto_email_transcript_enabled]"
1682 - value="1"
1683 - <?php checked($enabled, 1); ?>>
1684 - <?php esc_html_e('Enable Auto-Email of Full Transcript', 'mxchat'); ?>
1685 - </label>
1686 - <br><br>
1687 - <label for="mxchat_auto_email_transcript_delay">
1688 - <?php esc_html_e('Send transcript after:', 'mxchat'); ?>
1689 - </label>
1690 - <select id="mxchat_auto_email_transcript_delay"
1691 - name="mxchat_transcripts_options[mxchat_auto_email_transcript_delay]">
1692 - <option value="15" <?php selected($delay, '15'); ?>>
1693 - <?php esc_html_e('15 minutes', 'mxchat'); ?>
1694 - </option>
1695 - <option value="30" <?php selected($delay, '30'); ?>>
1696 - <?php esc_html_e('30 minutes', 'mxchat'); ?>
1697 - </option>
1698 - <option value="60" <?php selected($delay, '60'); ?>>
1699 - <?php esc_html_e('1 hour', 'mxchat'); ?>
1700 - </option>
1701 - </select>
1702 - <br><br>
1703 - <label>
1704 - <input type="checkbox"
1705 - id="mxchat_auto_email_transcript_require_contact"
1706 - name="mxchat_transcripts_options[mxchat_auto_email_transcript_require_contact]"
1707 - value="1"
1708 - <?php checked($require_contact, 1); ?>>
1709 - <?php esc_html_e('Only send if visitor provided contact info', 'mxchat'); ?>
1710 - </label>
1711 - <p class="description">
1712 - <?php esc_html_e('Automatically email the full transcript as a .txt file after the specified time has passed since the last user message. The scheduled email will be cancelled if a new message is received.', 'mxchat'); ?>
1713 - <br>
1714 - <?php esc_html_e('When "Only send if visitor provided contact info" is enabled, transcripts will only be emailed if the visitor shared an email address or phone number (including WhatsApp) in the chat.', 'mxchat'); ?>
414 + <p>
415 + Once you have your API key, simply enter it in the chatbot's settings to enable functionality. The chatbot relies on OpenAI’s models for generating responses, so having sufficient credits in your OpenAI account is essential for smooth operation.
1715 416 </p>
1716 - <?php
1717 -}
417 +</div>
1718 418
419 + <div class="faq-item">
420 + <h3>How do I add the chatbot to my site?</h3>
421 + <p>
422 + You can add the chatbot using the shortcode <code>[mxchat_chatbot floating="yes"]</code> or <code>[mxchat_chatbot floating="no"]</code>. For initial testing and styling, it's best to use the shortcode on a draft or non-public page. Once you’re ready to go live, enable the “Append Chat Widget to Body” option in the settings for site-wide integration (recommended) or add shortcode to the footer.
423 + </p>
424 + </div>
1719 425
426 + <div class="faq-item">
427 + <h3>How does the chatbot use my content to generate responses?</h3>
428 + <p>
429 + The chatbot uses AI and vector embeddings to connect users with relevant information. When you submit content, it converts it into a mathematical format. When a user asks a question, the bot matches it to your stored content and generates a response based on relevance. For example, submitting "Our phone number is 910-123-4567" allows the bot to retrieve this information when asked about contact details. To ensure related information is provided together, submit it in one entry.
430 + </p>
431 + </div>
1720 432
1721 -public function sanitize_transcripts_options($input) {
1722 - $sanitized = array();
433 + <div class="faq-item">
434 + <h3>Why does the chatbot sometimes make up links or information?</h3>
435 + <p>
436 + Occasionally, the chatbot may generate inaccurate links or information, known as "hallucinations." To minimize this, you can add system instructions to guide its behavior. For example, include an instruction like: "Only provide links you directly have access to or retrieve from the knowledge base. Do not make up links or information that you do not have direct access to." This helps the bot stay aligned with your content.
437 + </p>
438 + </div>
1723 439
1724 - $sanitized['mxchat_enable_notifications'] = isset($input['mxchat_enable_notifications']) ? 1 : 0;
440 + <div class="faq-item">
441 + <h3>How do intents work in MxChat?</h3>
442 + <p>
443 + Intents allow MxChat to recognize user requests and trigger specific actions, such as capturing emails or displaying product information. This helps provide a more interactive and responsive experience. For a detailed explanation of each intent, please refer to our <a href="https://mxchat.ai/documentation/#intents" target="_blank" rel="noopener noreferrer">Intents Documentation</a>.
444 + </p>
445 + </div>
1725 446
1726 - if (isset($input['mxchat_notification_email'])) {
1727 - $sanitized['mxchat_notification_email'] = sanitize_email($input['mxchat_notification_email']);
1728 - if (!is_email($sanitized['mxchat_notification_email'])) {
1729 - add_settings_error(
1730 - 'mxchat_transcripts_options',
1731 - 'invalid_email',
1732 - __('Please enter a valid email address for notifications.', 'mxchat'),
1733 - 'error'
1734 - );
1735 - $sanitized['mxchat_notification_email'] = get_option('admin_email');
1736 - }
1737 - }
1738 447
1739 - // Sanitize auto-delete setting
1740 - $valid_intervals = array('never', '1week', '2weeks', '1month');
1741 - if (isset($input['mxchat_auto_delete_transcripts'])) {
1742 - $sanitized['mxchat_auto_delete_transcripts'] = in_array($input['mxchat_auto_delete_transcripts'], $valid_intervals)
1743 - ? $input['mxchat_auto_delete_transcripts']
1744 - : 'never';
1745 - } else {
1746 - $sanitized['mxchat_auto_delete_transcripts'] = 'never';
1747 - }
448 + <div class="faq-item">
449 + <h3>How does the Complianz integration work?</h3>
450 + <p>
451 + The Pro version offers direct integration with the Complianz GDPR plugin, making it easy to stay compliant with GDPR. If Complianz is enabled on your website, users must accept consent before the chatbot widget appears. The chatbot will only display once the user has accepted, ensuring compliance with data privacy regulations.
452 + </p>
453 + </div>
1748 454
1749 - // Sanitize custom retention-days override (plan-9b80b1).
1750 - if (isset($input['mxchat_retention_days'])) {
1751 - $days = (int) $input['mxchat_retention_days'];
1752 - $sanitized['mxchat_retention_days'] = max(0, min(3650, $days));
1753 - } else {
1754 - $sanitized['mxchat_retention_days'] = 0;
1755 - }
455 + <div class="faq-item">
456 + <h3>How does the WooCommerce integration work?</h3>
457 + <p>
458 + With WooCommerce integration, the chatbot automatically embeds new products and updates existing ones. For already-published products, you can click update or submit the product sitemap. The “Order History Access” setting allows the bot to access users' order history (requires login) and assist with order inquiries. To enable the “Add to Cart” feature, add this system instruction: “After discussing a product, ask if the user wants to add it to their cart. The user must say 'Add to cart' exactly.” Currently, this feature supports English only, with more languages coming soon.
459 + </p>
460 + </div>
1756 461
1757 - // Get old values to check if auto-delete or retention-days changed.
1758 - $old_options = get_option('mxchat_transcripts_options');
1759 - $old_interval = isset($old_options['mxchat_auto_delete_transcripts']) ? $old_options['mxchat_auto_delete_transcripts'] : 'never';
1760 - $old_retention = isset($old_options['mxchat_retention_days']) ? (int) $old_options['mxchat_retention_days'] : 0;
462 + <div class="faq-item">
463 + <h3>Why isn't my chatbot responding as expected?</h3>
464 + <p>
465 + AI chatbots can sometimes behave in unexpected ways, especially if you're new to configuring AI for specific tasks. We're committed to helping you get the most out of your chatbot experience. While we’re working on comprehensive guides and video tutorials, our team is here to assist you directly. If your chatbot isn't delivering the responses you need, please don’t hesitate to <a href="https://mxchat.ai/contact/" target="_blank" rel="noopener noreferrer">contact us</a> for personalized support.
466 + </p>
467 + <p>
468 + We’re dedicated to your success and ready to guide you in aligning the AI's behavior to meet your goals.
469 + </p>
470 + </div>
1761 471
1762 - // If either setting changed, reschedule the cron job. The schedule_transcript_cleanup
1763 - // helper now treats "any active retention" (dropdown != never OR custom days > 0) as a
1764 - // reason to keep the daily cron registered.
1765 - $interval_changed = ($old_interval !== $sanitized['mxchat_auto_delete_transcripts']);
1766 - $retention_changed = ($old_retention !== $sanitized['mxchat_retention_days']);
1767 - if ($interval_changed || $retention_changed) {
1768 - $any_active = ($sanitized['mxchat_auto_delete_transcripts'] !== 'never') || ($sanitized['mxchat_retention_days'] > 0);
1769 - $this->schedule_transcript_cleanup($any_active ? 'active' : 'never');
1770 - }
472 + <div class="faq-item">
473 + <h3>What is Loops, and how do I get an API key?</h3>
474 + <p>
475 + Loops is a powerful SaaS email service that helps you automate and enhance your email marketing campaigns, making it easy to reach and engage with your audience. To integrate Loops with MxChat, you’ll need an API key from Loops. You can obtain this key by logging into your Loops account and navigating to the API settings. Visit the <a href="https://loops.so" target="_blank" rel="noopener noreferrer">Loops website</a> to get started or to sign up for an account.
476 + </p>
477 + </div>
1771 478
1772 - // Sanitize auto-email transcript settings
1773 - $sanitized['mxchat_auto_email_transcript_enabled'] = isset($input['mxchat_auto_email_transcript_enabled']) ? 1 : 0;
1774 479
1775 - $valid_delays = array('15', '30', '60');
1776 - if (isset($input['mxchat_auto_email_transcript_delay'])) {
1777 - $sanitized['mxchat_auto_email_transcript_delay'] = in_array($input['mxchat_auto_email_transcript_delay'], $valid_delays)
1778 - ? $input['mxchat_auto_email_transcript_delay']
1779 - : '30';
1780 - } else {
1781 - $sanitized['mxchat_auto_email_transcript_delay'] = '30';
1782 - }
1783 -
1784 - // Sanitize require contact info setting
1785 - $sanitized['mxchat_auto_email_transcript_require_contact'] = isset($input['mxchat_auto_email_transcript_require_contact']) ? 1 : 0;
1786 -
1787 - return $sanitized;
480 + </div>
481 + <?php submit_button(); ?>
482 + </form>
483 + </div>
484 + <?php
1788 485 }
1789 486
1790 -/**
1791 - * Schedule or unschedule the transcript cleanup cron job
1792 - */
1793 -public function schedule_transcript_cleanup($interval) {
1794 - // Clear any existing scheduled event
1795 - $timestamp = wp_next_scheduled('mxchat_cleanup_old_transcripts');
1796 - if ($timestamp) {
1797 - wp_unschedule_event($timestamp, 'mxchat_cleanup_old_transcripts');
1798 - }
1799 487
1800 - // Schedule new event whenever retention is active. "active" is the canonical
1801 - // value passed by sanitize_transcripts_options when either the dropdown != never
1802 - // OR the custom retention-days > 0; "never" turns the cron off. Any other value
1803 - // (the legacy "1week" / "2weeks" / "1month" strings) is also treated as active.
1804 - if ($interval !== 'never') {
1805 - // Schedule to run daily at 3 AM
1806 - $next_run = strtotime('tomorrow 3:00 AM');
1807 - wp_schedule_event($next_run, 'daily', 'mxchat_cleanup_old_transcripts');
1808 - }
1809 -}
488 + public function mxchat_create_transcripts_page() {
489 + ?>
490 + <div class="wrap mxchat-admin">
491 + <h2><?php esc_html_e('Chat Transcripts', 'mxchat'); ?></h2>
492 + <form id="mxchat-delete-form" method="post">
493 + <?php wp_nonce_field('mxchat_delete_chat_history', 'mxchat_delete_chat_nonce'); ?>
494 + <div class="mxchat-controls">
495 + <label for="mxchat-select-all-transcripts" class="mxchat-select-all-label">
496 + <input type="checkbox" id="mxchat-select-all-transcripts" /> <?php esc_html_e('Select All', 'mxchat'); ?>
497 + </label>
498 + <input type="submit" value="<?php esc_attr_e('Delete Selected', 'mxchat'); ?>" class="button delete-chats-button" />
499 + </div>
500 + <div id="mxchat-transcripts">
501 + <!-- Transcripts will be loaded here -->
502 + </div>
503 + </form>
504 + </div>
505 + <?php
506 + }
1810 507
1811 -/**
1812 - * Self-heal guard: keep the cleanup cron in sync with the retention setting.
1813 - *
1814 - * Runs on admin_init. Cost when nothing is wrong: one get_option() (cached) and
1815 - * one wp_next_scheduled() (reads the cached cron option) — no writes. It only
1816 - * schedules when retention is active but the event is missing, and only
1817 - * unschedules when retention is off but the event survived. Both directions
1818 - * reuse schedule_transcript_cleanup() so there is exactly one scheduling path.
1819 - * Legacy dropdown values (1week/2weeks/1month) count as active, matching the
1820 - * helper's own semantics. Deliberately not gated on DISABLE_WP_CRON: scheduling
1821 - * writes the cron option regardless of how cron is executed, so a server-side
1822 - * cron runner still picks the event up.
1823 - */
1824 -public function ensure_transcript_cleanup_scheduled() {
1825 - $options = get_option('mxchat_transcripts_options', array());
1826 - $interval = isset($options['mxchat_auto_delete_transcripts']) ? $options['mxchat_auto_delete_transcripts'] : 'never';
1827 - $days = isset($options['mxchat_retention_days']) ? (int) $options['mxchat_retention_days'] : 0;
1828 - $active = ($interval !== 'never') || ($days > 0);
1829 - $scheduled = (bool) wp_next_scheduled('mxchat_cleanup_old_transcripts');
1830 508
1831 - if ($active && !$scheduled) {
1832 - $this->schedule_transcript_cleanup('active');
1833 - } elseif (!$active && $scheduled) {
1834 - $this->schedule_transcript_cleanup('never');
1835 - }
1836 -}
1837 509
1838 -/**
1839 - * Delete old transcripts based on the configured interval
1840 - */
1841 -public function cleanup_old_transcripts() {
1842 - $options = get_option('mxchat_transcripts_options', array());
1843 - $interval = isset($options['mxchat_auto_delete_transcripts']) ? $options['mxchat_auto_delete_transcripts'] : 'never';
1844 - $custom_days = isset($options['mxchat_retention_days']) ? (int) $options['mxchat_retention_days'] : 0;
1845 510
1846 - // Custom retention-days (plan-9b80b1) takes precedence over the bucket dropdown.
1847 - $days = 0;
1848 - if ($custom_days > 0) {
1849 - $days = $custom_days;
1850 - } else {
1851 - switch ($interval) {
1852 - case '1week': $days = 7; break;
1853 - case '2weeks': $days = 14; break;
1854 - case '1month': $days = 30; break;
1855 - case 'never':
1856 - default:
1857 - $days = 0;
1858 - }
1859 - }
1860 -
1861 - // Devs can override the final day count programmatically.
1862 - $days = (int) apply_filters('mxchat_transcript_retention_days', $days);
1863 -
1864 - if ($days <= 0) {
1865 - return; // Retention disabled — bail.
1866 - }
1867 -
511 +public function mxchat_create_prompts_page() {
1868 512 global $wpdb;
1869 - $transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts';
1870 - $translations_table = $wpdb->prefix . 'mxchat_transcript_translations';
1871 - $url_clicks_table = $wpdb->prefix . 'mxchat_url_clicks';
513 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
1872 514
1873 - // Defensive: if the main table doesn't exist (fresh-ish install), bail.
1874 - if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $transcripts_table)) !== $transcripts_table) {
1875 - return;
515 + // Display success message if all prompts were deleted
516 + if (isset($_GET['all_deleted']) && $_GET['all_deleted'] === 'true') {
517 + echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__('All knowledge has been deleted successfully.', 'mxchat') . '</p></div>';
1876 518 }
1877 519
1878 - $cutoff_date = gmdate('Y-m-d H:i:s', time() - ($days * DAY_IN_SECONDS));
520 + // Set up pagination and search query
521 + $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field($_GET['_wpnonce']) : '';
522 + $search_query = (!empty($nonce) && wp_verify_nonce($nonce, 'mxchat_prompts_search_nonce') && isset($_GET['search'])) ? sanitize_text_field($_GET['search']) : '';
523 + $current_page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
524 + $per_page = 10;
525 + $offset = ($current_page - 1) * $per_page;
1879 526
1880 - // Cap at 5000 session_ids per run so a large unattended site doesn't OOM —
1881 - // the cron will pick up where it left off on the next tick (within hours).
1882 - $batch_cap = (int) apply_filters('mxchat_transcript_retention_batch_cap', 5000);
1883 -
1884 - $sessions_to_delete = $wpdb->get_col(
1885 - $wpdb->prepare(
1886 - "SELECT DISTINCT session_id FROM {$transcripts_table} WHERE timestamp IS NOT NULL AND timestamp < %s LIMIT %d",
1887 - $cutoff_date,
1888 - $batch_cap
1889 - )
1890 - );
1891 -
1892 - if (empty($sessions_to_delete)) {
1893 - return;
527 + // Modify query to handle search input
528 + $sql_search = "";
529 + if ($search_query) {
530 + $sql_search = $wpdb->prepare("WHERE article_content LIKE %s", '%' . $wpdb->esc_like($search_query) . '%');
1894 531 }
1895 532
1896 - $placeholders = implode(',', array_fill(0, count($sessions_to_delete), '%s'));
533 + // Retrieve total number of prompts, considering search filter
534 + $total_prompts = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name} {$sql_search}");
535 + $total_pages = ceil($total_prompts / $per_page);
1897 536
1898 - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1899 - // $placeholders is a server-built list of literal "%s" tokens.
1900 - $deleted_transcripts = (int) $wpdb->query(
537 + // Retrieve prompts from the database
538 + $prompts = $wpdb->get_results(
1901 539 $wpdb->prepare(
1902 - "DELETE FROM {$transcripts_table} WHERE session_id IN ($placeholders)",
1903 - $sessions_to_delete
540 + "SELECT * FROM {$table_name} {$sql_search} ORDER BY timestamp DESC LIMIT %d OFFSET %d",
541 + $per_page,
542 + $offset
1904 543 )
1905 544 );
1906 545
1907 - if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $translations_table)) === $translations_table) {
1908 - $wpdb->query(
1909 - $wpdb->prepare(
1910 - "DELETE FROM {$translations_table} WHERE session_id IN ($placeholders)",
1911 - $sessions_to_delete
1912 - )
1913 - );
1914 - }
546 + ?>
1915 547
1916 - if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $url_clicks_table)) === $url_clicks_table) {
1917 - $wpdb->query(
1918 - $wpdb->prepare(
1919 - "DELETE FROM {$url_clicks_table} WHERE session_id IN ($placeholders)",
1920 - $sessions_to_delete
1921 - )
1922 - );
1923 - }
1924 - // phpcs:enable
548 + <div class="wrap mxchat-admin">
549 + <div class="mxchat-grid-container">
550 + <!-- Submit Content Form -->
551 + <div class="mxchat-grid-item full-width">
552 + <h2>Submit Content</h2>
553 + <form id="mxchat-content-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_content')); ?>">
554 + <?php wp_nonce_field('mxchat_submit_content_action', 'mxchat_submit_content_nonce'); ?>
555 + <div class="mxchat-form-group">
556 + <label for="article_content">Article Content:</label>
557 + <textarea name="article_content" id="article_content" required></textarea>
558 + </div>
559 + <div class="mxchat-form-group">
560 + <label for="article_url">Article URL (Optional):</label>
561 + <input type="url" name="article_url" id="article_url" placeholder="Enter related URL for the content">
562 + </div>
563 + <input type="submit" name="submit_content" value="Submit Content" class="button button-primary submit-content-button" />
564 + </form>
565 + <div id="mxchat-content-loading" class="mxchat-content-spinner" style="display: none;"></div>
566 + <div id="mxchat-content-loading-text" class="mxchat-loading-text">Submitting content, please wait...</div>
567 + </div>
1925 568
1926 - update_option('mxchat_retention_last_swept_at', time(), false);
1927 - update_option('mxchat_retention_rows_last_deleted', $deleted_transcripts, false);
1928 -}
1929 569
1930 -public function export_chat_transcripts() {
1931 - if (!current_user_can('manage_options')) {
1932 - wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'mxchat'));
1933 - }
570 + <div class="mxchat-grid-item">
571 + <h2><?php esc_html_e('Submit Sitemap, URL, or PDF URL', 'mxchat'); ?></h2>
1934 572
1935 - check_ajax_referer('mxchat_export_transcripts', 'security');
573 + <?php
574 + // Check for any active processing
575 + $pdf_url = get_transient('mxchat_last_pdf_url');
576 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
577 + $pdf_status = $pdf_url ? $this->get_pdf_processing_status($pdf_url) : false;
578 + $sitemap_status = $sitemap_url ? $this->get_sitemap_processing_status($sitemap_url) : false;
1936 579
1937 - global $wpdb;
1938 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
580 + // Clear old completed statuses
581 + if ($pdf_status && $pdf_status['status'] === 'complete') {
582 + delete_transient('mxchat_last_pdf_url');
583 + $pdf_status = false;
584 + }
585 + if ($sitemap_status && $sitemap_status['status'] === 'complete') {
586 + delete_transient('mxchat_last_sitemap_url');
587 + $sitemap_status = false;
588 + }
1939 589
1940 - // Get all transcripts ordered by session and timestamp
1941 - $results = $wpdb->get_results(
1942 - "SELECT session_id, user_email, user_identifier, role, message, timestamp
1943 - FROM {$table_name}
1944 - ORDER BY session_id, timestamp ASC"
1945 - );
590 + $is_processing = ($pdf_status && $pdf_status['status'] === 'processing') ||
591 + ($sitemap_status && $sitemap_status['status'] === 'processing');
592 + ?>
1946 593
1947 - if (empty($results)) {
1948 - wp_send_json_error(array('message' => 'No transcripts found.'));
1949 - wp_die();
1950 - }
594 + <?php if (!$is_processing) : ?>
595 + <form id="mxchat-sitemap-form" method="post" class="mxchat-sitemap-form"
596 + action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_sitemap')); ?>">
1951 597
1952 - // Set headers for CSV download
1953 - header('Content-Type: text/csv');
1954 - header('Content-Disposition: attachment; filename="chat-transcripts-' . date('Y-m-d') . '.csv"');
1955 - header('Pragma: no-cache');
1956 - header('Expires: 0');
598 + <?php wp_nonce_field('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce'); ?>
1957 599
1958 - // Create output stream
1959 - $output = fopen('php://output', 'w');
600 + <div class="mxchat-search-group">
601 + <input type="url"
602 + name="sitemap_url"
603 + id="sitemap_url"
604 + placeholder="<?php esc_attr_e('Enter URL (Sitemap, PDF, or webpage)', 'mxchat'); ?>"
605 + required
606 + aria-label="<?php esc_attr_e('URL Input', 'mxchat'); ?>"
607 + />
608 + <input type="submit"
609 + name="submit_sitemap"
610 + value="<?php esc_attr_e('Submit', 'mxchat'); ?>"
611 + class="button button-primary"
612 + />
613 + </div>
614 + </form>
615 + <?php endif; ?>
1960 616
1961 - // Add UTF-8 BOM for proper Excel encoding
1962 - fputs($output, "\xEF\xBB\xBF");
617 + <div id="mxchat-sitemap-loading" class="mxchat-spinner" style="display: none;"
618 + aria-hidden="true"></div>
619 + <div id="mxchat-loading-text" class="screen-reader-text" style="display: none;">
620 + <?php esc_html_e('Loading information into database, please wait...', 'mxchat'); ?>
621 + </div>
1963 622
1964 - // Add CSV headers
1965 - fputcsv($output, array(
1966 - 'Session ID',
1967 - 'Email',
1968 - 'User Identifier',
1969 - 'Role',
1970 - 'Message',
1971 - 'Timestamp'
1972 - ));
623 + <?php if ($pdf_status && $pdf_status['status'] !== 'complete') : ?>
624 + <div class="mxchat-process-status pdf" role="alert" aria-live="polite">
625 + <h3><?php esc_html_e('PDF Processing Status (Refresh page for update)', 'mxchat'); ?></h3>
626 + <p>
627 + <?php
628 + printf(
629 + /* translators: 1: Current pages, 2: Total pages, 3: Percentage complete */
630 + esc_html__('Progress: %1$d of %2$d pages (%3$d%%)', 'mxchat'),
631 + absint($pdf_status['processed_pages']),
632 + absint($pdf_status['total_pages']),
633 + absint($pdf_status['percentage'])
634 + );
635 + ?>
636 + </p>
637 + <p>
638 + <?php
639 + printf(
640 + /* translators: %s: Current status */
641 + esc_html__('Status: %s', 'mxchat'),
642 + esc_html(ucfirst($pdf_status['status']))
643 + );
644 + ?>
645 + </p>
646 + <p>
647 + <?php
648 + printf(
649 + /* translators: %s: Time since last update */
650 + esc_html__('Last update: %s', 'mxchat'),
651 + esc_html($pdf_status['last_update'])
652 + );
653 + ?>
654 + </p>
655 + </div>
656 + <?php endif; ?>
1973 657
1974 - // Add data rows
1975 - foreach ($results as $row) {
1976 - fputcsv($output, array(
1977 - $row->session_id,
1978 - $row->user_email,
1979 - $row->user_identifier,
1980 - $row->role,
1981 - $row->message,
1982 - $row->timestamp
1983 - ));
1984 - }
658 + <?php if ($sitemap_status && $sitemap_status['status'] !== 'complete') : ?>
659 + <div class="mxchat-process-status sitemap" role="alert" aria-live="polite">
660 + <h3><?php esc_html_e('Sitemap Processing Status (Refresh page for update)', 'mxchat'); ?></h3>
661 + <p>
662 + <?php
663 + printf(
664 + /* translators: 1: Processed URLs, 2: Total URLs, 3: Percentage complete */
665 + esc_html__('Progress: %1$d of %2$d URLs (%3$d%%)', 'mxchat'),
666 + absint($sitemap_status['processed_urls']),
667 + absint($sitemap_status['total_urls']),
668 + absint($sitemap_status['percentage'])
669 + );
670 + ?>
671 + </p>
672 + <p>
673 + <?php
674 + printf(
675 + /* translators: %s: Current status */
676 + esc_html__('Status: %s', 'mxchat'),
677 + esc_html(ucfirst($sitemap_status['status']))
678 + );
679 + ?>
680 + </p>
681 + <p>
682 + <?php
683 + printf(
684 + /* translators: %s: Time since last update */
685 + esc_html__('Last update: %s', 'mxchat'),
686 + esc_html($sitemap_status['last_update'])
687 + );
688 + ?>
689 + </p>
690 + </div>
691 + <?php endif; ?>
1985 692
1986 - fclose($output);
1987 - wp_die();
1988 -}
693 + <?php if ($is_processing) : ?>
694 + <form id="mxchat-stop-form" method="post" class="mxchat-stop-form"
695 + action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
1989 696
1990 -// ============================================================================
1991 -// Leads tab (inside Transcripts)
1992 -//
1993 -// Leads are derived from existing data — no dedicated table. Primary source:
1994 -// wp_mxchat_chat_transcripts rows where user_email is populated. Secondary
1995 -// source: wp_options entries `mxchat_email_{session_id}` / `mxchat_name_{sid}`
1996 -// for "orphan" leads who submitted the pre-chat form but never chatted.
1997 -// ============================================================================
697 + <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
1998 698
1999 -/**
2000 - * Fetch leads: dedup-by-email rows, stats strip, and top pages in one call.
2001 - */
2002 -public function mxchat_fetch_leads() {
2003 - if (!current_user_can('manage_options')) {
2004 - wp_send_json_error(['message' => 'Insufficient permissions']);
2005 - wp_die();
2006 - }
699 + <input type="submit"
700 + name="stop_processing"
701 + value="<?php esc_attr_e('Stop Processing', 'mxchat'); ?>"
702 + class="button button-secondary"
703 + />
704 + </form>
705 +<?php endif; ?>
2007 706
2008 - global $wpdb;
2009 - $table = $wpdb->prefix . 'mxchat_chat_transcripts';
707 + </div>
2010 708
2011 - $page = isset($_POST['page']) ? max(1, absint($_POST['page'])) : 1;
2012 - $per_page = isset($_POST['per_page']) ? min(100, max(10, absint($_POST['per_page']))) : 25;
2013 - $offset = ($page - 1) * $per_page;
2014 - $search = isset($_POST['search']) ? sanitize_text_field(wp_unslash($_POST['search'])) : '';
2015 - $date_range = isset($_POST['date_range']) ? sanitize_key($_POST['date_range']) : 'all';
2016 - $status = isset($_POST['status']) ? sanitize_key($_POST['status']) : 'all';
2017 - $page_filter = isset($_POST['page_url']) ? esc_url_raw(wp_unslash($_POST['page_url'])) : '';
2018 - $sort = isset($_POST['sort']) ? sanitize_key($_POST['sort']) : 'last_seen';
2019 - $sort_dir = (isset($_POST['sort_dir']) && $_POST['sort_dir'] === 'asc') ? 'ASC' : 'DESC';
709 + <!-- Search Knowledge Form -->
710 + <div class="mxchat-grid-item">
711 + <h2>Search Knowledge</h2>
712 + <form method="get" id="knowledge-search">
713 + <?php wp_nonce_field('mxchat_prompts_search_nonce'); ?>
714 + <input type="hidden" name="page" value="mxchat-prompts" />
715 + <div class="mxchat-search-group">
716 + <input type="text" name="search" placeholder="Search Knowledge" value="<?php echo esc_attr($search_query); ?>" />
717 + <input type="submit" value="Search" class="button button-primary" />
718 + </div>
719 + </form>
720 + </div>
721 + </div>
2020 722
2021 - $date_cutoff = self::mxchat_leads_date_cutoff($date_range);
2022 - $has_page_url_column = !empty($wpdb->get_results("SHOW COLUMNS FROM $table LIKE 'originating_page_url'"));
723 + <!-- Table navigation with Delete All Button -->
724 + <div class="tablenav">
725 + <div class="tablenav-pages">
726 + <span class="displaying-num"><?php echo esc_html($total_prompts); ?> items</span>
2023 727
2024 - // Base WHERE for transcripts leads.
2025 - $where_clauses = ["user_email IS NOT NULL", "user_email != ''"];
2026 - $where_params = [];
728 + <!-- Delete All Prompts Button -->
729 + <form method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_delete_all_prompts')); ?>" style="display: inline;" onsubmit="return confirm('<?php esc_attr_e('Are you sure you want to delete all prompts? This action cannot be undone.', 'mxchat'); ?>');">
730 + <?php wp_nonce_field('mxchat_delete_all_prompts_action', 'mxchat_delete_all_prompts_nonce'); ?>
731 + <input type="submit" name="delete_all_prompts" value="<?php esc_attr_e('Delete All Knowledge', 'mxchat'); ?>" class="button mxchat-delete-all" />
732 + </form>
2027 733
2028 - if ($date_cutoff) {
2029 - $where_clauses[] = 'timestamp >= %s';
2030 - $where_params[] = $date_cutoff;
2031 - }
2032 - if ($page_filter && $has_page_url_column) {
2033 - $where_clauses[] = 'originating_page_url = %s';
2034 - $where_params[] = $page_filter;
2035 - }
2036 - if ($search !== '') {
2037 - $like = '%' . $wpdb->esc_like($search) . '%';
2038 - $where_clauses[] = '(user_email LIKE %s OR user_name LIKE %s)';
2039 - $where_params[] = $like;
2040 - $where_params[] = $like;
2041 - }
2042 - $where_sql = 'WHERE ' . implode(' AND ', $where_clauses);
734 + <?php
735 + // Display pagination links
736 + $page_links = paginate_links(array(
737 + 'base' => add_query_arg(array('paged' => '%#%', 'search' => urlencode($search_query), '_wpnonce' => wp_create_nonce('mxchat_prompts_search_nonce')), admin_url('admin.php?page=mxchat-prompts')),
738 + 'format' => '',
739 + 'prev_text' => __('&laquo; Previous'),
740 + 'next_text' => __('Next &raquo;'),
741 + 'total' => $total_pages,
742 + 'current' => $current_page,
743 + ));
2043 744
2044 - // Aggregate query grouped by email.
2045 - $select_sql = $has_page_url_column
2046 - ? "SELECT user_email, MAX(timestamp) AS last_seen, MIN(timestamp) AS first_seen,
2047 - COUNT(DISTINCT session_id) AS conversation_count"
2048 - : "SELECT user_email, MAX(timestamp) AS last_seen, MIN(timestamp) AS first_seen,
2049 - COUNT(DISTINCT session_id) AS conversation_count";
745 + if ($page_links) {
746 + echo '<div class="tablenav-pages">' . wp_kses_post($page_links) . '</div>';
747 + }
748 + ?>
749 + </div>
750 + </div>
2050 751
2051 - $order_column = in_array($sort, ['last_seen', 'conversation_count', 'first_seen'], true) ? $sort : 'last_seen';
2052 - $group_order_limit = " GROUP BY user_email ORDER BY {$order_column} {$sort_dir} LIMIT %d OFFSET %d";
752 + <!-- Prompts Table -->
753 + <table class="mxchat-table">
754 + <thead>
755 + <tr>
756 + <th>ID</th>
757 + <th>Article Content</th>
758 + <th>URL</th>
759 + <th>Actions</th>
760 + </tr>
761 + </thead>
762 + <tbody>
763 + <?php if ($prompts) : ?>
764 + <?php foreach ($prompts as $prompt) : ?>
765 + <tr id="prompt-<?php echo esc_attr($prompt->id); ?>">
766 + <td><?php echo esc_html($prompt->id); ?></td>
767 + <td class="mxchat_article_content_dashboard">
768 + <span class="content-view"><?php echo wp_kses_post(wpautop(esc_textarea($prompt->article_content))); ?></span>
769 + <textarea class="content-edit" style="display:none;"><?php echo esc_textarea($prompt->article_content); ?></textarea>
770 + </td>
771 + <td class="mxchat_article_url_dashboard">
772 + <span class="url-view">
773 + <?php if (!empty($prompt->source_url)) : ?>
774 + <a href="<?php echo esc_url($prompt->source_url); ?>" target="_blank"><?php echo esc_html($prompt->source_url); ?></a>
775 + <?php else : ?>
776 + N/A
777 + <?php endif; ?>
778 + </span>
779 + <input type="url" class="url-edit" value="<?php echo esc_attr($prompt->source_url); ?>" style="display:none;" />
780 + </td>
781 + <td>
782 + <button class="button edit-button" data-id="<?php echo esc_attr($prompt->id); ?>">Edit</button>
783 + <button class="button save-button" data-id="<?php echo esc_attr($prompt->id); ?>" style="display:none;">Save</button>
784 + <a href="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_delete_prompt&id=' . esc_attr($prompt->id) . '&_wpnonce=' . wp_create_nonce('mxchat_delete_prompt_nonce'))); ?>" class="button delete-button">Delete</a>
785 + </td>
786 + </tr>
787 + <?php endforeach; ?>
788 + <?php else : ?>
789 + <tr>
790 + <td colspan="4"><?php esc_html_e('No prompts found.', 'mxchat'); ?></td>
791 + </tr>
792 + <?php endif; ?>
793 + </tbody>
794 + </table>
795 + </div>
796 + <?php
797 +}
2053 798
2054 - $transcripts_sql = $wpdb->prepare(
2055 - "{$select_sql} FROM {$table} {$where_sql}{$group_order_limit}",
2056 - array_merge($where_params, [$per_page, $offset])
2057 - );
2058 - $transcript_rows = $wpdb->get_results($transcripts_sql);
2059 799
2060 - // Count of unique transcript-based leads under the same filters.
2061 - $count_sql = $wpdb->prepare(
2062 - "SELECT COUNT(DISTINCT user_email) FROM {$table} {$where_sql}",
2063 - $where_params
2064 - );
2065 - $transcripts_lead_count = (int) $wpdb->get_var($count_sql);
2066 -
2067 - // Hydrate each row: name, latest_session_id, top page.
2068 - $leads = [];
2069 - foreach ($transcript_rows as $row) {
2070 - $detail = $has_page_url_column
2071 - ? $wpdb->get_row($wpdb->prepare(
2072 - "SELECT session_id, user_name, originating_page_url, originating_page_title
2073 - FROM {$table} WHERE user_email = %s ORDER BY timestamp DESC LIMIT 1",
2074 - $row->user_email
2075 - ))
2076 - : $wpdb->get_row($wpdb->prepare(
2077 - "SELECT session_id, user_name FROM {$table}
2078 - WHERE user_email = %s ORDER BY timestamp DESC LIMIT 1",
2079 - $row->user_email
2080 - ));
2081 -
2082 - $leads[] = [
2083 - 'email' => $row->user_email,
2084 - 'name' => isset($detail->user_name) ? (string) $detail->user_name : '',
2085 - 'conversation_count' => (int) $row->conversation_count,
2086 - 'last_seen' => $row->last_seen,
2087 - 'last_seen_display' => self::mxchat_leads_format_relative($row->last_seen),
2088 - 'first_seen' => $row->first_seen,
2089 - 'latest_session_id' => isset($detail->session_id) ? $detail->session_id : '',
2090 - 'top_page_url' => isset($detail->originating_page_url) ? $detail->originating_page_url : '',
2091 - 'top_page_title' => isset($detail->originating_page_title) ? $detail->originating_page_title : '',
2092 - 'is_orphan' => false,
2093 - 'status' => 'active',
2094 - ];
800 +// Delete All Prompts
801 +public function mxchat_handle_delete_all_prompts() {
802 + // Verify nonce
803 + if (!isset($_POST['mxchat_delete_all_prompts_nonce']) || !wp_verify_nonce($_POST['mxchat_delete_all_prompts_nonce'], 'mxchat_delete_all_prompts_action')) {
804 + wp_die(__('Nonce verification failed.', 'mxchat'));
2095 805 }
2096 806
2097 - // Non-transcript lead sources. Built once here, then filtered/merged based on the
2098 - // status filter below. Deduplication priority when the same email appears in multiple
2099 - // sources: transcripts > chat_deleted > orphan.
2100 - $transcripts_emails_seen = array_flip(array_map(
2101 - function ($r) { return strtolower($r['email']); },
2102 - $leads
2103 - ));
2104 -
2105 - // Chat-deleted leads: had a conversation that an admin removed. Preserved via
2106 - // mxchat_lead_del_* options, with timestamps so they respect date filters.
2107 - $chat_deleted_leads_all = [];
2108 - if ($status === 'all' || $status === 'chat_deleted') {
2109 - $chat_deleted_leads_all = self::mxchat_collect_chat_deleted_leads($search, $date_cutoff);
2110 - // Dedup: drop any chat_deleted row whose email is already in the transcripts set.
2111 - $chat_deleted_leads_all = array_values(array_filter(
2112 - $chat_deleted_leads_all,
2113 - function ($row) use ($transcripts_emails_seen) {
2114 - return !isset($transcripts_emails_seen[strtolower($row['email'])]);
2115 - }
2116 - ));
2117 - foreach ($chat_deleted_leads_all as $row) {
2118 - $transcripts_emails_seen[strtolower($row['email'])] = true;
2119 - }
2120 - }
2121 -
2122 - // Orphans: pre-chat form captures with no conversation. No timestamp, so skipped
2123 - // when a date filter is active.
2124 - $orphan_leads_all = [];
2125 - if (($status === 'all' || $status === 'orphan') && !$date_cutoff && !$page_filter) {
2126 - $orphan_leads_all = self::mxchat_collect_orphan_leads($search);
2127 - $orphan_leads_all = array_values(array_filter(
2128 - $orphan_leads_all,
2129 - function ($row) use ($transcripts_emails_seen) {
2130 - return !isset($transcripts_emails_seen[strtolower($row['email'])]);
2131 - }
2132 - ));
2133 - }
2134 -
2135 - // Apply status filter to the transcripts-derived list.
2136 - if ($status === 'orphan' || $status === 'chat_deleted') {
2137 - $leads = [];
2138 - $transcripts_lead_count = 0;
2139 - }
2140 -
2141 - // Stitch the current page from the three buckets in priority order.
2142 - $total_count = $transcripts_lead_count + count($chat_deleted_leads_all) + count($orphan_leads_all);
2143 - $remaining_slots = $per_page - count($leads);
2144 -
2145 - if ($remaining_slots > 0 && !empty($chat_deleted_leads_all)) {
2146 - $start = max(0, ($page - 1) * $per_page - $transcripts_lead_count);
2147 - if ($start < count($chat_deleted_leads_all)) {
2148 - $leads = array_merge($leads, array_slice($chat_deleted_leads_all, $start, $remaining_slots));
2149 - $remaining_slots = $per_page - count($leads);
2150 - }
2151 - }
2152 -
2153 - if ($remaining_slots > 0 && !empty($orphan_leads_all)) {
2154 - $before = $transcripts_lead_count + count($chat_deleted_leads_all);
2155 - $start = max(0, ($page - 1) * $per_page - $before);
2156 - if ($start < count($orphan_leads_all)) {
2157 - $leads = array_merge($leads, array_slice($orphan_leads_all, $start, $remaining_slots));
2158 - }
2159 - }
2160 -
2161 - $total_pages = $per_page > 0 ? (int) ceil($total_count / $per_page) : 1;
2162 -
2163 - // Stats strip: always computed over full dataset, unaffected by filters.
2164 - $stats = self::mxchat_leads_stats($table, $has_page_url_column);
2165 -
2166 - // Top pages: top 5 by distinct emails captured.
2167 - $top_pages = [];
2168 - if ($has_page_url_column) {
2169 - $top_pages_rows = $wpdb->get_results(
2170 - "SELECT originating_page_url AS url,
2171 - MAX(originating_page_title) AS title,
2172 - COUNT(DISTINCT user_email) AS lead_count
2173 - FROM {$table}
2174 - WHERE user_email IS NOT NULL AND user_email != ''
2175 - AND originating_page_url IS NOT NULL AND originating_page_url != ''
2176 - GROUP BY originating_page_url
2177 - ORDER BY lead_count DESC, url ASC
2178 - LIMIT 5"
2179 - );
2180 - foreach ($top_pages_rows as $p) {
2181 - $top_pages[] = [
2182 - 'url' => $p->url,
2183 - 'title' => $p->title ?: $p->url,
2184 - 'lead_count' => (int) $p->lead_count,
2185 - ];
2186 - }
2187 - }
2188 -
2189 - wp_send_json([
2190 - 'success' => true,
2191 - 'leads' => $leads,
2192 - 'page' => $page,
2193 - 'per_page' => $per_page,
2194 - 'total_count' => $total_count,
2195 - 'total_pages' => $total_pages,
2196 - 'showing_start' => $total_count === 0 ? 0 : ($offset + 1),
2197 - 'showing_end' => min($offset + $per_page, $total_count),
2198 - 'stats' => $stats,
2199 - 'top_pages' => $top_pages,
2200 - ]);
2201 - wp_die();
2202 -}
2203 -
2204 -/**
2205 - * Delete one or more leads by email. Removes every transcripts row for that
2206 - * email and cleans up related wp_options (mxchat_email_{sid}, mxchat_name_{sid},
2207 - * mxchat_history_{sid}) and any orphan option entries matching the email.
2208 - */
2209 -public function mxchat_delete_leads() {
807 + // Check permissions
2210 808 if (!current_user_can('manage_options')) {
2211 - wp_send_json_error(['message' => 'Insufficient permissions']);
2212 - wp_die();
809 + wp_die(__('You do not have sufficient permissions to delete all prompts.', 'mxchat'));
2213 810 }
2214 - check_ajax_referer('mxchat_delete_leads', 'security');
2215 811
2216 - $emails_raw = isset($_POST['emails']) ? (array) wp_unslash($_POST['emails']) : [];
2217 - $emails = [];
2218 - foreach ($emails_raw as $e) {
2219 - $clean = sanitize_email((string) $e);
2220 - if ($clean) {
2221 - $emails[] = $clean;
2222 - }
2223 - }
2224 - if (empty($emails)) {
2225 - wp_send_json_error(['message' => 'No emails provided']);
2226 - wp_die();
2227 - }
2228 -
2229 - $summary = self::mxchat_wipe_leads_by_email($emails);
2230 -
2231 - wp_send_json([
2232 - 'success' => true,
2233 - 'deleted_leads' => count($emails),
2234 - 'deleted_sessions' => $summary['deleted_sessions'],
2235 - 'deleted_rows' => $summary['deleted_rows'],
2236 - ]);
2237 - wp_die();
2238 -}
2239 -
2240 -/**
2241 - * Fully wipe one or more leads by email: every transcripts row, every related wp_options
2242 - * entry (history, pre-chat capture, chat_deleted preservation, agent name, translations).
2243 - *
2244 - * Shared between the Leads-tab Delete button and the transcript-delete opt-in checkbox.
2245 - * Input emails must already be sanitized with sanitize_email().
2246 - */
2247 -private static function mxchat_wipe_leads_by_email(array $emails) {
2248 812 global $wpdb;
2249 - $table = $wpdb->prefix . 'mxchat_chat_transcripts';
2250 - $translations_table = $wpdb->prefix . 'mxchat_transcript_translations';
2251 - $has_translations = $wpdb->get_var("SHOW TABLES LIKE '$translations_table'") === $translations_table;
813 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2252 814
2253 - $deleted_sessions = 0;
2254 - $deleted_rows = 0;
2255 - $emails_lc = array_map('strtolower', $emails);
815 + // Delete all prompts from the table
816 + $wpdb->query("DELETE FROM {$table_name}");
2256 817
2257 - foreach ($emails as $email) {
2258 - $session_ids = $wpdb->get_col($wpdb->prepare(
2259 - "SELECT DISTINCT session_id FROM {$table} WHERE user_email = %s",
2260 - $email
2261 - ));
818 + // Clear relevant cache
819 + wp_cache_delete('all_prompts', 'mxchat_prompts');
2262 820
2263 - $rows_removed = $wpdb->delete($table, ['user_email' => $email], ['%s']);
2264 - if ($rows_removed !== false) {
2265 - $deleted_rows += (int) $rows_removed;
2266 - }
821 + // Redirect back with a success message
822 + $redirect_url = add_query_arg(array(
823 + 'page' => 'mxchat-prompts',
824 + 'all_deleted' => 'true'
825 + ), admin_url('admin.php'));
2267 826
2268 - foreach ($session_ids as $sid) {
2269 - $deleted_sessions++;
2270 - wp_cache_delete('chat_session_' . $sid, 'mxchat_chat_sessions');
2271 - delete_option('mxchat_history_' . $sid);
2272 - delete_option('mxchat_email_' . $sid);
2273 - delete_option('mxchat_name_' . $sid);
2274 - delete_option('mxchat_agent_name_' . $sid);
2275 - delete_option('mxchat_lead_del_email_' . $sid);
2276 - delete_option('mxchat_lead_del_name_' . $sid);
2277 - delete_option('mxchat_lead_del_ts_' . $sid);
2278 - if ($has_translations) {
2279 - $wpdb->delete($translations_table, ['session_id' => $sid], ['%s']);
2280 - }
2281 - }
2282 - }
827 + wp_safe_redirect($redirect_url);
828 + exit;
829 +}
2283 830
2284 - // Clean up any lingering option entries (orphan pre-chat captures + chat_deleted
2285 - // preservations) whose stored value matches one of the emails being wiped.
2286 - $lingering = $wpdb->get_results(
2287 - "SELECT option_name, option_value FROM {$wpdb->options}
2288 - WHERE option_name LIKE 'mxchat_email_%' OR option_name LIKE 'mxchat_lead_del_email_%'"
2289 - );
2290 - foreach ($lingering as $opt) {
2291 - if (!in_array(strtolower(trim($opt->option_value)), $emails_lc, true)) {
2292 - continue;
2293 - }
2294 - if (strpos($opt->option_name, 'mxchat_lead_del_email_') === 0) {
2295 - $sid = substr($opt->option_name, strlen('mxchat_lead_del_email_'));
2296 - delete_option('mxchat_lead_del_email_' . $sid);
2297 - delete_option('mxchat_lead_del_name_' . $sid);
2298 - delete_option('mxchat_lead_del_ts_' . $sid);
2299 - } else {
2300 - $sid = substr($opt->option_name, strlen('mxchat_email_'));
2301 - delete_option('mxchat_email_' . $sid);
2302 - delete_option('mxchat_name_' . $sid);
2303 - }
831 +// Single Prompt Deletion
832 +public function mxchat_handle_delete_prompt() {
833 + // Sanitize and validate nonce
834 + $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : '';
835 + if (empty($nonce) || !wp_verify_nonce($nonce, 'mxchat_delete_prompt_nonce')) {
836 + wp_die('Nonce verification failed.');
2304 837 }
2305 838
2306 - wp_cache_delete('all_chat_sessions', 'mxchat_chat_sessions');
2307 -
2308 - return [
2309 - 'deleted_sessions' => $deleted_sessions,
2310 - 'deleted_rows' => $deleted_rows,
2311 - ];
2312 -}
2313 -
2314 -/**
2315 - * Stream a leads CSV. scope=all exports every lead under current filters is not
2316 - * supported to keep semantics simple; caller either exports all leads or a
2317 - * specific set of selected emails.
2318 - */
2319 -public function mxchat_export_leads() {
839 + // Check permissions
2320 840 if (!current_user_can('manage_options')) {
2321 - wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'mxchat'));
841 + wp_die('You do not have sufficient permissions to delete prompts.');
2322 842 }
2323 - check_ajax_referer('mxchat_export_leads', 'security');
2324 843
2325 - $scope = isset($_POST['scope']) ? sanitize_key($_POST['scope']) : 'all';
2326 - $fields_mode = isset($_POST['fields']) ? sanitize_key($_POST['fields']) : 'email_and_name';
2327 - $emails_in = isset($_POST['emails']) ? (array) wp_unslash($_POST['emails']) : [];
2328 -
2329 - $emails_in_clean = [];
2330 - foreach ($emails_in as $e) {
2331 - $clean = sanitize_email((string) $e);
2332 - if ($clean) {
2333 - $emails_in_clean[] = $clean;
2334 - }
844 + // Validate and sanitize ID parameter
845 + $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
846 + if ($id <= 0) {
847 + wp_die('Invalid prompt ID.');
2335 848 }
2336 849
2337 850 global $wpdb;
2338 - $table = $wpdb->prefix . 'mxchat_chat_transcripts';
2339 - $has_page_url_column = !empty($wpdb->get_results("SHOW COLUMNS FROM $table LIKE 'originating_page_url'"));
851 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2340 852
2341 - // Collect leads from transcripts.
2342 - $transcripts_sql = "SELECT user_email AS email,
2343 - MAX(timestamp) AS last_seen,
2344 - COUNT(DISTINCT session_id) AS conversation_count
2345 - FROM {$table}
2346 - WHERE user_email IS NOT NULL AND user_email != ''";
2347 - $params = [];
2348 - if ($scope === 'selected' && !empty($emails_in_clean)) {
2349 - $placeholders = implode(',', array_fill(0, count($emails_in_clean), '%s'));
2350 - $transcripts_sql .= " AND user_email IN ({$placeholders})";
2351 - $params = $emails_in_clean;
2352 - }
2353 - $transcripts_sql .= " GROUP BY user_email ORDER BY last_seen DESC";
853 + // Clear cache and delete prompt
854 + wp_cache_delete('prompt_' . $id, 'mxchat_prompts');
855 + $wpdb->delete($table_name, array('id' => $id), array('%d'));
2354 856
2355 - $rows = !empty($params)
2356 - ? $wpdb->get_results($wpdb->prepare($transcripts_sql, $params))
2357 - : $wpdb->get_results($transcripts_sql);
2358 -
2359 - // Hydrate each row with name + top page.
2360 - $export_rows = [];
2361 - foreach ($rows as $row) {
2362 - $detail = $has_page_url_column
2363 - ? $wpdb->get_row($wpdb->prepare(
2364 - "SELECT user_name, originating_page_url FROM {$table}
2365 - WHERE user_email = %s ORDER BY timestamp DESC LIMIT 1",
2366 - $row->email
2367 - ))
2368 - : $wpdb->get_row($wpdb->prepare(
2369 - "SELECT user_name FROM {$table}
2370 - WHERE user_email = %s ORDER BY timestamp DESC LIMIT 1",
2371 - $row->email
2372 - ));
2373 - $export_rows[] = [
2374 - 'email' => $row->email,
2375 - 'name' => isset($detail->user_name) ? (string) $detail->user_name : '',
2376 - 'conversation_count' => (int) $row->conversation_count,
2377 - 'last_seen' => $row->last_seen,
2378 - 'top_page_url' => isset($detail->originating_page_url) ? $detail->originating_page_url : '',
2379 - ];
2380 - }
2381 -
2382 - // Include orphan + chat_deleted leads when exporting all.
2383 - if ($scope === 'all') {
2384 - $transcripts_emails_lc = array_flip(array_map(
2385 - function ($r) { return strtolower($r['email']); },
2386 - $export_rows
2387 - ));
2388 - foreach (self::mxchat_collect_chat_deleted_leads('') as $cd) {
2389 - if (isset($transcripts_emails_lc[strtolower($cd['email'])])) continue;
2390 - $transcripts_emails_lc[strtolower($cd['email'])] = true;
2391 - $export_rows[] = [
2392 - 'email' => $cd['email'],
2393 - 'name' => $cd['name'],
2394 - 'conversation_count' => 0,
2395 - 'last_seen' => $cd['last_seen'],
2396 - 'top_page_url' => '',
2397 - ];
2398 - }
2399 - foreach (self::mxchat_collect_orphan_leads('') as $orphan) {
2400 - if (isset($transcripts_emails_lc[strtolower($orphan['email'])])) continue;
2401 - $transcripts_emails_lc[strtolower($orphan['email'])] = true;
2402 - $export_rows[] = [
2403 - 'email' => $orphan['email'],
2404 - 'name' => $orphan['name'],
2405 - 'conversation_count' => 0,
2406 - 'last_seen' => '',
2407 - 'top_page_url' => '',
2408 - ];
2409 - }
2410 - }
2411 -
2412 - if (empty($export_rows)) {
2413 - wp_send_json_error(['message' => 'No leads to export.']);
2414 - wp_die();
2415 - }
2416 -
2417 - $filename = 'mxchat-leads-' . date('Y-m-d') . '.csv';
2418 - header('Content-Type: text/csv');
2419 - header('Content-Disposition: attachment; filename="' . $filename . '"');
2420 - header('Pragma: no-cache');
2421 - header('Expires: 0');
2422 -
2423 - $output = fopen('php://output', 'w');
2424 - fputs($output, "\xEF\xBB\xBF"); // UTF-8 BOM for Excel
2425 -
2426 - if ($fields_mode === 'email_only') {
2427 - fputcsv($output, ['Email']);
2428 - foreach ($export_rows as $r) {
2429 - fputcsv($output, [$r['email']]);
2430 - }
2431 - } else {
2432 - fputcsv($output, ['Email', 'Name', 'Conversations', 'Last seen', 'Top page']);
2433 - foreach ($export_rows as $r) {
2434 - fputcsv($output, [
2435 - $r['email'],
2436 - $r['name'],
2437 - $r['conversation_count'],
2438 - $r['last_seen'],
2439 - $r['top_page_url'],
2440 - ]);
2441 - }
2442 - }
2443 -
2444 - fclose($output);
2445 - wp_die();
857 + wp_safe_redirect(add_query_arg(array('page' => 'mxchat-prompts', 'deleted' => 'true'), admin_url('admin.php')));
858 + exit;
2446 859 }
2447 860
2448 -/**
2449 - * Stats strip payload (independent of filters).
2450 - */
2451 -private static function mxchat_leads_stats($table, $has_page_url_column) {
2452 - global $wpdb;
2453 861
2454 - $total_transcripts_emails = (int) $wpdb->get_var(
2455 - "SELECT COUNT(DISTINCT user_email) FROM {$table}
2456 - WHERE user_email IS NOT NULL AND user_email != ''"
2457 - );
2458 862
2459 - $new_this_week = (int) $wpdb->get_var($wpdb->prepare(
2460 - "SELECT COUNT(*) FROM (
2461 - SELECT user_email FROM {$table}
2462 - WHERE user_email IS NOT NULL AND user_email != ''
2463 - GROUP BY user_email
2464 - HAVING MIN(timestamp) >= %s
2465 - ) AS new_leads",
2466 - gmdate('Y-m-d H:i:s', strtotime('-7 days'))
2467 - ));
2468 863
2469 - $total_convos = (int) $wpdb->get_var(
2470 - "SELECT COUNT(DISTINCT session_id) FROM {$table}
2471 - WHERE user_email IS NOT NULL AND user_email != ''"
2472 - );
864 + public function mxchat_generate_embedding($text) {
865 + $options = get_option('mxchat_options');
866 + $api_key = $options['api_key'] ?? 'default_api_key';
2473 867
2474 - $orphan_count = count(self::mxchat_collect_orphan_leads(''));
2475 - $chat_deleted_count = self::mxchat_count_chat_deleted_leads();
868 + $response = wp_remote_post('https://api.openai.com/v1/embeddings', array(
869 + 'body' => wp_json_encode(array(
870 + 'model' => 'text-embedding-ada-002',
871 + 'input' => $text
872 + )),
873 + 'headers' => array(
874 + 'Authorization' => 'Bearer ' . $api_key,
875 + 'Content-Type' => 'application/json'
876 + ),
877 + ));
2476 878
2477 - // Total leads = unique emails across all three sources (dedup priority: transcripts > chat_deleted > orphan
2478 - // is already enforced at collection time in mxchat_fetch_leads; stats re-apply it here).
2479 - $total_leads = $total_transcripts_emails + $chat_deleted_count + $orphan_count;
2480 -
2481 - $avg = $total_transcripts_emails > 0
2482 - ? round($total_convos / $total_transcripts_emails, 1)
2483 - : 0;
2484 -
2485 - // Orphan % reflects *true* orphans only (pre-chat dropoffs). Chat-deleted leads are
2486 - // excluded so the metric stays meaningful — admins shouldn't see their cleanups
2487 - // inflate this number.
2488 - $orphan_pct = $total_leads > 0
2489 - ? (int) round(($orphan_count / $total_leads) * 100)
2490 - : 0;
2491 -
2492 - return [
2493 - 'total_leads' => $total_leads,
2494 - 'new_this_week' => $new_this_week,
2495 - 'avg_convos' => $avg,
2496 - 'orphan_pct' => $orphan_pct,
2497 - 'orphan_count' => $orphan_count,
2498 - 'chat_deleted_count' => $chat_deleted_count,
2499 - ];
2500 -}
2501 -
2502 -/**
2503 - * Collect leads who had a conversation that an admin later deleted (preserved via
2504 - * mxchat_lead_del_* options). Returns rows tagged status='chat_deleted' with the
2505 - * original last-seen timestamp so they still sort and filter sensibly.
2506 - *
2507 - * @param string $search Optional email/name substring filter.
2508 - * @param string $date_cutoff Optional 'Y-m-d H:i:s' cutoff — only rows with last_ts >= cutoff.
2509 - * @return array
2510 - */
2511 -private static function mxchat_collect_chat_deleted_leads($search = '', $date_cutoff = '') {
2512 - global $wpdb;
2513 - $table = $wpdb->prefix . 'mxchat_chat_transcripts';
2514 -
2515 - $rows = $wpdb->get_results(
2516 - "SELECT option_name, option_value FROM {$wpdb->options}
2517 - WHERE option_name LIKE 'mxchat_lead_del_email_%'"
2518 - );
2519 - if (empty($rows)) {
2520 - return [];
2521 - }
2522 -
2523 - // Emails that currently have transcripts rows should not appear as chat_deleted —
2524 - // they've come back and chatted, so they're active leads again.
2525 - $emails_in_transcripts = array_map(
2526 - 'strtolower',
2527 - (array) $wpdb->get_col(
2528 - "SELECT DISTINCT user_email FROM {$table}
2529 - WHERE user_email IS NOT NULL AND user_email != ''"
2530 - )
2531 - );
2532 - $emails_in_transcripts = array_flip($emails_in_transcripts);
2533 -
2534 - $needle = strtolower(trim((string) $search));
2535 - $by_email = [];
2536 -
2537 - foreach ($rows as $opt) {
2538 - $email = sanitize_email(trim((string) $opt->option_value));
2539 - if (!$email) {
2540 - continue;
879 + if (is_wp_error($response)) {
880 + return null;
2541 881 }
2542 - if (isset($emails_in_transcripts[strtolower($email)])) {
2543 - continue;
2544 - }
2545 - $sid = substr($opt->option_name, strlen('mxchat_lead_del_email_'));
2546 - if (!$sid) {
2547 - continue;
2548 - }
2549 - $name = (string) get_option('mxchat_lead_del_name_' . $sid, '');
2550 - $ts = (string) get_option('mxchat_lead_del_ts_' . $sid, '');
2551 882
2552 - if ($date_cutoff !== '' && ($ts === '' || $ts < $date_cutoff)) {
2553 - continue;
2554 - }
2555 - if ($needle !== '') {
2556 - $hay = strtolower($email . ' ' . $name);
2557 - if (strpos($hay, $needle) === false) {
2558 - continue;
2559 - }
2560 - }
2561 -
2562 - $key = strtolower($email);
2563 - if (!isset($by_email[$key]) || (isset($by_email[$key]['last_seen']) && $ts > $by_email[$key]['last_seen'])) {
2564 - $by_email[$key] = [
2565 - 'email' => $email,
2566 - 'name' => $name,
2567 - 'conversation_count' => 0,
2568 - 'last_seen' => $ts,
2569 - 'last_seen_display' => $ts ? self::mxchat_leads_format_relative($ts) : __('Chat deleted', 'mxchat'),
2570 - 'first_seen' => $ts,
2571 - 'latest_session_id' => '',
2572 - 'top_page_url' => '',
2573 - 'top_page_title' => '',
2574 - 'is_orphan' => false,
2575 - 'status' => 'chat_deleted',
2576 - ];
2577 - }
883 + $response_data = json_decode(wp_remote_retrieve_body($response), true);
884 + return $response_data['data'][0]['embedding'] ?? null;
2578 885 }
2579 886
2580 - // Newest chat_deleted first.
2581 - usort($by_email, function ($a, $b) {
2582 - return strcmp((string) $b['last_seen'], (string) $a['last_seen']);
2583 - });
2584 - return array_values($by_email);
2585 -}
2586 -
2587 -/**
2588 - * Count unique emails preserved as "chat deleted" (for the stats strip).
2589 - */
2590 -private static function mxchat_count_chat_deleted_leads() {
2591 - global $wpdb;
2592 - $table = $wpdb->prefix . 'mxchat_chat_transcripts';
2593 -
2594 - $emails = $wpdb->get_col(
2595 - "SELECT DISTINCT option_value FROM {$wpdb->options}
2596 - WHERE option_name LIKE 'mxchat_lead_del_email_%'"
2597 - );
2598 - if (empty($emails)) {
2599 - return 0;
2600 - }
2601 -
2602 - $transcripts_emails = array_map(
2603 - 'strtolower',
2604 - (array) $wpdb->get_col(
2605 - "SELECT DISTINCT user_email FROM {$table}
2606 - WHERE user_email IS NOT NULL AND user_email != ''"
2607 - )
2608 - );
2609 - $transcripts_emails = array_flip($transcripts_emails);
2610 -
2611 - $count = 0;
2612 - $seen = [];
2613 - foreach ($emails as $raw) {
2614 - $email = strtolower(trim((string) $raw));
2615 - if (!$email || isset($seen[$email]) || isset($transcripts_emails[$email])) {
2616 - continue;
887 + public function mxchat_delete_chat_history() {
888 + if (!current_user_can('manage_options')) {
889 + echo wp_json_encode(['error' => 'You do not have sufficient permissions.']);
890 + wp_die();
2617 891 }
2618 - $seen[$email] = true;
2619 - $count++;
2620 - }
2621 - return $count;
2622 -}
2623 892
2624 -/**
2625 - * Find leads who submitted the pre-chat form but never produced a transcripts row.
2626 - * Returned rows have no conversation_count, no timestamp.
2627 - *
2628 - * @param string $search Optional email/name substring filter.
2629 - * @return array
2630 - */
2631 -private static function mxchat_collect_orphan_leads($search = '') {
2632 - global $wpdb;
2633 - $table = $wpdb->prefix . 'mxchat_chat_transcripts';
893 + check_ajax_referer('mxchat_delete_chat_history', 'security');
2634 894
2635 - $option_rows = $wpdb->get_results(
2636 - "SELECT option_name, option_value FROM {$wpdb->options}
2637 - WHERE option_name LIKE 'mxchat_email_%'"
2638 - );
2639 - if (empty($option_rows)) {
2640 - return [];
2641 - }
895 + global $wpdb;
896 + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
2642 897
2643 - // Collect all session_ids that have real transcripts rows so we can exclude them.
2644 - $session_ids_with_rows = $wpdb->get_col(
2645 - "SELECT DISTINCT session_id FROM {$table}
2646 - WHERE user_email IS NOT NULL AND user_email != ''"
2647 - );
2648 - $session_ids_with_rows = array_flip($session_ids_with_rows);
898 + if (isset($_POST['delete_session_ids']) && is_array($_POST['delete_session_ids'])) {
899 + foreach ($_POST['delete_session_ids'] as $session_id) {
900 + $session_id_sanitized = sanitize_text_field($session_id);
2649 901
2650 - // Seen emails in transcripts (so orphans only include truly never-chatted leads).
2651 - $emails_in_transcripts = array_map(
2652 - 'strtolower',
2653 - (array) $wpdb->get_col(
2654 - "SELECT DISTINCT user_email FROM {$table}
2655 - WHERE user_email IS NOT NULL AND user_email != ''"
2656 - )
2657 - );
2658 - $emails_in_transcripts = array_flip($emails_in_transcripts);
902 + // Clear relevant cache before deletion
903 + $cache_key = 'chat_session_' . $session_id_sanitized;
904 + wp_cache_delete($cache_key, 'mxchat_chat_sessions');
2659 905
2660 - $orphans_by_email = [];
2661 - $needle = strtolower(trim((string) $search));
2662 -
2663 - foreach ($option_rows as $opt) {
2664 - $email = sanitize_email(trim((string) $opt->option_value));
2665 - if (!$email) {
2666 - continue;
2667 - }
2668 - $sid = substr($opt->option_name, strlen('mxchat_email_'));
2669 - if (!$sid) {
2670 - continue;
2671 - }
2672 - // Exclude leads who have any transcripts rows (they appear in the main list).
2673 - if (isset($emails_in_transcripts[strtolower($email)])) {
2674 - continue;
2675 - }
2676 - if (isset($session_ids_with_rows[$sid])) {
2677 - continue;
2678 - }
2679 -
2680 - $name_option = get_option('mxchat_name_' . $sid, '');
2681 - $name = is_string($name_option) ? trim($name_option) : '';
2682 -
2683 - if ($needle !== '') {
2684 - $hay = strtolower($email . ' ' . $name);
2685 - if (strpos($hay, $needle) === false) {
2686 - continue;
906 + // Perform the deletion
907 + $wpdb->delete($table_name, ['session_id' => $session_id_sanitized]);
2687 908 }
2688 - }
2689 909
2690 - $key = strtolower($email);
2691 - if (!isset($orphans_by_email[$key])) {
2692 - $orphans_by_email[$key] = [
2693 - 'email' => $email,
2694 - 'name' => $name,
2695 - 'conversation_count' => 0,
2696 - 'last_seen' => '',
2697 - 'last_seen_display' => __('No conversation yet', 'mxchat'),
2698 - 'first_seen' => '',
2699 - 'latest_session_id' => '',
2700 - 'top_page_url' => '',
2701 - 'top_page_title' => '',
2702 - 'is_orphan' => true,
2703 - 'status' => 'orphan',
2704 - ];
2705 - }
2706 - }
910 + // Optionally, clear a general cache if you have one
911 + wp_cache_delete('all_chat_sessions', 'mxchat_chat_sessions');
2707 912
2708 - return array_values($orphans_by_email);
2709 -}
2710 -
2711 -/**
2712 - * Map a date_range key to a SQL-comparable cutoff string, or '' for all-time.
2713 - */
2714 -private static function mxchat_leads_date_cutoff($date_range) {
2715 - switch ($date_range) {
2716 - case 'today': return gmdate('Y-m-d H:i:s', strtotime('-24 hours'));
2717 - case '7d': return gmdate('Y-m-d H:i:s', strtotime('-7 days'));
2718 - case '30d': return gmdate('Y-m-d H:i:s', strtotime('-30 days'));
2719 - case '90d': return gmdate('Y-m-d H:i:s', strtotime('-90 days'));
2720 - case 'all':
2721 - default: return '';
2722 - }
2723 -}
2724 -
2725 -/**
2726 - * Turn a UTC timestamp into a short relative display like "2h ago" or "Apr 12".
2727 - */
2728 -private static function mxchat_leads_format_relative($timestamp) {
2729 - if (!$timestamp) {
2730 - return '';
2731 - }
2732 - $ts = strtotime($timestamp . ' UTC');
2733 - if (!$ts) {
2734 - return '';
2735 - }
2736 - $diff = time() - $ts;
2737 - if ($diff < 60) return __('just now', 'mxchat');
2738 - if ($diff < 3600) return floor($diff / 60) . __('m ago', 'mxchat');
2739 - if ($diff < 86400) return floor($diff / 3600) . __('h ago', 'mxchat');
2740 - if ($diff < 604800) return floor($diff / 86400) . __('d ago', 'mxchat');
2741 - return wp_date('M j', $ts);
2742 -}
2743 -
2744 -/**
2745 - * Handle translation of chat messages via AJAX
2746 - */
2747 -public function mxchat_translate_messages() {
2748 - if (!current_user_can('manage_options')) {
2749 - wp_send_json_error(['error' => 'Insufficient permissions']);
2750 - wp_die();
2751 - }
2752 -
2753 - $session_id = isset($_POST['session_id']) ? MxChat_Utils::sanitize_session_id(wp_unslash($_POST['session_id'])) : '';
2754 - $target_lang = isset($_POST['target_lang']) ? sanitize_text_field($_POST['target_lang']) : 'en';
2755 - $messages_json = isset($_POST['messages']) ? wp_unslash($_POST['messages']) : '[]';
2756 - $messages = json_decode($messages_json, true);
2757 -
2758 - if (empty($session_id)) {
2759 - wp_send_json_error(['error' => 'No session ID provided']);
2760 - wp_die();
2761 - }
2762 -
2763 - if (empty($messages) || !is_array($messages)) {
2764 - wp_send_json_error(['error' => 'No messages to translate']);
2765 - wp_die();
2766 - }
2767 -
2768 - // Language names for prompting
2769 - $languages = [
2770 - 'en' => 'English',
2771 - 'es' => 'Spanish',
2772 - 'fr' => 'French',
2773 - 'de' => 'German',
2774 - 'it' => 'Italian',
2775 - 'pt' => 'Portuguese',
2776 - 'nl' => 'Dutch',
2777 - 'ru' => 'Russian',
2778 - 'zh' => 'Chinese',
2779 - 'ja' => 'Japanese',
2780 - 'ko' => 'Korean',
2781 - 'ar' => 'Arabic',
2782 - 'hi' => 'Hindi',
2783 - 'tr' => 'Turkish',
2784 - 'pl' => 'Polish',
2785 - 'vi' => 'Vietnamese',
2786 - 'th' => 'Thai',
2787 - 'id' => 'Indonesian',
2788 - 'sv' => 'Swedish',
2789 - 'da' => 'Danish'
2790 - ];
2791 -
2792 - $target_lang_name = isset($languages[$target_lang]) ? $languages[$target_lang] : 'English';
2793 -
2794 - // Build combined text for translation (numbered for parsing)
2795 - $numbered_messages = [];
2796 - foreach ($messages as $i => $msg) {
2797 - $content = isset($msg['content']) ? trim($msg['content']) : '';
2798 - if (!empty($content)) {
2799 - $numbered_messages[] = "[MSG" . $i . "]" . $content . "[/MSG" . $i . "]";
913 + echo wp_json_encode(['success' => 'Selected chat sessions have been deleted.']);
914 + } else {
915 + echo wp_json_encode(['error' => 'No chat sessions selected for deletion.']);
2800 916 }
2801 - }
2802 917
2803 - if (empty($numbered_messages)) {
2804 - wp_send_json_error(['error' => 'No valid messages to translate']);
2805 918 wp_die();
2806 919 }
2807 920
2808 - $combined_text = implode("\n\n", $numbered_messages);
2809 921
2810 - // Prepare the translation prompt
2811 - $system_prompt = "You are a translator. Translate the following messages to {$target_lang_name}. Keep the [MSG#] and [/MSG#] tags exactly as they are - only translate the content between them. Maintain the original formatting, line breaks, and any HTML tags. Return ONLY the translated messages with the tags, no explanations.";
922 +public function mxchat_save_inline_prompt() {
923 + // Check for nonce security
924 + check_ajax_referer('mxchat_save_inline_nonce');
2812 925
2813 - // Get user's selected model and determine provider
2814 - $options = get_option('mxchat_options', []);
2815 - $selected_model = $options['model'] ?? 'gpt-5.6-sol';
2816 -
2817 - // Check if using OpenRouter
2818 - if ($selected_model === 'openrouter') {
2819 - $provider = 'openrouter';
2820 - $selected_model = $options['openrouter_selected_model'] ?? '';
2821 - $api_key = $options['openrouter_api_key'] ?? '';
2822 -
2823 - if (empty($selected_model)) {
2824 - wp_send_json_error(['error' => 'No OpenRouter model selected']);
2825 - wp_die();
2826 - }
2827 - } else {
2828 - // Determine provider from model name
2829 - $model_parts = explode('-', $selected_model);
2830 - $provider = strtolower($model_parts[0]);
2831 -
2832 - // Get the appropriate API key based on provider
2833 - $api_key = '';
2834 - switch ($provider) {
2835 - case 'gpt':
2836 - case 'o1':
2837 - $api_key = $options['api_key'] ?? '';
2838 - break;
2839 - case 'claude':
2840 - $api_key = $options['claude_api_key'] ?? '';
2841 - break;
2842 - case 'grok':
2843 - $api_key = $options['xai_api_key'] ?? '';
2844 - break;
2845 - case 'deepseek':
2846 - $api_key = $options['deepseek_api_key'] ?? '';
2847 - break;
2848 - case 'gemini':
2849 - $api_key = $options['gemini_api_key'] ?? '';
2850 - break;
2851 - default:
2852 - // Default to OpenAI
2853 - $api_key = $options['api_key'] ?? '';
2854 - $provider = 'gpt';
2855 - break;
2856 - }
926 + // Verify permissions
927 + if (!current_user_can('manage_options')) {
928 + wp_send_json_error('Permission denied.');
929 + return;
2857 930 }
2858 931
2859 - if (empty($api_key)) {
2860 - wp_send_json_error(['error' => 'No API key configured for ' . $provider]);
2861 - wp_die();
2862 - }
2863 -
2864 - // Make API request based on provider
2865 - $response = $this->translate_with_provider($provider, $selected_model, $api_key, $system_prompt, $combined_text);
2866 -
2867 - if (is_wp_error($response)) {
2868 - wp_send_json_error(['error' => $response->get_error_message()]);
2869 - wp_die();
2870 - }
2871 -
2872 - // Parse the response to extract translated messages
2873 - $translations = [];
2874 - foreach ($messages as $msg) {
2875 - $index = $msg['index'];
2876 - $pattern = '/\[MSG' . $index . '\](.*?)\[\/MSG' . $index . '\]/s';
2877 - if (preg_match($pattern, $response, $matches)) {
2878 - $translations[] = [
2879 - 'index' => $index,
2880 - 'translated' => trim($matches[1])
2881 - ];
2882 - }
2883 - }
2884 -
2885 - // Save translations to database
2886 - if (!empty($translations)) {
2887 - $this->save_transcript_translation($session_id, $target_lang, $translations);
2888 - }
2889 -
2890 - wp_send_json(['success' => true, 'translations' => $translations, 'language' => $target_lang]);
2891 - wp_die();
2892 -}
2893 -
2894 -/**
2895 - * Save transcript translation to database
2896 - */
2897 -private function save_transcript_translation($session_id, $language_code, $translations) {
2898 932 global $wpdb;
2899 - $table_name = $wpdb->prefix . 'mxchat_transcript_translations';
933 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2900 934
2901 - // Check if table exists, create if not
2902 - if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") !== $table_name) {
2903 - mxchat_create_translations_table();
2904 - }
935 + // Validate and sanitize input data
936 + $prompt_id = isset($_POST['id']) ? absint($_POST['id']) : 0;
937 + $article_content = isset($_POST['article_content']) ? sanitize_textarea_field($_POST['article_content']) : '';
938 + $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
2905 939
2906 - $now = current_time('mysql');
2907 - $translations_json = wp_json_encode($translations);
940 + if ($prompt_id > 0 && !empty($article_content)) {
941 + // Re-generate the embedding vector for the updated content
942 + $embedding_vector = $this->mxchat_generate_embedding($article_content);
2908 943
2909 - // Use REPLACE to insert or update
2910 - $wpdb->query($wpdb->prepare(
2911 - "REPLACE INTO $table_name (session_id, language_code, translations, created_at, updated_at)
2912 - VALUES (%s, %s, %s, %s, %s)",
2913 - $session_id,
2914 - $language_code,
2915 - $translations_json,
2916 - $now,
2917 - $now
2918 - ));
2919 -}
944 + if (is_array($embedding_vector)) {
945 + // Serialize the embedding vector before storing it
946 + $embedding_vector_serialized = serialize($embedding_vector);
2920 947
2921 -/**
2922 - * Get saved translation for a session
2923 - */
2924 -public function mxchat_get_transcript_translation() {
2925 - if (!current_user_can('manage_options')) {
2926 - wp_send_json_error(['error' => 'Insufficient permissions']);
2927 - wp_die();
2928 - }
948 + // Update the prompt in the database
949 + $updated = $wpdb->update(
950 + $table_name,
951 + array(
952 + 'article_content' => $article_content,
953 + 'embedding_vector' => $embedding_vector_serialized,
954 + 'source_url' => $article_url,
955 + ),
956 + array('id' => $prompt_id),
957 + array('%s', '%s', '%s'),
958 + array('%d')
959 + );
2929 960
2930 - $session_id = isset($_POST['session_id']) ? MxChat_Utils::sanitize_session_id(wp_unslash($_POST['session_id'])) : '';
2931 -
2932 - if (empty($session_id)) {
2933 - wp_send_json_error(['error' => 'No session ID provided']);
2934 - wp_die();
2935 - }
2936 -
2937 - global $wpdb;
2938 - $table_name = $wpdb->prefix . 'mxchat_transcript_translations';
2939 -
2940 - // Check if table exists
2941 - if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") !== $table_name) {
2942 - wp_send_json(['success' => true, 'has_translation' => false]);
2943 - wp_die();
2944 - }
2945 -
2946 - // Get the most recent translation for this session
2947 - $result = $wpdb->get_row($wpdb->prepare(
2948 - "SELECT language_code, translations FROM $table_name WHERE session_id = %s ORDER BY updated_at DESC LIMIT 1",
2949 - $session_id
2950 - ));
2951 -
2952 - if ($result) {
2953 - $translations = json_decode($result->translations, true);
2954 - wp_send_json([
2955 - 'success' => true,
2956 - 'has_translation' => true,
2957 - 'language' => $result->language_code,
2958 - 'translations' => $translations
2959 - ]);
961 + if ($updated !== false) {
962 + wp_send_json_success();
963 + } else {
964 + wp_send_json_error('Database update failed.');
965 + }
966 + } else {
967 + wp_send_json_error('Embedding generation failed.');
968 + }
2960 969 } else {
2961 - wp_send_json(['success' => true, 'has_translation' => false]);
970 + wp_send_json_error('Invalid data.');
2962 971 }
2963 - wp_die();
2964 972 }
2965 973
2966 -/**
2967 - * Translate text using the user's selected provider and model
2968 - */
2969 -private function translate_with_provider($provider, $model, $api_key, $system_prompt, $text) {
2970 - switch ($provider) {
2971 - case 'claude':
2972 - return $this->translate_with_claude($api_key, $model, $system_prompt, $text);
2973 - case 'grok':
2974 - return $this->translate_with_xai($api_key, $model, $system_prompt, $text);
2975 - case 'deepseek':
2976 - return $this->translate_with_deepseek($api_key, $model, $system_prompt, $text);
2977 - case 'gemini':
2978 - return $this->translate_with_gemini($api_key, $model, $system_prompt, $text);
2979 - case 'openrouter':
2980 - return $this->translate_with_openrouter($api_key, $model, $system_prompt, $text);
2981 - case 'gpt':
2982 - case 'o1':
2983 - default:
2984 - return $this->translate_with_openai($api_key, $model, $system_prompt, $text);
2985 - }
2986 -}
2987 -
2988 -/**
2989 - * Translate text using OpenAI API
2990 - */
2991 -private function translate_with_openai($api_key, $model, $system_prompt, $text) {
2992 - $response = wp_remote_post('https://api.openai.com/v1/chat/completions', [
2993 - 'timeout' => 60,
2994 - 'headers' => [
2995 - 'Authorization' => 'Bearer ' . $api_key,
2996 - 'Content-Type' => 'application/json'
2997 - ],
2998 - 'body' => wp_json_encode([
2999 - 'model' => $model,
3000 - 'messages' => [
3001 - ['role' => 'system', 'content' => $system_prompt],
3002 - ['role' => 'user', 'content' => $text]
3003 - ],
3004 - 'temperature' => 0.3
3005 - ])
3006 - ]);
3007 -
3008 - if (is_wp_error($response)) {
3009 - return $response;
3010 - }
3011 -
3012 - $body = json_decode(wp_remote_retrieve_body($response), true);
3013 -
3014 - if (isset($body['error'])) {
3015 - return new WP_Error('api_error', $body['error']['message']);
3016 - }
3017 -
3018 - if (isset($body['choices'][0]['message']['content'])) {
3019 - return $body['choices'][0]['message']['content'];
3020 - }
3021 -
3022 - return new WP_Error('api_error', 'Invalid API response');
3023 -}
3024 -
3025 -/**
3026 - * Translate text using Claude API
3027 - */
3028 -private function translate_with_claude($api_key, $model, $system_prompt, $text) {
3029 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
3030 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
3031 - if ($model === 'claude-opus-4-20250514') { $model = 'claude-opus-4-8'; }
3032 - elseif ($model === 'claude-sonnet-4-20250514') { $model = 'claude-sonnet-4-6'; }
3033 - $response = wp_remote_post('https://api.anthropic.com/v1/messages', [
3034 - 'timeout' => 60,
3035 - 'headers' => [
3036 - 'x-api-key' => $api_key,
3037 - 'anthropic-version' => '2023-06-01',
3038 - 'Content-Type' => 'application/json'
3039 - ],
3040 - 'body' => wp_json_encode([
3041 - 'model' => $model,
3042 - 'max_tokens' => 4096,
3043 - 'system' => $system_prompt,
3044 - 'messages' => [
3045 - ['role' => 'user', 'content' => $text]
3046 - ]
3047 - ])
3048 - ]);
3049 -
3050 - if (is_wp_error($response)) {
3051 - return $response;
3052 - }
3053 -
3054 - $body = json_decode(wp_remote_retrieve_body($response), true);
3055 -
3056 - if (isset($body['error'])) {
3057 - return new WP_Error('api_error', $body['error']['message']);
3058 - }
3059 -
3060 - if (isset($body['content'][0]['text'])) {
3061 - return $body['content'][0]['text'];
3062 - }
3063 -
3064 - return new WP_Error('api_error', 'Invalid API response');
3065 -}
3066 -
3067 -/**
3068 - * Translate text using xAI (Grok) API
3069 - */
3070 -private function translate_with_xai($api_key, $model, $system_prompt, $text) {
3071 - $response = wp_remote_post('https://api.x.ai/v1/chat/completions', [
3072 - 'timeout' => 60,
3073 - 'headers' => [
3074 - 'Authorization' => 'Bearer ' . $api_key,
3075 - 'Content-Type' => 'application/json'
3076 - ],
3077 - 'body' => wp_json_encode([
3078 - 'model' => $model,
3079 - 'messages' => [
3080 - ['role' => 'system', 'content' => $system_prompt],
3081 - ['role' => 'user', 'content' => $text]
3082 - ],
3083 - 'temperature' => 0.3
3084 - ])
3085 - ]);
3086 -
3087 - if (is_wp_error($response)) {
3088 - return $response;
3089 - }
3090 -
3091 - $body = json_decode(wp_remote_retrieve_body($response), true);
3092 -
3093 - if (isset($body['error'])) {
3094 - return new WP_Error('api_error', $body['error']['message']);
3095 - }
3096 -
3097 - if (isset($body['choices'][0]['message']['content'])) {
3098 - return $body['choices'][0]['message']['content'];
3099 - }
3100 -
3101 - return new WP_Error('api_error', 'Invalid API response');
3102 -}
3103 -
3104 -/**
3105 - * Translate text using DeepSeek API
3106 - */
3107 -private function translate_with_deepseek($api_key, $model, $system_prompt, $text) {
3108 - $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', [
3109 - 'timeout' => 60,
3110 - 'headers' => [
3111 - 'Authorization' => 'Bearer ' . $api_key,
3112 - 'Content-Type' => 'application/json'
3113 - ],
3114 - 'body' => wp_json_encode([
3115 - 'model' => $model,
3116 - 'messages' => [
3117 - ['role' => 'system', 'content' => $system_prompt],
3118 - ['role' => 'user', 'content' => $text]
3119 - ],
3120 - 'temperature' => 0.3,
3121 - // DeepSeek V4 defaults to thinking mode ON (temperature ignored,
3122 - // slow responses); translation wants non-thinking.
3123 - 'thinking' => ['type' => 'disabled']
3124 - ])
3125 - ]);
3126 -
3127 - if (is_wp_error($response)) {
3128 - return $response;
3129 - }
3130 -
3131 - $body = json_decode(wp_remote_retrieve_body($response), true);
3132 -
3133 - if (isset($body['error'])) {
3134 - return new WP_Error('api_error', $body['error']['message']);
3135 - }
3136 -
3137 - if (isset($body['choices'][0]['message']['content'])) {
3138 - return $body['choices'][0]['message']['content'];
3139 - }
3140 -
3141 - return new WP_Error('api_error', 'Invalid API response');
3142 -}
3143 -
3144 -/**
3145 - * Translate text using Google Gemini API
3146 - */
3147 -private function translate_with_gemini($api_key, $model, $system_prompt, $text) {
3148 - if ($model === 'gemini-3-pro-preview') {
3149 - $model = 'gemini-3.1-pro-preview';
3150 - }
3151 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':generateContent?key=' . $api_key;
3152 -
3153 - $response = wp_remote_post($url, [
3154 - 'timeout' => 60,
3155 - 'headers' => [
3156 - 'Content-Type' => 'application/json'
3157 - ],
3158 - 'body' => wp_json_encode([
3159 - 'contents' => [
3160 - [
3161 - 'parts' => [
3162 - ['text' => $system_prompt . "\n\n" . $text]
3163 - ]
3164 - ]
3165 - ],
3166 - 'generationConfig' => [
3167 - 'temperature' => 0.3
3168 - ]
3169 - ])
3170 - ]);
3171 -
3172 - if (is_wp_error($response)) {
3173 - return $response;
3174 - }
3175 -
3176 - $body = json_decode(wp_remote_retrieve_body($response), true);
3177 -
3178 - if (isset($body['error'])) {
3179 - return new WP_Error('api_error', $body['error']['message']);
3180 - }
3181 -
3182 - if (isset($body['candidates'][0]['content']['parts'][0]['text'])) {
3183 - return $body['candidates'][0]['content']['parts'][0]['text'];
3184 - }
3185 -
3186 - return new WP_Error('api_error', 'Invalid API response');
3187 -}
3188 -
3189 -/**
3190 - * Translate text using OpenRouter API
3191 - */
3192 -private function translate_with_openrouter($api_key, $model, $system_prompt, $text) {
3193 - $response = wp_remote_post('https://openrouter.ai/api/v1/chat/completions', [
3194 - 'timeout' => 60,
3195 - 'headers' => [
3196 - 'Authorization' => 'Bearer ' . $api_key,
3197 - 'Content-Type' => 'application/json',
3198 - 'HTTP-Referer' => home_url(),
3199 - 'X-Title' => 'MxChat Translation'
3200 - ],
3201 - 'body' => wp_json_encode([
3202 - 'model' => $model,
3203 - 'messages' => [
3204 - ['role' => 'system', 'content' => $system_prompt],
3205 - ['role' => 'user', 'content' => $text]
3206 - ],
3207 - 'temperature' => 0.3
3208 - ])
3209 - ]);
3210 -
3211 - if (is_wp_error($response)) {
3212 - return $response;
3213 - }
3214 -
3215 - $body = json_decode(wp_remote_retrieve_body($response), true);
3216 -
3217 - if (isset($body['error'])) {
3218 - return new WP_Error('api_error', $body['error']['message']);
3219 - }
3220 -
3221 - if (isset($body['choices'][0]['message']['content'])) {
3222 - return $body['choices'][0]['message']['content'];
3223 - }
3224 -
3225 - return new WP_Error('api_error', 'Invalid API response');
3226 -}
3227 -
3228 974 public function mxchat_fetch_chat_history() {
3229 975 global $wpdb;
3230 976 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
3231 - $url_clicks_table = $wpdb->prefix . 'mxchat_url_clicks';
3232 977
3233 978 if (!current_user_can('manage_options')) {
3234 - wp_send_json_error(['message' => 'Insufficient permissions']);
3235 - wp_die();
979 + wp_die(esc_html__('You do not have sufficient permissions to view this page.', 'mxchat'));
3236 980 }
3237 981
3238 - $page = isset($_POST['page']) ? absint($_POST['page']) : 1;
3239 - $per_page = isset($_POST['per_page']) ? absint($_POST['per_page']) : 50;
3240 - $offset = ($page - 1) * $per_page;
3241 - $search = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
3242 - $sort_raw = isset($_POST['sort_order']) ? sanitize_key($_POST['sort_order']) : 'desc';
3243 - $allowed_sorts = array('asc', 'desc', 'rating_positive', 'rating_negative');
3244 - if (!in_array($sort_raw, $allowed_sorts, true)) { $sort_raw = 'desc'; }
3245 - $sort_order = ($sort_raw === 'asc') ? 'ASC' : 'DESC';
3246 - $ratings_table = $wpdb->prefix . 'mxchat_session_ratings';
3247 - $ratings_join = '';
3248 - $rating_table_exists = ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $ratings_table)) === $ratings_table);
3249 - if ($rating_table_exists && ($sort_raw === 'rating_positive' || $sort_raw === 'rating_negative')) {
3250 - // We sort by rating then recency. Wrap rating_value so NULL sorts last.
3251 - $rating_dir = ($sort_raw === 'rating_positive') ? 'DESC' : 'ASC';
3252 - $ratings_join = " LEFT JOIN {$ratings_table} r ON r.session_id = t.session_id ";
3253 - }
3254 -
3255 - // Build search condition
3256 - $search_condition = '';
3257 - $search_params = [];
3258 - if (!empty($search)) {
3259 - $search_condition = "WHERE (
3260 - session_id LIKE %s
3261 - OR user_email LIKE %s
3262 - OR user_name LIKE %s
3263 - OR user_identifier LIKE %s
3264 - OR message LIKE %s
3265 - )";
3266 - $search_params = array_fill(0, 5, '%' . $wpdb->esc_like($search) . '%');
3267 - }
3268 -
3269 - // Get total count
3270 - $count_query = !empty($search)
3271 - ? $wpdb->prepare("SELECT COUNT(DISTINCT session_id) FROM {$table_name} {$search_condition}", $search_params)
3272 - : "SELECT COUNT(DISTINCT session_id) FROM {$table_name}";
3273 - $total_sessions = (int) $wpdb->get_var($count_query);
3274 -
3275 - // Get session IDs for current page. Default sort is recency; rating sorts join the ratings table
3276 - // and order by rating value (NULLs last) with recency as tiebreaker.
3277 - if ($ratings_join) {
3278 - $order_by = "ORDER BY (r.rating_value IS NULL), r.rating_value {$rating_dir}, MAX(t.timestamp) DESC";
3279 - } else {
3280 - $order_by = "ORDER BY MAX(t.timestamp) {$sort_order}";
3281 - }
3282 - $session_query = !empty($search)
3283 - ? $wpdb->prepare(
3284 - "SELECT DISTINCT t.session_id FROM {$table_name} t {$ratings_join} {$search_condition}
3285 - GROUP BY t.session_id {$order_by} LIMIT %d OFFSET %d",
3286 - array_merge($search_params, [$per_page, $offset])
982 + // First get unique session IDs ordered by most recent message in each session
983 + $session_ids = $wpdb->get_col(
984 + $wpdb->prepare(
985 + "SELECT DISTINCT session_id
986 + FROM {$table_name}
987 + GROUP BY session_id
988 + ORDER BY MAX(timestamp) DESC"
3287 989 )
3288 - : $wpdb->prepare(
3289 - "SELECT DISTINCT t.session_id FROM {$table_name} t {$ratings_join}
3290 - GROUP BY t.session_id {$order_by} LIMIT %d OFFSET %d",
3291 - $per_page, $offset
3292 - );
3293 - $session_ids = $wpdb->get_col($session_query);
990 + );
3294 991
3295 - $total_pages = ceil($total_sessions / $per_page);
3296 -
3297 992 if (empty($session_ids)) {
3298 - wp_send_json([
3299 - 'success' => true,
3300 - 'sessions' => [],
3301 - 'page' => $page,
3302 - 'total_pages' => 0,
3303 - 'total_sessions' => 0,
3304 - 'showing_start' => 0,
3305 - 'showing_end' => 0
3306 - ]);
3307 - wp_die();
993 + wp_die(esc_html__('No chat history available.', 'mxchat'));
3308 994 }
3309 995
3310 - // Check for optional columns/tables
3311 - $url_table_exists = $wpdb->get_var("SHOW TABLES LIKE '$url_clicks_table'") === $url_clicks_table;
3312 - $originating_columns_exist = !empty($wpdb->get_results("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'"));
996 + ob_start();
997 + echo '<div class="mxchat-transcript">';
3313 998
3314 - // Batch-fetch session ratings for this page (plan-a5b006).
3315 - $ratings_map = array();
3316 - if ($rating_table_exists && !empty($session_ids)) {
3317 - $placeholders = implode(',', array_fill(0, count($session_ids), '%s'));
3318 - $rating_rows = $wpdb->get_results($wpdb->prepare(
3319 - "SELECT session_id, rating_value, rating_feedback FROM {$ratings_table} WHERE session_id IN ($placeholders)",
3320 - $session_ids
3321 - ));
3322 - foreach ($rating_rows as $row) {
3323 - $ratings_map[$row->session_id] = array(
3324 - 'value' => (int) $row->rating_value,
3325 - 'feedback' => (string) $row->rating_feedback,
3326 - );
3327 - }
3328 - }
3329 -
3330 - // Build session list data
3331 - $sessions = [];
999 + // Iterate through sessions from newest to oldest
3332 1000 foreach ($session_ids as $session_id) {
3333 - // Get session metadata
3334 - $session_data = $originating_columns_exist
3335 - ? $wpdb->get_row($wpdb->prepare(
3336 - "SELECT user_email, user_name, user_identifier, originating_page_url, originating_page_title, timestamp
3337 - FROM {$table_name} WHERE session_id = %s ORDER BY timestamp ASC LIMIT 1",
1001 + // Get the email associated with this session (if available)
1002 + $email = $wpdb->get_var(
1003 + $wpdb->prepare(
1004 + "SELECT user_email
1005 + FROM {$table_name}
1006 + WHERE session_id = %s AND user_email != ''
1007 + ORDER BY timestamp ASC
1008 + LIMIT 1",
3338 1009 $session_id
3339 - ))
3340 - : $wpdb->get_row($wpdb->prepare(
3341 - "SELECT user_email, user_name, user_identifier, timestamp FROM {$table_name}
3342 - WHERE session_id = %s LIMIT 1",
3343 - $session_id
3344 - ));
1010 + )
1011 + );
3345 1012
3346 - // Get message count and latest timestamp
3347 - $message_stats = $wpdb->get_row($wpdb->prepare(
3348 - "SELECT COUNT(*) as count, MAX(timestamp) as latest FROM {$table_name} WHERE session_id = %s",
3349 - $session_id
3350 - ));
3351 -
3352 - // Get first user message as preview
3353 - $first_user_msg = $wpdb->get_var($wpdb->prepare(
3354 - "SELECT message FROM {$table_name} WHERE session_id = %s AND role = 'user' ORDER BY timestamp ASC LIMIT 1",
3355 - $session_id
3356 - ));
3357 - $preview = $first_user_msg ? wp_trim_words(wp_strip_all_tags(stripslashes($first_user_msg)), 12, '...') : 'No messages';
3358 -
3359 - // Build display name
3360 - $user_email = !empty($session_data->user_email) ? $session_data->user_email : '';
3361 - $user_name = !empty($session_data->user_name) ? $session_data->user_name : '';
3362 - $user_identifier = !empty($session_data->user_identifier) ? $session_data->user_identifier : 'Guest';
3363 -
3364 - $display_name = $user_name ?: ($user_email ? explode('@', $user_email)[0] : $user_identifier);
3365 - $display_sub = $user_email ?: ('ID: ' . $user_identifier);
3366 -
3367 - // Format time - show relative for recent, date for older
3368 - $timestamp = strtotime($message_stats->latest . ' UTC');
3369 - $now = time();
3370 - $diff = $now - $timestamp;
3371 - if ($diff < 3600) {
3372 - $time_display = floor($diff / 60) . 'm ago';
3373 - } elseif ($diff < 86400) {
3374 - $time_display = floor($diff / 3600) . 'h ago';
3375 - } elseif ($diff < 604800) {
3376 - $time_display = floor($diff / 86400) . 'd ago';
1013 + // Debug: Check email retrieval
1014 + if (empty($email)) {
1015 + //error_log("No email found for session {$session_id}");
3377 1016 } else {
3378 - $time_display = wp_date('M j', $timestamp);
1017 + //error_log("Email for session {$session_id}: " . $email);
3379 1018 }
3380 1019
3381 - // Get initials for avatar
3382 - $initials = strtoupper(substr($display_name, 0, 2));
3383 - if (strlen($display_name) > 2 && strpos($display_name, ' ') !== false) {
3384 - $parts = explode(' ', $display_name);
3385 - $initials = strtoupper(substr($parts[0], 0, 1) . substr(end($parts), 0, 1));
3386 - }
3387 -
3388 - $rating_entry = isset($ratings_map[$session_id]) ? $ratings_map[$session_id] : null;
3389 - $rating_value = $rating_entry ? $rating_entry['value'] : null;
3390 - $rating_feedback = $rating_entry ? $rating_entry['feedback'] : '';
3391 -
3392 - $sessions[] = [
3393 - 'session_id' => $session_id,
3394 - 'display_name' => $display_name,
3395 - 'display_sub' => $display_sub,
3396 - 'initials' => $initials,
3397 - 'preview' => $preview,
3398 - 'message_count' => (int) $message_stats->count,
3399 - 'time_display' => $time_display,
3400 - 'timestamp' => $message_stats->latest,
3401 - 'rating_value' => $rating_value,
3402 - 'rating_feedback' => $rating_feedback,
3403 - ];
3404 - }
3405 -
3406 - wp_send_json([
3407 - 'success' => true,
3408 - 'sessions' => $sessions,
3409 - 'page' => $page,
3410 - 'total_pages' => $total_pages,
3411 - 'total_sessions' => $total_sessions,
3412 - 'showing_start' => $offset + 1,
3413 - 'showing_end' => min($offset + $per_page, $total_sessions)
3414 - ]);
3415 - wp_die();
3416 -}
3417 -
3418 -/**
3419 - * Fetch single conversation details for split-panel view
3420 - */
3421 -public function mxchat_fetch_conversation() {
3422 - global $wpdb;
3423 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
3424 - $url_clicks_table = $wpdb->prefix . 'mxchat_url_clicks';
3425 -
3426 - if (!current_user_can('manage_options')) {
3427 - wp_send_json_error(['message' => 'Insufficient permissions']);
3428 - wp_die();
3429 - }
3430 -
3431 - $session_id = isset($_POST['session_id']) ? MxChat_Utils::sanitize_session_id(wp_unslash($_POST['session_id'])) : '';
3432 - if (empty($session_id)) {
3433 - wp_send_json_error(['message' => 'No session ID provided']);
3434 - wp_die();
3435 - }
3436 -
3437 - // Check for optional columns/tables
3438 - $url_table_exists = $wpdb->get_var("SHOW TABLES LIKE '$url_clicks_table'") === $url_clicks_table;
3439 - $originating_columns_exist = !empty($wpdb->get_results("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'"));
3440 -
3441 - // Get session metadata
3442 - $session_data = $originating_columns_exist
3443 - ? $wpdb->get_row($wpdb->prepare(
3444 - "SELECT user_email, user_name, user_identifier, originating_page_url, originating_page_title, timestamp
3445 - FROM {$table_name} WHERE session_id = %s ORDER BY timestamp ASC LIMIT 1",
3446 - $session_id
3447 - ))
3448 - : $wpdb->get_row($wpdb->prepare(
3449 - "SELECT user_email, user_name, user_identifier, timestamp FROM {$table_name}
3450 - WHERE session_id = %s LIMIT 1",
3451 - $session_id
3452 - ));
3453 -
3454 - if (!$session_data) {
3455 - wp_send_json_error(['message' => 'Session not found']);
3456 - wp_die();
3457 - }
3458 -
3459 - // Get clicked URLs
3460 - $clicked_urls = [];
3461 - if ($url_table_exists) {
3462 - $url_clicks = $wpdb->get_results($wpdb->prepare(
3463 - "SELECT DISTINCT clicked_url FROM {$url_clicks_table}
3464 - WHERE session_id = %s ORDER BY click_timestamp ASC",
3465 - $session_id
3466 - ));
3467 - foreach ($url_clicks as $click) {
3468 - $clicked_urls[] = $click->clicked_url;
3469 - }
3470 - }
3471 -
3472 - // Get all messages
3473 - $messages = $wpdb->get_results($wpdb->prepare(
3474 - "SELECT * FROM {$table_name} WHERE session_id = %s ORDER BY timestamp ASC",
3475 - $session_id
3476 - ));
3477 -
3478 - // Build user info
3479 - $user_email = !empty($session_data->user_email) ? $session_data->user_email : '';
3480 - $user_name = !empty($session_data->user_name) ? $session_data->user_name : '';
3481 - $user_identifier = !empty($session_data->user_identifier) ? $session_data->user_identifier : 'Guest';
3482 -
3483 - $display_name = $user_name ?: ($user_email ? explode('@', $user_email)[0] : $user_identifier);
3484 - $display_sub = $user_email ?: ('ID: ' . $user_identifier);
3485 -
3486 - // Get initials
3487 - $initials = strtoupper(substr($display_name, 0, 2));
3488 - if (strlen($display_name) > 2 && strpos($display_name, ' ') !== false) {
3489 - $parts = explode(' ', $display_name);
3490 - $initials = strtoupper(substr($parts[0], 0, 1) . substr(end($parts), 0, 1));
3491 - }
3492 -
3493 - // Page info
3494 - $page_url = $originating_columns_exist && !empty($session_data->originating_page_url) ? $session_data->originating_page_url : '';
3495 - $page_title = '';
3496 - if ($page_url) {
3497 - $page_title = !empty($session_data->originating_page_title) ? $session_data->originating_page_title : parse_url($page_url, PHP_URL_PATH);
3498 - }
3499 -
3500 - // Format messages for output
3501 - $formatted_messages = [];
3502 - foreach ($messages as $msg) {
3503 - $is_user = ($msg->role === 'user');
3504 - // Live-agent replies (Slack/Telegram handoff) persist with role 'agent'.
3505 - // Kept OUT of $is_bot so the RAG Sources link below stays bot-only.
3506 - $is_agent = ($msg->role === 'agent');
3507 - $is_bot = ($msg->role === 'bot' || $msg->role === 'assistant');
3508 -
3509 - $content = wp_kses(
3510 - stripslashes($msg->message),
3511 - [
3512 - 'b' => [], 'strong' => [], 'i' => [], 'em' => [], 'u' => [],
3513 - 'br' => [], 'p' => [], 'ul' => [], 'ol' => [], 'li' => [],
3514 - 'a' => ['href' => [], 'title' => [], 'target' => [], 'class' => []],
3515 - 'div' => ['class' => [], 'id' => [], 'data-nonce' => []],
3516 - 'img' => ['src' => [], 'alt' => [], 'class' => []],
3517 - 'h3' => ['class' => []],
3518 - 'h4' => ['class' => []],
3519 - 'button' => ['type' => [], 'class' => [], 'data-product-id' => [], 'data-nonce' => [], 'data-product-type' => [], 'data-original-text' => [], 'data-mxchat-action' => []],
3520 - 'select' => ['class' => [], 'data-attribute' => []],
3521 - 'option' => ['value' => []],
3522 - 'span' => ['class' => []],
3523 - 'del' => [], 'ins' => [],
3524 - ]
1020 + // Get messages for this session ordered by timestamp
1021 + $messages = $wpdb->get_results(
1022 + $wpdb->prepare(
1023 + "SELECT * FROM {$table_name}
1024 + WHERE session_id = %s
1025 + ORDER BY timestamp ASC",
1026 + $session_id
1027 + )
3525 1028 );
3526 - $formatted_content = $this->format_transcript_message($content);
3527 1029
3528 - $has_rag = $is_bot && !empty($msg->rag_context);
1030 + // Start session block
1031 + echo '<div class="mxchat-session">';
1032 + echo '<div class="mxchat-session-header">';
1033 + // Wrap checkbox and session ID in one block
1034 + echo '<div class="mxchat-session-id">';
1035 + echo '<input type="checkbox" name="delete_session_ids[]" value="' . esc_attr($session_id) . '"> ';
1036 + echo '<strong>Session ID:</strong> ' . esc_html($session_id);
1037 + echo '</div>';
3529 1038
3530 - $formatted_messages[] = [
3531 - 'id' => $msg->id,
3532 - 'role' => $msg->role,
3533 - 'is_user' => $is_user,
3534 - 'is_agent' => $is_agent,
3535 - 'is_bot' => $is_bot,
3536 - 'content' => $formatted_content,
3537 - 'timestamp' => wp_date('g:i A', strtotime($msg->timestamp . ' UTC')),
3538 - 'full_timestamp' => wp_date('F j, Y g:i A', strtotime($msg->timestamp . ' UTC')),
3539 - 'has_rag' => $has_rag
3540 - ];
3541 - }
3542 -
3543 - // First message timestamp for "started" display
3544 - $started = !empty($messages) ? wp_date('M j, Y g:i A', strtotime($messages[0]->timestamp . ' UTC')) : '-';
3545 -
3546 - // Pull rating_feedback for this session (if any) so the details drawer can show it.
3547 - $rating_feedback = '';
3548 - $ratings_table_det = $wpdb->prefix . 'mxchat_session_ratings';
3549 - if ($wpdb->get_var("SHOW TABLES LIKE '$ratings_table_det'") === $ratings_table_det) {
3550 - $rating_feedback = (string) $wpdb->get_var($wpdb->prepare(
3551 - "SELECT rating_feedback FROM {$ratings_table_det} WHERE session_id = %s LIMIT 1",
3552 - $session_id
3553 - ));
3554 - }
3555 -
3556 - wp_send_json([
3557 - 'success' => true,
3558 - 'session_id' => $session_id,
3559 - 'user' => [
3560 - 'name' => $display_name,
3561 - 'sub' => $display_sub,
3562 - 'initials' => $initials,
3563 - 'email' => $user_email,
3564 - 'identifier' => $user_identifier
3565 - ],
3566 - 'page' => [
3567 - 'url' => $page_url,
3568 - 'title' => $page_title
3569 - ],
3570 - 'clicked_urls' => $clicked_urls,
3571 - 'messages' => $formatted_messages,
3572 - 'message_count' => count($messages),
3573 - 'started' => $started,
3574 - 'rating_feedback' => $rating_feedback
3575 - ]);
3576 - wp_die();
3577 -}
3578 -
3579 -
3580 -/**
3581 - * ALTERNATIVE: Simpler helper method using string replacement
3582 - */
3583 -private function highlight_clicked_links($message_content, $clicked_urls) {
3584 - if (empty($clicked_urls)) {
3585 - return wp_kses(
3586 - $message_content,
3587 - [
3588 - 'b' => [], 'strong' => [], 'i' => [], 'em' => [], 'u' => [],
3589 - 'br' => [], 'p' => [], 'ul' => [], 'ol' => [], 'li' => [],
3590 - 'a' => ['href' => [], 'title' => [], 'target' => [], 'class' => []]
3591 - ]
3592 - );
3593 - }
3594 -
3595 - // First apply standard sanitization
3596 - $message_content = wp_kses(
3597 - $message_content,
3598 - [
3599 - 'b' => [], 'strong' => [], 'i' => [], 'em' => [], 'u' => [],
3600 - 'br' => [], 'p' => [], 'ul' => [], 'ol' => [], 'li' => [],
3601 - 'a' => ['href' => [], 'title' => [], 'target' => [], 'class' => []]
3602 - ]
3603 - );
3604 -
3605 - // Process each clicked URL
3606 - foreach ($clicked_urls as $clicked_url) {
3607 - // Try multiple patterns to catch different link formats
3608 - $patterns = [
3609 - // Standard link format
3610 - '/<a([^>]*href=["\']' . preg_quote($clicked_url, '/') . '["\'][^>]*)>/i',
3611 - // Link with trailing slash
3612 - '/<a([^>]*href=["\']' . preg_quote(rtrim($clicked_url, '/'), '/') . '\/?["\'][^>]*)>/i',
3613 - // Encoded entities version
3614 - '/<a([^>]*href=["\']' . preg_quote(htmlentities($clicked_url), '/') . '["\'][^>]*)>/i',
3615 - ];
3616 -
3617 - foreach ($patterns as $pattern) {
3618 - if (preg_match($pattern, $message_content)) {
3619 - $message_content = preg_replace_callback(
3620 - $pattern,
3621 - function($matches) {
3622 - $full_match = $matches[0];
3623 - $attributes = $matches[1];
3624 -
3625 - // Check if it already has the class
3626 - if (strpos($full_match, 'mxchat-clicked-link') !== false) {
3627 - return $full_match;
3628 - }
3629 -
3630 - // Check if class attribute exists
3631 - if (preg_match('/class=["\']([^"\']*)["\']/', $attributes, $class_matches)) {
3632 - // Add to existing class
3633 - $new_attributes = preg_replace(
3634 - '/class=["\']([^"\']*)["\']/',
3635 - 'class="$1 mxchat-clicked-link"',
3636 - $attributes
3637 - );
3638 - } else {
3639 - // Add new class attribute
3640 - $new_attributes = $attributes . ' class="mxchat-clicked-link"';
3641 - }
3642 -
3643 - // Add title if not present
3644 - if (strpos($new_attributes, 'title=') === false) {
3645 - $new_attributes .= ' title="User clicked this link"';
3646 - }
3647 -
3648 - return '<a' . $new_attributes . '>';
3649 - },
3650 - $message_content
3651 - );
3652 -
3653 - // If we found and replaced, break out of the patterns loop
3654 - break;
3655 - }
1039 + // Place email directly below the session ID block
1040 + if (!empty($email)) {
1041 + echo '<div class="mxchat-session-email">';
1042 + echo '<strong>Email:</strong> ' . esc_html($email);
1043 + echo '</div>';
3656 1044 }
3657 - }
1045 + echo '</div>';
3658 1046
3659 - return $message_content;
3660 -}
1047 + echo '<div class="mxchat-messages">';
3661 1048
3662 -public function mxchat_create_prompts_page() {
3663 - //error_log('=== DEBUG: mxchat_create_prompts_page started ===');
1049 + // Display messages for this session
1050 + foreach ($messages as $transcript) {
1051 + $formatted_timestamp = date_i18n('F j, Y g:i a', strtotime($transcript->timestamp));
3664 1052
3665 - global $wpdb;
3666 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3667 -
3668 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
3669 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
3670 -
3671 - // Display success message if all prompts were deleted
3672 - if (isset($_GET['all_deleted']) && $_GET['all_deleted'] === 'true') {
3673 - echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__('All knowledge has been deleted successfully.', 'mxchat') . '</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">' . esc_html__('Dismiss this notice.', 'mxchat') . '</span></button></div>';
3674 - }
3675 -
3676 - // Set up pagination, search query, and content type filter
3677 - $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field($_GET['_wpnonce']) : '';
3678 - $search_query = (!empty($nonce) && wp_verify_nonce($nonce, 'mxchat_prompts_search_nonce') && isset($_GET['search'])) ? sanitize_text_field($_GET['search']) : '';
3679 - $content_type_filter = isset($_GET['content_type']) ? sanitize_key($_GET['content_type']) : ''; // ADDED 2.5.6
3680 - $current_page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
3681 - $per_page = 25;
3682 -
3683 - //error_log('DEBUG: Search query: ' . $search_query);
3684 - //error_log('DEBUG: Content type filter: ' . $content_type_filter);
3685 - //error_log('DEBUG: Current page: ' . $current_page);
3686 - //error_log('DEBUG: Per page: ' . $per_page);
3687 -
3688 - // ================================
3689 - // MULTI-BOT CONFIGURATION
3690 - // ================================
3691 -
3692 - // Check for multi-bot and set up bot selection
3693 - if (class_exists('MxChat_Multi_Bot_Manager')) {
3694 - $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance();
3695 - $available_bots = $multi_bot_manager->get_available_bots();
3696 -
3697 - // Get saved bot selection (user-specific first, then site-wide default)
3698 - $user_id = get_current_user_id();
3699 - $saved_bot_id = get_user_meta($user_id, 'mxchat_selected_knowledge_bot', true);
3700 - if (empty($saved_bot_id)) {
3701 - $saved_bot_id = get_option('mxchat_current_knowledge_bot', 'default');
3702 - }
3703 -
3704 - // Allow URL override but default to saved selection
3705 - $current_bot_id = isset($_GET['bot_id']) ? sanitize_key($_GET['bot_id']) : $saved_bot_id;
3706 - $multibot_active = true;
3707 - } else {
3708 - $current_bot_id = 'default';
3709 - $multibot_active = false;
3710 - }
3711 -
3712 - // ================================
3713 - // DATA SOURCE CONFIGURATION
3714 - // ================================
3715 -
3716 - // Get bot-specific Pinecone settings
3717 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($current_bot_id);
3718 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3719 - $pinecone_api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
3720 -
3721 - // Get OpenAI Vector Store settings
3722 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
3723 - $use_vectorstore = ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1';
3724 -
3725 - //error_log('DEBUG: Bot ' . $current_bot_id . ' - Use Pinecone: ' . ($use_pinecone ? 'YES' : 'NO'));
3726 - //error_log('DEBUG: Bot ' . $current_bot_id . ' - Has API Key: ' . (!empty($pinecone_api_key) ? 'YES' : 'NO'));
3727 - //error_log('DEBUG: Bot ' . $current_bot_id . ' - Host: ' . ($pinecone_options['mxchat_pinecone_host'] ?? 'NOT SET'));
3728 - //error_log('DEBUG: Bot ' . $current_bot_id . ' - Namespace: ' . ($pinecone_options['mxchat_pinecone_namespace'] ?? 'NOT SET'));
3729 -
3730 - if ($use_pinecone && !empty($pinecone_api_key)) {
3731 - //error_log('DEBUG: Using PINECONE data source with bot-specific config');
3732 - // PINECONE DATA SOURCE
3733 - $data_source = 'pinecone';
3734 -
3735 - // IMPORTANT: Pass the bot-specific $pinecone_options, not default options!
3736 - // UPDATED 2.5.6: Added content_type_filter parameter
3737 - $records = $pinecone_manager->mxchat_fetch_pinecone_records($pinecone_options, $search_query, $current_page, $per_page, $current_bot_id, $content_type_filter);
3738 -
3739 - // TEMPORARY DEBUG - Add this right after the fetch call
3740 - //error_log('=== DEBUG: Bot switching issue ===');
3741 - //error_log('Current bot ID: ' . $current_bot_id);
3742 - //error_log('Use Pinecone: ' . ($use_pinecone ? 'YES' : 'NO'));
3743 - //error_log('Records returned: ' . count($records['data'] ?? []));
3744 - //error_log('Total records: ' . ($records['total'] ?? 0));
3745 -
3746 - // Check first few records to see their bot_id
3747 - if (!empty($records['data'])) {
3748 - foreach (array_slice($records['data'], 0, 3) as $i => $record) {
3749 - $record_bot_id = $record->bot_id ?? 'NOT_SET';
3750 - //error_log('Record ' . ($i+1) . ' bot_id: ' . $record_bot_id . ', content preview: ' . substr($record->article_content ?? '', 0, 30) . '...');
1053 + // Determine message styling
1054 + switch ($transcript->role) {
1055 + case 'assistant':
1056 + case 'bot':
1057 + $message_class = 'bot-message';
1058 + $display_role = esc_html__('Chatbot', 'mxchat');
1059 + break;
1060 + case 'user':
1061 + $message_class = 'user-message';
1062 + $display_role = !empty($transcript->user_identifier)
1063 + ? sanitize_text_field($transcript->user_identifier)
1064 + : esc_html__('User', 'mxchat');
1065 + break;
1066 + case 'agent':
1067 + $message_class = 'agent-message';
1068 + $display_role = esc_html__('Agent', 'mxchat');
1069 + break;
1070 + default:
1071 + $message_class = 'unknown-message';
1072 + $display_role = esc_html__('Unknown', 'mxchat');
3751 1073 }
3752 - }
3753 - //error_log('=== END DEBUG ===');
3754 1074
3755 - $total_records = $records['total'] ?? 0;
3756 - $prompts = $records['data'] ?? array();
3757 - $total_in_database = $records['total_in_database'] ?? 0;
3758 - $showing_recent_only = $records['showing_recent_only'] ?? false;
3759 -
3760 - $total_pages = ceil($total_records / $per_page);
3761 -
3762 - } else {
3763 - //error_log('DEBUG: Using WORDPRESS DB data source');
3764 - // WORDPRESS DB DATA SOURCE (your existing logic)
3765 - $data_source = 'wordpress';
3766 -
3767 - // Initialize these variables for WordPress DB
3768 - $total_in_database = 0;
3769 - $showing_recent_only = false;
3770 -
3771 - $offset = ($current_page - 1) * $per_page;
3772 -
3773 - // UPDATED 2.5.6: Build WHERE clause for search and content type filtering
3774 - $where_clauses = array();
3775 - $where_values = array();
3776 -
3777 - if ($search_query) {
3778 - $where_clauses[] = "article_content LIKE %s";
3779 - $where_values[] = '%' . $wpdb->esc_like($search_query) . '%';
3780 - }
3781 -
3782 - if ($content_type_filter) {
3783 - $where_clauses[] = "content_type = %s";
3784 - $where_values[] = $content_type_filter;
3785 - }
3786 -
3787 - $sql_where = "";
3788 - if (!empty($where_clauses)) {
3789 - $sql_where = "WHERE " . implode(" AND ", $where_clauses);
3790 - }
3791 -
3792 - // UPDATED 2.6.3: Count unique entries (by source_url) instead of individual rows
3793 - // This ensures pagination shows X entries per page, not X chunks
3794 - // Entries with empty source_url are counted individually
3795 - if (!empty($where_values)) {
3796 - // Count unique source_urls + count of rows with empty source_url
3797 - $count_query = $wpdb->prepare(
3798 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} {$sql_where} AND source_url != '') +
3799 - (SELECT COUNT(*) FROM {$table_name} {$sql_where} AND (source_url = '' OR source_url IS NULL))",
3800 - array_merge($where_values, $where_values)
1075 + // Process message content
1076 + $message_content = wp_kses(
1077 + stripslashes($transcript->message),
1078 + [
1079 + 'b' => [], 'strong' => [], 'i' => [], 'em' => [], 'u' => [],
1080 + 'br' => [], 'p' => [], 'ul' => [], 'ol' => [], 'li' => [],
1081 + 'a' => ['href' => [], 'title' => []]
1082 + ]
3801 1083 );
3802 - } else {
3803 - $count_query = "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} WHERE source_url != '') +
3804 - (SELECT COUNT(*) FROM {$table_name} WHERE source_url = '' OR source_url IS NULL)";
3805 - }
3806 - $total_records = $wpdb->get_var($count_query);
3807 - $total_pages = ceil($total_records / $per_page);
1084 + $message_content = nl2br($message_content);
3808 1085
3809 - // UPDATED 2.6.3: Get unique source_urls for pagination, then fetch all their rows
3810 - // Step 1: Get the source_urls for this page (distinct URLs ordered by latest timestamp)
3811 - if (!empty($where_values)) {
3812 - $urls_query = $wpdb->prepare(
3813 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name} {$sql_where}
3814 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
3815 - array_merge($where_values, array($per_page, $offset))
3816 - );
3817 - } else {
3818 - $urls_query = $wpdb->prepare(
3819 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name}
3820 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
3821 - $per_page, $offset
3822 - );
1086 + // Render message
1087 + echo '<div class="mxchat-message ' . esc_attr($message_class) . '">';
1088 + echo '<div class="mxchat-message-header">' . esc_html($display_role) . '</div>';
1089 + echo '<div class="mxchat-message-content">' . $message_content . '</div>';
1090 + echo '<div class="mxchat-timestamp">' . esc_html($formatted_timestamp) . '</div>';
1091 + echo '</div>';
3823 1092 }
3824 - $page_urls = $wpdb->get_results($urls_query);
3825 1093
3826 - // Step 2: Fetch all rows for these source_urls
3827 - $prompts = array();
3828 - if (!empty($page_urls)) {
3829 - // Build URL order map to preserve newest-first ordering from step 1
3830 - $url_order_map = array();
3831 - $url_list = array();
3832 - $has_empty_url = false;
3833 - $order_index = 0;
3834 - foreach ($page_urls as $url_row) {
3835 - if (empty($url_row->source_url)) {
3836 - $has_empty_url = true;
3837 - $url_order_map['__empty__'] = $order_index++;
3838 - } else {
3839 - $url_list[] = $url_row->source_url;
3840 - $url_order_map[$url_row->source_url] = $order_index++;
3841 - }
3842 - }
3843 -
3844 - // Build query to fetch all rows for these URLs
3845 - $url_conditions = array();
3846 - $url_values = array();
3847 -
3848 - if (!empty($url_list)) {
3849 - $placeholders = implode(',', array_fill(0, count($url_list), '%s'));
3850 - $url_conditions[] = "source_url IN ($placeholders)";
3851 - $url_values = array_merge($url_values, $url_list);
3852 - }
3853 -
3854 - if ($has_empty_url) {
3855 - $url_conditions[] = "(source_url = '' OR source_url IS NULL)";
3856 - }
3857 -
3858 - if (!empty($url_conditions)) {
3859 - $url_where = "WHERE (" . implode(" OR ", $url_conditions) . ")";
3860 -
3861 - // Add original filters back
3862 - if (!empty($where_clauses)) {
3863 - $url_where .= " AND " . implode(" AND ", $where_clauses);
3864 - $url_values = array_merge($url_values, $where_values);
3865 - }
3866 -
3867 - if (!empty($url_values)) {
3868 - $prompts_query = $wpdb->prepare(
3869 - "SELECT * FROM {$table_name} {$url_where} ORDER BY timestamp DESC",
3870 - $url_values
3871 - );
3872 - } else {
3873 - $prompts_query = "SELECT * FROM {$table_name} {$url_where} ORDER BY timestamp DESC";
3874 - }
3875 -
3876 - $prompts = $wpdb->get_results($prompts_query);
3877 -
3878 - // Sort prompts by the original URL order (newest first), then by timestamp within each URL group
3879 - usort($prompts, function($a, $b) use ($url_order_map) {
3880 - $url_a = empty($a->source_url) ? '__empty__' : $a->source_url;
3881 - $url_b = empty($b->source_url) ? '__empty__' : $b->source_url;
3882 - $order_a = $url_order_map[$url_a] ?? PHP_INT_MAX;
3883 - $order_b = $url_order_map[$url_b] ?? PHP_INT_MAX;
3884 -
3885 - // First sort by URL order (newest URLs first)
3886 - if ($order_a !== $order_b) {
3887 - return $order_a - $order_b;
3888 - }
3889 -
3890 - // Within same URL, sort by timestamp DESC (newest chunks first)
3891 - return strtotime($b->timestamp) - strtotime($a->timestamp);
3892 - });
3893 - }
3894 - }
1094 + // Close session block
1095 + echo '</div></div>';
3895 1096 }
3896 1097
3897 - // ================================
3898 - // PAGINATION GENERATION
3899 - // ================================
3900 -
3901 - // Generate pagination links
3902 - if ($total_pages > 1) {
3903 - // Build a clean base URL with only necessary parameters
3904 - $pagination_args = array('page' => 'mxchat-prompts');
3905 -
3906 - // Preserve bot_id parameter if multi-bot is active
3907 - if ($multibot_active && !empty($current_bot_id) && $current_bot_id !== 'default') {
3908 - $pagination_args['bot_id'] = $current_bot_id;
3909 - }
3910 -
3911 - // Preserve search query and nonce if present
3912 - if ($search_query) {
3913 - $pagination_args['search'] = $search_query;
3914 - if (!empty($nonce)) {
3915 - $pagination_args['_wpnonce'] = $nonce;
3916 - }
3917 - }
3918 -
3919 - // Preserve content type filter if present
3920 - if ($content_type_filter) {
3921 - $pagination_args['content_type'] = $content_type_filter;
3922 - }
3923 -
3924 - // Build clean base URL
3925 - $base_url = add_query_arg($pagination_args, admin_url('admin.php'));
3926 -
3927 - $page_links = paginate_links(array(
3928 - 'base' => $base_url . '%_%',
3929 - 'format' => '&paged=%#%',
3930 - 'prev_text' => __('&laquo; Previous', 'mxchat'),
3931 - 'next_text' => __('Next &raquo;', 'mxchat'),
3932 - 'total' => $total_pages,
3933 - 'current' => $current_page,
3934 - 'type' => 'plain',
3935 - ));
3936 - } else {
3937 - $page_links = '';
3938 - }
3939 -
3940 - // ================================
3941 - // PROCESSING STATUS RETRIEVAL
3942 - // ================================
3943 -
3944 - // Retrieve processing statuses using queue-based method
3945 - $processing_statuses = $knowledge_manager->mxchat_get_processing_statuses();
3946 - $pdf_status = $processing_statuses['pdf_status'];
3947 - $sitemap_status = $processing_statuses['sitemap_status'];
3948 - $is_processing = $processing_statuses['is_processing'];
3949 -
3950 - //error_log('=== DEBUG: mxchat_create_prompts_page data preparation completed ===');
3951 -
3952 - // ================================
3953 - // RENDER PAGE WITH NEW SIDEBAR LAYOUT
3954 - // ================================
3955 -
3956 - // Include the new knowledge page template
3957 - require_once plugin_dir_path(__FILE__) . 'admin-knowledge-page.php';
3958 -
3959 - // Package all page data for the render function
3960 - $page_data = array(
3961 - 'prompts' => $prompts,
3962 - 'total_records' => $total_records,
3963 - 'total_pages' => $total_pages,
3964 - 'current_page' => $current_page,
3965 - 'per_page' => $per_page,
3966 - 'page_links' => $page_links,
3967 - 'search_query' => $search_query,
3968 - 'content_type_filter' => $content_type_filter,
3969 - 'data_source' => $data_source,
3970 - 'use_pinecone' => $use_pinecone,
3971 - 'use_vectorstore' => $use_vectorstore,
3972 - 'multibot_active' => $multibot_active,
3973 - 'current_bot_id' => $current_bot_id,
3974 - 'pdf_status' => $pdf_status,
3975 - 'sitemap_status' => $sitemap_status,
3976 - 'is_processing' => $is_processing,
3977 - 'total_in_database' => $total_in_database ?? 0,
3978 - 'showing_recent_only' => $showing_recent_only ?? false,
3979 - );
3980 -
3981 - // Render the new sidebar-based page
3982 - mxchat_render_knowledge_page($this, $knowledge_manager, $page_data);
3983 -}
3984 -
3985 -/**
3986 - * Get bot-specific Pinecone configuration
3987 - * Used in the knowledge retrieval functions
3988 - */
3989 -private function get_bot_pinecone_config($bot_id = 'default') {
3990 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
3991 -
3992 - // If default bot or multi-bot add-on not active, use default Pinecone config
3993 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
3994 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
3995 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
3996 - $config = array(
3997 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
3998 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
3999 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
4000 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
4001 - );
4002 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
4003 - return $config;
4004 - }
4005 -
4006 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
4007 -
4008 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
4009 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
4010 -
4011 - if (!empty($bot_pinecone_config)) {
4012 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
4013 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
4014 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
4015 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
4016 - } else {
4017 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
4018 - }
4019 -
4020 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
4021 -}
4022 -
4023 -public function mxchat_delete_chat_history() {
4024 - if (!current_user_can('manage_options')) {
4025 - echo wp_json_encode(['error' => esc_html__('You do not have sufficient permissions.', 'mxchat')]);
4026 - wp_die();
4027 - }
4028 - check_ajax_referer('mxchat_delete_chat_history', 'security');
4029 -
4030 - if (!isset($_POST['delete_session_ids']) || !is_array($_POST['delete_session_ids'])) {
4031 - echo wp_json_encode(['error' => esc_html__('No chat sessions selected for deletion.', 'mxchat')]);
4032 - wp_die();
4033 - }
4034 -
4035 - global $wpdb;
4036 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4037 - $translations_table = $wpdb->prefix . 'mxchat_transcript_translations';
4038 - $has_translations = $wpdb->get_var("SHOW TABLES LIKE '$translations_table'") === $translations_table;
4039 -
4040 - // When true, any lead attached to these sessions is fully wiped (all their sessions,
4041 - // across the whole table). Default false: the chat rows go away but the lead is
4042 - // preserved as a separate "chat deleted" lead in the Leads tab.
4043 - $also_delete_lead = !empty($_POST['also_delete_lead']) && $_POST['also_delete_lead'] !== 'false';
4044 -
4045 - $deleted_count = 0;
4046 - $preserved_as_deleted_leads = 0;
4047 - $emails_to_fully_wipe = [];
4048 -
4049 - foreach ((array) $_POST['delete_session_ids'] as $session_id) {
4050 - $session_id_sanitized = MxChat_Utils::sanitize_session_id($session_id);
4051 - if ($session_id_sanitized === '') {
4052 - continue;
4053 - }
4054 -
4055 - // Capture the lead info attached to this session *before* we delete the rows.
4056 - $lead_row = $wpdb->get_row($wpdb->prepare(
4057 - "SELECT user_email, user_name, MAX(timestamp) AS last_ts
4058 - FROM {$table_name}
4059 - WHERE session_id = %s AND user_email IS NOT NULL AND user_email != ''
4060 - GROUP BY user_email, user_name
4061 - ORDER BY last_ts DESC LIMIT 1",
4062 - $session_id_sanitized
4063 - ));
4064 -
4065 - wp_cache_delete('chat_session_' . $session_id_sanitized, 'mxchat_chat_sessions');
4066 - $wpdb->delete($table_name, ['session_id' => $session_id_sanitized]);
4067 -
4068 - if ($has_translations) {
4069 - $wpdb->delete($translations_table, ['session_id' => $session_id_sanitized]);
4070 - }
4071 -
4072 - delete_option('mxchat_history_' . $session_id_sanitized);
4073 - delete_option('mxchat_agent_name_' . $session_id_sanitized);
4074 -
4075 - if ($lead_row && !empty($lead_row->user_email)) {
4076 - if ($also_delete_lead) {
4077 - // Full-wipe requested — queue the email so that all their sessions and
4078 - // related options get swept below. Also clear this session's pre-chat
4079 - // capture options (they're no longer meaningful).
4080 - $emails_to_fully_wipe[strtolower($lead_row->user_email)] = $lead_row->user_email;
4081 - delete_option('mxchat_email_' . $session_id_sanitized);
4082 - delete_option('mxchat_name_' . $session_id_sanitized);
4083 - } else {
4084 - // Preserve the lead in a "Chat deleted" state via distinct option keys so
4085 - // they stay out of the orphan bucket (orphan = pre-chat form dropoff).
4086 - update_option('mxchat_lead_del_email_' . $session_id_sanitized, $lead_row->user_email, false);
4087 - if (!empty($lead_row->user_name)) {
4088 - update_option('mxchat_lead_del_name_' . $session_id_sanitized, $lead_row->user_name, false);
4089 - }
4090 - if (!empty($lead_row->last_ts)) {
4091 - update_option('mxchat_lead_del_ts_' . $session_id_sanitized, $lead_row->last_ts, false);
4092 - }
4093 - // Clean up pre-chat capture options for this session — chat_deleted supersedes.
4094 - delete_option('mxchat_email_' . $session_id_sanitized);
4095 - delete_option('mxchat_name_' . $session_id_sanitized);
4096 - $preserved_as_deleted_leads++;
4097 - }
4098 - } else {
4099 - // No lead attached — nothing to preserve. Clean up any orphan options anyway.
4100 - delete_option('mxchat_email_' . $session_id_sanitized);
4101 - delete_option('mxchat_name_' . $session_id_sanitized);
4102 - }
4103 -
4104 - $deleted_count++;
4105 - }
4106 -
4107 - // Opt-in full-lead wipe: sweep every remaining row + every option key (including
4108 - // chat_deleted preservation) for each affected email. Reuses the same internal
4109 - // helper as the Leads-tab Delete button for consistency.
4110 - if (!empty($emails_to_fully_wipe)) {
4111 - self::mxchat_wipe_leads_by_email(array_values($emails_to_fully_wipe));
4112 - }
4113 -
4114 - wp_cache_delete('all_chat_sessions', 'mxchat_chat_sessions');
4115 -
4116 - echo wp_json_encode([
4117 - 'success' => sprintf(
4118 - esc_html__('%d chat session(s) have been deleted.', 'mxchat'),
4119 - $deleted_count
4120 - ),
4121 - 'preserved_as_deleted_leads' => $preserved_as_deleted_leads,
4122 - 'leads_fully_wiped' => count($emails_to_fully_wipe),
4123 - ]);
1098 + echo '</div>';
1099 + $output = ob_get_clean();
1100 + echo $output;
4124 1101 wp_die();
4125 1102 }
4126 1103
4127 -/**
4128 - * Format transcript message content with markdown processing
4129 - * Converts markdown links, bold, italic, code blocks, and plain URLs to HTML
4130 - */
4131 -private function format_transcript_message($text) {
4132 - if (empty($text)) {
4133 - return '';
4134 - }
4135 1104
4136 - // Normalize line endings and clean up excessive whitespace
4137 - $text = str_replace("\r\n", "\n", $text);
4138 - $text = str_replace("\r", "\n", $text);
4139 1105
4140 - // Clean up existing <br> tags that may have been saved (legacy data)
4141 - // Convert <br>, <br/>, <br /> back to newlines for consistent processing
4142 - $text = preg_replace('/<br\s*\/?>\s*/i', "\n", $text);
4143 -
4144 - // Collapse 3+ consecutive newlines to just 2 (paragraph break)
4145 - $text = preg_replace('/\n{3,}/', "\n\n", $text);
4146 -
4147 - // Process markdown headers (# Header)
4148 - $text = preg_replace_callback('/^(#{1,6})\s+(.+)$/m', function($matches) {
4149 - $level = strlen($matches[1]);
4150 - $content = esc_html(trim($matches[2]));
4151 - return "<h{$level}>{$content}</h{$level}>";
4152 - }, $text);
4153 -
4154 - // Process code blocks with triple backticks
4155 - $text = preg_replace_callback('/```(\w+)?\n?([\s\S]*?)```/', function($matches) {
4156 - $language = !empty($matches[1]) ? ' class="language-' . esc_attr($matches[1]) . '"' : '';
4157 - $code = esc_html($matches[2]);
4158 - return "<pre><code{$language}>{$code}</code></pre>";
4159 - }, $text);
4160 -
4161 - // Process inline code with single backticks
4162 - $text = preg_replace('/`([^`]+)`/', '<code>$1</code>', $text);
4163 -
4164 - // Process bold text **text** or __text__
4165 - $text = preg_replace('/\*\*(.+?)\*\*/', '<strong>$1</strong>', $text);
4166 - $text = preg_replace('/__(.+?)__/', '<strong>$1</strong>', $text);
4167 -
4168 - // Process italic text *text* or _text_ (but not if part of URL)
4169 - $text = preg_replace('/(?<![*_\w])\*([^*]+)\*(?![*\w])/', '<em>$1</em>', $text);
4170 - $text = preg_replace('/(?<![*_\w])_([^_]+)_(?![*\w])/', '<em>$1</em>', $text);
4171 -
4172 - // Process markdown links [text](url)
4173 - $text = preg_replace_callback('/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/', function($matches) {
4174 - $link_text = esc_html($matches[1]);
4175 - $url = esc_url($matches[2]);
4176 - return "<a href=\"{$url}\" target=\"_blank\" rel=\"noopener\">{$link_text}</a>";
4177 - }, $text);
4178 -
4179 - // Process citation-style brackets [URL]
4180 - $text = preg_replace_callback('/\[(https?:\/\/[^\]]+)\]/', function($matches) {
4181 - $url = esc_url($matches[1]);
4182 - return "<a href=\"{$url}\" target=\"_blank\" rel=\"noopener\">{$url}</a>";
4183 - }, $text);
4184 -
4185 - // Process standalone URLs (not already in links or img src)
4186 - $text = preg_replace_callback(
4187 - '/(?<!href="|src="|">)(https?:\/\/[^\s<>"]+)(?![^<]*<\/a>)/',
4188 - function($matches) {
4189 - $url = esc_url($matches[1]);
4190 - // Truncate display URL if too long
4191 - $display = strlen($matches[1]) > 50 ? substr($matches[1], 0, 47) . '...' : $matches[1];
4192 - return "<a href=\"{$url}\" target=\"_blank\" rel=\"noopener\">{$display}</a>";
4193 - },
4194 - $text
4195 - );
4196 -
4197 - // Process mailto links
4198 - $text = preg_replace_callback('/\[([^\]]+)\]\((mailto:[^)]+)\)/', function($matches) {
4199 - $link_text = esc_html($matches[1]);
4200 - $mailto = esc_url($matches[2]);
4201 - return "<a href=\"{$mailto}\">{$link_text}</a>";
4202 - }, $text);
4203 -
4204 - // Convert paragraphs: split by double newlines, wrap in <p> tags
4205 - // This creates proper paragraph structure instead of excessive <br> tags
4206 - $paragraphs = preg_split('/\n\n+/', $text);
4207 -
4208 - // Filter out empty paragraphs but preserve content like "0"
4209 - $paragraphs = array_values(array_filter(array_map('trim', $paragraphs), function($p) {
4210 - return $p !== '';
4211 - }));
4212 -
4213 - if (empty($paragraphs)) {
4214 - // No content after filtering
4215 - return '';
4216 - } elseif (count($paragraphs) > 1) {
4217 - // Multiple paragraphs - wrap each in <p> tags, convert single newlines to <br>
4218 - $formatted_paragraphs = array_map(function($p) {
4219 - return nl2br($p);
4220 - }, $paragraphs);
4221 - $text = '<p>' . implode('</p><p>', $formatted_paragraphs) . '</p>';
4222 - } else {
4223 - // Single paragraph - just convert newlines to <br>
4224 - $text = nl2br($paragraphs[0]);
4225 - }
4226 -
4227 - return $text;
4228 -}
4229 -
4230 -/**
4231 - * AJAX handler to fetch RAG context for a specific message
4232 - * Used by the transcript viewer to show retrieved documents
4233 - */
4234 -public function mxchat_get_rag_context() {
4235 - // Check permissions
4236 - if (!current_user_can('manage_options')) {
4237 - wp_send_json_error(['message' => esc_html__('You do not have sufficient permissions.', 'mxchat')]);
4238 - wp_die();
4239 - }
4240 -
4241 - // Validate message ID
4242 - if (!isset($_POST['message_id']) || empty($_POST['message_id'])) {
4243 - wp_send_json_error(['message' => esc_html__('Message ID is required.', 'mxchat')]);
4244 - wp_die();
4245 - }
4246 -
4247 - $message_id = absint($_POST['message_id']);
4248 -
4249 - global $wpdb;
4250 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4251 -
4252 - // Fetch the RAG context for this message
4253 - $result = $wpdb->get_row($wpdb->prepare(
4254 - "SELECT rag_context FROM {$table_name} WHERE id = %d",
4255 - $message_id
4256 - ));
4257 -
4258 - if (!$result || empty($result->rag_context)) {
4259 - wp_send_json_error(['message' => esc_html__('No RAG context found for this message.', 'mxchat')]);
4260 - wp_die();
4261 - }
4262 -
4263 - // Decode the JSON data
4264 - $rag_context = json_decode($result->rag_context, true);
4265 -
4266 - if (json_last_error() !== JSON_ERROR_NONE) {
4267 - wp_send_json_error(['message' => esc_html__('Invalid RAG context data.', 'mxchat')]);
4268 - wp_die();
4269 - }
4270 -
4271 - wp_send_json_success($rag_context);
4272 - wp_die();
4273 -}
4274 -
4275 -public function display_admin_notices() {
4276 - // Check if we're on a MXChat admin page
4277 - $screen = get_current_screen();
4278 - if (!$screen || strpos($screen->base, 'mxchat') === false) {
4279 - return;
4280 - }
4281 -
4282 - $dismiss_button = '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . esc_html__('Dismiss this notice.', 'mxchat') . '</span></button>';
4283 -
4284 - // Check for error notices
4285 - $error_notice = get_transient('mxchat_admin_notice_error');
4286 - if ($error_notice) {
4287 - echo '<div class="notice notice-error is-dismissible"><p>' . wp_kses_post($error_notice) . '</p>' . $dismiss_button . '</div>';
4288 - delete_transient('mxchat_admin_notice_error');
4289 - }
4290 -
4291 - // Check for success notices
4292 - $success_notice = get_transient('mxchat_admin_notice_success');
4293 - if ($success_notice) {
4294 - echo '<div class="notice notice-success is-dismissible"><p>' . wp_kses_post($success_notice) . '</p>' . $dismiss_button . '</div>';
4295 - delete_transient('mxchat_admin_notice_success');
4296 - }
4297 -
4298 - // Check for info notices
4299 - $info_notice = get_transient('mxchat_admin_notice_info');
4300 - if ($info_notice) {
4301 - echo '<div class="notice notice-info is-dismissible"><p>' . wp_kses_post($info_notice) . '</p>' . $dismiss_button . '</div>';
4302 - delete_transient('mxchat_admin_notice_info');
4303 - }
4304 -}
4305 -
4306 -
4307 1106 public function mxchat_create_activation_page() {
4308 - // Include the new Pro & Extensions page template
4309 - require_once plugin_dir_path(__FILE__) . 'admin-pro-page.php';
1107 + $license_status = get_option('mxchat_license_status', 'inactive');
1108 + $license_error = get_option('mxchat_license_error', '');
1109 + ?>
1110 + <div class="wrap mxchat-admin">
1111 + <h2>MxChat Pro: Activation</h2>
1112 + <?php if ($license_status === 'inactive' && !empty($license_error)): ?>
1113 + <div class="error notice">
1114 + <p><?php echo esc_html($license_error); ?></p>
1115 + </div>
1116 + <?php endif; ?>
4310 1117
4311 - // Get addons configuration from the MxChat_Addons class
4312 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-addons.php';
4313 - $addons_instance = new MxChat_Addons();
4314 - $addons_config = $addons_instance->get_addons_config();
1118 + <form id="mxchat-activation-form" style="<?php echo $license_status === 'active' ? 'display: none;' : ''; ?>">
1119 + <table class="form-table">
1120 + <tr valign="top">
1121 + <th scope="row">Email Address</th>
1122 + <td>
1123 + <input type="email" id="mxchat_pro_email" name="mxchat_pro_email" value="<?php echo esc_attr(get_option('mxchat_pro_email')); ?>" class="regular-text" required />
1124 + </td>
1125 + </tr>
1126 + <tr valign="top">
1127 + <th scope="row">Activation Key</th>
1128 + <td>
1129 + <input type="text" id="mxchat_activation_key" name="mxchat_activation_key" value="<?php echo esc_attr(get_option('mxchat_activation_key')); ?>" class="regular-text" required />
1130 + </td>
1131 + </tr>
1132 + </table>
1133 + <?php if ($license_status !== 'active'): ?>
1134 + <button type="submit" id="activate_license_button" class="button button-primary"><?php esc_html_e('Activate License', 'mxchat'); ?></button>
1135 + <div id="mxchat-activation-spinner" class="mxchat-activation-spinner" style="display: none;"></div>
1136 + <?php endif; ?>
1137 + </form>
4315 1138
4316 - // Render the consolidated Pro & Extensions page
4317 - mxchat_render_pro_page($this, $addons_config);
1139 + <!-- License Status Display -->
1140 + <h3>License Status: <span id="mxchat-license-status"><?php echo $license_status === 'active' ? 'Active' : 'Inactive'; ?></span></h3>
1141 + </div>
1142 + <?php
4318 1143 }
4319 1144
4320 -/**
4321 - * Check if current activation is linked to a domain
4322 - * This checks YOUR website's database, not the user's local database
4323 - */
4324 -public function is_current_activation_linked($domain) {
4325 - $license_key = get_option('mxchat_activation_key');
4326 - $email = get_option('mxchat_pro_email');
4327 -
4328 - if (empty($license_key) || empty($email)) {
4329 - return false;
4330 - }
4331 -
4332 - // Check with YOUR website's API
4333 - $response = wp_remote_post('https://mxchat.ai/mxchat-api/check-domain', array(
4334 - 'body' => array(
4335 - 'license_key' => $license_key,
4336 - 'email' => $email,
4337 - 'domain' => $domain
4338 - ),
4339 - 'timeout' => 10,
4340 - 'sslverify' => false
4341 - ));
4342 -
4343 - if (is_wp_error($response)) {
4344 - return false;
4345 - }
4346 -
4347 - $body = json_decode(wp_remote_retrieve_body($response), true);
4348 - return isset($body['success']) && $body['success'] && isset($body['data']['linked']) && $body['data']['linked'];
4349 -}
4350 -
4351 -
4352 -public function mxchat_actions_page_html() {
4353 - if (!current_user_can('manage_options')) {
1145 +public function mxchat_intents_page_html() {
1146 + if ( ! current_user_can( 'manage_options' ) ) {
4354 1147 return;
4355 1148 }
4356 1149
1150 + // Fetch existing intents with pagination and filtering
4357 1151 global $wpdb;
4358 1152 $table_name = $wpdb->prefix . 'mxchat_intents';
1153 + $page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
1154 + $per_page = 20;
1155 + $offset = ( $page - 1 ) * $per_page;
4359 1156
4360 - // Get stats for dashboard
4361 - $total_actions = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
4362 - $enabled_actions = $wpdb->get_var("SELECT COUNT(*) FROM $table_name WHERE enabled = 1");
4363 - $disabled_actions = $total_actions - $enabled_actions;
4364 1157
4365 - // Get unique action types count
4366 - $action_types_count = $wpdb->get_var("SELECT COUNT(DISTINCT callback_function) FROM $table_name");
1158 + // Add at the start of mxchat_intents_page_html()
1159 +if (isset($_GET['updated']) && $_GET['updated'] === 'true') {
1160 + echo '<div class="notice notice-success is-dismissible"><p>' .
1161 + esc_html__('Intent updated successfully.', 'mxchat') .
1162 + '</p></div>';
1163 +}
4367 1164
4368 - // Get action type distribution
4369 - $type_distribution_raw = $wpdb->get_results("SELECT callback_function, COUNT(*) as count FROM $table_name GROUP BY callback_function ORDER BY count DESC LIMIT 10");
4370 - $available_callbacks = $this->mxchat_get_available_callbacks();
4371 - $callback_groups = $this->mxchat_get_available_callbacks(true, true);
1165 + // Build the WHERE clause based on filters
1166 + $where = '1=1';
1167 + $search_term = isset( $_GET['s'] ) ? trim( $_GET['s'] ) : '';
1168 + $callback_filter = isset( $_GET['callback_filter'] ) ? sanitize_text_field( $_GET['callback_filter'] ) : '';
4372 1169
4373 - $action_type_distribution = array();
4374 - foreach ($type_distribution_raw as $row) {
4375 - $label = isset($available_callbacks[$row->callback_function]['label'])
4376 - ? $available_callbacks[$row->callback_function]['label']
4377 - : $row->callback_function;
4378 - $action_type_distribution[$label] = $row->count;
1170 + if ( $search_term ) {
1171 + $search_term_like = '%' . $wpdb->esc_like( $search_term ) . '%';
1172 + $where .= $wpdb->prepare( ' AND (intent_label LIKE %s OR phrases LIKE %s)', $search_term_like, $search_term_like );
4379 1173 }
4380 1174
4381 - // Native function-calling (AI Tools) data — plan-mxchat-20260617-a41dee.
4382 - // The AI Tools checklist reads from MxChat_Tool_Registry, the SAME single
4383 - // source the chat-time function-calling loop reads, so the two never drift.
4384 - if (!class_exists('MxChat_Tool_Registry')) {
4385 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-tool-registry.php';
1175 + if ( $callback_filter ) {
1176 + $where .= $wpdb->prepare( ' AND callback_function = %s', $callback_filter );
4386 1177 }
4387 - if (!class_exists('MxChat_Model_Catalog')) {
4388 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-model-catalog.php';
4389 - }
4390 - $fc_options = get_option('mxchat_options', array());
4391 - $fc_current_model = isset($fc_options['model']) ? $fc_options['model'] : 'gpt-5.6-sol';
4392 - $fc_model_capable = class_exists('MxChat_Model_Catalog')
4393 - ? MxChat_Model_Catalog::supports_tools($fc_current_model) : true;
4394 - $active_tab = (isset($_GET['tab']) && $_GET['tab'] === 'ai-tools') ? 'ai-tools' : 'dashboard';
4395 1178
4396 - // Enrich each AI Tool with the dashicon its callback already uses on the
4397 - // Trigger Phrases "Choose what it does" grid, so the AI Tools cards/modal
4398 - // share the same iconography (plan 8bbf98 part 4). View-layer only — the
4399 - // registry's model-facing data is untouched. Default admin-generic.
4400 - $fc_tools = MxChat_Tool_Registry::available_tools();
4401 - foreach ($fc_tools as &$fc_tool_ref) {
4402 - $fc_cb_ref = $fc_tool_ref['callback'];
4403 - $fc_tool_ref['icon'] = isset($available_callbacks[$fc_cb_ref]['icon'])
4404 - ? $available_callbacks[$fc_cb_ref]['icon']
4405 - : 'admin-generic';
4406 - }
4407 - unset($fc_tool_ref);
1179 + // Get total count for pagination
1180 + $total_intents = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name WHERE $where" );
1181 + $total_pages = ceil( $total_intents / $per_page );
4408 1182
4409 - // Count of ACTIVE tools — drives the AI Tools sidebar nav badge, mirroring
4410 - // $total_actions for Trigger Phrases. A tool in the list = active (plan
4411 - // d450a7), so the badge shows the same number the list pane shows (plan 5f7409).
4412 - $total_tools = count(array_filter($fc_tools, function ($t) {
4413 - return !empty($t['enabled']);
4414 - }));
1183 + // Fetch intents with pagination and filters
1184 + $intents = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE $where LIMIT %d OFFSET %d", $per_page, $offset ) );
4415 1185
4416 - // Brave-key dependency check (plan 183856): Web Search + Image Search run on
4417 - // the Brave Search API. If either is enabled as a tool but no brave_api_key is
4418 - // configured, surface a graceful "key not set" notice so the admin isn't met
4419 - // with a silently no-firing tool.
4420 - $fc_brave_missing = false;
4421 - $brave_key = isset($fc_options['brave_api_key']) ? trim($fc_options['brave_api_key']) : '';
4422 - if ($brave_key === '') {
4423 - foreach ($fc_tools as $fc_t) {
4424 - if (!empty($fc_t['enabled']) && isset($fc_t['requires_key']) && $fc_t['requires_key'] === 'brave_api_key') {
4425 - $fc_brave_missing = true;
4426 - break;
4427 - }
4428 - }
4429 - }
1186 + // Get available callback functions with 'pro_only' flag
1187 + $available_callbacks = $this->mxchat_get_available_callbacks();
1188 + ?>
4430 1189
4431 - // Prepare page data
4432 - $page_data = array(
4433 - 'total_actions' => $total_actions,
4434 - 'enabled_actions' => $enabled_actions,
4435 - 'disabled_actions' => $disabled_actions,
4436 - 'action_types_count' => $action_types_count,
4437 - 'action_type_distribution' => $action_type_distribution,
4438 - 'available_callbacks' => $available_callbacks,
4439 - 'callback_groups' => $callback_groups,
4440 - // AI Tools section
4441 - 'active_tab' => $active_tab,
4442 - 'fc_enabled' => MxChat_Tool_Registry::is_enabled(),
4443 - 'total_tools' => $total_tools,
4444 - 'fc_tools' => $fc_tools,
4445 - 'fc_current_model' => $fc_current_model,
4446 - 'fc_model_capable' => $fc_model_capable,
4447 - 'fc_brave_missing' => $fc_brave_missing,
4448 - 'fc_saved' => isset($_GET['mxchat_fc_saved']),
4449 - );
4450 -
4451 - // Include and render the new template
4452 - require_once plugin_dir_path(__FILE__) . 'admin-actions-page.php';
4453 - mxchat_render_actions_page($this, $page_data);
4454 -}
4455 -
4456 -/**
4457 - * LEGACY HTML - Preserved below for reference, to be removed in future update
4458 - */
4459 -function mxchat_actions_page_legacy_html() {
4460 - // This function is deprecated and no longer used
4461 - // The new template is in includes/admin-actions-page.php
4462 - ?>
4463 - <div class="wrap mxchat-wrapper">
4464 - <!-- Hero Section -->
4465 - <div class="mxchat-hero">
4466 - <h1 class="mxchat-main-title">
4467 - <span class="mxchat-gradient-text">Actions</span> Manager
4468 - </h1>
4469 - <p class="mxchat-hero-subtitle">
4470 - <?php esc_html_e('Create and manage custom actions to enhance your chatbot\'s capabilities.', 'mxchat'); ?>
1190 + <div class="wrap mxchat-admin">
1191 + <!-- Add Intent Form -->
1192 + <form id="mxchat-add-intent-form" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
1193 + <input type="hidden" name="action" value="mxchat_add_intent">
1194 + <?php wp_nonce_field( 'mxchat_add_intent_nonce' ); ?>
1195 + <h2><?php esc_html_e( 'Add New Intent', 'mxchat' ); ?></h2>
1196 + <p class="description">
1197 + <?php esc_html_e( 'We highly encourage users to quickly read our ', 'mxchat' ); ?>
1198 + <a href="https://mxchat.ai/documentation/#intents" target="_blank" rel="noopener noreferrer">
1199 + <?php esc_html_e( 'documentation', 'mxchat' ); ?>
1200 + </a>
1201 + <?php esc_html_e( ' to better understand intents. Some intents require setup in the Integration tab. If your intent is not triggering lower similarity threshold test.', 'mxchat' ); ?>
4471 1202 </p>
4472 - </div>
4473 1203
4474 - <!-- Actions Header with Search and Filter -->
4475 - <div class="mxchat-actions-header">
4476 - <div class="mxchat-actions-filters">
4477 - <form method="get" class="mxchat-search-form">
4478 - <input type="hidden" name="page" value="mxchat-actions">
4479 - <div class="mxchat-search-group">
4480 - <span class="dashicons dashicons-search"></span>
4481 - <input type="text" name="s" class="mxchat-search-input"
4482 - placeholder="<?php esc_attr_e('Search Actions', 'mxchat'); ?>"
4483 - value="<?php echo esc_attr($search_term); ?>">
4484 - </div>
4485 - <select name="callback_filter" class="mxchat-action-filter">
4486 - <option value=""><?php esc_html_e('All Action Types', 'mxchat'); ?></option>
4487 - <?php foreach ($available_callbacks as $function => $callback_data) :
4488 - $label = $callback_data['label']; ?>
4489 - <option value="<?php echo esc_attr($function); ?>"
4490 - <?php selected($callback_filter, $function); ?>>
4491 - <?php echo esc_html($label); ?>
4492 - </option>
4493 - <?php endforeach; ?>
4494 - </select>
4495 - <button type="submit" class="mxchat-button-secondary">
4496 - <?php esc_html_e('Filter', 'mxchat'); ?>
4497 - </button>
4498 - </form>
1204 + <div class="mxchat-form-group">
1205 + <label for="intent_label"><?php esc_html_e( 'Intent Label (For your reference only)', 'mxchat' ); ?></label>
1206 + <input name="intent_label" type="text" id="intent_label" class="regular-text" required
1207 + placeholder="Example Email Capture: Newsletter Signup">
4499 1208 </div>
4500 - <div class="mxchat-actions-controls">
4501 - <button type="button" id="mxchat-add-action-btn" class="mxchat-button-primary">
4502 - <span class="dashicons dashicons-plus-alt"></span>
4503 - <?php esc_html_e('Add New Action', 'mxchat'); ?>
4504 - </button>
1209 + <div class="mxchat-form-group">
1210 + <label for="phrases"><?php esc_html_e( 'Phrases (comma-separated)', 'mxchat' ); ?></label>
1211 + <textarea name="phrases" id="phrases" rows="5" class="large-text" required
1212 + placeholder="Example Email Capture: sign me up, subscribe me, I want to join, add me to the newsletter, send me updates, keep me informed"></textarea>
4505 1213 </div>
4506 - </div>
1214 + <div class="mxchat-form-group">
1215 + <label for="callback_function"><?php esc_html_e( 'Callback Function', 'mxchat' ); ?></label>
1216 +<select name="callback_function" id="callback_function" required>
1217 + <option value=""><?php esc_html_e('Select a Callback', 'mxchat'); ?></option>
1218 + <?php
1219 + $groups = $this->mxchat_get_available_callbacks(true); // Fetch grouped data
4507 1220
4508 - <!-- Actions Grid Layout - All actions in a single grid -->
4509 - <div class="mxchat-actions-grid">
4510 - <div class="mxchat-cards-container">
4511 - <?php if (!empty($actions)) : ?>
4512 - <?php foreach ($actions as $action) :
4513 - $callback_function = $action->callback_function;
4514 - $callback_label = isset($available_callbacks[$callback_function]['label'])
4515 - ? $available_callbacks[$callback_function]['label']
4516 - : $callback_function;
4517 - $threshold_value = isset($action->similarity_threshold)
4518 - ? round($action->similarity_threshold * 100)
4519 - : 85;
1221 + foreach ($groups as $group_label => $group_callbacks) :
1222 + echo '<optgroup label="' . esc_attr($group_label) . '">';
1223 + foreach ($group_callbacks as $function => $data) :
1224 + $label = $data['label'];
1225 + $pro_only = $data['pro_only'];
1226 + $disabled = (!$this->is_activated && $pro_only) ? 'disabled' : '';
1227 + $label_suffix = (!$this->is_activated && $pro_only) ? ' (Pro Only)' : '';
1228 + ?>
1229 + <option value="<?php echo esc_attr($function); ?>" <?php echo $disabled; ?>>
1230 + <?php echo esc_html($label . $label_suffix); ?>
1231 + </option>
1232 + <?php endforeach;
1233 + echo '</optgroup>';
1234 + endforeach;
1235 + ?>
1236 +</select>
4520 1237
4521 - // Check if this is a form action
4522 - $is_form_action = strpos($action->intent_label, 'Form ') === 0;
4523 1238
4524 - // Get action status (enabled/disabled) - default to true if column doesn't exist
4525 - $is_enabled = isset($action->enabled) ? (bool)$action->enabled : true;
4526 -
4527 - // Get enabled bots for display
4528 - $enabled_bots = [];
4529 - if (isset($action->enabled_bots) && !empty($action->enabled_bots)) {
4530 - $enabled_bots = json_decode($action->enabled_bots, true);
4531 - if (!is_array($enabled_bots)) {
4532 - $enabled_bots = ['default'];
4533 - }
4534 - } else {
4535 - $enabled_bots = ['default']; // Backward compatibility
4536 - }
4537 - ?>
4538 - <div class="mxchat-action-card <?php echo $is_form_action ? 'mxchat-form-action' : ''; ?>">
4539 - <div class="mxchat-card-header">
4540 - <div class="mxchat-card-title"><?php echo esc_html($action->intent_label); ?></div>
4541 - <div class="mxchat-card-toggle">
4542 - <label class="mxchat-switch">
4543 - <input type="checkbox" class="mxchat-action-toggle"
4544 - data-action-id="<?php echo esc_attr($action->id); ?>"
4545 - <?php checked($is_enabled); ?>>
4546 - <span class="mxchat-slider round"></span>
4547 - </label>
4548 - </div>
4549 - </div>
4550 1239
4551 - <div class="mxchat-card-body">
4552 - <div class="mxchat-card-description">
4553 - <strong><?php esc_html_e('Type:', 'mxchat'); ?></strong>
4554 - <?php echo esc_html($callback_label); ?>
4555 - </div>
4556 -
4557 - <div class="mxchat-card-phrases">
4558 - <strong><?php esc_html_e('Trigger phrases:', 'mxchat'); ?></strong>
4559 - <div class="mxchat-phrases-preview">
4560 - <?php
4561 - // Check if the helper function exists, otherwise use a simple substring
4562 - if (method_exists($this, 'get_trimmed_phrases')) {
4563 - echo esc_html($this->get_trimmed_phrases($action->phrases));
4564 - } else {
4565 - echo esc_html(strlen($action->phrases) > 100 ?
4566 - substr($action->phrases, 0, 97) . '...' :
4567 - $action->phrases);
4568 - }
4569 - ?>
4570 - </div>
4571 - </div>
4572 -
4573 - <div class="mxchat-threshold-control">
4574 - <div class="mxchat-threshold-label">
4575 - <?php esc_html_e('Similarity Threshold:', 'mxchat'); ?>
4576 - <span class="mxchat-threshold-value"><?php echo esc_html($threshold_value); ?>%</span>
4577 - </div>
4578 - </div>
4579 -
4580 - <!-- Bot Availability Display (Read-only) -->
4581 - <div class="mxchat-card-bots">
4582 - <strong><?php esc_html_e('Assigned bots:', 'mxchat'); ?></strong>
4583 - <div class="mxchat-bot-badges">
4584 - <?php
4585 - foreach ($enabled_bots as $bot_id) {
4586 - if ($bot_id === 'default') {
4587 - echo '<span class="mxchat-bot-badge">' . esc_html__('Default Bot', 'mxchat') . '</span>';
4588 - } else {
4589 - // Try to get bot name from multi-bot manager
4590 - if (class_exists('MxChat_Multi_Bot_Core_Manager')) {
4591 - $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance();
4592 - $available_bots = $multi_bot_manager->get_available_bots();
4593 - $bot_name = isset($available_bots[$bot_id]) ? $available_bots[$bot_id] : $bot_id;
4594 - echo '<span class="mxchat-bot-badge">' . esc_html($bot_name) . '</span>';
4595 - } else {
4596 - echo '<span class="mxchat-bot-badge">' . esc_html($bot_id) . '</span>';
4597 - }
4598 - }
4599 - }
4600 - ?>
4601 - </div>
4602 - </div>
4603 - </div>
4604 -
4605 - <div class="mxchat-card-footer">
4606 - <?php
4607 - // Check if it's a form action
4608 - $is_form_action = preg_match('/Form (\d+)/', $action->intent_label, $form_matches);
4609 -
4610 - // Check if it's a recommendation flow action
4611 - $is_flow_action = preg_match('/Recommendation Flow (\d+)/', $action->intent_label, $flow_matches);
4612 -
4613 - if ($is_form_action) {
4614 - $form_id = isset($form_matches[1]) ? $form_matches[1] : '';
4615 - ?>
4616 - <a href="<?php echo esc_url(admin_url('admin.php?page=mxchat-forms&action=edit&form_id=' . $form_id)); ?>"
4617 - class="mxchat-button-primary">
4618 - <span class="dashicons dashicons-feedback"></span>
4619 - <?php esc_html_e('Edit Form', 'mxchat'); ?>
4620 - </a>
4621 - <?php } elseif ($is_flow_action) {
4622 - ?>
4623 - <a href="<?php echo esc_url(admin_url('admin.php?page=mxchat-smart-recommender')); ?>"
4624 - class="mxchat-button-primary">
4625 - <span class="dashicons dashicons-list-view"></span>
4626 - <?php esc_html_e('Manage Flows', 'mxchat'); ?>
4627 - </a>
4628 - <?php } else { ?>
4629 - <button type="button"
4630 - class="mxchat-button-secondary mxchat-edit-button"
4631 - data-action-id="<?php echo esc_attr($action->id); ?>"
4632 - data-phrases="<?php echo esc_attr($action->phrases); ?>"
4633 - data-label="<?php echo esc_attr($action->intent_label); ?>"
4634 - data-threshold="<?php echo esc_attr(round($action->similarity_threshold * 100)); ?>"
4635 - data-callback-function="<?php echo esc_attr($action->callback_function); ?>"
4636 - data-enabled-bots="<?php echo esc_attr(json_encode($enabled_bots)); ?>">
4637 - <span class="dashicons dashicons-edit"></span>
4638 - <?php esc_html_e('Edit', 'mxchat'); ?>
4639 - </button>
4640 - <form method="post"
4641 - action="<?php echo esc_url(admin_url('admin-post.php')); ?>"
4642 - class="mxchat-delete-form"
4643 - onsubmit="return confirm('<?php esc_attr_e('Are you sure you want to delete this action?', 'mxchat'); ?>');">
4644 - <?php wp_nonce_field('mxchat_delete_intent_nonce'); ?>
4645 - <input type="hidden" name="action" value="mxchat_delete_intent">
4646 - <input type="hidden" name="intent_id" value="<?php echo esc_attr($action->id); ?>">
4647 - <button type="submit" class="mxchat-button-text mxchat-delete-button">
4648 - <span class="dashicons dashicons-trash"></span>
4649 - <?php esc_html_e('Delete', 'mxchat'); ?>
4650 - </button>
4651 - </form>
4652 - <?php } ?>
4653 - </div>
4654 - </div>
4655 - <?php endforeach; ?>
4656 - <?php else : ?>
4657 - <!-- If no actions found -->
4658 - <div class="mxchat-no-actions">
4659 - <div class="mxchat-empty-state">
4660 - <span class="dashicons dashicons-format-chat"></span>
4661 - <h2><?php esc_html_e('No actions found', 'mxchat'); ?></h2>
4662 - <p><?php esc_html_e('Get started by creating your first action to enhance your chatbot.', 'mxchat'); ?></p>
4663 - <button type="button" id="mxchat-create-first-action" class="mxchat-button-primary">
4664 - <?php esc_html_e('Create Your First Action', 'mxchat'); ?>
4665 - </button>
4666 - </div>
4667 - </div>
4668 - <?php endif; ?>
4669 1240 </div>
1241 + <button type="submit" class="button button-primary submit-content-button">
1242 + <?php esc_html_e( 'Add Intent', 'mxchat' ); ?>
1243 + </button>
1244 + <div id="mxchat-intent-loading" style="display: none;"></div>
1245 + <div id="mxchat-intent-loading-text" style="display: none;">
1246 + <?php esc_html_e( 'Saving intent, please wait...', 'mxchat' ); ?>
4670 1247 </div>
4671 1248
4672 - <?php if ($total_pages > 1) : ?>
4673 - <div class="mxchat-pagination">
4674 - <?php
4675 - echo paginate_links(array(
4676 - 'base' => add_query_arg('paged', '%#%'),
4677 - 'format' => '',
4678 - 'prev_text' => __('&laquo; Previous', 'mxchat'),
4679 - 'next_text' => __('Next &raquo;', 'mxchat'),
4680 - 'total' => $total_pages,
4681 - 'current' => $page
4682 - ));
4683 - ?>
4684 - </div>
4685 - <?php endif; ?>
1249 + </form>
4686 1250
4687 -<!-- Add/Edit Action Modal with Step-Based Approach -->
4688 -<!-- Complete Modal HTML with Defined Groups Variable -->
4689 -<div id="mxchat-action-modal" class="mxchat-modal" style="display: none;">
4690 - <div class="mxchat-modal-content">
4691 - <span class="mxchat-modal-close">&times;</span>
4692 -
4693 - <form id="mxchat-action-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
4694 - <!-- Dynamic nonce field -->
4695 - <div id="action-nonce-container">
4696 - <?php wp_nonce_field('mxchat_add_intent_nonce', 'add_intent_nonce'); ?>
1251 + <!-- Filter Form -->
1252 + <form method="get" action="">
1253 + <input type="hidden" name="page" value="mxchat-intents">
1254 + <div class="mxchat-search-group">
1255 + <input type="text" name="s" id="mxchat-intent-search" placeholder="<?php esc_attr_e( 'Search Intents', 'mxchat' ); ?>" value="<?php echo esc_attr( $search_term ); ?>">
1256 + <select name="callback_filter" id="mxchat-callback-filter">
1257 + <option value=""><?php esc_html_e( 'All Callbacks', 'mxchat' ); ?></option>
1258 + <?php foreach ( $available_callbacks as $function => $callback_data ) : ?>
1259 + <?php $label = $callback_data['label']; ?>
1260 + <option value="<?php echo esc_attr( $function ); ?>" <?php selected( $callback_filter, $function ); ?>><?php echo esc_html( $label ); ?></option>
1261 + <?php endforeach; ?>
1262 + </select>
1263 + <button type="submit" class="button"><?php esc_html_e( 'Filter', 'mxchat' ); ?></button>
4697 1264 </div>
4698 - <input type="hidden" name="action" id="form_action_type" value="mxchat_add_intent">
4699 - <input type="hidden" name="intent_id" id="edit_action_id" value="">
4700 - <input type="hidden" name="callback_function" id="callback_function" value="">
1265 + </form>
4701 1266
4702 - <!-- Step 1: Action Type Selection -->
4703 - <div id="mxchat-action-step-1" class="mxchat-action-step active">
4704 - <div class="mxchat-step-indicator">
4705 - <div class="mxchat-step-number">1</div>
4706 - <div class="mxchat-step-title"><?php esc_html_e('Select Action Type', 'mxchat'); ?></div>
4707 - </div>
4708 -
4709 - <div id="mxchat-action-type-selector" class="mxchat-action-type-selector">
4710 - <div class="mxchat-action-type-search">
4711 - <span class="dashicons dashicons-search"></span>
4712 - <input type="text" id="action-type-search" placeholder="<?php esc_attr_e('Search action types...', 'mxchat'); ?>" class="mxchat-action-type-search-input">
4713 - </div>
4714 -
4715 - <?php
4716 - // Get the callbacks - IMPORTANT: Define the $groups variable here
4717 - $groups = $this->mxchat_get_available_callbacks(true, true);
4718 - ?>
4719 -
4720 - <div class="mxchat-action-type-categories">
4721 - <button type="button" class="mxchat-category-button active" data-category="all"><?php esc_html_e('All', 'mxchat'); ?></button>
1267 + <!-- Intents Table -->
1268 + <table class="wp-list-table mxchat-intents-table widefat fixed striped">
1269 + <thead>
1270 + <tr>
1271 + <th><?php esc_html_e( 'Intent Label', 'mxchat' ); ?></th>
1272 + <th><?php esc_html_e( 'Phrases', 'mxchat' ); ?></th>
1273 + <th><?php esc_html_e( 'Callback Function', 'mxchat' ); ?></th>
1274 + <th><?php esc_html_e( 'Similarity Threshold', 'mxchat' ); ?></th>
1275 + <th><?php esc_html_e( 'Actions', 'mxchat' ); ?></th>
1276 + </tr>
1277 + </thead>
1278 + <tbody>
1279 + <?php if ( $intents ) : ?>
1280 + <?php foreach ( $intents as $intent ) : ?>
4722 1281 <?php
4723 - // Get unique categories from the defined groups
4724 - foreach ($groups as $group_label => $group_callbacks) :
4725 - $category_slug = sanitize_title($group_label);
1282 + $callback_function = $intent->callback_function;
1283 + $callback_label = isset( $available_callbacks[ $callback_function ]['label'] ) ? $available_callbacks[ $callback_function ]['label'] : $callback_function;
1284 + $threshold_value = isset( $intent->similarity_threshold ) ? round( $intent->similarity_threshold * 100 ) : 85;
4726 1285 ?>
4727 - <button type="button" class="mxchat-category-button" data-category="<?php echo esc_attr($category_slug); ?>"><?php echo esc_html($group_label); ?></button>
4728 - <?php endforeach; ?>
4729 - </div>
1286 + <tr>
1287 + <td><?php echo esc_html( $intent->intent_label ); ?></td>
1288 + <td><?php echo esc_html( $intent->phrases ); ?></td>
1289 + <td><?php echo esc_html( $callback_label ); ?></td>
1290 + <!-- Similarity Threshold Column -->
1291 + <td>
1292 + <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
1293 + <?php wp_nonce_field( 'mxchat_update_intent_threshold_nonce' ); ?>
1294 + <input type="hidden" name="action" value="mxchat_update_intent_threshold">
1295 + <input type="hidden" name="intent_id" value="<?php echo esc_attr( $intent->id ); ?>">
1296 + <input type="range" name="intent_threshold" id="intent_threshold_<?php echo esc_attr( $intent->id ); ?>" min="70" max="95" value="<?php echo esc_attr( $threshold_value ); ?>" oninput="document.getElementById('threshold_output_<?php echo esc_attr( $intent->id ); ?>').value = this.value + '%'">
1297 + <output id="threshold_output_<?php echo esc_attr( $intent->id ); ?>"><?php echo esc_html( $threshold_value ); ?>%</output>
1298 + </td>
1299 + <!-- Actions Column -->
1300 + <td>
1301 + <button type="submit" class="button button-primary mxchat-save-button">
1302 + <?php esc_html_e( 'Save', 'mxchat' ); ?>
1303 + </button>
1304 + </form>
4730 1305
4731 - <div class="mxchat-action-types-grid">
4732 - <?php
4733 - // Generate action cards from available callbacks
4734 - foreach ($groups as $group_label => $group_callbacks) :
4735 - $category_slug = sanitize_title($group_label);
4736 1306
4737 - foreach ($group_callbacks as $function => $data) :
4738 - $label = $data['label'];
4739 - $icon = isset($data['icon']) ? $data['icon'] : 'admin-generic';
4740 - $description = isset($data['description']) ? $data['description'] : '';
4741 - $is_addon = isset($data['addon']) && $data['addon'] !== false;
4742 - $addon_name = isset($data['addon_name']) ? $data['addon_name'] : '';
4743 - $is_installed = isset($data['installed']) ? $data['installed'] : true;
4744 - $is_promo = !empty($data['addon_promo']) && !$is_installed;
4745 1307
4746 - // Determine card status and styling
4747 - $card_class = 'mxchat-action-type-card';
4748 - $icon_class = 'mxchat-action-type-icon';
4749 - $status_badge = '';
4750 1308
4751 - if ($is_promo) {
4752 - // Promotional card — not selectable, just informational
4753 - $card_class .= ' not-installed mxchat-promo-card';
4754 - $status_badge = '<span class="mxchat-addon-badge">' . esc_html__('Add-on Required', 'mxchat') . '</span>';
4755 - } elseif ($is_addon && !$is_installed) {
4756 - // Add-on not installed
4757 - $card_class .= ' not-installed';
4758 - $status_badge .= '<span class="mxchat-addon-badge">' . esc_html__('Add-on Required', 'mxchat') . '</span>';
4759 - }
1309 + <button type="button"
1310 + class="button button-secondary mxchat-edit-button"
1311 + data-intent-id="<?php echo esc_attr($intent->id); ?>"
1312 + data-phrases="<?php echo esc_attr($intent->phrases); ?>">
1313 + <?php esc_html_e('Edit', 'mxchat'); ?>
1314 + </button>
4760 1315
4761 - // Default description if none provided
4762 - if (empty($description)) {
4763 - $description = sprintf(
4764 - esc_html__('Use the %s action in your chatbot', 'mxchat'),
4765 - $label
4766 - );
4767 - }
4768 - ?>
4769 - <div class="<?php echo esc_attr($card_class); ?>"
4770 - data-category="<?php echo esc_attr($category_slug); ?>"
4771 - <?php if (!$is_promo) : ?>
4772 - data-value="<?php echo esc_attr($function); ?>"
4773 - data-label="<?php echo esc_attr($label); ?>"
4774 - <?php endif; ?>
4775 - data-pro="false"
4776 - data-addon="<?php echo esc_attr($is_addon ? $data['addon'] : ''); ?>"
4777 - data-installed="<?php echo $is_installed ? 'true' : 'false'; ?>"
4778 - <?php if ($is_promo) : ?>data-promo="true"<?php endif; ?>>
4779 - <div class="<?php echo esc_attr($icon_class); ?>">
4780 - <span class="dashicons dashicons-<?php echo esc_attr($icon); ?>"></span>
4781 - </div>
4782 - <div class="mxchat-action-type-info">
4783 - <h4><?php echo esc_html($label); ?></h4>
4784 - <p><?php echo esc_html($description); ?></p>
4785 - <?php if (!empty($status_badge)) : ?>
4786 - <?php echo $status_badge; ?>
4787 - <?php endif; ?>
4788 1316
4789 - <?php if ($is_promo || ($is_addon && !$is_installed)) : ?>
4790 - <div class="mxchat-addon-info">
4791 - <?php echo esc_html(sprintf(
4792 - __('Requires %s', 'mxchat'),
4793 - $addon_name
4794 - )); ?>
4795 - — <a href="https://mxchat.ai/" target="_blank"><?php esc_html_e('Get Add-on', 'mxchat'); ?></a>
4796 - </div>
4797 - <?php endif; ?>
4798 - </div>
4799 - </div>
4800 - <?php endforeach;
4801 - endforeach; ?>
4802 - </div>
4803 - </div>
4804 1317
4805 - <div class="mxchat-modal-actions">
4806 - <button type="button" class="mxchat-button-secondary mxchat-modal-cancel">
4807 - <?php esc_html_e('Cancel', 'mxchat'); ?>
4808 - </button>
4809 - </div>
4810 - </div>
4811 1318
4812 - <!-- Step 2: Action Configuration -->
4813 - <div id="mxchat-action-step-2" class="mxchat-action-step">
4814 - <div class="mxchat-step-indicator">
4815 - <div class="mxchat-step-number">2</div>
4816 - <div class="mxchat-step-title"><?php esc_html_e('Configure Action', 'mxchat'); ?></div>
4817 - </div>
1319 + <!-- Delete Form -->
1320 + <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" onsubmit="return confirm('<?php esc_attr_e( 'Are you sure you want to delete this intent?', 'mxchat' ); ?>');">
1321 + <?php wp_nonce_field( 'mxchat_delete_intent_nonce' ); ?>
1322 + <input type="hidden" name="action" value="mxchat_delete_intent">
1323 + <input type="hidden" name="intent_id" value="<?php echo esc_attr( $intent->id ); ?>">
1324 + <button type="submit" class="button mxchat-delete-all">
1325 + <?php esc_html_e( 'Delete', 'mxchat' ); ?>
1326 + </button>
1327 + </form>
1328 + </td>
1329 + </tr>
1330 + <?php endforeach; ?>
1331 + <?php else : ?>
1332 + <tr>
1333 + <td colspan="5"><?php esc_html_e( 'No intents found.', 'mxchat' ); ?></td>
1334 + </tr>
1335 + <?php endif; ?>
1336 + </tbody>
1337 + </table>
4818 1338
4819 - <div class="mxchat-selected-action">
4820 - <button type="button" class="mxchat-back-button" id="mxchat-back-to-step-1">
4821 - <span class="dashicons dashicons-arrow-left-alt"></span>
4822 - <?php esc_html_e('Back to Action Types', 'mxchat'); ?>
4823 - </button>
4824 - <div class="mxchat-selected-action-info">
4825 - <div id="selected-action-icon" class="mxchat-action-type-icon">
4826 - <span class="dashicons dashicons-admin-generic"></span>
4827 - </div>
4828 - <div class="mxchat-selected-action-details">
4829 - <h3 id="selected-action-title"><?php esc_html_e('Selected Action', 'mxchat'); ?></h3>
4830 - <p id="selected-action-description"><?php esc_html_e('Configure this action for your chatbot', 'mxchat'); ?></p>
4831 - </div>
4832 - </div>
4833 - </div>
4834 1339
4835 - <div class="mxchat-form-group">
4836 - <label for="intent_label">
4837 - <?php esc_html_e('Action Label (For your reference only)', 'mxchat'); ?>
4838 - </label>
4839 - <input name="intent_label" type="text" id="intent_label" required
4840 - class="mxchat-intent-input"
4841 - placeholder="<?php esc_attr_e('Example: Newsletter Signup', 'mxchat'); ?>">
4842 - </div>
4843 -
4844 - <div class="mxchat-form-group">
4845 - <label for="phrases">
4846 - <?php esc_html_e('Trigger Phrases (comma-separated)', 'mxchat'); ?>
4847 - </label>
4848 - <textarea name="phrases" id="action_phrases" rows="5" required
4849 - class="mxchat-intent-textarea"
4850 - placeholder="<?php esc_attr_e('Example: sign me up, subscribe me, I want to join, add me to the newsletter', 'mxchat'); ?>"></textarea>
4851 - </div>
4852 -
4853 - <div class="mxchat-form-group">
4854 - <label for="similarity_threshold">
4855 - <?php esc_html_e('Similarity Threshold', 'mxchat'); ?>
4856 - <span class="mxchat-threshold-value-display">85%</span>
4857 - </label>
4858 - <div class="mxchat-slider-group modal-slider">
4859 - <input type="range"
4860 - name="similarity_threshold"
4861 - id="similarity_threshold"
4862 - min="10"
4863 - max="95"
4864 - value="85"
4865 - class="mxchat-intent-slider"
4866 - oninput="document.querySelector('.mxchat-threshold-value-display').textContent = this.value + '%'">
4867 - </div>
4868 - <div class="mxchat-threshold-hint">
4869 - <?php esc_html_e('Lower values (10-30) make the action trigger more easily. Higher values (70-95) require more exact matches.', 'mxchat'); ?>
4870 - </div>
4871 - </div>
4872 -
4873 - <!-- Bot Selection Section -->
4874 - <div class="mxchat-form-group">
4875 - <label for="enabled_bots">
4876 - <?php esc_html_e('Which bots should we enable this action for?', 'mxchat'); ?>
4877 - </label>
4878 - <div class="mxchat-bot-selector">
4879 - <div class="mxchat-bot-option">
4880 - <label class="mxchat-checkbox-label">
4881 - <input type="checkbox"
4882 - name="enabled_bots[]"
4883 - value="default"
4884 - id="bot_default"
4885 - checked="checked">
4886 - <span class="mxchat-checkmark"></span>
4887 - <?php esc_html_e('Default Bot', 'mxchat'); ?>
4888 - </label>
4889 - </div>
4890 -
4891 - <?php if (class_exists('MxChat_Multi_Bot_Core_Manager')) :
4892 - $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance();
4893 - $available_bots = $multi_bot_manager->get_available_bots();
4894 -
4895 - foreach ($available_bots as $bot_id => $bot_name) :
4896 - if ($bot_id === 'default') continue; // Skip default, already shown above
4897 - ?>
4898 - <div class="mxchat-bot-option">
4899 - <label class="mxchat-checkbox-label">
4900 - <input type="checkbox"
4901 - name="enabled_bots[]"
4902 - value="<?php echo esc_attr($bot_id); ?>"
4903 - id="bot_<?php echo esc_attr($bot_id); ?>">
4904 - <span class="mxchat-checkmark"></span>
4905 - <?php echo esc_html($bot_name); ?>
4906 - </label>
4907 - </div>
4908 - <?php
4909 - endforeach;
4910 - endif; ?>
4911 - </div>
4912 - </div>
4913 -
4914 - <div class="mxchat-modal-actions">
4915 - <button type="button" class="mxchat-button-secondary mxchat-modal-cancel">
4916 - <?php esc_html_e('Cancel', 'mxchat'); ?>
4917 - </button>
4918 - <button type="submit" class="mxchat-button-primary" id="mxchat-save-action-btn">
4919 - <?php esc_html_e('Save Action', 'mxchat'); ?>
4920 - </button>
4921 - </div>
1340 + <div id="mxchat-edit-modal" class="mxchat-modal" style="display: none;">
1341 + <div class="mxchat-modal-content">
1342 + <span class="mxchat-modal-close">&times;</span>
1343 + <h2><?php esc_html_e('Edit Intent Phrases', 'mxchat'); ?></h2>
1344 + <form id="mxchat-edit-intent-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
1345 + <?php wp_nonce_field('mxchat_edit_intent_nonce'); ?>
1346 + <input type="hidden" name="action" value="mxchat_edit_intent">
1347 + <input type="hidden" name="intent_id" id="edit_intent_id">
1348 + <div class="mxchat-form-group">
1349 + <label for="edit_phrases"><?php esc_html_e('Phrases (comma-separated)', 'mxchat'); ?></label>
1350 + <textarea name="phrases" id="edit_phrases" rows="5" class="large-text" required></textarea>
4922 1351 </div>
1352 + <button type="submit" class="button button-primary">
1353 + <?php esc_html_e('Update Phrases', 'mxchat'); ?>
1354 + </button>
4923 1355 </form>
4924 1356 </div>
4925 1357 </div>
4926 1358
4927 -<div id="mxchat-action-loading" class="mxchat-action-loading" style="display: none;">
4928 - <div class="mxchat-action-loading-spinner"></div>
4929 - <div class="mxchat-action-loading-text">
4930 - <?php esc_html_e('Saving action, please wait...', 'mxchat'); ?>
4931 - </div>
4932 -</div>
4933 - </div><!-- .mxchat-wrapper -->
4934 - <?php
4935 -}
4936 -private function get_trimmed_phrases($phrases, $max_length = 100) {
4937 - if (strlen($phrases) <= $max_length) {
4938 - return $phrases;
4939 - }
4940 1359
4941 - $trimmed = substr($phrases, 0, $max_length);
4942 - $last_comma = strrpos($trimmed, ',');
4943 1360
4944 - if ($last_comma !== false) {
4945 - $trimmed = substr($trimmed, 0, $last_comma);
4946 - }
4947 1361
4948 - return $trimmed . '...';
1362 + </div>
1363 + <?php
4949 1364 }
4950 -public function mxchat_add_enabled_column_to_intents() {
4951 - global $wpdb;
4952 - $table_name = $wpdb->prefix . 'mxchat_intents';
4953 1365
4954 - // Check if the column already exists
4955 - $columns = $wpdb->get_results("SHOW COLUMNS FROM $table_name LIKE 'enabled'");
4956 1366
4957 - if (empty($columns)) {
4958 - // Add the column with default value of 1 (enabled)
4959 - $wpdb->query("ALTER TABLE $table_name ADD COLUMN enabled TINYINT(1) NOT NULL DEFAULT 1");
4960 - }
4961 -}
4962 1367
4963 -public function mxchat_handle_delete_intent() {
4964 - if ( ! current_user_can( 'manage_options' ) ) {
4965 - wp_die( esc_html__('Unauthorized user', 'mxchat') );
1368 +/**
1369 + * Handle editing of intent phrases
1370 + *
1371 + * @since 1.0.0
1372 + * @return void
1373 + */
1374 +public function handle_edit_intent() {
1375 + // Verify nonce and user capabilities
1376 + if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'mxchat_edit_intent_nonce')) {
1377 + wp_die(esc_html__('Security check failed.', 'mxchat'));
4966 1378 }
4967 1379
4968 - check_admin_referer('mxchat_delete_intent_nonce');
4969 -
4970 - if (isset($_POST['intent_id'])) {
4971 - global $wpdb;
4972 - $table_name = $wpdb->prefix . 'mxchat_intents';
4973 - $intent_id = intval($_POST['intent_id']);
4974 -
4975 - $wpdb->delete($table_name, ['id' => $intent_id], ['%d']);
4976 - }
4977 -
4978 - wp_safe_redirect(admin_url('admin.php?page=mxchat-actions'));
4979 - exit;
4980 -}
4981 -public function mxchat_handle_edit_intent() {
4982 - // Security checks (nonce and permissions)
4983 1380 if (!current_user_can('manage_options')) {
4984 - wp_die(esc_html__('Unauthorized user', 'mxchat'));
1381 + wp_die(esc_html__('You do not have permission to perform this action.', 'mxchat'));
4985 1382 }
4986 - check_admin_referer('mxchat_edit_intent');
4987 1383
4988 - // Get POST data
1384 + // Validate and sanitize input
4989 1385 $intent_id = isset($_POST['intent_id']) ? absint($_POST['intent_id']) : 0;
4990 - $intent_label = isset($_POST['intent_label']) ? sanitize_text_field($_POST['intent_label']) : '';
4991 - $phrases_input = isset($_POST['phrases']) ? sanitize_textarea_field($_POST['phrases']) : '';
4992 - $threshold_percentage = isset($_POST['similarity_threshold']) ? intval($_POST['similarity_threshold']) : 85;
4993 - $similarity_threshold = min(95, max(10, $threshold_percentage)) / 100; // Convert to 0.10–0.95
4994 -
4995 - // Handle enabled_bots
4996 - $enabled_bots = isset($_POST['enabled_bots']) ? $_POST['enabled_bots'] : array('default');
4997 - $enabled_bots = array_map('sanitize_text_field', $enabled_bots);
4998 -
4999 - // Ensure default is always included for backward compatibility
5000 - if (!in_array('default', $enabled_bots)) {
5001 - $enabled_bots[] = 'default';
1386 + if (!$intent_id) {
1387 + wp_die(esc_html__('Invalid intent ID.', 'mxchat'));
5002 1388 }
5003 -
5004 - $enabled_bots_json = json_encode($enabled_bots);
5005 1389
5006 - // Validate inputs
5007 - if (!$intent_id || empty($intent_label) || empty($phrases_input)) {
5008 - $this->handle_embedding_error(__('Invalid input. Please ensure all fields are filled out.', 'mxchat'));
5009 - return;
1390 + $phrases_input = isset($_POST['phrases']) ? sanitize_textarea_field($_POST['phrases']) : '';
1391 + if (empty($phrases_input)) {
1392 + wp_die(esc_html__('Phrases cannot be empty.', 'mxchat'));
5010 1393 }
5011 1394
1395 + // Process phrases the same way as in intent creation
5012 1396 $phrases_array = array_map('sanitize_text_field', array_filter(array_map('trim', explode(',', $phrases_input))));
1397 +
5013 1398 if (empty($phrases_array)) {
5014 - $this->handle_embedding_error(__('Please enter at least one valid phrase.', 'mxchat'));
5015 - return;
1399 + wp_die(esc_html__('Please enter at least one valid phrase.', 'mxchat'));
5016 1400 }
5017 1401
5018 - // Generate embeddings with improved error handling
1402 + // Generate embeddings and combine them
5019 1403 $vectors = [];
5020 - $failed_phrases = [];
5021 -
5022 1404 foreach ($phrases_array as $phrase) {
5023 1405 $embedding_vector = $this->mxchat_generate_embedding($phrase, $this->options['api_key']);
5024 1406 if (is_array($embedding_vector)) {
5025 1407 $vectors[] = $embedding_vector;
5026 1408 } else {
5027 - $failed_phrases[] = $phrase;
1409 + wp_die(esc_html__('Error generating embedding for phrase: ', 'mxchat') . esc_html($phrase));
5028 1410 }
5029 1411 }
5030 1412
5031 - if (!empty($failed_phrases)) {
5032 - $this->handle_embedding_error(
5033 - sprintf(
5034 - __('Error generating embeddings for phrases: %s. Check your embedding API.', 'mxchat'),
5035 - implode(', ', $failed_phrases)
5036 - )
5037 - );
5038 - return;
5039 - }
5040 -
5041 1413 if (empty($vectors)) {
5042 - $this->handle_embedding_error(__('No valid embeddings generated. Please check your phrases.', 'mxchat'));
5043 - return;
1414 + wp_die(esc_html__('No valid embeddings generated. Please check your phrases.', 'mxchat'));
5044 1415 }
5045 1416
1417 + // Create combined vector just like in intent creation
5046 1418 $combined_vector = $this->mxchat_average_vectors($vectors);
5047 1419 $serialized_vector = maybe_serialize($combined_vector);
5048 1420
5049 - // Update the database
5050 1421 global $wpdb;
5051 1422 $table_name = $wpdb->prefix . 'mxchat_intents';
5052 1423
1424 + // Update the intent with both new phrases and the combined vector
5053 1425 $result = $wpdb->update(
5054 1426 $table_name,
5055 1427 array(
5056 - 'intent_label' => $intent_label,
5057 1428 'phrases' => implode(', ', $phrases_array),
5058 - 'embedding_vector' => $serialized_vector,
5059 - 'similarity_threshold' => $similarity_threshold,
5060 - 'enabled_bots' => $enabled_bots_json, // Include enabled_bots in update
1429 + 'embedding_vector' => $serialized_vector
5061 1430 ),
5062 1431 array('id' => $intent_id),
5063 - array('%s', '%s', '%s', '%f', '%s'), // Format: string, string, string, float, string
5064 - array('%d') // Where format: integer
1432 + array('%s', '%s'),
1433 + array('%d')
5065 1434 );
5066 1435
5067 1436 if (false === $result) {
5068 - $this->handle_embedding_error(__('Failed to update action in database.', 'mxchat'));
5069 - return;
1437 + wp_die(esc_html__('Failed to update intent.', 'mxchat'));
5070 1438 }
5071 1439
5072 - // Set success message and redirect
5073 - set_transient('mxchat_admin_notice_success', __('Intent updated successfully!', 'mxchat'), 60);
5074 -
1440 + // Redirect back to the intents page with a success message
5075 1441 $redirect_url = add_query_arg(
5076 1442 array(
5077 - 'page' => 'mxchat-actions'
1443 + 'page' => 'mxchat-intents',
1444 + 'updated' => 'true'
5078 1445 ),
5079 1446 admin_url('admin.php')
5080 1447 );
1448 +
5081 1449 wp_safe_redirect($redirect_url);
5082 1450 exit;
5083 1451 }
5084 -public function mxchat_handle_add_intent() {
5085 - if (!current_user_can('manage_options')) {
5086 - wp_die(esc_html__('Unauthorized user', 'mxchat'));
1452 +public function mxchat_handle_update_intent_threshold() {
1453 + if ( ! current_user_can( 'manage_options' ) ) {
1454 + wp_die( esc_html__('Unauthorized user', 'mxchat') );
5087 1455 }
5088 - check_admin_referer('mxchat_add_intent_nonce');
5089 - global $wpdb;
5090 - $table_name = $wpdb->prefix . 'mxchat_intents';
5091 -
5092 - // Sanitize and get form data
5093 - $intent_label = isset($_POST['intent_label']) ? sanitize_text_field($_POST['intent_label']) : '';
5094 - $phrases_input = isset($_POST['phrases']) ? sanitize_textarea_field($_POST['phrases']) : '';
5095 - $callback_function = isset($_POST['callback_function']) ? sanitize_text_field($_POST['callback_function']) : '';
5096 -
5097 - // Get similarity threshold from form (convert percentage to decimal)
5098 - $similarity_threshold = isset($_POST['similarity_threshold']) ? floatval($_POST['similarity_threshold']) / 100 : 0.85;
5099 -
5100 - // Handle enabled_bots
5101 - $enabled_bots = isset($_POST['enabled_bots']) ? $_POST['enabled_bots'] : array('default');
5102 - $enabled_bots = array_map('sanitize_text_field', $enabled_bots);
5103 -
5104 - // Ensure default is always included for backward compatibility with existing actions
5105 - if (!in_array('default', $enabled_bots)) {
5106 - $enabled_bots[] = 'default';
5107 - }
5108 -
5109 - $enabled_bots_json = json_encode($enabled_bots);
5110 -
5111 - // Validate required fields
5112 - if (empty($intent_label) || empty($callback_function) || empty($phrases_input)) {
5113 - $this->handle_embedding_error(__('Invalid input. Please ensure all fields are filled out.', 'mxchat'));
5114 - return;
5115 - }
5116 -
5117 - // Validate callback function
5118 - $available_callbacks = $this->mxchat_get_available_callbacks();
5119 - if (!array_key_exists($callback_function, $available_callbacks)) {
5120 - $this->handle_embedding_error(__('Invalid callback function selected.', 'mxchat'));
5121 - return;
5122 - }
5123 -
5124 - // Check if this is an add-on promotional placeholder (not a real action)
5125 - if (!empty($available_callbacks[$callback_function]['addon_promo'])) {
5126 - $addon_name = isset($available_callbacks[$callback_function]['addon_name']) ? $available_callbacks[$callback_function]['addon_name'] : __('an add-on', 'mxchat');
5127 - $this->handle_embedding_error(sprintf(
5128 - __('This action requires the %s to be installed and activated.', 'mxchat'),
5129 - $addon_name
5130 - ));
5131 - return;
5132 - }
5133 -
5134 - // Process phrases
5135 - $phrases_array = array_map('sanitize_text_field', array_filter(array_map('trim', explode(',', $phrases_input))));
5136 - if (empty($phrases_array)) {
5137 - $this->handle_embedding_error(__('Please enter at least one valid phrase.', 'mxchat'));
5138 - return;
5139 - }
5140 -
5141 - // Generate embeddings with improved error handling
5142 - $vectors = [];
5143 - $failed_phrases = [];
5144 - foreach ($phrases_array as $phrase) {
5145 - $embedding_vector = $this->mxchat_generate_embedding($phrase, $this->options['api_key']);
5146 - if (is_array($embedding_vector)) {
5147 - $vectors[] = $embedding_vector;
5148 - } else {
5149 - $failed_phrases[] = $phrase;
5150 - }
5151 - }
5152 -
5153 - // Check for embedding failures
5154 - if (!empty($failed_phrases)) {
5155 - $this->handle_embedding_error(
5156 - sprintf(
5157 - __('Error generating embeddings for phrases: %s. Check your embedding API.', 'mxchat'),
5158 - implode(', ', $failed_phrases)
5159 - )
5160 - );
5161 - return;
5162 - }
5163 -
5164 - if (empty($vectors)) {
5165 - $this->handle_embedding_error(__('No valid embeddings generated. Please check your phrases.', 'mxchat'));
5166 - return;
5167 - }
5168 -
5169 - // Create combined vector and insert into database
5170 - $combined_vector = $this->mxchat_average_vectors($vectors);
5171 - $serialized_vector = maybe_serialize($combined_vector);
5172 -
5173 - $result = $wpdb->insert($table_name, [
5174 - 'intent_label' => $intent_label,
5175 - 'phrases' => implode(', ', $phrases_array),
5176 - 'embedding_vector' => $serialized_vector,
5177 - 'callback_function' => $callback_function,
5178 - 'similarity_threshold' => $similarity_threshold,
5179 - 'enabled_bots' => $enabled_bots_json, // NEW field
5180 - ]);
5181 -
5182 - if ($result === false) {
5183 - $this->handle_embedding_error(__('Database error: ', 'mxchat') . $wpdb->last_error);
5184 - return;
5185 - }
5186 -
5187 - // Set success message and redirect
5188 - set_transient('mxchat_admin_notice_success', __('New intent added successfully!', 'mxchat'), 60);
5189 - wp_safe_redirect(admin_url('admin.php?page=mxchat-actions'));
5190 - exit;
5191 -}
5192 1456
1457 + check_admin_referer('mxchat_update_intent_threshold_nonce');
5193 1458
1459 + if (isset($_POST['intent_id'], $_POST['intent_threshold'])) {
1460 + global $wpdb;
1461 + $table_name = $wpdb->prefix . 'mxchat_intents';
1462 + $intent_id = intval($_POST['intent_id']);
1463 + $threshold_percentage = max(70, min(95, intval($_POST['intent_threshold'])));
1464 + $similarity_threshold = $threshold_percentage / 100;
5194 1465
5195 -
5196 -private function handle_embedding_error($message, $redirect = true) {
5197 - // Store the error message in the existing transient
5198 - set_transient('mxchat_admin_notice_error', $message, 60);
5199 -
5200 - if ($redirect) {
5201 - // Redirect back to the actions page
5202 - $redirect_url = add_query_arg(
5203 - array(
5204 - 'page' => 'mxchat-actions'
5205 - ),
5206 - admin_url('admin.php')
1466 + $wpdb->update(
1467 + $table_name,
1468 + ['similarity_threshold' => $similarity_threshold],
1469 + ['id' => $intent_id],
1470 + ['%f'],
1471 + ['%d']
5207 1472 );
5208 - wp_safe_redirect($redirect_url);
5209 - exit;
5210 1473 }
5211 -}
5212 1474
5213 -/**
5214 - * AJAX handler to fetch actions list for the new split-panel UI
5215 - */
5216 -public function mxchat_fetch_actions_list() {
5217 - check_ajax_referer('mxchat_actions_nonce', 'security');
5218 -
5219 - if (!current_user_can('manage_options')) {
5220 - wp_send_json_error(__('Unauthorized', 'mxchat'));
5221 - }
5222 -
5223 - global $wpdb;
5224 - $table_name = $wpdb->prefix . 'mxchat_intents';
5225 -
5226 - $page = isset($_POST['page']) ? max(1, intval($_POST['page'])) : 1;
5227 - $per_page = isset($_POST['per_page']) ? min(100, max(1, intval($_POST['per_page']))) : 50;
5228 - $offset = ($page - 1) * $per_page;
5229 - $search = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
5230 - $callback_filter = isset($_POST['callback_filter']) ? sanitize_text_field($_POST['callback_filter']) : '';
5231 - $sort_order = isset($_POST['sort_order']) && $_POST['sort_order'] === 'asc' ? 'ASC' : 'DESC';
5232 -
5233 - // Build WHERE clause
5234 - $where = '1=1';
5235 - $params = array();
5236 -
5237 - if ($search) {
5238 - $search_like = '%' . $wpdb->esc_like($search) . '%';
5239 - $where .= ' AND (intent_label LIKE %s OR phrases LIKE %s)';
5240 - $params[] = $search_like;
5241 - $params[] = $search_like;
5242 - }
5243 -
5244 - if ($callback_filter) {
5245 - $where .= ' AND callback_function = %s';
5246 - $params[] = $callback_filter;
5247 - }
5248 -
5249 - // Get total count
5250 - $count_query = "SELECT COUNT(*) FROM $table_name WHERE $where";
5251 - if (!empty($params)) {
5252 - $count_query = $wpdb->prepare($count_query, $params);
5253 - }
5254 - $total_actions = $wpdb->get_var($count_query);
5255 -
5256 - // Get actions
5257 - $query = "SELECT * FROM $table_name WHERE $where ORDER BY id $sort_order LIMIT %d OFFSET %d";
5258 - $all_params = array_merge($params, array($per_page, $offset));
5259 - $actions = $wpdb->get_results($wpdb->prepare($query, $all_params));
5260 -
5261 - // Get available callbacks for labels/icons
5262 - $available_callbacks = $this->mxchat_get_available_callbacks();
5263 -
5264 - // Prefetch individual phrase counts for all fetched actions
5265 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
5266 - $phrase_counts = array();
5267 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
5268 - $action_ids = wp_list_pluck($actions, 'id');
5269 - if (!empty($action_ids)) {
5270 - $id_placeholders = implode(',', array_fill(0, count($action_ids), '%d'));
5271 - $count_results = $wpdb->get_results($wpdb->prepare(
5272 - "SELECT intent_id, COUNT(*) as cnt FROM $phrases_table WHERE intent_id IN ($id_placeholders) GROUP BY intent_id",
5273 - $action_ids
5274 - ));
5275 - foreach ($count_results as $row) {
5276 - $phrase_counts[$row->intent_id] = intval($row->cnt);
5277 - }
5278 - }
5279 - }
5280 -
5281 - // Format actions for response
5282 - $formatted_actions = array();
5283 - foreach ($actions as $action) {
5284 - $callback_data = isset($available_callbacks[$action->callback_function])
5285 - ? $available_callbacks[$action->callback_function]
5286 - : array('label' => $action->callback_function, 'icon' => 'admin-generic');
5287 -
5288 - $enabled_bots = json_decode($action->enabled_bots, true);
5289 - if (!is_array($enabled_bots)) {
5290 - $enabled_bots = array('default');
5291 - }
5292 -
5293 - $formatted_actions[] = array(
5294 - 'id' => intval($action->id),
5295 - 'label' => $action->intent_label,
5296 - 'phrases' => $action->phrases,
5297 - 'callback_function' => $action->callback_function,
5298 - 'callback_label' => $callback_data['label'],
5299 - 'icon' => isset($callback_data['icon']) ? $callback_data['icon'] : 'admin-generic',
5300 - 'threshold' => round($action->similarity_threshold * 100),
5301 - 'enabled' => (bool) $action->enabled,
5302 - 'enabled_bots' => $enabled_bots,
5303 - 'has_legacy_vector' => !empty($action->embedding_vector),
5304 - 'individual_phrase_count' => isset($phrase_counts[$action->id]) ? $phrase_counts[$action->id] : 0,
5305 - );
5306 - }
5307 -
5308 - $total_pages = ceil($total_actions / $per_page);
5309 - $showing_start = $total_actions > 0 ? $offset + 1 : 0;
5310 - $showing_end = min($offset + $per_page, $total_actions);
5311 -
5312 - wp_send_json_success(array(
5313 - 'actions' => $formatted_actions,
5314 - 'page' => $page,
5315 - 'per_page' => $per_page,
5316 - 'total_actions' => intval($total_actions),
5317 - 'total_pages' => $total_pages,
5318 - 'showing_start' => $showing_start,
5319 - 'showing_end' => $showing_end,
5320 - ));
1475 + wp_safe_redirect(admin_url('admin.php?page=mxchat-intents'));
1476 + exit;
5321 1477 }
5322 1478
5323 -/**
5324 - * AJAX handler to toggle action enabled status
5325 - */
5326 -public function mxchat_toggle_action_status() {
5327 - check_ajax_referer('mxchat_actions_nonce', 'security');
5328 -
5329 - if (!current_user_can('manage_options')) {
5330 - wp_send_json_error(__('Unauthorized', 'mxchat'));
1479 +public function mxchat_handle_add_intent() {
1480 + if ( ! current_user_can( 'manage_options' ) ) {
1481 + wp_die( esc_html__('Unauthorized user', 'mxchat') );
5331 1482 }
5332 1483
5333 - $action_id = isset($_POST['action_id']) ? intval($_POST['action_id']) : 0;
5334 - $enabled = isset($_POST['enabled']) ? intval($_POST['enabled']) : 0;
1484 + check_admin_referer('mxchat_add_intent_nonce');
5335 1485
5336 - if (!$action_id) {
5337 - wp_send_json_error(__('Invalid action ID', 'mxchat'));
5338 - }
5339 -
5340 1486 global $wpdb;
5341 1487 $table_name = $wpdb->prefix . 'mxchat_intents';
5342 1488
5343 - $result = $wpdb->update(
5344 - $table_name,
5345 - array('enabled' => $enabled ? 1 : 0),
5346 - array('id' => $action_id),
5347 - array('%d'),
5348 - array('%d')
5349 - );
5350 -
5351 - if ($result === false) {
5352 - wp_send_json_error(__('Failed to update action status', 'mxchat'));
5353 - }
5354 -
5355 - wp_send_json_success(array('enabled' => (bool) $enabled));
5356 -}
5357 -
5358 -/**
5359 - * AJAX handler to bulk delete actions
5360 - */
5361 -public function mxchat_bulk_delete_actions() {
5362 - check_ajax_referer('mxchat_delete_intent_nonce', 'security');
5363 -
5364 - if (!current_user_can('manage_options')) {
5365 - wp_send_json_error(__('Unauthorized', 'mxchat'));
5366 - }
5367 -
5368 - $action_ids = isset($_POST['action_ids']) ? array_map('intval', (array) $_POST['action_ids']) : array();
5369 -
5370 - if (empty($action_ids)) {
5371 - wp_send_json_error(__('No actions selected', 'mxchat'));
5372 - }
5373 -
5374 - global $wpdb;
5375 - $table_name = $wpdb->prefix . 'mxchat_intents';
5376 -
5377 - $placeholders = implode(',', array_fill(0, count($action_ids), '%d'));
5378 - $query = $wpdb->prepare("DELETE FROM $table_name WHERE id IN ($placeholders)", $action_ids);
5379 - $result = $wpdb->query($query);
5380 -
5381 - if ($result === false) {
5382 - wp_send_json_error(__('Failed to delete actions', 'mxchat'));
5383 - }
5384 -
5385 - // Also delete individual phrases for these intents
5386 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
5387 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
5388 - $wpdb->query($wpdb->prepare("DELETE FROM $phrases_table WHERE intent_id IN ($placeholders)", $action_ids));
5389 - }
5390 -
5391 - wp_send_json_success(array('deleted' => $result));
5392 -}
5393 -
5394 -/**
5395 - * AJAX handler to add a new intent/action
5396 - */
5397 -public function mxchat_add_intent_ajax() {
5398 - check_ajax_referer('mxchat_add_intent_nonce', 'security');
5399 -
5400 - if (!current_user_can('manage_options')) {
5401 - wp_send_json_error(__('Unauthorized', 'mxchat'));
5402 - }
5403 -
5404 - global $wpdb;
5405 - $table_name = $wpdb->prefix . 'mxchat_intents';
5406 -
5407 - // Sanitize input
5408 1489 $intent_label = isset($_POST['intent_label']) ? sanitize_text_field($_POST['intent_label']) : '';
5409 1490 $phrases_input = isset($_POST['phrases']) ? sanitize_textarea_field($_POST['phrases']) : '';
5410 1491 $callback_function = isset($_POST['callback_function']) ? sanitize_text_field($_POST['callback_function']) : '';
5411 - $similarity_threshold = isset($_POST['similarity_threshold']) ? floatval($_POST['similarity_threshold']) / 100 : 0.85;
5412 - $enabled_bots = isset($_POST['enabled_bots']) ? array_map('sanitize_text_field', (array) $_POST['enabled_bots']) : array('default');
1492 + $default_threshold = 0.85;
5413 1493
5414 - // Validate
5415 - // Check if using new individual phrases mode or legacy mode
5416 - $individual_phrases = isset($_POST['individual_phrases']) ? array_filter(array_map('sanitize_text_field', (array) $_POST['individual_phrases'])) : array();
5417 - $use_individual = !empty($individual_phrases);
5418 -
5419 - // Validate required fields (phrases not required when using individual mode)
5420 - if (empty($intent_label) || empty($callback_function)) {
5421 - wp_send_json_error(__('Please fill in all required fields.', 'mxchat'));
1494 + if (empty($intent_label) || empty($callback_function) || empty($phrases_input)) {
1495 + wp_die( esc_html__('Invalid input. Please ensure all fields are filled out.', 'mxchat') );
5422 1496 }
5423 - if (!$use_individual && empty($phrases_input)) {
5424 - wp_send_json_error(__('Please fill in all required fields.', 'mxchat'));
5425 - }
5426 1497
5427 - // Ensure default bot is included
5428 - if (!in_array('default', $enabled_bots)) {
5429 - $enabled_bots[] = 'default';
5430 - }
5431 - $enabled_bots_json = json_encode($enabled_bots);
1498 + $available_callbacks = $this->mxchat_get_available_callbacks();
5432 1499
5433 - if ($use_individual) {
5434 - // New mode: individual phrases each get their own vector
5435 - // Insert the intent row with empty legacy fields
5436 - $result = $wpdb->insert(
5437 - $table_name,
5438 - array(
5439 - 'intent_label' => $intent_label,
5440 - 'phrases' => '',
5441 - 'embedding_vector' => '',
5442 - 'similarity_threshold' => $similarity_threshold,
5443 - 'callback_function' => $callback_function,
5444 - 'enabled' => 1,
5445 - 'enabled_bots' => $enabled_bots_json,
5446 - )
5447 - );
5448 -
5449 - if ($result === false) {
5450 - wp_send_json_error(__('Failed to add action to database.', 'mxchat'));
5451 - }
5452 -
5453 - $intent_id = $wpdb->insert_id;
5454 -
5455 - // Insert each phrase individually with its own embedding
5456 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
5457 - $failed_phrases = array();
5458 - foreach ($individual_phrases as $phrase) {
5459 - $phrase = trim($phrase);
5460 - if (empty($phrase)) continue;
5461 -
5462 - $embedding_vector = $this->mxchat_generate_embedding($phrase);
5463 - if (is_wp_error($embedding_vector)) {
5464 - $failed_phrases[] = $phrase;
5465 - continue;
5466 - }
5467 -
5468 - $wpdb->insert(
5469 - $phrases_table,
5470 - array(
5471 - 'intent_id' => $intent_id,
5472 - 'phrase' => $phrase,
5473 - 'embedding_vector' => maybe_serialize($embedding_vector),
5474 - )
5475 - );
5476 - }
5477 -
5478 - $response = array('id' => $intent_id);
5479 - if (!empty($failed_phrases)) {
5480 - $response['failed_phrases'] = $failed_phrases;
5481 - }
5482 - wp_send_json_success($response);
5483 -
5484 - } else {
5485 - // Legacy mode: combine all phrases into one embedding (backwards compatible)
5486 - $phrases_array = array_filter(array_map('trim', preg_split('/[\n,]+/', $phrases_input)));
5487 - if (empty($phrases_array)) {
5488 - wp_send_json_error(__('Please provide at least one trigger phrase.', 'mxchat'));
5489 - }
5490 -
5491 - // Generate embedding (combine phrases into single string for embedding)
5492 - $embedding_vector = $this->mxchat_generate_embedding(implode(' ', $phrases_array));
5493 - if (is_wp_error($embedding_vector)) {
5494 - // Fallback: store without embedding
5495 - $serialized_vector = null;
5496 - } else {
5497 - $serialized_vector = maybe_serialize($embedding_vector);
5498 - }
5499 -
5500 - // Insert
5501 - $result = $wpdb->insert(
5502 - $table_name,
5503 - array(
5504 - 'intent_label' => $intent_label,
5505 - 'phrases' => implode(', ', $phrases_array),
5506 - 'embedding_vector' => $serialized_vector,
5507 - 'similarity_threshold' => $similarity_threshold,
5508 - 'callback_function' => $callback_function,
5509 - 'enabled' => 1,
5510 - 'enabled_bots' => $enabled_bots_json,
5511 - )
5512 - );
5513 -
5514 - if ($result === false) {
5515 - wp_send_json_error(__('Failed to add action to database.', 'mxchat'));
5516 - }
5517 -
5518 - wp_send_json_success(array('id' => $wpdb->insert_id));
1500 + if (!array_key_exists($callback_function, $available_callbacks)) {
1501 + wp_die( esc_html__('Invalid callback function selected.', 'mxchat') );
5519 1502 }
5520 -}
5521 1503
5522 -/**
5523 - * AJAX handler to edit an existing intent/action
5524 - */
5525 -public function mxchat_edit_intent_ajax() {
5526 - check_ajax_referer('mxchat_edit_intent', 'security');
5527 -
5528 - if (!current_user_can('manage_options')) {
5529 - wp_send_json_error(__('Unauthorized', 'mxchat'));
1504 + $is_pro_only = $available_callbacks[$callback_function]['pro_only'];
1505 + if ($is_pro_only && !$this->is_activated) {
1506 + wp_die( esc_html__('This callback function is available in the Pro version only.', 'mxchat') );
5530 1507 }
5531 1508
5532 - global $wpdb;
5533 - $table_name = $wpdb->prefix . 'mxchat_intents';
1509 + $phrases_array = array_map('sanitize_text_field', array_filter(array_map('trim', explode(',', $phrases_input))));
5534 1510
5535 - // Sanitize input
5536 - $intent_id = isset($_POST['intent_id']) ? intval($_POST['intent_id']) : 0;
5537 - $intent_label = isset($_POST['intent_label']) ? sanitize_text_field($_POST['intent_label']) : '';
5538 - $phrases_input = isset($_POST['phrases']) ? sanitize_textarea_field($_POST['phrases']) : '';
5539 - $callback_function = isset($_POST['callback_function']) ? sanitize_text_field($_POST['callback_function']) : '';
5540 - $similarity_threshold = isset($_POST['similarity_threshold']) ? floatval($_POST['similarity_threshold']) / 100 : 0.85;
5541 - $enabled_bots = isset($_POST['enabled_bots']) ? array_map('sanitize_text_field', (array) $_POST['enabled_bots']) : array('default');
5542 -
5543 - // Validate
5544 - if (!$intent_id || empty($intent_label)) {
5545 - wp_send_json_error(__('Please fill in all required fields.', 'mxchat'));
1511 + if (empty($phrases_array)) {
1512 + wp_die( esc_html__('Please enter at least one valid phrase.', 'mxchat') );
5546 1513 }
5547 1514
5548 - // Check if phrases are managed individually (empty phrases_input means individual mode)
5549 - $uses_individual_phrases = empty($phrases_input);
5550 -
5551 - if ($uses_individual_phrases) {
5552 - // Individual phrase mode: only update non-phrase fields, skip embedding regeneration
5553 - $update_data = array(
5554 - 'intent_label' => $intent_label,
5555 - 'similarity_threshold' => $similarity_threshold,
5556 - 'callback_function' => $callback_function,
5557 - 'enabled_bots' => json_encode($enabled_bots),
5558 - );
5559 - } else {
5560 - // Legacy mode: process phrases and regenerate embedding (backwards compatible for add-ons)
5561 - $phrases_array = array_filter(array_map('trim', preg_split('/[\n,]+/', $phrases_input)));
5562 - if (empty($phrases_array)) {
5563 - wp_send_json_error(__('Please provide at least one trigger phrase.', 'mxchat'));
5564 - }
5565 -
5566 - // Generate new embedding (combine phrases into single string for embedding)
5567 - $embedding_vector = $this->mxchat_generate_embedding(implode(' ', $phrases_array));
5568 - if (is_wp_error($embedding_vector)) {
5569 - // Keep existing embedding
5570 - $update_data = array(
5571 - 'intent_label' => $intent_label,
5572 - 'phrases' => implode(', ', $phrases_array),
5573 - 'similarity_threshold' => $similarity_threshold,
5574 - 'callback_function' => $callback_function,
5575 - 'enabled_bots' => json_encode($enabled_bots),
5576 - );
1515 + $vectors = [];
1516 + foreach ($phrases_array as $phrase) {
1517 + $embedding_vector = $this->mxchat_generate_embedding($phrase, $this->options['api_key']);
1518 + if (is_array($embedding_vector)) {
1519 + $vectors[] = $embedding_vector;
5577 1520 } else {
5578 - $serialized_vector = maybe_serialize($embedding_vector);
5579 - $update_data = array(
5580 - 'intent_label' => $intent_label,
5581 - 'phrases' => implode(', ', $phrases_array),
5582 - 'embedding_vector' => $serialized_vector,
5583 - 'similarity_threshold' => $similarity_threshold,
5584 - 'callback_function' => $callback_function,
5585 - 'enabled_bots' => json_encode($enabled_bots),
5586 - );
1521 + wp_die( esc_html__('Error generating embedding for phrase: ', 'mxchat') . esc_html($phrase) );
5587 1522 }
5588 1523 }
5589 1524
5590 - // Ensure default bot is included
5591 - if (!in_array('default', $enabled_bots)) {
5592 - $enabled_bots[] = 'default';
5593 - }
5594 - $update_data['enabled_bots'] = json_encode($enabled_bots);
1525 + if (!empty($vectors)) {
1526 + $combined_vector = $this->mxchat_average_vectors($vectors);
1527 + $serialized_vector = maybe_serialize($combined_vector);
5595 1528
5596 - $result = $wpdb->update(
5597 - $table_name,
5598 - $update_data,
5599 - array('id' => $intent_id),
5600 - null,
5601 - array('%d')
5602 - );
1529 + $result = $wpdb->insert($table_name, [
1530 + 'intent_label' => $intent_label,
1531 + 'phrases' => implode(', ', $phrases_array),
1532 + 'embedding_vector' => $serialized_vector,
1533 + 'callback_function' => $callback_function,
1534 + 'similarity_threshold' => $default_threshold,
1535 + ]);
5603 1536
5604 - if ($result === false) {
5605 - wp_send_json_error(__('Failed to update action.', 'mxchat'));
1537 + if ($result === false) {
1538 + wp_die( esc_html__('Database error: ', 'mxchat') . esc_html($wpdb->last_error) );
1539 + }
1540 + } else {
1541 + wp_die( esc_html__('No valid embeddings generated. Please check your phrases.', 'mxchat') );
5606 1542 }
5607 1543
5608 - wp_send_json_success(array('updated' => true));
1544 + wp_safe_redirect(admin_url('admin.php?page=mxchat-intents'));
1545 + exit;
5609 1546 }
5610 1547
5611 -/**
5612 - * AJAX handler to delete a single intent/action
5613 - */
5614 -public function mxchat_delete_intent_ajax() {
5615 - check_ajax_referer('mxchat_delete_intent_nonce', 'security');
5616 1548
5617 - if (!current_user_can('manage_options')) {
5618 - wp_send_json_error(__('Unauthorized', 'mxchat'));
5619 - }
5620 1549
5621 - $intent_id = isset($_POST['intent_id']) ? intval($_POST['intent_id']) : 0;
5622 1550
5623 - if (!$intent_id) {
5624 - wp_send_json_error(__('Invalid action ID', 'mxchat'));
5625 - }
5626 1551
5627 - global $wpdb;
5628 - $table_name = $wpdb->prefix . 'mxchat_intents';
5629 1552
5630 - $result = $wpdb->delete($table_name, array('id' => $intent_id), array('%d'));
1553 +private function mxchat_get_available_callbacks($grouped = false) {
1554 + $callbacks = [
1555 + 'mxchat_handle_email_capture' => [
1556 + 'label' => 'Email Capture',
1557 + 'pro_only' => false,
1558 + 'group' => 'Customer Engagement',
1559 + ],
1560 + 'mxchat_handle_product_inquiry' => [
1561 + 'label' => 'Show Product Card',
1562 + 'pro_only' => true,
1563 + 'group' => 'WooCommerce Features',
1564 + ],
1565 + 'mxchat_handle_order_history' => [
1566 + 'label' => 'Order History',
1567 + 'pro_only' => true,
1568 + 'group' => 'WooCommerce Features',
1569 + ],
1570 + 'mxchat_generate_image' => [
1571 + 'label' => 'Generate Image',
1572 + 'pro_only' => true,
1573 + 'group' => 'Other Features',
1574 + ],
1575 + 'mxchat_handle_search_request' => [
1576 + 'label' => 'Brave Web Search',
1577 + 'pro_only' => false,
1578 + 'group' => 'Search Features',
1579 + ],
1580 + 'mxchat_handle_image_search_request' => [
1581 + 'label' => 'Brave Image Search',
1582 + 'pro_only' => false,
1583 + 'group' => 'Search Features',
1584 + ],
1585 + 'mxchat_handle_add_to_cart_intent' => [
1586 + 'label' => 'Add to Cart',
1587 + 'pro_only' => true,
1588 + 'group' => 'WooCommerce Features',
1589 + ],
1590 + 'mxchat_handle_checkout_intent' => [
1591 + 'label' => 'Proceed to Checkout',
1592 + 'pro_only' => true,
1593 + 'group' => 'WooCommerce Features',
1594 + ],
1595 + 'mxchat_handle_pdf_discussion' => [
1596 + 'label' => 'Chat with PDF',
1597 + 'pro_only' => true,
1598 + 'group' => 'Other Features',
1599 + ],
1600 + 'mxchat_handle_product_recommendations' => [
1601 + 'label' => 'Product Recommendations',
1602 + 'pro_only' => true,
1603 + 'group' => 'WooCommerce Features',
1604 + ],
1605 + 'mxchat_live_agent_handover' => [
1606 + 'label' => 'Live Agent',
1607 + 'pro_only' => true,
1608 + 'group' => 'Customer Engagement',
1609 + ],
1610 + 'mxchat_handle_switch_to_chatbot_intent' => [
1611 + 'label' => 'Back to Chatbot',
1612 + 'pro_only' => true,
1613 + 'group' => 'Customer Engagement',
1614 + ],
1615 + ];
5631 1616
5632 - if ($result === false) {
5633 - wp_send_json_error(__('Failed to delete action', 'mxchat'));
1617 + // Return grouped structure if requested
1618 + if ($grouped) {
1619 + $grouped_callbacks = [];
1620 + foreach ($callbacks as $key => $data) {
1621 + $group_label = $data['group'] ?? 'Other Features';
1622 + $grouped_callbacks[$group_label][$key] = [
1623 + 'label' => $data['label'],
1624 + 'pro_only' => $data['pro_only'],
1625 + ];
1626 + }
1627 + return $grouped_callbacks;
5634 1628 }
5635 1629
5636 - // Also delete individual phrases for this intent
5637 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
5638 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
5639 - $wpdb->delete($phrases_table, array('intent_id' => $intent_id), array('%d'));
5640 - }
5641 -
5642 - wp_send_json_success(array('deleted' => true));
1630 + return $callbacks;
5643 1631 }
5644 1632
5645 -/**
5646 - * AJAX handler to add a single phrase with its own embedding to an intent
5647 - */
5648 -public function mxchat_add_phrase_ajax() {
5649 - check_ajax_referer('mxchat_add_phrase_nonce', 'security');
5650 1633
5651 - if (!current_user_can('manage_options')) {
5652 - wp_send_json_error(__('Unauthorized', 'mxchat'));
5653 - }
5654 1634
5655 - $intent_id = isset($_POST['intent_id']) ? intval($_POST['intent_id']) : 0;
5656 - $phrase = isset($_POST['phrase']) ? sanitize_text_field($_POST['phrase']) : '';
1635 +private function mxchat_average_vectors($vectors) {
1636 + $vector_length = count($vectors[0]);
1637 + $sum_vector = array_fill(0, $vector_length, 0);
5657 1638
5658 - if (!$intent_id || empty($phrase)) {
5659 - wp_send_json_error(__('Please provide an intent ID and phrase.', 'mxchat'));
1639 + foreach ($vectors as $vector) {
1640 + for ($i = 0; $i < $vector_length; $i++) {
1641 + $sum_vector[$i] += $vector[$i];
1642 + }
5660 1643 }
5661 1644
5662 - // Verify the intent exists
5663 - global $wpdb;
5664 - $intents_table = $wpdb->prefix . 'mxchat_intents';
5665 - $intent = $wpdb->get_row($wpdb->prepare("SELECT id FROM $intents_table WHERE id = %d", $intent_id));
5666 - if (!$intent) {
5667 - wp_send_json_error(__('Action not found.', 'mxchat'));
1645 + // Divide each component by the number of vectors to get the average
1646 + $num_vectors = count($vectors);
1647 + for ($i = 0; $i < $vector_length; $i++) {
1648 + $sum_vector[$i] /= $num_vectors;
5668 1649 }
5669 1650
5670 - // Generate embedding for this single phrase
5671 - $embedding_vector = $this->mxchat_generate_embedding($phrase);
5672 - if (is_wp_error($embedding_vector)) {
5673 - wp_send_json_error(__('Failed to generate embedding: ', 'mxchat') . $embedding_vector->get_error_message());
5674 - }
5675 -
5676 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
5677 - $result = $wpdb->insert(
5678 - $phrases_table,
5679 - array(
5680 - 'intent_id' => $intent_id,
5681 - 'phrase' => $phrase,
5682 - 'embedding_vector' => maybe_serialize($embedding_vector),
5683 - )
5684 - );
5685 -
5686 - if ($result === false) {
5687 - wp_send_json_error(__('Failed to add phrase.', 'mxchat'));
5688 - }
5689 -
5690 - wp_send_json_success(array('id' => $wpdb->insert_id, 'phrase' => $phrase));
1651 + return $sum_vector;
5691 1652 }
5692 1653
5693 -/**
5694 - * AJAX handler to delete a single phrase from wp_mxchat_intent_phrases
5695 - */
5696 -public function mxchat_delete_phrase_ajax() {
5697 - check_ajax_referer('mxchat_delete_phrase_nonce', 'security');
5698 -
5699 - if (!current_user_can('manage_options')) {
5700 - wp_send_json_error(__('Unauthorized', 'mxchat'));
1654 +public function mxchat_handle_delete_intent() {
1655 + if ( ! current_user_can( 'manage_options' ) ) {
1656 + wp_die( esc_html__('Unauthorized user', 'mxchat') );
5701 1657 }
5702 1658
5703 - $phrase_id = isset($_POST['phrase_id']) ? intval($_POST['phrase_id']) : 0;
5704 - if (!$phrase_id) {
5705 - wp_send_json_error(__('Invalid phrase ID.', 'mxchat'));
5706 - }
1659 + check_admin_referer('mxchat_delete_intent_nonce');
5707 1660
5708 - global $wpdb;
5709 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
5710 - $result = $wpdb->delete($phrases_table, array('id' => $phrase_id), array('%d'));
1661 + if (isset($_POST['intent_id'])) {
1662 + global $wpdb;
1663 + $table_name = $wpdb->prefix . 'mxchat_intents';
1664 + $intent_id = intval($_POST['intent_id']);
5711 1665
5712 - if ($result === false) {
5713 - wp_send_json_error(__('Failed to delete phrase.', 'mxchat'));
1666 + $wpdb->delete($table_name, ['id' => $intent_id], ['%d']);
5714 1667 }
5715 1668
5716 - wp_send_json_success(array('deleted' => true));
1669 + wp_safe_redirect(admin_url('admin.php?page=mxchat-intents'));
1670 + exit;
5717 1671 }
5718 1672
5719 -/**
5720 - * AJAX handler to fetch individual phrases for an intent
5721 - */
5722 -public function mxchat_get_phrases_ajax() {
5723 - check_ajax_referer('mxchat_get_phrases_nonce', 'security');
5724 1673
5725 - if (!current_user_can('manage_options')) {
5726 - wp_send_json_error(__('Unauthorized', 'mxchat'));
5727 - }
5728 -
5729 - $intent_id = isset($_POST['intent_id']) ? intval($_POST['intent_id']) : 0;
5730 - if (!$intent_id) {
5731 - wp_send_json_error(__('Invalid intent ID.', 'mxchat'));
5732 - }
5733 -
5734 - global $wpdb;
5735 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
5736 -
5737 - $phrases = array();
5738 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
5739 - $phrases = $wpdb->get_results($wpdb->prepare(
5740 - "SELECT id, phrase, created_at FROM $phrases_table WHERE intent_id = %d ORDER BY created_at ASC",
5741 - $intent_id
5742 - ));
5743 - }
5744 -
5745 - wp_send_json_success(array('phrases' => $phrases));
5746 -}
5747 -
5748 -/**
5749 - * AJAX handler to clear legacy phrases and embedding from the main intents table
5750 - */
5751 -public function mxchat_delete_legacy_phrases_ajax() {
5752 - check_ajax_referer('mxchat_delete_legacy_nonce', 'security');
5753 -
5754 - if (!current_user_can('manage_options')) {
5755 - wp_send_json_error(__('Unauthorized', 'mxchat'));
5756 - }
5757 -
5758 - $intent_id = isset($_POST['intent_id']) ? intval($_POST['intent_id']) : 0;
5759 - if (!$intent_id) {
5760 - wp_send_json_error(__('Invalid intent ID.', 'mxchat'));
5761 - }
5762 -
5763 - global $wpdb;
5764 - $table_name = $wpdb->prefix . 'mxchat_intents';
5765 -
5766 - $result = $wpdb->update(
5767 - $table_name,
5768 - array('phrases' => '', 'embedding_vector' => ''),
5769 - array('id' => $intent_id),
5770 - array('%s', '%s'),
5771 - array('%d')
5772 - );
5773 -
5774 - if ($result === false) {
5775 - wp_send_json_error(__('Failed to clear legacy phrases.', 'mxchat'));
5776 - }
5777 -
5778 - wp_send_json_success(array('cleared' => true));
5779 -}
5780 -
5781 -/**
5782 - * Enhanced get_available_callbacks function with form action exclusion
5783 - *
5784 - * @param bool $grouped Whether to return callbacks grouped by category
5785 - * @param bool $include_all Whether to include all potential actions (even if add-on not installed)
5786 - * @return array Callbacks data with icons, descriptions and availability status
5787 - */
5788 -private function mxchat_get_available_callbacks($grouped = false, $include_all = true) {
5789 - // Load WordPress plugin functions if needed
5790 - if (!function_exists('get_plugins')) {
5791 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
5792 - }
5793 -
5794 - // Get active plugins
5795 - $active_plugins = get_option('active_plugins', array());
5796 -
5797 - // Functions to exclude from the action selector only if Pro is activated
5798 - // If user doesn't have Pro, show these so they can see what they're missing
5799 - $excluded_when_pro_active_functions = array(
5800 - 'mxchat_handle_form_collection' // Forms add-on action
5801 - );
5802 -
5803 - // Always excluded functions (regardless of Pro status)
5804 - $always_excluded_functions = array();
5805 -
5806 - // Combine exclusion lists based on Pro activation status
5807 - $excluded_functions = $always_excluded_functions;
5808 - if ($this->is_activated) {
5809 - // Only exclude add-on managed functions if Pro is active
5810 - $excluded_functions = array_merge($excluded_functions, $excluded_when_pro_active_functions);
5811 - }
5812 -
5813 - // Define add-on plugin files and their corresponding action functions
5814 - $addon_plugins = array(
5815 - 'mxchat-woo/mxchat-woo.php' => array(
5816 - 'functions' => array(
5817 - 'mxchat_handle_product_recommendations',
5818 - 'mxchat_handle_order_history',
5819 - 'mxchat_show_product_card',
5820 - 'mxchat_add_to_cart',
5821 - 'mxchat_checkout_redirect',
5822 - 'mxchat_handle_featured_products'
5823 - ),
5824 - 'name' => __('WooCommerce Add-on', 'mxchat'),
5825 - 'pro_required' => true
5826 - ),
5827 - 'mxchat-perplexity/mxchat-perplexity.php' => array(
5828 - 'functions' => array('mxchat_perplexity_research'),
5829 - 'name' => __('Perplexity Add-on', 'mxchat'),
5830 - 'pro_required' => true
5831 - ),
5832 - 'mxchat-forms/mxchat-forms.php' => array(
5833 - 'functions' => array('mxchat_handle_form_collection'),
5834 - 'name' => __('Forms Add-on', 'mxchat'),
5835 - 'pro_required' => true
5836 - ),
5837 - // Add other add-ons and their functions here
5838 - );
5839 -
5840 - // Get the functions that are provided by active add-ons
5841 - $addon_provided_functions = array();
5842 - $addon_function_mapping = array(); // Maps functions to their add-on info
5843 -
5844 - // Check which add-ons are active
5845 - foreach ($addon_plugins as $plugin_file => $addon_info) {
5846 - $is_active = in_array($plugin_file, $active_plugins);
5847 -
5848 - // For each function in this addon
5849 - foreach ($addon_info['functions'] as $function) {
5850 - // Consider a function installed only if:
5851 - // 1. The add-on is active AND
5852 - // 2. Either it doesn't require Pro OR Pro is activated
5853 - $is_installed = $is_active && (!$addon_info['pro_required'] || $this->is_activated);
5854 -
5855 - // If the add-on is installed, mark this function as provided by an add-on
5856 - if ($is_installed) {
5857 - $addon_provided_functions[] = $function;
5858 - }
5859 -
5860 - // Store addon info for this function regardless of installation status
5861 - $addon_function_mapping[$function] = array(
5862 - 'addon' => basename(dirname($plugin_file)),
5863 - 'addon_name' => $addon_info['name'],
5864 - 'pro_required' => $addon_info['pro_required'],
5865 - 'is_active' => $is_active,
5866 - 'is_installed' => $is_installed
5867 - );
5868 - }
5869 - }
5870 -
5871 - // Core callbacks - always available in the base plugin
5872 - $core_callbacks = array(
5873 - 'mxchat_handle_email_capture' => array(
5874 - 'label' => __('Loops Email Capture', 'mxchat'),
5875 - 'pro_only' => false,
5876 - 'group' => __('Customer Engagement', 'mxchat'),
5877 - 'icon' => 'email-alt',
5878 - 'description' => __('Collect visitor emails for your mailing list in Loops', 'mxchat'),
5879 - 'addon' => false, // Not from an add-on
5880 - 'installed' => true // Always installed with base plugin
5881 - ),
5882 - 'mxchat_handle_search_request' => array(
5883 - 'label' => __('Brave Web Search', 'mxchat'),
5884 - 'pro_only' => false,
5885 - 'group' => __('Search Features', 'mxchat'),
5886 - 'icon' => 'search',
5887 - 'description' => __('Let users search the web directly from the chat (requires a Brave Search API key)', 'mxchat'),
5888 - 'addon' => false,
5889 - 'installed' => true
5890 - ),
5891 - 'mxchat_handle_image_search_request' => array(
5892 - 'label' => __('Brave Image Search', 'mxchat'),
5893 - 'pro_only' => false,
5894 - 'group' => __('Search Features', 'mxchat'),
5895 - 'icon' => 'format-image',
5896 - 'description' => __('Search and display images in the chat conversation (requires a Brave Search API key)', 'mxchat'),
5897 - 'addon' => false,
5898 - 'installed' => true
5899 - ),
5900 - // Pro core features - check is_activated property
5901 - 'mxchat_generate_image' => array(
5902 - 'label' => __('Generate Image (OpenAI)', 'mxchat'),
5903 - 'pro_only' => false,
5904 - 'group' => __('Other Features', 'mxchat'),
5905 - 'icon' => 'art',
5906 - 'description' => __('Create images with GPT Image from OpenAI (requires OpenAI API key)', 'mxchat'),
5907 - 'addon' => false,
5908 - 'installed' => true
5909 - ),
5910 - 'mxchat_generate_gemini_image' => array(
5911 - 'label' => __('Generate Image (Gemini)', 'mxchat'),
5912 - 'pro_only' => false,
5913 - 'group' => __('Other Features', 'mxchat'),
5914 - 'icon' => 'art',
5915 - 'description' => __('Create images with Imagen from Google (requires Gemini API key)', 'mxchat'),
5916 - 'addon' => false,
5917 - 'installed' => true
5918 - ),
5919 - 'mxchat_handle_pdf_discussion' => array(
5920 - 'label' => __('Chat with PDF', 'mxchat'),
5921 - 'pro_only' => false,
5922 - 'group' => __('Other Features', 'mxchat'),
5923 - 'icon' => 'media-document',
5924 - 'description' => __('Answer questions about uploaded PDF documents', 'mxchat'),
5925 - 'addon' => false,
5926 - 'installed' => true
5927 - ),
5928 - 'mxchat_live_agent_handover' => array(
5929 - 'label' => __('Slack Live Agent', 'mxchat'),
5930 - 'pro_only' => false,
5931 - 'group' => __('Customer Engagement', 'mxchat'),
5932 - 'icon' => 'admin-users',
5933 - 'description' => __('Transfer conversation to a human support agent on Slack', 'mxchat'),
5934 - 'addon' => false,
5935 - 'installed' => true
5936 - ),
5937 - 'mxchat_telegram_live_agent_handover' => array(
5938 - 'label' => __('Telegram Live Agent', 'mxchat'),
5939 - 'pro_only' => false,
5940 - 'group' => __('Customer Engagement', 'mxchat'),
5941 - 'icon' => 'format-chat',
5942 - 'description' => __('Transfer conversation to a human support agent on Telegram', 'mxchat'),
5943 - 'addon' => false,
5944 - 'installed' => true
5945 - ),
5946 - 'mxchat_handle_switch_to_chatbot_intent' => array(
5947 - 'label' => __('Back to Chatbot', 'mxchat'),
5948 - 'pro_only' => false,
5949 - 'group' => __('Customer Engagement', 'mxchat'),
5950 - 'icon' => 'backup',
5951 - 'description' => __('Return from live agent mode to AI chatbot', 'mxchat'),
5952 - 'addon' => false,
5953 - 'installed' => true
5954 - ),
5955 - );
5956 -
5957 - // Add-on callbacks with placeholders - only include if the add-on is NOT active
5958 - // These are promotional/informational only — not selectable as real actions
5959 - $addon_callbacks = array(
5960 - // WooCommerce Add-on
5961 - 'mxchat_handle_product_recommendations' => array(
5962 - 'label' => __('Product Recommendations', 'mxchat'),
5963 - 'pro_only' => false,
5964 - 'addon_promo' => true,
5965 - 'group' => __('WooCommerce Features', 'mxchat'),
5966 - 'icon' => 'cart',
5967 - 'description' => __('Suggest products based on customer preferences', 'mxchat'),
5968 - ),
5969 - 'mxchat_handle_order_history' => array(
5970 - 'label' => __('Order History', 'mxchat'),
5971 - 'pro_only' => false,
5972 - 'addon_promo' => true,
5973 - 'group' => __('WooCommerce Features', 'mxchat'),
5974 - 'icon' => 'clipboard',
5975 - 'description' => __('Allow customers to check their order status', 'mxchat'),
5976 - ),
5977 - 'mxchat_show_product_card' => array(
5978 - 'label' => __('Show Product Card', 'mxchat'),
5979 - 'pro_only' => false,
5980 - 'addon_promo' => true,
5981 - 'group' => __('WooCommerce Features', 'mxchat'),
5982 - 'icon' => 'products',
5983 - 'description' => __('Display product information in the chat', 'mxchat'),
5984 - ),
5985 - 'mxchat_add_to_cart' => array(
5986 - 'label' => __('Add to Cart', 'mxchat'),
5987 - 'pro_only' => false,
5988 - 'addon_promo' => true,
5989 - 'group' => __('WooCommerce Features', 'mxchat'),
5990 - 'icon' => 'plus-alt',
5991 - 'description' => __('Add products to cart directly from chat', 'mxchat'),
5992 - ),
5993 - 'mxchat_checkout_redirect' => array(
5994 - 'label' => __('Proceed to Checkout', 'mxchat'),
5995 - 'pro_only' => false,
5996 - 'addon_promo' => true,
5997 - 'group' => __('WooCommerce Features', 'mxchat'),
5998 - 'icon' => 'arrow-right-alt',
5999 - 'description' => __('Redirect customer to checkout page', 'mxchat'),
6000 - ),
6001 - 'mxchat_handle_featured_products' => array(
6002 - 'label' => __('Featured Products Showcase', 'mxchat'),
6003 - 'pro_only' => false,
6004 - 'addon_promo' => true,
6005 - 'group' => __('WooCommerce Features', 'mxchat'),
6006 - 'icon' => 'star-filled',
6007 - 'description' => __('Display a curated selection of products with an AI-generated message', 'mxchat'),
6008 - ),
6009 -
6010 - // Perplexity Add-on
6011 - 'mxchat_perplexity_research' => array(
6012 - 'label' => __('Perplexity Research', 'mxchat'),
6013 - 'pro_only' => false,
6014 - 'addon_promo' => true,
6015 - 'group' => __('Search Features', 'mxchat'),
6016 - 'icon' => 'book-alt',
6017 - 'description' => __('Allows the chatbot to search the web for accurate, up-to-date answers', 'mxchat'),
6018 - ),
6019 -
6020 - // Forms Add-on
6021 - 'mxchat_handle_form_collection' => array(
6022 - 'label' => __('Form Collection', 'mxchat'),
6023 - 'pro_only' => false,
6024 - 'addon_promo' => true,
6025 - 'group' => __('Form Features', 'mxchat'),
6026 - 'icon' => 'feedback',
6027 - 'description' => __('Collect user information through custom forms in chat', 'mxchat'),
6028 - ),
6029 - );
6030 -
6031 - // Enhance add-on callbacks with installation status and addon info
6032 - foreach ($addon_callbacks as $function => $data) {
6033 - if (isset($addon_function_mapping[$function])) {
6034 - $addon_info = $addon_function_mapping[$function];
6035 -
6036 - $addon_callbacks[$function]['addon'] = $addon_info['addon'];
6037 - $addon_callbacks[$function]['addon_name'] = $addon_info['addon_name'];
6038 - $addon_callbacks[$function]['installed'] = $addon_info['is_installed'];
6039 -
6040 - // Set pro_only based on add-on configuration
6041 - $addon_callbacks[$function]['pro_only'] = $addon_info['pro_required'];
6042 - } else {
6043 - $addon_callbacks[$function]['addon'] = 'unknown';
6044 - $addon_callbacks[$function]['addon_name'] = __('Unknown Add-on', 'mxchat');
6045 - $addon_callbacks[$function]['installed'] = false;
6046 - }
6047 - }
6048 -
6049 - // Initialize callbacks with core features
6050 - $callbacks = $core_callbacks;
6051 -
6052 - // Get callbacks from active add-ons
6053 - $active_addon_callbacks = apply_filters('mxchat_available_callbacks', array());
6054 -
6055 - // Add placeholder callbacks only for add-ons that aren't active
6056 - if ($include_all) {
6057 - foreach ($addon_callbacks as $function => $data) {
6058 - // Skip placeholders for functions provided by active add-ons
6059 - if (in_array($function, $addon_provided_functions)) {
6060 - continue;
6061 - }
6062 -
6063 - // Skip excluded functions
6064 - if (in_array($function, $excluded_functions)) {
6065 - continue;
6066 - }
6067 -
6068 - // Add the placeholder
6069 - $callbacks[$function] = $data;
6070 - }
6071 - }
6072 -
6073 - // Add callbacks from active add-ons (will override placeholders)
6074 - foreach ($active_addon_callbacks as $function => $data) {
6075 - // Skip excluded functions
6076 - if (in_array($function, $excluded_functions)) {
6077 - continue;
6078 - }
6079 -
6080 - // Always include callbacks from add-ons
6081 - $callbacks[$function] = $data;
6082 -
6083 - // Ensure they have the proper add-on info
6084 - if (isset($addon_function_mapping[$function])) {
6085 - $addon_info = $addon_function_mapping[$function];
6086 - $callbacks[$function]['addon'] = $addon_info['addon'];
6087 - $callbacks[$function]['addon_name'] = $addon_info['addon_name'];
6088 - $callbacks[$function]['installed'] = $addon_info['is_installed'];
6089 - $callbacks[$function]['pro_only'] = $addon_info['pro_required'];
6090 - }
6091 - }
6092 -
6093 - // Just before returning callbacks, sort them to prioritize free features
6094 - if (!$grouped) {
6095 - // Create temporary arrays for sorting
6096 - $free_callbacks = array();
6097 - $pro_callbacks = array();
6098 -
6099 - // Split callbacks into free and pro
6100 - foreach ($callbacks as $key => $data) {
6101 - if (isset($data['pro_only']) && $data['pro_only']) {
6102 - $pro_callbacks[$key] = $data;
6103 - } else {
6104 - $free_callbacks[$key] = $data;
6105 - }
6106 - }
6107 -
6108 - // Merge with free callbacks first
6109 - $callbacks = array_merge($free_callbacks, $pro_callbacks);
6110 - }
6111 -
6112 - // Return grouped structure if requested
6113 - if ($grouped) {
6114 - $grouped_callbacks = array();
6115 - foreach ($callbacks as $key => $data) {
6116 - $group_label = isset($data['group']) ? $data['group'] : __('Other Features', 'mxchat');
6117 -
6118 - // Ensure we carry forward all the new fields in grouped mode
6119 - $callback_data = array(
6120 - 'label' => $data['label'],
6121 - 'pro_only' => isset($data['pro_only']) ? $data['pro_only'] : false,
6122 - 'icon' => isset($data['icon']) ? $data['icon'] : 'admin-generic',
6123 - 'description' => isset($data['description']) ? $data['description'] : __('Custom action for your chatbot', 'mxchat'),
6124 - 'addon' => isset($data['addon']) ? $data['addon'] : false,
6125 - 'addon_name' => isset($data['addon_name']) ? $data['addon_name'] : '',
6126 - 'installed' => isset($data['installed']) ? $data['installed'] : true
6127 - );
6128 -
6129 - $grouped_callbacks[$group_label][$key] = $callback_data;
6130 - }
6131 -
6132 - // Sort within each group to prioritize free features
6133 - foreach ($grouped_callbacks as $group => $items) {
6134 - $free_items = array();
6135 - $pro_items = array();
6136 -
6137 - foreach ($items as $key => $data) {
6138 - if (isset($data['pro_only']) && $data['pro_only']) {
6139 - $pro_items[$key] = $data;
6140 - } else {
6141 - $free_items[$key] = $data;
6142 - }
6143 - }
6144 -
6145 - $grouped_callbacks[$group] = array_merge($free_items, $pro_items);
6146 - }
6147 -
6148 - return $grouped_callbacks;
6149 - }
6150 -
6151 - return $callbacks;
6152 -}
6153 -
6154 1674 public function mxchat_page_init() {
1675 + // Register settings
6155 1676 register_setting(
6156 1677 'mxchat_option_group',
6157 1678 'mxchat_options',
6158 1679 array($this, 'mxchat_sanitize')
@@ -6163,201 +1684,70 @@
6163 1684 'mxchat_similarity_threshold',
6164 1685 array(
6165 1686 'type' => 'number',
6166 1687 'sanitize_callback' => function($value) {
6167 - $value = absint($value);
6168 - return min(max($value, 20), 95);
1688 + $value = absint($value); // Ensure it's an integer
1689 + return min(max($value, 70), 85); // Enforce range
6169 1690 },
6170 1691 'default' => 80,
6171 1692 )
6172 1693 );
6173 1694
1695 +
6174 1696 // Chatbot Settings Section
6175 1697 add_settings_section(
6176 1698 'mxchat_chatbot_section',
6177 - esc_html__('Chatbot Settings', 'mxchat'),
1699 + 'Chatbot Settings',
6178 1700 null,
6179 1701 'mxchat-chatbot'
6180 1702 );
6181 1703
6182 - // API Keys Settings Section
6183 - add_settings_section(
6184 - 'mxchat_api_keys_section',
6185 - esc_html__('API Keys', 'mxchat'),
6186 - array($this, 'mxchat_api_keys_section_callback'),
6187 - 'mxchat-api-keys'
6188 - );
6189 -
6190 - // OpenAI API Key
6191 - add_settings_field(
6192 - 'api_key',
6193 - esc_html__('OpenAI API Key', 'mxchat'),
6194 - array($this, 'api_key_callback'),
6195 - 'mxchat-api-keys',
6196 - 'mxchat_api_keys_section'
6197 - );
6198 -
6199 - // X.AI API Key
6200 - add_settings_field(
6201 - 'xai_api_key',
6202 - esc_html__('X.AI API Key', 'mxchat'),
6203 - array($this, 'xai_api_key_callback'),
6204 - 'mxchat-api-keys',
6205 - 'mxchat_api_keys_section'
6206 - );
6207 -
6208 - // Claude API Key
6209 - add_settings_field(
6210 - 'claude_api_key',
6211 - esc_html__('Claude API Key', 'mxchat'),
6212 - array($this, 'claude_api_key_callback'),
6213 - 'mxchat-api-keys',
6214 - 'mxchat_api_keys_section'
6215 - );
6216 -
6217 - // DeepSeek API Key
6218 - add_settings_field(
6219 - 'deepseek_api_key',
6220 - esc_html__('DeepSeek API Key', 'mxchat'),
6221 - array($this, 'deepseek_api_key_callback'),
6222 - 'mxchat-api-keys',
6223 - 'mxchat_api_keys_section'
6224 - );
6225 -
6226 - // Google Gemini API Key
6227 - add_settings_field(
6228 - 'gemini_api_key',
6229 - esc_html__('Google Gemini API Key', 'mxchat'),
6230 - array($this, 'gemini_api_key_callback'),
6231 - 'mxchat-api-keys',
6232 - 'mxchat_api_keys_section'
6233 - );
6234 -
6235 - // Voyage AI API Key
6236 - add_settings_field(
6237 - 'voyage_api_key',
6238 - esc_html__('Voyage AI API Key', 'mxchat'),
6239 - array($this, 'voyage_api_key_callback'),
6240 - 'mxchat-api-keys',
6241 - 'mxchat_api_keys_section'
6242 - );
6243 -
6244 - // OpenRouter API Key
6245 - add_settings_field(
6246 - 'openrouter_api_key',
6247 - esc_html__('OpenRouter API Key', 'mxchat'),
6248 - array($this, 'openrouter_api_key_callback'),
6249 - 'mxchat-api-keys',
6250 - 'mxchat_api_keys_section'
6251 - );
6252 -
6253 - // Custom (OpenAI-compatible) Provider — for Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.
6254 - add_settings_field(
6255 - 'custom_provider',
6256 - esc_html__('Custom Provider (OpenAI-compatible)', 'mxchat'),
6257 - array($this, 'custom_provider_callback'),
6258 - 'mxchat-api-keys',
6259 - 'mxchat_api_keys_section'
6260 - );
6261 -
6262 - // Loops API Key
6263 - add_settings_field(
6264 - 'loops_api_key',
6265 - esc_html__('Loops API Key', 'mxchat'),
6266 - array($this, 'mxchat_loops_api_key_callback'),
6267 - 'mxchat-api-keys',
6268 - 'mxchat_api_keys_section'
6269 - );
6270 -
6271 - // Brave Search API Key
6272 - add_settings_field(
6273 - 'brave_api_key',
6274 - __('Brave API Key', 'mxchat'),
6275 - array($this, 'mxchat_brave_api_key_callback'),
6276 - 'mxchat-api-keys',
6277 - 'mxchat_api_keys_section'
6278 - );
6279 -
6280 1704 // Similarity Threshold Slider
6281 1705 add_settings_field(
6282 1706 'similarity_threshold', // Field ID
6283 - esc_html__('Similarity Threshold', 'mxchat'), // Field title
1707 + 'Similarity Threshold', // Field title
6284 1708 array($this, 'mxchat_similarity_threshold_callback'), // Callback function
6285 1709 'mxchat-chatbot', // Page
6286 1710 'mxchat_chatbot_section' // Section
6287 1711 );
6288 1712
6289 - // RAG Sources Limit Slider
1713 + // Existing fields...
6290 1714 add_settings_field(
6291 - 'rag_sources_limit', // Field ID
6292 - esc_html__('RAG Sources Limit', 'mxchat'), // Field title
6293 - array($this, 'mxchat_rag_sources_limit_callback'), // Callback function
6294 - 'mxchat-chatbot', // Page
6295 - 'mxchat_chatbot_section' // Section
6296 - );
6297 -
6298 - // RAG Chunks Limit Slider
6299 - add_settings_field(
6300 - 'rag_chunks_limit', // Field ID
6301 - esc_html__('RAG Chunks Limit', 'mxchat'), // Field title
6302 - array($this, 'mxchat_rag_chunks_limit_callback'), // Callback function
6303 - 'mxchat-chatbot', // Page
6304 - 'mxchat_chatbot_section' // Section
6305 - );
6306 -
6307 - add_settings_field(
6308 - 'append_to_body',
6309 - esc_html__('Auto-Display Chatbot', 'mxchat'),
6310 - array($this, 'mxchat_append_to_body_callback'),
1715 + 'api_key',
1716 + 'OpenAI API Key',
1717 + array($this, 'api_key_callback'),
6311 1718 'mxchat-chatbot',
6312 1719 'mxchat_chatbot_section'
6313 1720 );
6314 1721
6315 1722 add_settings_field(
6316 - 'contextual_awareness_toggle',
6317 - esc_html__('Contextual Awareness', 'mxchat'),
6318 - array($this, 'mxchat_contextual_awareness_callback'),
6319 - 'mxchat-chatbot',
6320 - 'mxchat_chatbot_section'
1723 + 'xai_api_key',
1724 + 'X.AI API Key',
1725 + array($this, 'xai_api_key_callback'),
1726 + 'mxchat-chatbot',
1727 + 'mxchat_chatbot_section'
6321 1728 );
6322 1729
6323 1730 add_settings_field(
6324 - 'citation_links_toggle',
6325 - esc_html__('Citation Links', 'mxchat'),
6326 - array($this, 'mxchat_citation_links_toggle_callback'),
1731 + 'claude_api_key',
1732 + 'Claude API Key',
1733 + array($this, 'claude_api_key_callback'),
6327 1734 'mxchat-chatbot',
6328 1735 'mxchat_chatbot_section'
6329 1736 );
6330 1737
6331 - // Satisfaction rating toggle (plan-a5b006).
1738 +
6332 1739 add_settings_field(
6333 - 'satisfaction_rating_enabled',
6334 - esc_html__('Satisfaction Rating Prompt', 'mxchat'),
6335 - array($this, 'mxchat_satisfaction_rating_toggle_callback'),
1740 + 'system_prompt_instructions',
1741 + 'AI Instructions (Behavior)',
1742 + array($this, 'system_prompt_instructions_callback'),
6336 1743 'mxchat-chatbot',
6337 1744 'mxchat_chatbot_section'
6338 1745 );
6339 1746
6340 - // Satisfaction rating customization (plan-141a12, plan-29caac):
6341 - // the 5 customization fields are now inline-rendered inside
6342 - // mxchat_satisfaction_rating_toggle_callback's sub-options wrapper.
6343 -
6344 1747 add_settings_field(
6345 - 'enable_streaming_toggle',
6346 - esc_html__('Enable Streaming', 'mxchat'),
6347 - array($this, 'enable_streaming_toggle_callback'),
6348 - 'mxchat-chatbot',
6349 - 'mxchat_chatbot_section', // Same section as your working toggle
6350 - array(
6351 - 'class' => 'mxchat-setting-row streaming-setting',
6352 - 'style' => 'display: none;' // Hidden by default, shown when OpenAI/Claude selected
6353 - )
6354 - );
6355 -
6356 -
6357 - add_settings_field(
6358 1748 'model',
6359 - esc_html__('Chat Model', 'mxchat'),
1749 + 'Model',
6360 1750 array($this, 'mxchat_model_callback'),
6361 1751 'mxchat-chatbot',
6362 1752 'mxchat_chatbot_section'
6363 1753 );
@@ -6362,43 +1752,18 @@
6362 1752 'mxchat_chatbot_section'
6363 1753 );
6364 1754
6365 1755 add_settings_field(
6366 - 'embedding_model',
6367 - esc_html__('Embedding Model', 'mxchat'),
6368 - array($this, 'embedding_model_callback'),
6369 - 'mxchat-chatbot',
6370 - 'mxchat_chatbot_section'
6371 - );
6372 -
6373 - add_settings_field(
6374 - 'system_prompt_instructions',
6375 - esc_html__('AI Instructions (Behavior)', 'mxchat'),
6376 - array($this, 'system_prompt_instructions_callback'),
6377 - 'mxchat-chatbot',
6378 - 'mxchat_chatbot_section'
6379 - );
6380 -
6381 -
6382 - add_settings_field(
6383 1756 'top_bar_title',
6384 - esc_html__('Top Bar Title', 'mxchat'),
1757 + 'Top Bar Title',
6385 1758 array($this, 'mxchat_top_bar_title_callback'),
6386 1759 'mxchat-chatbot',
6387 1760 'mxchat_chatbot_section'
6388 1761 );
6389 1762
6390 - add_settings_field(
6391 - 'ai_agent_text',
6392 - esc_html__('AI Agent Text', 'mxchat'),
6393 - array($this, 'mxchat_ai_agent_text_callback'),
6394 - 'mxchat-chatbot',
6395 - 'mxchat_chatbot_section'
6396 - );
6397 -
6398 - add_settings_field(
1763 + add_settings_field(
6399 1764 'enable_email_block',
6400 - esc_html__('Require Email To Chat', 'mxchat'),
1765 + 'Require Email Before Chat',
6401 1766 array($this, 'enable_email_block_callback'),
6402 1767 'mxchat-chatbot',
6403 1768 'mxchat_chatbot_section'
6404 1769 );
@@ -6404,9 +1769,9 @@
6404 1769 );
6405 1770
6406 1771 add_settings_field(
6407 1772 'email_blocker_header_content',
6408 - esc_html__('Require Email Chat Content', 'mxchat'),
1773 + 'Require Email Chat Content',
6409 1774 array($this, 'email_blocker_header_content_callback'),
6410 1775 'mxchat-chatbot',
6411 1776 'mxchat_chatbot_section'
6412 1777 );
@@ -6412,9 +1777,9 @@
6412 1777 );
6413 1778
6414 1779 add_settings_field(
6415 1780 'email_blocker_button_text',
6416 - esc_html__('Require Email Chat Button Text', 'mxchat'),
1781 + 'Require Email Chat Button Text',
6417 1782 [$this, 'email_blocker_button_text_callback'],
6418 1783 'mxchat-chatbot',
6419 1784 'mxchat_chatbot_section'
6420 1785 );
@@ -6419,26 +1784,10 @@
6419 1784 'mxchat_chatbot_section'
6420 1785 );
6421 1786
6422 1787 add_settings_field(
6423 - 'enable_name_field',
6424 - esc_html__('Require Name Field', 'mxchat'),
6425 - array($this, 'enable_name_field_callback'),
6426 - 'mxchat-chatbot',
6427 - 'mxchat_chatbot_section'
6428 - );
6429 -
6430 - add_settings_field(
6431 - 'name_field_placeholder',
6432 - esc_html__('Name Field Placeholder', 'mxchat'),
6433 - array($this, 'name_field_placeholder_callback'),
6434 - 'mxchat-chatbot',
6435 - 'mxchat_chatbot_section'
6436 - );
6437 -
6438 - add_settings_field(
6439 1788 'intro_message',
6440 - esc_html__('Introductory Message', 'mxchat'),
1789 + 'Introductory Message',
6441 1790 array($this, 'mxchat_intro_message_callback'),
6442 1791 'mxchat-chatbot',
6443 1792 'mxchat_chatbot_section'
6444 1793 );
@@ -6444,9 +1793,9 @@
6444 1793 );
6445 1794
6446 1795 add_settings_field(
6447 1796 'input_copy',
6448 - esc_html__('Input Copy', 'mxchat'),
1797 + 'Input Copy',
6449 1798 array($this, 'mxchat_input_copy_callback'),
6450 1799 'mxchat-chatbot',
6451 1800 'mxchat_chatbot_section'
6452 1801 );
@@ -6451,146 +1800,189 @@
6451 1800 'mxchat_chatbot_section'
6452 1801 );
6453 1802
6454 1803 add_settings_field(
6455 - 'pre_chat_message',
6456 - esc_html__('Chat Teaser Pop-up', 'mxchat'),
6457 - array($this, 'mxchat_pre_chat_message_callback'),
1804 + 'rate_limit_logged_in',
1805 + __('Rate Limit for Logged-in Users', 'mxchat'),
1806 + array($this, 'mxchat_rate_limit_logged_in_callback'),
6458 1807 'mxchat-chatbot',
6459 1808 'mxchat_chatbot_section'
6460 1809 );
6461 1810
6462 1811 add_settings_field(
6463 - 'privacy_toggle',
6464 - esc_html__('Toggle Privacy Notice', 'mxchat'),
6465 - array($this, 'mxchat_privacy_toggle_callback'),
1812 + 'rate_limit_logged_out',
1813 + __('Rate Limit for Logged-out Users', 'mxchat'),
1814 + array($this, 'mxchat_rate_limit_logged_out_callback'),
6466 1815 'mxchat-chatbot',
6467 1816 'mxchat_chatbot_section'
6468 1817 );
6469 1818
6470 1819 add_settings_field(
6471 - 'complianz_toggle',
6472 - esc_html__('Enable Complianz', 'mxchat'),
6473 - array($this, 'mxchat_complianz_toggle_callback'),
1820 + 'rate_limit_message',
1821 + 'Rate Limit Message',
1822 + array($this, 'mxchat_rate_limit_message_callback'),
6474 1823 'mxchat-chatbot',
6475 1824 'mxchat_chatbot_section'
6476 1825 );
6477 1826
6478 1827 add_settings_field(
6479 - 'link_target_toggle',
6480 - esc_html__('Open Links in a New Tab', 'mxchat'),
6481 - array($this, 'mxchat_link_target_toggle_callback'),
1828 + 'pre_chat_message',
1829 + 'Chat Teaser Pop-up',
1830 + array($this, 'mxchat_pre_chat_message_callback'),
6482 1831 'mxchat-chatbot',
6483 1832 'mxchat_chatbot_section'
6484 1833 );
6485 1834
6486 - add_settings_field(
6487 - 'chat_persistence_toggle',
6488 - esc_html__('Enable Chat Persistence', 'mxchat'),
6489 - array($this, 'mxchat_chat_persistence_toggle_callback'),
1835 + add_settings_field(
1836 + 'append_to_body',
1837 + 'Append Chat Widget to Body',
1838 + array($this, 'mxchat_append_to_body_callback'),
6490 1839 'mxchat-chatbot',
6491 1840 'mxchat_chatbot_section'
6492 1841 );
6493 1842
6494 - add_settings_field(
6495 - 'print_button_enabled',
6496 - esc_html__('Show Download Transcript Button', 'mxchat'),
6497 - array($this, 'mxchat_print_button_toggle_callback'),
1843 + add_settings_field(
1844 + 'privacy_toggle',
1845 + 'Toggle Privacy Notice',
1846 + array($this, 'mxchat_privacy_toggle_callback'),
6498 1847 'mxchat-chatbot',
6499 1848 'mxchat_chatbot_section'
6500 1849 );
6501 1850
6502 1851 add_settings_field(
6503 - 'reset_chat_enabled',
6504 - esc_html__('Show Start-New-Chat Button', 'mxchat'),
6505 - array($this, 'mxchat_reset_chat_toggle_callback'),
1852 + 'complianz_toggle',
1853 + 'Enable Complianz',
1854 + array($this, 'mxchat_complianz_toggle_callback'),
6506 1855 'mxchat-chatbot',
6507 1856 'mxchat_chatbot_section'
6508 1857 );
6509 1858
6510 1859 add_settings_field(
6511 - 'reset_chat_label',
6512 - esc_html__('Start-New-Chat Button Label', 'mxchat'),
6513 - array($this, 'mxchat_reset_chat_label_callback'),
1860 + 'link_target_toggle',
1861 + 'Open Links in a New Tab',
1862 + array($this, 'mxchat_link_target_toggle_callback'),
6514 1863 'mxchat-chatbot',
6515 1864 'mxchat_chatbot_section'
6516 1865 );
6517 1866
6518 1867 add_settings_field(
6519 - 'popular_question_1',
6520 - esc_html__('Quick Question 1', 'mxchat'),
6521 - array($this, 'mxchat_popular_question_1_callback'),
6522 - 'mxchat-chatbot',
6523 - 'mxchat_chatbot_section'
1868 + 'chat_persistence_toggle',
1869 + 'Enable Chat Persistence',
1870 + array($this, 'mxchat_chat_persistence_toggle_callback'),
1871 + 'mxchat-chatbot',
1872 + 'mxchat_chatbot_section'
6524 1873 );
6525 1874
6526 1875 add_settings_field(
6527 - 'popular_question_2',
6528 - esc_html__('Quick Question 2', 'mxchat'),
6529 - array($this, 'mxchat_popular_question_2_callback'),
6530 - 'mxchat-chatbot',
6531 - 'mxchat_chatbot_section'
6532 - );
1876 + 'popular_question_1',
1877 + 'Popular Question 1',
1878 + array($this, 'mxchat_popular_question_1_callback'),
1879 + 'mxchat-chatbot',
1880 + 'mxchat_chatbot_section'
1881 +);
6533 1882
6534 - add_settings_field(
6535 - 'popular_question_3',
6536 - esc_html__('Quick Question 3', 'mxchat'),
6537 - array($this, 'mxchat_popular_question_3_callback'),
6538 - 'mxchat-chatbot',
6539 - 'mxchat_chatbot_section'
6540 - );
1883 +add_settings_field(
1884 + 'popular_question_2',
1885 + 'Popular Question 2',
1886 + array($this, 'mxchat_popular_question_2_callback'),
1887 + 'mxchat-chatbot',
1888 + 'mxchat_chatbot_section'
1889 +);
6541 1890
6542 - add_settings_field(
6543 - 'additional_popular_questions',
6544 - esc_html__('Additional Quick Questions', 'mxchat'),
6545 - array($this, 'mxchat_additional_popular_questions_callback'),
6546 - 'mxchat-chatbot',
6547 - 'mxchat_chatbot_section'
6548 - );
1891 +add_settings_field(
1892 + 'popular_question_3',
1893 + 'Popular Question 3',
1894 + array($this, 'mxchat_popular_question_3_callback'),
1895 + 'mxchat-chatbot',
1896 + 'mxchat_chatbot_section'
1897 +);
6549 1898
1899 +add_settings_field(
1900 + 'additional_popular_questions',
1901 + 'Additional Popular Questions',
1902 + array($this, 'mxchat_additional_popular_questions_callback'),
1903 + 'mxchat-chatbot',
1904 + 'mxchat_chatbot_section'
1905 +);
6550 1906
6551 - add_settings_field(
6552 - 'rate_limits',
6553 - __('Rate Limits Settings', 'mxchat'),
6554 - array($this, 'mxchat_rate_limits_callback'),
6555 - 'mxchat-chatbot',
6556 - 'mxchat_chatbot_section'
6557 - );
6558 1907
6559 - // Loops Settings Section
6560 - add_settings_section(
6561 - 'mxchat_loops_section',
6562 - esc_html__('Loops Settings', 'mxchat'),
6563 - null,
6564 - 'mxchat-embed'
6565 - );
1908 +// WooCommerce Settings Section
1909 +add_settings_section(
1910 + 'mxchat_woocommerce_section',
1911 + 'WooCommerce Settings',
1912 + null,
1913 + 'mxchat-embed'
1914 +);
6566 1915
6567 - // Loops Settings Fields (API Key moved to API Keys tab)
6568 - add_settings_field(
6569 - 'loops_mailing_list',
6570 - esc_html__('Loops Mailing List', 'mxchat'),
6571 - array($this, 'mxchat_loops_mailing_list_callback'),
6572 - 'mxchat-embed',
6573 - 'mxchat_loops_section'
6574 - );
1916 +// Loops Settings Section
1917 +add_settings_section(
1918 + 'mxchat_loops_section',
1919 + 'Loops Settings',
1920 + null,
1921 + 'mxchat-embed'
1922 +);
6575 1923
6576 - add_settings_field(
6577 - 'triggered_phrase_response',
6578 - esc_html__('Triggered Phrase Response', 'mxchat'),
6579 - array($this, 'mxchat_triggered_phrase_response_callback'),
6580 - 'mxchat-embed',
6581 - 'mxchat_loops_section'
6582 - );
1924 +// WooCommerce Settings Fields
1925 +add_settings_field(
1926 + 'enable_woocommerce_integration',
1927 + 'Automatically Embed Products',
1928 + array($this, 'mxchat_enable_woocommerce_integration_callback'),
1929 + 'mxchat-embed',
1930 + 'mxchat_woocommerce_section'
1931 +);
6583 1932
6584 - add_settings_field(
6585 - 'email_capture_response',
6586 - esc_html__('Email Capture Response', 'mxchat'),
6587 - array($this, 'mxchat_email_capture_response_callback'),
6588 - 'mxchat-embed',
6589 - 'mxchat_loops_section'
6590 - );
6591 1933
6592 - // Brave Search Settings Fields
1934 +add_settings_field(
1935 + 'woocommerce_consumer_key',
1936 + 'WooCommerce Consumer Key',
1937 + array($this, 'mxchat_woocommerce_consumer_key_callback'),
1938 + 'mxchat-embed',
1939 + 'mxchat_woocommerce_section'
1940 +);
1941 +
1942 +add_settings_field(
1943 + 'woocommerce_consumer_secret',
1944 + 'WooCommerce Consumer Secret',
1945 + array($this, 'mxchat_woocommerce_consumer_secret_callback'),
1946 + 'mxchat-embed',
1947 + 'mxchat_woocommerce_section'
1948 +);
1949 +
1950 +// Loops Settings Fields
1951 +add_settings_field(
1952 + 'loops_api_key',
1953 + 'Loops API Key',
1954 + array($this, 'mxchat_loops_api_key_callback'),
1955 + 'mxchat-embed',
1956 + 'mxchat_loops_section'
1957 +);
1958 +
1959 +add_settings_field(
1960 + 'loops_mailing_list',
1961 + 'Loops Mailing List',
1962 + array($this, 'mxchat_loops_mailing_list_callback'),
1963 + 'mxchat-embed',
1964 + 'mxchat_loops_section'
1965 +);
1966 +
1967 +add_settings_field(
1968 + 'triggered_phrase_response',
1969 + 'Triggered Phrase Response',
1970 + array($this, 'mxchat_triggered_phrase_response_callback'),
1971 + 'mxchat-embed',
1972 + 'mxchat_loops_section'
1973 +);
1974 +
1975 +add_settings_field(
1976 + 'email_capture_response',
1977 + 'Email Capture Response',
1978 + array($this, 'mxchat_email_capture_response_callback'),
1979 + 'mxchat-embed',
1980 + 'mxchat_loops_section'
1981 +);
1982 +
1983 +
1984 + // Brave Search Settings Fields
6593 1985 add_settings_section(
6594 1986 'mxchat_brave_section',
6595 1987 __('Brave Search Settings', 'mxchat'),
6596 1988 array($this, 'mxchat_brave_section_callback'),
@@ -6596,10 +1988,17 @@
6596 1988 array($this, 'mxchat_brave_section_callback'),
6597 1989 'mxchat-embed'
6598 1990 );
6599 1991
6600 - // Brave API Key moved to API Keys tab
6601 1992 add_settings_field(
1993 + 'brave_api_key',
1994 + __('Brave API Key', 'mxchat'),
1995 + array($this, 'mxchat_brave_api_key_callback'),
1996 + 'mxchat-embed',
1997 + 'mxchat_brave_section'
1998 + );
1999 +
2000 + add_settings_field(
6602 2001 'brave_image_count',
6603 2002 __('Number of Images to Return', 'mxchat'),
6604 2003 array($this, 'mxchat_brave_image_count_callback'),
6605 2004 'mxchat-embed',
@@ -6637,545 +2036,397 @@
6637 2036 'mxchat-embed',
6638 2037 'mxchat_brave_section'
6639 2038 );
6640 2039
2040 +
6641 2041 // Chat with PDF Intent Settings Fields
6642 - add_settings_section(
6643 - 'mxchat_pdf_intent_section',
6644 - __('Toolbar Settings & Intents', 'mxchat'),
6645 - array($this, 'mxchat_pdf_intent_section_callback'),
6646 - 'mxchat-embed'
6647 - );
2042 +add_settings_section(
2043 + 'mxchat_pdf_intent_section',
2044 + __('Toolbar Settings & Intents', 'mxchat'),
2045 + array($this, 'mxchat_pdf_intent_section_callback'),
2046 + 'mxchat-embed'
2047 +);
6648 2048
6649 - add_settings_field(
6650 - 'chat_toolbar_toggle',
6651 - __('Show Chat Toolbar', 'mxchat'),
6652 - array($this, 'mxchat_chat_toolbar_toggle_callback'),
6653 - 'mxchat-embed',
6654 - 'mxchat_pdf_intent_section'
6655 - );
2049 +add_settings_field(
2050 + 'chat_toolbar_toggle',
2051 + __('Show Chat Toolbar', 'mxchat'),
2052 + array($this, 'mxchat_chat_toolbar_toggle_callback'),
2053 + 'mxchat-embed',
2054 + 'mxchat_pdf_intent_section'
2055 +);
6656 2056
6657 - // PDF Upload Button Toggle
6658 - add_settings_field(
6659 - 'show_pdf_upload_button',
6660 - __('Show PDF Upload Button', 'mxchat'),
6661 - array($this, 'mxchat_show_pdf_upload_button_callback'),
6662 - 'mxchat-embed',
6663 - 'mxchat_pdf_intent_section'
6664 - );
6665 2057
6666 - // Word Upload Button Toggle
6667 - add_settings_field(
6668 - 'show_word_upload_button',
6669 - __('Show Word Upload Button', 'mxchat'),
6670 - array($this, 'mxchat_show_word_upload_button_callback'),
6671 - 'mxchat-embed',
6672 - 'mxchat_pdf_intent_section'
6673 - );
2058 +add_settings_field(
2059 + 'pdf_intent_trigger_text',
2060 + __('Intent Trigger Text', 'mxchat'),
2061 + array($this, 'mxchat_pdf_intent_trigger_text_callback'),
2062 + 'mxchat-embed',
2063 + 'mxchat_pdf_intent_section'
2064 +);
6674 2065
6675 - add_settings_field(
6676 - 'pdf_intent_trigger_text',
6677 - __('Intent Trigger Text', 'mxchat'),
6678 - array($this, 'mxchat_pdf_intent_trigger_text_callback'),
6679 - 'mxchat-embed',
6680 - 'mxchat_pdf_intent_section'
6681 - );
2066 +add_settings_field(
2067 + 'pdf_intent_success_text',
2068 + __('Success Text', 'mxchat'),
2069 + array($this, 'mxchat_pdf_intent_success_text_callback'),
2070 + 'mxchat-embed',
2071 + 'mxchat_pdf_intent_section'
2072 +);
6682 2073
6683 - add_settings_field(
6684 - 'pdf_intent_success_text',
6685 - __('Success Text', 'mxchat'),
6686 - array($this, 'mxchat_pdf_intent_success_text_callback'),
6687 - 'mxchat-embed',
6688 - 'mxchat_pdf_intent_section'
6689 - );
2074 +add_settings_field(
2075 + 'pdf_intent_error_text',
2076 + __('Error Text', 'mxchat'),
2077 + array($this, 'mxchat_pdf_intent_error_text_callback'),
2078 + 'mxchat-embed',
2079 + 'mxchat_pdf_intent_section'
2080 +);
6690 2081
6691 - add_settings_field(
6692 - 'pdf_intent_error_text',
6693 - __('Error Text', 'mxchat'),
6694 - array($this, 'mxchat_pdf_intent_error_text_callback'),
6695 - 'mxchat-embed',
6696 - 'mxchat_pdf_intent_section'
6697 - );
2082 +// Add PDF Maximum Pages Field
2083 +add_settings_field(
2084 + 'pdf_max_pages',
2085 + __('Maximum Document Pages', 'mxchat'),
2086 + array($this, 'mxchat_pdf_max_pages_callback'),
2087 + 'mxchat-embed',
2088 + 'mxchat_pdf_intent_section'
2089 +);
6698 2090
6699 - // Add PDF Maximum Pages Field
6700 - add_settings_field(
6701 - 'pdf_max_pages',
6702 - __('Maximum Document Pages', 'mxchat'),
6703 - array($this, 'mxchat_pdf_max_pages_callback'),
6704 - 'mxchat-embed',
6705 - 'mxchat_pdf_intent_section'
6706 - );
6707 2091
6708 - // Live Agent Settings Fields
2092 +
2093 +
2094 +// Live Agent Settings Fields
2095 +add_settings_section(
2096 + 'mxchat_live_agent_section',
2097 + __('Live Agent Settings', 'mxchat'),
2098 + array($this, 'mxchat_live_agent_section_callback'),
2099 + 'mxchat-embed'
2100 +);
2101 +
2102 +// Live Agent Status Fields (add at top of live agent settings)
2103 +add_settings_field(
2104 + 'live_agent_status',
2105 + __('Live Agent Status', 'mxchat'),
2106 + array($this, 'mxchat_live_agent_status_callback'),
2107 + 'mxchat-embed',
2108 + 'mxchat_live_agent_section'
2109 +);
2110 +
2111 +add_settings_field(
2112 + 'live_agent_notification_message',
2113 + __('Notification Message', 'mxchat'),
2114 + array($this, 'mxchat_live_agent_notification_message_callback'),
2115 + 'mxchat-embed',
2116 + 'mxchat_live_agent_section'
2117 +);
2118 +
2119 +add_settings_field(
2120 + 'live_agent_away_message',
2121 + __('Away Message', 'mxchat'),
2122 + array($this, 'mxchat_live_agent_away_message_callback'),
2123 + 'mxchat-embed',
2124 + 'mxchat_live_agent_section'
2125 +);
2126 +
2127 +add_settings_field(
2128 + 'live_agent_webhook_url',
2129 + __('Slack Webhook URL', 'mxchat'),
2130 + array($this, 'mxchat_live_agent_webhook_url_callback'),
2131 + 'mxchat-embed',
2132 + 'mxchat_live_agent_section'
2133 +);
2134 +
2135 +add_settings_field(
2136 + 'live_agent_secret_key',
2137 + __('Slack Secret Key', 'mxchat'),
2138 + array($this, 'mxchat_live_agent_secret_key_callback'),
2139 + 'mxchat-embed',
2140 + 'mxchat_live_agent_section'
2141 +);
2142 +
2143 +// Live Agent Integration Fields
2144 +add_settings_field(
2145 + 'live_agent_bot_token',
2146 + __('Slack Bot OAuth Token', 'mxchat'),
2147 + array($this, 'mxchat_live_agent_bot_token_callback'),
2148 + 'mxchat-embed',
2149 + 'mxchat_live_agent_section'
2150 +);
2151 +
2152 +
2153 + // Theme Settings Section
6709 2154 add_settings_section(
6710 - 'mxchat_live_agent_section',
6711 - __('Live Agent Settings', 'mxchat'),
6712 - array($this, 'mxchat_live_agent_section_callback'),
6713 - 'mxchat-embed'
2155 + 'mxchat_theme_section',
2156 + 'Theme Settings',
2157 + null,
2158 + 'mxchat-theme'
6714 2159 );
6715 2160
6716 - // Live Agent Status Fields (add at top of live agent settings)
6717 2161 add_settings_field(
6718 - 'live_agent_status',
6719 - __('Live Agent Status', 'mxchat'),
6720 - array($this, 'mxchat_live_agent_status_callback'),
6721 - 'mxchat-embed',
6722 - 'mxchat_live_agent_section'
2162 + 'close_button_color',
2163 + 'Close Button & Title Color',
2164 + array($this, 'mxchat_close_button_color_callback'),
2165 + 'mxchat-theme',
2166 + 'mxchat_theme_section'
6723 2167 );
6724 2168
6725 - // Slack availability schedule (plans 8ccaa2 + 99d7a4). Each handoff channel
6726 - // owns an independent schedule rendered under its own Integrations tab; this
6727 - // one governs Slack only. Same callback as Telegram's, parameterized.
6728 2169 add_settings_field(
6729 - 'live_agent_schedule_slack',
6730 - __('Availability Schedule', 'mxchat'),
6731 - array($this, 'mxchat_live_agent_schedule_callback'),
6732 - 'mxchat-embed',
6733 - 'mxchat_live_agent_section',
6734 - array('channel' => 'slack')
2170 + 'chatbot_bg_color',
2171 + 'Chatbot Background Color',
2172 + array($this, 'mxchat_chatbot_bg_color_callback'),
2173 + 'mxchat-theme',
2174 + 'mxchat_theme_section'
6735 2175 );
6736 2176
6737 2177 add_settings_field(
6738 - 'live_agent_notification_message',
6739 - __('Notification Message', 'mxchat'),
6740 - array($this, 'mxchat_live_agent_notification_message_callback'),
6741 - 'mxchat-embed',
6742 - 'mxchat_live_agent_section'
2178 + 'user_message_bg_color',
2179 + 'User Message Background Color',
2180 + array($this, 'mxchat_user_message_bg_color_callback'),
2181 + 'mxchat-theme',
2182 + 'mxchat_theme_section'
6743 2183 );
6744 2184
6745 2185 add_settings_field(
6746 - 'live_agent_away_message',
6747 - __('Away Message', 'mxchat'),
6748 - array($this, 'mxchat_live_agent_away_message_callback'),
6749 - 'mxchat-embed',
6750 - 'mxchat_live_agent_section'
2186 + 'user_message_font_color',
2187 + 'User Message Font Color',
2188 + array($this, 'mxchat_user_message_font_color_callback'),
2189 + 'mxchat-theme',
2190 + 'mxchat_theme_section'
6751 2191 );
6752 2192
6753 2193 add_settings_field(
6754 - 'live_agent_user_ids',
6755 - __('Slack Agent User IDs', 'mxchat'),
6756 - array($this, 'mxchat_live_agent_user_ids_callback'),
6757 - 'mxchat-embed',
6758 - 'mxchat_live_agent_section'
2194 + 'bot_message_bg_color',
2195 + 'Bot Message Background Color',
2196 + array($this, 'mxchat_bot_message_bg_color_callback'),
2197 + 'mxchat-theme',
2198 + 'mxchat_theme_section'
6759 2199 );
6760 2200
6761 - // Shared handoff channel (plan 9f7756): route every handoff into one
6762 - // pre-existing channel as threads instead of creating chat-* channels.
6763 2201 add_settings_field(
6764 - 'live_agent_shared_channel',
6765 - __('Shared Handoff Channel', 'mxchat'),
6766 - array($this, 'mxchat_live_agent_shared_channel_callback'),
6767 - 'mxchat-embed',
6768 - 'mxchat_live_agent_section'
2202 + 'bot_message_font_color',
2203 + 'Bot Message Font Color',
2204 + array($this, 'mxchat_bot_message_font_color_callback'),
2205 + 'mxchat-theme',
2206 + 'mxchat_theme_section'
6769 2207 );
6770 2208
6771 - // Auto-archive per-conversation chat- channels on !endchat (plan 7458a7).
6772 - // Default OFF; never touches the shared handoff channel.
6773 2209 add_settings_field(
6774 - 'live_agent_archive_on_end_toggle',
6775 - __('Archive Channel When Chat Ends', 'mxchat'),
6776 - array($this, 'mxchat_live_agent_archive_on_end_callback'),
6777 - 'mxchat-embed',
6778 - 'mxchat_live_agent_section'
2210 + 'top_bar_bg_color',
2211 + 'Top Bar Background Color',
2212 + array($this, 'mxchat_top_bar_bg_color_callback'),
2213 + 'mxchat-theme',
2214 + 'mxchat_theme_section'
6779 2215 );
6780 2216
6781 2217 add_settings_field(
6782 - 'live_agent_webhook_url',
6783 - __('Slack Webhook URL', 'mxchat'),
6784 - array($this, 'mxchat_live_agent_webhook_url_callback'),
6785 - 'mxchat-embed',
6786 - 'mxchat_live_agent_section'
2218 + 'send_button_font_color',
2219 + 'Send Button Color',
2220 + array($this, 'mxchat_send_button_font_color_callback'),
2221 + 'mxchat-theme',
2222 + 'mxchat_theme_section'
6787 2223 );
6788 2224
6789 2225 add_settings_field(
6790 - 'live_agent_secret_key',
6791 - __('Slack Secret Key', 'mxchat'),
6792 - array($this, 'mxchat_live_agent_secret_key_callback'),
6793 - 'mxchat-embed',
6794 - 'mxchat_live_agent_section'
2226 + 'chat_input_font_color',
2227 + 'Chat Input Font Color',
2228 + array($this, 'mxchat_chat_input_font_color_callback'),
2229 + 'mxchat-theme',
2230 + 'mxchat_theme_section'
6795 2231 );
6796 2232
6797 - // Live Agent Integration Fields
6798 2233 add_settings_field(
6799 - 'live_agent_bot_token',
6800 - __('Slack Bot OAuth Token', 'mxchat'),
6801 - array($this, 'mxchat_live_agent_bot_token_callback'),
6802 - 'mxchat-embed',
6803 - 'mxchat_live_agent_section'
2234 + 'chatbot_background_color',
2235 + 'Floating Widget Background Color',
2236 + array($this, 'mxchat_chatbot_background_color_callback'),
2237 + 'mxchat-theme',
2238 + 'mxchat_theme_section'
6804 2239 );
6805 2240
6806 - // Telegram Integration Section
6807 - add_settings_section(
6808 - 'mxchat_telegram_section',
6809 - __('Telegram Settings', 'mxchat'),
6810 - array($this, 'mxchat_telegram_section_callback'),
6811 - 'mxchat-embed'
2241 + add_settings_field(
2242 + 'icon_color',
2243 + 'Chatbot Icon Color',
2244 + array($this, 'mxchat_icon_color_callback'),
2245 + 'mxchat-theme',
2246 + 'mxchat_theme_section'
6812 2247 );
6813 2248
6814 2249 add_settings_field(
6815 - 'telegram_status',
6816 - __('Live Agent Status', 'mxchat'),
6817 - array($this, 'mxchat_telegram_status_callback'),
6818 - 'mxchat-embed',
6819 - 'mxchat_telegram_section'
2250 + 'custom_icon',
2251 + 'Custom Chatbot Icon (PNG)',
2252 + array($this, 'mxchat_custom_icon_callback'),
2253 + 'mxchat-theme',
2254 + 'mxchat_theme_section'
6820 2255 );
6821 2256
6822 - // Telegram availability schedule (plan 99d7a4) — independent of Slack's,
6823 - // rendered right under the Telegram status toggle it extends.
6824 2257 add_settings_field(
6825 - 'live_agent_schedule_telegram',
6826 - __('Availability Schedule', 'mxchat'),
6827 - array($this, 'mxchat_live_agent_schedule_callback'),
6828 - 'mxchat-embed',
6829 - 'mxchat_telegram_section',
6830 - array('channel' => 'telegram')
2258 + 'title_icon',
2259 + 'Title Bar Icon (PNG)',
2260 + array($this, 'mxchat_title_icon_callback'),
2261 + 'mxchat-theme',
2262 + 'mxchat_theme_section'
6831 2263 );
6832 2264
2265 +
6833 2266 add_settings_field(
6834 - 'telegram_notification_message',
6835 - __('Notification Message', 'mxchat'),
6836 - array($this, 'mxchat_telegram_notification_message_callback'),
6837 - 'mxchat-embed',
6838 - 'mxchat_telegram_section'
2267 + 'live_agent_message_bg_color',
2268 + 'Live Agent Background Color',
2269 + array($this, 'mxchat_live_agent_message_bg_color_callback'),
2270 + 'mxchat-theme',
2271 + 'mxchat_theme_section'
6839 2272 );
6840 2273
6841 2274 add_settings_field(
6842 - 'telegram_away_message',
6843 - __('Away Message', 'mxchat'),
6844 - array($this, 'mxchat_telegram_away_message_callback'),
6845 - 'mxchat-embed',
6846 - 'mxchat_telegram_section'
2275 + 'live_agent_message_font_color',
2276 + 'Live Agent Font Color',
2277 + array($this, 'mxchat_live_agent_message_font_color_callback'),
2278 + 'mxchat-theme',
2279 + 'mxchat_theme_section'
6847 2280 );
6848 2281
2282 + // Mode Indicator Background Color
6849 2283 add_settings_field(
6850 - 'telegram_bot_token',
6851 - __('Telegram Bot Token', 'mxchat'),
6852 - array($this, 'mxchat_telegram_bot_token_callback'),
6853 - 'mxchat-embed',
6854 - 'mxchat_telegram_section'
2284 + 'mode_indicator_bg_color',
2285 + 'Mode Indicator Background Color',
2286 + array($this, 'mxchat_mode_indicator_bg_color_callback'),
2287 + 'mxchat-theme',
2288 + 'mxchat_theme_section'
6855 2289 );
6856 2290
2291 + // Mode Indicator Font Color
6857 2292 add_settings_field(
6858 - 'telegram_group_id',
6859 - __('Telegram Group ID', 'mxchat'),
6860 - array($this, 'mxchat_telegram_group_id_callback'),
6861 - 'mxchat-embed',
6862 - 'mxchat_telegram_section'
2293 + 'mode_indicator_font_color',
2294 + 'Mode Indicator Font Color',
2295 + array($this, 'mxchat_mode_indicator_font_color_callback'),
2296 + 'mxchat-theme',
2297 + 'mxchat_theme_section'
6863 2298 );
6864 2299
6865 2300 add_settings_field(
6866 - 'telegram_webhook_secret',
6867 - __('Webhook Secret Token', 'mxchat'),
6868 - array($this, 'mxchat_telegram_webhook_secret_callback'),
6869 - 'mxchat-embed',
6870 - 'mxchat_telegram_section'
2301 + 'toolbar_icon_color',
2302 + 'Toolbar Icon Color',
2303 + array($this, 'mxchat_toolbar_icon_color_callback'),
2304 + 'mxchat-theme',
2305 + 'mxchat_theme_section'
6871 2306 );
6872 2307
6873 2308 // General Settings Section
6874 2309 add_settings_section(
6875 2310 'mxchat_general_section',
6876 - esc_html__('YouTube Tutorials', 'mxchat'),
2311 + 'Frequently Asked Questions (FAQ)',
6877 2312 null,
6878 2313 'mxchat-general'
6879 2314 );
6880 -}
6881 2315
6882 -public function mxchat_prompts_page_init() {
6883 - register_setting(
6884 - 'mxchat_prompts_options',
6885 - 'mxchat_prompts_options',
6886 - array(
6887 - 'type' => 'array',
6888 - 'description' => __('MXChat Knowledge Base Settings', 'mxchat'),
6889 - 'default' => array(
6890 - 'mxchat_auto_sync_posts' => 0,
6891 - 'mxchat_auto_sync_pages' => 0,
6892 - 'mxchat_use_pinecone' => 0,
6893 - 'mxchat_pinecone_api_key' => '',
6894 - 'mxchat_pinecone_environment' => '',
6895 - 'mxchat_pinecone_index' => '',
6896 - 'mxchat_pinecone_host' => '',
6897 - ),
6898 - 'sanitize_callback' => array($this, 'sanitize_prompts_options'),
6899 - )
6900 - );
6901 2316
6902 - add_action('admin_notices', array($this, 'sync_settings_notice'));
6903 -}
6904 2317
6905 -public function mxchat_transcripts_page_init() {
6906 - register_setting(
6907 - 'mxchat_transcripts_options',
6908 - 'mxchat_transcripts_options',
6909 - array(
6910 - 'type' => 'array',
6911 - 'description' => __('MXChat Transcripts Notification Settings', 'mxchat'),
6912 - 'default' => array(
6913 - 'mxchat_enable_notifications' => 0,
6914 - 'mxchat_notification_email' => get_option('admin_email'),
6915 - 'mxchat_auto_delete_transcripts' => 'never',
6916 - ),
6917 - 'sanitize_callback' => array($this, 'sanitize_transcripts_options'),
6918 - )
6919 - );
6920 2318
6921 - add_settings_section(
6922 - 'mxchat_transcripts_notification_section',
6923 - esc_html__('Chat Notification Settings', 'mxchat'),
6924 - array($this, 'mxchat_transcripts_notification_section_callback'),
6925 - 'mxchat-transcripts'
6926 - );
6927 2319
6928 - add_settings_field(
6929 - 'mxchat_enable_notifications',
6930 - esc_html__('Enable Chat Notifications', 'mxchat'),
6931 - array($this, 'mxchat_enable_notifications_callback'),
6932 - 'mxchat-transcripts',
6933 - 'mxchat_transcripts_notification_section'
6934 - );
6935 2320
6936 - add_settings_field(
6937 - 'mxchat_notification_email',
6938 - esc_html__('Notification Email Address', 'mxchat'),
6939 - array($this, 'mxchat_notification_email_callback'),
6940 - 'mxchat-transcripts',
6941 - 'mxchat_transcripts_notification_section'
6942 - );
2321 +}
6943 2322
6944 - add_settings_field(
6945 - 'mxchat_auto_delete_transcripts',
6946 - esc_html__('Auto-Delete Old Transcripts', 'mxchat'),
6947 - array($this, 'mxchat_auto_delete_transcripts_callback'),
6948 - 'mxchat-transcripts',
6949 - 'mxchat_transcripts_notification_section'
6950 - );
2323 +public function mxchat_handle_activate_license() {
2324 + check_ajax_referer('mxchat_activate_license_nonce', 'security');
6951 2325
6952 - add_settings_field(
6953 - 'mxchat_retention_days',
6954 - esc_html__('Custom Retention (Days)', 'mxchat'),
6955 - array($this, 'mxchat_retention_days_callback'),
6956 - 'mxchat-transcripts',
6957 - 'mxchat_transcripts_notification_section'
6958 - );
2326 + $license_key = isset($_POST['mxchat_activation_key']) ? sanitize_text_field($_POST['mxchat_activation_key']) : '';
2327 + $customer_email = isset($_POST['mxchat_pro_email']) ? sanitize_email($_POST['mxchat_pro_email']) : '';
6959 2328
6960 - add_settings_field(
6961 - 'mxchat_auto_email_transcript',
6962 - esc_html__('Auto-Email Full Transcript', 'mxchat'),
6963 - array($this, 'mxchat_auto_email_transcript_callback'),
6964 - 'mxchat-transcripts',
6965 - 'mxchat_transcripts_notification_section'
6966 - );
6967 -}
2329 + if (empty($license_key) || empty($customer_email)) {
2330 + wp_send_json_error('Email or License Key is missing');
2331 + }
6968 2332
2333 + $product_id = 'MxChatPRO';
6969 2334
6970 -/**
6971 - * Sanitize all prompts options
6972 - *
6973 - * @param array $input The unsanitized options array
6974 - * @return array The sanitized options array
6975 - */
6976 -public function sanitize_prompts_options($input) {
6977 - // Log the incoming input.
6978 - //error_log('Sanitizing inputs: ' . print_r($input, true));
2335 + $response = wp_remote_get("http://mxchat.ai/?wc-api=software-api&request=activation&email={$customer_email}&license_key={$license_key}&product_id={$product_id}");
6979 2336
6980 - $sanitized = array();
2337 + if (is_wp_error($response)) {
2338 + wp_send_json_error('Activation failed due to a server error');
2339 + }
6981 2340
6982 - // Boolean options
6983 - $sanitized['mxchat_auto_sync_posts'] = isset($input['mxchat_auto_sync_posts']) ? 1 : 0;
6984 - $sanitized['mxchat_auto_sync_pages'] = isset($input['mxchat_auto_sync_pages']) ? 1 : 0;
6985 -$sanitized['mxchat_use_pinecone'] = !empty($input['mxchat_use_pinecone']) ? 1 : 0;
2341 + $body = wp_remote_retrieve_body($response);
2342 + $data = json_decode($body);
6986 2343
6987 - // API Key: if less than 32 characters, flag as invalid.
6988 - $api_key = sanitize_text_field($input['mxchat_pinecone_api_key'] ?? '');
6989 - if (!empty($api_key) && strlen($api_key) < 32) {
6990 - add_settings_error(
6991 - 'mxchat_prompts_options',
6992 - 'invalid_api_key',
6993 - __('The Pinecone API key appears to be invalid. Please check your API key.', 'mxchat')
6994 - );
6995 - $existing_options = get_option('mxchat_prompts_options', array());
6996 - $sanitized['mxchat_pinecone_api_key'] = $existing_options['mxchat_pinecone_api_key'] ?? '';
2344 + if ($data && isset($data->activated) && $data->activated) {
2345 + update_option('mxchat_license_status', 'active');
2346 + update_option('mxchat_pro_email', $customer_email);
2347 + update_option('mxchat_activation_key', $license_key);
2348 + wp_send_json_success();
6997 2349 } else {
6998 - $sanitized['mxchat_pinecone_api_key'] = $api_key;
2350 + $error_message = isset($data->error) ? $data->error : 'Activation failed';
2351 + update_option('mxchat_license_status', 'inactive');
2352 + update_option('mxchat_license_error', $error_message);
2353 + wp_send_json_error($error_message);
6999 2354 }
2355 +}
7000 2356
7001 - // Environment and Index Name
7002 - $sanitized['mxchat_pinecone_environment'] = sanitize_text_field($input['mxchat_pinecone_environment'] ?? '');
7003 - $sanitized['mxchat_pinecone_index'] = sanitize_text_field($input['mxchat_pinecone_index'] ?? '');
2357 +public function mxchat_rate_limit_logged_in_callback() {
2358 + $rate_limits = array('1', '3', '5', '10', '15', '20', '100', 'unlimited'); // Add 'unlimited' option
2359 + $selected_rate_limit = isset($this->options['rate_limit_logged_in']) ? $this->options['rate_limit_logged_in'] : '100';
7004 2360
7005 - // Host: Remove protocol and validate format.
7006 - $host = sanitize_text_field($input['mxchat_pinecone_host'] ?? '');
7007 - $host = preg_replace('#^https?://#', '', $host);
7008 - //error_log('Host after removing protocol: ' . $host);
7009 - if (!empty($host)) {
7010 - if (!preg_match('/^[\w-]+\.svc\.[\w-]+\.pinecone\.io$/', $host)) {
7011 - add_settings_error(
7012 - 'mxchat_prompts_options',
7013 - 'invalid_host',
7014 - __('The Pinecone host appears to be invalid. It should look like "mxchat-vectors-zrmsquq.svc.aped-4627-b74a.pinecone.io"', 'mxchat')
7015 - );
7016 - $existing_options = get_option('mxchat_prompts_options', array());
7017 - $sanitized['mxchat_pinecone_host'] = $existing_options['mxchat_pinecone_host'] ?? '';
7018 - } else {
7019 - $sanitized['mxchat_pinecone_host'] = $host;
7020 - }
7021 - } else {
7022 - $sanitized['mxchat_pinecone_host'] = '';
2361 + echo '<div class="pro-feature-wrapper active">';
2362 + echo '<select id="rate_limit_logged_in" name="mxchat_options[rate_limit_logged_in]">';
2363 + foreach ($rate_limits as $limit) {
2364 + echo '<option value="' . esc_attr($limit) . '" ' . selected($selected_rate_limit, $limit, false) . '>' . esc_html($limit) . '</option>';
7023 2365 }
7024 -
7025 - //error_log('Final sanitized array: ' . print_r($sanitized, true));
7026 -
7027 - return $sanitized;
2366 + echo '</select>';
2367 + echo '<p class="description">Set the maximum number of messages a logged-in user can send within a 24-hour period.</p>';
2368 + echo '</div>';
7028 2369 }
7029 2370
7030 -public function sync_settings_notice() {
7031 - // Only show notice on our plugin page
7032 - if (!isset($_GET['page']) || $_GET['page'] !== 'mxchat-prompts') {
7033 - return;
7034 - }
2371 +public function mxchat_rate_limit_logged_out_callback() {
2372 + $rate_limits = array('1', '3', '5', '10', '15', '20', '100', 'unlimited'); // Add 'unlimited' option
2373 + $selected_rate_limit = isset($this->options['rate_limit_logged_out']) ? $this->options['rate_limit_logged_out'] : '10';
7035 2374
7036 - // Check if settings were updated
7037 - if (isset($_GET['settings-updated'])) {
7038 -
7039 - ?>
7040 - <div class="notice notice-success is-dismissible">
7041 - <p><?php esc_html_e('Sync settings updated successfully.', 'mxchat'); ?></p>
7042 - <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e('Dismiss this notice.', 'mxchat'); ?></span></button>
7043 - </div>
7044 - <?php
7045 -
2375 + echo '<div class="pro-feature-wrapper active">';
2376 + echo '<select id="rate_limit_logged_out" name="mxchat_options[rate_limit_logged_out]">';
2377 + foreach ($rate_limits as $limit) {
2378 + echo '<option value="' . esc_attr($limit) . '" ' . selected($selected_rate_limit, $limit, false) . '>' . esc_html($limit) . '</option>';
7046 2379 }
2380 + echo '</select>';
2381 + echo '<p class="description">Set the maximum number of messages a logged-out user can send within a 24-hour period.</p>';
2382 + echo '</div>';
7047 2383 }
7048 -// Add this sanitization function to your class
7049 -public function sanitize_sync_setting($input) {
7050 - return (bool)$input ? __('1', 'mxchat') : __('', 'mxchat');
7051 -}
7052 2384
7053 -public function mxchat_rate_limits_callback() {
7054 - $all_options = get_option('mxchat_options', []);
7055 2385
7056 - // Define available rate limits
7057 - $rate_limits = array('1', '3', '5', '10', '15', '20', '50', '100', 'unlimited');
7058 -
7059 - // Define available timeframes
7060 - $timeframes = array(
7061 - 'hourly' => __('Per Hour', 'mxchat'),
7062 - 'daily' => __('Per Day', 'mxchat'),
7063 - 'weekly' => __('Per Week', 'mxchat'),
7064 - 'monthly' => __('Per Month', 'mxchat')
2386 +public function mxchat_rate_limit_message_callback() {
2387 + // Remove the is_activated check and make it always enabled
2388 + echo '<div class="pro-feature-wrapper active">';
2389 + printf(
2390 + '<textarea id="rate_limit_message" name="mxchat_options[rate_limit_message]" rows="3" cols="50">%s</textarea>',
2391 + isset($this->options['rate_limit_message']) ? esc_textarea($this->options['rate_limit_message']) : 'Rate limit exceeded. Please try again later.'
7065 2392 );
7066 -
7067 - // Get all roles plus a "logged_out" pseudo-role
7068 - $roles = wp_roles()->get_names();
7069 - $roles['logged_out'] = __('Logged Out Users', 'mxchat');
7070 -
7071 - // Start the wrapper
7072 - echo '<div class="pro-feature-wrapper active">';
7073 - echo '<div class="mxchat-rate-limits-container">';
7074 -
7075 - echo '<p class="description" style="margin-bottom: 20px;">' .
7076 - esc_html__('Set message limits for each user role and customize the experience when users reach those limits. You can use {limit}, {timeframe}, {count}, and {remaining} as placeholders.', 'mxchat') .
7077 - '</p>';
7078 -
7079 - // Add markdown link documentation
7080 - echo '<div class="notice notice-info inline" style="margin-bottom: 20px; padding: 10px;">';
7081 - echo '<p><strong>' . esc_html__('Markdown Links Supported:', 'mxchat') . '</strong></p>';
7082 - echo '<p>' . esc_html__('You can include clickable links in your custom messages using markdown syntax:', 'mxchat') . '</p>';
7083 - echo '<ul style="margin-left: 20px;">';
7084 - echo '<li><code>[Link text](https://example.com)</code> - Creates a clickable link</li>';
7085 - echo '<li><code>[Visit our pricing](https://example.com/pricing)</code> - Link with custom text</li>';
7086 - echo '<li><code>Plain URLs like https://example.com will also become clickable</code></li>';
7087 - echo '</ul>';
7088 2393 echo '</div>';
2394 +}
7089 2395
7090 - // Output the controls for each role
7091 - foreach ($roles as $role_id => $role_name) {
7092 - // Get saved options or defaults
7093 - $default_limit = ($role_id === 'logged_out') ? '10' : '100';
7094 - $default_timeframe = 'daily';
7095 - $default_message = __('Rate limit exceeded. Please try again later.', 'mxchat');
7096 2396
7097 - $selected_limit = isset($all_options['rate_limits'][$role_id]['limit'])
7098 - ? $all_options['rate_limits'][$role_id]['limit']
7099 - : $default_limit;
2397 +public function mxchat_enable_woocommerce_integration_callback() {
2398 + $checked = isset($this->options['enable_woocommerce_integration']) && $this->options['enable_woocommerce_integration'] === '1' ? 'checked' : '';
2399 + $disabled = $this->is_activated ? '' : 'disabled';
2400 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
7100 2401
7101 - $selected_timeframe = isset($all_options['rate_limits'][$role_id]['timeframe'])
7102 - ? $all_options['rate_limits'][$role_id]['timeframe']
7103 - : $default_timeframe;
2402 + echo '<div class="' . esc_attr($class) . '">';
2403 + echo '<label class="toggle-switch">';
2404 + echo '<input type="checkbox" id="enable_woocommerce_integration" name="mxchat_options[enable_woocommerce_integration]" value="1" ' . $checked . ' ' . $disabled . '>';
2405 + echo '<span class="slider"></span>';
2406 + echo '</label>';
7104 2407
7105 - $custom_message = isset($all_options['rate_limits'][$role_id]['message'])
7106 - ? $all_options['rate_limits'][$role_id]['message']
7107 - : $default_message;
2408 + echo '<p class="description">';
2409 + echo 'Automatically syncs new product additions, updates, and removals to the knowledge base. For existing products, submit your product sitemap in the Knowledge Base section to add them initially.';
2410 + echo '</p>';
7108 2411
7109 - // Output the row
7110 - echo '<div class="mxchat-rate-limit-row mxchat-autosave-section">';
7111 -
7112 - // Role label
7113 - echo '<div class="mxchat-rate-limit-role">' . esc_html($role_name) . '</div>';
7114 -
7115 - // Controls section
7116 - echo '<div class="mxchat-rate-limit-controls-wrapper">';
7117 -
7118 - // Rate limit and timeframe controls
7119 - echo '<div class="mxchat-rate-limit-controls">';
7120 -
7121 - // Limit dropdown
7122 - echo '<div>';
7123 - echo '<label for="rate_limits_' . esc_attr($role_id) . '_limit">' . esc_html__('Limit:', 'mxchat') . '</label>';
7124 - echo '<select
7125 - id="rate_limits_' . esc_attr($role_id) . '_limit"
7126 - name="mxchat_options[rate_limits][' . esc_attr($role_id) . '][limit]"
7127 - class="mxchat-autosave-field">';
7128 - foreach ($rate_limits as $limit) {
7129 - echo '<option value="' . esc_attr($limit) . '" ' . selected($selected_limit, $limit, false) . '>' . esc_html($limit) . '</option>';
7130 - }
7131 - echo '</select>';
2412 + if (!$this->is_activated) {
2413 + echo '<div class="pro-feature-overlay">';
2414 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
7132 2415 echo '</div>';
2416 + }
7133 2417
7134 - // Timeframe dropdown
7135 - echo '<div>';
7136 - echo '<label for="rate_limits_' . esc_attr($role_id) . '_timeframe">' . esc_html__('Timeframe:', 'mxchat') . '</label>';
7137 - echo '<select
7138 - id="rate_limits_' . esc_attr($role_id) . '_timeframe"
7139 - name="mxchat_options[rate_limits][' . esc_attr($role_id) . '][timeframe]"
7140 - class="mxchat-autosave-field">';
7141 - foreach ($timeframes as $value => $label) {
7142 - echo '<option value="' . esc_attr($value) . '" ' . selected($selected_timeframe, $value, false) . '>' . esc_html($label) . '</option>';
7143 - }
7144 - echo '</select>';
7145 - echo '</div>';
2418 + echo '</div>';
2419 +}
7146 2420
7147 - echo '</div>'; // End controls
7148 2421
7149 - // Custom message textarea
7150 - echo '<div class="mxchat-rate-limit-message">';
7151 - echo '<label for="rate_limits_' . esc_attr($role_id) . '_message">' . esc_html__('Custom Message:', 'mxchat') . '</label>';
7152 - echo '<textarea
7153 - id="rate_limits_' . esc_attr($role_id) . '_message"
7154 - name="mxchat_options[rate_limits][' . esc_attr($role_id) . '][message]"
7155 - class="mxchat-autosave-field"
7156 - placeholder="' . esc_attr__('Enter custom message when rate limit is exceeded', 'mxchat') . '">' .
7157 - esc_textarea($custom_message) .
7158 - '</textarea>';
7159 - echo '<p class="description">' .
7160 - esc_html__('Example: Rate limit reached! [Visit our pricing page](https://example.com/pricing) to upgrade.', 'mxchat') .
7161 - '</p>';
7162 - echo '</div>'; // End message
7163 2422
7164 - echo '</div>'; // End controls wrapper
7165 2423
7166 - echo '</div>'; // End row
7167 - }
7168 2424
7169 - echo '</div>'; // End container
7170 -
7171 - echo '</div>'; // End pro-feature-wrapper
7172 -}
7173 -
7174 -private function mxchat_add_option_field($id, $title, $callback = '') {
2425 + private function mxchat_add_option_field($id, $title, $callback = '') {
7175 2426 add_settings_field(
7176 2427 $id,
7177 - __($title, 'mxchat'),
2428 + $title,
7178 2429 $callback ? array($this, $callback) : array($this, $id . '_callback'),
7179 2430 'mxchat-max',
7180 2431 'mxchat_setting_section_id',
7181 2432 $id === 'model' ? ['label_for' => 'model'] : []
@@ -7181,1549 +2432,964 @@
7181 2432 $id === 'model' ? ['label_for' => 'model'] : []
7182 2433 );
7183 2434 }
7184 2435
7185 -// API Keys Section Callback
7186 -public function mxchat_api_keys_section_callback() {
7187 - echo '<p>' . esc_html__('Manage all your API keys in one place. Add the API keys for the services you want to use with your chatbot.', 'mxchat') . '</p>';
7188 -}
7189 -
7190 -// OpenAI API Key
7191 2436 public function api_key_callback() {
7192 2437 $apiKey = isset($this->options['api_key']) ? esc_attr($this->options['api_key']) : '';
7193 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
7194 -
7195 - echo '<div class="api-key-wrapper">';
7196 - echo '<input type="text" id="api_key" name="api_key" value="' . $apiKey . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7197 - echo '<button type="button" id="toggleApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7198 - echo '<p class="description">' . esc_html__('Required for OpenAI GPT models and OpenAI embeddings. Get your API key from OpenAI Platform.', 'mxchat') . '</p>';
7199 - echo $this->mxchat_provider_key_test_button('openai', 'api_key');
7200 - echo '</div>';
2438 + echo '<input type="password" id="api_key" name="mxchat_options[api_key]" value="' . $apiKey . '" class="regular-text" />';
2439 + echo '<button type="button" id="toggleApiKeyVisibility">Show</button>';
2440 + echo '<p class="description">The OpenAI API key is required even when using other models as it is used for vector embedding functionality.</p>';
7201 2441 }
7202 2442
7203 -// X.AI API Key
7204 -public function xai_api_key_callback() {
7205 - $xaiApiKey = isset($this->options['xai_api_key']) ? esc_attr($this->options['xai_api_key']) : '';
7206 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
7207 2443
7208 - echo '<div class="api-key-wrapper">';
7209 - echo '<input type="text" id="xai_api_key" name="xai_api_key" value="' . $xaiApiKey . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7210 - echo '<button type="button" id="toggleXaiApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7211 - echo '<p class="description">' . esc_html__('Required for X.AI Grok models. Get your API key from X.AI Console.', 'mxchat') . '</p>';
7212 - echo $this->mxchat_provider_key_test_button('xai', 'xai_api_key');
7213 - echo '</div>';
7214 -}
7215 -// Claude API Key
7216 -public function claude_api_key_callback() {
7217 - $claudeApiKey = isset($this->options['claude_api_key']) ? esc_attr($this->options['claude_api_key']) : '';
7218 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
2444 + public function xai_api_key_callback() {
2445 + // Check if the feature is activated (paid feature)
2446 + $disabled = $this->is_activated ? '' : 'disabled'; // Disable input if not activated
2447 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive'; // CSS class to style the wrapper based on activation status
7219 2448
7220 - echo '<div class="api-key-wrapper">';
7221 - echo '<input type="text" id="claude_api_key" name="claude_api_key" value="' . $claudeApiKey . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7222 - echo '<button type="button" id="toggleClaudeApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7223 - echo '<p class="description">' . esc_html__('Required for Anthropic Claude models. Get your API key from Anthropic Console.', 'mxchat') . '</p>';
7224 - echo $this->mxchat_provider_key_test_button('claude', 'claude_api_key');
7225 - echo '</div>';
7226 -}
2449 + // Render the input field for the X.AI API key
2450 + echo '<div class="' . esc_attr($class) . '">';
2451 + printf(
2452 + '<input type="password" id="xai_api_key" name="mxchat_options[xai_api_key]" value="%s" class="regular-text" %s />',
2453 + isset($this->options['xai_api_key']) ? esc_attr($this->options['xai_api_key']) : '',
2454 + $disabled
2455 + );
2456 + echo '<button type="button" id="toggleXaiApiKeyVisibility">Show</button>';
7227 2457
7228 -// DeepSeek API Key
7229 -public function deepseek_api_key_callback() {
7230 - $apiKey = isset($this->options['deepseek_api_key']) ? esc_attr($this->options['deepseek_api_key']) : '';
7231 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
2458 + // If the feature is not activated, show the overlay with a "Pro Only" message
2459 + if (!$this->is_activated) {
2460 + echo '<div class="pro-feature-overlay">';
2461 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2462 + echo '</div>';
2463 + }
7232 2464
7233 - echo '<div class="api-key-wrapper">';
7234 - echo '<input type="text" id="deepseek_api_key" name="deepseek_api_key" value="' . $apiKey . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7235 - echo '<button type="button" id="toggleDeepSeekApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7236 - echo '<p class="description">' . esc_html__('Required for DeepSeek models. Get your API key from DeepSeek Platform.', 'mxchat') . '</p>';
7237 - echo $this->mxchat_provider_key_test_button('deepseek', 'deepseek_api_key');
7238 - echo '</div>';
7239 -}
2465 + echo '</div>';
2466 + }
7240 2467
7241 -// Gemini API Key
7242 -public function gemini_api_key_callback() {
7243 - $geminiApiKey = isset($this->options['gemini_api_key']) ? esc_attr($this->options['gemini_api_key']) : '';
7244 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
2468 + public function claude_api_key_callback() {
2469 + // Check if the feature is activated (paid feature)
2470 + $disabled = $this->is_activated ? '' : 'disabled'; // Disable input if not activated
2471 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive'; // CSS class to style the wrapper based on activation status
7245 2472
7246 - echo '<div class="api-key-wrapper">';
7247 - echo '<input type="text" id="gemini_api_key" name="gemini_api_key" value="' . $geminiApiKey . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7248 - echo '<button type="button" id="toggleGeminiApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7249 - echo '<p class="description">' . esc_html__('Required for Google Gemini models and embeddings. Get your API key from Google AI Studio.', 'mxchat') . '</p>';
7250 - echo $this->mxchat_provider_key_test_button('gemini', 'gemini_api_key');
7251 - echo '</div>';
7252 -}
2473 + // Render the input field for the Claude API key
2474 + echo '<div class="' . esc_attr($class) . '">';
2475 + printf(
2476 + '<input type="password" id="claude_api_key" name="mxchat_options[claude_api_key]" value="%s" class="regular-text" %s />',
2477 + isset($this->options['claude_api_key']) ? esc_attr($this->options['claude_api_key']) : '',
2478 + $disabled
2479 + );
2480 + echo '<button type="button" id="toggleClaudeApiKeyVisibility">Show</button>';
7253 2481
2482 + // If the feature is not activated, show the overlay with a "Pro Only" message
2483 + if (!$this->is_activated) {
2484 + echo '<div class="pro-feature-overlay">';
2485 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2486 + echo '</div>';
2487 + }
7254 2488
7255 -// OpenRouter API Key
7256 -public function openrouter_api_key_callback() {
7257 - $openrouterApiKey = isset($this->options['openrouter_api_key']) ? esc_attr($this->options['openrouter_api_key']) : '';
7258 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
2489 + echo '</div>';
2490 + }
7259 2491
7260 - echo '<div class="api-key-wrapper">';
7261 - echo '<input type="text" id="openrouter_api_key" name="openrouter_api_key" value="' . $openrouterApiKey . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7262 - echo '<button type="button" id="toggleOpenRouterApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7263 - echo '<p class="description">' . esc_html__('Required for OpenRouter models. Get your API key from OpenRouter.ai', 'mxchat') . '</p>';
7264 - echo $this->mxchat_provider_key_test_button('openrouter', 'openrouter_api_key');
7265 - echo '</div>';
7266 -}
2492 + public function mxchat_woocommerce_consumer_key_callback() {
2493 + $disabled = $this->is_activated ? '' : 'disabled';
2494 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
7267 2495
7268 -/**
7269 - * Renders a "Test key" button + result target next to a built-in provider key
7270 - * field, and emits the shared delegated click handler ONCE (static guard). The
7271 - * button carries data-target = the key field id so the owner can test the value
7272 - * they just typed (test-before-save); the AJAX handler falls back to the saved
7273 - * key when the field is empty. Styled with the WP `button` class to match the
7274 - * adjacent Custom-provider "Test Connection" button on this same page.
7275 - * plan-mxchat-20260623-c41f74.
7276 - */
7277 -public function mxchat_provider_key_test_button($provider, $target_id) {
7278 - static $script_emitted = false;
7279 - $nonce = wp_create_nonce('mxchat_test_provider_key');
7280 - $html = '<div class="mxchat-key-test" style="margin-top:8px;">'
7281 - . '<button type="button" class="button mxchat-test-provider-key" data-provider="' . esc_attr($provider) . '" data-target="' . esc_attr($target_id) . '" data-nonce="' . esc_attr($nonce) . '">' . esc_html__('Test key', 'mxchat') . '</button>'
7282 - . '<span class="mxchat-test-provider-key-result" style="margin-left:10px;font-size:13px;vertical-align:middle;"></span>'
7283 - . '</div>';
7284 - if (!$script_emitted) {
7285 - $script_emitted = true;
7286 - $html .= $this->mxchat_provider_key_test_script();
7287 - }
7288 - return $html;
7289 -}
2496 + echo '<div class="' . esc_attr($class) . '">';
2497 + printf(
2498 + '<input type="text" id="woocommerce_consumer_key" name="mxchat_options[woocommerce_consumer_key]" value="%s" class="regular-text" %s />',
2499 + isset($this->options['woocommerce_consumer_key']) ? esc_attr($this->options['woocommerce_consumer_key']) : '',
2500 + $disabled
2501 + );
7290 2502
7291 -/**
7292 - * One-time delegated click handler shared by every .mxchat-test-provider-key
7293 - * button. Posts the typed key value + provider to mxchat_test_provider_key and
7294 - * renders the success/error message inline. Mirrors the Custom-provider test.
7295 - */
7296 -private function mxchat_provider_key_test_script() {
7297 - $t_testing = esc_js(__('Testing...', 'mxchat'));
7298 - $t_valid = esc_js(__('Key is valid.', 'mxchat'));
7299 - $t_failed = esc_js(__('Failed', 'mxchat'));
7300 - $t_req = esc_js(__('Request failed', 'mxchat'));
7301 - return '<script>(function(){'
7302 - . 'if (window.__mxchatProviderKeyTestWired) { return; }'
7303 - . 'window.__mxchatProviderKeyTestWired = true;'
7304 - . 'document.addEventListener("click", function(e){'
7305 - . 'var btn = e.target && e.target.closest ? e.target.closest(".mxchat-test-provider-key") : null;'
7306 - . 'if (!btn) { return; }'
7307 - . 'e.preventDefault();'
7308 - . 'var field = btn.getAttribute("data-target") ? document.getElementById(btn.getAttribute("data-target")) : null;'
7309 - . 'var out = btn.parentNode ? btn.parentNode.querySelector(".mxchat-test-provider-key-result") : null;'
7310 - . 'if (out) { out.textContent = "' . $t_testing . '"; out.style.color = "#646970"; }'
7311 - . 'btn.disabled = true;'
7312 - . 'var fd = new FormData();'
7313 - . 'fd.append("action", "mxchat_test_provider_key");'
7314 - . 'fd.append("_wpnonce", btn.getAttribute("data-nonce"));'
7315 - . 'fd.append("provider", btn.getAttribute("data-provider") || "");'
7316 - . 'fd.append("key", field ? field.value : "");'
7317 - . 'fetch(ajaxurl, { method:"POST", credentials:"same-origin", body: fd })'
7318 - . '.then(function(r){ return r.json(); })'
7319 - . '.then(function(j){'
7320 - . 'if (out) {'
7321 - . 'if (j && j.success) { out.textContent = "✓ " + ((j.data && j.data.message) ? j.data.message : "' . $t_valid . '"); out.style.color = "#00a32a"; }'
7322 - . 'else { out.textContent = "⚠ " + ((j && j.data && j.data.message) ? j.data.message : "' . $t_failed . '"); out.style.color = "#d63638"; }'
7323 - . '}'
7324 - . 'btn.disabled = false;'
7325 - . '})'
7326 - . '.catch(function(){ if (out) { out.textContent = "⚠ ' . $t_req . '"; out.style.color = "#d63638"; } btn.disabled = false; });'
7327 - . '});'
7328 - . '})();</script>';
7329 -}
2503 + if (!$this->is_activated) {
2504 + echo '<div class="pro-feature-overlay">';
2505 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2506 + echo '</div>';
2507 + }
7330 2508
7331 -// Custom (OpenAI-compatible) Provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.
7332 -public function custom_provider_callback() {
7333 - $base_url = isset($this->options['custom_provider_base_url']) ? esc_attr($this->options['custom_provider_base_url']) : '';
7334 - $api_key = isset($this->options['custom_provider_api_key']) ? esc_attr($this->options['custom_provider_api_key']) : '';
7335 - $model_name = isset($this->options['custom_provider_model']) ? esc_attr($this->options['custom_provider_model']) : '';
7336 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? esc_attr($this->options['custom_provider_auth_scheme']) : 'bearer';
7337 - $api_version = isset($this->options['custom_provider_api_version']) ? esc_attr($this->options['custom_provider_api_version']) : '';
7338 - $use_embed = !empty($this->options['custom_provider_for_embeddings']) && $this->options['custom_provider_for_embeddings'] === 'on';
7339 - $use_images = !empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on';
7340 - $embed_model = isset($this->options['custom_provider_embedding_model']) ? esc_attr($this->options['custom_provider_embedding_model']) : '';
7341 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
7342 - $test_nonce = wp_create_nonce('mxchat_test_custom_provider');
2509 + echo '</div>';
2510 + }
7343 2511
7344 - echo '<style>
7345 - .mxchat-cp { max-width: 680px; }
7346 - .mxchat-cp .mxchat-cp-intro { margin: 0 0 16px; color: #50575e; font-size: 13px; line-height: 1.5; }
7347 - .mxchat-cp .mxchat-cp-row { display: block; margin: 0 0 18px; }
7348 - .mxchat-cp .mxchat-cp-row > label { display: block; font-weight: 600; margin: 0 0 6px; color: #1d2327; font-size: 13px; }
7349 - .mxchat-cp .mxchat-cp-row > input[type="text"],
7350 - .mxchat-cp .mxchat-cp-row > input[type="password"],
7351 - .mxchat-cp .mxchat-cp-row > select { display: block; width: 100%; max-width: 480px; margin: 0; }
7352 - .mxchat-cp .mxchat-cp-row > .description { display: block; margin: 6px 0 0; color: #646970; font-size: 12px; line-height: 1.5; max-width: 480px; }
7353 - .mxchat-cp .mxchat-cp-test { margin-top: 4px; padding-top: 14px; border-top: 1px solid #e5e7eb; }
7354 - .mxchat-cp .mxchat-cp-test .mxchat-cp-test-status { display: inline-block; margin-left: 10px; vertical-align: middle; font-size: 13px; }
7355 - .mxchat-cp .mxchat-cp-azure { margin: 0 0 18px; padding: 12px 14px; background: #f6f7ff; border: 1px solid #dfe1f5; border-left: 3px solid #7873f5; border-radius: 6px; max-width: 480px; }
7356 - .mxchat-cp .mxchat-cp-azure > .mxchat-cp-azure-title { display: block; font-weight: 600; color: #1d2327; font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px; margin: 0 0 8px; }
7357 - .mxchat-cp .mxchat-cp-azure ol { margin: 0; padding: 0 0 0 18px; color: #50575e; font-size: 12px; line-height: 1.7; }
7358 - .mxchat-cp .mxchat-cp-azure code { background: #eceefb; padding: 1px 5px; border-radius: 3px; font-size: 11px; }
7359 - </style>';
2512 + public function mxchat_woocommerce_consumer_secret_callback() {
2513 + $disabled = $this->is_activated ? '' : 'disabled';
2514 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
7360 2515
7361 - echo '<div class="api-key-wrapper mxchat-cp">';
7362 - echo '<p class="mxchat-cp-intro">' . esc_html__('Point MxChat at any OpenAI-compatible /v1/chat/completions endpoint: Ollama, LM Studio, vLLM, llama.cpp, LocalAI, Azure OpenAI, etc. Then select "Custom (OpenAI-compatible)" in the model picker.', 'mxchat') . '</p>';
2516 + echo '<div class="' . esc_attr($class) . '">';
2517 + printf(
2518 + '<input type="password" id="woocommerce_consumer_secret" name="mxchat_options[woocommerce_consumer_secret]" value="%s" class="regular-text" %s />',
2519 + isset($this->options['woocommerce_consumer_secret']) ? esc_attr($this->options['woocommerce_consumer_secret']) : '',
2520 + $disabled
2521 + );
2522 + echo '<button type="button" id="toggleWooCommerceSecretVisibility">Show</button>';
7363 2523
7364 - // Azure OpenAI quick start — consolidates the 4-field Azure recipe in one scannable callout
7365 - // so admins can configure Azure without piecing it together from each field's hint.
7366 - echo '<div class="mxchat-cp-azure">';
7367 - echo '<span class="mxchat-cp-azure-title">' . esc_html__('Azure OpenAI quick start', 'mxchat') . '</span>';
7368 - echo '<ol>';
7369 - echo '<li>' . wp_kses(__('<strong>Base URL</strong> → <code>https://&lt;resource&gt;.openai.azure.com/openai/deployments/&lt;deployment&gt;</code>', 'mxchat'), array('strong' => array(), 'code' => array())) . '</li>';
7370 - echo '<li>' . wp_kses(__('<strong>API Key</strong> → your Azure OpenAI key (required)', 'mxchat'), array('strong' => array(), 'code' => array())) . '</li>';
7371 - echo '<li>' . wp_kses(__('<strong>Auth Scheme</strong> → <code>api-key header (Azure OpenAI)</code>', 'mxchat'), array('strong' => array(), 'code' => array())) . '</li>';
7372 - echo '<li>' . wp_kses(__('<strong>API Version</strong> → required for Azure, e.g. <code>2024-08-01-preview</code>', 'mxchat'), array('strong' => array(), 'code' => array())) . '</li>';
7373 - echo '</ol>';
7374 - echo '</div>';
2524 + if (!$this->is_activated) {
2525 + echo '<div class="pro-feature-overlay">';
2526 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2527 + echo '</div>';
2528 + }
7375 2529
7376 - echo '<div class="mxchat-cp-row">';
7377 - echo '<label for="custom_provider_base_url">' . esc_html__('Base URL', 'mxchat') . '</label>';
7378 - echo '<input type="text" id="custom_provider_base_url" name="custom_provider_base_url" value="' . $base_url . '" class="regular-text mxchat-autosave-field" placeholder="http://localhost:11434/v1" autocomplete="off" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7379 - echo '<p class="description">' . esc_html__('Examples: Ollama http://localhost:11434/v1 · LM Studio http://localhost:1234/v1 · vLLM http://gpu:8000/v1 · Azure https://<resource>.openai.azure.com/openai/deployments/<deployment>', 'mxchat') . '</p>';
7380 - echo '</div>';
2530 + echo '</div>';
2531 + }
7381 2532
7382 - echo '<div class="mxchat-cp-row">';
7383 - echo '<label for="custom_provider_api_key">' . esc_html__('API Key (optional)', 'mxchat') . '</label>';
7384 - echo '<input type="password" id="custom_provider_api_key" name="custom_provider_api_key" value="' . $api_key . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7385 - echo '<p class="description">' . esc_html__('Leave empty for unauthenticated local servers. Required for Azure / vLLM / hosted endpoints.', 'mxchat') . '</p>';
7386 - echo '</div>';
2533 + public function mxchat_loops_api_key_callback() {
2534 + $loops_api_key = isset($this->options['loops_api_key']) ? esc_attr($this->options['loops_api_key']) : '';
7387 2535
7388 - echo '<div class="mxchat-cp-row">';
7389 - echo '<label for="custom_provider_model">' . esc_html__('Model Name', 'mxchat') . '</label>';
7390 - echo '<input type="text" id="custom_provider_model" name="custom_provider_model" value="' . $model_name . '" class="regular-text mxchat-autosave-field" placeholder="llama3.2" autocomplete="off" data-lpignore="true" data-nonce="' . $nonce . '" />';
7391 - echo '<p class="description">' . esc_html__('The model identifier the upstream server expects (e.g. llama3.2, mistral, gpt-oss). For Azure this is the deployment ID — leave empty if the Base URL already includes /deployments/<deployment>.', 'mxchat') . '</p>';
7392 - echo '</div>';
2536 + echo '<div class="api-key-wrapper">';
2537 + printf(
2538 + '<input type="password" id="loops_api_key" name="mxchat_options[loops_api_key]" value="%s" class="regular-text" />',
2539 + $loops_api_key
2540 + );
2541 + echo '<button type="button" id="toggleLoopsApiKeyVisibility">Show</button>';
2542 + echo '</div>';
2543 + echo '<p class="description">Enter your Loops API Key here. (See FAQ for details)</p>';
2544 + }
7393 2545
7394 - echo '<div class="mxchat-cp-row">';
7395 - echo '<label for="custom_provider_auth_scheme">' . esc_html__('Auth Scheme', 'mxchat') . '</label>';
7396 - echo '<select id="custom_provider_auth_scheme" name="custom_provider_auth_scheme" class="mxchat-autosave-field" data-nonce="' . $nonce . '">';
7397 - echo '<option value="bearer"' . selected($auth_scheme, 'bearer', false) . '>' . esc_html__('Authorization: Bearer (OpenAI / Ollama / vLLM / LM Studio)', 'mxchat') . '</option>';
7398 - echo '<option value="api-key"' . selected($auth_scheme, 'api-key', false) . '>' . esc_html__('api-key header (Azure OpenAI)', 'mxchat') . '</option>';
7399 - echo '</select>';
7400 - echo '<p class="description">' . esc_html__('Most OpenAI-compatible servers use Bearer. Azure OpenAI uses the api-key header.', 'mxchat') . '</p>';
7401 - echo '</div>';
2546 + public function mxchat_loops_mailing_list_callback() {
2547 + // Retrieve Loops API key and lists
2548 + $loops_api_key = isset($this->options['loops_api_key']) ? $this->options['loops_api_key'] : '';
2549 + $selected_list = isset($this->options['loops_mailing_list']) ? $this->options['loops_mailing_list'] : '';
7402 2550
7403 - echo '<div class="mxchat-cp-row">';
7404 - echo '<label for="custom_provider_api_version">' . esc_html__('API Version (Azure only)', 'mxchat') . '</label>';
7405 - echo '<input type="text" id="custom_provider_api_version" name="custom_provider_api_version" value="' . $api_version . '" class="regular-text mxchat-autosave-field" placeholder="2024-08-01-preview" autocomplete="off" data-lpignore="true" data-nonce="' . $nonce . '" />';
7406 - echo '<p class="description">' . esc_html__('Appended as ?api-version=... on the request URL. Required for Azure OpenAI; leave empty for non-Azure providers.', 'mxchat') . '</p>';
7407 - echo '</div>';
2551 + if ($loops_api_key) {
2552 + // Fetch lists from Loops API
2553 + $lists = $this->mxchat_fetch_loops_mailing_lists($loops_api_key);
7408 2554
7409 - // Extended-use checkboxes — opt-in routing of other dispatcher paths through the custom provider.
7410 - echo '<div class="mxchat-cp-row">';
7411 - echo '<label style="font-weight:600; display:block; margin:0 0 6px; color:#1d2327; font-size:13px;">' . esc_html__('Extended routing (opt-in)', 'mxchat') . '</label>';
7412 - echo '<label style="display:block; margin:0 0 6px; font-weight:400;"><input type="checkbox" id="custom_provider_for_embeddings" name="custom_provider_for_embeddings" value="on"' . checked($use_embed, true, false) . ' class="mxchat-autosave-field" data-nonce="' . $nonce . '" /> ' . esc_html__('Use custom provider for embeddings', 'mxchat') . '</label>';
7413 - echo '<label style="display:block; margin:0 0 0; font-weight:400;"><input type="checkbox" id="custom_provider_for_images" name="custom_provider_for_images" value="on"' . checked($use_images, true, false) . ' class="mxchat-autosave-field" data-nonce="' . $nonce . '" /> ' . esc_html__('Use custom provider for image generation', 'mxchat') . '</label>';
7414 - echo '<p class="description">' . esc_html__('When off, embeddings and image generation continue to use OpenAI (current behavior). Turn on only if your endpoint exposes OpenAI-compatible /embeddings or /images/generations routes (e.g. Ollama, vLLM, LocalAI).', 'mxchat') . '</p>';
7415 - echo '</div>';
2555 + if ($lists) {
2556 + echo '<select id="loops_mailing_list" name="mxchat_options[loops_mailing_list]">';
2557 + foreach ($lists as $list) {
2558 + echo '<option value="' . esc_attr($list['id']) . '" ' . selected($selected_list, $list['id'], false) . '>' . esc_html($list['name']) . '</option>';
2559 + }
2560 + echo '</select>';
2561 + } else {
2562 + echo '<p class="description">No lists found. Please verify your API Key.</p>';
2563 + }
2564 + } else {
2565 + echo '<p class="description">Enter a valid Loops API Key to load mailing lists.</p>';
2566 + }
2567 + }
7416 2568
7417 - echo '<div class="mxchat-cp-row">';
7418 - echo '<label for="custom_provider_embedding_model">' . esc_html__('Custom Embedding Model', 'mxchat') . '</label>';
7419 - echo '<input type="text" id="custom_provider_embedding_model" name="custom_provider_embedding_model" value="' . $embed_model . '" class="regular-text mxchat-autosave-field" placeholder="nomic-embed-text" autocomplete="off" data-lpignore="true" data-nonce="' . $nonce . '" />';
7420 - echo '<p class="description">' . esc_html__('Only used when "Use custom provider for embeddings" is on. The embedding model name is separate from the chat model name above (e.g. Ollama embedding models: nomic-embed-text, mxbai-embed-large). Leave blank to fall back to the chat model name.', 'mxchat') . '</p>';
7421 - echo '</div>';
2569 + public function mxchat_triggered_phrase_response_callback() {
2570 + $triggered_response = isset($this->options['triggered_phrase_response']) ? $this->options['triggered_phrase_response'] : '';
2571 + echo '<textarea id="triggered_phrase_response" name="mxchat_options[triggered_phrase_response]" rows="3" cols="50">' . esc_textarea($triggered_response) . '</textarea>';
2572 + echo '<p class="description">Enter the chatbot response when a trigger keyword is detected, prompting the user to share their email.</p>';
2573 + }
7422 2574
7423 - echo '<div class="mxchat-cp-test">';
7424 - echo '<button type="button" class="button" id="mxchat-test-custom-provider" data-nonce="' . $test_nonce . '">' . esc_html__('Test Connection', 'mxchat') . '</button>';
7425 - echo '<span id="mxchat-test-custom-provider-result" class="mxchat-cp-test-status"></span>';
7426 - echo '</div>';
2575 + public function mxchat_email_capture_response_callback() {
2576 + $email_capture_response = isset($this->options['email_capture_response']) ? $this->options['email_capture_response'] : 'Thank you for providing your email! You\'ve been added to our list.';
2577 + echo '<textarea id="email_capture_response" name="mxchat_options[email_capture_response]" rows="3" cols="50">' . esc_textarea($email_capture_response) . '</textarea>';
2578 + echo '<p class="description">Enter the message to send when a user provides their email.</p>';
2579 + }
7427 2580
7428 - echo '<script>(function(){
7429 - var btn = document.getElementById("mxchat-test-custom-provider");
7430 - if (!btn || btn._wired) { return; } btn._wired = true;
7431 - btn.addEventListener("click", function(){
7432 - var out = document.getElementById("mxchat-test-custom-provider-result");
7433 - out.textContent = "' . esc_js(__('Testing...', 'mxchat')) . '";
7434 - out.style.color = "#646970";
7435 - var fd = new FormData();
7436 - fd.append("action", "mxchat_test_custom_provider");
7437 - fd.append("_wpnonce", btn.getAttribute("data-nonce"));
7438 - fetch(ajaxurl, { method:"POST", credentials:"same-origin", body: fd })
7439 - .then(function(r){ return r.json(); })
7440 - .then(function(j){
7441 - if (j && j.success) {
7442 - out.textContent = "✓ " + (j.data && j.data.message ? j.data.message : "' . esc_js(__('OK', 'mxchat')) . '");
7443 - out.style.color = "#00a32a";
7444 - } else {
7445 - out.textContent = "⚠ " + (j && j.data && j.data.message ? j.data.message : "' . esc_js(__('Failed', 'mxchat')) . '");
7446 - out.style.color = "#d63638";
7447 - }
7448 - })
7449 - .catch(function(){
7450 - out.textContent = "⚠ ' . esc_js(__('Request failed', 'mxchat')) . '";
7451 - out.style.color = "#d63638";
7452 - });
7453 - });
7454 - })();</script>';
7455 2581
7456 - echo '</div>';
7457 -}
2582 + public function mxchat_pre_chat_message_callback() {
2583 + printf(
2584 + '<textarea id="pre_chat_message" name="mxchat_options[pre_chat_message]" rows="5" cols="50">%s</textarea>',
2585 + isset($this->options['pre_chat_message']) ? esc_textarea($this->options['pre_chat_message']) : ''
2586 + );
2587 + }
7458 2588
7459 -// Voyage API Key
7460 -public function voyage_api_key_callback() {
7461 - $apiKey = isset($this->options['voyage_api_key']) ? esc_attr($this->options['voyage_api_key']) : '';
7462 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
2589 + // Callback for AI Instructions textarea
2590 + public function system_prompt_instructions_callback() {
2591 + printf(
2592 + '<textarea id="system_prompt_instructions" name="mxchat_options[system_prompt_instructions]" rows="5" cols="50">%s</textarea>',
2593 + isset($this->options['system_prompt_instructions']) ? esc_textarea($this->options['system_prompt_instructions']) : ''
2594 + );
2595 + }
7463 2596
7464 - echo '<div class="api-key-wrapper">';
7465 - echo '<input type="text" id="voyage_api_key" name="voyage_api_key" value="' . $apiKey . '" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="' . $nonce . '" />';
7466 - echo '<button type="button" id="toggleVoyageAPIKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7467 - echo '<p class="description">' . esc_html__('Required for Voyage AI embedding models. Get your API key from Voyage AI.', 'mxchat') . '</p>';
7468 - echo '</div>';
7469 -}
7470 -
7471 -public function mxchat_loops_api_key_callback() {
7472 - $loops_api_key = isset($this->options['loops_api_key']) ? esc_attr($this->options['loops_api_key']) : '';
7473 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
7474 -
7475 - echo '<div class="api-key-wrapper">';
7476 - echo sprintf(
7477 - '<input type="text" id="loops_api_key" name="loops_api_key" value="%s" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="%s" />',
7478 - $loops_api_key,
7479 - $nonce
2597 +public function mxchat_model_callback() {
2598 + $models = array(
2599 + 'X.AI Models' => array(
2600 + 'grok-beta' => 'grok-beta (Early Beta)'
2601 + ),
2602 + 'Claude Models' => array(
2603 + 'claude-3-5-sonnet-20241022' => 'Claude 3.5 Sonnet (Most Intelligent)',
2604 + 'claude-3-opus-20240229' => 'Claude 3 Opus (Highly Complex Tasks)',
2605 + 'claude-3-sonnet-20240229' => 'Claude 3 Sonnet (Balanced)',
2606 + 'claude-3-haiku-20240307' => 'Claude 3 Haiku (Fastest)'
2607 + ),
2608 + 'OpenAI Models' => array(
2609 + 'gpt-4o' => 'GPT-4o (Recommended)',
2610 + 'gpt-4o-mini' => 'GPT-4o Mini (Fast and Lightweight)',
2611 + 'gpt-4-turbo' => 'GPT-4 Turbo (High-Performance)',
2612 + 'gpt-4' => 'GPT-4 (High Intelligence)',
2613 + 'gpt-3.5-turbo' => 'GPT-3.5 Turbo (Affordable and Fast)'
2614 + )
7480 2615 );
7481 - echo '<button type="button" id="toggleLoopsApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
7482 - echo '</div>';
7483 - // Cross-reference back to where the list is chosen, so the two screens point
7484 - // at each other (plan-mxchat-20260802-907a63).
7485 - echo '<p class="description">' . wp_kses(
7486 - sprintf(
7487 - /* translators: %s: link to the Loops integration tab. */
7488 - __('Required for Loops email integration. Get your API key from Loops.so, then choose your mailing list under %s.', 'mxchat'),
7489 - '<a href="#integrations-loops" data-target="integrations-loops">' . esc_html__('Integrations, Loops', 'mxchat') . '</a>'
7490 - ),
7491 - self::mxchat_loops_pointer_allowed_html()
7492 - ) . '</p>';
7493 -}
7494 -public function mxchat_loops_mailing_list_callback() {
7495 - // Add error handling and type checking
7496 - $loops_api_key = '';
7497 - $selected_list = '';
7498 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
7499 2616
7500 - // Safely get the API key
7501 - if (isset($this->options['loops_api_key']) && is_string($this->options['loops_api_key'])) {
7502 - $loops_api_key = $this->options['loops_api_key'];
7503 - }
2617 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-3.5-turbo';
7504 2618
7505 - // Safely get the selected list
7506 - if (isset($this->options['loops_mailing_list']) && is_string($this->options['loops_mailing_list'])) {
7507 - $selected_list = $this->options['loops_mailing_list'];
7508 - }
2619 + echo '<select id="model" name="mxchat_options[model]">';
7509 2620
7510 - if (!empty($loops_api_key)) {
7511 - $lists = $this->mxchat_fetch_loops_mailing_lists($loops_api_key);
7512 - if (is_array($lists) && !empty($lists)) {
7513 - echo '<div class="mxchat-field-wrapper">';
7514 - echo '<select id="loops_mailing_list" name="loops_mailing_list" class="mxchat-autosave-field" data-nonce="' . $nonce . '">';
2621 + foreach ($models as $group_label => $group_models) {
2622 + echo '<optgroup label="' . esc_attr($group_label) . '">';
7515 2623
7516 - // Add a default "Select a list" option
7517 - echo '<option value="" ' . selected($selected_list, '', false) . '>' . esc_html__('Select a list', 'mxchat') . '</option>';
2624 + foreach ($group_models as $model_value => $model_label) {
2625 + // Disable if not activated for paid models (Claude or X.AI)
2626 + $disabled = (!$this->is_activated && ($group_label === 'X.AI Models' || $group_label === 'Claude Models')) ? 'disabled' : '';
2627 + $label_suffix = (!$this->is_activated && ($group_label === 'X.AI Models' || $group_label === 'Claude Models')) ? ' (Pro Only)' : '';
7518 2628
7519 - foreach ($lists as $list) {
7520 - if (is_array($list) && isset($list['id']) && isset($list['name'])) {
7521 - echo sprintf(
7522 - '<option value="%s" %s>%s</option>',
7523 - esc_attr($list['id']),
7524 - selected($selected_list, $list['id'], false),
7525 - esc_html($list['name'])
7526 - );
7527 - }
7528 - }
7529 - echo '</select>';
7530 - echo '</div>';
7531 - echo '<p class="description">' . esc_html__('Please select a mailing list to use with Loops.', 'mxchat') . '</p>';
7532 - } else {
7533 - echo '<p class="description">' . wp_kses(
7534 - self::mxchat_loops_api_key_pointer(
7535 - /* translators: %s: link to the API Keys tab. */
7536 - __('No lists found. Please check your Loops API key under %s.', 'mxchat')
7537 - ),
7538 - self::mxchat_loops_pointer_allowed_html()
7539 - ) . '</p>';
2629 + // Output the <option> element
2630 + echo '<option value="' . esc_attr($model_value) . '" ' . selected($selected_model, $model_value, false) . ' ' . $disabled . '>' . esc_html($model_label . $label_suffix) . '</option>';
7540 2631 }
7541 - } else {
7542 - echo '<p class="description">' . wp_kses(
7543 - self::mxchat_loops_api_key_pointer(
7544 - /* translators: %s: link to the API Keys tab. */
7545 - __('Enter your Loops API key under %s to load mailing lists.', 'mxchat')
7546 - ),
7547 - self::mxchat_loops_pointer_allowed_html()
7548 - ) . '</p>';
2632 +
2633 + echo '</optgroup>';
7549 2634 }
7550 -}
7551 2635
7552 -/**
7553 - * Build the "API Keys" pointer used by the Loops mailing-list field.
7554 - *
7555 - * plan-mxchat-20260802-907a63. The Loops API key field was moved to the API
7556 - * Keys tab, but this field's copy still read as though the input were beside
7557 - * it, so users had nowhere to go. Both live on the SAME screen
7558 - * (admin.php?page=mxchat-settings) — the key is under the API Keys tab, this
7559 - * dropdown under Integrations > Loops — so the pointer is an in-page tab
7560 - * switch, not a cross-page link.
7561 - *
7562 - * The anchor carries data-target="api-keys", which the shared admin shell
7563 - * (js/admin-sidebar.js) already wires for EVERY [data-target] element inside
7564 - * .mxch-admin-wrapper — so this actually switches tabs with no new JS. A plain
7565 - * href="#api-keys" would NOT work: the shell has no hashchange handling, so the
7566 - * link would look right and land the user on the wrong tab.
7567 - *
7568 - * @param string $template Translatable string containing one %s placeholder.
7569 - * @return string
7570 - */
7571 -private static function mxchat_loops_api_key_pointer($template) {
7572 - return sprintf(
7573 - $template,
7574 - '<a href="#api-keys" data-target="api-keys">' . esc_html__('API Keys', 'mxchat') . '</a>'
7575 - );
2636 + echo '</select>';
7576 2637 }
7577 2638
7578 -/**
7579 - * Allowed HTML for the Loops pointer — anchor plus the shell's tab-switch hook.
7580 - */
7581 -private static function mxchat_loops_pointer_allowed_html() {
7582 - return array(
7583 - 'a' => array(
7584 - 'href' => array(),
7585 - 'data-target' => array(),
7586 - ),
7587 - );
7588 -}
7589 -public function mxchat_triggered_phrase_response_callback() {
7590 - $default_response = __('Would you like to join our mailing list? Please provide your email below.', 'mxchat');
7591 - $triggered_response = isset($this->options['triggered_phrase_response'])
7592 - ? $this->options['triggered_phrase_response']
7593 - : $default_response;
7594 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
7595 2639
7596 - echo '<div class="mxchat-field-wrapper">';
7597 - echo sprintf(
7598 - '<textarea id="triggered_phrase_response" name="triggered_phrase_response" rows="3" cols="50" class="mxchat-autosave-field" data-nonce="%s">%s</textarea>',
7599 - $nonce,
7600 - esc_textarea($triggered_response)
7601 - );
7602 - echo '</div>';
7603 - echo '<p class="description">' . esc_html__('Enter the instruction for the AI when a trigger keyword is detected. The AI will use this as guidance to naturally ask for the user\'s email in a conversational way.', 'mxchat') . '</p>';
7604 -}
7605 2640
7606 -public function mxchat_email_capture_response_callback() {
7607 - $default_response = __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
7608 - $email_capture_response = isset($this->options['email_capture_response'])
7609 - ? $this->options['email_capture_response']
7610 - : $default_response;
7611 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
2641 + public function mxchat_top_bar_title_callback() {
2642 + printf(
2643 + '<input type="text" id="top_bar_title" name="mxchat_options[top_bar_title]" value="%s" />',
2644 + isset($this->options['top_bar_title']) ? esc_attr($this->options['top_bar_title']) : ''
2645 + );
2646 + }
7612 2647
7613 - echo '<div class="mxchat-field-wrapper">';
7614 - echo sprintf(
7615 - '<textarea id="email_capture_response" name="email_capture_response" rows="3" cols="50" class="mxchat-autosave-field" data-nonce="%s">%s</textarea>',
7616 - $nonce,
7617 - esc_textarea($email_capture_response)
7618 - );
7619 - echo '</div>';
7620 - echo '<p class="description">' . esc_html__('Enter the instruction for the AI when a user provides their email. The AI will use this as guidance to naturally confirm the email capture in a conversational way.', 'mxchat') . '</p>';
7621 -}
7622 -public function mxchat_pre_chat_message_callback() {
7623 - // Load the entire 'mxchat_options' array
7624 - $all_options = get_option('mxchat_options', []);
7625 -
7626 - // Retrieve the saved message or use the default value
7627 - $default_message = __('Hey there! Ask me anything!', 'mxchat');
7628 - $pre_chat_message = isset($all_options['pre_chat_message']) ? $all_options['pre_chat_message'] : $default_message;
7629 -
7630 - // Output the textarea
2648 +public function enable_email_block_callback() {
2649 + $email_block_checked = isset($this->options['enable_email_block']) && $this->options['enable_email_block'] === '1' ? 'checked' : '';
2650 + echo '<label class="toggle-switch">';
7631 2651 printf(
7632 - '<textarea id="pre_chat_message" name="pre_chat_message" rows="5" cols="50">%s</textarea>',
7633 - esc_textarea($pre_chat_message)
2652 + '<input type="checkbox" id="enable_email_block" name="mxchat_options[enable_email_block]" value="1" %s />',
2653 + esc_attr($email_block_checked)
7634 2654 );
2655 + echo '<span class="slider"></span>';
2656 + echo '</label>';
2657 + echo '<p class="description">Enable to require email before a user chats. Their email will appear at the top of transcript.</p>';
7635 2658 }
7636 2659
7637 -// Callback for AI Instructions textarea
7638 -public function system_prompt_instructions_callback() {
7639 - // Retrieve the current value of the system prompt instructions
7640 - $instructions = isset($this->options['system_prompt_instructions']) ? esc_textarea($this->options['system_prompt_instructions']) : '';
7641 - // Render the textarea field
7642 - printf(
7643 - '<textarea id="system_prompt_instructions" name="system_prompt_instructions" rows="5" cols="50">%s</textarea>',
7644 - $instructions
7645 - );
7646 - // Personalization hint
7647 - echo '<p class="description" style="margin-top: 8px;">';
7648 - echo esc_html__('Use {visitor_name} to personalize AI responses when lead capture is enabled.', 'mxchat') . '<br>';
7649 - echo '<code style="font-size: 12px;">' . esc_html__('Example: The visitor\'s name is {visitor_name}. Address them by name.', 'mxchat') . '</code>';
7650 - echo '</p>';
7651 - // Sample instructions button
7652 - echo '<div class="mxchat-instructions-container">';
7653 - echo '<button type="button" class="mxchat-instructions-btn" id="mxchatViewSampleBtn">';
7654 - echo '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">';
7655 - echo '<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/>';
7656 - echo '<path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/>';
7657 - echo '</svg>';
7658 - echo esc_html__('View Sample Instructions', 'mxchat');
7659 - echo '</button>';
7660 - echo '</div>';
2660 + public function email_blocker_header_content_callback() {
2661 + // Grab the current stored value or default to empty
2662 + $content = isset($this->options['email_blocker_header_content'])
2663 + ? $this->options['email_blocker_header_content']
2664 + : '';
7661 2665
7662 - // Add modal to WordPress admin footer instead of inline
7663 - add_action('admin_footer', array($this, 'render_sample_instructions_modal'));
7664 -}
2666 + // Use esc_textarea to safely escape existing content in the <textarea>
2667 + printf(
2668 + '<textarea
2669 + id="email_blocker_header_content"
2670 + name="mxchat_options[email_blocker_header_content]"
2671 + rows="5"
2672 + cols="70"
2673 + >%s</textarea>',
2674 + esc_textarea($content)
2675 + );
7665 2676
7666 -// New method to render modal in admin footer
7667 -public function render_sample_instructions_modal() {
7668 - static $modal_rendered = false;
7669 - if ($modal_rendered) return; // Prevent duplicate modals
7670 - $modal_rendered = true;
2677 + echo '<p class="description">';
2678 + echo 'You may enter HTML here, such as &lt;h2&gt;Welcome&lt;/h2&gt; or &lt;p&gt;Let\'s get started&lt;/p&gt;. Enter your email to begin chatting ';
2679 + echo 'Only allowed tags (like &lt;p&gt;, &lt;h2&gt;, &lt;strong&gt;, etc.) will be kept.';
2680 + echo '</p>';
2681 + }
7671 2682
7672 - echo '<div class="mxchat-instructions-modal-overlay" id="mxchatSampleModal">';
7673 - echo '<div class="mxchat-instructions-modal-content">';
7674 - echo '<div class="mxchat-instructions-modal-header">';
7675 - echo '<h3 class="mxchat-instructions-modal-title">';
7676 - echo '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">';
7677 - echo '<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/>';
7678 - echo '<path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/>';
7679 - echo '</svg>';
7680 - echo esc_html__('Sample AI Instructions', 'mxchat');
7681 - echo '</h3>';
7682 - echo '<button type="button" class="mxchat-instructions-modal-close" id="mxchatModalClose">';
7683 - echo '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">';
7684 - echo '<line x1="18" y1="6" x2="6" y2="18"/>';
7685 - echo '<line x1="6" y1="6" x2="18" y2="18"/>';
7686 - echo '</svg>';
7687 - echo '</button>';
7688 - echo '</div>';
7689 - echo '<div class="mxchat-instructions-modal-body">';
7690 - echo '<div class="mxchat-instructions-content">';
7691 - echo esc_html('You are an AI Chatbot assistant for this website. Your main goal is to assist visitors with questions and provide helpful information. Here are your key guidelines:
2683 + public function email_blocker_button_text_callback() {
2684 + // Retrieve current value or empty if not set
2685 + $button_text = isset($this->options['email_blocker_button_text'])
2686 + ? $this->options['email_blocker_button_text']
2687 + : '';
7692 2688
7693 -# Response Style - CRITICALLY IMPORTANT
7694 -- MAXIMUM LENGTH: 1-3 short sentences per response
7695 -- Ultra-concise: Get straight to the answer with no filler
7696 -- No introductions like "Sure!" or "I\'d be happy to help"
7697 -- No phrases like "based on my knowledge" or "according to information"
7698 -- No explanatory text before giving the answer
7699 -- No summaries or repetition
7700 -- Hyperlink all URLs
7701 -- Respond in user\'s language
7702 -- Minor chit chat or conversation is okay, but try to keep it focused on [insert topic]
2689 + // Use esc_attr to safely render the existing text
2690 + printf(
2691 + '<input type="text" id="email_blocker_button_text" name="mxchat_options[email_blocker_button_text]" value="%s" style="width: 300px;" />',
2692 + esc_attr($button_text)
2693 + );
7703 2694
7704 -# Knowledge Base Requirements - PREVENT HALLUCINATIONS
7705 -- ONLY answer using information explicitly provided in OFFICIAL KNOWLEDGE DATABASE CONTENT sections marked with ===== delimiters
7706 -- If required information is NOT in the knowledge database: "I don\'t have enough information in my knowledge base to answer that question accurately."
7707 -- NEVER invent or hallucinate URLs, links, product specs, procedures, dates, statistics, names, contacts, or company information
7708 -- When knowledge base information is unclear or contradictory, acknowledge the limitation rather than guessing
7709 -- Better to admit insufficient information than provide inaccurate answers');
7710 - echo '</div>';
7711 - echo '<button type="button" class="mxchat-instructions-copy-btn" id="mxchatCopyBtn">';
7712 - echo '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">';
7713 - echo '<rect x="9" y="9" width="13" height="13" rx="2" ry="2"/>';
7714 - echo '<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>';
7715 - echo '</svg>';
7716 - echo esc_html__('Copy Instructions', 'mxchat');
7717 - echo '</button>';
7718 - echo '</div>';
7719 - echo '<div class="mxchat-instructions-modal-footer">';
7720 - echo '<button type="button" class="mxchat-instructions-btn-secondary" id="mxchatCloseBtn">' . esc_html__('Close', 'mxchat') . '</button>';
7721 - echo '</div>';
7722 - echo '</div>';
7723 - echo '</div>';
7724 -}
2695 + echo '<p class="description">';
2696 + echo 'Enter the text you want on the submit button, e.g. "Start Chat".';
2697 + echo '</p>';
2698 + }
7725 2699
2700 + public function mxchat_intro_message_callback() {
2701 + printf(
2702 + '<textarea id="intro_message" name="mxchat_options[intro_message]" rows="5" cols="50">%s</textarea>',
2703 + isset($this->options['intro_message']) ? esc_textarea($this->options['intro_message']) : 'Hello! How can I assist you today?'
2704 + );
2705 + }
7726 2706
7727 -public function mxchat_model_callback() {
7728 - // Catalog refactor (plan-d14e89): single source of truth lives in
7729 - // includes/class-mxchat-model-catalog.php. Dropdown groups are the
7730 - // provider labels; each group maps model_id => "Label" strings.
7731 - if (!class_exists('MxChat_Model_Catalog')) {
7732 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-model-catalog.php';
2707 + public function mxchat_input_copy_callback() {
2708 + printf(
2709 + '<input type="text" id="input_copy" name="mxchat_options[input_copy]" value="%s" placeholder="How can I assist?" />',
2710 + isset($this->options['input_copy']) ? esc_attr($this->options['input_copy']) : 'How can I assist?'
2711 + );
2712 + echo '<p class="description">This is the placeholder text for the chat input field.</p>';
7733 2713 }
7734 - // Retrieve the currently selected model from saved options
7735 - $selected_model = isset($this->options['model']) ? esc_attr($this->options['model']) : 'gpt-5.6-sol';
7736 2714
7737 - // Pass the saved model so a provider-retired id keeps rendering as the
7738 - // current selection instead of a blank select (plan e46b8f).
7739 - $models = MxChat_Model_Catalog::settings_dropdown_groups($selected_model);
7740 2715
7741 - // Begin the select dropdown
7742 - echo '<select id="model" name="model">';
7743 2716
7744 - // Iterate over groups of models
7745 - foreach ($models as $group_label => $group_models) {
7746 - echo '<optgroup label="' . esc_attr($group_label) . '">';
7747 2717
7748 - foreach ($group_models as $model_value => $model_label) {
7749 - echo '<option value="' . esc_attr($model_value) . '" ' . selected($selected_model, $model_value, false) . '>' . esc_html($model_label) . '</option>';
7750 - }
7751 2718
7752 - echo '</optgroup>';
7753 - }
7754 2719
7755 - echo '</select>';
2720 + public function mxchat_close_button_color_callback() {
2721 + $disabled = $this->is_activated ? '' : 'disabled';
7756 2722
7757 - // Add a note for OpenRouter
7758 - echo '<p class="description" id="openrouter-model-note" style="display:none; color: #d63638; font-weight: 500;">';
7759 - echo '<span class="dashicons dashicons-info" style="font-size: 16px; vertical-align: middle;"></span> ';
7760 - echo esc_html__('After entering your OpenRouter API key above, click the button below to load available models.', 'mxchat');
7761 - echo '</p>';
2723 + echo '<div class="pro-feature-wrapper">';
2724 + printf(
2725 + '<input type="text" id="close_button_color" name="mxchat_options[close_button_color]" value="%s" class="my-color-field" data-default-color="#4a4a4a" %s />',
2726 + isset($this->options['close_button_color']) ? esc_attr($this->options['close_button_color']) : '',
2727 + esc_attr($disabled)
2728 + );
7762 2729
7763 - // API Key Status Messages (hidden by default, shown by JS based on selected model)
7764 - $has_openai_key = !empty($this->options['api_key']);
7765 - $has_claude_key = !empty($this->options['claude_api_key']);
7766 - $has_xai_key = !empty($this->options['xai_api_key']);
7767 - $has_deepseek_key = !empty($this->options['deepseek_api_key']);
7768 - $has_gemini_key = !empty($this->options['gemini_api_key']);
7769 - $has_openrouter_key = !empty($this->options['openrouter_api_key']);
7770 -
7771 - // OpenAI/GPT models
7772 - echo '<p class="mxchat-api-status" data-provider="openai" style="display:none;">';
7773 - if ($has_openai_key) {
7774 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for OpenAI detected', 'mxchat') . '</span>';
7775 - } else {
7776 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for OpenAI detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
2730 + if (!$this->is_activated) {
2731 + echo '<div class="pro-feature-overlay">';
2732 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2733 + echo '</div>';
7777 2734 }
7778 - echo '</p>';
7779 2735
7780 - // Claude models
7781 - echo '<p class="mxchat-api-status" data-provider="claude" style="display:none;">';
7782 - if ($has_claude_key) {
7783 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for Anthropic (Claude) detected', 'mxchat') . '</span>';
7784 - } else {
7785 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for Anthropic (Claude) detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
2736 + echo '</div>';
7786 2737 }
7787 - echo '</p>';
7788 2738
7789 - // X.AI models
7790 - echo '<p class="mxchat-api-status" data-provider="xai" style="display:none;">';
7791 - if ($has_xai_key) {
7792 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for X.AI (Grok) detected', 'mxchat') . '</span>';
7793 - } else {
7794 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for X.AI (Grok) detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
7795 - }
7796 - echo '</p>';
2739 + public function mxchat_chatbot_bg_color_callback() {
2740 + $disabled = $this->is_activated ? '' : 'disabled';
7797 2741
7798 - // DeepSeek models
7799 - echo '<p class="mxchat-api-status" data-provider="deepseek" style="display:none;">';
7800 - if ($has_deepseek_key) {
7801 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for DeepSeek detected', 'mxchat') . '</span>';
7802 - } else {
7803 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for DeepSeek detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
7804 - }
7805 - echo '</p>';
2742 + echo '<div class="pro-feature-wrapper">';
2743 + printf(
2744 + '<input type="text" id="chatbot_bg_color" name="mxchat_options[chatbot_bg_color]" value="%s" class="my-color-field" data-default-color="#f9f9f9" %s />',
2745 + isset($this->options['chatbot_bg_color']) ? esc_attr($this->options['chatbot_bg_color']) : '',
2746 + esc_attr($disabled)
2747 + );
7806 2748
7807 - // Gemini models
7808 - echo '<p class="mxchat-api-status" data-provider="gemini" style="display:none;">';
7809 - if ($has_gemini_key) {
7810 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for Google Gemini detected', 'mxchat') . '</span>';
7811 - } else {
7812 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for Google Gemini detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
7813 - }
7814 - echo '</p>';
2749 + if (!$this->is_activated) {
2750 + echo '<div class="pro-feature-overlay">';
2751 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2752 + echo '</div>';
2753 + }
7815 2754
7816 - // OpenRouter models
7817 - echo '<p class="mxchat-api-status" data-provider="openrouter" style="display:none;">';
7818 - if ($has_openrouter_key) {
7819 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for OpenRouter detected', 'mxchat') . '</span>';
7820 - } else {
7821 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for OpenRouter detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
2755 + echo '</div>';
7822 2756 }
7823 - echo '</p>';
7824 2757
7825 - // ADD THESE HIDDEN FIELDS RIGHT HERE:
7826 - $openrouter_model = isset($this->options['openrouter_selected_model']) ? esc_attr($this->options['openrouter_selected_model']) : '';
7827 - $openrouter_model_name = isset($this->options['openrouter_selected_model_name']) ? esc_attr($this->options['openrouter_selected_model_name']) : '';
2758 + public function mxchat_user_message_bg_color_callback() {
2759 + $disabled = $this->is_activated ? '' : 'disabled';
7828 2760
7829 - echo '<input type="hidden" id="openrouter_selected_model" name="openrouter_selected_model" value="' . $openrouter_model . '" />';
7830 - echo '<input type="hidden" id="openrouter_selected_model_name" name="openrouter_selected_model_name" value="' . $openrouter_model_name . '" />';
7831 -}
2761 + echo '<div class="pro-feature-wrapper">';
2762 + printf(
2763 + '<input type="text" id="user_message_bg_color" name="mxchat_options[user_message_bg_color]" value="%s" class="my-color-field" data-default-color="#0078d7" %s />',
2764 + isset($this->options['user_message_bg_color']) ? esc_attr($this->options['user_message_bg_color']) : '',
2765 + esc_attr($disabled)
2766 + );
7832 2767
7833 -// Update your existing callback method
7834 -public function enable_streaming_toggle_callback() {
7835 - // Get value from options array, default to 'on'
7836 - $enabled = isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on';
7837 - $checked = ($enabled === 'on') ? 'checked' : '';
2768 + if (!$this->is_activated) {
2769 + echo '<div class="pro-feature-overlay">';
2770 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2771 + echo '</div>';
2772 + }
7838 2773
7839 - echo '<label class="toggle-switch">';
7840 - echo sprintf(
7841 - '<input type="checkbox" id="enable_streaming_toggle" name="enable_streaming_toggle" value="on" %s />',
7842 - esc_attr($checked)
7843 - );
7844 - echo '<span class="slider"></span>';
7845 - echo '</label>';
2774 + echo '</div>';
2775 + }
7846 2776
7847 - // Test button — branded .mxch-btn with inline SVG (IDs preserved for AJAX binding)
7848 - echo '<div class="mxch-streaming-test-row">';
7849 - echo '<button type="button" id="mxchat-test-streaming-btn" class="mxch-btn mxch-btn-secondary">';
7850 - echo '<svg class="mxch-streaming-test-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/></svg>';
7851 - echo esc_html__('Test Streaming Compatibility', 'mxchat');
7852 - echo '</button>';
7853 - echo '<p id="mxchat-test-streaming-result" class="mxch-streaming-test-result"></p>';
7854 - echo '</div>';
7855 -}
2777 + public function mxchat_user_message_font_color_callback() {
2778 + $disabled = $this->is_activated ? '' : 'disabled';
7856 2779
7857 -// Web Search toggle callback
7858 -public function enable_web_search_toggle_callback() {
7859 - // Get value from options array, default to 'off'
7860 - $enabled = isset($this->options['enable_web_search']) ? $this->options['enable_web_search'] : 'off';
7861 - $checked = ($enabled === 'on') ? 'checked' : '';
2780 + echo '<div class="pro-feature-wrapper">';
2781 + printf(
2782 + '<input type="text" id="user_message_font_color" name="mxchat_options[user_message_font_color]" value="%s" class="my-color-field" data-default-color="#ffffff" %s />',
2783 + isset($this->options['user_message_font_color']) ? esc_attr($this->options['user_message_font_color']) : '',
2784 + esc_attr($disabled)
2785 + );
7862 2786
7863 - // Get current model to determine if we should show/enable the toggle
7864 - $current_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.6-sol';
2787 + if (!$this->is_activated) {
2788 + echo '<div class="pro-feature-overlay">';
2789 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2790 + echo '</div>';
2791 + }
7865 2792
7866 - // Models that DON'T support web search — OpenAI-docs-driven exception list.
7867 - // Keep hardcoded; the catalog can't infer "supports web search" per-model, so any
7868 - // future OpenAI model that lacks Responses-API web_search support is added here.
7869 - $unsupported_models = array('gpt-4.1-nano');
2793 + echo '</div>';
2794 + }
7870 2795
7871 - // Web-search-capable chat-model allowlists, derived from the central model catalog
7872 - // (class-mxchat-model-catalog.php). When a new OpenAI/Gemini chat model is added there,
7873 - // the Web Search toggle picks it up automatically — no edit here.
7874 - // OpenAI grounds via the Responses-API web_search tool; Gemini grounds natively via the
7875 - // Google Search tool (plan 46b9ea wired the Gemini dispatch — every shipped Gemini chat
7876 - // model is 2.x/3.x and grounds, matching that path's empty opt-out list, so all catalog
7877 - // Gemini models are supported here).
7878 - if (!class_exists('MxChat_Model_Catalog')) {
7879 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-model-catalog.php';
7880 - }
7881 - $chat_catalog = MxChat_Model_Catalog::chat_models();
7882 - $openai_models = (isset($chat_catalog['openai']['models']) && is_array($chat_catalog['openai']['models']))
7883 - ? array_keys($chat_catalog['openai']['models'])
7884 - : array();
7885 - $gemini_models = (isset($chat_catalog['gemini']['models']) && is_array($chat_catalog['gemini']['models']))
7886 - ? array_keys($chat_catalog['gemini']['models'])
7887 - : array();
2796 + public function mxchat_bot_message_bg_color_callback() {
2797 + $disabled = $this->is_activated ? '' : 'disabled';
7888 2798
7889 - $is_capable = in_array($current_model, $openai_models) || in_array($current_model, $gemini_models);
7890 - $is_supported = $is_capable && !in_array($current_model, $unsupported_models);
2799 + echo '<div class="pro-feature-wrapper">';
2800 + printf(
2801 + '<input type="text" id="bot_message_bg_color" name="mxchat_options[bot_message_bg_color]" value="%s" class="my-color-field" data-default-color="#e1e1e1" %s />',
2802 + isset($this->options['bot_message_bg_color']) ? esc_attr($this->options['bot_message_bg_color']) : '',
2803 + esc_attr($disabled)
2804 + );
7891 2805
7892 - // Wrapper div with data attributes for JS to show/hide. data-openai-models is kept for
7893 - // back-compat; data-gemini-models is the added second provider the JS now also honors.
7894 - echo '<div id="web-search-toggle-wrapper" data-openai-models="' . esc_attr(implode(',', $openai_models)) . '" data-gemini-models="' . esc_attr(implode(',', $gemini_models)) . '" data-unsupported-models="' . esc_attr(implode(',', $unsupported_models)) . '"' . (!$is_supported ? ' style="display:none;"' : '') . '>';
2806 + if (!$this->is_activated) {
2807 + echo '<div class="pro-feature-overlay">';
2808 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2809 + echo '</div>';
2810 + }
7895 2811
7896 - echo '<label class="toggle-switch">';
7897 - echo sprintf(
7898 - '<input type="checkbox" id="enable_web_search" name="enable_web_search" value="on" %s />',
7899 - esc_attr($checked)
7900 - );
7901 - echo '<span class="slider"></span>';
7902 - echo '</label>';
2812 + echo '</div>';
2813 + }
7903 2814
7904 - echo '</div>';
2815 + public function mxchat_bot_message_font_color_callback() {
2816 + $disabled = $this->is_activated ? '' : 'disabled';
7905 2817
7906 - // Message shown when a model that can't ground (Claude, Grok, DeepSeek, OpenRouter, etc.) is selected
7907 - echo '<p id="web-search-unavailable-message" class="description" style="color: #666;' . ($is_supported ? ' display:none;' : '') . '">';
7908 - echo '<span class="dashicons dashicons-info" style="font-size: 16px; vertical-align: middle; margin-right: 4px;"></span>';
7909 - echo esc_html__('Web search is only available for OpenAI and Gemini models.', 'mxchat');
7910 - echo '</p>';
7911 -}
2818 + echo '<div class="pro-feature-wrapper">';
2819 + printf(
2820 + '<input type="text" id="bot_message_font_color" name="mxchat_options[bot_message_font_color]" value="%s" class="my-color-field" data-default-color="#333333" %s />',
2821 + isset($this->options['bot_message_font_color']) ? esc_attr($this->options['bot_message_font_color']) : '',
2822 + esc_attr($disabled)
2823 + );
7912 2824
7913 -// AJAX handler to fetch OpenRouter models
7914 -public function fetch_openrouter_models() {
7915 - check_ajax_referer('mxchat_fetch_openrouter_models', 'nonce');
2825 + if (!$this->is_activated) {
2826 + echo '<div class="pro-feature-overlay">';
2827 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2828 + echo '</div>';
2829 + }
7916 2830
7917 - // plan-mxchat-20260731-c63fb6 — nonce is not authorization.
7918 - if (!current_user_can('manage_options')) {
7919 - wp_send_json_error(array('message' => esc_html__('Unauthorized', 'mxchat')), 403);
2831 + echo '</div>';
7920 2832 }
7921 2833
7922 - $api_key = isset($_POST['api_key']) ? sanitize_text_field($_POST['api_key']) : '';
7923 -
7924 - if (empty($api_key)) {
7925 - wp_send_json_error(array('message' => 'API key is required'));
7926 - }
7927 -
7928 - $response = wp_remote_get('https://openrouter.ai/api/v1/models', array(
7929 - 'headers' => array(
7930 - 'Authorization' => 'Bearer ' . $api_key,
7931 - 'Content-Type' => 'application/json',
7932 - ),
7933 - 'timeout' => 15,
7934 - ));
7935 -
7936 - if (is_wp_error($response)) {
7937 - wp_send_json_error(array('message' => $response->get_error_message()));
7938 - }
7939 -
7940 - $body = wp_remote_retrieve_body($response);
7941 - $data = json_decode($body, true);
7942 -
7943 - if (isset($data['data']) && is_array($data['data'])) {
7944 - // Format the models for the frontend
7945 - $models = array_map(function($model) {
7946 - return array(
7947 - 'id' => $model['id'],
7948 - 'name' => $model['name'] ?? $model['id'],
7949 - 'description' => $model['description'] ?? '',
7950 - 'context_length' => $model['context_length'] ?? 0,
7951 - 'pricing' => array(
7952 - 'prompt' => $model['pricing']['prompt'] ?? 0,
7953 - 'completion' => $model['pricing']['completion'] ?? 0,
7954 - ),
7955 - );
7956 - }, $data['data']);
7957 -
7958 - wp_send_json_success(array('models' => $models));
7959 - } else {
7960 - wp_send_json_error(array('message' => 'Invalid response from OpenRouter'));
7961 - }
7962 -}
2834 + public function mxchat_live_agent_message_bg_color_callback() {
2835 + $disabled = $this->is_activated ? '' : 'disabled';
7963 2836
7964 -// Callback function for embedding model selection
7965 -public function embedding_model_callback() {
7966 - $models = array(
7967 - esc_html__('OpenAI Embeddings', 'mxchat') => array(
7968 - 'text-embedding-3-small' => esc_html__('TE3 Small (1536, Efficient)', 'mxchat'),
7969 - 'text-embedding-ada-002' => esc_html__('Ada 2 (1536, Recommended)', 'mxchat'),
7970 - 'text-embedding-3-large' => esc_html__('TE3 Large (3072, Powerful)', 'mxchat'),
7971 - ),
7972 - esc_html__('Voyage AI Embeddings', 'mxchat') => array(
7973 - 'voyage-3-large' => esc_html__('Voyage-3 Large (2048, Most Capable)', 'mxchat'),
7974 - ),
7975 - esc_html__('Google Gemini Embeddings', 'mxchat') => array(
7976 - 'gemini-embedding-001' => esc_html__('Gemini Embedding (1536, Stable)', 'mxchat'),
7977 - )
7978 - );
7979 - $selected_model = isset($this->options['embedding_model']) ? esc_attr($this->options['embedding_model']) : 'text-embedding-ada-002';
7980 - echo '<select id="embedding_model" name="embedding_model">';
7981 - foreach ($models as $group_label => $group_models) {
7982 - echo '<optgroup label="' . esc_attr($group_label) . '">';
7983 - foreach ($group_models as $model_value => $model_label) {
7984 - echo '<option value="' . esc_attr($model_value) . '" ' . selected($selected_model, $model_value, false) . '>' . esc_html($model_label) . '</option>';
2837 + echo '<div class="pro-feature-wrapper">';
2838 + printf(
2839 + '<input type="text" id="live_agent_message_bg_color" name="mxchat_options[live_agent_message_bg_color]" value="%s" class="my-color-field" data-default-color="#ffffff" %s />',
2840 + isset($this->options['live_agent_message_bg_color']) ? esc_attr($this->options['live_agent_message_bg_color']) : '',
2841 + esc_attr($disabled)
2842 + );
2843 +
2844 + if (!$this->is_activated) {
2845 + echo '<div class="pro-feature-overlay">';
2846 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2847 + echo '</div>';
7985 2848 }
7986 - echo '</optgroup>';
2849 +
2850 + echo '</div>';
7987 2851 }
7988 - echo '</select>';
7989 2852
7990 - // API Key Status Messages for Embedding Models
7991 - $has_openai_key = !empty($this->options['api_key']);
7992 - $has_voyage_key = !empty($this->options['voyage_api_key']);
7993 - $has_gemini_key = !empty($this->options['gemini_api_key']);
2853 + public function mxchat_live_agent_message_font_color_callback() {
2854 + $disabled = $this->is_activated ? '' : 'disabled';
7994 2855
7995 - // OpenAI Embeddings
7996 - echo '<p class="mxchat-embedding-api-status" data-provider="openai" style="display:none;">';
7997 - if ($has_openai_key) {
7998 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for OpenAI detected', 'mxchat') . '</span>';
7999 - } else {
8000 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for OpenAI detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
8001 - }
8002 - echo '</p>';
2856 + echo '<div class="pro-feature-wrapper">';
2857 + printf(
2858 + '<input type="text" id="live_agent_message_font_color" name="mxchat_options[live_agent_message_font_color]" value="%s" class="my-color-field" data-default-color="#333333" %s />',
2859 + isset($this->options['live_agent_message_font_color']) ? esc_attr($this->options['live_agent_message_font_color']) : '',
2860 + esc_attr($disabled)
2861 + );
8003 2862
8004 - // Voyage AI Embeddings
8005 - echo '<p class="mxchat-embedding-api-status" data-provider="voyage" style="display:none;">';
8006 - if ($has_voyage_key) {
8007 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for Voyage AI detected', 'mxchat') . '</span>';
8008 - } else {
8009 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for Voyage AI detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
8010 - }
8011 - echo '</p>';
2863 + if (!$this->is_activated) {
2864 + echo '<div class="pro-feature-overlay">';
2865 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2866 + echo '</div>';
2867 + }
8012 2868
8013 - // Gemini Embeddings
8014 - echo '<p class="mxchat-embedding-api-status" data-provider="gemini" style="display:none;">';
8015 - if ($has_gemini_key) {
8016 - echo '<span style="color: #00a32a;">✓ ' . esc_html__('API key for Google Gemini detected', 'mxchat') . '</span>';
8017 - } else {
8018 - echo '<span style="color: #d63638;">⚠ ' . esc_html__('No API key for Google Gemini detected. Please enter API key in API Keys tab.', 'mxchat') . '</span>';
2869 + echo '</div>';
8019 2870 }
8020 - echo '</p>';
8021 2871
8022 -}
2872 + // Mode Indicator Background Color Callback
2873 +public function mxchat_mode_indicator_bg_color_callback() {
2874 + $disabled = $this->is_activated ? '' : 'disabled';
8023 2875
2876 + echo '<div class="pro-feature-wrapper">';
2877 + printf(
2878 + '<input type="text" id="mode_indicator_bg_color" name="mxchat_options[mode_indicator_bg_color]" value="%s" class="my-color-field" data-default-color="#767676" %s />',
2879 + isset($this->options['mode_indicator_bg_color']) ? esc_attr($this->options['mode_indicator_bg_color']) : '',
2880 + esc_attr($disabled)
2881 + );
8024 2882
8025 -public function mxchat_top_bar_title_callback() {
8026 - // Retrieve the current value of the top bar title from saved options
8027 - $top_bar_title = isset($this->options['top_bar_title']) ? esc_attr($this->options['top_bar_title']) : '';
2883 + if (!$this->is_activated) {
2884 + echo '<div class="pro-feature-overlay">';
2885 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2886 + echo '</div>';
2887 + }
8028 2888
8029 - // Render the input field
8030 - echo '<input type="text" id="top_bar_title" name="top_bar_title" value="' . $top_bar_title . '" />';
2889 + echo '</div>';
8031 2890 }
8032 -public function mxchat_ai_agent_text_callback() {
8033 - // Retrieve the current value of the AI agent text from saved options
8034 - $ai_agent_text = isset($this->options['ai_agent_text']) ? esc_attr($this->options['ai_agent_text']) : '';
8035 - // Render the input field
8036 - echo '<input type="text" id="ai_agent_text" name="ai_agent_text" value="' . $ai_agent_text . '" />';
8037 -}
8038 2891
2892 +// Mode Indicator Font Color Callback
2893 +public function mxchat_mode_indicator_font_color_callback() {
2894 + $disabled = $this->is_activated ? '' : 'disabled';
8039 2895
8040 -public function enable_email_block_callback() {
8041 - // Load full plugin options array
8042 - $all_options = get_option('mxchat_options', []);
8043 -
8044 - // Get the value, default to 'off'
8045 - $enable_email_block = isset($all_options['enable_email_block']) ? $all_options['enable_email_block'] : 'off';
8046 -
8047 - // Check if it's 'on'
8048 - $checked = ($enable_email_block === 'on') ? 'checked' : '';
8049 -
8050 - echo '<label class="toggle-switch">';
8051 - echo sprintf(
8052 - '<input type="checkbox" id="enable_email_block" name="enable_email_block" value="on" %s />',
8053 - esc_attr($checked)
2896 + echo '<div class="pro-feature-wrapper">';
2897 + printf(
2898 + '<input type="text" id="mode_indicator_font_color" name="mxchat_options[mode_indicator_font_color]" value="%s" class="my-color-field" data-default-color="#ffffff" %s />',
2899 + isset($this->options['mode_indicator_font_color']) ? esc_attr($this->options['mode_indicator_font_color']) : '',
2900 + esc_attr($disabled)
8054 2901 );
8055 - echo '<span class="slider"></span>';
8056 - echo '</label>';
8057 -}
8058 2902
8059 -public function email_blocker_header_content_callback() {
8060 - // Load the entire 'mxchat_options' array
8061 - $all_options = get_option('mxchat_options', []);
2903 + if (!$this->is_activated) {
2904 + echo '<div class="pro-feature-overlay">';
2905 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2906 + echo '</div>';
2907 + }
8062 2908
8063 - // Retrieve the saved content or default to empty
8064 - $content = isset($all_options['email_blocker_header_content'])
8065 - ? $all_options['email_blocker_header_content']
8066 - : '';
8067 -
8068 - // Render the textarea - IMPORTANT: name should be just "email_blocker_header_content"
8069 - echo '<textarea
8070 - id="email_blocker_header_content"
8071 - name="email_blocker_header_content"
8072 - rows="5"
8073 - cols="70"
8074 - data-setting="email_blocker_header_content"
8075 - >' . esc_textarea($content) . '</textarea>';
2909 + echo '</div>';
8076 2910 }
8077 2911
8078 -public function email_blocker_button_text_callback() {
8079 - // Load the entire 'mxchat_options' array
8080 - $all_options = get_option('mxchat_options', []);
2912 +public function mxchat_toolbar_icon_color_callback() {
2913 + $disabled = $this->is_activated ? '' : 'disabled';
8081 2914
8082 - // Retrieve the saved button text or default to empty
8083 - $button_text = isset($all_options['email_blocker_button_text'])
8084 - ? $all_options['email_blocker_button_text']
8085 - : '';
8086 -
8087 - // Use esc_attr to safely render the existing text
8088 - echo '<input type="text" id="email_blocker_button_text" name="email_blocker_button_text" value="' . esc_attr($button_text) . '" style="width: 300px;" />';
8089 -}
8090 -
8091 -//Enable name field callback
8092 -public function enable_name_field_callback() {
8093 - // Load full plugin options array
8094 - $all_options = get_option('mxchat_options', []);
8095 - // Get the value, default to 'off'
8096 - $enable_name_field = isset($all_options['enable_name_field']) ? $all_options['enable_name_field'] : 'off';
8097 - // Check if it's 'on'
8098 - $checked = ($enable_name_field === 'on') ? 'checked' : '';
8099 - echo '<label class="toggle-switch">';
8100 - echo sprintf(
8101 - '<input type="checkbox" id="enable_name_field" name="enable_name_field" value="on" %s />',
8102 - esc_attr($checked)
2915 + echo '<div class="pro-feature-wrapper">';
2916 + printf(
2917 + '<input type="text" id="toolbar_icon_color" name="mxchat_options[toolbar_icon_color]" value="%s" class="my-color-field" data-default-color="#212121" %s />',
2918 + isset($this->options['toolbar_icon_color']) ? esc_attr($this->options['toolbar_icon_color']) : '',
2919 + esc_attr($disabled)
8103 2920 );
8104 - echo '<span class="slider"></span>';
8105 - echo '</label>';
8106 -}
8107 -//Name field placeholder callback
8108 -public function name_field_placeholder_callback() {
8109 - $all_options = get_option('mxchat_options', []);
8110 - $placeholder = isset($all_options['name_field_placeholder'])
8111 - ? $all_options['name_field_placeholder']
8112 - : esc_html__('Enter your name', 'mxchat');
8113 2921
8114 - echo '<input type="text" id="name_field_placeholder" name="name_field_placeholder" value="' . esc_attr($placeholder) . '" style="width: 300px;" />';
8115 -}
2922 + if (!$this->is_activated) {
2923 + echo '<div class="pro-feature-overlay">';
2924 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2925 + echo '</div>';
2926 + }
8116 2927
8117 -
8118 -
8119 -public function mxchat_intro_message_callback() {
8120 - // Load the entire 'mxchat_options' array
8121 - $all_options = get_option('mxchat_options', []);
8122 - // Retrieve the saved intro message or use the default
8123 - $default_message = __('Hello! How can I assist you today?', 'mxchat');
8124 - $saved_message = isset($all_options['intro_message']) ? $all_options['intro_message'] : $default_message;
8125 - // Escape on output (esc_textarea) — neutralizes any payload already stored before the
8126 - // Wordfence Stored-XSS fix (CWE-79, plan-3f8158) and prevents </textarea> context-breakout.
8127 - ?>
8128 - <textarea id="intro_message" name="intro_message" rows="5" cols="50"><?php echo esc_textarea( $saved_message ); ?></textarea>
8129 - <p class="description" style="margin-top: 8px;">
8130 - <?php esc_html_e('Use {visitor_name} to personalize greetings when lead capture is enabled.', 'mxchat'); ?><br>
8131 - <code style="font-size: 12px;"><?php esc_html_e('Example: Hello {visitor_name}! How can I help you today?', 'mxchat'); ?></code>
8132 - </p>
8133 - <?php
2928 + echo '</div>';
8134 2929 }
8135 2930
8136 -public function mxchat_input_copy_callback() {
8137 - // Load the entire 'mxchat_options' array
8138 - $all_options = get_option('mxchat_options', []);
8139 2931
8140 - // Retrieve the saved input copy or use the default value
8141 - $default_copy = __('How can I assist?', 'mxchat');
8142 - $input_copy = isset($all_options['input_copy']) ? $all_options['input_copy'] : $default_copy;
8143 2932
8144 - // Output the input field with the saved value
8145 - printf(
8146 - '<input type="text" id="input_copy" name="input_copy" value="%s" placeholder="%s" />',
8147 - esc_attr($input_copy),
8148 - esc_attr__('How can I assist?', 'mxchat')
8149 - );
8150 -}
8151 2933
2934 + public function mxchat_top_bar_bg_color_callback() {
2935 + $disabled = $this->is_activated ? '' : 'disabled';
8152 2936
8153 -public function mxchat_append_to_body_callback() {
8154 - // Fetch fresh options to ensure we have the latest saved values
8155 - $options = get_option('mxchat_options', array());
2937 + echo '<div class="pro-feature-wrapper">';
2938 + printf(
2939 + '<input type="text" id="top_bar_bg_color" name="mxchat_options[top_bar_bg_color]" value="%s" class="my-color-field" data-default-color="#00b294" %s />',
2940 + isset($this->options['top_bar_bg_color']) ? esc_attr($this->options['top_bar_bg_color']) : '',
2941 + esc_attr($disabled)
2942 + );
8156 2943
8157 - // Get value from options array, default to 'off'
8158 - $append_to_body = isset($options['append_to_body']) ? $options['append_to_body'] : 'off';
8159 - $checked = ($append_to_body === 'on') ? 'checked' : '';
2944 + if (!$this->is_activated) {
2945 + echo '<div class="pro-feature-overlay">';
2946 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2947 + echo '</div>';
2948 + }
8160 2949
8161 - // Get post type visibility settings
8162 - $visibility_mode = isset($options['post_type_visibility_mode']) ? $options['post_type_visibility_mode'] : 'all';
8163 - $visibility_list = isset($options['post_type_visibility_list']) ? $options['post_type_visibility_list'] : array();
8164 - if (!is_array($visibility_list)) {
8165 - $visibility_list = array();
2950 + echo '</div>';
8166 2951 }
8167 2952
8168 - echo '<div class="mxchat-autosave-section">';
2953 + public function mxchat_send_button_font_color_callback() {
2954 + $disabled = $this->is_activated ? '' : 'disabled';
8169 2955
8170 - // Main toggle
8171 - echo '<label class="toggle-switch">';
8172 - echo sprintf(
8173 - '<input type="checkbox" id="append_to_body" name="append_to_body" value="on" %s />',
8174 - esc_attr($checked)
8175 - );
8176 - echo '<span class="slider"></span>';
8177 - echo '</label>';
2956 + echo '<div class="pro-feature-wrapper">';
2957 + printf(
2958 + '<input type="text" id="send_button_font_color" name="mxchat_options[send_button_font_color]" value="%s" class="my-color-field" data-default-color="#ffffff" %s />',
2959 + isset($this->options['send_button_font_color']) ? esc_attr($this->options['send_button_font_color']) : '',
2960 + esc_attr($disabled)
2961 + );
8178 2962
8179 - // Post Type Visibility Options (only visible when auto-display is ON)
8180 - $display_style = ($append_to_body === 'on') ? '' : 'display: none;';
8181 - echo '<div id="post-type-visibility-options" class="mxchat-sub-options" style="' . esc_attr($display_style) . '">';
2963 + if (!$this->is_activated) {
2964 + echo '<div class="pro-feature-overlay">';
2965 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2966 + echo '</div>';
2967 + }
8182 2968
8183 - echo '<div class="mxchat-post-type-visibility-header">';
8184 - echo '<h4>' . esc_html__('Post Type Visibility', 'mxchat') . '</h4>';
8185 - echo '</div>';
2969 + echo '</div>';
2970 + }
8186 2971
8187 - // Mode selector (radio buttons)
8188 - echo '<div class="mxchat-visibility-mode">';
2972 + public function mxchat_chatbot_background_color_callback() {
2973 + $disabled = $this->is_activated ? '' : 'disabled';
8189 2974
8190 - echo '<label class="mxchat-radio-label">';
8191 - echo '<input type="radio" name="post_type_visibility_mode" value="all" ' . checked($visibility_mode, 'all', false) . ' />';
8192 - echo '<span>' . esc_html__('Show on all post types', 'mxchat') . '</span>';
8193 - echo '</label>';
2975 + echo '<div class="pro-feature-wrapper">';
2976 + printf(
2977 + '<input type="text" id="chatbot_background_color" name="mxchat_options[chatbot_background_color]" value="%s" class="my-color-field" data-default-color="#000000" %s />',
2978 + isset($this->options['chatbot_background_color']) ? esc_attr($this->options['chatbot_background_color']) : '',
2979 + esc_attr($disabled)
2980 + );
8194 2981
8195 - echo '<label class="mxchat-radio-label">';
8196 - echo '<input type="radio" name="post_type_visibility_mode" value="include" ' . checked($visibility_mode, 'include', false) . ' />';
8197 - echo '<span>' . esc_html__('Only show on selected post types', 'mxchat') . '</span>';
8198 - echo '</label>';
2982 + if (!$this->is_activated) {
2983 + echo '<div class="pro-feature-overlay">';
2984 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
2985 + echo '</div>';
2986 + }
8199 2987
8200 - echo '<label class="mxchat-radio-label">';
8201 - echo '<input type="radio" name="post_type_visibility_mode" value="exclude" ' . checked($visibility_mode, 'exclude', false) . ' />';
8202 - echo '<span>' . esc_html__('Hide on selected post types', 'mxchat') . '</span>';
8203 - echo '</label>';
2988 + echo '</div>';
2989 + }
8204 2990
8205 - echo '</div>';
2991 + public function mxchat_icon_color_callback() {
2992 + $disabled = $this->is_activated ? '' : 'disabled';
8206 2993
8207 - // Post type checkboxes (only visible when mode is include or exclude)
8208 - $list_display = ($visibility_mode !== 'all') ? '' : 'display: none;';
8209 - echo '<div id="post-type-list" class="mxchat-post-type-list" style="' . esc_attr($list_display) . '">';
2994 + echo '<div class="pro-feature-wrapper">';
2995 + printf(
2996 + '<input type="text" id="icon_color" name="mxchat_options[icon_color]" value="%s" class="my-color-field" data-default-color="#ffffff" %s />',
2997 + isset($this->options['icon_color']) ? esc_attr($this->options['icon_color']) : '',
2998 + esc_attr($disabled)
2999 + );
8210 3000
8211 - // Get all public post types
8212 - $post_types = get_post_types(array('public' => true), 'objects');
8213 -
8214 - foreach ($post_types as $post_type) {
8215 - // Skip attachments
8216 - if ($post_type->name === 'attachment') {
8217 - continue;
3001 + if (!$this->is_activated) {
3002 + echo '<div class="pro-feature-overlay">';
3003 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3004 + echo '</div>';
8218 3005 }
8219 3006
8220 - $is_checked = in_array($post_type->name, $visibility_list) ? 'checked' : '';
8221 -
8222 - echo '<label class="mxchat-checkbox-label">';
8223 - echo '<input type="checkbox" name="post_type_visibility_list[]" value="' . esc_attr($post_type->name) . '" ' . $is_checked . ' />';
8224 - echo '<span>' . esc_html($post_type->label) . '</span>';
8225 - echo '</label>';
3007 + echo '</div>';
8226 3008 }
8227 3009
8228 - echo '</div>'; // End post-type-list
8229 - echo '</div>'; // End post-type-visibility-options
8230 - echo '</div>'; // End mxchat-autosave-section
8231 -}
3010 + public function mxchat_custom_icon_callback() {
3011 + $disabled = $this->is_activated ? '' : 'disabled';
3012 + $custom_icon_url = isset($this->options['custom_icon']) ? esc_url($this->options['custom_icon']) : '';
8232 3013
8233 -public function mxchat_contextual_awareness_callback() {
8234 - // Get value from options array, default to 'off'
8235 - $contextual_awareness = isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off';
8236 - $checked = ($contextual_awareness === 'on') ? 'checked' : '';
8237 - echo '<label class="toggle-switch">';
8238 - echo sprintf(
8239 - '<input type="checkbox" id="contextual_awareness_toggle" name="contextual_awareness_toggle" value="on" %s />',
8240 - esc_attr($checked)
8241 - );
8242 - echo '<span class="slider"></span>';
8243 - echo '</label>';
8244 -}
3014 + echo '<div class="pro-feature-wrapper">';
3015 + printf(
3016 + '<input type="url" id="custom_icon" name="mxchat_options[custom_icon]" value="%s" placeholder="Enter PNG URL" %s />',
3017 + $custom_icon_url,
3018 + esc_attr($disabled)
3019 + );
3020 + echo '<p class="description">Upload your PNG icon and paste the URL here. Recommended size: 48x48 pixels.</p>';
8245 3021
8246 -public function mxchat_citation_links_toggle_callback() {
8247 - // Get value from options array, default to 'on' (enabled by default)
8248 - $citation_links = isset($this->options['citation_links_toggle']) ? $this->options['citation_links_toggle'] : 'on';
8249 - $checked = ($citation_links === 'on') ? 'checked' : '';
8250 - echo '<label class="toggle-switch">';
8251 - echo sprintf(
8252 - '<input type="checkbox" id="citation_links_toggle" name="citation_links_toggle" value="on" %s />',
8253 - esc_attr($checked)
8254 - );
8255 - echo '<span class="slider"></span>';
8256 - echo '</label>';
8257 -}
3022 + if (!$this->is_activated) {
3023 + echo '<div class="pro-feature-overlay">';
3024 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3025 + echo '</div>';
3026 + }
8258 3027
8259 -/**
8260 - * Toggle for the end-of-session satisfaction rating prompt (plan-a5b006).
8261 - * Default ON. The widget reads this through the localized object; the
8262 - * mxchat_satisfaction_rating_enabled filter still lets developers force
8263 - * the value site-wide.
8264 - *
8265 - * The 5 customization fields (idle/question/thanks/placeholder/saved) are
8266 - * rendered inline here inside a single wrapper div whose initial display
8267 - * is set server-side from the toggle value (plan-29caac). Mirrors the
8268 - * auto-display chatbot pattern at mxchat_append_to_body_callback — no
8269 - * DOMContentLoaded race because rows exist as direct children of this
8270 - * callback's output, and the wrapper's display: none is inline at render
8271 - * time so refresh shows the correct state with no flash.
8272 - */
8273 -public function mxchat_satisfaction_rating_toggle_callback() {
8274 - $options = $this->options;
8275 - $value = isset($options['satisfaction_rating_enabled']) ? $options['satisfaction_rating_enabled'] : 'off';
8276 - $checked = ($value === 'on') ? 'checked' : '';
3028 + echo '</div>';
3029 + }
8277 3030
8278 - $idle = isset($options['satisfaction_rating_idle_seconds']) ? intval($options['satisfaction_rating_idle_seconds']) : 60;
8279 - $idle = max(5, min(600, $idle));
8280 - $question = isset($options['satisfaction_rating_question']) ? $options['satisfaction_rating_question'] : '';
8281 - $thanks = isset($options['satisfaction_rating_thanks']) ? $options['satisfaction_rating_thanks'] : '';
8282 - $placeholder = isset($options['satisfaction_rating_placeholder']) ? $options['satisfaction_rating_placeholder'] : '';
8283 - $saved = isset($options['satisfaction_rating_saved']) ? $options['satisfaction_rating_saved'] : '';
3031 + public function mxchat_title_icon_callback() {
3032 + $disabled = $this->is_activated ? '' : 'disabled';
3033 + $title_icon_url = isset($this->options['custom_icon']) ? esc_url($this->options['title_icon']) : '';
8284 3034
8285 - echo '<div class="mxchat-autosave-section">';
3035 + echo '<div class="pro-feature-wrapper">';
3036 + printf(
3037 + '<input type="url" id="title_icon" name="mxchat_options[title_icon]" value="%s" placeholder="Enter PNG URL" %s />',
3038 + $title_icon_url,
3039 + esc_attr($disabled)
3040 + );
3041 + echo '<p class="description">Upload your PNG icon and paste the URL here. Recommended size: 48x48 pixels.</p>';
8286 3042
8287 - echo '<label class="toggle-switch">';
8288 - echo sprintf(
8289 - '<input type="checkbox" id="satisfaction_rating_enabled" name="satisfaction_rating_enabled" value="on" %s />',
8290 - esc_attr($checked)
8291 - );
8292 - echo '<span class="slider"></span>';
8293 - echo '</label>';
3043 + if (!$this->is_activated) {
3044 + echo '<div class="pro-feature-overlay">';
3045 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3046 + echo '</div>';
3047 + }
8294 3048
8295 - ?>
8296 - <style>
8297 - .mxchat-sub-options-field { margin: 12px 0; }
8298 - .mxchat-sub-options-field label { display: inline-block; margin-bottom: 4px; }
8299 - #satisfaction-rating-sub-options h4 { margin: 16px 0 8px; }
8300 - </style>
8301 - <?php
3049 + echo '</div>';
3050 + }
8302 3051
8303 - $display_style = ($value === 'on') ? '' : 'display: none;';
8304 - echo '<div id="satisfaction-rating-sub-options" class="mxchat-sub-options" style="' . esc_attr($display_style) . '">';
8305 3052
8306 - echo '<h4>' . esc_html__('Customize the prompt (optional)', 'mxchat') . '</h4>';
3053 + public function mxchat_chat_input_font_color_callback() {
3054 + $disabled = $this->is_activated ? '' : 'disabled';
8307 3055
8308 - echo '<div class="mxchat-sub-options-field">';
8309 - echo '<label for="satisfaction_rating_idle_seconds"><strong>' . esc_html__('Idle Timeout', 'mxchat') . '</strong></label><br />';
8310 - printf(
8311 - '<input type="number" id="satisfaction_rating_idle_seconds" name="satisfaction_rating_idle_seconds" value="%d" min="5" max="600" step="1" class="small-text" /> <span class="description">%s</span>',
8312 - (int) $idle,
8313 - esc_html__('seconds of user inactivity before the prompt appears (5-600)', 'mxchat')
8314 - );
8315 - echo '</div>';
3056 + echo '<div class="pro-feature-wrapper">';
3057 + printf(
3058 + '<input type="text" id="chat_input_font_color" name="mxchat_options[chat_input_font_color]" value="%s" class="my-color-field" data-default-color="#555555" %s />',
3059 + isset($this->options['chat_input_font_color']) ? esc_attr($this->options['chat_input_font_color']) : '',
3060 + esc_attr($disabled)
3061 + );
8316 3062
8317 - echo '<div class="mxchat-sub-options-field">';
8318 - echo '<label for="satisfaction_rating_question"><strong>' . esc_html__('Prompt Question', 'mxchat') . '</strong></label><br />';
8319 - printf(
8320 - '<input type="text" id="satisfaction_rating_question" name="satisfaction_rating_question" value="%s" maxlength="200" class="regular-text" placeholder="%s" />',
8321 - esc_attr($question),
8322 - esc_attr__('Was this helpful?', 'mxchat')
8323 - );
8324 - echo '<p class="description">' . esc_html__('Leave blank for the default. Shown above the thumbs up/down.', 'mxchat') . '</p>';
8325 - echo '</div>';
3063 + if (!$this->is_activated) {
3064 + echo '<div class="pro-feature-overlay">';
3065 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3066 + echo '</div>';
3067 + }
8326 3068
8327 - echo '<div class="mxchat-sub-options-field">';
8328 - echo '<label for="satisfaction_rating_thanks"><strong>' . esc_html__('Thank-You Message', 'mxchat') . '</strong></label><br />';
8329 - printf(
8330 - '<input type="text" id="satisfaction_rating_thanks" name="satisfaction_rating_thanks" value="%s" maxlength="300" class="regular-text" placeholder="%s" />',
8331 - esc_attr($thanks),
8332 - esc_attr__('Thanks! Anything we should improve? (optional)', 'mxchat')
8333 - );
8334 - echo '<p class="description">' . esc_html__('Leave blank for the default. Shown after the user clicks a thumb.', 'mxchat') . '</p>';
8335 - echo '</div>';
3069 + echo '</div>';
3070 + }
8336 3071
8337 - echo '<div class="mxchat-sub-options-field">';
8338 - echo '<label for="satisfaction_rating_placeholder"><strong>' . esc_html__('Feedback Placeholder', 'mxchat') . '</strong></label><br />';
8339 - printf(
8340 - '<input type="text" id="satisfaction_rating_placeholder" name="satisfaction_rating_placeholder" value="%s" maxlength="200" class="regular-text" placeholder="%s" />',
8341 - esc_attr($placeholder),
8342 - esc_attr__('Tell us what could be better…', 'mxchat')
8343 - );
8344 - echo '<p class="description">' . esc_html__('Leave blank for the default. Placeholder text inside the feedback textarea.', 'mxchat') . '</p>';
8345 - echo '</div>';
8346 3072
8347 - echo '<div class="mxchat-sub-options-field">';
8348 - echo '<label for="satisfaction_rating_saved"><strong>' . esc_html__('Saved Confirmation', 'mxchat') . '</strong></label><br />';
3073 +public function mxchat_append_to_body_callback() {
3074 + $append_to_body_checked = isset($this->options['append_to_body']) && $this->options['append_to_body'] === 'on' ? 'checked' : '';
3075 + echo '<label class="toggle-switch">';
8349 3076 printf(
8350 - '<input type="text" id="satisfaction_rating_saved" name="satisfaction_rating_saved" value="%s" maxlength="200" class="regular-text" placeholder="%s" />',
8351 - esc_attr($saved),
8352 - esc_attr__('Thanks for the feedback.', 'mxchat')
3077 + '<input type="checkbox" id="append_to_body" name="mxchat_options[append_to_body]" %s />',
3078 + esc_attr($append_to_body_checked)
8353 3079 );
8354 - echo '<p class="description">' . esc_html__('Leave blank for the default. Shown after the feedback is sent.', 'mxchat') . '</p>';
8355 - echo '</div>';
3080 + echo '<span class="slider"></span>';
3081 + echo '</label>';
3082 +echo '<p class="description">Enable this option to automatically append the chat widget to the body of every page (recommended). No need to manually use the shortcode [mxchat_chatbot floating="yes"].</p>';
3083 +}
8356 3084
8357 - echo '</div>'; // #satisfaction-rating-sub-options
8358 - echo '</div>'; // .mxchat-autosave-section
8359 3085
8360 - ?>
8361 - <script>
8362 - (function() {
8363 - document.addEventListener('DOMContentLoaded', function() {
8364 - var toggle = document.getElementById('satisfaction_rating_enabled');
8365 - var subOptions = document.getElementById('satisfaction-rating-sub-options');
8366 - if (!toggle || !subOptions) return;
8367 - toggle.addEventListener('change', function() {
8368 - subOptions.style.display = toggle.checked ? '' : 'none';
8369 - });
8370 - });
8371 - })();
8372 - </script>
8373 - <?php
8374 -}
8375 -
8376 3086 public function mxchat_privacy_toggle_callback() {
8377 - // Load from mxchat_options array
8378 - $options = get_option('mxchat_options', []);
3087 + // Check if the privacy toggle is enabled
3088 + $privacy_toggle_checked = isset($this->options['privacy_toggle']) && $this->options['privacy_toggle'] === 'on' ? 'checked' : '';
8379 3089
8380 - // Get privacy toggle value with fallback
8381 - $privacy_toggle = isset($options['privacy_toggle']) ? $options['privacy_toggle'] : 'off';
8382 - $checked = ($privacy_toggle === 'on') ? 'checked' : '';
3090 + // Retrieve the stored privacy text if it exists
3091 + $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>.';
8383 3092
8384 - // Get privacy text with fallback
8385 - $privacy_text = isset($options['privacy_text'])
8386 - ? $options['privacy_text']
8387 - : __('By chatting, you agree to our <a href="https://example.com/privacy-policy" target="_blank">privacy policy</a>.', 'mxchat');
8388 -
8389 3093 // Output the toggle switch
8390 3094 echo '<label class="toggle-switch">';
8391 - echo sprintf(
8392 - '<input type="checkbox" id="privacy_toggle" name="privacy_toggle" value="on" %s />',
8393 - esc_attr($checked)
3095 + printf(
3096 + '<input type="checkbox" id="privacy_toggle" name="mxchat_options[privacy_toggle]" %s />',
3097 + esc_attr($privacy_toggle_checked)
8394 3098 );
8395 3099 echo '<span class="slider"></span>';
8396 3100 echo '</label>';
3101 + echo '<p class="description">Enable this option to display a privacy notice below the chat widget.</p>';
8397 3102
8398 3103 // Output the custom text input field
8399 - echo sprintf(
8400 - '<textarea id="privacy_text" name="privacy_text" rows="5" cols="50" class="regular-text">%s</textarea>',
3104 + printf(
3105 + '<textarea id="privacy_text" name="mxchat_options[privacy_text]" rows="5" cols="50" class="regular-text">%s</textarea>',
8401 3106 esc_textarea($privacy_text)
8402 3107 );
3108 + echo '<p class="description">Enter the privacy policy text. You can include HTML links.</p>';
8403 3109 }
8404 3110
8405 3111
8406 3112 public function mxchat_complianz_toggle_callback() {
8407 - // Load from mxchat_options array
8408 - $options = get_option('mxchat_options', []);
3113 + // Check if the Complianz toggle is enabled
3114 + $complianz_toggle_checked = isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on' ? 'checked' : '';
8409 3115
8410 - // Get complianz toggle value with fallback
8411 - $complianz_toggle = isset($options['complianz_toggle']) ? $options['complianz_toggle'] : 'off';
8412 - $checked = ($complianz_toggle === 'on') ? 'checked' : '';
3116 + // Check if the plugin is activated (paid feature)
3117 + $disabled = $this->is_activated ? '' : 'disabled';
3118 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8413 3119
3120 + echo '<div class="' . esc_attr($class) . '">';
3121 +
8414 3122 // Output the toggle switch
8415 3123 echo '<label class="toggle-switch">';
8416 - echo sprintf(
8417 - '<input type="checkbox" id="complianz_toggle" name="complianz_toggle" value="on" %s />',
8418 - esc_attr($checked)
3124 + printf(
3125 + '<input type="checkbox" id="complianz_toggle" name="mxchat_options[complianz_toggle]" %s %s />',
3126 + esc_attr($complianz_toggle_checked),
3127 + esc_attr($disabled)
8419 3128 );
8420 3129 echo '<span class="slider"></span>';
8421 3130 echo '</label>';
3131 + echo '<p class="description">Enable this option to apply Complianz consent logic to the chatbot (must have Complianz Plugin).</p>';
3132 +
3133 + // If the feature is not activated, show the Pro feature overlay
3134 + if (!$this->is_activated) {
3135 + echo '<div class="pro-feature-overlay">';
3136 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3137 + echo '</div>';
3138 + }
3139 +
3140 + echo '</div>';
8422 3141 }
8423 3142
8424 -public function mxchat_link_target_toggle_callback() {
8425 - // Load from mxchat_options array
8426 - $options = get_option('mxchat_options', []);
8427 3143
8428 - // Get link target toggle value with fallback
8429 - $link_target_toggle = isset($options['link_target_toggle']) ? $options['link_target_toggle'] : 'off';
8430 - $checked = ($link_target_toggle === 'on') ? 'checked' : '';
8431 3144
8432 - // Output the toggle switch
8433 - echo '<label class="toggle-switch">';
8434 - echo sprintf(
8435 - '<input type="checkbox" id="link_target_toggle" name="link_target_toggle" value="on" %s />',
8436 - esc_attr($checked)
8437 - );
8438 - echo '<span class="slider"></span>';
8439 - echo '</label>';
8440 -}
8441 3145
8442 -public function mxchat_chat_persistence_toggle_callback() {
8443 - // Load from mxchat_options array
8444 - $options = get_option('mxchat_options', []);
8445 3146
8446 - // Get chat persistence toggle value with fallback
8447 - $chat_persistence_toggle = isset($options['chat_persistence_toggle']) ? $options['chat_persistence_toggle'] : 'off';
8448 - $checked = ($chat_persistence_toggle === 'on') ? 'checked' : '';
3147 +public function mxchat_link_target_toggle_callback() {
3148 + // Check if the toggle is enabled in the options
3149 + $link_target_toggle = isset($this->options['link_target_toggle']) && $this->options['link_target_toggle'] === 'on' ? 'checked' : '';
8449 3150
8450 3151 // Output the toggle switch
8451 3152 echo '<label class="toggle-switch">';
8452 - echo sprintf(
8453 - '<input type="checkbox" id="chat_persistence_toggle" name="chat_persistence_toggle" value="on" %s />',
8454 - esc_attr($checked)
3153 + printf(
3154 + '<input type="checkbox" id="link_target_toggle" name="mxchat_options[link_target_toggle]" %s />',
3155 + esc_attr($link_target_toggle)
8455 3156 );
8456 3157 echo '<span class="slider"></span>';
8457 3158 echo '</label>';
3159 + echo '<p class="description">Enable to open links in a new tab (default is to open in the same tab).</p>';
8458 3160 }
8459 3161
8460 -public function mxchat_print_button_toggle_callback() {
8461 - // Load from mxchat_options array
8462 - $options = get_option('mxchat_options', []);
3162 +public function mxchat_chat_persistence_toggle_callback() {
3163 + // Check if chat persistence toggle is enabled
3164 + $chat_persistence_toggle_checked = isset($this->options['chat_persistence_toggle']) && $this->options['chat_persistence_toggle'] === 'on' ? 'checked' : '';
8463 3165
8464 - // Default ON — the option was previously unexposed and the button always showed.
8465 - $print_button_enabled = isset($options['print_button_enabled']) ? $options['print_button_enabled'] : 'on';
8466 - $checked = ($print_button_enabled === 'on') ? 'checked' : '';
3166 + // Check if the plugin is activated (paid feature)
3167 + $disabled = $this->is_activated ? '' : 'disabled';
3168 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8467 3169
3170 + echo '<div class="' . esc_attr($class) . '">';
3171 +
8468 3172 // Output the toggle switch
8469 3173 echo '<label class="toggle-switch">';
8470 - echo sprintf(
8471 - '<input type="checkbox" id="print_button_enabled" name="print_button_enabled" value="on" %s />',
8472 - esc_attr($checked)
3174 + printf(
3175 + '<input type="checkbox" id="chat_persistence_toggle" name="mxchat_options[chat_persistence_toggle]" %s %s />',
3176 + esc_attr($chat_persistence_toggle_checked),
3177 + esc_attr($disabled)
8473 3178 );
8474 3179 echo '<span class="slider"></span>';
8475 3180 echo '</label>';
8476 -}
3181 + echo '<p class="description">Enable to keep chat history when users navigate tabs or return to the site within 24 hours.</p>';
8477 3182
8478 -/**
8479 - * Editor Assistant toggle (plan-8cb0cb). Standalone option, NOT in mxchat_options
8480 - * (bypasses the mxchat_sanitize strip-trap + autosave normalization). Default OFF.
8481 - * Saved by the mxchat_editor_assistant_enabled case in class-ajax-handler.php.
8482 - */
8483 -public function mxchat_editor_assistant_toggle_callback() {
8484 - $enabled = get_option('mxchat_editor_assistant_enabled', 'off');
8485 - $checked = ($enabled === 'on') ? 'checked' : '';
3183 + // If not activated, show Pro feature overlay
3184 + if (!$this->is_activated) {
3185 + echo '<div class="pro-feature-overlay">';
3186 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3187 + echo '</div>';
3188 + }
8486 3189
8487 - echo '<label class="toggle-switch">';
8488 - echo sprintf(
8489 - '<input type="checkbox" id="mxchat_editor_assistant_enabled" name="mxchat_editor_assistant_enabled" value="on" %s />',
8490 - esc_attr($checked)
8491 - );
8492 - echo '<span class="slider"></span>';
8493 - echo '</label>';
3190 + echo '</div>';
8494 3191 }
8495 3192
8496 -public function mxchat_reset_chat_toggle_callback() {
8497 - // Load from mxchat_options array. plan ac2e81 — default OFF (new, opt-in).
8498 - $options = get_option('mxchat_options', []);
8499 - $reset_chat_enabled = isset($options['reset_chat_enabled']) ? $options['reset_chat_enabled'] : 'off';
8500 - $checked = ($reset_chat_enabled === 'on') ? 'checked' : '';
8501 3193
8502 - echo '<label class="toggle-switch">';
8503 - echo sprintf(
8504 - '<input type="checkbox" id="reset_chat_enabled" name="reset_chat_enabled" value="on" %s />',
8505 - esc_attr($checked)
3194 +public function mxchat_popular_question_1_callback() {
3195 + printf(
3196 + '<input type="text" id="popular_question_1" name="mxchat_options[popular_question_1]" value="%s" placeholder="Enter Popular Question 1" />',
3197 + isset($this->options['popular_question_1']) ? esc_attr($this->options['popular_question_1']) : ''
8506 3198 );
8507 - echo '<span class="slider"></span>';
8508 - echo '</label>';
3199 + echo '<p class="description">This will be the first popular question in the chatbot.</p>';
8509 3200 }
8510 3201
8511 -public function mxchat_reset_chat_label_callback() {
8512 - // Editable label for the "Start new chat" menu item. plan ac2e81.
8513 - $options = get_option('mxchat_options', []);
8514 - $reset_chat_label = isset($options['reset_chat_label']) ? $options['reset_chat_label'] : '';
3202 +public function mxchat_popular_question_2_callback() {
3203 + // Check if the plugin is activated (paid feature)
3204 + $disabled = $this->is_activated ? '' : 'disabled';
3205 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8515 3206
3207 + echo '<div class="' . esc_attr($class) . '">';
3208 +
8516 3209 printf(
8517 - '<input type="text" id="reset_chat_label" name="reset_chat_label" value="%s" placeholder="%s" class="regular-text" />',
8518 - esc_attr($reset_chat_label),
8519 - esc_attr__('Start new chat', 'mxchat')
3210 + '<input type="text" id="popular_question_2" name="mxchat_options[popular_question_2]" value="%s" placeholder="Enter Popular Question 2" %s />',
3211 + isset($this->options['popular_question_2']) ? esc_attr($this->options['popular_question_2']) : '',
3212 + esc_attr($disabled)
8520 3213 );
8521 -}
3214 + echo '<p class="description">This will be the second popular question in the chatbot.</p>';
8522 3215
8523 -public function mxchat_popular_question_1_callback() {
8524 - // Load the full plugin options array
8525 - $all_options = get_option('mxchat_options', []);
3216 + // If not activated, show Pro feature overlay
3217 + if (!$this->is_activated) {
3218 + echo '<div class="pro-feature-overlay">';
3219 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3220 + echo '</div>';
3221 + }
8526 3222
8527 - // Retrieve the specific option for popular_question_1
8528 - $popular_question_1 = isset($all_options['popular_question_1']) ? $all_options['popular_question_1'] : '';
8529 -
8530 - // Render the input field
8531 - printf(
8532 - '<input type="text" id="popular_question_1" name="popular_question_1" value="%s" placeholder="%s" class="regular-text" />',
8533 - esc_attr($popular_question_1),
8534 - esc_attr__('Enter Quick Question 1', 'mxchat')
8535 - );
3223 + echo '</div>';
8536 3224 }
8537 3225
3226 +public function mxchat_popular_question_3_callback() {
3227 + // Check if the plugin is activated (paid feature)
3228 + $disabled = $this->is_activated ? '' : 'disabled';
3229 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8538 3230
8539 -public function mxchat_popular_question_2_callback() {
8540 - // Load the full plugin options array
8541 - $all_options = get_option('mxchat_options', []);
3231 + echo '<div class="' . esc_attr($class) . '">';
8542 3232
8543 - // Retrieve the specific option for popular_question_2
8544 - $popular_question_2 = isset($all_options['popular_question_2']) ? $all_options['popular_question_2'] : '';
8545 -
8546 - // Render the input field
8547 3233 printf(
8548 - '<input type="text" id="popular_question_2" name="popular_question_2" value="%s" placeholder="%s" class="regular-text" />',
8549 - esc_attr($popular_question_2),
8550 - esc_attr__('Enter Quick Question 2', 'mxchat')
3234 + '<input type="text" id="popular_question_3" name="mxchat_options[popular_question_3]" value="%s" placeholder="Enter Popular Question 3" %s />',
3235 + isset($this->options['popular_question_3']) ? esc_attr($this->options['popular_question_3']) : '',
3236 + esc_attr($disabled)
8551 3237 );
8552 -}
3238 + echo '<p class="description">This will be the third popular question in the chatbot.</p>';
8553 3239
3240 + // If not activated, show Pro feature overlay
3241 + if (!$this->is_activated) {
3242 + echo '<div class="pro-feature-overlay">';
3243 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3244 + echo '</div>';
3245 + }
8554 3246
8555 -public function mxchat_popular_question_3_callback() {
8556 - // Load the full plugin options array
8557 - $all_options = get_option('mxchat_options', []);
8558 -
8559 - // Retrieve the specific option for popular_question_3
8560 - $popular_question_3 = isset($all_options['popular_question_3']) ? $all_options['popular_question_3'] : '';
8561 -
8562 - // Render the input field
8563 - printf(
8564 - '<input type="text" id="popular_question_3" name="popular_question_3" value="%s" placeholder="%s" class="regular-text" />',
8565 - esc_attr($popular_question_3),
8566 - esc_attr(__('Enter Quick Question 3', 'mxchat'))
8567 - );
3247 + echo '</div>';
8568 3248 }
8569 3249
8570 3250 public function mxchat_additional_popular_questions_callback() {
8571 - $options = get_option('mxchat_options', []);
8572 - $additional_questions = isset($options['additional_popular_questions'])
8573 - ? $options['additional_popular_questions']
8574 - : get_option('additional_popular_questions', array());
3251 + $disabled = $this->is_activated ? '' : 'disabled';
3252 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
3253 + $additional_questions = isset($this->options['additional_popular_questions']) ? $this->options['additional_popular_questions'] : array();
8575 3254
3255 + echo '<div class="' . esc_attr($class) . '">';
8576 3256 echo '<div id="mxchat-additional-questions-container">';
3257 +
8577 3258 if (!empty($additional_questions)) {
8578 3259 foreach ($additional_questions as $index => $question) {
8579 3260 printf(
8580 3261 '<div class="mxchat-question-row">
8581 - <input type="text" name="additional_popular_questions[]"
8582 - value="%s"
8583 - placeholder="%s"
8584 - class="regular-text mxchat-question-input"
8585 - data-question-index="%d" />
8586 - <button type="button" class="button mxchat-remove-question"
8587 - aria-label="%s">%s</button>
3262 + <input type="text" name="mxchat_options[additional_popular_questions][]" value="%s" placeholder="Enter Additional Popular Question %d" %s />
3263 + <button type="button" class="button mxchat-remove-question" aria-label="Remove question" %s>Remove</button>
8588 3264 </div>',
8589 3265 esc_attr($question),
8590 - esc_attr(sprintf(__('Enter Additional Quick Question %d', 'mxchat'), $index + 4)),
8591 - $index,
8592 - esc_attr(__('Remove question', 'mxchat')),
8593 - esc_html__('Remove', 'mxchat')
3266 + $index + 4, // Start numbering from 4
3267 + esc_attr($disabled),
3268 + esc_attr($disabled)
8594 3269 );
8595 3270 }
8596 3271 } else {
8597 3272 printf(
8598 3273 '<div class="mxchat-question-row">
8599 - <input type="text" name="additional_popular_questions[]"
8600 - value=""
8601 - placeholder="%s"
8602 - class="regular-text mxchat-question-input"
8603 - data-question-index="0" />
8604 - <button type="button" class="button mxchat-remove-question"
8605 - aria-label="%s">%s</button>
3274 + <input type="text" name="mxchat_options[additional_popular_questions][]" value="" placeholder="Enter Additional Popular Question 4" %s />
3275 + <button type="button" class="button mxchat-remove-question" aria-label="Remove question" %s>Remove</button>
8606 3276 </div>',
8607 - esc_attr(__('Enter Additional Quick Question 4', 'mxchat')),
8608 - esc_attr(__('Remove question', 'mxchat')),
8609 - esc_html__('Remove', 'mxchat')
3277 + esc_attr($disabled),
3278 + esc_attr($disabled)
8610 3279 );
8611 3280 }
3281 +
8612 3282 echo '</div>';
3283 +
8613 3284 printf(
8614 - '<button type="button" class="button mxchat-add-question" aria-label="%s">%s</button>',
8615 - esc_attr(__('Add question', 'mxchat')),
8616 - esc_html__('Add Question', 'mxchat')
3285 + '<button type="button" class="button mxchat-add-question" aria-label="Add question" %s>Add Question</button>',
3286 + esc_attr($disabled)
8617 3287 );
3288 +
3289 + echo '<p class="description">You can add more popular questions beyond the default three here.</p>';
3290 +
3291 + if (!$this->is_activated) {
3292 + echo '<div class="pro-feature-overlay">';
3293 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3294 + echo '</div>';
3295 + }
3296 +
3297 + echo '</div>';
8618 3298 }
8619 3299
3300 +
3301 +/**
3302 + * Callback for Brave API Key field.
3303 + */
8620 3304 public function mxchat_brave_api_key_callback() {
8621 - $brave_api_key = isset($this->options['brave_api_key']) ? esc_attr($this->options['brave_api_key']) : '';
8622 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
3305 + $options = get_option('mxchat_options');
3306 + $brave_api_key = isset($options['brave_api_key']) ? esc_attr($options['brave_api_key']) : '';
8623 3307
8624 3308 echo '<div class="api-key-wrapper">';
8625 - echo sprintf(
8626 - '<input type="text" id="brave_api_key" name="brave_api_key" value="%s" class="regular-text mxchat-autosave-field mxchat-api-key-field" autocomplete="new-password" data-lpignore="true" data-form-type="other" data-nonce="%s" />',
8627 - $brave_api_key,
8628 - $nonce
3309 + printf(
3310 + '<input type="password" id="brave_api_key" name="mxchat_options[brave_api_key]" value="%s" class="regular-text" />',
3311 + $brave_api_key
8629 3312 );
8630 - echo '<button type="button" id="toggleBraveApiKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
3313 + echo '<button type="button" id="toggleBraveApiKeyVisibility" class="button-secondary">Show</button>';
8631 3314 echo '</div>';
8632 - echo '<p class="description">' . __('Required for Brave Search integration. Get your API key from Brave Search API.', 'mxchat') . '</p>';
3315 + echo '<p class="description">' . __('Enter your Brave Search API Key here. (See FAQ for details)', 'mxchat') . '</p>';
3316 +
3317 + ?>
3318 + <?php
8633 3319 }
8634 3320
3321 +/**
3322 + * Callback for Number of Images to Return field.
3323 + */
8635 3324 public function mxchat_brave_image_count_callback() {
8636 - $brave_image_count = isset($this->options['brave_image_count'])
8637 - ? intval($this->options['brave_image_count'])
8638 - : 4;
8639 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
3325 + $options = get_option('mxchat_options');
3326 + $brave_image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
8640 3327
8641 - echo '<div class="mxchat-field-wrapper">';
8642 - echo sprintf(
8643 - '<input type="number" id="brave_image_count" name="brave_image_count"
8644 - value="%d" min="1" max="6" class="small-text mxchat-autosave-field" data-nonce="%s" />',
8645 - $brave_image_count,
8646 - $nonce
3328 + printf(
3329 + '<input type="number" id="brave_image_count" name="mxchat_options[brave_image_count]" value="%d" min="1" max="6" />',
3330 + $brave_image_count
8647 3331 );
8648 - echo '</div>';
8649 3332 echo '<p class="description">' . __('Select the number of images to return (1-6).', 'mxchat') . '</p>';
8650 3333 }
8651 3334
3335 +/**
3336 + * Callback for Safe Search field.
3337 + */
8652 3338 public function mxchat_brave_safe_search_callback() {
8653 - $brave_safe_search = isset($this->options['brave_safe_search'])
8654 - ? esc_attr($this->options['brave_safe_search'])
8655 - : 'strict';
8656 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
3339 + $options = get_option('mxchat_options');
3340 + $brave_safe_search = isset($options['brave_safe_search']) ? esc_attr($options['brave_safe_search']) : 'strict';
8657 3341
8658 - echo '<div class="mxchat-field-wrapper">';
8659 - echo '<select id="brave_safe_search" name="brave_safe_search" class="mxchat-autosave-field" data-nonce="' . $nonce . '">';
8660 - echo sprintf(
8661 - '<option value="strict" %s>%s</option>',
8662 - selected($brave_safe_search, 'strict', false),
8663 - __('Strict', 'mxchat')
8664 - );
8665 - echo sprintf(
8666 - '<option value="off" %s>%s</option>',
8667 - selected($brave_safe_search, 'off', false),
8668 - __('Off', 'mxchat')
8669 - );
8670 - echo '</select>';
8671 - echo '</div>';
8672 - echo '<p class="description">' .
8673 - esc_html__('Set the Safe Search level for image searches. Brave Search only supports "Strict" and "Off" options.', 'mxchat') .
8674 - '</p>';
3342 + ?>
3343 + <select id="brave_safe_search" name="mxchat_options[brave_safe_search]">
3344 + <option value="strict" <?php selected($brave_safe_search, 'strict'); ?>><?php _e('Strict', 'mxchat'); ?></option>
3345 + <option value="off" <?php selected($brave_safe_search, 'off'); ?>><?php _e('Off', 'mxchat'); ?></option>
3346 + </select>
3347 + <p class="description"><?php _e('Set the Safe Search level for image searches. Brave Search only supports "Strict" and "Off" options.', 'mxchat'); ?></p>
3348 + <?php
8675 3349 }
8676 3350
3351 +
3352 +/**
3353 + * Callback for Number of News Articles field.
3354 + */
8677 3355 public function mxchat_brave_news_count_callback() {
8678 - $brave_news_count = isset($this->options['brave_news_count'])
8679 - ? intval($this->options['brave_news_count'])
8680 - : 3;
8681 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
3356 + $options = get_option('mxchat_options');
3357 + $brave_news_count = isset($options['brave_news_count']) ? intval($options['brave_news_count']) : 3;
8682 3358
8683 - echo '<div class="mxchat-field-wrapper">';
8684 - echo sprintf(
8685 - '<input type="number" id="brave_news_count" name="brave_news_count"
8686 - value="%d" min="1" max="10" class="small-text mxchat-autosave-field" data-nonce="%s" />',
8687 - $brave_news_count,
8688 - $nonce
3359 + printf(
3360 + '<input type="number" id="brave_news_count" name="mxchat_options[brave_news_count]" value="%d" min="1" max="10" />',
3361 + $brave_news_count
8689 3362 );
8690 - echo '</div>';
8691 - echo '<p class="description">' . esc_html__('Select the number of news articles to retrieve (1-10).', 'mxchat') . '</p>';
3363 + echo '<p class="description">' . __('Select the number of news articles to retrieve (1-10).', 'mxchat') . '</p>';
8692 3364 }
8693 3365
3366 +/**
3367 + * Callback for Country field.
3368 + */
8694 3369 public function mxchat_brave_country_callback() {
8695 - $brave_country = isset($this->options['brave_country'])
8696 - ? esc_attr($this->options['brave_country'])
8697 - : 'us';
8698 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
3370 + $options = get_option('mxchat_options');
3371 + $brave_country = isset($options['brave_country']) ? esc_attr($options['brave_country']) : 'us';
8699 3372
8700 - echo '<div class="mxchat-field-wrapper">';
8701 - echo sprintf(
8702 - '<input type="text" id="brave_country" name="brave_country"
8703 - value="%s" maxlength="2" class="small-text mxchat-autosave-field" data-nonce="%s" />',
8704 - $brave_country,
8705 - $nonce
3373 + printf(
3374 + '<input type="text" id="brave_country" name="mxchat_options[brave_country]" value="%s" maxlength="2" class="small-text" />',
3375 + $brave_country
8706 3376 );
8707 - echo '</div>';
8708 - echo '<p class="description">' . esc_html__('Enter the country code (e.g., "us" for United States).', 'mxchat') . '</p>';
3377 + echo '<p class="description">' . __('Enter the country code (e.g., "us" for United States).', 'mxchat') . '</p>';
8709 3378 }
8710 3379
3380 +/**
3381 + * Callback for Language field.
3382 + */
8711 3383 public function mxchat_brave_language_callback() {
8712 - $brave_language = isset($this->options['brave_language'])
8713 - ? esc_attr($this->options['brave_language'])
8714 - : 'en';
8715 - $nonce = wp_create_nonce('mxchat_autosave_nonce');
3384 + $options = get_option('mxchat_options');
3385 + $brave_language = isset($options['brave_language']) ? esc_attr($options['brave_language']) : 'en';
8716 3386
8717 - echo '<div class="mxchat-field-wrapper">';
8718 - echo sprintf(
8719 - '<input type="text" id="brave_language" name="brave_language"
8720 - value="%s" maxlength="2" class="small-text mxchat-autosave-field" data-nonce="%s" />',
8721 - $brave_language,
8722 - $nonce
3387 + printf(
3388 + '<input type="text" id="brave_language" name="mxchat_options[brave_language]" value="%s" maxlength="2" class="small-text" />',
3389 + $brave_language
8723 3390 );
8724 - echo '</div>';
8725 - echo '<p class="description">' . esc_html__('Enter the language code (e.g., "en" for English).', 'mxchat') . '</p>';
3391 + echo '<p class="description">' . __('Enter the language code (e.g., "en" for English).', 'mxchat') . '</p>';
8726 3392 }
8727 3393
8728 3394
8729 3395
@@ -8734,1318 +3400,607 @@
8734 3400 echo '<p>' . esc_html__('Configure the intent settings for the Chat with PDF feature.', 'mxchat') . '</p>';
8735 3401 }
8736 3402
8737 3403 public function mxchat_chat_toolbar_toggle_callback() {
8738 - // Get chat toolbar toggle value with fallback
8739 - $chat_toolbar_toggle = isset($this->options['chat_toolbar_toggle']) ? $this->options['chat_toolbar_toggle'] : 'off';
8740 - $checked = ($chat_toolbar_toggle === 'on') ? 'checked' : '';
3404 + $checked = isset($this->options['chat_toolbar_toggle']) && $this->options['chat_toolbar_toggle'] === 'on' ? 'checked' : '';
3405 + $disabled = $this->is_activated ? '' : 'disabled';
3406 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8741 3407
8742 - // Output the toggle switch
3408 + echo '<div class="' . esc_attr($class) . '">';
8743 3409 echo '<label class="toggle-switch">';
8744 - echo sprintf(
8745 - '<input type="checkbox" id="chat_toolbar_toggle" name="chat_toolbar_toggle" value="on" %s />',
8746 - esc_attr($checked)
3410 + printf(
3411 + '<input type="checkbox" id="chat_toolbar_toggle" name="mxchat_options[chat_toolbar_toggle]" value="on" %s %s>',
3412 + esc_attr($checked),
3413 + esc_attr($disabled)
8747 3414 );
8748 3415 echo '<span class="slider"></span>';
8749 3416 echo '</label>';
8750 3417
8751 - echo '<p class="description">' . esc_html__('Enable to display the chat toolbar, adding two icons below the chatbot input field for uploading PDF and Word documents (default is hidden).', 'mxchat') . '</p>';
8752 -}
3418 + if (!$this->is_activated) {
3419 + echo '<div class="pro-feature-overlay">';
3420 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3421 + echo '</div>';
3422 + }
8753 3423
8754 -/**
8755 - * Callback for PDF upload button toggle setting
8756 - */
8757 -public function mxchat_show_pdf_upload_button_callback() {
8758 - // Get toggle value with fallback
8759 - $show_pdf_button = isset($this->options['show_pdf_upload_button']) ? $this->options['show_pdf_upload_button'] : 'on';
8760 - $checked = ($show_pdf_button === 'on') ? 'checked' : '';
8761 -
8762 - // Output the toggle switch
8763 - echo '<label class="toggle-switch">';
8764 - echo sprintf(
8765 - '<input type="checkbox" id="show_pdf_upload_button" name="show_pdf_upload_button" value="on" %s />',
8766 - esc_attr($checked)
8767 - );
8768 - echo '<span class="slider"></span>';
8769 - echo '</label>';
8770 -
8771 - echo '<p class="description">' . esc_html__('Enable to show the PDF upload button in the chatbot toolbar. Disable to hide it.', 'mxchat') . '</p>';
3424 + echo '</div>';
3425 +echo '<p class="description">Enable to display the chat toolbar, adding two icons below the chatbot input field for uploading PDF and Word documents (default is hidden).</p>';
8772 3426 }
8773 3427
8774 -/**
8775 - * Callback for Word upload button toggle setting
8776 - */
8777 -public function mxchat_show_word_upload_button_callback() {
8778 - // Get toggle value with fallback
8779 - $show_word_button = isset($this->options['show_word_upload_button']) ? $this->options['show_word_upload_button'] : 'on';
8780 - $checked = ($show_word_button === 'on') ? 'checked' : '';
8781 3428
8782 - // Output the toggle switch
8783 - echo '<label class="toggle-switch">';
8784 - echo sprintf(
8785 - '<input type="checkbox" id="show_word_upload_button" name="show_word_upload_button" value="on" %s />',
8786 - esc_attr($checked)
8787 - );
8788 - echo '<span class="slider"></span>';
8789 - echo '</label>';
8790 3429
8791 - echo '<p class="description">' . esc_html__('Enable to show the Word document upload button in the chatbot toolbar. Disable to hide it.', 'mxchat') . '</p>';
8792 -}
8793 -
3430 +// Intent Trigger Text Callback
8794 3431 public function mxchat_pdf_intent_trigger_text_callback() {
8795 - $default_text = __("Please provide the URL to the PDF you'd like to discuss.", 'mxchat');
3432 + $disabled = $this->is_activated ? '' : 'disabled';
3433 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8796 3434
8797 - echo sprintf(
8798 - '<textarea id="pdf_intent_trigger_text"
8799 - name="pdf_intent_trigger_text"
8800 - rows="3"
8801 - cols="50"
8802 - placeholder="%s">%s</textarea>',
8803 - esc_attr__('Enter trigger text', 'mxchat'),
8804 - isset($this->options['pdf_intent_trigger_text'])
8805 - ? esc_textarea($this->options['pdf_intent_trigger_text'])
8806 - : esc_textarea($default_text)
3435 + echo '<div class="' . esc_attr($class) . '">';
3436 + printf(
3437 + '<textarea id="pdf_intent_trigger_text" name="mxchat_options[pdf_intent_trigger_text]" rows="3" cols="50" placeholder="Enter trigger text" %s>%s</textarea>',
3438 + esc_attr($disabled),
3439 + isset($this->options['pdf_intent_trigger_text']) ? esc_textarea($this->options['pdf_intent_trigger_text']) : ''
8807 3440 );
8808 - echo '<p class="description">' . esc_html__('Text displayed when the intent is triggered.', 'mxchat') . '</p>';
3441 + echo '<p class="description">Text displayed when the intent is triggered.</p>';
3442 +
3443 + if (!$this->is_activated) {
3444 + echo '<div class="pro-feature-overlay">';
3445 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3446 + echo '</div>';
3447 + }
3448 + echo '</div>';
8809 3449 }
8810 3450
3451 +// Success Text Callback
8811 3452 public function mxchat_pdf_intent_success_text_callback() {
8812 - $default_text = __("I've processed the PDF. What questions do you have about it?", 'mxchat');
3453 + $disabled = $this->is_activated ? '' : 'disabled';
3454 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8813 3455
8814 - echo sprintf(
8815 - '<textarea id="pdf_intent_success_text"
8816 - name="pdf_intent_success_text"
8817 - rows="3"
8818 - cols="50"
8819 - placeholder="%s">%s</textarea>',
8820 - esc_attr__('Enter success text', 'mxchat'),
8821 - isset($this->options['pdf_intent_success_text'])
8822 - ? esc_textarea($this->options['pdf_intent_success_text'])
8823 - : esc_textarea($default_text)
3456 + echo '<div class="' . esc_attr($class) . '">';
3457 + printf(
3458 + '<textarea id="pdf_intent_success_text" name="mxchat_options[pdf_intent_success_text]" rows="3" cols="50" placeholder="Enter success text" %s>%s</textarea>',
3459 + esc_attr($disabled),
3460 + isset($this->options['pdf_intent_success_text']) ? esc_textarea($this->options['pdf_intent_success_text']) : ''
8824 3461 );
8825 - echo '<p class="description">' . esc_html__('Text displayed when the intent is successful.', 'mxchat') . '</p>';
3462 + echo '<p class="description">Text displayed when the intent is successful.</p>';
3463 +
3464 + if (!$this->is_activated) {
3465 + echo '<div class="pro-feature-overlay">';
3466 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3467 + echo '</div>';
3468 + }
3469 + echo '</div>';
8826 3470 }
8827 3471
3472 +// Error Text Callback
8828 3473 public function mxchat_pdf_intent_error_text_callback() {
8829 - $default_text = __("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
3474 + $disabled = $this->is_activated ? '' : 'disabled';
3475 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8830 3476
8831 - echo sprintf(
8832 - '<textarea id="pdf_intent_error_text"
8833 - name="pdf_intent_error_text"
8834 - rows="3"
8835 - cols="50"
8836 - placeholder="%s">%s</textarea>',
8837 - esc_attr__('Enter error text', 'mxchat'),
8838 - isset($this->options['pdf_intent_error_text'])
8839 - ? esc_textarea($this->options['pdf_intent_error_text'])
8840 - : esc_textarea($default_text)
3477 + echo '<div class="' . esc_attr($class) . '">';
3478 + printf(
3479 + '<textarea id="pdf_intent_error_text" name="mxchat_options[pdf_intent_error_text]" rows="3" cols="50" placeholder="Enter error text" %s>%s</textarea>',
3480 + esc_attr($disabled),
3481 + isset($this->options['pdf_intent_error_text']) ? esc_textarea($this->options['pdf_intent_error_text']) : ''
8841 3482 );
8842 - echo '<p class="description">' . esc_html__('Text displayed when an error occurs during the intent.', 'mxchat') . '</p>';
3483 + echo '<p class="description">Text displayed when an error occurs during the intent.</p>';
3484 +
3485 + if (!$this->is_activated) {
3486 + echo '<div class="pro-feature-overlay">';
3487 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3488 + echo '</div>';
3489 + }
3490 + echo '</div>';
8843 3491 }
8844 3492
3493 +// Maximum PDF Pages Callback
8845 3494 public function mxchat_pdf_max_pages_callback() {
3495 + $disabled = $this->is_activated ? '' : 'disabled';
3496 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8846 3497 $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
8847 3498
8848 - echo sprintf(
8849 - '<input type="range"
8850 - id="pdf_max_pages"
8851 - name="pdf_max_pages"
8852 - min="1"
8853 - max="69"
8854 - value="%d"
8855 - class="range-slider" />',
8856 - esc_attr($max_pages)
3499 + echo '<div class="' . esc_attr($class) . '">';
3500 + printf(
3501 + '<input type="range" id="pdf_max_pages" name="mxchat_options[pdf_max_pages]" min="1" max="69" value="%d" %s oninput="document.getElementById(\'pdf_max_pages_output\').innerText = this.value;" />',
3502 + esc_attr($max_pages),
3503 + esc_attr($disabled)
8857 3504 );
8858 3505 echo '<span id="pdf_max_pages_output">' . esc_html($max_pages) . '</span>';
8859 - echo '<p class="description">' . esc_html__('Set the maximum number of document pages users can upload for processing. (1-69 pages)', 'mxchat') . '</p>';
3506 + echo '<p class="description">Set the maximum number of document pages users can upload for processing. (1-69 pages)</p>';
3507 +
3508 + if (!$this->is_activated) {
3509 + echo '<div class="pro-feature-overlay">';
3510 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3511 + echo '</div>';
3512 + }
3513 + echo '</div>';
8860 3514 }
8861 3515
3516 +
3517 +// Status Toggle Callback
8862 3518 public function mxchat_live_agent_status_callback() {
8863 - // Always get fresh options instead of using cached $this->options
8864 - $fresh_options = get_option('mxchat_options');
8865 - $status = isset($fresh_options['live_agent_status']) ? $fresh_options['live_agent_status'] : 'off';
3519 + $disabled = $this->is_activated ? '' : 'disabled';
3520 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
3521 + $status = isset($this->options['live_agent_status']) ? $this->options['live_agent_status'] : 'offline';
8866 3522
8867 - echo '<label class="toggle-switch">';
8868 - echo sprintf(
8869 - '<input type="checkbox" id="live_agent_status" name="live_agent_status" value="on" %s />',
8870 - checked($status, 'on', false)
3523 + echo '<div class="' . esc_attr($class) . '">';
3524 + echo '<label class="mxchat-switch">';
3525 + printf(
3526 + '<input type="checkbox" id="live_agent_status" name="mxchat_options[live_agent_status]" value="online" %s %s />',
3527 + checked($status, 'online', false),
3528 + esc_attr($disabled)
8871 3529 );
8872 - echo '<span class="slider"></span>';
3530 + echo '<span class="mxchat-slider"></span>';
8873 3531 echo '</label>';
8874 3532 echo '<label for="live_agent_status" class="mxchat-status-label">';
8875 - echo '<span class="status-text">' . ($status === 'on' ? esc_html__('Online', 'mxchat') : esc_html__('Offline', 'mxchat')) . '</span>';
3533 + echo '<span class="status-text">' . ($status === 'online' ? esc_html__('Online', 'mxchat') : esc_html__('Offline', 'mxchat')) . '</span>';
8876 3534 echo '</label>';
8877 -}
8878 3535
8879 -/**
8880 - * Availability-schedule editor (plans 8ccaa2 + 99d7a4).
8881 - *
8882 - * ONE callback, parameterized by channel ('slack' | 'telegram' via the
8883 - * add_settings_field $args) — the markup and CSS are shared, only the bound
8884 - * option and the helper text differ. Each channel's editor renders under its
8885 - * own Integrations tab and governs ONLY that channel's handoff. While the
8886 - * master toggle is off the day grid is inert and handoff availability stays
8887 - * governed solely by that channel's manual status toggle.
8888 - *
8889 - * The day inputs carry NO name attribute and are marked .mxchat-la-field so the
8890 - * generic autosave skips them; the editor JS folds them into the channel's
8891 - * hidden live_agent_schedule_<channel> input and fires one change, reusing the
8892 - * existing autosave transport rather than growing a second one. All hooks the
8893 - * JS needs are CLASSES scoped inside .mxchat-la-schedule, never ids — the
8894 - * markup exists twice on the page.
8895 - */
8896 -public function mxchat_live_agent_schedule_callback($args = array()) {
8897 - if (!class_exists('MxChat_Live_Agent_Schedule')) {
8898 - return;
3536 + if (!$this->is_activated) {
3537 + echo '<div class="pro-feature-overlay">';
3538 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3539 + echo '</div>';
8899 3540 }
8900 - $channel = (isset($args['channel']) && $args['channel'] === 'telegram') ? 'telegram' : 'slack';
8901 - $field = 'live_agent_schedule_' . $channel;
8902 - $sched = MxChat_Live_Agent_Schedule::get($channel);
8903 - $labels = MxChat_Live_Agent_Schedule::day_labels();
8904 - $tz = MxChat_Live_Agent_Schedule::timezone_label();
8905 - $enabled = !empty($sched['enabled']);
8906 - $channel_label = ($channel === 'telegram') ? __('Telegram', 'mxchat') : __('Slack', 'mxchat');
8907 - ?>
8908 - <div class="mxchat-la-schedule<?php echo $enabled ? ' is-active' : ''; ?>"
8909 - id="mxchat-la-schedule-<?php echo esc_attr($channel); ?>"
8910 - data-channel="<?php echo esc_attr($channel); ?>">
8911 - <label class="toggle-switch">
8912 - <input type="checkbox" id="<?php echo esc_attr($field); ?>_enabled"
8913 - class="mxchat-la-field mxchat-la-enabled" <?php checked($enabled); ?> />
8914 - <span class="slider"></span>
8915 - </label>
8916 - <label for="<?php echo esc_attr($field); ?>_enabled" class="mxchat-status-label">
8917 - <span class="status-text mxchat-la-status-text">
8918 - <?php echo $enabled
8919 - ? esc_html__('Scheduled hours', 'mxchat')
8920 - : esc_html__('Always available', 'mxchat'); ?>
8921 - </span>
8922 - </label>
8923 -
8924 - <div class="mxchat-la-days" aria-hidden="<?php echo $enabled ? 'false' : 'true'; ?>">
8925 - <?php foreach ($labels as $n => $label) :
8926 - $day = $sched['days'][$n];
8927 - $on = !empty($day['enabled']);
8928 - ?>
8929 - <div class="mxchat-la-day<?php echo $on ? ' is-on' : ''; ?>" data-day="<?php echo esc_attr($n); ?>">
8930 - <label class="mxchat-la-day-label">
8931 - <input type="checkbox" class="mxchat-la-field mxchat-la-day-enabled"
8932 - data-day="<?php echo esc_attr($n); ?>" <?php checked($on); ?> />
8933 - <span class="mxchat-la-day-name"><?php echo esc_html($label); ?></span>
8934 - </label>
8935 - <div class="mxchat-la-times">
8936 - <input type="time" class="mxchat-la-field mxchat-la-start"
8937 - data-day="<?php echo esc_attr($n); ?>"
8938 - value="<?php echo esc_attr($day['start']); ?>"
8939 - aria-label="<?php echo esc_attr(sprintf(__('%s start time', 'mxchat'), $label)); ?>" />
8940 - <span class="mxchat-la-dash">&ndash;</span>
8941 - <input type="time" class="mxchat-la-field mxchat-la-end"
8942 - data-day="<?php echo esc_attr($n); ?>"
8943 - value="<?php echo esc_attr($day['end']); ?>"
8944 - aria-label="<?php echo esc_attr(sprintf(__('%s end time', 'mxchat'), $label)); ?>" />
8945 - </div>
8946 - </div>
8947 - <?php endforeach; ?>
8948 - </div>
8949 -
8950 - <input type="hidden" name="<?php echo esc_attr($field); ?>" id="<?php echo esc_attr($field); ?>"
8951 - class="mxchat-la-hidden" value="<?php echo esc_attr(wp_json_encode($sched)); ?>" />
8952 - </div>
8953 - <p class="description">
8954 - <?php printf(
8955 - /* translators: 1: the handoff channel, e.g. Slack or Telegram; 2: the site's timezone, e.g. America/New_York */
8956 - esc_html__('When on, visitors are only offered a %1$s human handoff during these hours — outside them the chatbot keeps answering and never offers to fetch an agent. Applies to %1$s only. Times use the site timezone (%2$s). Set a day\'s end time earlier than its start for an overnight shift; identical start and end means all day.', 'mxchat'),
8957 - '<strong>' . esc_html($channel_label) . '</strong>',
8958 - '<strong>' . esc_html($tz) . '</strong>'
8959 - ); ?>
8960 - </p>
8961 - <?php
3541 + echo '</div>';
8962 3542 }
8963 3543
3544 +// Away Message Callback
8964 3545 public function mxchat_live_agent_away_message_callback() {
3546 + $disabled = $this->is_activated ? '' : 'disabled';
3547 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8965 3548 $message = isset($this->options['live_agent_away_message'])
8966 3549 ? $this->options['live_agent_away_message']
8967 3550 : __('Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.', 'mxchat');
8968 3551
3552 + echo '<div class="' . esc_attr($class) . '">';
8969 3553 printf(
8970 - '<textarea id="live_agent_away_message" name="live_agent_away_message" rows="3" cols="50">%s</textarea>',
3554 + '<textarea id="live_agent_away_message" name="mxchat_options[live_agent_away_message]" rows="3" cols="50" %s>%s</textarea>',
3555 + esc_attr($disabled),
8971 3556 esc_textarea($message)
8972 3557 );
8973 3558 echo '<p class="description">' . esc_html__('Message shown when live agents are offline.', 'mxchat') . '</p>';
3559 +
3560 + if (!$this->is_activated) {
3561 + echo '<div class="pro-feature-overlay">';
3562 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3563 + echo '</div>';
3564 + }
3565 + echo '</div>';
8974 3566 }
8975 -
8976 3567 public function mxchat_live_agent_notification_message_callback() {
3568 + $disabled = $this->is_activated ? '' : 'disabled';
3569 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8977 3570 $message = isset($this->options['live_agent_notification_message'])
8978 3571 ? $this->options['live_agent_notification_message']
8979 3572 : __('Live agent has been notified.', 'mxchat');
8980 3573
3574 + echo '<div class="' . esc_attr($class) . '">';
8981 3575 printf(
8982 - '<textarea id="live_agent_notification_message" name="live_agent_notification_message" rows="3" cols="50">%s</textarea>',
3576 + '<textarea id="live_agent_notification_message" name="mxchat_options[live_agent_notification_message]" rows="3" cols="50" %s>%s</textarea>',
3577 + esc_attr($disabled),
8983 3578 esc_textarea($message)
8984 3579 );
8985 3580 echo '<p class="description">' . esc_html__('Message shown when live transfer activated.', 'mxchat') . '</p>';
3581 +
3582 + if (!$this->is_activated) {
3583 + echo '<div class="pro-feature-overlay">';
3584 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3585 + echo '</div>';
3586 + }
3587 + echo '</div>';
8986 3588 }
8987 3589
8988 3590 public function mxchat_live_agent_webhook_url_callback() {
3591 + $disabled = $this->is_activated ? '' : 'disabled';
3592 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
8989 3593 $webhook_url = isset($this->options['live_agent_webhook_url'])
8990 3594 ? esc_url($this->options['live_agent_webhook_url'])
8991 3595 : esc_url(get_option('live_agent_webhook_url', ''));
8992 3596
3597 + echo '<div class="' . esc_attr($class) . '">';
8993 3598 printf(
8994 - '<input type="password" id="live_agent_webhook_url" name="live_agent_webhook_url" value="%s" class="regular-text" />',
8995 - $webhook_url
3599 + '<input type="password" id="live_agent_webhook_url" name="mxchat_options[live_agent_webhook_url]" value="%s" class="regular-text" %s />',
3600 + $webhook_url,
3601 + esc_attr($disabled)
8996 3602 );
8997 - echo '<button type="button" id="toggleWebhookUrlVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
8998 - echo '<p class="description">' . esc_html__('Enter your Slack webhook URL for live agent notifications.', 'mxchat') . '</p>';
8999 -}
9000 -
9001 -public function mxchat_live_agent_secret_key_callback() {
9002 - printf(
9003 - '<input type="password" id="live_agent_secret_key" name="live_agent_secret_key" value="%s" class="regular-text" />',
9004 - isset($this->options['live_agent_secret_key']) ? esc_attr($this->options['live_agent_secret_key']) : ''
9005 - );
9006 - echo '<button type="button" id="toggleSecretKeyVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
9007 - echo '<p class="description">' . esc_html__('Secret key for validating Slack requests. Keep this secure.', 'mxchat') . '</p>';
9008 -}
9009 -
9010 -public function mxchat_live_agent_bot_token_callback() {
9011 - printf(
9012 - '<input type="password" id="live_agent_bot_token" name="live_agent_bot_token" value="%s" class="regular-text" />',
9013 - isset($this->options['live_agent_bot_token']) ? esc_attr($this->options['live_agent_bot_token']) : ''
9014 - );
9015 - echo '<button type="button" id="toggleBotTokenVisibility">' . esc_html__('Show', 'mxchat') . '</button>';
9016 - echo '<p class="description">' . esc_html__('Your Slack Bot OAuth Token (starts with xoxb-). Keep this secure.', 'mxchat') . '</p>';
9017 -}
9018 -
9019 -public function mxchat_live_agent_user_ids_callback() {
9020 - $user_ids = isset($this->options['live_agent_user_ids'])
9021 - ? esc_textarea($this->options['live_agent_user_ids'])
9022 - : '';
9023 -
9024 - printf(
9025 - '<textarea id="live_agent_user_ids" name="live_agent_user_ids" rows="4" class="large-text">%s</textarea>',
9026 - $user_ids
9027 - );
9028 - echo '<p class="description">' . esc_html__('Enter Slack User IDs of agents who should be automatically invited to chat channels (one per line). Find user IDs in Slack profiles under "More" → "Copy member ID". Example: U1234567890', 'mxchat') . '</p>';
9029 -}
9030 -
9031 -public function mxchat_live_agent_shared_channel_callback() {
9032 - $value = isset($this->options['live_agent_shared_channel']) ? $this->options['live_agent_shared_channel'] : '';
9033 - printf(
9034 - '<input type="text" id="live_agent_shared_channel" name="live_agent_shared_channel" value="%s" class="regular-text" placeholder="#support or C0123456789" />',
9035 - esc_attr($value)
9036 - );
9037 - echo '<p class="description">' . esc_html__('Optional. Route ALL live agent handoffs into this one existing Slack channel — each conversation becomes its own thread there. Enter a channel ID (starts with C) or a #channel-name, and invite your bot to the channel first (/invite @YourBot). Agents reply inside a conversation\'s thread; !endchat inside the thread ends that chat. Leave blank to keep creating a separate chat- channel per conversation.', 'mxchat') . '</p>';
9038 -
9039 - // Surface the last failed handoff so a wrong name / missing invite is
9040 - // visible right where it gets fixed. A successful handoff clears this.
9041 - $shared_error = get_option('mxchat_slack_shared_channel_error');
9042 - if (!empty($shared_error['error']) && trim((string) $value) !== '' && ($shared_error['configured'] ?? '') === trim((string) $value)) {
9043 - echo '<p class="description"><strong>' . esc_html__('⚠ Last handoff could not reach this channel', 'mxchat') . '</strong> — '
9044 - . esc_html(sprintf(
9045 - /* translators: %s: Slack API error code */
9046 - __('Slack said "%s". Handoffs are falling back to per-conversation channels until this is fixed. Check the channel name or ID and make sure the bot has been invited to it.', 'mxchat'),
9047 - $shared_error['error']
9048 - )) . '</p>';
3603 + echo '<button type="button" id="toggleWebhookUrlVisibility">Show</button>';
3604 + echo '<p class="description">Enter your Slack webhook URL for live agent notifications.</p>';
3605 + if (!$this->is_activated) {
3606 + echo '<div class="pro-feature-overlay">';
3607 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3608 + echo '</div>';
9049 3609 }
3610 + echo '</div>';
9050 3611 }
9051 3612
9052 -public function mxchat_live_agent_archive_on_end_callback() {
9053 - $value = isset($this->options['live_agent_archive_on_end_toggle']) ? $this->options['live_agent_archive_on_end_toggle'] : 'off';
9054 - printf(
9055 - '<input type="checkbox" id="live_agent_archive_on_end_toggle" name="live_agent_archive_on_end_toggle" value="on" %s />',
9056 - checked('on', $value, false)
9057 - );
9058 - echo '<label for="live_agent_archive_on_end_toggle" class="mxchat-status-label">' . esc_html__('Archive the conversation\'s chat- channel when an agent ends the chat with !endchat', 'mxchat') . '</label>';
9059 - echo '<p class="description">' . esc_html__('Keeps your Slack sidebar tidy on busy sites — each ended conversation\'s channel is archived instead of living forever. Archived channels stay searchable in Slack, so the record is never lost. Only applies to per-conversation chat- channels; a Shared Handoff Channel is never archived. If a returning visitor requests an agent again, a fresh channel is created automatically.', 'mxchat') . '</p>';
9060 -}
9061 3613
9062 -/**
9063 - * Telegram Integration Callbacks
9064 - */
9065 -public function mxchat_telegram_section_callback() {
9066 - echo '<p>' . esc_html__('Configure Telegram integration for live agent support.', 'mxchat') . '</p>';
3614 +public function mxchat_similarity_threshold_callback() {
3615 + $threshold = get_option('mxchat_similarity_threshold', 80); // Default to 80
3616 + ?>
3617 + <input type="range" id="mxchat_similarity_threshold" name="mxchat_similarity_threshold"
3618 + min="70" max="85" step="1"
3619 + value="<?php echo esc_attr($threshold); ?>"
3620 + oninput="document.getElementById('mxchat_threshold_value').textContent = this.value;">
3621 + <span id="mxchat_threshold_value"><?php echo esc_html($threshold); ?></span>
3622 + <p class="description">
3623 + Set the similarity threshold (recommended: 75) to balance accuracy; too high might limit your bot's ability to find relevant knowledge.
3624 + </p>
3625 + <?php
9067 3626 }
9068 3627
9069 -public function mxchat_telegram_status_callback() {
9070 - $fresh_options = get_option('mxchat_options');
9071 - $status = isset($fresh_options['telegram_status']) ? $fresh_options['telegram_status'] : 'off';
9072 3628
9073 - echo '<label class="toggle-switch">';
9074 - echo sprintf(
9075 - '<input type="checkbox" id="telegram_status" name="telegram_status" value="on" %s />',
9076 - checked($status, 'on', false)
9077 - );
9078 - echo '<span class="slider"></span>';
9079 - echo '</label>';
9080 - echo '<label for="telegram_status" class="mxchat-status-label">';
9081 - echo '<span class="status-text">' . ($status === 'on' ? esc_html__('Online', 'mxchat') : esc_html__('Offline', 'mxchat')) . '</span>';
9082 - echo '</label>';
9083 -}
9084 3629
9085 -public function mxchat_telegram_notification_message_callback() {
9086 - $message = isset($this->options['telegram_notification_message'])
9087 - ? $this->options['telegram_notification_message']
9088 - : __("I've notified a support agent. Please allow a moment for them to respond.", 'mxchat');
3630 +public function mxchat_live_agent_secret_key_callback() {
3631 + $disabled = $this->is_activated ? '' : 'disabled';
3632 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
9089 3633
3634 + echo '<div class="' . esc_attr($class) . '">';
9090 3635 printf(
9091 - '<textarea id="telegram_notification_message" name="telegram_notification_message" rows="3" cols="50">%s</textarea>',
9092 - esc_textarea($message)
3636 + '<input type="password" id="live_agent_secret_key" name="mxchat_options[live_agent_secret_key]" value="%s" class="regular-text" %s />',
3637 + isset($this->options['live_agent_secret_key']) ? esc_attr($this->options['live_agent_secret_key']) : '',
3638 + esc_attr($disabled)
9093 3639 );
9094 - echo '<p class="description">' . esc_html__('Message shown when live transfer activated.', 'mxchat') . '</p>';
9095 -}
3640 + echo '<button type="button" id="toggleSecretKeyVisibility">Show</button>';
3641 + echo '<p class="description">Secret key for validating Slack requests. Keep this secure.</p>';
9096 3642
9097 -public function mxchat_telegram_away_message_callback() {
9098 - $message = isset($this->options['telegram_away_message'])
9099 - ? $this->options['telegram_away_message']
9100 - : __('Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.', 'mxchat');
9101 -
9102 - printf(
9103 - '<textarea id="telegram_away_message" name="telegram_away_message" rows="3" cols="50">%s</textarea>',
9104 - esc_textarea($message)
9105 - );
9106 - echo '<p class="description">' . esc_html__('Message shown when live agents are offline.', 'mxchat') . '</p>';
3643 + if (!$this->is_activated) {
3644 + echo '<div class="pro-feature-overlay">';
3645 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3646 + echo '</div>';
3647 + }
3648 + echo '</div>';
9107 3649 }
9108 3650
9109 -public function mxchat_telegram_bot_token_callback() {
9110 - printf(
9111 - '<input type="password" id="telegram_bot_token" name="telegram_bot_token" value="%s" class="regular-text" />',
9112 - isset($this->options['telegram_bot_token']) ? esc_attr($this->options['telegram_bot_token']) : ''
9113 - );
9114 - echo '<button type="button" class="button button-secondary" onclick="var f=document.getElementById(\'telegram_bot_token\'); if(f.type===\'password\'){f.type=\'text\';this.textContent=\'' . esc_js(__('Hide', 'mxchat')) . '\';}else{f.type=\'password\';this.textContent=\'' . esc_js(__('Show', 'mxchat')) . '\';}">' . esc_html__('Show', 'mxchat') . '</button>';
9115 - echo '<p class="description">' . esc_html__('Your Telegram Bot Token from @BotFather (e.g., 123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ).', 'mxchat') . '</p>';
9116 -}
3651 +public function mxchat_live_agent_bot_token_callback() {
3652 + $disabled = $this->is_activated ? '' : 'disabled';
3653 + $class = $this->is_activated ? 'pro-feature-wrapper active' : 'pro-feature-wrapper inactive';
9117 3654
9118 -public function mxchat_telegram_group_id_callback() {
3655 + echo '<div class="' . esc_attr($class) . '">';
9119 3656 printf(
9120 - '<input type="text" id="telegram_group_id" name="telegram_group_id" value="%s" class="regular-text" />',
9121 - isset($this->options['telegram_group_id']) ? esc_attr($this->options['telegram_group_id']) : ''
3657 + '<input type="password" id="live_agent_bot_token" name="mxchat_options[live_agent_bot_token]" value="%s" class="regular-text" %s />',
3658 + isset($this->options['live_agent_bot_token']) ? esc_attr($this->options['live_agent_bot_token']) : '',
3659 + esc_attr($disabled)
9122 3660 );
9123 - echo '<p class="description">' . esc_html__('Your Telegram supergroup ID (starts with -100). The group must have forum topics enabled.', 'mxchat') . '</p>';
9124 -}
3661 + echo '<button type="button" id="toggleBotTokenVisibility">Show</button>';
3662 + echo '<p class="description">Your Slack Bot OAuth Token (starts with xoxb-). Keep this secure.</p>';
9125 3663
9126 -public function mxchat_telegram_webhook_secret_callback() {
9127 - $secret = isset($this->options['telegram_webhook_secret']) ? $this->options['telegram_webhook_secret'] : '';
9128 -
9129 - // Auto-generate secret if empty
9130 - if (empty($secret)) {
9131 - $secret = wp_generate_password(64, false, false);
9132 - $options = get_option('mxchat_options', []);
9133 - $options['telegram_webhook_secret'] = $secret;
9134 - update_option('mxchat_options', $options);
9135 - $this->options['telegram_webhook_secret'] = $secret;
3664 + if (!$this->is_activated) {
3665 + echo '<div class="pro-feature-overlay">';
3666 + echo '<a href="https://mxchat.ai/" target="_blank"><img src="' . plugin_dir_url(__FILE__) . '../images/pro-only-dark.png" alt="Pro Only" /></a>';
3667 + echo '</div>';
9136 3668 }
9137 -
9138 - printf(
9139 - '<input type="password" id="telegram_webhook_secret" name="telegram_webhook_secret" value="%s" class="regular-text" />',
9140 - esc_attr($secret)
9141 - );
9142 - echo '<button type="button" class="button button-secondary" onclick="var f=document.getElementById(\'telegram_webhook_secret\'); if(f.type===\'password\'){f.type=\'text\';this.textContent=\'' . esc_js(__('Hide', 'mxchat')) . '\';}else{f.type=\'password\';this.textContent=\'' . esc_js(__('Show', 'mxchat')) . '\';}">' . esc_html__('Show', 'mxchat') . '</button>';
9143 - echo '<p class="description">' . esc_html__('Secret token for webhook verification. Use this when setting up your Telegram webhook with the secret_token parameter.', 'mxchat') . '</p>';
9144 -}
9145 -
9146 -public function mxchat_similarity_threshold_callback() {
9147 - // Load from mxchat_options array
9148 - $options = get_option('mxchat_options', []);
9149 -
9150 - // Get value from options array with default of 35
9151 - $threshold = isset($options['similarity_threshold']) ? $options['similarity_threshold'] : 35;
9152 -
9153 - echo '<div class="slider-container">';
9154 - echo sprintf(
9155 - '<input type="range"
9156 - id="similarity_threshold"
9157 - name="similarity_threshold"
9158 - min="20"
9159 - max="85"
9160 - step="1"
9161 - value="%s"
9162 - class="range-slider" />',
9163 - esc_attr($threshold)
9164 - );
9165 - echo sprintf(
9166 - '<span id="threshold_value" class="range-value">%s</span>',
9167 - esc_html($threshold)
9168 - );
9169 3669 echo '</div>';
9170 3670 }
9171 3671
9172 -public function mxchat_max_input_length_callback() {
9173 - // Load from mxchat_options array (plan a3fae2 part C).
9174 - $options = get_option('mxchat_options', []);
3672 + public function mxchat_enqueue_admin_assets() {
3673 + wp_enqueue_style('wp-color-picker');
9175 3674
9176 - // 0 = unlimited (default — preserves current behavior, no cap).
9177 - $max_input_length = isset($options['max_input_length']) ? intval($options['max_input_length']) : 0;
3675 + // Get the plugin version or file modification time for cache busting
3676 + $plugin_version = '1.5.8'; // Replace this with your plugin's version
9178 3677
9179 - echo sprintf(
9180 - '<input type="number"
9181 - id="max_input_length"
9182 - name="max_input_length"
9183 - min="0"
9184 - max="100000"
9185 - step="1"
9186 - value="%s"
9187 - placeholder="0"
9188 - class="mxch-input mxch-input-sm" />',
9189 - esc_attr($max_input_length)
9190 - );
9191 -}
3678 + // File paths
3679 + $color_picker_js_path = plugin_dir_path(__FILE__) . '../js/my-color-picker.js';
3680 + $embedding_check_js_path = plugin_dir_path(__FILE__) . '../js/embedding-check.js';
3681 + $admin_css_path = plugin_dir_path(__FILE__) . '../css/admin-style.css';
3682 + $transcripts_js_path = plugin_dir_path(__FILE__) . '../js/mxchat_transcripts.js';
9192 3683
9193 -public function mxchat_rag_sources_limit_callback() {
9194 - // Load from mxchat_options array
9195 - $options = get_option('mxchat_options', []);
3684 + // Check if files exist and get modification times
3685 + $color_picker_version = file_exists($color_picker_js_path) ? filemtime($color_picker_js_path) : $plugin_version;
3686 + $embedding_check_version = file_exists($embedding_check_js_path) ? filemtime($embedding_check_js_path) : $plugin_version;
3687 + $admin_css_version = file_exists($admin_css_path) ? filemtime($admin_css_path) : $plugin_version;
3688 + $transcripts_js_version = file_exists($transcripts_js_path) ? filemtime($transcripts_js_path) : $plugin_version;
9196 3689
9197 - // Get value from options array with default of 3
9198 - $rag_sources_limit = isset($options['rag_sources_limit']) ? intval($options['rag_sources_limit']) : 3;
3690 + // Enqueue scripts and styles with corrected paths
3691 + wp_enqueue_script(
3692 + 'mxchat-color-picker',
3693 + plugin_dir_url(__FILE__) . '../js/my-color-picker.js',
3694 + array('wp-color-picker'),
3695 + $color_picker_version,
3696 + true
3697 + );
9199 3698
9200 - echo '<div class="slider-container">';
9201 - echo sprintf(
9202 - '<input type="range"
9203 - id="rag_sources_limit"
9204 - name="rag_sources_limit"
9205 - min="3"
9206 - max="10"
9207 - step="1"
9208 - value="%s"
9209 - class="range-slider" />',
9210 - esc_attr($rag_sources_limit)
9211 - );
9212 - echo sprintf(
9213 - '<span id="rag_sources_limit_value" class="range-value">%s</span>',
9214 - esc_html($rag_sources_limit)
9215 - );
9216 - echo '</div>';
9217 -}
3699 + wp_enqueue_script(
3700 + 'mxchat-embedding-check',
3701 + plugin_dir_url(__FILE__) . '../js/embedding-check.js',
3702 + array(),
3703 + $embedding_check_version,
3704 + true
3705 + );
9218 3706
9219 -public function mxchat_rag_chunks_limit_callback() {
9220 - // Load from mxchat_options array
9221 - $options = get_option('mxchat_options', []);
3707 + wp_enqueue_script(
3708 + 'mxchat-transcripts-js',
3709 + plugin_dir_url(__FILE__) . '../js/mxchat_transcripts.js',
3710 + array('jquery'),
3711 + $transcripts_js_version,
3712 + true
3713 + );
9222 3714
9223 - // Get value from options array with default of 15
9224 - $rag_chunks_limit = isset($options['rag_chunks_limit']) ? intval($options['rag_chunks_limit']) : 15;
3715 + wp_enqueue_script(
3716 + 'mxchat-admin-js',
3717 + plugin_dir_url(__FILE__) . '../js/mxchat-admin.js',
3718 + array('jquery'),
3719 + $plugin_version,
3720 + true
3721 + );
9225 3722
9226 - echo '<div class="slider-container">';
9227 - echo sprintf(
9228 - '<input type="range"
9229 - id="rag_chunks_limit"
9230 - name="rag_chunks_limit"
9231 - min="8"
9232 - max="20"
9233 - step="1"
9234 - value="%s"
9235 - class="range-slider" />',
9236 - esc_attr($rag_chunks_limit)
9237 - );
9238 - echo sprintf(
9239 - '<span id="rag_chunks_limit_value" class="range-value">%s</span>',
9240 - esc_html($rag_chunks_limit)
9241 - );
9242 - echo '</div>';
9243 -}
9244 3723
9245 -/**
9246 - * Add body class on the Onboarding wizard page so CSS-only chrome surgery
9247 - * (collapsing the WP admin sidebar) only applies on this page.
9248 - * Plan: plan-mxchat-20260527-905439.
9249 - */
9250 -public function mxchat_add_onboarding_body_class($classes) {
9251 - if (isset($_GET['page']) && $_GET['page'] === 'mxchat-onboarding') {
9252 - $classes .= ' mxchat-onboarding-focused';
9253 - }
9254 - return $classes;
9255 -}
9256 3724
9257 -public function mxchat_enqueue_admin_assets($hook_suffix = '') {
9258 - // Authorization gate (plan-mxchat-20260731-c63fb6). Previously the ONLY
9259 - // guard here was the strpos() on $_GET['page'] below — which is
9260 - // attacker-controlled — so ANY logged-in user (Subscriber included) could
9261 - // load a page they are legitimately allowed to see, e.g.
9262 - // /wp-admin/profile.php?page=mxchat, and receive every nonce this method
9263 - // localizes. Three of the handlers behind those nonces verified the nonce
9264 - // but checked no capability. Every MxChat menu page is registered
9265 - // 'manage_options' (see mxchat_add_admin_menu), so this is a no-op for
9266 - // legitimate users.
9267 - if (!current_user_can('manage_options')) {
9268 - return;
9269 - }
3725 + // Localize the script with data for the activation process
3726 + wp_localize_script('mxchat-admin-js', 'mxchatAdmin', array(
3727 + 'ajax_url' => admin_url('admin-ajax.php'),
3728 + 'nonce' => wp_create_nonce('mxchat_activate_license_nonce'),
3729 + ));
9270 3730
9271 - // Get plugin version
9272 - $version = MXCHAT_VERSION;
3731 + wp_enqueue_style(
3732 + 'mxchat-admin-css',
3733 + plugin_dir_url(__FILE__) . '../css/admin-style.css',
3734 + array(),
3735 + $admin_css_version
3736 + );
9273 3737
9274 - // Use file modification time for development (remove in production)
9275 - if (defined('WP_DEBUG') && WP_DEBUG) {
9276 - $version = filemtime(plugin_dir_path(__FILE__) . '../mxchat-basic.php');
9277 - }
3738 + wp_localize_script('mxchat-color-picker', 'mxchatStyleSettings', array(
3739 + 'ajax_url' => admin_url('admin-ajax.php'),
3740 + 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
3741 + 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
3742 + 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
3743 + 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
3744 + 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
3745 + 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
3746 + 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
3747 + 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
3748 + 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
3749 + 'icon_color' => $this->options['icon_color'] ?? '#fff',
3750 + 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
3751 + 'pre_chat_message' => $this->options['pre_chat_message'] ?? 'Hey there! Ask me anything!',
3752 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
9278 3753
9279 - $current_page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : '';
9280 - $plugin_url = plugin_dir_url(__FILE__) . '../';
3754 + // New fields for Loops Integration
3755 + 'loops_api_key' => $this->options['loops_api_key'] ?? '',
3756 + 'loops_mailing_list' => $this->options['loops_mailing_list'] ?? '',
3757 + 'triggered_phrase_response' => $this->options['triggered_phrase_response'] ?? 'Would you like to join our mailing list? Please provide your email below.',
3758 + 'email_capture_response' => $this->options['email_capture_response'] ?? 'Thank you for providing your email! You\'ve been added to our list.',
3759 + 'pdf_intent_trigger_text' => $this->options['pdf_intent_trigger_text'] ?? "Please provide the URL to the PDF you'd like to discuss.",
3760 + 'pdf_intent_success_text' => $this->options['pdf_intent_success_text'] ?? "I've processed the PDF. What questions do you have about it?",
3761 + 'pdf_intent_error_text' => $this->options['pdf_intent_error_text'] ?? "Sorry, I couldn't process the PDF. Please ensure it's a valid file.",
3762 + 'pdf_max_pages' => $this->options['pdf_max_pages'] ?? 69,
9281 3763
9282 - // Only load on MxChat pages. Prefer the server-derived $hook_suffix that
9283 - // admin_enqueue_scripts passes us — unlike $_GET['page'] it cannot be
9284 - // forged onto a screen MxChat does not own. Falls back to the legacy
9285 - // $_GET check only when the hook is unavailable (direct/legacy callers).
9286 - if ($hook_suffix !== '') {
9287 - if (strpos($hook_suffix, 'mxchat') === false) {
9288 - return;
9289 - }
9290 - } elseif (strpos($current_page, 'mxchat') === false) {
9291 - return;
9292 - }
3764 + // Live Agent Integration
3765 + 'live_agent_webhook_url' => $this->options['live_agent_webhook_url'] ?? '',
3766 + 'live_agent_secret_key' => $this->options['live_agent_secret_key'] ?? '',
3767 + 'live_agent_bot_token' => $this->options['live_agent_bot_token'] ?? '',
9293 3768
9294 - // Always load these on all MxChat pages
9295 - $this->enqueue_core_admin_assets($plugin_url, $version);
9296 - $this->enqueue_page_specific_assets($current_page, $plugin_url, $version);
9297 - $this->localize_admin_scripts($current_page);
9298 -}
9299 -private function enqueue_core_admin_assets($plugin_url, $version) {
9300 - // Core admin styles
9301 - wp_enqueue_style('mxchat-admin-css', $plugin_url . 'css/admin-style.css', array(), $version);
9302 - wp_enqueue_style('mxchat-knowledge-css', $plugin_url . 'css/knowledge-style.css', array(), $version);
3769 + 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
3770 + 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
3771 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
3772 + 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
3773 + 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
3774 + 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
9303 3775
9304 - // New sidebar navigation styles (for main settings page)
9305 - wp_enqueue_style('mxchat-admin-sidebar-css', $plugin_url . 'css/admin-sidebar.css', array(), $version);
9306 -
9307 - // Core admin scripts
9308 - wp_enqueue_script('mxchat-admin-js', $plugin_url . 'js/mxchat-admin.js', array('jquery'), $version, true);
9309 -}
9310 -private function enqueue_page_specific_assets($current_page, $plugin_url, $version) {
9311 - switch ($current_page) {
9312 - case 'mxchat-prompts':
9313 - // Knowledge processing page assets
9314 - wp_enqueue_style('mxchat-content-selector-css', $plugin_url . 'css/content-selector.css', array(), $version);
9315 - wp_enqueue_script('mxchat-content-selector-js', $plugin_url . 'js/content-selector.js', array('jquery'), $version, true);
9316 - // Add the knowledge processing script (common script needed for WordPress dismiss functionality)
9317 - wp_enqueue_script('mxchat-knowledge-processing', $plugin_url . 'js/knowledge-processing.js', array('jquery', 'common'), $version, true);
9318 - // Per-entry "View indexed content" inspector (plan-d8cb4b) reuses the
9319 - // Testing tab's match-card / chunk-detail components, which are scoped
9320 - // under .mxch-testing-results. Load that stylesheet here so the inspector
9321 - // modal renders identically to the Testing tab.
9322 - wp_enqueue_style('mxchat-admin-testing-css', $plugin_url . 'css/admin-testing-tab.css', array('mxchat-admin-sidebar-css'), $version);
9323 - break;
9324 -
9325 - case 'mxchat-transcripts':
9326 - // Load admin sidebar CSS first (shared styles for sidebar navigation)
9327 - wp_enqueue_style('mxchat-admin-sidebar-css', $plugin_url . 'css/admin-sidebar.css', array(), $version);
9328 - // Load transcripts-specific styles
9329 - wp_enqueue_style('mxchat-chat-transcripts-css', $plugin_url . 'css/chat-transcripts.css', array('mxchat-admin-sidebar-css'), $version);
9330 - wp_enqueue_script('mxchat-transcripts-js', $plugin_url . 'js/mxchat_transcripts.js', array('jquery'), $version, true);
9331 - break;
9332 -
9333 - case 'mxchat-actions':
9334 - // Load admin sidebar CSS first (shared styles for sidebar navigation)
9335 - wp_enqueue_style('mxchat-admin-sidebar-css', $plugin_url . 'css/admin-sidebar.css', array(), $version);
9336 - // Load actions-specific styles
9337 - wp_enqueue_style('mxchat-actions-css', $plugin_url . 'css/actions.css', array('mxchat-admin-sidebar-css'), $version);
9338 - wp_enqueue_script('mxchat-actions-js', $plugin_url . 'js/mxchat_actions.js', array('jquery'), $version, true);
9339 -
9340 - // Localize script data for actions page
9341 - $is_activated = get_option('mxchat_pro_license_status') === 'active';
9342 - wp_localize_script('mxchat-actions-js', 'mxchActionsData', array(
9343 - 'ajaxUrl' => admin_url('admin-ajax.php'),
9344 - 'nonce' => wp_create_nonce('mxchat_actions_nonce'),
9345 - 'addNonce' => wp_create_nonce('mxchat_add_intent_nonce'),
9346 - 'editNonce' => wp_create_nonce('mxchat_edit_intent'),
9347 - 'deleteNonce' => wp_create_nonce('mxchat_delete_intent_nonce'),
9348 - 'toggleNonce' => wp_create_nonce('mxchat_actions_nonce'),
9349 - 'addPhraseNonce' => wp_create_nonce('mxchat_add_phrase_nonce'),
9350 - 'deletePhraseNonce' => wp_create_nonce('mxchat_delete_phrase_nonce'),
9351 - 'getPhrasesNonce' => wp_create_nonce('mxchat_get_phrases_nonce'),
9352 - 'deleteLegacyNonce' => wp_create_nonce('mxchat_delete_legacy_nonce'),
9353 - 'isActivated' => $is_activated,
9354 - 'i18n' => array(
9355 - 'confirmDelete' => __('Are you sure you want to delete this trigger phrase?', 'mxchat'),
9356 - 'confirmBulkDelete' => __('Are you sure you want to delete the selected trigger phrases?', 'mxchat'),
9357 - 'saving' => __('Saving...', 'mxchat'),
9358 - 'saved' => __('Saved successfully!', 'mxchat'),
9359 - 'error' => __('An error occurred. Please try again.', 'mxchat'),
9360 - 'proRequired' => __('This feature requires MxChat Pro.', 'mxchat'),
9361 - 'addonRequired' => __('This feature requires an add-on.', 'mxchat')
9362 - )
9363 3776 ));
9364 - break;
9365 3777
9366 - case 'mxchat-activation':
9367 - // Load admin sidebar CSS first (shared styles for sidebar navigation)
9368 - wp_enqueue_style('mxchat-admin-sidebar-css', $plugin_url . 'css/admin-sidebar.css', array(), $version);
9369 - // Load pro page-specific styles
9370 - wp_enqueue_style('mxchat-pro-css', $plugin_url . 'css/admin-pro.css', array('mxchat-admin-sidebar-css'), $version);
9371 - // Load pro page JavaScript
9372 - wp_enqueue_script('mxchat-pro-js', $plugin_url . 'js/mxchat_pro.js', array('jquery'), $version, true);
9373 - // Load activation script for license activation/deactivation
9374 - wp_enqueue_script('mxchat-activation-js', $plugin_url . 'js/activation-script.js', array('jquery'), $version, true);
9375 - break;
9376 -
9377 - case 'mxchat-content':
9378 - // Load admin sidebar CSS (shared styles for sidebar navigation)
9379 - wp_enqueue_style('mxchat-admin-sidebar-css', $plugin_url . 'css/admin-sidebar.css', array(), $version);
9380 - // Load content page-specific styles
9381 - wp_enqueue_style('mxchat-content-css', $plugin_url . 'css/admin-content.css', array('mxchat-admin-sidebar-css'), $version);
9382 - // Load content page JavaScript
9383 - wp_enqueue_script('mxchat-content-js', $plugin_url . 'js/mxchat-content.js', array('jquery'), $version, true);
9384 - break;
9385 -
9386 - case 'mxchat-api-access':
9387 - // Shared sidebar shell (CSS + JS for tab switching / mobile menu / copy buttons).
9388 - wp_enqueue_style('mxchat-admin-sidebar-css', $plugin_url . 'css/admin-sidebar.css', array(), $version);
9389 - wp_enqueue_script('mxchat-admin-sidebar-js', $plugin_url . 'js/admin-sidebar.js', array(), $version, true);
9390 - wp_localize_script('mxchat-admin-sidebar-js', 'MxChatAdminSidebarI18n', array(
9391 - 'copied' => __('Copied', 'mxchat'),
9392 - ));
9393 - break;
9394 -
9395 - case 'mxchat-max':
9396 - case 'mxchat-onboarding':
9397 - // Onboarding page — uses the shared admin shell PLUS the wizard
9398 - // overlay (plan-905439). admin-onboarding-wizard.css scopes the
9399 - // WP-chrome surgery to body.mxchat-onboarding-focused so it only
9400 - // applies on THIS page. The body class is added below via the
9401 - // admin_body_class filter.
9402 - wp_enqueue_style('mxchat-admin-sidebar-css', $plugin_url . 'css/admin-sidebar.css', array(), $version);
9403 - wp_enqueue_script('mxchat-admin-sidebar-js', $plugin_url . 'js/admin-sidebar.js', array(), $version, true);
9404 - wp_localize_script('mxchat-admin-sidebar-js', 'MxChatAdminSidebarI18n', array(
9405 - 'copied' => __('Copied', 'mxchat'),
9406 - ));
9407 - // admin-style.css provides the .mxchat-instructions-modal-* classes
9408 - // the new Behavior step's "View Sample Instructions" modal needs.
9409 - // Enqueued AFTER admin-sidebar.css but BEFORE the wizard overlay
9410 - // so the wizard's own rules win where they collide. plan-a2e4d6.
9411 - wp_enqueue_style('mxchat-admin-style-css', $plugin_url . 'css/admin-style.css', array('mxchat-admin-sidebar-css'), $version);
9412 - wp_enqueue_style('mxchat-admin-onboarding-wizard-css', $plugin_url . 'css/admin-onboarding-wizard.css', array('mxchat-admin-sidebar-css', 'mxchat-admin-style-css'), $version);
9413 - wp_enqueue_script('mxchat-admin-onboarding-wizard-js', $plugin_url . 'js/admin-onboarding-wizard.js', array(), $version, true);
9414 - break;
9415 - default:
9416 - wp_enqueue_script(
9417 - 'mxchat-test-streaming-js',
9418 - $plugin_url . 'js/mxchat-test-streaming.js',
9419 - ['jquery'],
9420 - $version,
9421 - true
9422 - );
9423 -
9424 - wp_localize_script('mxchat-test-streaming-js', 'mxchatTestStreamingAjax', [
9425 - 'ajax_url' => admin_url('admin-ajax.php'),
9426 - 'nonce' => wp_create_nonce('mxchat_test_streaming_nonce'),
9427 - 'settings_nonce' => wp_create_nonce('mxchat_save_setting_nonce')
9428 - ]);
9429 -
9430 - // Testing Tab: Load chatbot assets for the embedded testing chatbot
9431 - wp_enqueue_style('mxchat-chat-css', $plugin_url . 'css/chat-style.css', array(), $version);
9432 - wp_enqueue_style('mxchat-admin-testing-css', $plugin_url . 'css/admin-testing-tab.css', array('mxchat-admin-sidebar-css', 'mxchat-chat-css'), $version);
9433 -
9434 - wp_enqueue_script('mxchat-chat-js', $plugin_url . 'js/chat-script.js', array('jquery'), $version, true);
9435 - wp_enqueue_script('mxchat-admin-testing-js', $plugin_url . 'js/admin-testing-tab.js', array('jquery', 'mxchat-chat-js'), $version, true);
9436 -
9437 - // Allow add-ons to enqueue their public CSS/JS for the testing chatbot
9438 - do_action('mxchat_enqueue_testing_tab_assets');
9439 -
9440 - // Localize mxchatChat for the chatbot JS (same settings as frontend)
9441 - $options = get_option('mxchat_options', array());
9442 - $prompts_options = get_option('mxchat_prompts_options', array());
9443 - $theme_options = get_option('mxchat_theme_options', array());
9444 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
9445 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
9446 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
9447 -
9448 - wp_localize_script('mxchat-chat-js', 'mxchatChat', array(
9449 - 'ajax_url' => admin_url('admin-ajax.php'),
9450 - 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
9451 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.6-sol',
9452 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
9453 - 'contextual_awareness_toggle' => isset($options['contextual_awareness_toggle']) ? $options['contextual_awareness_toggle'] : 'off',
9454 - 'link_target_toggle' => $options['link_target_toggle'] ?? 'off',
9455 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
9456 - 'complianz_toggle' => isset($options['complianz_toggle']) && $options['complianz_toggle'] === 'on',
9457 - 'user_message_bg_color' => $options['user_message_bg_color'] ?? '#fff',
9458 - 'user_message_font_color' => $options['user_message_font_color'] ?? '#212121',
9459 - 'bot_message_bg_color' => $options['bot_message_bg_color'] ?? '#212121',
9460 - 'bot_message_font_color' => $options['bot_message_font_color'] ?? '#fff',
9461 - 'top_bar_bg_color' => $options['top_bar_bg_color'] ?? '#212121',
9462 - 'send_button_font_color' => $options['send_button_font_color'] ?? '#212121',
9463 - 'close_button_color' => $options['close_button_color'] ?? '#fff',
9464 - 'chatbot_background_color' => $options['chatbot_background_color'] ?? '#212121',
9465 - 'chatbot_bg_color' => $options['chatbot_bg_color'] ?? '#fff',
9466 - 'icon_color' => $options['icon_color'] ?? '#fff',
9467 - 'chat_input_font_color' => $options['chat_input_font_color'] ?? '#212121',
9468 - 'chat_persistence_toggle' => 'off', // Always off for testing chatbot
9469 - 'appendWidgetToBody' => 'off',
9470 - 'live_agent_message_bg_color' => $options['live_agent_message_bg_color'] ?? '#ffffff',
9471 - 'live_agent_message_font_color' => $options['live_agent_message_font_color'] ?? '#333333',
9472 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
9473 - 'mode_indicator_bg_color' => $options['mode_indicator_bg_color'] ?? '#767676',
9474 - 'mode_indicator_font_color' => $options['mode_indicator_font_color'] ?? '#ffffff',
9475 - 'toolbar_icon_color' => $options['toolbar_icon_color'] ?? '#212121',
9476 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
9477 - 'email_collection_enabled' => 'off', // No email collection in testing
9478 - 'initial_email_state' => null,
9479 - 'skip_email_check' => true,
9480 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
9481 - 'skip_inline_colors' => $skip_inline_colors,
9482 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array()
9483 - ));
9484 -
9485 - // Localize testing tab script data
9486 - wp_localize_script('mxchat-admin-testing-js', 'mxchatAdminTestData', array(
9487 - 'ajaxUrl' => admin_url('admin-ajax.php'),
9488 - 'nonce' => wp_create_nonce('mxchat_test_nonce'),
9489 - 'isAdmin' => true,
9490 - 'testingEnabled' => true
9491 - ));
9492 -
9493 - // Add testing enabled flag for the chatbot to return debug data
9494 - add_action('admin_footer', function() {
9495 - echo '<script>window.mxchatTestingEnabled = true;</script>';
9496 - });
9497 -
9498 - break;
9499 - }
9500 -}
9501 -private function localize_admin_scripts($current_page) {
9502 - // Base localization data for main admin script
9503 - $base_data = array(
9504 - 'ajax_url' => admin_url('admin-ajax.php'),
9505 - 'nonce' => wp_create_nonce('mxchat_admin_nonce'),
9506 - 'admin_url' => admin_url(),
9507 - 'license_nonce' => wp_create_nonce('mxchat_activate_license_nonce'),
9508 - 'inline_edit_nonce' => wp_create_nonce('mxchat_save_inline_nonce'),
9509 - 'setting_nonce' => wp_create_nonce('mxchat_save_setting_nonce'),
9510 - 'export_nonce' => wp_create_nonce('mxchat_export_transcripts'),
9511 - 'actions_nonce' => wp_create_nonce('mxchat_actions_nonce'),
9512 - 'add_intent_nonce' => wp_create_nonce('mxchat_add_intent_nonce'),
9513 - 'edit_intent_nonce' => wp_create_nonce('mxchat_edit_intent'),
9514 - 'add_phrase_nonce' => wp_create_nonce('mxchat_add_phrase_nonce'),
9515 - 'delete_phrase_nonce' => wp_create_nonce('mxchat_delete_phrase_nonce'),
9516 - 'get_phrases_nonce' => wp_create_nonce('mxchat_get_phrases_nonce'),
9517 - 'delete_legacy_nonce' => wp_create_nonce('mxchat_delete_legacy_nonce'),
9518 - 'toggle_action_nonce' => wp_create_nonce('mxchat_actions_nonce'),
9519 - 'fetch_openrouter_models_nonce' => wp_create_nonce('mxchat_fetch_openrouter_models'),
9520 - 'is_activated' => $this->is_activated ? '1' : '0',
9521 - 'status_refresh_interval' => 5000,
9522 - 'discard_changes_confirm' => __('Discard your unsaved changes?', 'mxchat'),
9523 - // Live-agent schedule status text (plan 8ccaa2).
9524 - 'i18n_scheduled_hours' => __('Scheduled hours', 'mxchat'),
9525 - 'i18n_always_available' => __('Always available', 'mxchat'),
9526 - 'prompts_setting_nonce' => wp_create_nonce('mxchat_prompts_setting_nonce'),
9527 - 'ajaxurl' => admin_url('admin-ajax.php')
9528 - );
9529 -
9530 - // Localize main admin script with base data
9531 - wp_localize_script('mxchat-admin-js', 'mxchatAdmin', $base_data);
9532 -
9533 - // Canonical chat-model catalog for the modal picker grid
9534 - // (plan-d14e89). Adding a model in class-mxchat-model-catalog.php
9535 - // automatically appears here.
9536 - if (!class_exists('MxChat_Model_Catalog')) {
9537 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-model-catalog.php';
9538 - }
9539 - wp_localize_script('mxchat-admin-js', 'mxchatChatModelCatalog', MxChat_Model_Catalog::js_picker_shape());
9540 -
9541 - // Page-specific localizations
9542 - $this->localize_page_specific_scripts($current_page);
9543 -}
9544 -private function localize_page_specific_scripts($current_page) {
9545 - switch ($current_page) {
9546 - case 'mxchat-prompts':
9547 - // Status updater localization
9548 - wp_localize_script('mxchat-status-updater', 'mxchat_status_data', array(
9549 - 'ajax_url' => admin_url('admin-ajax.php'),
9550 - 'nonce' => wp_create_nonce('mxchat_status_nonce')
9551 - ));
9552 -
9553 - // Content selector localization
9554 - $mxchat_options_for_selector = get_option('mxchat_options', array());
9555 - $acf_pdf_extract_default = !empty($mxchat_options_for_selector['acf_pdf_extract_default']);
9556 - wp_localize_script('mxchat-content-selector-js', 'mxchatSelector', array(
9557 - 'ajaxurl' => admin_url('admin-ajax.php'),
9558 - 'nonce' => wp_create_nonce('mxchat_content_selector_nonce'),
9559 - 'acfPdfExtractDefault' => $acf_pdf_extract_default ? 1 : 0,
9560 - 'i18n' => array(
9561 - 'searchPlaceholder' => __('Search posts and pages...', 'mxchat'),
9562 - 'selectAll' => __('Select All', 'mxchat'),
9563 - 'process' => __('Process Selected', 'mxchat'),
9564 - 'cancel' => __('Cancel', 'mxchat'),
9565 - 'noResults' => __('No content found.', 'mxchat'),
9566 - 'extractingPdfs' => __('extracting %d PDF(s)...', 'mxchat'),
9567 - 'pdfExtractedSuffix' => __(' (%d PDF(s) extracted)', 'mxchat')
9568 - )
9569 - ));
9570 -
9571 - wp_localize_script('mxchat-knowledge-processing', 'mxchatAdmin', array(
9572 - 'ajax_url' => admin_url('admin-ajax.php'),
9573 - 'status_nonce' => wp_create_nonce('mxchat_status_nonce'),
9574 - 'queue_nonce' => wp_create_nonce('mxchat_queue_nonce'),
9575 - 'stop_nonce' => wp_create_nonce('mxchat_stop_processing_action'),
9576 - 'settings_nonce' => wp_create_nonce('mxchat_prompts_setting_nonce'),
9577 - 'setting_nonce' => wp_create_nonce('mxchat_save_setting_nonce'),
9578 - 'entries_nonce' => wp_create_nonce('mxchat_entries_nonce'),
9579 - 'admin_url' => admin_url(),
9580 - 'ajaxurl' => admin_url('admin-ajax.php'),
9581 - 'status_refresh_interval' => 2000,
9582 - 'bot_id' => isset($_GET['bot_id']) ? sanitize_text_field($_GET['bot_id']) : 'default'
9583 - ));
9584 - break;
9585 -
9586 - case 'mxchat-activation':
9587 - wp_localize_script('mxchat-activation-js', 'mxchatAdmin', array(
9588 - 'ajax_url' => admin_url('admin-ajax.php'),
9589 - 'license_nonce' => wp_create_nonce('mxchat_activate_license_nonce')
9590 - ));
9591 - break;
9592 -
9593 - case 'mxchat-content':
9594 - wp_localize_script('mxchat-content-js', 'mxchatContent', array(
9595 - 'ajaxUrl' => admin_url('admin-ajax.php'),
9596 - 'nonce' => wp_create_nonce('mxchat_content_nonce'),
9597 - 'settingNonce' => wp_create_nonce('mxchat_save_setting_nonce'),
9598 - 'previewUrl' => home_url('/?p='),
9599 - 'isActivated' => $this->is_activated(),
9600 - 'hasAdvancedContent' => apply_filters('mxchat_content_pro_feature', false, 'seo_readability'),
9601 - 'hasGSC' => apply_filters('mxchat_content_pro_feature', false, 'gsc_integration'),
9602 - // Whether Search Console is actually LINKED — distinct from hasGSC
9603 - // (add-on active). Reads the same option the add-on's settings
9604 - // card keys off; harmless false when the add-on is absent.
9605 - 'gscLinked' => (bool) get_option('mxchat_gsc_connected', false),
9606 - 'seoOptimize' => array(
9607 - 'meta_description' => ($options['seo_optimize_meta_desc'] ?? 'on') === 'on',
9608 - 'seo_title' => ($options['seo_optimize_seo_title'] ?? 'on') === 'on',
9609 - 'slug' => ($options['seo_optimize_slug'] ?? 'on') === 'on',
9610 - 'readability' => ($options['seo_optimize_readability'] ?? 'on') === 'on',
9611 - 'internal_links' => ($options['seo_optimize_internal_links'] ?? 'on') === 'on',
9612 - 'img_alt' => ($options['seo_optimize_img_alt'] ?? 'on') === 'on',
9613 - 'featured_img' => ($options['seo_optimize_featured_img'] ?? 'on') === 'on',
9614 - ),
9615 - 'i18n' => array(
9616 - 'generating' => __('Generating...', 'mxchat'),
9617 - 'planning' => __('Planning content structure...', 'mxchat'),
9618 - 'images' => __('Generating images...', 'mxchat'),
9619 - 'writing' => __('Writing full content...', 'mxchat'),
9620 - 'creating' => __('Creating WordPress post...', 'mxchat'),
9621 - 'done' => __('Content generated successfully!', 'mxchat'),
9622 - 'error' => __('An error occurred. Please try again.', 'mxchat'),
9623 - 'editSuccess' => __('Content updated successfully.', 'mxchat'),
9624 - 'promptEmpty' => __('Please enter a prompt.', 'mxchat'),
9625 - )
9626 - ));
9627 - break;
9628 -
9629 - case 'mxchat-transcripts':
9630 - // Get chart data for localization
9631 - $chart_data = $this->get_transcripts_chart_data();
9632 -
9633 - wp_localize_script('mxchat-transcripts-js', 'mxchatAdmin', array(
9634 - 'ajax_url' => admin_url('admin-ajax.php'),
9635 - 'export_nonce' => wp_create_nonce('mxchat_export_transcripts'),
9636 - 'delete_nonce' => wp_create_nonce('mxchat_delete_chat_history'),
9637 - 'setting_nonce' => wp_create_nonce('mxchat_save_setting_nonce'),
9638 - 'translate_nonce' => wp_create_nonce('mxchat_translate_messages')
9639 - ));
9640 -
9641 - // Localize chart data separately - use array_values to ensure proper JSON array encoding
9642 - wp_localize_script('mxchat-transcripts-js', 'mxchatChartData', array(
9643 - 'labels' => array_values($chart_data['labels']),
9644 - 'chats' => array_values($chart_data['chats']),
9645 - 'messages' => array_values($chart_data['messages'])
9646 - ));
9647 - break;
9648 -
9649 - case 'mxchat-settings':
9650 - default:
9651 - // The 'mxchatStyleSettings' localize that used to live here was
9652 - // REMOVED by plan-mxchat-20260731-c63fb6.
9653 - //
9654 - // It targeted the script handle 'mxchat-color-picker', which is
9655 - // registered/enqueued NOWHERE in the plugin — so wp_localize_script()
9656 - // returned false and printed nothing. That accident was the only
9657 - // thing keeping it from being a live secret disclosure: the payload
9658 - // carried loops_api_key, live_agent_secret_key and
9659 - // live_agent_bot_token in full, and this whole method ran for any
9660 - // logged-in user (see the capability gate added to
9661 - // mxchat_enqueue_admin_assets). One future
9662 - // wp_enqueue_script('mxchat-color-picker', ...) would have printed
9663 - // three secrets into page HTML for Subscribers.
9664 - //
9665 - // Nothing consumed the object: a tree-wide grep for
9666 - // 'mxchatStyleSettings' returns only this call site, and js/ names
9667 - // loops_api_key only at mxchat-admin.js:572, as a field-NAME string
9668 - // in an autosave allowlist — not a value read. Removed entirely
9669 - // rather than deleting just the three secret keys, so there is no
9670 - // dead payload left for someone to re-arm.
9671 - break;
9672 - }
9673 -
9674 - // Additional localization that was in the original code
9675 - wp_localize_script('mxchat-admin-js', 'mxchatPromptsAdmin', array(
9676 - 'ajax_url' => admin_url('admin-ajax.php'),
9677 - 'prompts_setting_nonce' => wp_create_nonce('mxchat_prompts_setting_nonce'),
3778 + // Localize the script to pass the nonce and other data to JavaScript
3779 + wp_localize_script('mxchat-admin-js', 'mxchatInlineEdit', array(
3780 + 'nonce' => wp_create_nonce('mxchat_save_inline_nonce'),
3781 + 'ajax_url' => admin_url('admin-ajax.php')
9678 3782 ));
9679 -}
9680 3783
9681 -public function mxchat_sanitize($input) {
9682 - $new_input = array();
9683 3784
9684 - if (isset($input['api_key'])) {
9685 - $new_input['api_key'] = sanitize_text_field($input['api_key']);
9686 3785 }
9687 3786
9688 - if (isset($input['similarity_threshold'])) {
9689 - $new_input['similarity_threshold'] = absint($input['similarity_threshold']); // Ensure it's an integer
9690 - $new_input['similarity_threshold'] = min(max($new_input['similarity_threshold'], 20), 85); // Enforce range
9691 - }
3787 + public function mxchat_sanitize($input) {
3788 + $new_input = array();
9692 3789
9693 - if (isset($input['rag_sources_limit'])) {
9694 - $new_input['rag_sources_limit'] = absint($input['rag_sources_limit']); // Ensure it's an integer
9695 - $new_input['rag_sources_limit'] = min(max($new_input['rag_sources_limit'], 3), 10); // Enforce range 3-10
9696 - }
3790 + if (isset($input['api_key'])) {
3791 + $new_input['api_key'] = sanitize_text_field($input['api_key']);
3792 + }
9697 3793
9698 - if (isset($input['rag_chunks_limit'])) {
9699 - $new_input['rag_chunks_limit'] = absint($input['rag_chunks_limit']); // Ensure it's an integer
9700 - $new_input['rag_chunks_limit'] = min(max($new_input['rag_chunks_limit'], 8), 20); // Enforce range 8-20
9701 - }
3794 + if (isset($input['similarity_threshold'])) {
3795 + $new_input['similarity_threshold'] = absint($input['similarity_threshold']); // Ensure it's an integer
3796 + $new_input['similarity_threshold'] = min(max($new_input['similarity_threshold'], 70), 85); // Enforce range
3797 + }
9702 3798
9703 - if (isset($input['xai_api_key'])) {
9704 - $new_input['xai_api_key'] = sanitize_text_field($input['xai_api_key']);
9705 - }
3799 + if (isset($input['xai_api_key'])) {
3800 + $new_input['xai_api_key'] = sanitize_text_field($input['xai_api_key']);
3801 + }
9706 3802
9707 - if (isset($input['claude_api_key'])) {
9708 - $new_input['claude_api_key'] = sanitize_text_field($input['claude_api_key']);
9709 - }
3803 + if (isset($input['claude_api_key'])) {
3804 + $new_input['claude_api_key'] = sanitize_text_field($input['claude_api_key']);
3805 + }
9710 3806
9711 - if (isset($input['enable_streaming_toggle'])) {
9712 - $new_input['enable_streaming_toggle'] = ($input['enable_streaming_toggle'] === 'on') ? 'on' : 'off';
9713 - } else {
9714 - // If checkbox not checked, it won't be in $input, so set to 'off'
9715 - $new_input['enable_streaming_toggle'] = 'off';
9716 - }
3807 + if (isset($input['enable_woocommerce_integration'])) {
3808 + $new_input['enable_woocommerce_integration'] = isset($input['enable_woocommerce_integration']) && $input['enable_woocommerce_integration'] === '1' ? '1' : '0';
9717 3809
9718 - if (isset($input['enable_web_search'])) {
9719 - $new_input['enable_web_search'] = ($input['enable_web_search'] === 'on') ? 'on' : 'off';
9720 - } else {
9721 - // If checkbox not checked, it won't be in $input, so set to 'off'
9722 - $new_input['enable_web_search'] = 'off';
9723 - }
3810 + }
9724 3811
9725 - if (isset($input['deepseek_api_key'])) {
9726 - $new_input['deepseek_api_key'] = sanitize_text_field($input['deepseek_api_key']);
9727 - }
3812 + if (isset($input['privacy_toggle'])) {
3813 + $new_input['privacy_toggle'] = $input['privacy_toggle'];
3814 + }
9728 3815
9729 - if (isset($input['gemini_api_key'])) {
9730 - $new_input['gemini_api_key'] = sanitize_text_field($input['gemini_api_key']);
9731 - }
3816 + if (isset($input['complianz_toggle'])) {
3817 + $new_input['complianz_toggle'] = $input['complianz_toggle'];
3818 + }
9732 3819
9733 - if (isset($input['enable_woocommerce_integration'])) {
9734 - $new_input['enable_woocommerce_integration'] = $input['enable_woocommerce_integration'] === 'on' ? 'on' : 'off';
9735 - }
3820 + // Handle custom privacy text input
3821 + if (isset($input['privacy_text'])) {
3822 + // Allow basic HTML for links
3823 + $new_input['privacy_text'] = wp_kses_post($input['privacy_text']);
3824 + }
9736 3825
9737 - if (isset($input['privacy_toggle'])) {
9738 - $new_input['privacy_toggle'] = $input['privacy_toggle'];
9739 - }
3826 + if (isset($input['system_prompt_instructions'])) {
3827 + $new_input['system_prompt_instructions'] = sanitize_textarea_field($input['system_prompt_instructions']);
3828 + }
9740 3829
9741 - if (isset($input['complianz_toggle'])) {
9742 - $new_input['complianz_toggle'] = $input['complianz_toggle'];
9743 - }
3830 + if (isset($input['mxchat_pro_email'])) {
3831 + $new_input['mxchat_pro_email'] = sanitize_email($input['mxchat_pro_email']);
3832 + }
9744 3833
9745 - // Handle custom privacy text input
9746 - if (isset($input['privacy_text'])) {
9747 - // Allow basic HTML for links
9748 - $new_input['privacy_text'] = wp_kses_post($input['privacy_text']);
9749 - }
3834 + if (isset($input['mxchat_activation_key'])) {
3835 + $new_input['mxchat_activation_key'] = sanitize_text_field($input['mxchat_activation_key']);
3836 + }
9750 3837
9751 - if (isset($input['system_prompt_instructions'])) {
9752 - $new_input['system_prompt_instructions'] = sanitize_textarea_field($input['system_prompt_instructions']);
9753 - }
3838 + if (isset($input['append_to_body'])) {
3839 + $new_input['append_to_body'] = $input['append_to_body'] === 'on' ? 'on' : 'off';
3840 + }
9754 3841
9755 - if (isset($input['mxchat_pro_email'])) {
9756 - $new_input['mxchat_pro_email'] = sanitize_email($input['mxchat_pro_email']);
9757 - }
3842 + if (isset($input['top_bar_title'])) {
3843 + $new_input['top_bar_title'] = sanitize_text_field($input['top_bar_title']);
3844 + }
9758 3845
9759 - if (isset($input['mxchat_activation_key'])) {
9760 - $new_input['mxchat_activation_key'] = sanitize_text_field($input['mxchat_activation_key']);
9761 - }
3846 + if (isset($input['enable_email_block'])) {
3847 + $new_input['enable_email_block'] = sanitize_text_field($input['enable_email_block']);
3848 + }
9762 3849
9763 - if (isset($input['append_to_body'])) {
9764 - $new_input['append_to_body'] = $input['append_to_body'] === 'on' ? 'on' : 'off';
9765 - }
3850 + if (isset($input['email_blocker_header_content'])) {
3851 + // wp_kses_post() allows standard HTML tags permitted by WordPress
3852 + $new_input['email_blocker_header_content'] = wp_kses_post($input['email_blocker_header_content']);
3853 + }
3854 + if (isset($input['email_blocker_button_text'])) {
3855 + $new_input['email_blocker_button_text'] = sanitize_text_field($input['email_blocker_button_text']);
3856 + }
9766 3857
9767 - // Post type visibility settings
9768 - if (isset($input['post_type_visibility_mode'])) {
9769 - $allowed_modes = array('all', 'include', 'exclude');
9770 - $new_input['post_type_visibility_mode'] = in_array($input['post_type_visibility_mode'], $allowed_modes)
9771 - ? $input['post_type_visibility_mode']
9772 - : 'all';
9773 - }
3858 + if (isset($input['intro_message'])) {
3859 + $new_input['intro_message'] = sanitize_text_field($input['intro_message']);
3860 + }
9774 3861
9775 - if (isset($input['post_type_visibility_list'])) {
9776 - if (is_array($input['post_type_visibility_list'])) {
9777 - $new_input['post_type_visibility_list'] = array_map('sanitize_key', $input['post_type_visibility_list']);
9778 - } else {
9779 - $new_input['post_type_visibility_list'] = array();
3862 + if (isset($input['input_copy'])) {
3863 + $new_input['input_copy'] = sanitize_text_field($input['input_copy']);
9780 3864 }
9781 - }
9782 3865
9783 - if (isset($input['contextual_awareness_toggle'])) {
9784 - $new_input['contextual_awareness_toggle'] = $input['contextual_awareness_toggle'] === 'on' ? 'on' : 'off';
9785 -}
9786 3866
9787 - if (isset($input['citation_links_toggle'])) {
9788 - $new_input['citation_links_toggle'] = $input['citation_links_toggle'] === 'on' ? 'on' : 'off';
9789 -}
3867 + if (isset($input['rate_limit_message'])) {
3868 + $new_input['rate_limit_message'] = sanitize_text_field($input['rate_limit_message']);
3869 + }
9790 3870
9791 - // Satisfaction rating prompt — defaults ON when unchecked (so first save
9792 - // doesn't accidentally disable it). The form posts 'on' when checked;
9793 - // unchecked checkboxes don't post a value at all, so we infer 'off' only
9794 - // when the autosave/PHP request explicitly clears it via empty string.
9795 - if (array_key_exists('satisfaction_rating_enabled', $input)) {
9796 - $new_input['satisfaction_rating_enabled'] = $input['satisfaction_rating_enabled'] === 'on' ? 'on' : 'off';
9797 - }
3871 + if (isset($input['rate_limit_logged_in'])) {
3872 + $allowed_limits = array('1', '3', '5', '10', '15', '20', '100', 'unlimited'); // Add 'unlimited' to allowed values
3873 + $rate_limit = sanitize_text_field($input['rate_limit_logged_in']);
9798 3874
9799 - // Satisfaction rating customization (plan-141a12). Idle is clamped 5-600;
9800 - // the 4 text strings are sanitized + capped server-side. Blank values are
9801 - // preserved so the integrator falls back to translated defaults.
9802 - if (array_key_exists('satisfaction_rating_idle_seconds', $input)) {
9803 - $new_input['satisfaction_rating_idle_seconds'] = max(5, min(600, intval($input['satisfaction_rating_idle_seconds'])));
9804 - }
9805 -
9806 - // Max input length in characters (plan a3fae2 part C). 0 = unlimited (default,
9807 - // preserves current behavior). Clamp to a sane ceiling so a typo can't lock out
9808 - // all input. REQUIRED here — mxchat_sanitize() rebuilds the array from a whitelist,
9809 - // so without this isset-handler the key is stripped on the next save of ANY field
9810 - // (the 2c02ea global-rate-limit footgun).
9811 - if (array_key_exists('max_input_length', $input)) {
9812 - $new_input['max_input_length'] = max(0, min(100000, intval($input['max_input_length'])));
9813 - }
9814 - if (array_key_exists('satisfaction_rating_question', $input)) {
9815 - $new_input['satisfaction_rating_question'] = mb_substr(sanitize_text_field($input['satisfaction_rating_question']), 0, 200);
9816 - }
9817 - if (array_key_exists('satisfaction_rating_thanks', $input)) {
9818 - $new_input['satisfaction_rating_thanks'] = mb_substr(sanitize_text_field($input['satisfaction_rating_thanks']), 0, 300);
9819 - }
9820 - if (array_key_exists('satisfaction_rating_placeholder', $input)) {
9821 - $new_input['satisfaction_rating_placeholder'] = mb_substr(sanitize_text_field($input['satisfaction_rating_placeholder']), 0, 200);
9822 - }
9823 - if (array_key_exists('satisfaction_rating_saved', $input)) {
9824 - $new_input['satisfaction_rating_saved'] = mb_substr(sanitize_text_field($input['satisfaction_rating_saved']), 0, 200);
9825 - }
9826 -
9827 - if (isset($input['top_bar_title'])) {
9828 - $new_input['top_bar_title'] = sanitize_text_field($input['top_bar_title']);
9829 - }
9830 -
9831 - if (isset($input['ai_agent_text'])) {
9832 - $new_input['ai_agent_text'] = sanitize_text_field($input['ai_agent_text']);
3875 + if (in_array($rate_limit, $allowed_limits, true)) {
3876 + $new_input['rate_limit_logged_in'] = $rate_limit;
3877 + } else {
3878 + $new_input['rate_limit_logged_in'] = '100'; // Default for logged-in users
3879 + }
9833 3880 }
9834 3881
9835 - if (isset($input['enable_email_block'])) {
9836 - $new_input['enable_email_block'] = sanitize_text_field($input['enable_email_block']);
9837 - }
3882 + if (isset($input['rate_limit_logged_out'])) {
3883 + $allowed_limits = array('1', '3', '5', '10', '15', '20', '100', 'unlimited'); // Add 'unlimited' to allowed values
3884 + $rate_limit = sanitize_text_field($input['rate_limit_logged_out']);
9838 3885
9839 - if (isset($input['email_blocker_header_content'])) {
9840 - // wp_kses_post() allows standard HTML tags permitted by WordPress
9841 - $new_input['email_blocker_header_content'] = wp_kses_post($input['email_blocker_header_content']);
9842 - }
9843 - if (isset($input['email_blocker_button_text'])) {
9844 - $new_input['email_blocker_button_text'] = sanitize_text_field($input['email_blocker_button_text']);
9845 - }
9846 - // Sanitize name field toggle
9847 - if (isset($input['enable_name_field'])) {
9848 - $new_input['enable_name_field'] = ($input['enable_name_field'] === 'on') ? 'on' : 'off';
9849 - } else {
9850 - $new_input['enable_name_field'] = 'off';
9851 - }
9852 - // Sanitize name field placeholder
9853 - if (isset($input['name_field_placeholder'])) {
9854 - $new_input['name_field_placeholder'] = sanitize_text_field($input['name_field_placeholder']);
9855 - }
9856 - if (isset($input['intro_message'])) {
9857 - $new_input['intro_message'] = wp_kses_post($input['intro_message']); // Use wp_kses_post instead
9858 - }
9859 -
9860 - if (isset($input['input_copy'])) {
9861 - $new_input['input_copy'] = sanitize_text_field($input['input_copy']);
9862 - }
9863 -
9864 - if (isset($input['rate_limit_message'])) {
9865 - $new_input['rate_limit_message'] = sanitize_text_field($input['rate_limit_message']);
9866 - }
9867 -
9868 -// Handle the new rate limits format
9869 -if (isset($input['rate_limits']) && is_array($input['rate_limits'])) {
9870 - $new_input['rate_limits'] = array();
9871 - $allowed_limits = array('1', '3', '5', '10', '15', '20', '50', '100', 'unlimited');
9872 - $allowed_timeframes = array('hourly', 'daily', 'weekly', 'monthly');
9873 -
9874 - foreach ($input['rate_limits'] as $role_id => $settings) {
9875 - $new_input['rate_limits'][$role_id] = array();
9876 -
9877 - // Sanitize limit
9878 - if (isset($settings['limit'])) {
9879 - $limit = sanitize_text_field($settings['limit']);
9880 - // Accept presets, 'unlimited', the '__custom__' sentinel, OR any positive
9881 - // integer (custom value) — mirrors the global branch (plan-2c02ea). Without
9882 - // the custom path the per-role custom input was dropped and reset to the role
9883 - // default on every save (plan-7e23e7).
9884 - if (in_array($limit, $allowed_limits, true) || $limit === '__custom__' || (ctype_digit($limit) && (int) $limit >= 1)) {
9885 - $new_input['rate_limits'][$role_id]['limit'] = $limit;
3886 + if (in_array($rate_limit, $allowed_limits, true)) {
3887 + $new_input['rate_limit_logged_out'] = $rate_limit;
9886 3888 } else {
9887 - $new_input['rate_limits'][$role_id]['limit'] = ($role_id === 'logged_out') ? '10' : '100'; // Default
3889 + $new_input['rate_limit_logged_out'] = '10'; // Default for logged-out users
9888 3890 }
9889 3891 }
9890 3892
9891 - // Preserve the per-role custom value (mirrors the global branch's limit_custom).
9892 - if (isset($settings['limit_custom'])) {
9893 - $new_input['rate_limits'][$role_id]['limit_custom'] = preg_replace('/[^0-9]/', '', (string) $settings['limit_custom']);
3893 + if (isset($input['pre_chat_message'])) {
3894 + $new_input['pre_chat_message'] = sanitize_textarea_field($input['pre_chat_message']);
9894 3895 }
9895 3896
9896 - // Sanitize timeframe
9897 - if (isset($settings['timeframe'])) {
9898 - $timeframe = sanitize_text_field($settings['timeframe']);
9899 - if (in_array($timeframe, $allowed_timeframes, true)) {
9900 - $new_input['rate_limits'][$role_id]['timeframe'] = $timeframe;
9901 - } else {
9902 - $new_input['rate_limits'][$role_id]['timeframe'] = 'daily'; // Default
3897 + if (isset($input['model'])) {
3898 + $allowed_models = array(
3899 + 'grok-beta',
3900 + 'claude-3-5-sonnet-20241022',
3901 + 'claude-3-opus-20240229',
3902 + 'claude-3-sonnet-20240229',
3903 + 'claude-3-haiku-20240307',
3904 + 'gpt-4o',
3905 + 'gpt-4o-mini',
3906 + 'gpt-4-turbo',
3907 + 'gpt-4',
3908 + 'gpt-3.5-turbo',
3909 + );
3910 + if (in_array($input['model'], $allowed_models)) {
3911 + $new_input['model'] = sanitize_text_field($input['model']);
9903 3912 }
9904 3913 }
9905 3914
9906 - // Sanitize message
9907 - if (isset($settings['message'])) {
9908 - $new_input['rate_limits'][$role_id]['message'] = sanitize_textarea_field($settings['message']);
3915 + // Sanitize new pro features
3916 + if (isset($input['close_button_color'])) {
3917 + $new_input['close_button_color'] = sanitize_hex_color($input['close_button_color']);
9909 3918 }
9910 - }
9911 -}
9912 3919
9913 -// Handle the whole-chatbot global rate limit (plan-mxchat-20260603-2c02ea).
9914 -// Mirrors the per-role block above, adapted to the single global shape. Without
9915 -// this branch the whitelist-rebuild dropped rate_limits_global entirely on every
9916 -// save, so the global cap silently fell back to its 'unlimited' default.
9917 -// MUST accept arbitrary positive integers so the custom-value path (d55f65) is not regressed.
9918 -if (isset($input['rate_limits_global']) && is_array($input['rate_limits_global'])) {
9919 - $g = $input['rate_limits_global'];
9920 - $allowed_limits = array('1', '3', '5', '10', '15', '20', '50', '100', 'unlimited');
9921 - $allowed_timeframes = array('hourly', 'daily', 'weekly', 'monthly');
9922 - $global_out = array();
3920 + if (isset($input['chatbot_bg_color'])) {
3921 + $new_input['chatbot_bg_color'] = sanitize_hex_color($input['chatbot_bg_color']);
3922 + }
9923 3923
9924 - if (isset($g['limit'])) {
9925 - $limit = sanitize_text_field($g['limit']);
9926 - // Accept presets, 'unlimited', the '__custom__' sentinel (resolved by the
9927 - // autosave handler / renderer), OR any positive integer (custom value).
9928 - if (in_array($limit, $allowed_limits, true) || $limit === '__custom__' || (ctype_digit($limit) && (int) $limit >= 1)) {
9929 - $global_out['limit'] = $limit;
9930 - } else {
9931 - $global_out['limit'] = 'unlimited';
3924 + if (isset($input['woocommerce_consumer_key'])) {
3925 + $new_input['woocommerce_consumer_key'] = sanitize_text_field($input['woocommerce_consumer_key']);
9932 3926 }
9933 - }
9934 3927
9935 - if (isset($g['limit_custom'])) {
9936 - $global_out['limit_custom'] = preg_replace('/[^0-9]/', '', (string) $g['limit_custom']);
9937 - }
3928 + if (isset($input['woocommerce_consumer_secret'])) {
3929 + $new_input['woocommerce_consumer_secret'] = sanitize_text_field($input['woocommerce_consumer_secret']);
3930 + }
9938 3931
9939 - if (isset($g['timeframe'])) {
9940 - $timeframe = sanitize_text_field($g['timeframe']);
9941 - $global_out['timeframe'] = in_array($timeframe, $allowed_timeframes, true) ? $timeframe : 'daily';
9942 - }
3932 + if (isset($input['user_message_bg_color'])) {
3933 + $new_input['user_message_bg_color'] = sanitize_hex_color($input['user_message_bg_color']);
3934 + }
9943 3935
9944 - if (isset($g['message'])) {
9945 - $global_out['message'] = sanitize_textarea_field($g['message']);
9946 - }
3936 + if (isset($input['user_message_font_color'])) {
3937 + $new_input['user_message_font_color'] = sanitize_hex_color($input['user_message_font_color']);
3938 + }
9947 3939
9948 - $new_input['rate_limits_global'] = $global_out;
9949 -}
3940 + if (isset($input['bot_message_bg_color'])) {
3941 + $new_input['bot_message_bg_color'] = sanitize_hex_color($input['bot_message_bg_color']);
3942 + }
9950 3943
9951 - if (isset($input['pre_chat_message'])) {
9952 - $new_input['pre_chat_message'] = sanitize_textarea_field($input['pre_chat_message']);
9953 - }
3944 + if (isset($input['bot_message_font_color'])) {
3945 + $new_input['bot_message_font_color'] = sanitize_hex_color($input['bot_message_font_color']);
3946 + }
9954 3947
9955 - if (isset($input['voyage_api_key'])) {
9956 - $new_input['voyage_api_key'] = sanitize_text_field($input['voyage_api_key']);
9957 - }
3948 + if (isset($input['live_agent_message_bg_color'])) {
3949 + $new_input['live_agent_message_bg_color'] = sanitize_hex_color($input['live_agent_message_bg_color']);
3950 + }
9958 3951
9959 - // Add to your sanitize function
9960 - if (isset($input['embedding_model'])) {
9961 - // Catalog refactor (plan-d14e89): allowlist derived from the canonical
9962 - // catalog in includes/class-mxchat-model-catalog.php.
9963 - if (!class_exists('MxChat_Model_Catalog')) {
9964 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-model-catalog.php';
3952 + if (isset($input['live_agent_message_font_color'])) {
3953 + $new_input['live_agent_message_font_color'] = sanitize_hex_color($input['live_agent_message_font_color']);
9965 3954 }
9966 - $allowed_models = MxChat_Model_Catalog::embedding_model_ids();
9967 - if (in_array($input['embedding_model'], $allowed_models)) {
9968 - $new_input['embedding_model'] = sanitize_text_field($input['embedding_model']);
3955 +
3956 + if (isset($input['mode_indicator_bg_color'])) {
3957 + $new_input['mode_indicator_bg_color'] = sanitize_hex_color($input['mode_indicator_bg_color']);
9969 3958 }
9970 - }
9971 3959
9972 -if (isset($input['model'])) {
9973 - if ($input['model'] === 'openrouter') {
9974 - $new_input['model'] = 'openrouter';
9975 - } else {
9976 - if (!class_exists('MxChat_Model_Catalog')) {
9977 - require_once plugin_dir_path(__FILE__) . 'class-mxchat-model-catalog.php';
3960 + if (isset($input['mode_indicator_font_color'])) {
3961 + $new_input['mode_indicator_font_color'] = sanitize_hex_color($input['mode_indicator_font_color']);
9978 3962 }
9979 - $allowed_models = MxChat_Model_Catalog::chat_model_ids();
9980 3963
9981 - if (in_array($input['model'], $allowed_models)) {
9982 - $new_input['model'] = sanitize_text_field($input['model']);
9983 - } else {
9984 - // Fallback for any deprecated model
9985 - $new_input['model'] = 'gpt-5.6-sol';
3964 + if (isset($input['toolbar_icon_color'])) {
3965 + $new_input['toolbar_icon_color'] = sanitize_hex_color($input['toolbar_icon_color']);
9986 3966 }
9987 - }
9988 -}
9989 -
9990 -if (isset($input['openrouter_selected_model'])) {
9991 - // Just sanitize it, don't validate against a whitelist
9992 - $new_input['openrouter_selected_model'] = sanitize_text_field($input['openrouter_selected_model']);
9993 -}
9994 3967
9995 -// ADD THIS:
9996 -if (isset($input['openrouter_selected_model_name'])) {
9997 - $new_input['openrouter_selected_model_name'] = sanitize_text_field($input['openrouter_selected_model_name']);
9998 -}
9999 -
10000 - if (isset($input['openrouter_api_key'])) {
10001 - $new_input['openrouter_api_key'] = sanitize_text_field($input['openrouter_api_key']);
10002 - }
3968 + if (isset($input['top_bar_bg_color'])) {
3969 + $new_input['top_bar_bg_color'] = sanitize_hex_color($input['top_bar_bg_color']);
3970 + }
10003 3971
10004 - // Custom (OpenAI-compatible) Provider — Ollama, LM Studio, vLLM, Azure OpenAI, etc.
10005 - if (isset($input['custom_provider_base_url'])) {
10006 - $new_input['custom_provider_base_url'] = esc_url_raw(rtrim(trim((string) $input['custom_provider_base_url']), '/'));
10007 - }
10008 - if (isset($input['custom_provider_api_key'])) {
10009 - $new_input['custom_provider_api_key'] = sanitize_text_field($input['custom_provider_api_key']);
10010 - }
10011 - if (isset($input['custom_provider_model'])) {
10012 - $new_input['custom_provider_model'] = sanitize_text_field($input['custom_provider_model']);
10013 - }
10014 - if (isset($input['custom_provider_auth_scheme'])) {
10015 - $scheme = sanitize_text_field($input['custom_provider_auth_scheme']);
10016 - $new_input['custom_provider_auth_scheme'] = in_array($scheme, array('bearer', 'api-key'), true) ? $scheme : 'bearer';
10017 - }
10018 - if (isset($input['custom_provider_for_embeddings'])) {
10019 - $new_input['custom_provider_for_embeddings'] = ($input['custom_provider_for_embeddings'] === 'on') ? 'on' : 'off';
10020 - }
10021 - if (isset($input['custom_provider_for_images'])) {
10022 - $new_input['custom_provider_for_images'] = ($input['custom_provider_for_images'] === 'on') ? 'on' : 'off';
10023 - }
10024 - if (isset($input['custom_provider_embedding_model'])) {
10025 - $new_input['custom_provider_embedding_model'] = sanitize_text_field($input['custom_provider_embedding_model']);
10026 - }
10027 - if (isset($input['custom_provider_api_version'])) {
10028 - $new_input['custom_provider_api_version'] = sanitize_text_field($input['custom_provider_api_version']);
10029 - }
3972 + if (isset($input['send_button_font_color'])) {
3973 + $new_input['send_button_font_color'] = sanitize_hex_color($input['send_button_font_color']);
3974 + }
10030 3975
3976 + if (isset($input['chatbot_background_color'])) {
3977 + $new_input['chatbot_background_color'] = sanitize_hex_color($input['chatbot_background_color']);
3978 + }
10031 3979
3980 + if (isset($input['icon_color'])) {
3981 + $new_input['icon_color'] = sanitize_hex_color($input['icon_color']);
3982 + }
10032 3983
10033 - if (isset($input['woocommerce_consumer_key'])) {
10034 - $new_input['woocommerce_consumer_key'] = sanitize_text_field($input['woocommerce_consumer_key']);
10035 - }
3984 + if (isset($input['custom_icon'])) {
3985 + $new_input['custom_icon'] = esc_url_raw($input['custom_icon']);
3986 + }
10036 3987
10037 - if (isset($input['woocommerce_consumer_secret'])) {
10038 - $new_input['woocommerce_consumer_secret'] = sanitize_text_field($input['woocommerce_consumer_secret']);
10039 - }
10040 3988
3989 + if (isset($input['title_icon'])) {
3990 + $new_input['title_icon'] = esc_url_raw($input['title_icon']);
3991 + }
10041 3992
10042 - // Sanitize link_target_toggle
10043 - if (isset($input['link_target_toggle'])) {
10044 - $new_input['link_target_toggle'] = $input['link_target_toggle'] === 'on' ? 'on' : 'off';
10045 - }
3993 + if (isset($input['chat_input_font_color'])) {
3994 + $new_input['chat_input_font_color'] = sanitize_hex_color($input['chat_input_font_color']);
3995 + }
10046 3996
10047 - // Sanitize Loops API Key
3997 + // Sanitize link_target_toggle
3998 + if (isset($input['link_target_toggle'])) {
3999 + $new_input['link_target_toggle'] = $input['link_target_toggle'] === 'on' ? 'on' : 'off';
4000 + }
4001 +
4002 + // Sanitize Loops API Key
10048 4003 if (isset($input['loops_api_key'])) {
10049 4004 $new_input['loops_api_key'] = sanitize_text_field($input['loops_api_key']);
10050 4005 }
10051 4006
@@ -10052,25 +4007,10 @@
10052 4007 if (isset($input['chat_persistence_toggle'])) {
10053 4008 $new_input['chat_persistence_toggle'] = $input['chat_persistence_toggle'] === 'on' ? 'on' : 'off';
10054 4009 }
10055 4010
10056 - // No else clause: an absent key stays absent, so the front-end default ('on') applies
10057 - // and the rebuild never strips a saved 'off' (autosave passes the full options array back through here).
10058 - if (isset($input['print_button_enabled'])) {
10059 - $new_input['print_button_enabled'] = $input['print_button_enabled'] === 'on' ? 'on' : 'off';
10060 - }
10061 4011
10062 - // plan ac2e81 — "Start new chat" toggle (default OFF) + editable label.
10063 - // No else on the toggle: an absent key stays absent so the front-end '?? off'
10064 - // default applies, and the full-array autosave rebuild preserves a saved value.
10065 - if (isset($input['reset_chat_enabled'])) {
10066 - $new_input['reset_chat_enabled'] = $input['reset_chat_enabled'] === 'on' ? 'on' : 'off';
10067 - }
10068 4012
10069 - if (isset($input['reset_chat_label'])) {
10070 - $new_input['reset_chat_label'] = sanitize_text_field($input['reset_chat_label']);
10071 - }
10072 -
10073 4013 if (isset($input['popular_question_1'])) {
10074 4014 $new_input['popular_question_1'] = sanitize_text_field($input['popular_question_1']);
10075 4015 }
10076 4016
@@ -10090,17 +4030,19 @@
10090 4030 if (isset($input['loops_mailing_list'])) {
10091 4031 $new_input['loops_mailing_list'] = sanitize_text_field($input['loops_mailing_list']);
10092 4032 }
10093 4033
10094 -// Sanitize Triggered Phrase Response
4034 + // Sanitize Triggered Phrase Response
10095 4035 if (isset($input['triggered_phrase_response'])) {
10096 4036 $new_input['triggered_phrase_response'] = wp_kses_post($input['triggered_phrase_response']);
10097 4037 }
4038 +
10098 4039 if (isset($input['email_capture_response'])) {
10099 - $new_input['email_capture_response'] = wp_kses_post($input['email_capture_response']);
4040 + $new_input['email_capture_response'] = sanitize_textarea_field($input['email_capture_response']);
10100 4041 }
10101 4042
10102 - // Sanitize Brave Search Settings
4043 +
4044 + // Sanitize Brave Search Settings
10103 4045 if (isset($input['brave_api_key'])) {
10104 4046 $new_input['brave_api_key'] = sanitize_text_field($input['brave_api_key']);
10105 4047 }
10106 4048
@@ -10115,9 +4057,9 @@
10115 4057 }
10116 4058
10117 4059 if (isset($input['brave_news_count'])) {
10118 4060 $news_count = intval($input['brave_news_count']);
10119 - $new_input['brave_news_count'] = ($news_count >=1 && $news_count <=10) ? $news_count : 3;
4061 + $new_input['brave_news_count'] = ($news_count >=1 && $news_count <=10) ? $news_count : 3;
10120 4062 }
10121 4063
10122 4064 if (isset($input['brave_country'])) {
10123 4065 $new_input['brave_country'] = sanitize_text_field($input['brave_country']);
@@ -10126,26 +4068,14 @@
10126 4068 if (isset($input['brave_language'])) {
10127 4069 $new_input['brave_language'] = sanitize_text_field($input['brave_language']);
10128 4070 }
10129 4071
4072 +
10130 4073 if (isset($input['chat_toolbar_toggle'])) {
10131 4074 $new_input['chat_toolbar_toggle'] = $input['chat_toolbar_toggle'] === 'on' ? 'on' : 'off';
10132 4075 }
10133 4076
10134 - // Sanitize PDF upload button toggle
10135 - if (isset($input['show_pdf_upload_button'])) {
10136 - $new_input['show_pdf_upload_button'] = $input['show_pdf_upload_button'] === 'on' ? 'on' : 'off';
10137 - } else {
10138 - $new_input['show_pdf_upload_button'] = 'off'; // If checkbox is unchecked
10139 - }
10140 4077
10141 - // Sanitize Word upload button toggle
10142 - if (isset($input['show_word_upload_button'])) {
10143 - $new_input['show_word_upload_button'] = $input['show_word_upload_button'] === 'on' ? 'on' : 'off';
10144 - } else {
10145 - $new_input['show_word_upload_button'] = 'off'; // If checkbox is unchecked
10146 - }
10147 -
10148 4078 if (isset($input['pdf_intent_trigger_text'])) {
10149 4079 $new_input['pdf_intent_trigger_text'] = sanitize_text_field($input['pdf_intent_trigger_text']);
10150 4080 }
10151 4081
@@ -10163,748 +4093,651 @@
10163 4093 $new_input['pdf_max_pages'] = 69; // Default to 69 if out of range
10164 4094 }
10165 4095 }
10166 4096
10167 - if (isset($input['live_agent_webhook_url'])) {
10168 - $new_input['live_agent_webhook_url'] = esc_url_raw($input['live_agent_webhook_url']);
10169 - }
10170 - if (isset($input['live_agent_secret_key'])) {
10171 - $new_input['live_agent_secret_key'] = sanitize_text_field($input['live_agent_secret_key']);
10172 - }
10173 4097
10174 - // Live Agent Integration
10175 - if (isset($input['live_agent_bot_token'])) {
10176 - $new_input['live_agent_bot_token'] = sanitize_text_field($input['live_agent_bot_token']);
10177 - }
4098 + if (isset($input['live_agent_webhook_url'])) {
4099 + $new_input['live_agent_webhook_url'] = esc_url_raw($input['live_agent_webhook_url']);
4100 + }
4101 + if (isset($input['live_agent_secret_key'])) {
4102 + $new_input['live_agent_secret_key'] = sanitize_text_field($input['live_agent_secret_key']);
4103 + }
10178 4104
10179 - if (isset($input['live_agent_shared_channel'])) {
10180 - $new_input['live_agent_shared_channel'] = sanitize_text_field($input['live_agent_shared_channel']);
10181 - }
4105 + // Live Agent Integration
4106 + if (isset($input['live_agent_bot_token'])) {
4107 + $new_input['live_agent_bot_token'] = sanitize_text_field($input['live_agent_bot_token']);
4108 + }
10182 4109
10183 - // Default-OFF toggle: absent key stays absent (unchecked box = off).
10184 - if (isset($input['live_agent_archive_on_end_toggle'])) {
10185 - $new_input['live_agent_archive_on_end_toggle'] = ($input['live_agent_archive_on_end_toggle'] === 'on') ? 'on' : 'off';
10186 - }
10187 -
10188 - if (isset($input['live_agent_user_ids'])) {
10189 - $new_input['live_agent_user_ids'] = sanitize_textarea_field($input['live_agent_user_ids']);
10190 - }
10191 -
10192 4110 if (isset($input['live_agent_status'])) {
10193 - $new_input['live_agent_status'] = ($input['live_agent_status'] === 'on') ? 'on' : 'off';
4111 + $new_input['live_agent_status'] = ($input['live_agent_status'] === 'online') ? 'online' : 'offline';
10194 4112 }
10195 4113 if (isset($input['live_agent_away_message'])) {
10196 4114 $new_input['live_agent_away_message'] = sanitize_textarea_field($input['live_agent_away_message']);
10197 4115 }
10198 - if (isset($input['live_agent_notification_message'])) {
4116 + if (isset($input['live_agent_notification_message'])) {
10199 4117 $new_input['live_agent_notification_message'] = sanitize_textarea_field($input['live_agent_notification_message']);
10200 4118 }
10201 4119
10202 - // Telegram Integration
10203 - if (isset($input['telegram_status'])) {
10204 - $new_input['telegram_status'] = ($input['telegram_status'] === 'on') ? 'on' : 'off';
10205 - }
10206 - if (isset($input['telegram_bot_token'])) {
10207 - $new_input['telegram_bot_token'] = sanitize_text_field($input['telegram_bot_token']);
10208 - }
10209 - if (isset($input['telegram_group_id'])) {
10210 - $new_input['telegram_group_id'] = sanitize_text_field($input['telegram_group_id']);
10211 - }
10212 - if (isset($input['telegram_webhook_secret'])) {
10213 - $new_input['telegram_webhook_secret'] = sanitize_text_field($input['telegram_webhook_secret']);
10214 - }
10215 - if (isset($input['telegram_notification_message'])) {
10216 - $new_input['telegram_notification_message'] = sanitize_textarea_field($input['telegram_notification_message']);
10217 - }
10218 - if (isset($input['telegram_away_message'])) {
10219 - $new_input['telegram_away_message'] = sanitize_textarea_field($input['telegram_away_message']);
10220 - }
10221 4120
10222 - // Sanitize script loading strategy
10223 - if (isset($input['script_loading_strategy'])) {
10224 - $allowed_strategies = array('default', 'defer', 'delay_1s', 'delay_3s', 'delay_5s', 'on_interaction');
10225 - $new_input['script_loading_strategy'] = in_array($input['script_loading_strategy'], $allowed_strategies)
10226 - ? $input['script_loading_strategy']
10227 - : 'default';
4121 + return $new_input;
10228 4122 }
10229 4123
10230 - // Sanitize debug mode
10231 - if (isset($input['debug_mode'])) {
10232 - $new_input['debug_mode'] = ($input['debug_mode'] === 'on') ? 'on' : 'off';
10233 - }
10234 4124
10235 - // Content Generator Settings
10236 - if (isset($input['content_model'])) {
10237 - $new_input['content_model'] = sanitize_text_field($input['content_model']);
4125 + // Method to append the chatbot to the body
4126 + public function mxchat_append_chatbot_to_body() {
4127 + $options = get_option('mxchat_options');
4128 + if (isset($options['append_to_body']) && $options['append_to_body'] === 'on') {
4129 + echo do_shortcode('[mxchat_chatbot floating="yes"]');
4130 + }
10238 4131 }
10239 - if (isset($input['content_image_model'])) {
10240 - $new_input['content_image_model'] = sanitize_text_field($input['content_image_model']);
10241 - }
10242 - if (isset($input['content_image_quality'])) {
10243 - $q = sanitize_text_field($input['content_image_quality']);
10244 - $new_input['content_image_quality'] = in_array($q, array('auto', 'low', 'medium', 'high'), true) ? $q : 'auto';
10245 - }
10246 - if (isset($input['content_enable_images'])) {
10247 - $new_input['content_enable_images'] = ($input['content_enable_images'] === 'on') ? 'on' : 'off';
10248 - }
10249 - if (isset($input['content_use_placeholders'])) {
10250 - $new_input['content_use_placeholders'] = ($input['content_use_placeholders'] === 'on') ? 'on' : 'off';
10251 - }
10252 - if (isset($input['content_internal_linking'])) {
10253 - $new_input['content_internal_linking'] = ($input['content_internal_linking'] === 'on') ? 'on' : 'off';
10254 - }
10255 - if (isset($input['content_tool_use'])) {
10256 - $new_input['content_tool_use'] = ($input['content_tool_use'] === 'on') ? 'on' : 'off';
10257 - }
10258 - if (isset($input['content_image_count'])) {
10259 - $new_input['content_image_count'] = (string) max(1, min(5, (int) $input['content_image_count']));
10260 - }
10261 4132
10262 - // SEO Optimize toggle fields
10263 - foreach (array('seo_optimize_meta_desc', 'seo_optimize_seo_title', 'seo_optimize_slug', 'seo_optimize_readability', 'seo_optimize_internal_links', 'seo_optimize_img_alt', 'seo_optimize_featured_img') as $seo_key) {
10264 - if (isset($input[$seo_key])) {
10265 - $new_input[$seo_key] = ($input[$seo_key] === 'on') ? 'on' : 'off';
10266 - }
10267 - }
10268 4133
10269 - // Preserve content generator settings when saving from main settings page
10270 - // (where content fields are not in the form submission)
10271 - $existing = get_option('mxchat_options', array());
10272 - foreach (array('content_model', 'content_image_model', 'content_image_quality', 'content_image_count', 'content_enable_images', 'content_use_placeholders', 'content_internal_linking', 'content_tool_use', 'seo_optimize_meta_desc', 'seo_optimize_seo_title', 'seo_optimize_slug', 'seo_optimize_readability', 'seo_optimize_internal_links', 'seo_optimize_img_alt', 'seo_optimize_featured_img') as $key) {
10273 - if (!isset($new_input[$key]) && isset($existing[$key])) {
10274 - $new_input[$key] = $existing[$key];
10275 - }
10276 - }
10277 4134
10278 - return $new_input;
10279 -}
4135 +private static function mxchat_extract_main_content($html) {
4136 + $dom = new DOMDocument;
4137 + libxml_use_internal_errors(true); // Suppress HTML parsing errors
4138 + @$dom->loadHTML($html);
4139 + libxml_clear_errors();
10280 4140
10281 -/**
10282 - * Log a debug message if debug mode is enabled
10283 - *
10284 - * @param string $type The type of log entry (settings_save, api_error, activation, etc.)
10285 - * @param string $message The log message
10286 - * @param array $data Optional additional data to log
10287 - * @return bool Whether the message was logged
10288 - */
10289 -public static function mxchat_log_debug( $type, $message, $data = array() ) {
10290 - $options = get_option( 'mxchat_options', array() );
4141 + $xpath = new DOMXPath($dom);
10291 4142
10292 - // Check if debug mode is enabled
10293 - if ( ! isset( $options['debug_mode'] ) || $options['debug_mode'] !== 'on' ) {
10294 - return false;
10295 - }
4143 + // Simplified selectors focusing on common content areas
4144 + $selectors = [
4145 + '//article',
4146 + '//*[@id="content"]',
4147 + '//*[@class="entry-content"]',
4148 + '//main',
4149 + ];
10296 4150
10297 - // Get current log
10298 - $log = get_option( 'mxchat_debug_log', array() );
10299 - if ( ! is_array( $log ) ) {
10300 - $log = array();
4151 + foreach ($selectors as $selector) {
4152 + $nodes = $xpath->query($selector);
4153 + if ($nodes->length > 0) {
4154 + $content = '';
4155 + foreach ($nodes as $node) {
4156 + $content .= $dom->saveHTML($node);
4157 + }
4158 + return $content;
4159 + }
10301 4160 }
10302 4161
10303 - // Add new entry
10304 - $entry = array(
10305 - 'time' => current_time( 'Y-m-d H:i:s' ),
10306 - 'type' => sanitize_key( $type ),
10307 - 'message' => sanitize_text_field( $message ),
10308 - );
4162 + // Fallback: Return the entire body content if no specific selector matches
4163 + $body = $dom->getElementsByTagName('body');
4164 + return $body->length > 0 ? $dom->saveHTML($body->item(0)) : $html;
4165 +}
10309 4166
10310 - if ( ! empty( $data ) ) {
10311 - $entry['data'] = $data;
4167 +public function mxchat_handle_sitemap_submission() {
4168 + // Check if the form was submitted and verify permissions
4169 + if (!isset($_POST['submit_sitemap']) || !current_user_can('manage_options')) {
4170 + wp_die(esc_html__('Unauthorized access', 'mxchat'));
10312 4171 }
10313 4172
10314 - // Add to beginning of array (newest first)
10315 - array_unshift( $log, $entry );
4173 + // Verify nonce
4174 + check_admin_referer('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce');
10316 4175
10317 - // Keep only last 100 entries
10318 - if ( count( $log ) > 100 ) {
10319 - $log = array_slice( $log, 0, 100 );
4176 + // Validate URL
4177 + if (!isset($_POST['sitemap_url']) || empty($_POST['sitemap_url'])) {
4178 + set_transient('mxchat_admin_notice_error',
4179 + esc_html__('Please provide a valid URL.', 'mxchat'),
4180 + 30
4181 + );
4182 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4183 + exit;
10320 4184 }
10321 4185
10322 - // Save log
10323 - update_option( 'mxchat_debug_log', $log, false );
4186 + $submitted_url = esc_url_raw($_POST['sitemap_url']);
4187 + $response = wp_remote_get($submitted_url, array('timeout' => 30));
10324 4188
10325 - return true;
10326 -}
4189 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
4190 + $error_message = is_wp_error($response) ? $response->get_error_message() : 'Failed to fetch URL';
4191 + set_transient('mxchat_admin_notice_error',
4192 + sprintf(
4193 + esc_html__('Failed to fetch the URL: %s', 'mxchat'),
4194 + esc_html($error_message)
4195 + ),
4196 + 30
4197 + );
4198 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4199 + exit;
4200 + }
10327 4201
10328 -/**
10329 - * Get the debug log entries
10330 - *
10331 - * @return array The debug log entries
10332 - */
10333 -public static function mxchat_get_debug_log() {
10334 - $log = get_option( 'mxchat_debug_log', array() );
10335 - return is_array( $log ) ? $log : array();
10336 -}
4202 + $content_type = wp_remote_retrieve_header($response, 'content-type');
4203 + $body_content = wp_remote_retrieve_body($response);
10337 4204
10338 -/**
10339 - * Clear the debug log
10340 - *
10341 - * @return bool Whether the log was cleared
10342 - */
10343 -public static function mxchat_clear_debug_log() {
10344 - return delete_option( 'mxchat_debug_log' );
10345 -}
10346 -
10347 -/**
10348 - * Export settings as JSON with masked API keys
10349 - *
10350 - * @return array The sanitized settings array
10351 - */
10352 -public static function mxchat_export_settings() {
10353 - $options = get_option( 'mxchat_options', array() );
10354 -
10355 - if ( ! is_array( $options ) ) {
10356 - return array();
4205 + if (empty($body_content)) {
4206 + set_transient('mxchat_admin_notice_error',
4207 + esc_html__('Empty response received from URL.', 'mxchat'),
4208 + 30
4209 + );
4210 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4211 + exit;
10357 4212 }
10358 4213
10359 - // List of API key fields to mask
10360 - $api_key_fields = array(
10361 - 'api_key',
10362 - 'xai_api_key',
10363 - 'claude_api_key',
10364 - 'deepseek_api_key',
10365 - 'voyage_api_key',
10366 - 'gemini_api_key',
10367 - 'openrouter_api_key',
10368 - 'loops_api_key',
10369 - 'brave_api_key',
10370 - 'live_agent_secret_key',
10371 - 'live_agent_bot_token',
10372 - 'telegram_bot_token',
10373 - 'telegram_webhook_secret',
10374 - 'woocommerce_consumer_key',
10375 - 'woocommerce_consumer_secret',
10376 - );
4214 + // Handle PDF URL
4215 + if ($this->is_pdf_url($submitted_url, $response)) {
4216 + $result = $this->handle_pdf_for_knowledge_base($submitted_url, $response);
10377 4217
10378 - // Mask API keys (show only last 4 characters)
10379 - foreach ( $api_key_fields as $field ) {
10380 - if ( isset( $options[ $field ] ) && ! empty( $options[ $field ] ) ) {
10381 - $value = $options[ $field ];
10382 - if ( strlen( $value ) > 4 ) {
10383 - $options[ $field ] = str_repeat( '*', strlen( $value ) - 4 ) . substr( $value, -4 );
10384 - } else {
10385 - $options[ $field ] = '****';
10386 - }
4218 + if ($result === 'scheduled') {
4219 + set_transient(
4220 + 'mxchat_last_pdf_url',
4221 + sanitize_text_field($submitted_url),
4222 + DAY_IN_SECONDS
4223 + );
4224 + set_transient('mxchat_admin_notice_info',
4225 + esc_html__('PDF processing has started in the background. You can check the progress in the Knowledge Base section.', 'mxchat'),
4226 + 30
4227 + );
4228 + } else {
4229 + set_transient('mxchat_admin_notice_error',
4230 + esc_html__('Failed to start PDF processing. Please try again.', 'mxchat'),
4231 + 30
4232 + );
10387 4233 }
4234 +
4235 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4236 + exit;
10388 4237 }
10389 4238
10390 - // Add metadata
10391 - $export = array(
10392 - 'plugin_version' => defined( 'MXCHAT_VERSION' ) ? MXCHAT_VERSION : 'unknown',
10393 - 'export_date' => current_time( 'Y-m-d H:i:s' ),
10394 - 'wordpress_version' => get_bloginfo( 'version' ),
10395 - 'php_version' => phpversion(),
10396 - 'settings' => $options,
10397 - );
4239 + // Handle Sitemap XML
4240 + if (strpos($content_type, 'xml') !== false || strpos($body_content, '<urlset') !== false) {
4241 + libxml_use_internal_errors(true);
4242 + $xml = simplexml_load_string($body_content);
4243 + $xml_errors = libxml_get_errors();
4244 + libxml_clear_errors();
10398 4245
10399 - return $export;
10400 -}
4246 + if ($xml === false || !empty($xml_errors)) {
4247 + set_transient('mxchat_admin_notice_error',
4248 + esc_html__('Invalid sitemap XML. Please provide a valid sitemap.', 'mxchat'),
4249 + 30
4250 + );
4251 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4252 + exit;
4253 + }
10401 4254
10402 -/**
10403 - * Reset all settings to defaults
10404 - *
10405 - * @return bool Whether the reset was successful
10406 - */
10407 -public static function mxchat_reset_all_settings() {
10408 - // Delete main options
10409 - $deleted = delete_option( 'mxchat_options' );
4255 + $result = $this->handle_sitemap_for_knowledge_base($xml, $submitted_url);
10410 4256
10411 - // Also clear the debug log
10412 - delete_option( 'mxchat_debug_log' );
4257 + if ($result === 'scheduled') {
4258 + set_transient(
4259 + 'mxchat_last_sitemap_url',
4260 + sanitize_text_field($submitted_url),
4261 + DAY_IN_SECONDS
4262 + );
4263 + set_transient('mxchat_admin_notice_info',
4264 + esc_html__('Sitemap processing has started in the background. You can check the progress in the Knowledge Base section.', 'mxchat'),
4265 + 30
4266 + );
4267 + } else {
4268 + set_transient('mxchat_admin_notice_error',
4269 + esc_html__('Failed to start sitemap processing. Please try again.', 'mxchat'),
4270 + 30
4271 + );
4272 + }
10413 4273
10414 - // Log the reset (will create new log since we just cleared it)
10415 - // We need to temporarily enable debug mode to log this
10416 - $temp_options = array( 'debug_mode' => 'on' );
10417 - update_option( 'mxchat_options', $temp_options );
4274 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4275 + exit;
4276 + }
10418 4277
10419 - self::mxchat_log_debug( 'reset', 'All settings have been reset to defaults' );
4278 + // Handle Regular URL
4279 + $page_content = $this->mxchat_extract_main_content($body_content);
4280 + $sanitized_content = $this->mxchat_sanitize_content_for_api($page_content);
10420 4281
10421 - // Now delete again to trigger re-initialization
10422 - delete_option( 'mxchat_options' );
10423 -
10424 - return $deleted;
10425 -}
10426 -
10427 - // Method to append the chatbot to the body
10428 - public function mxchat_append_chatbot_to_body() {
10429 - $options = get_option('mxchat_options');
10430 - if (isset($options['append_to_body']) && $options['append_to_body'] === 'on') {
10431 - echo do_shortcode('[mxchat_chatbot floating="yes"]');
10432 - }
4282 + if (empty($sanitized_content)) {
4283 + set_transient('mxchat_admin_notice_error',
4284 + esc_html__('No valid content found on the provided URL.', 'mxchat'),
4285 + 30
4286 + );
4287 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4288 + exit;
10433 4289 }
10434 4290
10435 -
10436 -
10437 -
10438 -
10439 -private function mxchat_fetch_loops_mailing_lists($api_key) {
10440 - $url = 'https://app.loops.so/api/v1/lists';
10441 - $response = wp_remote_get($url, array(
10442 - 'headers' => array(
10443 - 'Authorization' => 'Bearer ' . $api_key,
10444 - 'Content-Type' => 'application/json'
10445 - )
10446 - ));
10447 -
10448 - if (is_wp_error($response)) {
10449 - return array();
4291 + $embedding_vector = $this->mxchat_generate_embedding($sanitized_content);
4292 + if (is_array($embedding_vector)) {
4293 + MxChat_Utils::submit_content_to_db(
4294 + $sanitized_content,
4295 + $submitted_url,
4296 + $this->options['api_key']
4297 + );
4298 + set_transient('mxchat_admin_notice_success',
4299 + esc_html__('URL content successfully submitted!', 'mxchat'),
4300 + 30
4301 + );
4302 + } else {
4303 + set_transient('mxchat_admin_notice_error',
4304 + esc_html__('Failed to generate embedding for the URL content. Please check your API key and try again.', 'mxchat'),
4305 + 30
4306 + );
10450 4307 }
10451 4308
10452 - $body = wp_remote_retrieve_body($response);
10453 - $lists = json_decode($body, true);
4309 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
4310 + exit;
4311 +}
4312 +private function is_pdf_url($url, $response) {
4313 + $content_type = wp_remote_retrieve_header($response, 'content-type');
4314 + $file_extension = strtolower(pathinfo($url, PATHINFO_EXTENSION));
10454 4315
10455 - return isset($lists) && is_array($lists) ? $lists : array();
4316 + return strpos($content_type, 'pdf') !== false || $file_extension === 'pdf';
10456 4317 }
10457 4318
10458 -function mxchat_calculate_cosine_similarity($vec1, $vec2) {
10459 - if (empty($vec1) || empty($vec2)) {
10460 - return 0.0;
4319 +private function handle_pdf_for_knowledge_base($pdf_url, $response) {
4320 + if (!current_user_can('manage_options')) {
4321 + //error_log('Unauthorized PDF processing attempt');
4322 + return false;
10461 4323 }
10462 4324
10463 - $dot_product = 0.0;
10464 - $norm_a = 0.0;
10465 - $norm_b = 0.0;
4325 + $pdf_url = esc_url_raw($pdf_url);
4326 + $upload_dir = wp_upload_dir();
10466 4327
10467 - for ($i = 0; $i < count($vec1); $i++) {
10468 - $dot_product += $vec1[$i] * $vec2[$i];
10469 - $norm_a += pow($vec1[$i], 2);
10470 - $norm_b += pow($vec2[$i], 2);
4328 + if (isset($upload_dir['error']) && $upload_dir['error'] !== false) {
4329 + //error_log(sprintf('Upload directory error: %s', esc_html($upload_dir['error'])));
4330 + return false;
10471 4331 }
10472 4332
10473 - if ($norm_a == 0.0 || $norm_b == 0.0) {
10474 - return 0.0;
10475 - } else {
10476 - return $dot_product / (sqrt($norm_a) * sqrt($norm_b));
4333 + $pdf_filename = sanitize_file_name('mxchat_kb_' . time() . '.pdf');
4334 + $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
4335 +
4336 + $response_body = wp_remote_retrieve_body($response);
4337 + if (empty($response_body)) {
4338 + //error_log('Empty PDF response body');
4339 + return false;
10477 4340 }
10478 -}
10479 4341
10480 -/**
10481 - * Validates nonce and deletes all chat prompts
10482 - */
10483 -public function mxchat_handle_delete_all_prompts() {
10484 - //error_log('=== DELETE ALL DEBUG START ===');
10485 -
10486 - // Verify nonce
10487 - if (!isset($_POST['mxchat_delete_all_prompts_nonce']) || !wp_verify_nonce($_POST['mxchat_delete_all_prompts_nonce'], 'mxchat_delete_all_prompts_action')) {
10488 - //error_log('DEBUG: Nonce verification failed');
10489 - wp_die(__('Nonce verification failed.', 'mxchat'));
4342 + if (!wp_mkdir_p(dirname($pdf_path))) {
4343 + //error_log(sprintf('Failed to create directory for PDF: %s', esc_html($pdf_path)));
4344 + return false;
10490 4345 }
10491 -
10492 - // Check permissions
10493 - if (!current_user_can('manage_options')) {
10494 - //error_log('DEBUG: Permission check failed');
10495 - wp_die(__('You do not have sufficient permissions to delete all prompts.', 'mxchat'));
10496 - }
10497 -
10498 - // Get bot_id and content type filter from POST data
10499 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
10500 - $content_type_filter = isset($_POST['content_type_filter']) ? sanitize_text_field($_POST['content_type_filter']) : '';
10501 4346
10502 - $success = true;
10503 - $error_messages = array();
4347 + try {
4348 + file_put_contents($pdf_path, $response_body);
10504 4349
10505 - // Get bot-specific Pinecone configuration
10506 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
10507 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
4350 + if (!file_exists($pdf_path)) {
4351 + throw new Exception('Failed to save PDF file');
4352 + }
10508 4353
10509 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4354 + $parser = new \Smalot\PdfParser\Parser();
4355 + $pdf = $parser->parseFile($pdf_path);
4356 + $total_pages = absint(count($pdf->getPages()));
10510 4357
10511 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
10512 - // Delete from Pinecone (with optional content type filter)
10513 - $result = $pinecone_manager->mxchat_delete_all_from_pinecone($pinecone_options, $content_type_filter);
4358 + if ($total_pages < 1) {
4359 + throw new Exception('Invalid PDF: no pages found');
4360 + }
10514 4361
10515 - if (!$result['success']) {
10516 - $success = false;
10517 - $error_messages[] = $result['message'];
10518 - }
4362 + wp_schedule_single_event(time(), 'mxchat_process_pdf_pages', array(
4363 + 'pdf_path' => $pdf_path,
4364 + 'pdf_url' => $pdf_url,
4365 + 'total_pages' => $total_pages,
4366 + 'batch_size' => absint(15),
4367 + 'batch_pause' => absint(10)
4368 + ));
10519 4369
10520 - } else {
10521 - // Delete from WordPress database
10522 - global $wpdb;
10523 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4370 + $status_data = array(
4371 + 'total_pages' => $total_pages,
4372 + 'processed_pages' => 0,
4373 + 'status' => 'processing',
4374 + 'last_update' => time()
4375 + );
10524 4376
10525 - // Build WHERE conditions
10526 - $where_clauses = array();
10527 - $where_values = array();
4377 + set_transient(
4378 + sanitize_key('mxchat_pdf_status_' . md5($pdf_url)),
4379 + array_map('sanitize_text_field', $status_data),
4380 + DAY_IN_SECONDS
4381 + );
10528 4382
10529 - // Bot filter
10530 - if ($bot_id !== 'default' && class_exists('MxChat_Multi_Bot_Manager')) {
10531 - $where_clauses[] = 'bot_id = %s';
10532 - $where_values[] = $bot_id;
10533 - }
4383 + return 'scheduled';
10534 4384
10535 - // Content type filter
10536 - if (!empty($content_type_filter)) {
10537 - $where_clauses[] = 'content_type = %s';
10538 - $where_values[] = $content_type_filter;
4385 + } catch (Exception $e) {
4386 + //error_log(sprintf('Error preparing PDF for processing: %s', esc_html($e->getMessage())));
4387 + if (file_exists($pdf_path)) {
4388 + wp_delete_file($pdf_path);
10539 4389 }
4390 + return false;
4391 + }
4392 +}
4393 +public static function process_pdf_pages_cron($pdf_path, $pdf_url, $total_pages, $batch_size, $batch_pause) {
4394 + // Validate inputs
4395 + $pdf_path = sanitize_text_field($pdf_path);
4396 + $pdf_url = esc_url_raw($pdf_url);
4397 + $total_pages = absint($total_pages);
4398 + $batch_size = absint($batch_size);
4399 + $batch_pause = absint($batch_pause);
10540 4400
10541 - if (!empty($where_clauses)) {
10542 - $where_sql = implode(' AND ', $where_clauses);
10543 - $result = $wpdb->query($wpdb->prepare("DELETE FROM {$table_name} WHERE {$where_sql}", $where_values));
10544 - } else {
10545 - // No filters — delete all
10546 - $result = $wpdb->query("DELETE FROM {$table_name}");
4401 + try {
4402 + if (!file_exists($pdf_path)) {
4403 + throw new Exception(sprintf('PDF file not found at path: %s', esc_html($pdf_path)));
10547 4404 }
10548 4405
10549 - if ($result === false) {
10550 - $success = false;
10551 - $error_messages[] = 'Failed to delete from WordPress database';
10552 - }
10553 - }
4406 + $parser = new \Smalot\PdfParser\Parser();
4407 + $pdf = $parser->parseFile($pdf_path);
4408 + $pages = $pdf->getPages();
10554 4409
10555 - // Redirect back with a success message and bot_id
10556 - $redirect_url = add_query_arg(array(
10557 - 'page' => 'mxchat-prompts',
10558 - 'bot_id' => $bot_id,
10559 - 'all_deleted' => $success ? 'true' : 'false'
10560 - ), admin_url('admin.php'));
10561 -
10562 - wp_safe_redirect($redirect_url);
10563 - exit;
10564 -}
4410 + // Get current progress
4411 + $status_key = sanitize_key('mxchat_pdf_status_' . md5($pdf_url));
4412 + $status = get_transient($status_key);
10565 4413
4414 + if (!$status || !is_array($status)) {
4415 + throw new Exception('Invalid status data retrieved from transient');
4416 + }
10566 4417
4418 + $start_page = absint($status['processed_pages']);
4419 + $end_page = min($start_page + $batch_size, $total_pages);
10567 4420
10568 - /**
10569 - * Handles deletion prompt with nonce validation
10570 - */
10571 - public function mxchat_handle_delete_prompt() {
10572 - // Sanitize and validate nonce
10573 - $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : '';
10574 - if (empty($nonce) || !wp_verify_nonce($nonce, 'mxchat_delete_prompt_nonce')) {
10575 - wp_die(esc_html__('Nonce verification failed.', 'mxchat'));
10576 - }
4421 + $instance = new self(); // Create an instance of the class
10577 4422
10578 - // Check permissions
10579 - if (!current_user_can('manage_options')) {
10580 - wp_die(esc_html__('You do not have sufficient permissions to delete prompts.', 'mxchat'));
10581 - }
4423 + for ($i = $start_page; $i < $end_page; $i++) {
4424 + $text = $pages[$i]->getText();
4425 + $sanitized_content = $instance->mxchat_sanitize_content_for_api($text); // Call via instance
10582 4426
10583 - // Get ID and source parameters
10584 - $id = isset($_GET['id']) ? sanitize_text_field($_GET['id']) : '';
10585 - $source = isset($_GET['source']) ? sanitize_text_field($_GET['source']) : '';
4427 + if (!empty($sanitized_content)) {
4428 + $embedding_vector = $instance->mxchat_generate_embedding($sanitized_content); // Call via instance
4429 + if (is_array($embedding_vector)) {
4430 + $metadata = array(
4431 + 'document_type' => 'pdf',
4432 + 'total_pages' => $total_pages,
4433 + 'current_page' => $i + 1,
4434 + 'prev_page' => $i > 0 ? $i : null,
4435 + 'next_page' => $i < ($total_pages - 1) ? $i + 2 : null,
4436 + 'source_url' => $pdf_url
4437 + );
10586 4438
10587 - if (empty($id)) {
10588 - wp_die(esc_html__('Invalid prompt ID.', 'mxchat'));
10589 - }
4439 + $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized_content;
4440 + $page_url = esc_url($pdf_url . "#page=" . ($i + 1));
10590 4441
10591 - $success = false;
10592 - $error_message = '';
4442 + $options = get_option('mxchat_options');
4443 + MxChat_Utils::submit_content_to_db($content_with_metadata, $page_url, $options['api_key']);
4444 + }
4445 + }
10593 4446
10594 - // Check if Pinecone is enabled and determine source automatically if not specified
10595 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
10596 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4447 + // Update progress with sanitized data
4448 + $status['processed_pages'] = absint($i + 1);
4449 + $status['last_update'] = time();
4450 + set_transient($status_key, array_map('sanitize_text_field', $status), DAY_IN_SECONDS);
4451 + }
10597 4452
10598 - // If source is not specified, determine based on Pinecone configuration
10599 - if (empty($source)) {
10600 - $source = $use_pinecone ? 'pinecone' : 'wordpress';
10601 - }
4453 + // Schedule next batch if needed
4454 + if ($end_page < $total_pages) {
4455 + wp_schedule_single_event(time() + $batch_pause, 'mxchat_process_pdf_pages', array(
4456 + 'pdf_path' => $pdf_path,
4457 + 'pdf_url' => $pdf_url,
4458 + 'total_pages' => $total_pages,
4459 + 'batch_size' => $batch_size,
4460 + 'batch_pause' => $batch_pause
4461 + ));
4462 + } else {
4463 + // Processing complete
4464 + $status['status'] = 'complete';
4465 + set_transient($status_key, array_map('sanitize_text_field', $status), DAY_IN_SECONDS);
4466 + if (file_exists($pdf_path)) {
4467 + wp_delete_file($pdf_path);
4468 + }
4469 + }
10602 4470
10603 - if ($source === 'pinecone' || $use_pinecone) {
10604 - //error_log('[MXCHAT-DELETE] Deleting from Pinecone, ID: ' . $id);
4471 + } catch (\Exception $e) {
4472 + $status['status'] = 'error';
4473 + $status['error'] = sanitize_text_field($e->getMessage());
4474 + set_transient($status_key, array_map('sanitize_text_field', $status), DAY_IN_SECONDS);
4475 + if (file_exists($pdf_path)) {
4476 + wp_delete_file($pdf_path);
4477 + }
4478 + }
4479 +}
10605 4480
10606 - // Handle Pinecone deletion
10607 - if (empty($pinecone_options['mxchat_pinecone_host']) ||
10608 - empty($pinecone_options['mxchat_pinecone_api_key'])) {
10609 - wp_die(esc_html__('Pinecone configuration is missing.', 'mxchat'));
10610 - }
4481 +public function get_pdf_processing_status($pdf_url) {
4482 + $pdf_url = esc_url_raw($pdf_url);
4483 + $status = get_transient(sanitize_key('mxchat_pdf_status_' . md5($pdf_url)));
10611 4484
10612 - // Delete from Pinecone using the vector ID directly
10613 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
10614 - $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
10615 - $id,
10616 - $pinecone_options['mxchat_pinecone_api_key'],
10617 - $pinecone_options['mxchat_pinecone_host']
10618 - );
10619 - if ($result['success']) {
10620 - $success = true;
4485 + if (!$status || !is_array($status)) {
4486 + return false;
4487 + }
10621 4488
10622 - // Remove from vector cache
10623 - $pinecone_manager->mxchat_remove_from_pinecone_vector_cache($id);
10624 - $pinecone_manager->mxchat_remove_from_processed_content_caches($id);
4489 + return array(
4490 + 'total_pages' => absint($status['total_pages']),
4491 + 'processed_pages' => absint($status['processed_pages']),
4492 + 'percentage' => round((absint($status['processed_pages']) / absint($status['total_pages'])) * 100),
4493 + 'status' => sanitize_text_field($status['status']),
4494 + 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ago'
4495 + );
4496 +}
10625 4497
10626 - set_transient('mxchat_admin_notice_success',
10627 - esc_html__('Vector deleted successfully from Pinecone.', 'mxchat'), 30);
10628 - } else {
10629 - $error_message = $result['message'];
10630 - set_transient('mxchat_admin_notice_error',
10631 - esc_html__('Failed to delete from Pinecone: ', 'mxchat') . esc_html($error_message), 30);
10632 - }
10633 - } else {
10634 - //error_log('[MXCHAT-DELETE] Deleting from WordPress database, ID: ' . $id);
4498 +private function handle_sitemap_for_knowledge_base($xml, $sitemap_url) {
4499 + if (!current_user_can('manage_options')) {
4500 + //error_log('Unauthorized sitemap processing attempt');
4501 + return false;
4502 + }
10635 4503
10636 - // Handle WordPress database deletion
10637 - global $wpdb;
10638 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4504 + try {
4505 + $sitemap_url = esc_url_raw($sitemap_url);
10639 4506
10640 - // Clear cache and delete prompt
10641 - wp_cache_delete('prompt_' . $id, 'mxchat_prompts');
4507 + if (!$xml || !is_object($xml)) {
4508 + throw new Exception('Invalid XML object provided');
4509 + }
10642 4510
10643 - $result = $wpdb->delete(
10644 - $table_name,
10645 - array('id' => intval($id)),
10646 - array('%d')
10647 - );
4511 + $urls = [];
4512 + foreach ($xml->url as $url_element) {
4513 + $url = esc_url_raw((string)$url_element->loc);
4514 + if ($url) {
4515 + $urls[] = $url;
4516 + }
4517 + }
10648 4518
10649 - if ($result !== false) {
10650 - $success = true;
10651 - set_transient('mxchat_admin_notice_success',
10652 - esc_html__('Entry deleted successfully.', 'mxchat'), 30);
10653 - } else {
10654 - set_transient('mxchat_admin_notice_error',
10655 - esc_html__('Failed to delete entry from database.', 'mxchat'), 30);
10656 - }
10657 - }
4519 + $total_urls = absint(count($urls));
10658 4520
10659 - // Redirect back to the prompts page
10660 - wp_safe_redirect(add_query_arg(
10661 - array(
10662 - 'page' => 'mxchat-prompts',
10663 - 'deleted' => $success ? 'true' : 'false'
10664 - ),
10665 - admin_url('admin.php')
10666 - ));
10667 - exit;
10668 - }
4521 + if ($total_urls < 1) {
4522 + throw new Exception('No valid URLs found in sitemap');
4523 + }
10669 4524
4525 + wp_schedule_single_event(time(), 'mxchat_process_sitemap_urls', array(
4526 + 'urls' => $urls,
4527 + 'sitemap_url' => $sitemap_url,
4528 + 'total_urls' => $total_urls,
4529 + 'batch_size' => absint(10),
4530 + 'batch_pause' => absint(5)
4531 + ));
10670 4532
4533 + $status_data = array(
4534 + 'total_urls' => $total_urls,
4535 + 'processed_urls' => 0,
4536 + 'status' => 'processing',
4537 + 'last_update' => time()
4538 + );
10671 4539
4540 + set_transient(
4541 + sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url)),
4542 + array_map('sanitize_text_field', $status_data),
4543 + DAY_IN_SECONDS
4544 + );
10672 4545
10673 - /**
10674 - * Averages multiple vectors into a single vector
10675 - */
10676 - private function mxchat_average_vectors($vectors) {
10677 - $vector_length = count($vectors[0]);
10678 - $sum_vector = array_fill(0, $vector_length, 0);
4546 + return 'scheduled';
10679 4547
10680 - foreach ($vectors as $vector) {
10681 - for ($i = 0; $i < $vector_length; $i++) {
10682 - $sum_vector[$i] += $vector[$i];
10683 - }
10684 - }
4548 + } catch (\Exception $e) {
4549 + //error_log(sprintf('Error preparing sitemap for processing: %s', esc_html($e->getMessage())));
4550 + return false;
4551 + }
4552 +}
4553 +public static function process_sitemap_urls_cron($urls, $sitemap_url, $total_urls, $batch_size, $batch_pause) {
4554 + // Validate inputs
4555 + $sitemap_url = esc_url_raw($sitemap_url);
4556 + $total_urls = absint($total_urls);
4557 + $batch_size = absint($batch_size);
4558 + $batch_pause = absint($batch_pause);
10685 4559
10686 - // Divide each component by the number of vectors to get the average
10687 - $num_vectors = count($vectors);
10688 - for ($i = 0; $i < $vector_length; $i++) {
10689 - $sum_vector[$i] /= $num_vectors;
10690 - }
4560 + if (!is_array($urls) || empty($urls)) {
4561 + return;
4562 + }
10691 4563
10692 - return $sum_vector;
10693 - }
4564 + try {
4565 + $status_key = sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url));
4566 + $status = get_transient($status_key);
10694 4567
4568 + if (!$status || !is_array($status)) {
4569 + throw new Exception('Invalid status data retrieved from transient');
4570 + }
10695 4571
4572 + $start_url = absint($status['processed_urls']);
4573 + $end_url = min($start_url + $batch_size, $total_urls);
10696 4574
4575 + $instance = new self(); // Create an instance of the class
4576 + $embedding_success = true;
10697 4577
10698 - /**
10699 - * Generates embeddings from input text for MXChat
10700 - */
10701 - public function mxchat_generate_embedding($text) {
10702 - // Enable detailed logging for debugging
10703 - //error_log('[MXCHAT-EMBED] Starting embedding generation. Text length: ' . strlen($text) . ' bytes');
10704 - //error_log('[MXCHAT-EMBED] Text preview: ' . substr($text, 0, 100) . '...');
4578 + for ($i = $start_url; $i < $end_url; $i++) {
4579 + $page_url = esc_url_raw($urls[$i]);
4580 + $page_response = wp_remote_get($page_url);
10705 4581
10706 - $options = get_option('mxchat_options');
10707 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
10708 - //error_log('[MXCHAT-EMBED] Selected embedding model: ' . $selected_model);
4582 + if (is_wp_error($page_response) || wp_remote_retrieve_response_code($page_response) !== 200) {
4583 + continue;
4584 + }
10709 4585
10710 - // Determine provider and endpoint
10711 - if (strpos($selected_model, 'voyage') === 0) {
10712 - $api_key = $options['voyage_api_key'] ?? '';
10713 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
10714 - $provider_name = 'Voyage AI';
10715 - //error_log('[MXCHAT-EMBED] Using Voyage AI API');
10716 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
10717 - $api_key = $options['gemini_api_key'] ?? '';
10718 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
10719 - $provider_name = 'Google Gemini';
10720 - //error_log('[MXCHAT-EMBED] Using Google Gemini API');
10721 - } else {
10722 - $api_key = $options['api_key'] ?? '';
10723 - $endpoint = 'https://api.openai.com/v1/embeddings';
10724 - $provider_name = 'OpenAI';
10725 - //error_log('[MXCHAT-EMBED] Using OpenAI API');
10726 - }
4586 + $page_html = wp_remote_retrieve_body($page_response);
4587 + $page_content = $instance->mxchat_extract_main_content($page_html); // Call via instance
4588 + $sanitized_content = $instance->mxchat_sanitize_content_for_api($page_content); // Call via instance
10727 4589
10728 - //error_log('[MXCHAT-EMBED] Using endpoint: ' . $endpoint);
4590 + if (!empty($sanitized_content)) {
4591 + $embedding_vector = $instance->mxchat_generate_embedding($sanitized_content); // Call via instance
4592 + if (is_array($embedding_vector)) {
4593 + $options = get_option('mxchat_options');
4594 + MxChat_Utils::submit_content_to_db($sanitized_content, $page_url, $options['api_key']);
4595 + } else {
4596 + $embedding_success = false;
4597 + }
4598 + }
10729 4599
10730 - if (empty($api_key)) {
10731 - $error_message = sprintf('Missing %s API key. Please configure your API key in the MxChat settings.', $provider_name);
10732 - //error_log('[MXCHAT-EMBED] Error: ' . $error_message);
10733 - return $error_message;
10734 - }
4600 + $status['processed_urls'] = absint($i + 1);
4601 + $status['last_update'] = time();
4602 + set_transient($status_key, array_map('sanitize_text_field', $status), DAY_IN_SECONDS);
4603 + }
10735 4604
10736 - // Check if text is too long (for OpenAI, roughly estimate tokens as words/0.75)
10737 - $estimated_tokens = ceil(str_word_count($text) / 0.75);
10738 - //error_log('[MXCHAT-EMBED] Estimated token count: ~' . $estimated_tokens);
4605 + if ($end_url < $total_urls) {
4606 + wp_schedule_single_event(time() + $batch_pause, 'mxchat_process_sitemap_urls', array(
4607 + 'urls' => array_map('esc_url_raw', $urls),
4608 + 'sitemap_url' => $sitemap_url,
4609 + 'total_urls' => $total_urls,
4610 + 'batch_size' => $batch_size,
4611 + 'batch_pause' => $batch_pause,
4612 + ));
4613 + } else {
4614 + $status['status'] = 'complete';
4615 + set_transient($status_key, array_map('sanitize_text_field', $status), DAY_IN_SECONDS);
4616 + }
4617 + } catch (\Exception $e) {
4618 + $status['status'] = 'error';
4619 + $status['error'] = sanitize_text_field($e->getMessage());
4620 + set_transient($status_key, array_map('sanitize_text_field', $status), DAY_IN_SECONDS);
4621 + }
4622 +}
10739 4623
10740 - if ($estimated_tokens > 8000 && strpos($selected_model, 'voyage') === false && strpos($selected_model, 'gemini-embedding') === false) {
10741 - //error_log('[MXCHAT-EMBED] Warning: Text may exceed OpenAI token limits (8K for most models)');
10742 - // Consider truncating text here
10743 - }
4624 +public function get_sitemap_processing_status($sitemap_url) {
4625 + $sitemap_url = esc_url_raw($sitemap_url);
4626 + $status = get_transient(sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url)));
10744 4627
10745 - // Prepare request body based on provider
10746 - if (strpos($selected_model, 'gemini-embedding') === 0) {
10747 - // Gemini API format
10748 - $request_body = array(
10749 - 'model' => 'models/' . $selected_model,
10750 - 'content' => array(
10751 - 'parts' => array(
10752 - array('text' => $text)
10753 - )
10754 - )
10755 - );
4628 + if (!$status || !is_array($status)) {
4629 + return false;
4630 + }
10756 4631
10757 - // Set output dimensionality to 1536 for consistency with other models
10758 - $request_body['outputDimensionality'] = 1536;
10759 - } else {
10760 - // OpenAI/Voyage API format
10761 - $request_body = array(
10762 - 'model' => $selected_model,
10763 - 'input' => $text
10764 - );
4632 + return array(
4633 + 'total_urls' => absint($status['total_urls']),
4634 + 'processed_urls' => absint($status['processed_urls']),
4635 + 'percentage' => round((absint($status['processed_urls']) / absint($status['total_urls'])) * 100),
4636 + 'status' => sanitize_text_field($status['status']),
4637 + 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ago'
4638 + );
4639 +}
4640 +public function mxchat_stop_processing() {
4641 + // Verify permissions
4642 + if (!current_user_can('manage_options')) {
4643 + wp_die(esc_html__('Unauthorized access', 'mxchat'));
4644 + }
10765 4645
10766 - // Add output_dimension for voyage-3-large model
10767 - if ($selected_model === 'voyage-3-large') {
10768 - $request_body['output_dimension'] = 2048;
10769 - }
10770 - }
4646 + // Verify nonce
4647 + check_admin_referer('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce');
10771 4648
10772 - //error_log('[MXCHAT-EMBED] Request prepared with model: ' . $selected_model);
4649 + // Get the last sitemap URL and clear its transient
4650 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
4651 + if ($sitemap_url) {
4652 + delete_transient('mxchat_sitemap_status_' . md5($sitemap_url));
4653 + delete_transient('mxchat_last_sitemap_url');
4654 + }
10773 4655
10774 - // Prepare headers based on provider
10775 - if (strpos($selected_model, 'gemini-embedding') === 0) {
10776 - // Gemini uses API key as query parameter
10777 - $endpoint .= '?key=' . $api_key;
10778 - $headers = array(
10779 - 'Content-Type' => 'application/json'
10780 - );
10781 - } else {
10782 - // OpenAI/Voyage use Bearer token
10783 - $headers = array(
10784 - 'Authorization' => 'Bearer ' . $api_key,
10785 - 'Content-Type' => 'application/json'
10786 - );
10787 - }
4656 + // Get the last PDF URL and clear its transient
4657 + $pdf_url = get_transient('mxchat_last_pdf_url');
4658 + if ($pdf_url) {
4659 + delete_transient('mxchat_pdf_status_' . md5($pdf_url));
4660 + delete_transient('mxchat_last_pdf_url');
4661 + }
10788 4662
10789 - // Make API request
10790 - //error_log('[MXCHAT-EMBED] Sending API request to: ' . $endpoint);
10791 - $response = wp_remote_post($endpoint, array(
10792 - 'body' => wp_json_encode($request_body),
10793 - 'headers' => $headers,
10794 - 'timeout' => 60 // Increased timeout for large inputs
10795 - ));
4663 + // Unschedule any pending sitemap events
4664 + $timestamp = wp_next_scheduled('mxchat_process_sitemap_urls');
4665 + if ($timestamp) {
4666 + wp_unschedule_event($timestamp, 'mxchat_process_sitemap_urls');
4667 + }
10796 4668
10797 - // Handle wp_remote_post errors
10798 - if (is_wp_error($response)) {
10799 - $error_message = $response->get_error_message();
10800 - //error_log('[MXCHAT-EMBED] WP Remote Post Error: ' . $error_message);
10801 - return 'Connection error: ' . $error_message;
10802 - }
4669 + // Redirect back with a success message
4670 + set_transient('mxchat_admin_notice_success',
4671 + esc_html__('Processing has been stopped successfully.', 'mxchat'),
4672 + 30
4673 + );
4674 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
4675 + exit;
4676 +}
4677 +private function mxchat_sanitize_content_for_api($content) {
4678 + // Remove script, style tags, and HTML comments
4679 + $content = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $content);
4680 + $content = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $content);
4681 + $content = preg_replace('/<!--(.|\s)*?-->/', '', $content);
10803 4682
10804 - // Get and check HTTP response code
10805 - $http_code = wp_remote_retrieve_response_code($response);
10806 - //error_log('[MXCHAT-EMBED] API Response Code: ' . $http_code);
4683 + // Remove all HTML tags and decode HTML entities
4684 + $content = wp_strip_all_tags($content);
4685 + $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5);
10807 4686
10808 - if ($http_code !== 200) {
10809 - $error_body = wp_remote_retrieve_body($response);
10810 - //error_log('[MXCHAT-EMBED] API Error Response Body: ' . $error_body);
4687 + // Trim and normalize whitespace
4688 + $content = trim(preg_replace('/\s+/', ' ', $content));
10811 4689
10812 - // Try to parse error for more details
10813 - $error_json = json_decode($error_body, true);
10814 - if (json_last_error() === JSON_ERROR_NONE && isset($error_json['error'])) {
10815 - $error_type = $error_json['error']['type'] ?? 'unknown';
10816 - $error_message = $error_json['error']['message'] ?? 'No message';
10817 - //error_log('[MXCHAT-EMBED] API Error Type: ' . $error_type);
10818 - //error_log('[MXCHAT-EMBED] API Error Message: ' . $error_message);
4690 + return $content;
4691 +}
10819 4692
10820 - // Customize error message for common API errors
10821 - if ($error_type === 'invalid_request_error' && strpos($error_message, 'API key') !== false) {
10822 - $error_message = sprintf('Invalid %s API key. Please check your API key in the MxChat settings.', $provider_name);
10823 - } elseif ($error_type === 'authentication_error') {
10824 - $error_message = sprintf('%s authentication failed. Please verify your API key in the MxChat settings.', $provider_name);
10825 - }
10826 4693
10827 - //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
10828 - return $error_message;
10829 - }
10830 4694
10831 - $error_message = sprintf("API Error (HTTP %d): Unable to generate embedding", $http_code);
10832 - //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
10833 - return $error_message;
10834 - }
4695 +private function mxchat_fetch_loops_mailing_lists($api_key) {
4696 + $url = 'https://app.loops.so/api/v1/lists';
4697 + $response = wp_remote_get($url, array(
4698 + 'headers' => array(
4699 + 'Authorization' => 'Bearer ' . $api_key,
4700 + 'Content-Type' => 'application/json'
4701 + )
4702 + ));
10835 4703
10836 - // Parse response body
10837 - $response_body = wp_remote_retrieve_body($response);
10838 - //error_log('[MXCHAT-EMBED] Received response length: ' . strlen($response_body) . ' bytes');
4704 + if (is_wp_error($response)) {
4705 + return array();
4706 + }
10839 4707
10840 - $response_data = json_decode($response_body, true);
4708 + $body = wp_remote_retrieve_body($response);
4709 + $lists = json_decode($body, true);
10841 4710
10842 - if (json_last_error() !== JSON_ERROR_NONE) {
10843 - $error = json_last_error_msg();
10844 - //error_log('[MXCHAT-EMBED] JSON Parse Error: ' . $error);
10845 - //error_log('[MXCHAT-EMBED] Response preview: ' . substr($response_body, 0, 200));
10846 - return "Failed to parse API response: $error";
10847 - }
4711 + return isset($lists) && is_array($lists) ? $lists : array();
4712 +}
10848 4713
10849 - // Handle different response formats based on provider
10850 - if (strpos($selected_model, 'gemini-embedding') === 0) {
10851 - // Gemini API response format
10852 - if (isset($response_data['embedding']['values'])) {
10853 - $embedding_dimensions = count($response_data['embedding']['values']);
10854 - //error_log('[MXCHAT-EMBED] Successfully extracted Gemini embedding with ' . $embedding_dimensions . ' dimensions');
10855 4714
10856 - // Check if embedding dimensions are as expected (should be 1536)
10857 - if ($embedding_dimensions !== 1536) {
10858 - //error_log('[MXCHAT-EMBED] Warning: Unexpected Gemini embedding dimensions: ' . $embedding_dimensions);
10859 - }
10860 4715
10861 - MxChat_Utils::stamp_active_embedding_model($selected_model);
10862 - return $response_data['embedding']['values'];
10863 - } else {
10864 - //error_log('[MXCHAT-EMBED] Error: No embedding found in Gemini response');
10865 - //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
10866 4716
10867 - if (isset($response_data['error'])) {
10868 - $error_message = "Gemini API Error in response: " . wp_json_encode($response_data['error']);
10869 - //error_log('[MXCHAT-EMBED] ' . $error_message);
10870 - return $error_message;
10871 - }
10872 4717
10873 - $error_message = "Invalid Gemini API response format: No embedding found";
10874 - //error_log('[MXCHAT-EMBED] ' . $error_message);
10875 - return $error_message;
10876 - }
10877 - } else {
10878 - // OpenAI/Voyage API response format
10879 - if (isset($response_data['data'][0]['embedding'])) {
10880 - $embedding_dimensions = count($response_data['data'][0]['embedding']);
10881 - //error_log('[MXCHAT-EMBED] Successfully extracted embedding with ' . $embedding_dimensions . ' dimensions');
4718 +function mxchat_calculate_cosine_similarity($vec1, $vec2) {
4719 + if (empty($vec1) || empty($vec2)) {
4720 + return 0.0;
4721 + }
10882 4722
10883 - // Check if embedding dimensions are as expected
10884 - if (($selected_model === 'text-embedding-ada-002' && $embedding_dimensions !== 1536) ||
10885 - ($selected_model === 'voyage-3-large' && $embedding_dimensions !== 2048)) {
10886 - //error_log('[MXCHAT-EMBED] Warning: Unexpected embedding dimensions');
10887 - }
4723 + $dot_product = 0.0;
4724 + $norm_a = 0.0;
4725 + $norm_b = 0.0;
10888 4726
10889 - MxChat_Utils::stamp_active_embedding_model($selected_model);
10890 - return $response_data['data'][0]['embedding'];
10891 - } else {
10892 - //error_log('[MXCHAT-EMBED] Error: No embedding found in response');
10893 - //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
4727 + for ($i = 0; $i < count($vec1); $i++) {
4728 + $dot_product += $vec1[$i] * $vec2[$i];
4729 + $norm_a += pow($vec1[$i], 2);
4730 + $norm_b += pow($vec2[$i], 2);
4731 + }
10894 4732
10895 - if (isset($response_data['error'])) {
10896 - $error_message = "API Error in response: " . wp_json_encode($response_data['error']);
10897 - //error_log('[MXCHAT-EMBED] ' . $error_message);
10898 - return $error_message;
10899 - }
4733 + if ($norm_a == 0.0 || $norm_b == 0.0) {
4734 + return 0.0;
4735 + } else {
4736 + return $dot_product / (sqrt($norm_a) * sqrt($norm_b));
4737 + }
4738 +}
10900 4739
10901 - $error_message = "Invalid API response format: No embedding found";
10902 - //error_log('[MXCHAT-EMBED] ' . $error_message);
10903 - return $error_message;
10904 - }
10905 - }
10906 - }
10907 4740
10908 4741
10909 4742
10910 4743 }