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

admin-knowledge-page.php in MxChat – AI Chatbot & Content Generation for WordPress 3.2.18, at includes/admin-knowledge-page.php

2,578 lines 176.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MxChat Knowledge Base Page - Redesigned with Sidebar Navigation
4 *
5 * @package MxChat
6 * @since 2.7.0
7 */
8
9 if (!defined('ABSPATH')) {
10 exit;
11 }
12
13 /**
14 * Render the new sidebar-based Knowledge Base page
15 */
16 function mxchat_render_knowledge_page($admin_instance, $knowledge_manager, $page_data) {
17 $is_activated = $admin_instance->is_activated();
18 $plugin_url = plugin_dir_url(dirname(__FILE__));
19
20 // Check if integrations are active
21 $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
22 $is_pinecone_active = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
23
24 $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
25 $is_vectorstore_active = ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1';
26
27 // Extract page data
28 extract($page_data);
29 ?>
30 <div class="mxch-admin-wrapper">
31 <!-- Mobile Header -->
32 <header class="mxch-mobile-header">
33 <a href="#" class="mxch-mobile-logo">
34 <div class="mxch-mobile-logo-icon">
35 <img src="<?php echo esc_url($plugin_url . 'images/icon-128x128.png'); ?>" alt="MxChat">
36 </div>
37 <span class="mxch-mobile-logo-text">MxChat</span>
38 </a>
39 <button type="button" class="mxch-mobile-menu-btn" aria-label="<?php esc_attr_e('Open menu', 'mxchat'); ?>">
40 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="1"/><circle cx="12" cy="5" r="1"/><circle cx="12" cy="19" r="1"/></svg>
41 </button>
42 </header>
43
44 <!-- Mobile Menu Overlay -->
45 <div class="mxch-mobile-overlay"></div>
46
47 <!-- Mobile Menu Modal -->
48 <div class="mxch-mobile-menu">
49 <div class="mxch-mobile-menu-header">
50 <span class="mxch-mobile-menu-title"><?php esc_html_e('Knowledge Base', 'mxchat'); ?></span>
51 <button type="button" class="mxch-mobile-menu-close" aria-label="<?php esc_attr_e('Close menu', 'mxchat'); ?>">
52 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
53 </button>
54 </div>
55 <nav class="mxch-mobile-menu-nav">
56 <!-- Import Section -->
57 <div class="mxch-mobile-nav-section">
58 <div class="mxch-mobile-nav-section-title"><?php esc_html_e('Import', 'mxchat'); ?></div>
59 <button class="mxch-mobile-nav-link active" data-target="import-options">
60 <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"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
61 <span><?php esc_html_e('Import Options', 'mxchat'); ?></span>
62 </button>
63 <button class="mxch-mobile-nav-link" data-target="knowledge-base">
64 <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"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"/><path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"/></svg>
65 <span><?php esc_html_e('Knowledge Base', 'mxchat'); ?></span>
66 </button>
67 </div>
68 <!-- Settings Section -->
69 <div class="mxch-mobile-nav-section">
70 <div class="mxch-mobile-nav-section-title"><?php esc_html_e('Settings', 'mxchat'); ?></div>
71 <button class="mxch-mobile-nav-link" data-target="auto-sync">
72 <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"><path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/><path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16"/><path d="M16 16h5v5"/></svg>
73 <span><?php esc_html_e('Auto-Sync', 'mxchat'); ?></span>
74 </button>
75 <button class="mxch-mobile-nav-link" data-target="chunking">
76 <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"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
77 <span><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></span>
78 </button>
79 <button class="mxch-mobile-nav-link" data-target="role-restrictions">
80 <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"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
81 <span><?php esc_html_e('Role Restrictions', 'mxchat'); ?></span>
82 </button>
83 <button class="mxch-mobile-nav-link" data-target="acf-fields">
84 <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"><path d="M14.5 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7.5L14.5 2z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><line x1="10" y1="9" x2="8" y2="9"/></svg>
85 <span><?php esc_html_e('ACF Fields', 'mxchat'); ?></span>
86 </button>
87 <button class="mxch-mobile-nav-link" data-target="custom-meta">
88 <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"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M12 4v16"/></svg>
89 <span><?php esc_html_e('Custom Meta', 'mxchat'); ?></span>
90 </button>
91 </div>
92 <!-- Integrations Section -->
93 <div class="mxch-mobile-nav-section">
94 <div class="mxch-mobile-nav-section-title"><?php esc_html_e('Integrations', 'mxchat'); ?></div>
95 <button class="mxch-mobile-nav-link" data-target="pinecone">
96 <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"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></svg>
97 <span><?php esc_html_e('Pinecone', 'mxchat'); ?></span>
98 <?php if ($is_pinecone_active): ?>
99 <span class="mxch-nav-link-badge mxch-active-badge"><?php esc_html_e('Active', 'mxchat'); ?></span>
100 <?php endif; ?>
101 </button>
102 <button class="mxch-mobile-nav-link" data-target="openai-vectorstore">
103 <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"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/><path d="M11 8v6"/><path d="M8 11h6"/></svg>
104 <span><?php esc_html_e('OpenAI Vector Store', 'mxchat'); ?></span>
105 <?php if ($is_vectorstore_active): ?>
106 <span class="mxch-nav-link-badge mxch-active-badge"><?php esc_html_e('Active', 'mxchat'); ?></span>
107 <?php endif; ?>
108 </button>
109 </div>
110 <?php if (!$is_activated): ?>
111 <div class="mxch-mobile-menu-footer">
112 <a href="https://mxchat.ai/" target="_blank" class="mxch-mobile-upgrade-btn">
113 <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"><path d="M6 9H4.5a2.5 2.5 0 0 1 0-5H6"/><path d="M18 9h1.5a2.5 2.5 0 0 0 0-5H18"/><path d="M4 22h16"/><path d="M10 14.66V17c0 .55-.47.98-.97 1.21C7.85 18.75 7 20.24 7 22"/><path d="M14 14.66V17c0 .55.47.98.97 1.21C16.15 18.75 17 20.24 17 22"/><path d="M18 2H6v7a6 6 0 0 0 12 0V2Z"/></svg>
114 <?php esc_html_e('Upgrade to Pro', 'mxchat'); ?>
115 </a>
116 </div>
117 <?php endif; ?>
118 </nav>
119 </div>
120
121 <!-- Sidebar Navigation -->
122 <aside class="mxch-sidebar">
123 <div class="mxch-sidebar-header">
124 <a href="#" class="mxch-sidebar-logo">
125 <div class="mxch-sidebar-logo-icon">
126 <img src="<?php echo esc_url($plugin_url . 'images/icon-128x128.png'); ?>" alt="MxChat">
127 </div>
128 <span class="mxch-sidebar-logo-text">MxChat</span>
129 <span class="mxch-sidebar-version">v<?php echo esc_html(MXCHAT_VERSION ?? '2.7.0'); ?></span>
130 </a>
131 </div>
132
133 <nav class="mxch-sidebar-nav">
134 <!-- Import Section -->
135 <div class="mxch-nav-section">
136 <div class="mxch-nav-section-title"><?php esc_html_e('Import', 'mxchat'); ?></div>
137
138 <div class="mxch-nav-item" data-section="import-options">
139 <button class="mxch-nav-link active" data-target="import-options">
140 <span class="mxch-nav-link-icon">
141 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
142 </span>
143 <span class="mxch-nav-link-text"><?php esc_html_e('Import Options', 'mxchat'); ?></span>
144 </button>
145 </div>
146
147 <div class="mxch-nav-item" data-section="knowledge-base">
148 <button class="mxch-nav-link" data-target="knowledge-base">
149 <span class="mxch-nav-link-icon">
150 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"/><path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"/></svg>
151 </span>
152 <span class="mxch-nav-link-text"><?php esc_html_e('Knowledge Base', 'mxchat'); ?></span>
153 <span id="mxchat-sidebar-count" class="mxch-nav-link-badge"><?php echo esc_html($total_records); ?></span>
154 </button>
155 </div>
156 </div>
157
158 <!-- Settings Section -->
159 <div class="mxch-nav-section">
160 <div class="mxch-nav-section-title"><?php esc_html_e('Settings', 'mxchat'); ?></div>
161
162 <div class="mxch-nav-item" data-section="auto-sync">
163 <button class="mxch-nav-link" data-target="auto-sync">
164 <span class="mxch-nav-link-icon">
165 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/><path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16"/><path d="M16 16h5v5"/></svg>
166 </span>
167 <span class="mxch-nav-link-text"><?php esc_html_e('Auto-Sync', 'mxchat'); ?></span>
168 </button>
169 </div>
170
171 <div class="mxch-nav-item" data-section="chunking">
172 <button class="mxch-nav-link" data-target="chunking">
173 <span class="mxch-nav-link-icon">
174 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
175 </span>
176 <span class="mxch-nav-link-text"><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></span>
177 </button>
178 </div>
179
180 <div class="mxch-nav-item" data-section="role-restrictions">
181 <button class="mxch-nav-link" data-target="role-restrictions">
182 <span class="mxch-nav-link-icon">
183 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
184 </span>
185 <span class="mxch-nav-link-text"><?php esc_html_e('Role Restrictions', 'mxchat'); ?></span>
186 </button>
187 </div>
188
189 <div class="mxch-nav-item" data-section="acf-fields">
190 <button class="mxch-nav-link" data-target="acf-fields">
191 <span class="mxch-nav-link-icon">
192 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14.5 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7.5L14.5 2z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><line x1="10" y1="9" x2="8" y2="9"/></svg>
193 </span>
194 <span class="mxch-nav-link-text"><?php esc_html_e('ACF Fields', 'mxchat'); ?></span>
195 </button>
196 </div>
197
198 <div class="mxch-nav-item" data-section="custom-meta">
199 <button class="mxch-nav-link" data-target="custom-meta">
200 <span class="mxch-nav-link-icon">
201 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M12 4v16"/></svg>
202 </span>
203 <span class="mxch-nav-link-text"><?php esc_html_e('Custom Meta', 'mxchat'); ?></span>
204 </button>
205 </div>
206 </div>
207
208 <!-- Integrations Section -->
209 <div class="mxch-nav-section">
210 <div class="mxch-nav-section-title"><?php esc_html_e('Integrations', 'mxchat'); ?></div>
211
212 <div class="mxch-nav-item" data-section="pinecone">
213 <button class="mxch-nav-link" data-target="pinecone">
214 <span class="mxch-nav-link-icon">
215 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></svg>
216 </span>
217 <span class="mxch-nav-link-text"><?php esc_html_e('Pinecone', 'mxchat'); ?></span>
218 <?php if ($is_pinecone_active): ?>
219 <span class="mxch-nav-link-badge mxch-active-badge"><?php esc_html_e('Active', 'mxchat'); ?></span>
220 <?php endif; ?>
221 </button>
222 </div>
223
224 <div class="mxch-nav-item" data-section="openai-vectorstore">
225 <button class="mxch-nav-link" data-target="openai-vectorstore">
226 <span class="mxch-nav-link-icon">
227 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/><path d="M11 8v6"/><path d="M8 11h6"/></svg>
228 </span>
229 <span class="mxch-nav-link-text"><?php esc_html_e('OpenAI Vector Store', 'mxchat'); ?></span>
230 <?php if ($is_vectorstore_active): ?>
231 <span class="mxch-nav-link-badge mxch-active-badge"><?php esc_html_e('Active', 'mxchat'); ?></span>
232 <?php endif; ?>
233 </button>
234 </div>
235 </div>
236 </nav>
237
238 <?php if (!$is_activated): ?>
239 <div class="mxch-sidebar-footer">
240 <a href="https://mxchat.ai/" target="_blank" class="mxch-sidebar-upgrade-v2">
241 <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"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>
242 <?php esc_html_e('Pro Upgrade', 'mxchat'); ?>
243 </a>
244 </div>
245 <?php endif; ?>
246 </aside>
247
248 <!-- Main Content Area -->
249 <main class="mxch-content">
250 <?php
251 // Multi-Bot Selector
252 mxchat_render_knowledge_bot_selector($multibot_active, $current_bot_id);
253
254 // Global Processing Status - Always visible when processing
255 mxchat_render_global_processing_status($page_data, $knowledge_manager);
256
257 // Render content sections
258 mxchat_render_import_options_section($admin_instance, $knowledge_manager, $page_data);
259 mxchat_render_knowledge_base_section($admin_instance, $knowledge_manager, $page_data);
260 mxchat_render_auto_sync_section($knowledge_manager);
261 mxchat_render_chunking_section();
262 mxchat_render_role_restrictions_section($knowledge_manager);
263 mxchat_render_acf_fields_section($knowledge_manager);
264 mxchat_render_custom_meta_section();
265 mxchat_render_pinecone_section();
266 mxchat_render_openai_vectorstore_section();
267 ?>
268
269 </main>
270 </div>
271
272 <?php
273 // Render the Content Selector Modal
274 mxchat_render_content_selector_modal();
275
276 // Render the Edit Entry Modal
277 mxchat_render_edit_entry_modal();
278
279 // Render the read-only Knowledge Inspector modal (plan-d8cb4b)
280 mxchat_render_inspect_entry_modal();
281
282 // Render the navigation JavaScript
283 mxchat_render_knowledge_page_scripts();
284 }
285
286 /**
287 * Render Multi-Bot Selector
288 */
289 function mxchat_render_knowledge_bot_selector($multibot_active, $current_bot_id) {
290 if (!$multibot_active || !class_exists('MxChat_Multi_Bot_Manager')) {
291 return;
292 }
293
294 $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance();
295 $available_bots = $multi_bot_manager->get_available_bots();
296 ?>
297 <div class="mxch-card" style="margin-bottom: 24px;">
298 <div class="mxch-card-body">
299 <div style="display: flex; align-items: center; gap: 15px; flex-wrap: wrap;">
300 <label for="mxchat-bot-selector" style="font-weight: 600; white-space: nowrap;">
301 <?php esc_html_e('Select Bot Database:', 'mxchat'); ?>
302 </label>
303 <select id="mxchat-bot-selector" class="mxch-select" style="min-width: 200px; max-width: 300px;">
304 <?php foreach ($available_bots as $bot_id => $bot_name) : ?>
305 <option value="<?php echo esc_attr($bot_id); ?>" <?php selected($current_bot_id, $bot_id); ?>>
306 <?php echo esc_html($bot_name); ?>
307 </option>
308 <?php endforeach; ?>
309 </select>
310 <span style="color: var(--mxch-text-secondary); font-size: 13px;">
311 <?php esc_html_e('Content will be added to the selected bot\'s knowledge base', 'mxchat'); ?>
312 </span>
313 <span id="mxchat-bot-save-status" style="display: none; color: var(--mxch-success); font-size: 13px;">
314 <?php esc_html_e('Saved', 'mxchat'); ?>
315 </span>
316 </div>
317 </div>
318 </div>
319 <?php
320 }
321
322 /**
323 * Render Global Processing Status - Always visible when processing is active
324 */
325 function mxchat_render_global_processing_status($page_data, $knowledge_manager) {
326 extract($page_data);
327
328 // Only show if there's active processing
329 if (!$is_processing) {
330 return;
331 }
332 ?>
333 <div class="mxch-processing-status-global mxchat-import-section" style="margin-bottom: 24px;">
334 <?php
335 // PDF Processing Status - Using mxchat-status-card class for JavaScript compatibility
336 if ($pdf_status && $pdf_status['status'] === 'processing') : ?>
337 <div class="mxchat-status-card mxch-card" data-card-type="pdf" data-queue-id="<?php echo esc_attr(get_transient('mxchat_active_queue_pdf')); ?>" style="border-left: 4px solid var(--mxch-primary); margin-bottom: 16px;">
338 <div class="mxchat-status-header mxch-card-header" style="background: var(--mxch-primary-light);">
339 <h4 style="margin: 0; display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600;">
340 <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"><path d="M14.5 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7.5L14.5 2z"/><polyline points="14 2 14 8 20 8"/></svg>
341 <?php esc_html_e('PDF Processing Status', 'mxchat'); ?>
342 </h4>
343 <div class="mxchat-status-warning" style="background: #fff3cd; color: #856404; padding: 8px 12px; border-radius: 4px; font-size: 13px; margin: 10px 0;">
344 <?php esc_html_e('Keep this tab open - Processing runs in your browser', 'mxchat'); ?>
345 </div>
346 <form method="post" class="mxchat-stop-form" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
347 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
348 <button type="submit" name="stop_processing" class="mxchat-button-secondary mxch-btn mxch-btn-secondary mxch-btn-sm">
349 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
350 </button>
351 </form>
352 </div>
353 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin: 0;">
354 <div class="mxchat-progress-fill" style="height: 100%; background: var(--mxch-primary); width: <?php echo esc_attr($pdf_status['percentage']); ?>%; transition: width 0.3s;"></div>
355 </div>
356 <div class="mxchat-status-details mxch-card-body">
357 <p><?php printf(esc_html__('Progress: %1$d of %2$d pages (%3$d%%)', 'mxchat'), absint($pdf_status['processed_pages']), absint($pdf_status['total_pages']), absint($pdf_status['percentage'])); ?></p>
358 <?php if (!empty($pdf_status['failed_pages']) && $pdf_status['failed_pages'] > 0) : ?>
359 <p class="error-count" style="color: var(--mxch-error);"><strong><?php esc_html_e('Failed pages:', 'mxchat'); ?></strong> <?php echo esc_html($pdf_status['failed_pages']); ?></p>
360 <?php endif; ?>
361 <p><strong><?php esc_html_e('Status:', 'mxchat'); ?></strong> <?php esc_html_e('Processing', 'mxchat'); ?></p>
362 </div>
363 </div>
364 <?php endif;
365
366 // Sitemap Processing Status - Using mxchat-status-card class for JavaScript compatibility
367 if ($sitemap_status && $sitemap_status['status'] === 'processing') : ?>
368 <div class="mxchat-status-card mxch-card" data-card-type="sitemap" data-queue-id="<?php echo esc_attr(get_transient('mxchat_active_queue_sitemap')); ?>" style="border-left: 4px solid var(--mxch-primary); margin-bottom: 16px;">
369 <div class="mxchat-status-header mxch-card-header" style="background: var(--mxch-primary-light);">
370 <h4 style="margin: 0; display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600;">
371 <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"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>
372 <?php esc_html_e('Sitemap Processing Status', 'mxchat'); ?>
373 </h4>
374 <div class="mxchat-status-warning" style="background: #fff3cd; color: #856404; padding: 8px 12px; border-radius: 4px; font-size: 13px; margin: 10px 0;">
375 <?php esc_html_e('Keep this tab open - Processing runs in your browser', 'mxchat'); ?>
376 </div>
377 <form method="post" class="mxchat-stop-form" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
378 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
379 <button type="submit" name="stop_processing" class="mxchat-button-secondary mxch-btn mxch-btn-secondary mxch-btn-sm">
380 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
381 </button>
382 </form>
383 </div>
384 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin: 0;">
385 <div class="mxchat-progress-fill" style="height: 100%; background: var(--mxch-primary); width: <?php echo esc_attr($sitemap_status['percentage']); ?>%; transition: width 0.3s;"></div>
386 </div>
387 <div class="mxchat-status-details mxch-card-body">
388 <p><?php printf(esc_html__('Progress: %1$d of %2$d URLs (%3$d%%)', 'mxchat'), absint($sitemap_status['processed_urls']), absint($sitemap_status['total_urls']), absint($sitemap_status['percentage'])); ?></p>
389 <?php if (!empty($sitemap_status['failed_urls']) && $sitemap_status['failed_urls'] > 0) : ?>
390 <p class="error-count" style="color: var(--mxch-error);"><strong><?php esc_html_e('Failed URLs:', 'mxchat'); ?></strong> <?php echo esc_html($sitemap_status['failed_urls']); ?></p>
391 <?php endif; ?>
392 <p><strong><?php esc_html_e('Status:', 'mxchat'); ?></strong> <?php esc_html_e('Processing', 'mxchat'); ?></p>
393 </div>
394 </div>
395 <?php endif;
396
397 // Completed status cards
398 echo $knowledge_manager->mxchat_render_completed_status_cards();
399 ?>
400 </div>
401 <?php
402 }
403
404 /**
405 * Render Import Options Section
406 */
407 function mxchat_render_import_options_section($admin_instance, $knowledge_manager, $page_data) {
408 extract($page_data);
409 $options = $admin_instance->options ?? get_option('mxchat_options', array());
410
411 // Check if user is using WordPress database (not Pinecone or OpenAI Vector Store)
412 $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
413 $is_pinecone_active = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
414 $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
415 $is_vectorstore_active = ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1';
416 $is_using_wordpress_db = !$is_pinecone_active && !$is_vectorstore_active;
417
418 // Check embedding API key
419 $embedding_model = isset($options['embedding_model']) ? esc_attr($options['embedding_model']) : 'text-embedding-ada-002';
420 $has_openai_key = !empty($options['api_key']);
421 $has_voyage_key = !empty($options['voyage_api_key']);
422 $has_gemini_key = !empty($options['gemini_api_key']);
423
424 $has_required_key = false;
425 $required_key_type = '';
426
427 if (strpos($embedding_model, 'text-embedding-') !== false && $has_openai_key) {
428 $has_required_key = true;
429 $required_key_type = 'OpenAI';
430 } elseif (strpos($embedding_model, 'voyage-') !== false && $has_voyage_key) {
431 $has_required_key = true;
432 $required_key_type = 'Voyage AI';
433 } elseif (strpos($embedding_model, 'gemini-embedding-') !== false && $has_gemini_key) {
434 $has_required_key = true;
435 $required_key_type = 'Google Gemini';
436 } elseif (strpos($embedding_model, 'text-embedding-') !== false) {
437 $required_key_type = 'OpenAI';
438 } elseif (strpos($embedding_model, 'voyage-') !== false) {
439 $required_key_type = 'Voyage AI';
440 } elseif (strpos($embedding_model, 'gemini-embedding-') !== false) {
441 $required_key_type = 'Google Gemini';
442 }
443 ?>
444 <div id="import-options" class="mxch-section active">
445 <div class="mxch-content-header">
446 <h1 class="mxch-content-title"><?php esc_html_e('Import Options', 'mxchat'); ?></h1>
447 <p class="mxch-content-subtitle"><?php esc_html_e('Import content to your knowledge base from various sources.', 'mxchat'); ?></p>
448 </div>
449
450 <!-- API Key Status -->
451 <div class="mxch-notice <?php echo $has_required_key ? 'mxch-notice-success' : 'mxch-notice-warning'; ?>" style="margin-bottom: 24px;">
452 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
453 <?php if ($has_required_key): ?>
454 <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/>
455 <?php else: ?>
456 <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/>
457 <?php endif; ?>
458 </svg>
459 <div>
460 <?php if ($has_required_key): ?>
461 <?php echo wp_kses_post(sprintf(__('We detected your %s API key. <strong>Remember to add credits to your %s account</strong> before using the knowledgebase.', 'mxchat'), $required_key_type, $required_key_type)); ?>
462 <?php else: ?>
463 <strong><?php esc_html_e('Important:', 'mxchat'); ?></strong>
464 <?php echo sprintf(esc_html__('Before importing knowledge, you must add a %s API key with sufficient credits in the Chatbot settings.', 'mxchat'), $required_key_type); ?>
465 <a href="<?php echo admin_url('admin.php?page=mxchat-max#api-keys'); ?>"><?php esc_html_e('Go to API Key Settings', 'mxchat'); ?></a>
466 <?php endif; ?>
467 </div>
468 </div>
469
470 <?php if ($is_using_wordpress_db): ?>
471 <!-- WordPress Database Caution Notice -->
472 <div class="mxch-notice mxch-notice-warning" style="margin-bottom: 24px;">
473 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
474 <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/>
475 </svg>
476 <div>
477 <strong><?php esc_html_e('Using WordPress Database:', 'mxchat'); ?></strong>
478 <?php esc_html_e('The WordPress database is not optimized for vector search with large datasets. If you plan to have more than 500 knowledge entries, we highly recommend using Pinecone for better performance and scalability.', 'mxchat'); ?>
479 </div>
480 </div>
481 <?php endif; ?>
482
483 <div class="mxch-card">
484 <div class="mxch-card-header">
485 <h3 class="mxch-card-title">
486 <svg class="mxch-card-title-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
487 <?php esc_html_e('Choose Import Method', 'mxchat'); ?>
488 </h3>
489 </div>
490 <div class="mxch-card-body">
491 <!-- Import Options Grid -->
492 <div class="mxchat-import-options">
493 <!-- WordPress Import Option -->
494 <button type="button" id="mxchat-open-content-selector" class="mxchat-import-box mxchat-import-wordpress" data-option="wordpress">
495 <div class="mxchat-import-icon">
496 <span class="dashicons dashicons-wordpress"></span>
497 </div>
498 <div class="mxchat-import-content">
499 <h4><?php esc_html_e('WordPress Content', 'mxchat'); ?></h4>
500 <p><?php esc_html_e('Import specific posts and pages to your knowledge base.', 'mxchat'); ?></p>
501 </div>
502 <div class="mxchat-recommended-tag"><?php esc_html_e('Recommended', 'mxchat'); ?></div>
503 </button>
504
505 <!-- Sitemap Import Option -->
506 <button type="button" class="mxchat-import-box" data-option="sitemap" data-placeholder="<?php esc_attr_e('Enter sitemap URL here', 'mxchat'); ?>" data-type="sitemap">
507 <div class="mxchat-import-icon">
508 <span class="dashicons dashicons-admin-site-alt"></span>
509 </div>
510 <div class="mxchat-import-content">
511 <h4><?php esc_html_e('Sitemap Import', 'mxchat'); ?></h4>
512 <p><?php esc_html_e('Use a content-specific sub-sitemap, not the sitemap index.', 'mxchat'); ?></p>
513 </div>
514 </button>
515
516 <!-- Direct URL Import Option -->
517 <button type="button" class="mxchat-import-box" data-option="url" data-placeholder="<?php esc_attr_e('Enter webpage URL here', 'mxchat'); ?>" data-type="url">
518 <div class="mxchat-import-icon">
519 <span class="dashicons dashicons-admin-links"></span>
520 </div>
521 <div class="mxchat-import-content">
522 <h4><?php esc_html_e('Direct URL', 'mxchat'); ?></h4>
523 <p><?php esc_html_e('Import content from any webpage.', 'mxchat'); ?></p>
524 </div>
525 </button>
526
527 <!-- Direct Content Import Option -->
528 <button type="button" class="mxchat-import-box" data-option="content" data-type="content">
529 <div class="mxchat-import-icon">
530 <span class="dashicons dashicons-editor-paste-text"></span>
531 </div>
532 <div class="mxchat-import-content">
533 <h4><?php esc_html_e('Direct Content', 'mxchat'); ?></h4>
534 <p><?php esc_html_e('Submit content to be vectorized.', 'mxchat'); ?></p>
535 </div>
536 </button>
537
538 <!-- PDF Import Option (URL) -->
539 <button type="button" class="mxchat-import-box" data-option="pdf-url" data-placeholder="<?php esc_attr_e('Enter PDF URL here', 'mxchat'); ?>" data-type="pdf">
540 <div class="mxchat-import-icon">
541 <span class="dashicons dashicons-media-document"></span>
542 </div>
543 <div class="mxchat-import-content">
544 <h4><?php esc_html_e('PDF Import', 'mxchat'); ?></h4>
545 <p><?php esc_html_e('Import knowledge from PDF URL.', 'mxchat'); ?></p>
546 </div>
547 </button>
548
549 <!-- PDF File Upload Option -->
550 <button type="button" class="mxchat-import-box" data-option="pdf-upload" data-type="pdf-upload">
551 <div class="mxchat-import-icon">
552 <span class="dashicons dashicons-upload"></span>
553 </div>
554 <div class="mxchat-import-content">
555 <h4><?php esc_html_e('PDF Upload', 'mxchat'); ?></h4>
556 <p><?php esc_html_e('Upload a PDF file from your computer.', 'mxchat'); ?></p>
557 </div>
558 </button>
559
560 <!-- YouTube Video Import Option -->
561 <button type="button" class="mxchat-import-box" data-option="youtube" data-type="youtube">
562 <div class="mxchat-import-icon">
563 <span class="dashicons dashicons-youtube"></span>
564 </div>
565 <div class="mxchat-import-content">
566 <h4><?php esc_html_e('YouTube', 'mxchat'); ?></h4>
567 <p><?php esc_html_e('Index a video so the chat can surface it as a playable embed.', 'mxchat'); ?></p>
568 </div>
569 </button>
570 </div>
571
572 <!-- Detected Sitemaps Section (hidden by default, shown when Sitemap Import is clicked) -->
573 <div id="mxchat-detected-sitemaps" class="mxchat-detected-sitemaps" style="display: none;">
574 <div class="mxchat-sitemaps-header">
575 <h4>
576 <span class="dashicons dashicons-admin-site-alt"></span>
577 <?php esc_html_e('Detected Sitemaps', 'mxchat'); ?>
578 </h4>
579 <button type="button" id="mxchat-refresh-sitemaps" class="mxch-btn mxch-btn-ghost mxch-btn-sm" title="<?php esc_attr_e('Refresh', 'mxchat'); ?>">
580 <span class="dashicons dashicons-update"></span>
581 </button>
582 </div>
583 <div id="mxchat-sitemaps-list" class="mxchat-sitemaps-list">
584 <!-- Sitemaps will be loaded here via AJAX -->
585 </div>
586 <input type="hidden" id="mxchat-detect-sitemaps-nonce" value="<?php echo wp_create_nonce('mxchat_detect_sitemaps_nonce'); ?>">
587 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
588 <input type="hidden" id="mxchat-sitemap-bot-id" value="<?php echo esc_attr($current_bot_id); ?>">
589 <?php endif; ?>
590 </div>
591
592 <!-- No Sitemaps Found Message (hidden by default) -->
593 <div id="mxchat-no-sitemaps" class="mxchat-no-sitemaps" style="display: none;">
594 <span class="dashicons dashicons-info"></span>
595 <p>
596 <?php esc_html_e('No sitemaps detected. You can manually enter a sitemap URL using the input field above.', 'mxchat'); ?>
597 </p>
598 </div>
599
600 <!-- Sitemaps Loading State (hidden by default) -->
601 <div id="mxchat-sitemaps-loading" class="mxchat-sitemaps-loading" style="display: none;">
602 <span class="dashicons dashicons-update spin"></span>
603 <p><?php esc_html_e('Detecting sitemaps...', 'mxchat'); ?></p>
604 </div>
605
606 <!-- Input Areas for URL and Content -->
607 <?php if (!$is_processing) : ?>
608 <div class="mxchat-import-input-area" id="mxchat-url-input-area" style="display: none; margin-top: 20px;">
609 <form id="mxchat-url-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_sitemap')); ?>">
610 <?php wp_nonce_field('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce'); ?>
611 <input type="hidden" name="import_type" id="import_type" value="url">
612 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
613 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
614 <?php endif; ?>
615 <div style="display: flex; gap: 10px;">
616 <input type="url" name="sitemap_url" id="sitemap_url" class="mxch-input" placeholder="<?php esc_attr_e('Enter URL here', 'mxchat'); ?>" required style="flex: 1;" />
617 <button type="submit" name="submit_sitemap" class="mxch-btn mxch-btn-primary">
618 <?php esc_html_e('Import', 'mxchat'); ?>
619 </button>
620 </div>
621 <p class="mxch-field-description" id="url-description-text"></p>
622 </form>
623 </div>
624
625 <div class="mxchat-import-input-area" id="mxchat-content-input-area" style="display: none; margin-top: 20px;">
626 <form id="mxchat-content-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_content')); ?>">
627 <?php wp_nonce_field('mxchat_submit_content_action', 'mxchat_submit_content_nonce'); ?>
628 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
629 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
630 <?php endif; ?>
631 <div class="mxch-field">
632 <textarea name="article_content" id="article_content" class="mxch-textarea" placeholder="<?php esc_attr_e('Enter your content here...', 'mxchat'); ?>" required rows="6"></textarea>
633 </div>
634 <div class="mxch-field">
635 <input type="url" name="article_url" id="article_url" class="mxch-input" placeholder="<?php esc_attr_e('Enter source URL (Optional)', 'mxchat'); ?>">
636 <div style="margin-top: 8px; display: flex; align-items: center; gap: 8px;">
637 <label style="display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--mxch-text-secondary); cursor: pointer;">
638 <input type="checkbox" id="mxchat-unique-url-toggle" style="margin: 0;">
639 <?php esc_html_e('Generate unique URL for duplicate content', 'mxchat'); ?>
640 </label>
641 <button type="button" id="mxchat-generate-unique-url" class="mxch-btn mxch-btn-secondary mxch-btn-sm" style="display: none;">
642 <span class="dashicons dashicons-randomize" style="font-size: 14px; margin-top: 2px;"></span>
643 <?php esc_html_e('Generate Unique', 'mxchat'); ?>
644 </button>
645 </div>
646 <p class="mxch-field-description">
647 <?php esc_html_e('Enable this option to submit multiple entries with the same base URL. A unique reference will be appended.', 'mxchat'); ?>
648 </p>
649 </div>
650 <button type="submit" name="submit_content" class="mxch-btn mxch-btn-primary">
651 <?php esc_html_e('Import Content', 'mxchat'); ?>
652 </button>
653 </form>
654 </div>
655
656 <div class="mxchat-import-input-area" id="mxchat-pdf-upload-area" style="display: none; margin-top: 20px;">
657 <form id="mxchat-pdf-upload-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_pdf_file')); ?>" enctype="multipart/form-data">
658 <?php wp_nonce_field('mxchat_submit_pdf_file_action', 'mxchat_submit_pdf_file_nonce'); ?>
659 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
660 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
661 <?php endif; ?>
662 <div class="mxch-field">
663 <input type="file" name="pdf_file" id="mxchat-pdf-file-input" accept=".pdf" required>
664 </div>
665 <p class="mxch-field-description">
666 <?php esc_html_e('Select a PDF file from your computer to import into the knowledge base. Maximum file size depends on your server settings.', 'mxchat'); ?>
667 </p>
668 <button type="submit" name="submit_pdf_file" class="mxch-btn mxch-btn-primary">
669 <?php esc_html_e('Import PDF', 'mxchat'); ?>
670 </button>
671 </form>
672 </div>
673
674 <?php
675 // Prefill state: set when an auto-import found no transcript — the
676 // handler bounced back here so the admin can augment the entry with
677 // a manual description (re-importing the same URL updates the row).
678 $yt_prefill = isset($_GET['mxchat_yt_prefill']) && $_GET['mxchat_yt_prefill'] === '1';
679 $yt_prefill_url = $yt_prefill && isset($_GET['yt_url']) ? esc_url_raw(rawurldecode(wp_unslash($_GET['yt_url']))) : '';
680 $yt_prefill_title = $yt_prefill && isset($_GET['yt_title']) ? sanitize_text_field(rawurldecode(wp_unslash($_GET['yt_title']))) : '';
681 ?>
682 <div class="mxchat-import-input-area" id="mxchat-youtube-input-area" style="display: none; margin-top: 20px;">
683 <form id="mxchat-youtube-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_youtube')); ?>"<?php echo $yt_prefill ? ' data-yt-prefill="1"' : ''; ?>>
684 <?php wp_nonce_field('mxchat_submit_youtube_action', 'mxchat_submit_youtube_nonce'); ?>
685 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
686 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
687 <?php endif; ?>
688 <div class="mxch-field">
689 <input type="url" name="youtube_url" id="mxchat-youtube-url" class="mxch-input" placeholder="<?php esc_attr_e('Paste a YouTube video URL (watch, youtu.be, or Shorts link)', 'mxchat'); ?>" required value="<?php echo esc_attr($yt_prefill_url); ?>">
690 </div>
691 <div class="mxch-field">
692 <span class="mxch-field-label"><?php esc_html_e('How should this video be described for matching?', 'mxchat'); ?></span>
693 <div style="display: flex; flex-direction: column; gap: 6px; margin-top: 8px;">
694 <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
695 <input type="radio" name="youtube_description_mode" value="auto" <?php checked(!$yt_prefill); ?>>
696 <span>
697 <strong><?php esc_html_e('Auto-fetch', 'mxchat'); ?></strong>
698 <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('We pull the video\'s title, channel, and captions (when available) to describe it.', 'mxchat'); ?></span>
699 </span>
700 </label>
701 <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
702 <input type="radio" name="youtube_description_mode" value="manual" <?php checked($yt_prefill); ?>>
703 <span>
704 <strong><?php esc_html_e('Write my own', 'mxchat'); ?></strong>
705 <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('You write the description the chatbot matches against — best for curated guides.', 'mxchat'); ?></span>
706 </span>
707 </label>
708 </div>
709 </div>
710 <div class="mxch-field" id="mxchat-youtube-description-field" style="<?php echo $yt_prefill ? '' : 'display: none;'; ?>">
711 <textarea name="youtube_description" id="mxchat-youtube-description" class="mxch-textarea" rows="5" placeholder="<?php esc_attr_e('Describe what this video covers and the questions it answers...', 'mxchat'); ?>"><?php echo esc_textarea($yt_prefill_title !== '' ? $yt_prefill_title . "\n\n" : ''); ?></textarea>
712 <p class="mxch-field-description"><?php esc_html_e('This text is what gets matched against visitor questions. Importing the same video URL again updates its existing entry.', 'mxchat'); ?></p>
713 </div>
714 <button type="submit" name="submit_youtube" value="1" class="mxch-btn mxch-btn-primary">
715 <?php esc_html_e('Import Video', 'mxchat'); ?>
716 </button>
717 </form>
718 </div>
719 <?php endif; ?>
720 </div>
721 </div>
722 </div>
723 <?php
724 }
725
726 /**
727 * Render Processing Status Cards (legacy - kept for reference)
728 * Note: Processing status is now rendered globally via mxchat_render_global_processing_status()
729 */
730 function mxchat_render_processing_status($page_data, $knowledge_manager) {
731 extract($page_data);
732
733 // PDF Processing Status
734 if ($pdf_status && $pdf_status['status'] === 'processing') : ?>
735 <div class="mxch-card" style="margin-top: 20px;">
736 <div class="mxch-card-header" style="background: var(--mxch-primary-light);">
737 <h3 class="mxch-card-title"><?php esc_html_e('PDF Processing Status', 'mxchat'); ?></h3>
738 <div style="display: flex; gap: 10px;">
739 <form method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
740 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
741 <button type="submit" name="stop_processing" class="mxch-btn mxch-btn-secondary mxch-btn-sm">
742 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
743 </button>
744 </form>
745 <button type="button" class="mxch-btn mxch-btn-primary mxch-btn-sm mxchat-manual-batch-btn" data-process-type="pdf" data-url="<?php echo esc_attr(get_transient('mxchat_last_pdf_url')); ?>">
746 <?php esc_html_e('Process Batch', 'mxchat'); ?>
747 </button>
748 </div>
749 </div>
750 <div class="mxch-card-body">
751 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin-bottom: 15px;">
752 <div class="mxchat-progress-fill" style="height: 100%; background: var(--mxch-primary); width: <?php echo esc_attr($pdf_status['percentage']); ?>%; transition: width 0.3s;"></div>
753 </div>
754 <p><?php printf(esc_html__('Progress: %1$d of %2$d pages (%3$d%%)', 'mxchat'), absint($pdf_status['processed_pages']), absint($pdf_status['total_pages']), absint($pdf_status['percentage'])); ?></p>
755 <?php if (!empty($pdf_status['failed_pages']) && $pdf_status['failed_pages'] > 0) : ?>
756 <p><strong><?php esc_html_e('Failed pages:', 'mxchat'); ?></strong> <?php echo esc_html($pdf_status['failed_pages']); ?></p>
757 <?php endif; ?>
758 <p><strong><?php esc_html_e('Last update:', 'mxchat'); ?></strong> <?php echo esc_html($pdf_status['last_update']); ?></p>
759 </div>
760 </div>
761 <?php endif;
762
763 // Sitemap Processing Status
764 if ($sitemap_status && $sitemap_status['status'] === 'processing') : ?>
765 <div class="mxch-card" style="margin-top: 20px;">
766 <div class="mxch-card-header" style="background: var(--mxch-primary-light);">
767 <h3 class="mxch-card-title"><?php esc_html_e('Sitemap Processing Status', 'mxchat'); ?></h3>
768 <div style="display: flex; gap: 10px;">
769 <form method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
770 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
771 <button type="submit" name="stop_processing" class="mxch-btn mxch-btn-secondary mxch-btn-sm">
772 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
773 </button>
774 </form>
775 <button type="button" class="mxch-btn mxch-btn-primary mxch-btn-sm mxchat-manual-batch-btn" data-process-type="sitemap" data-url="<?php echo esc_attr(get_transient('mxchat_last_sitemap_url')); ?>">
776 <?php esc_html_e('Process Batch', 'mxchat'); ?>
777 </button>
778 </div>
779 </div>
780 <div class="mxch-card-body">
781 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin-bottom: 15px;">
782 <div class="mxchat-progress-fill" style="height: 100%; background: var(--mxch-primary); width: <?php echo esc_attr($sitemap_status['percentage']); ?>%; transition: width 0.3s;"></div>
783 </div>
784 <p><?php printf(esc_html__('Progress: %1$d of %2$d URLs (%3$d%%)', 'mxchat'), absint($sitemap_status['processed_urls']), absint($sitemap_status['total_urls']), absint($sitemap_status['percentage'])); ?></p>
785 <?php if (!empty($sitemap_status['failed_urls']) && $sitemap_status['failed_urls'] > 0) : ?>
786 <p><strong><?php esc_html_e('Failed URLs:', 'mxchat'); ?></strong> <?php echo esc_html($sitemap_status['failed_urls']); ?></p>
787 <?php endif; ?>
788 <p><strong><?php esc_html_e('Last update:', 'mxchat'); ?></strong> <?php echo esc_html($sitemap_status['last_update']); ?></p>
789 </div>
790 </div>
791 <?php endif;
792
793 // Completed status cards
794 echo $knowledge_manager->mxchat_render_completed_status_cards();
795 }
796
797 /**
798 * Render Knowledge Base Section (table)
799 */
800 function mxchat_render_knowledge_base_section($admin_instance, $knowledge_manager, $page_data) {
801 extract($page_data);
802
803 // Group prompts by source_url for chunked content display
804 $grouped_prompts = array();
805
806 if ($prompts) {
807 foreach ($prompts as $prompt) {
808 $source_url = $prompt->source_url ?? '';
809
810 // Check if chunk metadata exists on the record itself (Pinecone)
811 if (isset($prompt->chunk_index) && $prompt->chunk_index !== null) {
812 // Pinecone: metadata is already on the record object
813 $prompt->chunk_metadata = array(
814 'chunk_index' => intval($prompt->chunk_index),
815 'total_chunks' => isset($prompt->total_chunks) ? intval($prompt->total_chunks) : null,
816 'is_chunked' => isset($prompt->is_chunked) ? (bool) $prompt->is_chunked : true
817 );
818 $prompt->display_content = $prompt->article_content; // Pinecone content is already clean
819 } else {
820 // WordPress: Parse chunk metadata from JSON prefix in article_content
821 if (class_exists('MxChat_Chunker')) {
822 $chunk_meta = MxChat_Chunker::parse_stored_chunk($prompt->article_content);
823 $prompt->chunk_metadata = $chunk_meta['metadata'];
824 $prompt->display_content = $chunk_meta['text']; // Store clean content without JSON prefix
825 } else {
826 $prompt->chunk_metadata = array();
827 $prompt->display_content = $prompt->article_content;
828 }
829 }
830
831 if (!empty($source_url)) {
832 if (!isset($grouped_prompts[$source_url])) {
833 $grouped_prompts[$source_url] = array();
834 }
835 $grouped_prompts[$source_url][] = $prompt;
836 } else {
837 // Use a unique key for ungrouped prompts
838 $grouped_prompts['_ungrouped_' . $prompt->id] = array($prompt);
839 }
840 }
841
842 // Sort each group by chunk_index from metadata
843 foreach ($grouped_prompts as $source_url => &$group) {
844 usort($group, function($a, $b) {
845 $index_a = isset($a->chunk_metadata['chunk_index']) ? intval($a->chunk_metadata['chunk_index']) : 0;
846 $index_b = isset($b->chunk_metadata['chunk_index']) ? intval($b->chunk_metadata['chunk_index']) : 0;
847 return $index_a - $index_b;
848 });
849 }
850 unset($group); // Break reference
851 }
852 $display_index = 0;
853 ?>
854 <div id="knowledge-base" class="mxch-section">
855 <div class="mxch-content-header">
856 <h1 class="mxch-content-title"><?php esc_html_e('Knowledge Base', 'mxchat'); ?></h1>
857 <p class="mxch-content-subtitle"><?php esc_html_e('View and manage your imported knowledge entries.', 'mxchat'); ?></p>
858 </div>
859
860 <?php if (!empty($use_vectorstore)) : ?>
861 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
862 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
863 <span><strong><?php esc_html_e('OpenAI Vector Store is Active', 'mxchat'); ?></strong> - <?php esc_html_e('Your chatbot is using OpenAI\'s hosted Vector Store for knowledge retrieval. The entries below are stored locally and are not being searched. To use this local database, disable Vector Store in the Integrations settings.', 'mxchat'); ?></span>
864 </div>
865 <?php endif; ?>
866
867 <div class="mxch-card">
868 <div class="mxch-card-header">
869 <h3 class="mxch-card-title">
870 <?php esc_html_e('Knowledge Entries', 'mxchat'); ?>
871 <span id="mxchat-entry-count" style="font-weight: normal; color: var(--mxch-text-secondary);">(<?php echo esc_html($total_records); ?>)</span>
872 <?php if (!empty($use_vectorstore)) : ?>
873 <span style="display: inline-flex; align-items: center; gap: 4px; padding: 4px 8px; border-radius: 12px; font-size: 12px; font-weight: 500; margin-left: 8px; background: #fff3e0; color: #e65100;">
874 <span class="dashicons dashicons-database" style="font-size: 14px;"></span>
875 <?php esc_html_e('Not in use', 'mxchat'); ?>
876 </span>
877 <?php elseif ($use_pinecone) : ?>
878 <span style="display: inline-flex; align-items: center; gap: 4px; padding: 4px 8px; border-radius: 12px; font-size: 12px; font-weight: 500; margin-left: 8px; background: #e3f2fd; color: #1976d2;">
879 <span class="dashicons dashicons-cloud" style="font-size: 14px;"></span>
880 <?php esc_html_e('Pinecone', 'mxchat'); ?>
881 </span>
882 <?php else : ?>
883 <span style="display: inline-flex; align-items: center; gap: 4px; padding: 4px 8px; border-radius: 12px; font-size: 12px; font-weight: 500; margin-left: 8px; background: #f3e5f5; color: #7b1fa2;">
884 <span class="dashicons dashicons-database-view" style="font-size: 14px;"></span>
885 <?php esc_html_e('WordPress DB', 'mxchat'); ?>
886 </span>
887 <?php endif; ?>
888 </h3>
889 <div class="mxch-kb-header-controls">
890 <form method="get" id="knowledge-search" class="mxch-kb-search-form">
891 <?php wp_nonce_field('mxchat_prompts_search_nonce'); ?>
892 <input type="hidden" name="page" value="mxchat-prompts" />
893 <?php if ($multibot_active && !empty($current_bot_id) && $current_bot_id !== 'default') : ?>
894 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>" />
895 <?php endif; ?>
896 <input type="text" name="search" class="mxch-input mxch-input-sm mxch-kb-search-input" placeholder="<?php esc_attr_e('Search...', 'mxchat'); ?>" value="<?php echo esc_attr($search_query); ?>" />
897 <select name="content_type" class="mxch-select mxch-kb-type-filter" onchange="this.form.submit()">
898 <option value=""><?php esc_html_e('All Types', 'mxchat'); ?></option>
899 <?php
900 $post_types = get_post_types(array('public' => true), 'objects');
901 foreach ($post_types as $post_type) {
902 echo '<option value="' . esc_attr($post_type->name) . '" ' . selected($content_type_filter, $post_type->name, false) . '>' . esc_html($post_type->label) . '</option>';
903 }
904 ?>
905 <option value="pdf" <?php selected($content_type_filter, 'pdf'); ?>><?php esc_html_e('PDFs', 'mxchat'); ?></option>
906 <option value="url" <?php selected($content_type_filter, 'url'); ?>><?php esc_html_e('URLs', 'mxchat'); ?></option>
907 </select>
908 </form>
909 <!-- Delete Button Container - transforms between Delete All and Delete Selected -->
910 <div class="mxchat-delete-container" id="mxchat-delete-container">
911 <!-- Delete All Form (shown when nothing selected) -->
912 <?php
913 // Build confirm message and button text based on active filter
914 $delete_all_label = __('Delete All', 'mxchat');
915 $delete_all_confirm = __('Are you sure you want to delete all knowledge?', 'mxchat');
916 if (!empty($content_type_filter)) {
917 $filter_display = $content_type_filter;
918 // Try to get a human-readable label
919 $pt_obj = get_post_type_object($content_type_filter);
920 if ($pt_obj) {
921 $filter_display = $pt_obj->label;
922 } elseif ($content_type_filter === 'pdf') {
923 $filter_display = 'PDFs';
924 } elseif ($content_type_filter === 'url') {
925 $filter_display = 'URLs';
926 }
927 /* translators: %s: content type label (e.g. "Pages", "PDFs") */
928 $delete_all_label = sprintf(__('Delete All %s', 'mxchat'), $filter_display);
929 /* translators: %s: content type label */
930 $delete_all_confirm = sprintf(__('Are you sure you want to delete all %s from the knowledge base?', 'mxchat'), $filter_display);
931 }
932 ?>
933 <form method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_delete_all_prompts')); ?>" id="mxchat-delete-all-form" onsubmit="return confirm('<?php echo esc_attr($delete_all_confirm); ?>');">
934 <?php wp_nonce_field('mxchat_delete_all_prompts_action', 'mxchat_delete_all_prompts_nonce'); ?>
935 <input type="hidden" name="data_source" value="<?php echo esc_attr($data_source); ?>" />
936 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>" />
937 <?php if (!empty($content_type_filter)) : ?>
938 <input type="hidden" name="content_type_filter" value="<?php echo esc_attr($content_type_filter); ?>" />
939 <?php endif; ?>
940 <button type="submit" class="mxch-btn mxch-btn-secondary mxch-btn-sm" style="color: var(--mxch-error);">
941 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
942 <?php echo esc_html($delete_all_label); ?>
943 </button>
944 </form>
945 <!-- Delete Selected Button (shown when items selected) -->
946 <button type="button"
947 class="mxch-btn mxch-btn-secondary mxch-btn-sm mxchat-bulk-delete"
948 id="mxchat-delete-selected-entries"
949 style="display: none; color: var(--mxch-error);"
950 data-nonce="<?php echo wp_create_nonce('mxchat_bulk_delete_knowledge_nonce'); ?>"
951 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
952 data-data-source="<?php echo esc_attr($data_source); ?>">
953 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
954 <span class="mxchat-bulk-delete-text"><?php esc_html_e('Delete Selected', 'mxchat'); ?></span>
955 <span class="mxchat-selected-count" id="mxchat-selected-entry-count"></span>
956 </button>
957 </div>
958 </div>
959 </div>
960 <div class="mxch-card-body" style="padding: 0;">
961 <?php if ($use_pinecone) : ?>
962 <div class="mxch-notice mxch-notice-info mxch-pinecone-notice" style="margin: 16px; border-radius: var(--mxch-radius-md); display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px;">
963 <div style="display: flex; align-items: center; gap: 8px;">
964 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
965 <span><?php esc_html_e('Changes may take up to 60 seconds to process in Pinecone. Click "Refresh Entries" to see updates.', 'mxchat'); ?></span>
966 </div>
967 <button type="button" id="mxchat-refresh-pinecone-entries" class="mxch-btn mxch-btn-secondary mxch-btn-sm">
968 <span class="dashicons dashicons-update"></span>
969 <?php esc_html_e('Refresh Entries', 'mxchat'); ?>
970 </button>
971 </div>
972 <?php endif; ?>
973
974 <div class="mxchat-table-wrapper" style="overflow-x: auto;">
975 <table class="mxchat-records-table" id="mxchat-records-table" style="width: 100%; border-collapse: collapse;">
976 <thead>
977 <tr style="background: #f8fafc; border-bottom: 1px solid var(--mxch-card-border);">
978 <th style="padding: 12px 16px; width: 40px; text-align: center;">
979 <input type="checkbox" id="mxchat-select-all-entries" class="mxchat-entry-checkbox-all" title="<?php esc_attr_e('Select All', 'mxchat'); ?>">
980 </th>
981 <th style="padding: 12px 16px; text-align: left; font-weight: 600; color: var(--mxch-text-secondary); font-size: 12px; text-transform: uppercase;"><?php esc_html_e('ID', 'mxchat'); ?></th>
982 <th style="padding: 12px 16px; text-align: left; font-weight: 600; color: var(--mxch-text-secondary); font-size: 12px; text-transform: uppercase;"><?php esc_html_e('Content', 'mxchat'); ?></th>
983 <th style="padding: 12px 16px; text-align: left; font-weight: 600; color: var(--mxch-text-secondary); font-size: 12px; text-transform: uppercase;"><?php esc_html_e('Source', 'mxchat'); ?></th>
984 <th style="padding: 12px 16px; text-align: left; font-weight: 600; color: var(--mxch-text-secondary); font-size: 12px; text-transform: uppercase;"><?php esc_html_e('Actions', 'mxchat'); ?></th>
985 </tr>
986 </thead>
987 <tbody id="mxchat-entries-tbody">
988 <?php if (empty($grouped_prompts)) : ?>
989 <tr>
990 <td colspan="5" style="padding: 40px; text-align: center; color: var(--mxch-text-muted);">
991 <?php esc_html_e('No knowledge entries found. Use the Import Options to add content.', 'mxchat'); ?>
992 </td>
993 </tr>
994 <?php else : ?>
995 <?php
996 // Display grouped prompts
997 foreach ($grouped_prompts as $source_url => $group) :
998 $chunk_count = count($group);
999 $first_prompt = $group[0];
1000 $display_index++;
1001
1002 if ($chunk_count > 1) :
1003 // Multiple chunks - show grouped row with expand button
1004 $group_id = 'group-' . md5($source_url);
1005 ?>
1006 <tr id="prompt-<?php echo esc_attr($first_prompt->id); ?>"
1007 class="mxchat-chunk-group-header"
1008 data-source="<?php echo esc_attr($data_source); ?>"
1009 data-group-id="<?php echo esc_attr($group_id); ?>"
1010 style="border-bottom: 1px solid var(--mxch-card-border); <?php if ($data_source === 'pinecone') echo 'background: rgba(33, 150, 243, 0.02);'; ?>">
1011 <td style="padding: 12px 16px; text-align: center;">
1012 <input type="checkbox"
1013 class="mxchat-entry-checkbox"
1014 data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
1015 data-source="<?php echo esc_attr($data_source); ?>"
1016 data-source-url="<?php echo esc_attr($source_url); ?>"
1017 data-is-group="true"
1018 data-chunk-count="<?php echo esc_attr($chunk_count); ?>">
1019 </td>
1020 <td style="padding: 12px 16px; font-size: 13px;">
1021 <?php if ($data_source === 'pinecone') : ?>
1022 <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
1023 <?php else : ?>
1024 <?php echo esc_html($first_prompt->id); ?>
1025 <?php endif; ?>
1026 </td>
1027 <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
1028 <div class="mxchat-chunk-group-info">
1029 <button type="button" class="mxchat-chunk-toggle" data-group-id="<?php echo esc_attr($group_id); ?>">
1030 <span class="dashicons dashicons-arrow-right-alt2"></span>
1031 </button>
1032 <span class="mxchat-chunk-badge"><?php echo esc_html($chunk_count); ?> <?php esc_html_e('chunks', 'mxchat'); ?></span>
1033 <span class="mxchat-chunk-preview">
1034 <?php
1035 $parent_content = isset($first_prompt->display_content) ? $first_prompt->display_content : $first_prompt->article_content;
1036 $content_preview = mb_substr($parent_content, 0, 100);
1037 echo esc_html($content_preview . '...');
1038 ?>
1039 </span>
1040 </div>
1041 </td>
1042 <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
1043 <?php if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0 && strpos($source_url, '_ungrouped_') !== 0) : ?>
1044 <a href="<?php echo esc_url($source_url); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
1045 <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
1046 <?php esc_html_e('View Source', 'mxchat'); ?>
1047 </a>
1048 <?php else : ?>
1049 <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
1050 <?php endif; ?>
1051 </td>
1052 <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
1053 <button type="button"
1054 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
1055 data-source-url="<?php echo esc_attr($source_url); ?>"
1056 data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
1057 data-data-source="<?php echo esc_attr($data_source); ?>"
1058 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1059 data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
1060 title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
1061 <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
1062 </button>
1063 <?php if ($data_source !== 'pinecone') : ?>
1064 <button type="button"
1065 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
1066 data-source-url="<?php echo esc_attr($source_url); ?>"
1067 data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
1068 data-data-source="<?php echo esc_attr($data_source); ?>"
1069 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1070 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1071 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1072 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1073 </button>
1074 <?php endif; ?>
1075 <button type="button"
1076 class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
1077 data-source-url="<?php echo esc_attr($source_url); ?>"
1078 data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
1079 data-data-source="<?php echo esc_attr($data_source); ?>"
1080 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1081 data-nonce="<?php echo wp_create_nonce('mxchat_delete_chunks_nonce'); ?>"
1082 style="color: var(--mxch-error);"
1083 title="<?php esc_attr_e('Delete all chunks', 'mxchat'); ?>">
1084 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1085 </button>
1086 </td>
1087 </tr>
1088 <?php
1089 // Render hidden chunk rows
1090 foreach ($group as $chunk_index => $prompt) :
1091 $meta_chunk_index = isset($prompt->chunk_metadata['chunk_index']) ? intval($prompt->chunk_metadata['chunk_index']) : $chunk_index;
1092 $meta_total_chunks = isset($prompt->chunk_metadata['total_chunks']) ? intval($prompt->chunk_metadata['total_chunks']) : $chunk_count;
1093 $content = isset($prompt->display_content) ? $prompt->display_content : $prompt->article_content;
1094 $preview_length = 150;
1095 $content_preview = mb_strlen($content) > $preview_length
1096 ? mb_substr($content, 0, $preview_length) . '...'
1097 : $content;
1098 ?>
1099 <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
1100 class="mxchat-chunk-row <?php echo esc_attr($group_id); ?>"
1101 data-source="<?php echo esc_attr($data_source); ?>"
1102 style="display: none; background: #f8f9fa; border-bottom: 1px solid var(--mxch-card-border);">
1103 <td style="padding: 12px 16px; text-align: center;">
1104 <!-- Checkbox placeholder for chunk rows -->
1105 </td>
1106 <td style="padding: 12px 16px 12px 30px; font-size: 13px;">
1107 <!-- Hidden ID column for chunks -->
1108 </td>
1109 <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
1110 <div class="mxchat-accordion-wrapper">
1111 <div class="mxchat-content-preview">
1112 <span class="mxchat-chunk-indicator" style="margin-right: 10px; color: var(--mxch-text-secondary); font-size: 12px;">
1113 <?php printf(esc_html__('Chunk %d of %d', 'mxchat'), $meta_chunk_index + 1, $meta_total_chunks); ?>
1114 </span>
1115 <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
1116 <?php if (mb_strlen($content) > $preview_length) : ?>
1117 <button class="mxchat-expand-toggle" type="button">
1118 <span class="dashicons dashicons-arrow-down-alt2"></span>
1119 </button>
1120 <?php endif; ?>
1121 </div>
1122 <div class="mxchat-content-full" style="display: none;">
1123 <div class="content-view">
1124 <?php
1125 if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
1126 echo '<div dir="rtl" lang="he" class="rtl-content">';
1127 echo wp_kses_post(wpautop($content));
1128 echo '</div>';
1129 } else {
1130 echo wp_kses_post(wpautop($content));
1131 }
1132 ?>
1133 </div>
1134 </div>
1135 </div>
1136 </td>
1137 <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
1138 <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Same as parent', 'mxchat'); ?></span>
1139 </td>
1140 <td class="mxchat-actions-cell" style="padding: 12px 16px;">
1141 <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Managed by group', 'mxchat'); ?></span>
1142 </td>
1143 </tr>
1144 <?php endforeach; ?>
1145 <?php else :
1146 // Single entry - display normally with accordion
1147 $prompt = $first_prompt;
1148 $content = isset($prompt->display_content) ? $prompt->display_content : $prompt->article_content;
1149 $preview_length = 150;
1150 $content_preview = mb_strlen($content) > $preview_length
1151 ? mb_substr($content, 0, $preview_length) . '...'
1152 : $content;
1153 ?>
1154 <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
1155 data-source="<?php echo esc_attr($data_source); ?>"
1156 style="border-bottom: 1px solid var(--mxch-card-border); <?php if ($data_source === 'pinecone') echo 'background: rgba(33, 150, 243, 0.02);'; ?>">
1157 <td style="padding: 12px 16px; text-align: center;">
1158 <input type="checkbox"
1159 class="mxchat-entry-checkbox"
1160 data-entry-id="<?php echo esc_attr($prompt->id); ?>"
1161 data-source="<?php echo esc_attr($data_source); ?>"
1162 data-source-url="<?php echo esc_attr($source_url); ?>"
1163 data-is-group="false">
1164 </td>
1165 <td style="padding: 12px 16px; font-size: 13px;">
1166 <?php if ($data_source === 'pinecone') : ?>
1167 <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
1168 <?php else : ?>
1169 <?php echo esc_html($prompt->id); ?>
1170 <?php endif; ?>
1171 </td>
1172 <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
1173 <div class="mxchat-accordion-wrapper">
1174 <div class="mxchat-content-preview">
1175 <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
1176 <?php if (mb_strlen($content) > $preview_length) : ?>
1177 <button class="mxchat-expand-toggle" type="button">
1178 <span class="dashicons dashicons-arrow-down-alt2"></span>
1179 </button>
1180 <?php endif; ?>
1181 </div>
1182 <div class="mxchat-content-full" style="display: none;">
1183 <div class="content-view">
1184 <?php
1185 if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
1186 echo '<div dir="rtl" lang="he" class="rtl-content">';
1187 echo wp_kses_post(wpautop($content));
1188 echo '</div>';
1189 } else {
1190 echo wp_kses_post(wpautop($content));
1191 }
1192 ?>
1193 </div>
1194 </div>
1195 </div>
1196 </td>
1197 <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
1198 <?php
1199 $actual_source = $source_url;
1200 if (strpos($source_url, '_ungrouped_') === 0) {
1201 $actual_source = $prompt->source_url ?? '';
1202 }
1203 if (!empty($actual_source) && strpos($actual_source, 'mxchat://') !== 0) : ?>
1204 <a href="<?php echo esc_url($actual_source); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
1205 <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
1206 <?php esc_html_e('View', 'mxchat'); ?>
1207 </a>
1208 <?php else : ?>
1209 <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
1210 <?php endif; ?>
1211 </td>
1212 <td style="padding: 12px 16px; white-space: nowrap;">
1213 <button type="button"
1214 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
1215 data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
1216 data-entry-id="<?php echo esc_attr($prompt->id); ?>"
1217 data-data-source="<?php echo esc_attr($data_source); ?>"
1218 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1219 data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
1220 title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
1221 <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
1222 </button>
1223 <?php if ($data_source !== 'pinecone') : ?>
1224 <button type="button"
1225 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
1226 data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
1227 data-entry-id="<?php echo esc_attr($prompt->id); ?>"
1228 data-data-source="<?php echo esc_attr($data_source); ?>"
1229 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1230 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1231 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1232 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1233 </button>
1234 <?php endif; ?>
1235 <?php if ($data_source === 'pinecone') : ?>
1236 <button type="button" class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-ajax" data-vector-id="<?php echo esc_attr($prompt->id); ?>" data-bot-id="<?php echo esc_attr($current_bot_id); ?>" data-nonce="<?php echo wp_create_nonce('mxchat_delete_pinecone_prompt_nonce'); ?>" style="color: var(--mxch-error);">
1237 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1238 </button>
1239 <?php else : ?>
1240 <button type="button" class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-wordpress" data-entry-id="<?php echo esc_attr($prompt->id); ?>" data-bot-id="<?php echo esc_attr($current_bot_id); ?>" data-nonce="<?php echo wp_create_nonce('mxchat_delete_wordpress_prompt_nonce'); ?>" style="color: var(--mxch-error);">
1241 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1242 </button>
1243 <?php endif; ?>
1244 </td>
1245 </tr>
1246 <?php endif; ?>
1247 <?php endforeach; ?>
1248 <?php endif; ?>
1249 </tbody>
1250 </table>
1251 </div>
1252
1253 <!-- Pagination wrapper - always present so JS can populate it after processing -->
1254 <div id="mxchat-kb-pagination" class="mxchat-kb-pagination-wrapper" style="<?php echo $total_pages > 1 ? 'padding: 16px; border-top: 1px solid var(--mxch-card-border); text-align: center;' : ''; ?>" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>" data-search="<?php echo esc_attr($search_query); ?>" data-content-type="<?php echo esc_attr($content_type_filter); ?>">
1255 <?php if ($total_pages > 1) : ?>
1256 <div class="mxchat-ajax-pagination" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>">
1257 <?php if ($current_page > 1) : ?>
1258 <a href="#" class="mxchat-page-link" data-page="<?php echo ($current_page - 1); ?>"><?php esc_html_e('&laquo; Previous', 'mxchat'); ?></a>
1259 <?php endif; ?>
1260
1261 <?php
1262 $start_page = max(1, $current_page - 2);
1263 $end_page = min($total_pages, $current_page + 2);
1264
1265 if ($start_page > 1) {
1266 echo '<a href="#" class="mxchat-page-link" data-page="1">1</a> ';
1267 if ($start_page > 2) {
1268 echo '<span class="mxchat-page-dots">...</span> ';
1269 }
1270 }
1271
1272 for ($i = $start_page; $i <= $end_page; $i++) {
1273 if ($i == $current_page) {
1274 echo '<span class="mxchat-page-current">' . $i . '</span> ';
1275 } else {
1276 echo '<a href="#" class="mxchat-page-link" data-page="' . $i . '">' . $i . '</a> ';
1277 }
1278 }
1279
1280 if ($end_page < $total_pages) {
1281 if ($end_page < $total_pages - 1) {
1282 echo '<span class="mxchat-page-dots">...</span> ';
1283 }
1284 echo '<a href="#" class="mxchat-page-link" data-page="' . $total_pages . '">' . $total_pages . '</a> ';
1285 }
1286 ?>
1287
1288 <?php if ($current_page < $total_pages) : ?>
1289 <a href="#" class="mxchat-page-link" data-page="<?php echo ($current_page + 1); ?>"><?php esc_html_e('Next &raquo;', 'mxchat'); ?></a>
1290 <?php endif; ?>
1291 </div>
1292 <?php endif; ?>
1293 </div>
1294 </div>
1295 </div>
1296 </div>
1297 <?php
1298 }
1299
1300 /**
1301 * Render Auto-Sync Section
1302 */
1303 function mxchat_render_auto_sync_section($knowledge_manager) {
1304 ?>
1305 <div id="auto-sync" class="mxch-section">
1306 <div class="mxch-content-header">
1307 <h1 class="mxch-content-title"><?php esc_html_e('Auto-Sync Settings', 'mxchat'); ?></h1>
1308 <p class="mxch-content-subtitle"><?php esc_html_e('Automatically sync WordPress content to your knowledge base when published or updated.', 'mxchat'); ?></p>
1309 </div>
1310
1311 <div class="mxch-card">
1312 <div class="mxch-card-body mxchat-autosave-section">
1313 <div class="mxch-field">
1314 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 16px;">
1315 <label class="mxchat-toggle-switch">
1316 <input type="checkbox" name="mxchat_auto_sync_posts" class="mxchat-autosave-field" value="1" data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>" <?php checked(get_option('mxchat_auto_sync_posts', '0'), '1'); ?>>
1317 <span class="mxchat-toggle-slider"></span>
1318 </label>
1319 <span style="font-weight: 500;"><?php esc_html_e('Auto-sync Posts', 'mxchat'); ?></span>
1320 </div>
1321 <div style="display: flex; align-items: center; gap: 12px;">
1322 <label class="mxchat-toggle-switch">
1323 <input type="checkbox" name="mxchat_auto_sync_pages" class="mxchat-autosave-field" value="1" data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>" <?php checked(get_option('mxchat_auto_sync_pages', '0'), '1'); ?>>
1324 <span class="mxchat-toggle-slider"></span>
1325 </label>
1326 <span style="font-weight: 500;"><?php esc_html_e('Auto-sync Pages', 'mxchat'); ?></span>
1327 </div>
1328 </div>
1329
1330 <div class="mxch-field" style="margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--mxch-card-border);">
1331 <div style="display: flex; align-items: center; gap: 12px;">
1332 <label class="mxchat-toggle-switch">
1333 <input type="checkbox" name="mxchat_auto_sync_acf_pdfs" class="mxchat-autosave-field" value="1" data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>" <?php checked(get_option('mxchat_auto_sync_acf_pdfs', '0'), '1'); ?>>
1334 <span class="mxchat-toggle-slider"></span>
1335 </label>
1336 <span style="font-weight: 500;"><?php esc_html_e('Extract text from PDFs in ACF fields on save', 'mxchat'); ?></span>
1337 </div>
1338 <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('When ON, editor saves re-extract every PDF referenced in ACF fields. Default OFF — saves stay fast and don\'t re-parse the same PDFs on every edit. The manual content selector has its own per-batch checkbox; this setting only controls the auto-sync path.', 'mxchat'); ?></p>
1339 </div>
1340
1341 <div style="margin-top: 24px;">
1342 <button id="mxchat-custom-post-types-toggle" class="mxch-btn mxch-btn-secondary">
1343 <?php esc_html_e('Advanced Custom Post Sync Settings', 'mxchat'); ?>
1344 <span style="margin-left: 5px;"></span>
1345 </button>
1346 <div id="mxchat-custom-post-types-container" style="display: none; margin-top: 16px; padding: 16px; background: #f8fafc; border-radius: var(--mxch-radius-md);">
1347 <h4 style="margin: 0 0 12px 0;"><?php esc_html_e('Sync Custom Post Types', 'mxchat'); ?></h4>
1348 <?php
1349 $post_types = $knowledge_manager->mxchat_get_public_post_types();
1350 unset($post_types['post'], $post_types['page']);
1351
1352 if (!empty($post_types)) {
1353 foreach ($post_types as $post_type => $label) {
1354 $option_name = 'mxchat_auto_sync_' . $post_type;
1355 $is_enabled = get_option($option_name, '0');
1356 ?>
1357 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 8px;">
1358 <label class="mxchat-toggle-switch">
1359 <input type="checkbox" name="<?php echo esc_attr($option_name); ?>" class="mxchat-autosave-field" value="1" data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>" <?php checked($is_enabled, '1'); ?>>
1360 <span class="mxchat-toggle-slider"></span>
1361 </label>
1362 <span><?php echo esc_html($label); ?> (<?php echo esc_html($post_type); ?>)</span>
1363 </div>
1364 <?php
1365 }
1366 } else {
1367 echo '<p style="color: var(--mxch-text-muted);">' . esc_html__('No custom post types found.', 'mxchat') . '</p>';
1368 }
1369 ?>
1370 </div>
1371 </div>
1372 </div>
1373 </div>
1374 </div>
1375 <?php
1376 }
1377
1378 /**
1379 * Render Chunking Section
1380 */
1381 function mxchat_render_chunking_section() {
1382 $chunking_settings = MxChat_Chunker::get_settings();
1383 $chunking_enabled = $chunking_settings['chunking_enabled'];
1384 $chunk_size = $chunking_settings['chunk_size'];
1385 ?>
1386 <div id="chunking" class="mxch-section">
1387 <div class="mxch-content-header">
1388 <h1 class="mxch-content-title"><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></h1>
1389 <p class="mxch-content-subtitle"><?php esc_html_e('Control how content is split for indexing and how the knowledge base is searched.', 'mxchat'); ?></p>
1390 </div>
1391
1392 <div class="mxch-card">
1393 <div class="mxch-card-header">
1394 <h2 class="mxch-card-title"><?php esc_html_e('Chunking', 'mxchat'); ?></h2>
1395 </div>
1396 <div class="mxch-card-body mxchat-autosave-section">
1397 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1398 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1399 <span><?php esc_html_e('Chunking improves retrieval quality for long documents. All chunks are reassembled before sending to the AI. Only applies to new submissions.', 'mxchat'); ?></span>
1400 </div>
1401
1402 <div class="mxch-field">
1403 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
1404 <label class="mxchat-toggle-switch">
1405 <input type="checkbox" name="mxchat_chunking_enabled" id="mxchat_chunking_enabled" class="mxchat-autosave-field" value="1" data-option-name="mxchat_chunking_enabled" data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>" <?php checked($chunking_enabled, true); ?>>
1406 <span class="mxchat-toggle-slider"></span>
1407 </label>
1408 <span style="font-weight: 500;"><?php esc_html_e('Enable Content Chunking', 'mxchat'); ?></span>
1409 </div>
1410 </div>
1411
1412 <div class="mxch-field">
1413 <label class="mxch-field-label" for="mxchat_chunk_size"><?php esc_html_e('Chunk Size (characters)', 'mxchat'); ?></label>
1414 <input type="number" name="mxchat_chunk_size" id="mxchat_chunk_size" class="mxch-input mxchat-autosave-field" value="<?php echo esc_attr($chunk_size); ?>" min="1000" max="10000" step="500" data-option-name="mxchat_chunk_size" data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>" style="max-width: 200px;">
1415 <p class="mxch-field-description"><?php esc_html_e('Recommended: 4000 characters (~1000 tokens). Range: 1000-10000. Larger chunks preserve more context.', 'mxchat'); ?></p>
1416 </div>
1417 </div>
1418 </div>
1419
1420 <?php
1421 // Retrieval tuning (plan 11720c) — the hybrid keyword boost moved here
1422 // from Settings → Behavior: it is KB retrieval tuning, so it lives with
1423 // its natural siblings. SAME option key and save transport as before —
1424 // the name routes to the mxchat_save_setting handler, whose
1425 // mxchat_hybrid_keyword_toggle case still runs capability detection.
1426 ?>
1427 <div class="mxch-card">
1428 <div class="mxch-card-header">
1429 <h2 class="mxch-card-title"><?php esc_html_e('Retrieval', 'mxchat'); ?></h2>
1430 </div>
1431 <div class="mxch-card-body mxchat-autosave-section">
1432 <div class="mxch-field">
1433 <div style="display: flex; align-items: center; gap: 12px;">
1434 <label class="mxchat-toggle-switch">
1435 <input type="checkbox" name="mxchat_hybrid_keyword_toggle" id="mxchat_hybrid_keyword_toggle" class="mxchat-autosave-field" value="on" <?php checked(get_option('mxchat_hybrid_keyword_toggle', 'off'), 'on'); ?>>
1436 <span class="mxchat-toggle-slider"></span>
1437 </label>
1438 <span style="font-weight: 500;"><?php esc_html_e('Hybrid keyword boost (WP-DB knowledge base)', 'mxchat'); ?></span>
1439 </div>
1440 <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Combines keyword matching with vector similarity when searching your WordPress-database knowledge base. Helps exact-token questions — product codes, SKUs, error codes, names — find the right document even when semantic similarity alone would miss it.', 'mxchat'); ?></p>
1441 <p class="mxch-field-hint"><?php esc_html_e('Only affects the WordPress-database knowledge base; Pinecone is unaffected. Changes which sources are retrieved on existing installs, so test after enabling. The Transcripts sources panel labels how each match was found while this is on.', 'mxchat'); ?></p>
1442 </div>
1443 </div>
1444 </div>
1445 </div>
1446 <?php
1447 }
1448
1449 /**
1450 * Render Role Restrictions Section
1451 */
1452 function mxchat_render_role_restrictions_section($knowledge_manager) {
1453 $role_options = array(
1454 'public' => __('Public (Everyone)', 'mxchat'),
1455 'logged_in' => __('Logged In Users', 'mxchat'),
1456 'subscriber' => __('Subscribers & Above', 'mxchat'),
1457 'contributor' => __('Contributors & Above', 'mxchat'),
1458 'author' => __('Authors & Above', 'mxchat'),
1459 'editor' => __('Editors & Above', 'mxchat'),
1460 'administrator' => __('Administrators Only', 'mxchat')
1461 );
1462 ?>
1463 <div id="role-restrictions" class="mxch-section">
1464 <div class="mxch-content-header">
1465 <h1 class="mxch-content-title"><?php esc_html_e('Role-Based Content Restrictions', 'mxchat'); ?></h1>
1466 <p class="mxch-content-subtitle"><?php esc_html_e('Automatically restrict content access based on WordPress tags.', 'mxchat'); ?></p>
1467 </div>
1468
1469 <div class="mxch-card">
1470 <div class="mxch-card-body">
1471 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1472 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1473 <span><strong><?php esc_html_e('How it works:', 'mxchat'); ?></strong> <?php esc_html_e('Add a tag below and select which role should have access. Content with that tag will be restricted to that role level.', 'mxchat'); ?></span>
1474 </div>
1475
1476 <h4 style="margin: 0 0 16px 0;"><?php esc_html_e('Add Tag-Role Mapping', 'mxchat'); ?></h4>
1477 <div style="display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 24px;">
1478 <div class="mxch-field" style="flex: 1; min-width: 200px; margin-bottom: 0;">
1479 <label class="mxch-field-label" for="mxchat-tag-input"><?php esc_html_e('Tag (name or slug)', 'mxchat'); ?></label>
1480 <input type="text" id="mxchat-tag-input" class="mxch-input" placeholder="<?php esc_attr_e('e.g., Premium Content or premium-content', 'mxchat'); ?>">
1481 <span class="mxch-field-hint"><?php esc_html_e('Enter an existing post tag by its display name or its slug.', 'mxchat'); ?></span>
1482 </div>
1483 <div class="mxch-field" style="flex: 1; min-width: 200px; margin-bottom: 0;">
1484 <label class="mxch-field-label" for="mxchat-role-select"><?php esc_html_e('Required Role', 'mxchat'); ?></label>
1485 <select id="mxchat-role-select" class="mxch-select">
1486 <?php foreach ($role_options as $role_key => $role_label) : ?>
1487 <option value="<?php echo esc_attr($role_key); ?>"><?php echo esc_html($role_label); ?></option>
1488 <?php endforeach; ?>
1489 </select>
1490 </div>
1491 <button type="button" id="mxchat-add-tag-role" class="mxch-btn mxch-btn-primary">
1492 <span class="dashicons dashicons-plus-alt" style="font-size: 16px;"></span>
1493 <?php esc_html_e('Add Mapping', 'mxchat'); ?>
1494 </button>
1495 </div>
1496
1497 <h4 style="margin: 24px 0 16px 0;"><?php esc_html_e('Current Tag-Role Mappings', 'mxchat'); ?></h4>
1498 <div id="mxchat-mappings-container">
1499 <div class="mxchat-loading-mappings" style="text-align: center; padding: 20px; color: var(--mxch-text-muted);">
1500 <?php esc_html_e('Loading mappings...', 'mxchat'); ?>
1501 </div>
1502 </div>
1503 <div id="mxchat-no-mappings" style="display: none; text-align: center; padding: 40px; color: var(--mxch-text-muted);">
1504 <span class="dashicons dashicons-tag" style="font-size: 48px; opacity: 0.3;"></span>
1505 <p><?php esc_html_e('No tag-role mappings yet. Add your first mapping above.', 'mxchat'); ?></p>
1506 </div>
1507
1508 <div style="margin-top: 30px; padding-top: 24px; border-top: 1px solid var(--mxch-card-border);">
1509 <h4 style="margin: 0 0 12px 0;"><?php esc_html_e('Bulk Update Existing Content', 'mxchat'); ?></h4>
1510 <p class="mxch-field-description" style="margin-bottom: 16px;"><?php esc_html_e('Apply role restrictions to all existing content that has the mapped tags.', 'mxchat'); ?></p>
1511 <button type="button" id="mxchat-bulk-update-roles" class="mxch-btn mxch-btn-secondary">
1512 <span class="dashicons dashicons-update" style="font-size: 16px;"></span>
1513 <?php esc_html_e('Update All Existing Content', 'mxchat'); ?>
1514 </button>
1515 <div id="mxchat-bulk-update-progress" style="display: none; margin-top: 15px;">
1516 <div style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden;">
1517 <div class="mxchat-progress-fill" style="height: 100%; background: var(--mxch-primary); width: 0%; transition: width 0.3s;"></div>
1518 </div>
1519 <p class="mxchat-progress-text" style="margin-top: 10px; color: var(--mxch-text-secondary);"><?php esc_html_e('Processing...', 'mxchat'); ?></p>
1520 </div>
1521 <div id="mxchat-bulk-update-result" style="display: none; margin-top: 15px;"></div>
1522 </div>
1523 </div>
1524 </div>
1525 </div>
1526 <?php
1527 }
1528
1529 /**
1530 * Render ACF Fields Section
1531 */
1532 function mxchat_render_acf_fields_section($knowledge_manager) {
1533 $excluded_fields = get_option('mxchat_acf_excluded_fields', array());
1534 if (!is_array($excluded_fields)) {
1535 $excluded_fields = array();
1536 }
1537 ?>
1538 <div id="acf-fields" class="mxch-section">
1539 <div class="mxch-content-header">
1540 <h1 class="mxch-content-title"><?php esc_html_e('ACF Field Settings', 'mxchat'); ?></h1>
1541 <p class="mxch-content-subtitle"><?php esc_html_e('Control which Advanced Custom Fields are included in knowledge base embeddings.', 'mxchat'); ?></p>
1542 </div>
1543
1544 <div class="mxch-card">
1545 <div class="mxch-card-body mxchat-autosave-section">
1546 <?php
1547 // Install-level ACF→PDF import extraction (plan 11720c) — moved
1548 // here from a per-import checkbox in the content-selector modal.
1549 // Default OFF, matching its own "recommended only if…" guidance.
1550 ?>
1551 <div class="mxch-field" style="margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid var(--mxch-card-border);">
1552 <div style="display: flex; align-items: center; gap: 12px;">
1553 <label class="mxchat-toggle-switch">
1554 <input type="checkbox" name="mxchat_acf_pdf_extraction" id="mxchat_acf_pdf_extraction" class="mxchat-autosave-field" value="on" <?php checked(get_option('mxchat_acf_pdf_extraction', '0'), '1'); ?>>
1555 <span class="mxchat-toggle-slider"></span>
1556 </label>
1557 <span style="font-weight: 500;"><?php esc_html_e('Extract text from PDFs linked in ACF File/Image fields during imports', 'mxchat'); ?></span>
1558 </div>
1559 <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Applies to WordPress Content imports. Adds 200-500ms per post and ~30 KB to each knowledge base entry. Recommended only if your ACF setup uses PDF attachments for primary content. Editor-save extraction has its own toggle under Auto-Sync.', 'mxchat'); ?></p>
1560 </div>
1561 <?php if (!function_exists('acf_get_field_groups')): ?>
1562 <div class="mxch-notice mxch-notice-info">
1563 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1564 <span><?php esc_html_e('Advanced Custom Fields (ACF) plugin is not detected. Install and activate ACF to use this feature.', 'mxchat'); ?></span>
1565 </div>
1566 <?php else:
1567 $all_acf_fields = $knowledge_manager->mxchat_get_all_acf_fields();
1568
1569 if (empty($all_acf_fields)): ?>
1570 <div class="mxch-notice mxch-notice-info">
1571 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1572 <span><?php esc_html_e('No ACF field groups found. Create field groups in ACF to control which fields are included in embeddings.', 'mxchat'); ?></span>
1573 </div>
1574 <?php else: ?>
1575 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1576 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1577 <span><?php esc_html_e('Toggle fields ON to include them in knowledge base embeddings. Toggle OFF to exclude sensitive or irrelevant fields.', 'mxchat'); ?></span>
1578 </div>
1579
1580 <?php foreach ($all_acf_fields as $group_title => $fields): ?>
1581 <div class="mxchat-acf-field-group" style="margin-bottom: 24px;">
1582 <h4 style="margin: 0 0 12px 0; color: var(--mxch-text-primary); font-weight: 600;">
1583 <?php echo esc_html($group_title); ?>
1584 </h4>
1585 <div style="background: #f8fafc; border-radius: var(--mxch-radius-md); padding: 16px;">
1586 <?php foreach ($fields as $field):
1587 $field_name = $field['name'];
1588 $is_enabled = !in_array($field_name, $excluded_fields);
1589 ?>
1590 <div style="display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #e2e8f0;">
1591 <div>
1592 <span style="font-weight: 500;"><?php echo esc_html($field['label']); ?></span>
1593 <span style="color: var(--mxch-text-muted); font-size: 12px; margin-left: 8px;">(<?php echo esc_html($field_name); ?>)</span>
1594 <span style="color: var(--mxch-text-muted); font-size: 11px; margin-left: 8px; background: #e2e8f0; padding: 2px 6px; border-radius: 4px;"><?php echo esc_html($field['type']); ?></span>
1595 </div>
1596 <label class="mxchat-toggle-switch">
1597 <input type="checkbox"
1598 name="mxchat_acf_field_<?php echo esc_attr($field_name); ?>"
1599 class="mxchat-autosave-field"
1600 value="on"
1601 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1602 <?php checked($is_enabled, true); ?>>
1603 <span class="mxchat-toggle-slider"></span>
1604 </label>
1605 </div>
1606 <?php endforeach; ?>
1607 </div>
1608 </div>
1609 <?php endforeach; ?>
1610 <?php endif; ?>
1611 <?php endif; ?>
1612 </div>
1613 </div>
1614 </div>
1615 <?php
1616 }
1617
1618 /**
1619 * Render Custom Meta Section
1620 */
1621 function mxchat_render_custom_meta_section() {
1622 $whitelist = get_option('mxchat_custom_meta_whitelist', '');
1623 ?>
1624 <div id="custom-meta" class="mxch-section">
1625 <div class="mxch-content-header">
1626 <h1 class="mxch-content-title"><?php esc_html_e('Custom Post Meta', 'mxchat'); ?></h1>
1627 <p class="mxch-content-subtitle"><?php esc_html_e('Include custom post meta fields (non-ACF) in knowledge base embeddings.', 'mxchat'); ?></p>
1628 </div>
1629
1630 <div class="mxch-card">
1631 <div class="mxch-card-body mxchat-autosave-section">
1632 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1633 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1634 <span><?php esc_html_e('Enter post meta keys to include in embeddings. This is useful for OptionTree fields, theme meta boxes, or any custom post meta that is not managed by ACF.', 'mxchat'); ?></span>
1635 </div>
1636
1637 <!-- Meta-key discovery scanner (plan-mxchat-20260709-fe8e4e): mirror the ACF picker's
1638 discover-for-you UX for non-ACF meta, so owners click keys instead of typing them blind. -->
1639 <div class="mxch-field mxchat-meta-scan">
1640 <label class="mxch-field-label"><?php esc_html_e('Discover meta keys', 'mxchat'); ?></label>
1641 <p class="mxch-field-description" style="margin-top: 0;">
1642 <?php esc_html_e('Scan your published content for custom meta keys, then click any you want to include. This gives non-ACF meta the same discover-and-click experience as the ACF picker.', 'mxchat'); ?>
1643 </p>
1644 <div class="mxchat-meta-scan-controls">
1645 <button type="button" id="mxchat-scan-meta-btn" class="mxch-btn mxch-btn-secondary" data-nonce="<?php echo esc_attr(wp_create_nonce('mxchat_prompts_setting_nonce')); ?>">
1646 <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"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
1647 <span class="mxchat-scan-label"><?php esc_html_e('Scan my content for meta keys', 'mxchat'); ?></span>
1648 </button>
1649 <label class="mxchat-meta-scan-internal">
1650 <input type="checkbox" id="mxchat-scan-internal">
1651 <?php esc_html_e('Include internal keys (underscore-prefixed)', 'mxchat'); ?>
1652 </label>
1653 </div>
1654 <div id="mxchat-meta-scan-results" class="mxchat-meta-scan-results" aria-live="polite"></div>
1655 </div>
1656
1657 <div class="mxch-field">
1658 <label class="mxch-field-label" for="mxchat_custom_meta_whitelist">
1659 <?php esc_html_e('Meta Key Whitelist', 'mxchat'); ?>
1660 </label>
1661 <textarea
1662 name="mxchat_custom_meta_whitelist"
1663 id="mxchat_custom_meta_whitelist"
1664 class="mxch-textarea mxchat-autosave-field"
1665 rows="6"
1666 placeholder="speaker_profession&#10;speaker_company&#10;event_location&#10;_custom_field_key"
1667 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1668 style="font-family: monospace;"
1669 ><?php echo esc_textarea($whitelist); ?></textarea>
1670 <p class="mxch-field-description">
1671 <?php esc_html_e('Enter one meta key per line. These fields will be appended to the content during embedding. Text values are added as-is; array or repeater values are flattened into a comma-separated list. Empty values are skipped.', 'mxchat'); ?>
1672 </p>
1673 </div>
1674
1675 <div style="margin-top: 20px; padding: 16px; background: #f8fafc; border-radius: var(--mxch-radius-md);">
1676 <h4 style="margin: 0 0 12px 0; font-size: 14px;"><?php esc_html_e('How to find meta keys:', 'mxchat'); ?></h4>
1677 <ul style="margin: 0; padding-left: 20px; color: var(--mxch-text-secondary); font-size: 13px;">
1678 <li><?php esc_html_e('Check your theme documentation for meta key names', 'mxchat'); ?></li>
1679 <li><?php esc_html_e('Look in the wp_postmeta database table', 'mxchat'); ?></li>
1680 <li><?php esc_html_e('Use the Scan my content for meta keys button above to discover the keys on your published content automatically', 'mxchat'); ?></li>
1681 <li><?php esc_html_e('Meta keys starting with underscore (_) are usually hidden/internal', 'mxchat'); ?></li>
1682 </ul>
1683 </div>
1684 </div>
1685 </div>
1686 </div>
1687 <?php
1688 }
1689
1690 /**
1691 * Render Pinecone Section
1692 */
1693 function mxchat_render_pinecone_section() {
1694 $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
1695 $use_pinecone = $pinecone_options['mxchat_use_pinecone'] ?? '0';
1696 ?>
1697 <div id="pinecone" class="mxch-section">
1698 <div class="mxch-content-header">
1699 <h1 class="mxch-content-title"><?php esc_html_e('Pinecone Vector Database', 'mxchat'); ?></h1>
1700 <p class="mxch-content-subtitle"><?php esc_html_e('Configure Pinecone for enhanced search performance with larger knowledge bases.', 'mxchat'); ?></p>
1701 </div>
1702
1703 <div class="mxch-card">
1704 <div class="mxch-card-body">
1705 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1706 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1707 <span><strong><?php esc_html_e('Pinecone is optional.', 'mxchat'); ?></strong> <?php esc_html_e('MxChat works without it. When enabled, content is stored in Pinecone for faster similarity searches.', 'mxchat'); ?></span>
1708 </div>
1709
1710 <form method="post" action="options.php">
1711 <?php settings_fields('mxchat_pinecone_addon_options'); ?>
1712
1713 <div class="mxch-field">
1714 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
1715 <label class="mxchat-toggle-switch">
1716 <input type="checkbox" name="mxchat_pinecone_addon_options[mxchat_use_pinecone]" value="1" <?php checked($use_pinecone, '1'); ?>>
1717 <span class="mxchat-toggle-slider"></span>
1718 </label>
1719 <span style="font-weight: 500;"><?php esc_html_e('Enable Pinecone Database', 'mxchat'); ?></span>
1720 </div>
1721 </div>
1722
1723 <div class="mxchat-pinecone-settings" <?php echo $use_pinecone ? '' : 'style="display: none;"'; ?>>
1724 <?php if ($use_pinecone) : ?>
1725 <div class="mxch-notice mxch-notice-success" style="margin-bottom: 20px;">
1726 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
1727 <span><?php esc_html_e('Pinecone is enabled. All new knowledge base content will be stored in Pinecone.', 'mxchat'); ?></span>
1728 </div>
1729 <?php endif; ?>
1730
1731 <div class="mxch-field">
1732 <label class="mxch-field-label" for="mxchat_pinecone_api_key">
1733 <?php esc_html_e('Pinecone API Key', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1734 </label>
1735 <input type="password" id="mxchat_pinecone_api_key" name="mxchat_pinecone_addon_options[mxchat_pinecone_api_key]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_api_key'] ?? ''); ?>" class="mxch-input" placeholder="pcsk_...">
1736 <p class="mxch-field-description">
1737 <?php esc_html_e('Found in your Pinecone dashboard under API Keys.', 'mxchat'); ?>
1738 <a href="https://app.pinecone.io/" target="_blank"><?php esc_html_e('Open Pinecone Dashboard', 'mxchat'); ?></a>
1739 </p>
1740 </div>
1741
1742 <div class="mxch-field">
1743 <label class="mxch-field-label" for="mxchat_pinecone_environment"><?php esc_html_e('Region', 'mxchat'); ?></label>
1744 <input type="text" id="mxchat_pinecone_environment" name="mxchat_pinecone_addon_options[mxchat_pinecone_environment]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_environment'] ?? ''); ?>" class="mxch-input" placeholder="e.g., gcp-starter">
1745 <p class="mxch-field-description"><?php esc_html_e('Your Pinecone environment/region (e.g., gcp-starter, us-west1-gcp)', 'mxchat'); ?></p>
1746 </div>
1747
1748 <div class="mxch-field">
1749 <label class="mxch-field-label" for="mxchat_pinecone_index">
1750 <?php esc_html_e('Index Name', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1751 </label>
1752 <input type="text" id="mxchat_pinecone_index" name="mxchat_pinecone_addon_options[mxchat_pinecone_index]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_index'] ?? ''); ?>" class="mxch-input" placeholder="e.g., my-wordpress-vectors">
1753 <p class="mxch-field-description"><?php esc_html_e('The name of your Pinecone index. Must be created in your Pinecone dashboard first.', 'mxchat'); ?></p>
1754 </div>
1755
1756 <div class="mxch-field">
1757 <label class="mxch-field-label" for="mxchat_pinecone_host">
1758 <?php esc_html_e('Pinecone Host', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1759 </label>
1760 <input type="text" id="mxchat_pinecone_host" name="mxchat_pinecone_addon_options[mxchat_pinecone_host]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_host'] ?? ''); ?>" class="mxch-input" placeholder="e.g., my-index-xyz123.svc.pinecone.io">
1761 <p class="mxch-field-description"><?php esc_html_e('The hostname from your Pinecone index URL (exclude https://). Found in index details.', 'mxchat'); ?></p>
1762 </div>
1763 </div>
1764
1765 <?php submit_button(__('Save Pinecone Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1766 </form>
1767 </div>
1768 </div>
1769 </div>
1770 <?php
1771 }
1772
1773 /**
1774 * Render OpenAI Vector Store Section
1775 */
1776 function mxchat_render_openai_vectorstore_section() {
1777 $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
1778 $use_vectorstore = $vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0';
1779 $vectorstore_ids = $vectorstore_options['mxchat_vectorstore_ids'] ?? '';
1780 $max_results = $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5;
1781
1782 // Get current chat model to check compatibility
1783 $mxchat_options = get_option('mxchat_options', array());
1784 $current_model = $mxchat_options['model'] ?? 'gpt-5.6-sol';
1785 $is_openai_model = preg_match('/^(gpt-|o1-|o3-)/', $current_model);
1786 ?>
1787 <div id="openai-vectorstore" class="mxch-section">
1788 <div class="mxch-content-header">
1789 <h1 class="mxch-content-title"><?php esc_html_e('OpenAI Vector Store', 'mxchat'); ?></h1>
1790 <p class="mxch-content-subtitle"><?php esc_html_e('Use OpenAI\'s hosted vector database for knowledge retrieval. Your documents are stored and searched directly on OpenAI\'s platform.', 'mxchat'); ?></p>
1791 </div>
1792
1793 <div class="mxch-card">
1794 <div class="mxch-card-body">
1795 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1796 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1797 <span><strong><?php esc_html_e('Requires OpenAI Chat Model.', 'mxchat'); ?></strong> <?php esc_html_e('Vector Store search only works with OpenAI models (gpt-5.6-sol, gpt-5.6-terra, etc.). Create Vector Stores in your OpenAI Dashboard.', 'mxchat'); ?></span>
1798 </div>
1799
1800 <?php if (!$is_openai_model && $use_vectorstore === '1'): ?>
1801 <div class="mxch-notice mxch-notice-warning" style="margin-bottom: 20px;">
1802 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
1803 <span><strong><?php esc_html_e('Model Incompatible:', 'mxchat'); ?></strong> <?php printf(esc_html__('Your current chat model (%s) is not an OpenAI model. Vector Store search will not work until you switch to an OpenAI model.', 'mxchat'), esc_html($current_model)); ?></span>
1804 </div>
1805 <?php endif; ?>
1806
1807 <form method="post" action="options.php" id="mxchat-vectorstore-form">
1808 <?php settings_fields('mxchat_openai_vectorstore_options'); ?>
1809
1810 <div class="mxch-field">
1811 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
1812 <label class="mxchat-toggle-switch">
1813 <input type="checkbox" name="mxchat_openai_vectorstore_options[mxchat_use_openai_vectorstore]" value="1" <?php checked($use_vectorstore, '1'); ?> id="mxchat_use_openai_vectorstore">
1814 <span class="mxchat-toggle-slider"></span>
1815 </label>
1816 <span style="font-weight: 500;"><?php esc_html_e('Enable OpenAI Vector Store', 'mxchat'); ?></span>
1817 </div>
1818 </div>
1819
1820 <div class="mxchat-vectorstore-settings" <?php echo $use_vectorstore === '1' ? '' : 'style="display: none;"'; ?>>
1821 <?php if ($use_vectorstore === '1'): ?>
1822 <div class="mxch-notice mxch-notice-success" style="margin-bottom: 20px;">
1823 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
1824 <span><?php esc_html_e('OpenAI Vector Store is enabled. Queries will search your Vector Store for relevant content.', 'mxchat'); ?></span>
1825 </div>
1826 <?php endif; ?>
1827
1828 <div class="mxch-field">
1829 <label class="mxch-field-label" for="mxchat_vectorstore_ids">
1830 <?php esc_html_e('Vector Store ID(s)', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1831 </label>
1832 <input type="text" id="mxchat_vectorstore_ids" name="mxchat_openai_vectorstore_options[mxchat_vectorstore_ids]" value="<?php echo esc_attr($vectorstore_ids); ?>" class="mxch-input" placeholder="vs_abc123xyz">
1833 <p class="mxch-field-description">
1834 <?php esc_html_e('Enter your Vector Store ID from the OpenAI Dashboard. For multiple stores, separate with commas.', 'mxchat'); ?>
1835 <a href="https://platform.openai.com/storage/vector_stores" target="_blank"><?php esc_html_e('Open OpenAI Vector Store Dashboard', 'mxchat'); ?></a>
1836 </p>
1837 </div>
1838
1839 <div class="mxch-field">
1840 <label class="mxch-field-label" for="mxchat_vectorstore_max_results">
1841 <?php esc_html_e('Max Results', 'mxchat'); ?>
1842 </label>
1843 <select id="mxchat_vectorstore_max_results" name="mxchat_openai_vectorstore_options[mxchat_vectorstore_max_results]" class="mxch-input" style="width: auto;">
1844 <?php for ($i = 1; $i <= 10; $i++): ?>
1845 <option value="<?php echo $i; ?>" <?php selected($max_results, $i); ?>><?php echo $i; ?></option>
1846 <?php endfor; ?>
1847 </select>
1848 <p class="mxch-field-description"><?php esc_html_e('Maximum number of content chunks to retrieve from the Vector Store.', 'mxchat'); ?></p>
1849 </div>
1850 </div>
1851
1852 <?php submit_button(__('Save Vector Store Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1853 </form>
1854 </div>
1855 </div>
1856 </div>
1857 <?php
1858 }
1859
1860 /**
1861 * Render Content Selector Modal
1862 */
1863 function mxchat_render_content_selector_modal() {
1864 ?>
1865 <div id="mxchat-kb-content-selector-modal" class="mxchat-kb-modal">
1866 <div class="mxchat-kb-modal-content">
1867 <div class="mxchat-kb-modal-header">
1868 <h3>
1869 <?php esc_html_e('Select WordPress Content', 'mxchat'); ?><br>
1870 <span class="mxchat-kb-header-note"><?php esc_html_e('(Content imported here will be tagged "In Knowledge Base")', 'mxchat'); ?></span>
1871 </h3>
1872 <span class="mxchat-kb-modal-close">&times;</span>
1873 </div>
1874 <div class="mxchat-kb-modal-filters">
1875 <div class="mxchat-kb-search-group">
1876 <input type="text" id="mxchat-kb-content-search" placeholder="<?php esc_attr_e('Search...', 'mxchat'); ?>">
1877 </div>
1878 <div class="mxchat-kb-filter-group">
1879 <select id="mxchat-kb-content-type-filter">
1880 <option value="all"><?php esc_html_e('All Content Types', 'mxchat'); ?></option>
1881 <option value="post"><?php esc_html_e('Posts', 'mxchat'); ?></option>
1882 <option value="page"><?php esc_html_e('Pages', 'mxchat'); ?></option>
1883 <?php
1884 $post_types = get_post_types(array('public' => true), 'objects');
1885 foreach ($post_types as $post_type) {
1886 if (!in_array($post_type->name, array('post', 'page'))) {
1887 echo '<option value="' . esc_attr($post_type->name) . '">' . esc_html($post_type->label) . '</option>';
1888 }
1889 }
1890 ?>
1891 </select>
1892 <select id="mxchat-kb-content-status-filter">
1893 <option value="publish"><?php esc_html_e('Published', 'mxchat'); ?></option>
1894 <option value="draft"><?php esc_html_e('Drafts', 'mxchat'); ?></option>
1895 <option value="all"><?php esc_html_e('All Statuses', 'mxchat'); ?></option>
1896 </select>
1897 <select id="mxchat-kb-processed-filter">
1898 <option value="all"><?php esc_html_e('All Content', 'mxchat'); ?></option>
1899 <option value="processed"><?php esc_html_e('In Knowledge Base', 'mxchat'); ?></option>
1900 <option value="unprocessed"><?php esc_html_e('Not In Knowledge Base', 'mxchat'); ?></option>
1901 </select>
1902 </div>
1903 </div>
1904 <div class="mxchat-kb-content-selection">
1905 <div class="mxchat-kb-selection-header">
1906 <label>
1907 <input type="checkbox" id="mxchat-kb-select-all">
1908 <?php esc_html_e('Select All on Page', 'mxchat'); ?>
1909 </label>
1910 <span class="mxchat-kb-selection-count">0 <?php esc_html_e('selected', 'mxchat'); ?></span>
1911 <span class="mxchat-kb-selection-hint" style="margin-left: auto; font-size: 12px; color: var(--mxch-text-muted);">
1912 <span class="dashicons dashicons-info-outline" style="font-size: 14px; vertical-align: middle;"></span>
1913 <?php esc_html_e('Selections persist across pages', 'mxchat'); ?>
1914 </span>
1915 </div>
1916 <div class="mxchat-kb-content-list">
1917 <div class="mxchat-kb-loading">
1918 <span class="mxchat-kb-spinner is-active"></span>
1919 <?php esc_html_e('Loading content...', 'mxchat'); ?>
1920 </div>
1921 </div>
1922 </div>
1923 <div class="mxchat-kb-modal-footer">
1924 <div class="mxchat-kb-pagination"></div>
1925 <div class="mxchat-kb-footer-actions">
1926 <button type="button" id="mxchat-kb-process-selected" class="mxchat-kb-button-primary" disabled>
1927 <?php esc_html_e('Process Selected Content', 'mxchat'); ?>
1928 <span class="mxchat-kb-selected-count">(0)</span>
1929 </button>
1930 <button type="button" class="mxchat-kb-button-secondary mxchat-kb-modal-close">
1931 <?php esc_html_e('Cancel', 'mxchat'); ?>
1932 </button>
1933 </div>
1934 </div>
1935 </div>
1936 </div>
1937 <?php
1938 }
1939
1940 /**
1941 * Render Edit Entry Modal
1942 */
1943 function mxchat_render_edit_entry_modal() {
1944 ?>
1945 <div id="mxchat-kb-edit-modal" class="mxchat-kb-modal">
1946 <div class="mxchat-kb-modal-content mxchat-kb-edit-modal-content">
1947 <div class="mxchat-kb-modal-header">
1948 <h3 id="mxchat-kb-edit-title"><?php esc_html_e('Edit Knowledge Entry', 'mxchat'); ?></h3>
1949 <span class="mxchat-kb-modal-close" id="mxchat-kb-edit-close">&times;</span>
1950 </div>
1951 <div class="mxchat-kb-edit-body">
1952 <div class="mxchat-kb-edit-source" id="mxchat-kb-edit-source"></div>
1953 <textarea id="mxchat-kb-edit-textarea" class="mxchat-kb-edit-textarea" placeholder="<?php esc_attr_e('Loading content...', 'mxchat'); ?>"></textarea>
1954 <div class="mxchat-kb-edit-meta">
1955 <span id="mxchat-kb-edit-charcount"></span>
1956 <span id="mxchat-kb-edit-chunkinfo"></span>
1957 </div>
1958 </div>
1959 <div class="mxchat-kb-edit-footer">
1960 <div class="mxchat-kb-edit-notice" id="mxchat-kb-edit-notice"></div>
1961 <div class="mxchat-kb-edit-actions">
1962 <button type="button" id="mxchat-kb-edit-cancel" class="mxch-btn mxch-btn-ghost">
1963 <?php esc_html_e('Cancel', 'mxchat'); ?>
1964 </button>
1965 <button type="button" id="mxchat-kb-edit-save" class="mxch-btn mxch-btn-primary">
1966 <span class="mxchat-kb-edit-save-text"><?php esc_html_e('Save & Re-embed', 'mxchat'); ?></span>
1967 <span class="mxchat-kb-edit-save-spinner" style="display:none;">
1968 <span class="mxchat-kb-spinner is-active" style="margin: 0;"></span>
1969 <?php esc_html_e('Saving...', 'mxchat'); ?>
1970 </span>
1971 </button>
1972 </div>
1973 </div>
1974 </div>
1975 </div>
1976 <?php
1977 }
1978
1979 /**
1980 * Render the read-only Knowledge Inspector modal (plan-mxchat-20260628-d8cb4b).
1981 * Shows exactly what was indexed for an entry: the per-chunk stored text + lengths
1982 * and (for Pinecone) the stored metadata fields, rendered with the Testing tab's
1983 * own card components so the two surfaces feel like one system. Self-contained
1984 * markup + JS so it doesn't entangle the edit-modal init.
1985 */
1986 function mxchat_render_inspect_entry_modal() {
1987 ?>
1988 <div id="mxchat-kb-inspect-modal" class="mxchat-kb-modal">
1989 <div class="mxchat-kb-modal-content mxchat-kb-inspect-modal-content">
1990 <div class="mxchat-kb-modal-header">
1991 <h3 id="mxchat-kb-inspect-title"><?php esc_html_e('Indexed Content', 'mxchat'); ?></h3>
1992 <span class="mxchat-kb-modal-close" id="mxchat-kb-inspect-close">&times;</span>
1993 </div>
1994 <div class="mxchat-kb-inspect-body">
1995 <div class="mxchat-kb-edit-source" id="mxchat-kb-inspect-source"></div>
1996 <div class="mxch-testing-results" id="mxchat-kb-inspect-results">
1997 <div class="mxch-testing-no-data"><?php esc_html_e('Loading indexed content…', 'mxchat'); ?></div>
1998 </div>
1999 </div>
2000 <div class="mxchat-kb-edit-footer">
2001 <div class="mxchat-kb-edit-notice" id="mxchat-kb-inspect-notice"></div>
2002 <div class="mxchat-kb-edit-actions">
2003 <button type="button" id="mxchat-kb-inspect-close-btn" class="mxch-btn mxch-btn-primary">
2004 <?php esc_html_e('Close', 'mxchat'); ?>
2005 </button>
2006 </div>
2007 </div>
2008 </div>
2009 </div>
2010 <script>
2011 (function() {
2012 'use strict';
2013 document.addEventListener('DOMContentLoaded', function() {
2014 var modal = document.getElementById('mxchat-kb-inspect-modal');
2015 if (!modal) return;
2016 var closeX = document.getElementById('mxchat-kb-inspect-close');
2017 var closeBtn = document.getElementById('mxchat-kb-inspect-close-btn');
2018 var sourceEl = document.getElementById('mxchat-kb-inspect-source');
2019 var resultsEl= document.getElementById('mxchat-kb-inspect-results');
2020 var titleEl = document.getElementById('mxchat-kb-inspect-title');
2021 var noticeEl = document.getElementById('mxchat-kb-inspect-notice');
2022
2023 var I18N = {
2024 title: <?php echo wp_json_encode( esc_html__('Indexed Content', 'mxchat') ); ?>,
2025 loading: <?php echo wp_json_encode( esc_html__('Loading indexed content…', 'mxchat') ); ?>,
2026 manual: <?php echo wp_json_encode( esc_html__('Manual Content', 'mxchat') ); ?>,
2027 source: <?php echo wp_json_encode( esc_html__('Source', 'mxchat') ); ?>,
2028 store: <?php echo wp_json_encode( esc_html__('Storage', 'mxchat') ); ?>,
2029 type: <?php echo wp_json_encode( esc_html__('Content type', 'mxchat') ); ?>,
2030 chunksLbl:<?php echo wp_json_encode( esc_html__('Chunks', 'mxchat') ); ?>,
2031 totalLen: <?php echo wp_json_encode( esc_html__('Total indexed length', 'mxchat') ); ?>,
2032 chunk: <?php echo wp_json_encode( esc_html__('Chunk', 'mxchat') ); ?>,
2033 indexed: <?php echo wp_json_encode( esc_html__('Indexed', 'mxchat') ); ?>,
2034 chars: <?php echo wp_json_encode( esc_html__('chars', 'mxchat') ); ?>,
2035 showText: <?php echo wp_json_encode( esc_html__('Show indexed text', 'mxchat') ); ?>,
2036 hideText: <?php echo wp_json_encode( esc_html__('Hide indexed text', 'mxchat') ); ?>,
2037 wpStore: <?php echo wp_json_encode( esc_html__('Local WordPress database', 'mxchat') ); ?>,
2038 pcStore: <?php echo wp_json_encode( esc_html__('Pinecone', 'mxchat') ); ?>,
2039 meta: <?php echo wp_json_encode( esc_html__('Vector metadata', 'mxchat') ); ?>,
2040 loadFail: <?php echo wp_json_encode( esc_html__('Failed to load indexed content.', 'mxchat') ); ?>,
2041 netErr: <?php echo wp_json_encode( esc_html__('Network error: ', 'mxchat') ); ?>
2042 };
2043
2044 function esc(str) {
2045 var d = document.createElement('div');
2046 d.textContent = (str === null || str === undefined) ? '' : String(str);
2047 return d.innerHTML;
2048 }
2049 function fmt(n) { try { return Number(n).toLocaleString(); } catch (e) { return n; } }
2050
2051 document.addEventListener('click', function(e) {
2052 var btn = e.target.closest('.mxchat-inspect-entry-btn');
2053 if (btn) { e.preventDefault(); open(btn); }
2054 });
2055 if (closeX) closeX.addEventListener('click', close);
2056 if (closeBtn) closeBtn.addEventListener('click', close);
2057 // Overlay click closes, but only when the whole gesture happened on the
2058 // overlay — otherwise selecting indexed text and releasing past the
2059 // dialog edge dispatches the click on the overlay and closes it.
2060 var downOnOverlay = false;
2061 modal.addEventListener('mousedown', function(e) { downOnOverlay = (e.target === modal); });
2062 modal.addEventListener('click', function(e) {
2063 var overlayGesture = downOnOverlay;
2064 downOnOverlay = false;
2065 if (e.target === modal && overlayGesture) close();
2066 });
2067 document.addEventListener('keydown', function(e) {
2068 if (e.key === 'Escape' && modal.classList.contains('active')) close();
2069 });
2070
2071 function close() { modal.classList.remove('active'); }
2072
2073 function open(btn) {
2074 var entry = {
2075 sourceUrl: btn.getAttribute('data-source-url') || '',
2076 entryId: btn.getAttribute('data-entry-id') || '',
2077 dataSource: btn.getAttribute('data-data-source') || 'wordpress',
2078 botId: btn.getAttribute('data-bot-id') || 'default',
2079 nonce: btn.getAttribute('data-nonce') || ''
2080 };
2081
2082 titleEl.textContent = I18N.title;
2083 noticeEl.textContent = '';
2084 noticeEl.className = 'mxchat-kb-edit-notice';
2085 resultsEl.innerHTML = '<div class="mxch-testing-no-data">' + esc(I18N.loading) + '</div>';
2086
2087 if (entry.sourceUrl && entry.sourceUrl.indexOf('mxchat://') !== 0 && entry.sourceUrl.indexOf('_ungrouped_') !== 0) {
2088 sourceEl.innerHTML = esc(I18N.source) + ': <a href="' + esc(entry.sourceUrl) + '" target="_blank" rel="noopener">' + esc(entry.sourceUrl) + '</a>';
2089 } else {
2090 sourceEl.textContent = I18N.manual;
2091 }
2092
2093 modal.classList.add('active');
2094
2095 var fd = new FormData();
2096 fd.append('action', 'mxchat_inspect_entry');
2097 fd.append('nonce', entry.nonce);
2098 fd.append('source_url', entry.sourceUrl);
2099 fd.append('entry_id', entry.entryId);
2100 fd.append('data_source', entry.dataSource);
2101 fd.append('bot_id', entry.botId);
2102
2103 fetch(ajaxurl, { method: 'POST', body: fd })
2104 .then(function(r) { return r.json(); })
2105 .then(function(resp) {
2106 if (resp && resp.success) {
2107 render(resp.data);
2108 } else {
2109 var msg = (resp && resp.data && resp.data.message) || I18N.loadFail;
2110 resultsEl.innerHTML = '';
2111 showNotice(msg, 'error');
2112 }
2113 })
2114 .catch(function(err) {
2115 resultsEl.innerHTML = '';
2116 showNotice(I18N.netErr + err.message, 'error');
2117 });
2118 }
2119
2120 function render(data) {
2121 var storeLabel = data.store === 'pinecone' ? I18N.pcStore : I18N.wpStore;
2122 var html = '';
2123
2124 // Summary card — what this entry is + how much was indexed.
2125 html += '<div class="match-card above-threshold">' +
2126 '<div class="match-header">' +
2127 '<div class="match-title"><span class="status-icon">&#128230;</span> ' + esc(data.chunk_count) + ' ' + esc(I18N.chunksLbl.toLowerCase()) + '</div>' +
2128 '<span class="context-label">' + esc(storeLabel) + '</span>' +
2129 '</div>' +
2130 '<div class="mxch-testing-field"><label>' + esc(I18N.type) + '</label><code>' + esc(data.content_type || '') + '</code> ' +
2131 '&nbsp; <label style="display:inline; text-transform:none; letter-spacing:0; font-weight:600;">' + esc(I18N.totalLen) + ':</label> ' + esc(fmt(data.assembled_length)) + ' ' + esc(I18N.chars) + '</div>';
2132 if (data.metadata_note) {
2133 html += '<div class="match-source"><span class="source-icon">&#8505;&#65039;</span> ' + esc(data.metadata_note) + '</div>';
2134 }
2135 html += '</div>';
2136
2137 // One card per stored chunk — the actual embedded text.
2138 var chunks = data.chunks || [];
2139 chunks.forEach(function(chunk, i) {
2140 var num = (chunk.index !== null && chunk.index !== undefined) ? (chunk.index + 1) : (i + 1);
2141 var metaRows = '';
2142 if (chunk.metadata && typeof chunk.metadata === 'object') {
2143 var keys = Object.keys(chunk.metadata);
2144 if (keys.length) {
2145 metaRows += '<div class="chunk-detail-row chunk-used" style="display:block;"><span class="chunk-detail-num">' + esc(I18N.meta) + '</span></div>';
2146 keys.forEach(function(k) {
2147 metaRows += '<div class="chunk-detail-row"><span class="chunk-detail-num">' + esc(k) + '</span><span class="chunk-detail-score">' + esc(chunk.metadata[k]) + '</span></div>';
2148 });
2149 }
2150 }
2151 html += '<div class="match-card above-threshold">' +
2152 '<div class="match-header">' +
2153 '<div class="match-title"><span class="status-icon">&#10003;</span> ' + esc(I18N.chunk) + ' ' + esc(num) +
2154 '<span class="chunk-summary">' + esc(fmt(chunk.length)) + ' ' + esc(I18N.chars) + '</span>' +
2155 '</div>' +
2156 '<span class="context-label">' + esc(I18N.indexed) + '</span>' +
2157 '</div>' +
2158 '<span class="chunk-expand-toggle" data-chunk="' + i + '">&#9654; ' + esc(I18N.showText) + '</span>' +
2159 '<div class="chunk-details" data-chunk="' + i + '">' +
2160 '<pre class="mxch-kb-inspect-chunk-text">' + esc(chunk.text) + '</pre>' +
2161 metaRows +
2162 '</div>' +
2163 '</div>';
2164 });
2165
2166 resultsEl.innerHTML = html;
2167
2168 resultsEl.querySelectorAll('.chunk-expand-toggle').forEach(function(toggle) {
2169 toggle.addEventListener('click', function() {
2170 var id = this.getAttribute('data-chunk');
2171 var details = resultsEl.querySelector('.chunk-details[data-chunk="' + id + '"]');
2172 if (!details) return;
2173 var expanded = details.classList.toggle('expanded');
2174 this.innerHTML = (expanded ? '&#9660; ' + esc(I18N.hideText) : '&#9654; ' + esc(I18N.showText));
2175 });
2176 });
2177 }
2178
2179 function showNotice(msg, type) {
2180 noticeEl.textContent = msg;
2181 noticeEl.className = 'mxchat-kb-edit-notice' + (type ? ' ' + type : '');
2182 }
2183 });
2184 })();
2185 </script>
2186 <?php
2187 }
2188
2189 /**
2190 * Render Knowledge Page Navigation Scripts
2191 */
2192 function mxchat_render_knowledge_page_scripts() {
2193 ?>
2194 <script>
2195 (function() {
2196 'use strict';
2197
2198 // Wait for DOM
2199 document.addEventListener('DOMContentLoaded', function() {
2200 initKnowledgeNavigation();
2201 initMobileMenu();
2202 initImportOptions();
2203 initCustomPostTypesToggle();
2204 // Sitemap detection is now initialized when user clicks the Sitemap Import option
2205 });
2206
2207 function initKnowledgeNavigation() {
2208 const navLinks = document.querySelectorAll('.mxch-nav-link[data-target], .mxch-mobile-nav-link[data-target]');
2209 const sections = document.querySelectorAll('.mxch-section');
2210
2211 function showSection(targetId) {
2212 // Hide all sections
2213 sections.forEach(section => section.classList.remove('active'));
2214 // Show target section
2215 const target = document.getElementById(targetId);
2216 if (target) {
2217 target.classList.add('active');
2218 }
2219 // Update nav links
2220 navLinks.forEach(link => {
2221 link.classList.toggle('active', link.dataset.target === targetId);
2222 });
2223 // Update URL hash
2224 history.replaceState(null, null, '#' + targetId);
2225 }
2226
2227 navLinks.forEach(link => {
2228 link.addEventListener('click', function(e) {
2229 e.preventDefault();
2230 const targetId = this.dataset.target;
2231 showSection(targetId);
2232 // Close mobile menu if open
2233 const mobileMenu = document.querySelector('.mxch-mobile-menu');
2234 const overlay = document.querySelector('.mxch-mobile-overlay');
2235 if (mobileMenu) mobileMenu.classList.remove('open');
2236 if (overlay) overlay.classList.remove('open');
2237 });
2238 });
2239
2240 // Handle initial hash
2241 const hash = window.location.hash.substring(1);
2242 if (hash && document.getElementById(hash)) {
2243 showSection(hash);
2244 }
2245 }
2246
2247 function initMobileMenu() {
2248 const menuBtn = document.querySelector('.mxch-mobile-menu-btn');
2249 const closeBtn = document.querySelector('.mxch-mobile-menu-close');
2250 const overlay = document.querySelector('.mxch-mobile-overlay');
2251 const menu = document.querySelector('.mxch-mobile-menu');
2252
2253 if (menuBtn && menu) {
2254 menuBtn.addEventListener('click', function() {
2255 menu.classList.add('open');
2256 if (overlay) overlay.classList.add('open');
2257 });
2258 }
2259
2260 if (closeBtn && menu) {
2261 closeBtn.addEventListener('click', function() {
2262 menu.classList.remove('open');
2263 if (overlay) overlay.classList.remove('open');
2264 });
2265 }
2266
2267 if (overlay && menu) {
2268 overlay.addEventListener('click', function() {
2269 menu.classList.remove('open');
2270 overlay.classList.remove('open');
2271 });
2272 }
2273 }
2274
2275 function initImportOptions() {
2276 const importBoxes = document.querySelectorAll('.mxchat-import-box');
2277 const urlInputArea = document.getElementById('mxchat-url-input-area');
2278 const contentInputArea = document.getElementById('mxchat-content-input-area');
2279 const urlInput = document.getElementById('sitemap_url');
2280 const importTypeField = document.getElementById('import_type');
2281 const descriptionText = document.getElementById('url-description-text');
2282
2283 // Sitemap detection elements
2284 const sitemapsLoading = document.getElementById('mxchat-sitemaps-loading');
2285 const detectedSitemaps = document.getElementById('mxchat-detected-sitemaps');
2286 const noSitemaps = document.getElementById('mxchat-no-sitemaps');
2287
2288 // Helper to hide all sitemap detection UI
2289 function hideSitemapDetection() {
2290 if (sitemapsLoading) sitemapsLoading.style.display = 'none';
2291 if (detectedSitemaps) detectedSitemaps.style.display = 'none';
2292 if (noSitemaps) noSitemaps.style.display = 'none';
2293 }
2294
2295 importBoxes.forEach(box => {
2296 box.addEventListener('click', function() {
2297 const option = this.dataset.option;
2298 const type = this.dataset.type;
2299
2300 // Remove active from all boxes
2301 importBoxes.forEach(b => b.classList.remove('active'));
2302 this.classList.add('active');
2303
2304 // Handle different import types
2305 if (option === 'wordpress') {
2306 // WordPress content selector - handled by existing modal
2307 hideSitemapDetection();
2308 return;
2309 }
2310
2311 if (option === 'content') {
2312 if (urlInputArea) urlInputArea.style.display = 'none';
2313 if (contentInputArea) contentInputArea.style.display = 'block';
2314 hideSitemapDetection();
2315 } else if (type === 'sitemap' || type === 'url' || type === 'pdf') {
2316 if (contentInputArea) contentInputArea.style.display = 'none';
2317 if (urlInputArea) urlInputArea.style.display = 'block';
2318
2319 if (urlInput) {
2320 urlInput.placeholder = this.dataset.placeholder || 'Enter URL here';
2321 }
2322 if (importTypeField) {
2323 importTypeField.value = type;
2324 }
2325 if (descriptionText) {
2326 const descriptions = {
2327 'sitemap': '<?php echo esc_js(__('Use a content-specific sub-sitemap (e.g., post-sitemap.xml), not the main sitemap index.', 'mxchat')); ?>',
2328 'url': '<?php echo esc_js(__('Enter the URL of any webpage to import its content.', 'mxchat')); ?>',
2329 'pdf': '<?php echo esc_js(__('Enter the URL of a publicly accessible PDF file.', 'mxchat')); ?>'
2330 };
2331 descriptionText.textContent = descriptions[type] || '';
2332 }
2333
2334 // Only show sitemap detection when Sitemap Import is selected
2335 if (type === 'sitemap') {
2336 // Initialize sitemap detection (function is defined in knowledge-processing.js)
2337 if (typeof window.mxchatInitSitemapDetection === 'function') {
2338 window.mxchatInitSitemapDetection();
2339 }
2340 } else {
2341 hideSitemapDetection();
2342 }
2343 }
2344 });
2345 });
2346 }
2347
2348 function initCustomPostTypesToggle() {
2349 const toggleBtn = document.getElementById('mxchat-custom-post-types-toggle');
2350 const container = document.getElementById('mxchat-custom-post-types-container');
2351
2352 if (toggleBtn && container) {
2353 toggleBtn.addEventListener('click', function() {
2354 const isHidden = container.style.display === 'none';
2355 container.style.display = isHidden ? 'block' : 'none';
2356 const icon = this.querySelector('span:last-child');
2357 if (icon) {
2358 icon.textContent = isHidden ? '' : '';
2359 }
2360 });
2361 }
2362 }
2363
2364 // Sitemap detection is now handled by knowledge-processing.js
2365 // It will be initialized when user clicks "Sitemap Import" option
2366
2367 // ─── Edit Entry Modal ────────────────────────────────────
2368 initEditModal();
2369
2370 function initEditModal() {
2371 var modal = document.getElementById('mxchat-kb-edit-modal');
2372 var textarea = document.getElementById('mxchat-kb-edit-textarea');
2373 var saveBtn = document.getElementById('mxchat-kb-edit-save');
2374 var cancelBtn = document.getElementById('mxchat-kb-edit-cancel');
2375 var closeBtn = document.getElementById('mxchat-kb-edit-close');
2376 var notice = document.getElementById('mxchat-kb-edit-notice');
2377 var charCount = document.getElementById('mxchat-kb-edit-charcount');
2378 var chunkInfo = document.getElementById('mxchat-kb-edit-chunkinfo');
2379 var sourceEl = document.getElementById('mxchat-kb-edit-source');
2380 var titleEl = document.getElementById('mxchat-kb-edit-title');
2381 var saveText = saveBtn ? saveBtn.querySelector('.mxchat-kb-edit-save-text') : null;
2382 var saveSpin = saveBtn ? saveBtn.querySelector('.mxchat-kb-edit-save-spinner') : null;
2383
2384 if (!modal) return;
2385
2386 var currentEntry = {};
2387 var loadedContent = '';
2388
2389 var EDIT_I18N = {
2390 discard: <?php echo wp_json_encode( __('Discard your unsaved changes to this entry?', 'mxchat') ); ?>
2391 };
2392
2393 // Bind edit buttons (delegated)
2394 document.addEventListener('click', function(e) {
2395 var btn = e.target.closest('.mxchat-edit-entry-btn');
2396 if (btn) {
2397 e.preventDefault();
2398 openEditModal(btn);
2399 }
2400 });
2401
2402 // Close handlers. The overlay deliberately has no click-to-close: a click
2403 // fires on the common ancestor of its mousedown and mouseup, so releasing
2404 // a text-selection drag past the dialog edge targets the overlay and would
2405 // discard the edit. Closing is explicit only, and confirms when dirty.
2406 if (closeBtn) closeBtn.addEventListener('click', requestClose);
2407 if (cancelBtn) cancelBtn.addEventListener('click', requestClose);
2408 document.addEventListener('keydown', function(e) {
2409 if (e.key === 'Escape' && modal.classList.contains('active')) requestClose();
2410 });
2411
2412 // Character count
2413 if (textarea) {
2414 textarea.addEventListener('input', function() {
2415 updateCharCount();
2416 });
2417 }
2418
2419 // Save handler
2420 if (saveBtn) saveBtn.addEventListener('click', saveContent);
2421
2422 function openEditModal(btn) {
2423 currentEntry = {
2424 sourceUrl: btn.getAttribute('data-source-url') || '',
2425 entryId: btn.getAttribute('data-entry-id') || '',
2426 dataSource: btn.getAttribute('data-data-source') || 'wordpress',
2427 botId: btn.getAttribute('data-bot-id') || 'default',
2428 nonce: btn.getAttribute('data-nonce') || ''
2429 };
2430
2431 // Reset state
2432 textarea.value = '';
2433 loadedContent = '';
2434 textarea.placeholder = 'Loading content...';
2435 textarea.disabled = true;
2436 saveBtn.disabled = true;
2437 notice.textContent = '';
2438 notice.className = 'mxchat-kb-edit-notice';
2439 charCount.textContent = '';
2440 chunkInfo.textContent = '';
2441
2442 // Show source
2443 if (currentEntry.sourceUrl && currentEntry.sourceUrl.indexOf('mxchat://') !== 0) {
2444 sourceEl.innerHTML = 'Source: <a href="' + escapeHtml(currentEntry.sourceUrl) + '" target="_blank">' + escapeHtml(truncate(currentEntry.sourceUrl, 60)) + '</a>';
2445 } else {
2446 sourceEl.textContent = 'Manual Content';
2447 }
2448
2449 // Show modal
2450 modal.classList.add('active');
2451
2452 // Fetch content
2453 var formData = new FormData();
2454 formData.append('action', 'mxchat_get_entry_content');
2455 formData.append('nonce', currentEntry.nonce);
2456 formData.append('source_url', currentEntry.sourceUrl);
2457 formData.append('entry_id', currentEntry.entryId);
2458 formData.append('data_source', currentEntry.dataSource);
2459 formData.append('bot_id', currentEntry.botId);
2460
2461 fetch(ajaxurl, { method: 'POST', body: formData })
2462 .then(function(r) { return r.json(); })
2463 .then(function(resp) {
2464 if (resp.success) {
2465 textarea.value = resp.data.content || '';
2466 loadedContent = textarea.value;
2467 textarea.disabled = false;
2468 textarea.placeholder = 'Edit your content here...';
2469 saveBtn.disabled = false;
2470 currentEntry.contentType = resp.data.content_type || 'content';
2471
2472 updateCharCount();
2473
2474 if (resp.data.is_chunked) {
2475 chunkInfo.textContent = resp.data.chunk_count + ' chunks — will be re-chunked on save';
2476 titleEl.textContent = 'Edit Knowledge Entry (' + resp.data.chunk_count + ' chunks)';
2477 } else {
2478 chunkInfo.textContent = '';
2479 titleEl.textContent = 'Edit Knowledge Entry';
2480 }
2481
2482 textarea.focus();
2483 } else {
2484 textarea.placeholder = '';
2485 showNotice((resp.data && resp.data.message) || 'Failed to load content.', 'error');
2486 }
2487 })
2488 .catch(function(err) {
2489 textarea.placeholder = '';
2490 showNotice('Network error: ' + err.message, 'error');
2491 });
2492 }
2493
2494 function saveContent() {
2495 if (!textarea.value.trim()) {
2496 showNotice('Content cannot be empty.', 'error');
2497 return;
2498 }
2499
2500 saveBtn.disabled = true;
2501 if (saveText) saveText.style.display = 'none';
2502 if (saveSpin) saveSpin.style.display = '';
2503 showNotice('Saving and re-embedding...', '');
2504
2505 var formData = new FormData();
2506 formData.append('action', 'mxchat_save_entry_content');
2507 formData.append('nonce', currentEntry.nonce);
2508 formData.append('source_url', currentEntry.sourceUrl);
2509 formData.append('entry_id', currentEntry.entryId);
2510 formData.append('content', textarea.value);
2511 formData.append('data_source', currentEntry.dataSource);
2512 formData.append('bot_id', currentEntry.botId);
2513 formData.append('content_type', currentEntry.contentType || 'content');
2514
2515 fetch(ajaxurl, { method: 'POST', body: formData })
2516 .then(function(r) { return r.json(); })
2517 .then(function(resp) {
2518 if (saveText) saveText.style.display = '';
2519 if (saveSpin) saveSpin.style.display = 'none';
2520 saveBtn.disabled = false;
2521
2522 if (resp.success) {
2523 showNotice('Saved successfully!', 'success');
2524 setTimeout(function() {
2525 closeModal();
2526 window.location.reload();
2527 }, 1000);
2528 } else {
2529 showNotice((resp.data && resp.data.message) || 'Save failed.', 'error');
2530 }
2531 })
2532 .catch(function(err) {
2533 if (saveText) saveText.style.display = '';
2534 if (saveSpin) saveSpin.style.display = 'none';
2535 saveBtn.disabled = false;
2536 showNotice('Network error: ' + err.message, 'error');
2537 });
2538 }
2539
2540 function isDirty() {
2541 return !textarea.disabled && textarea.value !== loadedContent;
2542 }
2543
2544 function requestClose() {
2545 if (isDirty() && !window.confirm(EDIT_I18N.discard)) return;
2546 closeModal();
2547 }
2548
2549 function closeModal() {
2550 modal.classList.remove('active');
2551 }
2552
2553 function updateCharCount() {
2554 var len = textarea.value.length;
2555 charCount.textContent = len.toLocaleString() + ' characters';
2556 }
2557
2558 function showNotice(msg, type) {
2559 notice.textContent = msg;
2560 notice.className = 'mxchat-kb-edit-notice' + (type ? ' ' + type : '');
2561 }
2562
2563 function escapeHtml(str) {
2564 var div = document.createElement('div');
2565 div.textContent = str;
2566 return div.innerHTML;
2567 }
2568
2569 function truncate(str, max) {
2570 return str.length > max ? str.substring(0, max) + '...' : str;
2571 }
2572 }
2573
2574 })();
2575 </script>
2576 <?php
2577 }
2578