PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.6
MxChat – AI Chatbot & Content Generation for WordPress v3.2.6
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.6, at includes/admin-knowledge-page.php

2,211 lines 150.5 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('Content Chunking', '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('Content Chunking', '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 navigation JavaScript
280 mxchat_render_knowledge_page_scripts();
281 }
282
283 /**
284 * Render Multi-Bot Selector
285 */
286 function mxchat_render_knowledge_bot_selector($multibot_active, $current_bot_id) {
287 if (!$multibot_active || !class_exists('MxChat_Multi_Bot_Manager')) {
288 return;
289 }
290
291 $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance();
292 $available_bots = $multi_bot_manager->get_available_bots();
293 ?>
294 <div class="mxch-card" style="margin-bottom: 24px;">
295 <div class="mxch-card-body">
296 <div style="display: flex; align-items: center; gap: 15px; flex-wrap: wrap;">
297 <label for="mxchat-bot-selector" style="font-weight: 600; white-space: nowrap;">
298 <?php esc_html_e('Select Bot Database:', 'mxchat'); ?>
299 </label>
300 <select id="mxchat-bot-selector" class="mxch-select" style="min-width: 200px; max-width: 300px;">
301 <?php foreach ($available_bots as $bot_id => $bot_name) : ?>
302 <option value="<?php echo esc_attr($bot_id); ?>" <?php selected($current_bot_id, $bot_id); ?>>
303 <?php echo esc_html($bot_name); ?>
304 </option>
305 <?php endforeach; ?>
306 </select>
307 <span style="color: var(--mxch-text-secondary); font-size: 13px;">
308 <?php esc_html_e('Content will be added to the selected bot\'s knowledge base', 'mxchat'); ?>
309 </span>
310 <span id="mxchat-bot-save-status" style="display: none; color: var(--mxch-success); font-size: 13px;">
311 <?php esc_html_e('Saved', 'mxchat'); ?>
312 </span>
313 </div>
314 </div>
315 </div>
316 <?php
317 }
318
319 /**
320 * Render Global Processing Status - Always visible when processing is active
321 */
322 function mxchat_render_global_processing_status($page_data, $knowledge_manager) {
323 extract($page_data);
324
325 // Only show if there's active processing
326 if (!$is_processing) {
327 return;
328 }
329 ?>
330 <div class="mxch-processing-status-global mxchat-import-section" style="margin-bottom: 24px;">
331 <?php
332 // PDF Processing Status - Using mxchat-status-card class for JavaScript compatibility
333 if ($pdf_status && $pdf_status['status'] === 'processing') : ?>
334 <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;">
335 <div class="mxchat-status-header mxch-card-header" style="background: var(--mxch-primary-light);">
336 <h4 style="margin: 0; display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600;">
337 <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>
338 <?php esc_html_e('PDF Processing Status', 'mxchat'); ?>
339 </h4>
340 <div class="mxchat-status-warning" style="background: #fff3cd; color: #856404; padding: 8px 12px; border-radius: 4px; font-size: 13px; margin: 10px 0;">
341 <?php esc_html_e('Keep this tab open - Processing runs in your browser', 'mxchat'); ?>
342 </div>
343 <form method="post" class="mxchat-stop-form" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
344 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
345 <button type="submit" name="stop_processing" class="mxchat-button-secondary mxch-btn mxch-btn-secondary mxch-btn-sm">
346 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
347 </button>
348 </form>
349 </div>
350 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin: 0;">
351 <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>
352 </div>
353 <div class="mxchat-status-details mxch-card-body">
354 <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>
355 <?php if (!empty($pdf_status['failed_pages']) && $pdf_status['failed_pages'] > 0) : ?>
356 <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>
357 <?php endif; ?>
358 <p><strong><?php esc_html_e('Status:', 'mxchat'); ?></strong> <?php esc_html_e('Processing', 'mxchat'); ?></p>
359 </div>
360 </div>
361 <?php endif;
362
363 // Sitemap Processing Status - Using mxchat-status-card class for JavaScript compatibility
364 if ($sitemap_status && $sitemap_status['status'] === 'processing') : ?>
365 <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;">
366 <div class="mxchat-status-header mxch-card-header" style="background: var(--mxch-primary-light);">
367 <h4 style="margin: 0; display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600;">
368 <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>
369 <?php esc_html_e('Sitemap Processing Status', 'mxchat'); ?>
370 </h4>
371 <div class="mxchat-status-warning" style="background: #fff3cd; color: #856404; padding: 8px 12px; border-radius: 4px; font-size: 13px; margin: 10px 0;">
372 <?php esc_html_e('Keep this tab open - Processing runs in your browser', 'mxchat'); ?>
373 </div>
374 <form method="post" class="mxchat-stop-form" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
375 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
376 <button type="submit" name="stop_processing" class="mxchat-button-secondary mxch-btn mxch-btn-secondary mxch-btn-sm">
377 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
378 </button>
379 </form>
380 </div>
381 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin: 0;">
382 <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>
383 </div>
384 <div class="mxchat-status-details mxch-card-body">
385 <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>
386 <?php if (!empty($sitemap_status['failed_urls']) && $sitemap_status['failed_urls'] > 0) : ?>
387 <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>
388 <?php endif; ?>
389 <p><strong><?php esc_html_e('Status:', 'mxchat'); ?></strong> <?php esc_html_e('Processing', 'mxchat'); ?></p>
390 </div>
391 </div>
392 <?php endif;
393
394 // Completed status cards
395 echo $knowledge_manager->mxchat_render_completed_status_cards();
396 ?>
397 </div>
398 <?php
399 }
400
401 /**
402 * Render Import Options Section
403 */
404 function mxchat_render_import_options_section($admin_instance, $knowledge_manager, $page_data) {
405 extract($page_data);
406 $options = $admin_instance->options ?? get_option('mxchat_options', array());
407
408 // Check if user is using WordPress database (not Pinecone or OpenAI Vector Store)
409 $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
410 $is_pinecone_active = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
411 $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
412 $is_vectorstore_active = ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1';
413 $is_using_wordpress_db = !$is_pinecone_active && !$is_vectorstore_active;
414
415 // Check embedding API key
416 $embedding_model = isset($options['embedding_model']) ? esc_attr($options['embedding_model']) : 'text-embedding-ada-002';
417 $has_openai_key = !empty($options['api_key']);
418 $has_voyage_key = !empty($options['voyage_api_key']);
419 $has_gemini_key = !empty($options['gemini_api_key']);
420
421 $has_required_key = false;
422 $required_key_type = '';
423
424 if (strpos($embedding_model, 'text-embedding-') !== false && $has_openai_key) {
425 $has_required_key = true;
426 $required_key_type = 'OpenAI';
427 } elseif (strpos($embedding_model, 'voyage-') !== false && $has_voyage_key) {
428 $has_required_key = true;
429 $required_key_type = 'Voyage AI';
430 } elseif (strpos($embedding_model, 'gemini-embedding-') !== false && $has_gemini_key) {
431 $has_required_key = true;
432 $required_key_type = 'Google Gemini';
433 } elseif (strpos($embedding_model, 'text-embedding-') !== false) {
434 $required_key_type = 'OpenAI';
435 } elseif (strpos($embedding_model, 'voyage-') !== false) {
436 $required_key_type = 'Voyage AI';
437 } elseif (strpos($embedding_model, 'gemini-embedding-') !== false) {
438 $required_key_type = 'Google Gemini';
439 }
440 ?>
441 <div id="import-options" class="mxch-section active">
442 <div class="mxch-content-header">
443 <h1 class="mxch-content-title"><?php esc_html_e('Import Options', 'mxchat'); ?></h1>
444 <p class="mxch-content-subtitle"><?php esc_html_e('Import content to your knowledge base from various sources.', 'mxchat'); ?></p>
445 </div>
446
447 <!-- API Key Status -->
448 <div class="mxch-notice <?php echo $has_required_key ? 'mxch-notice-success' : 'mxch-notice-warning'; ?>" style="margin-bottom: 24px;">
449 <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">
450 <?php if ($has_required_key): ?>
451 <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/>
452 <?php else: ?>
453 <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"/>
454 <?php endif; ?>
455 </svg>
456 <div>
457 <?php if ($has_required_key): ?>
458 <?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)); ?>
459 <?php else: ?>
460 <strong><?php esc_html_e('Important:', 'mxchat'); ?></strong>
461 <?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); ?>
462 <a href="<?php echo admin_url('admin.php?page=mxchat-max#api-keys'); ?>"><?php esc_html_e('Go to API Key Settings', 'mxchat'); ?></a>
463 <?php endif; ?>
464 </div>
465 </div>
466
467 <?php if ($is_using_wordpress_db): ?>
468 <!-- WordPress Database Caution Notice -->
469 <div class="mxch-notice mxch-notice-warning" style="margin-bottom: 24px;">
470 <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">
471 <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"/>
472 </svg>
473 <div>
474 <strong><?php esc_html_e('Using WordPress Database:', 'mxchat'); ?></strong>
475 <?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'); ?>
476 </div>
477 </div>
478 <?php endif; ?>
479
480 <div class="mxch-card">
481 <div class="mxch-card-header">
482 <h3 class="mxch-card-title">
483 <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>
484 <?php esc_html_e('Choose Import Method', 'mxchat'); ?>
485 </h3>
486 </div>
487 <div class="mxch-card-body">
488 <!-- Import Options Grid -->
489 <div class="mxchat-import-options">
490 <!-- WordPress Import Option -->
491 <button type="button" id="mxchat-open-content-selector" class="mxchat-import-box mxchat-import-wordpress" data-option="wordpress">
492 <div class="mxchat-import-icon">
493 <span class="dashicons dashicons-wordpress"></span>
494 </div>
495 <div class="mxchat-import-content">
496 <h4><?php esc_html_e('WordPress Content', 'mxchat'); ?></h4>
497 <p><?php esc_html_e('Import specific posts and pages to your knowledge base.', 'mxchat'); ?></p>
498 </div>
499 <div class="mxchat-recommended-tag"><?php esc_html_e('Recommended', 'mxchat'); ?></div>
500 </button>
501
502 <!-- Sitemap Import Option -->
503 <button type="button" class="mxchat-import-box" data-option="sitemap" data-placeholder="<?php esc_attr_e('Enter sitemap URL here', 'mxchat'); ?>" data-type="sitemap">
504 <div class="mxchat-import-icon">
505 <span class="dashicons dashicons-admin-site-alt"></span>
506 </div>
507 <div class="mxchat-import-content">
508 <h4><?php esc_html_e('Sitemap Import', 'mxchat'); ?></h4>
509 <p><?php esc_html_e('Use a content-specific sub-sitemap, not the sitemap index.', 'mxchat'); ?></p>
510 </div>
511 </button>
512
513 <!-- Direct URL Import Option -->
514 <button type="button" class="mxchat-import-box" data-option="url" data-placeholder="<?php esc_attr_e('Enter webpage URL here', 'mxchat'); ?>" data-type="url">
515 <div class="mxchat-import-icon">
516 <span class="dashicons dashicons-admin-links"></span>
517 </div>
518 <div class="mxchat-import-content">
519 <h4><?php esc_html_e('Direct URL', 'mxchat'); ?></h4>
520 <p><?php esc_html_e('Import content from any webpage.', 'mxchat'); ?></p>
521 </div>
522 </button>
523
524 <!-- Direct Content Import Option -->
525 <button type="button" class="mxchat-import-box" data-option="content" data-type="content">
526 <div class="mxchat-import-icon">
527 <span class="dashicons dashicons-editor-paste-text"></span>
528 </div>
529 <div class="mxchat-import-content">
530 <h4><?php esc_html_e('Direct Content', 'mxchat'); ?></h4>
531 <p><?php esc_html_e('Submit content to be vectorized.', 'mxchat'); ?></p>
532 </div>
533 </button>
534
535 <!-- PDF Import Option (URL) -->
536 <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">
537 <div class="mxchat-import-icon">
538 <span class="dashicons dashicons-media-document"></span>
539 </div>
540 <div class="mxchat-import-content">
541 <h4><?php esc_html_e('PDF Import', 'mxchat'); ?></h4>
542 <p><?php esc_html_e('Import knowledge from PDF URL.', 'mxchat'); ?></p>
543 </div>
544 </button>
545
546 <!-- PDF File Upload Option -->
547 <button type="button" class="mxchat-import-box" data-option="pdf-upload" data-type="pdf-upload">
548 <div class="mxchat-import-icon">
549 <span class="dashicons dashicons-upload"></span>
550 </div>
551 <div class="mxchat-import-content">
552 <h4><?php esc_html_e('PDF Upload', 'mxchat'); ?></h4>
553 <p><?php esc_html_e('Upload a PDF file from your computer.', 'mxchat'); ?></p>
554 </div>
555 </button>
556 </div>
557
558 <!-- Detected Sitemaps Section (hidden by default, shown when Sitemap Import is clicked) -->
559 <div id="mxchat-detected-sitemaps" class="mxchat-detected-sitemaps" style="display: none;">
560 <div class="mxchat-sitemaps-header">
561 <h4>
562 <span class="dashicons dashicons-admin-site-alt"></span>
563 <?php esc_html_e('Detected Sitemaps', 'mxchat'); ?>
564 </h4>
565 <button type="button" id="mxchat-refresh-sitemaps" class="mxch-btn mxch-btn-ghost mxch-btn-sm" title="<?php esc_attr_e('Refresh', 'mxchat'); ?>">
566 <span class="dashicons dashicons-update"></span>
567 </button>
568 </div>
569 <div id="mxchat-sitemaps-list" class="mxchat-sitemaps-list">
570 <!-- Sitemaps will be loaded here via AJAX -->
571 </div>
572 <input type="hidden" id="mxchat-detect-sitemaps-nonce" value="<?php echo wp_create_nonce('mxchat_detect_sitemaps_nonce'); ?>">
573 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
574 <input type="hidden" id="mxchat-sitemap-bot-id" value="<?php echo esc_attr($current_bot_id); ?>">
575 <?php endif; ?>
576 </div>
577
578 <!-- No Sitemaps Found Message (hidden by default) -->
579 <div id="mxchat-no-sitemaps" class="mxchat-no-sitemaps" style="display: none;">
580 <span class="dashicons dashicons-info"></span>
581 <p>
582 <?php esc_html_e('No sitemaps detected. You can manually enter a sitemap URL using the input field above.', 'mxchat'); ?>
583 </p>
584 </div>
585
586 <!-- Sitemaps Loading State (hidden by default) -->
587 <div id="mxchat-sitemaps-loading" class="mxchat-sitemaps-loading" style="display: none;">
588 <span class="dashicons dashicons-update spin"></span>
589 <p><?php esc_html_e('Detecting sitemaps...', 'mxchat'); ?></p>
590 </div>
591
592 <!-- Input Areas for URL and Content -->
593 <?php if (!$is_processing) : ?>
594 <div class="mxchat-import-input-area" id="mxchat-url-input-area" style="display: none; margin-top: 20px;">
595 <form id="mxchat-url-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_sitemap')); ?>">
596 <?php wp_nonce_field('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce'); ?>
597 <input type="hidden" name="import_type" id="import_type" value="url">
598 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
599 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
600 <?php endif; ?>
601 <div style="display: flex; gap: 10px;">
602 <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;" />
603 <button type="submit" name="submit_sitemap" class="mxch-btn mxch-btn-primary">
604 <?php esc_html_e('Import', 'mxchat'); ?>
605 </button>
606 </div>
607 <p class="mxch-field-description" id="url-description-text"></p>
608 </form>
609 </div>
610
611 <div class="mxchat-import-input-area" id="mxchat-content-input-area" style="display: none; margin-top: 20px;">
612 <form id="mxchat-content-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_content')); ?>">
613 <?php wp_nonce_field('mxchat_submit_content_action', 'mxchat_submit_content_nonce'); ?>
614 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
615 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
616 <?php endif; ?>
617 <div class="mxch-field">
618 <textarea name="article_content" id="article_content" class="mxch-textarea" placeholder="<?php esc_attr_e('Enter your content here...', 'mxchat'); ?>" required rows="6"></textarea>
619 </div>
620 <div class="mxch-field">
621 <input type="url" name="article_url" id="article_url" class="mxch-input" placeholder="<?php esc_attr_e('Enter source URL (Optional)', 'mxchat'); ?>">
622 <div style="margin-top: 8px; display: flex; align-items: center; gap: 8px;">
623 <label style="display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--mxch-text-secondary); cursor: pointer;">
624 <input type="checkbox" id="mxchat-unique-url-toggle" style="margin: 0;">
625 <?php esc_html_e('Generate unique URL for duplicate content', 'mxchat'); ?>
626 </label>
627 <button type="button" id="mxchat-generate-unique-url" class="mxch-btn mxch-btn-secondary mxch-btn-sm" style="display: none;">
628 <span class="dashicons dashicons-randomize" style="font-size: 14px; margin-top: 2px;"></span>
629 <?php esc_html_e('Generate Unique', 'mxchat'); ?>
630 </button>
631 </div>
632 <p class="mxch-field-description">
633 <?php esc_html_e('Enable this option to submit multiple entries with the same base URL. A unique reference will be appended.', 'mxchat'); ?>
634 </p>
635 </div>
636 <button type="submit" name="submit_content" class="mxch-btn mxch-btn-primary">
637 <?php esc_html_e('Import Content', 'mxchat'); ?>
638 </button>
639 </form>
640 </div>
641
642 <div class="mxchat-import-input-area" id="mxchat-pdf-upload-area" style="display: none; margin-top: 20px;">
643 <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">
644 <?php wp_nonce_field('mxchat_submit_pdf_file_action', 'mxchat_submit_pdf_file_nonce'); ?>
645 <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
646 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
647 <?php endif; ?>
648 <div class="mxch-field">
649 <input type="file" name="pdf_file" id="mxchat-pdf-file-input" accept=".pdf" required>
650 </div>
651 <p class="mxch-field-description">
652 <?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'); ?>
653 </p>
654 <button type="submit" name="submit_pdf_file" class="mxch-btn mxch-btn-primary">
655 <?php esc_html_e('Import PDF', 'mxchat'); ?>
656 </button>
657 </form>
658 </div>
659 <?php endif; ?>
660 </div>
661 </div>
662 </div>
663 <?php
664 }
665
666 /**
667 * Render Processing Status Cards (legacy - kept for reference)
668 * Note: Processing status is now rendered globally via mxchat_render_global_processing_status()
669 */
670 function mxchat_render_processing_status($page_data, $knowledge_manager) {
671 extract($page_data);
672
673 // PDF Processing Status
674 if ($pdf_status && $pdf_status['status'] === 'processing') : ?>
675 <div class="mxch-card" style="margin-top: 20px;">
676 <div class="mxch-card-header" style="background: var(--mxch-primary-light);">
677 <h3 class="mxch-card-title"><?php esc_html_e('PDF Processing Status', 'mxchat'); ?></h3>
678 <div style="display: flex; gap: 10px;">
679 <form method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
680 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
681 <button type="submit" name="stop_processing" class="mxch-btn mxch-btn-secondary mxch-btn-sm">
682 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
683 </button>
684 </form>
685 <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')); ?>">
686 <?php esc_html_e('Process Batch', 'mxchat'); ?>
687 </button>
688 </div>
689 </div>
690 <div class="mxch-card-body">
691 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin-bottom: 15px;">
692 <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>
693 </div>
694 <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>
695 <?php if (!empty($pdf_status['failed_pages']) && $pdf_status['failed_pages'] > 0) : ?>
696 <p><strong><?php esc_html_e('Failed pages:', 'mxchat'); ?></strong> <?php echo esc_html($pdf_status['failed_pages']); ?></p>
697 <?php endif; ?>
698 <p><strong><?php esc_html_e('Last update:', 'mxchat'); ?></strong> <?php echo esc_html($pdf_status['last_update']); ?></p>
699 </div>
700 </div>
701 <?php endif;
702
703 // Sitemap Processing Status
704 if ($sitemap_status && $sitemap_status['status'] === 'processing') : ?>
705 <div class="mxch-card" style="margin-top: 20px;">
706 <div class="mxch-card-header" style="background: var(--mxch-primary-light);">
707 <h3 class="mxch-card-title"><?php esc_html_e('Sitemap Processing Status', 'mxchat'); ?></h3>
708 <div style="display: flex; gap: 10px;">
709 <form method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_stop_processing')); ?>">
710 <?php wp_nonce_field('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce'); ?>
711 <button type="submit" name="stop_processing" class="mxch-btn mxch-btn-secondary mxch-btn-sm">
712 <?php esc_html_e('Stop Processing', 'mxchat'); ?>
713 </button>
714 </form>
715 <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')); ?>">
716 <?php esc_html_e('Process Batch', 'mxchat'); ?>
717 </button>
718 </div>
719 </div>
720 <div class="mxch-card-body">
721 <div class="mxchat-progress-bar" style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin-bottom: 15px;">
722 <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>
723 </div>
724 <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>
725 <?php if (!empty($sitemap_status['failed_urls']) && $sitemap_status['failed_urls'] > 0) : ?>
726 <p><strong><?php esc_html_e('Failed URLs:', 'mxchat'); ?></strong> <?php echo esc_html($sitemap_status['failed_urls']); ?></p>
727 <?php endif; ?>
728 <p><strong><?php esc_html_e('Last update:', 'mxchat'); ?></strong> <?php echo esc_html($sitemap_status['last_update']); ?></p>
729 </div>
730 </div>
731 <?php endif;
732
733 // Completed status cards
734 echo $knowledge_manager->mxchat_render_completed_status_cards();
735 }
736
737 /**
738 * Render Knowledge Base Section (table)
739 */
740 function mxchat_render_knowledge_base_section($admin_instance, $knowledge_manager, $page_data) {
741 extract($page_data);
742
743 // Group prompts by source_url for chunked content display
744 $grouped_prompts = array();
745
746 if ($prompts) {
747 foreach ($prompts as $prompt) {
748 $source_url = $prompt->source_url ?? '';
749
750 // Check if chunk metadata exists on the record itself (Pinecone)
751 if (isset($prompt->chunk_index) && $prompt->chunk_index !== null) {
752 // Pinecone: metadata is already on the record object
753 $prompt->chunk_metadata = array(
754 'chunk_index' => intval($prompt->chunk_index),
755 'total_chunks' => isset($prompt->total_chunks) ? intval($prompt->total_chunks) : null,
756 'is_chunked' => isset($prompt->is_chunked) ? (bool) $prompt->is_chunked : true
757 );
758 $prompt->display_content = $prompt->article_content; // Pinecone content is already clean
759 } else {
760 // WordPress: Parse chunk metadata from JSON prefix in article_content
761 if (class_exists('MxChat_Chunker')) {
762 $chunk_meta = MxChat_Chunker::parse_stored_chunk($prompt->article_content);
763 $prompt->chunk_metadata = $chunk_meta['metadata'];
764 $prompt->display_content = $chunk_meta['text']; // Store clean content without JSON prefix
765 } else {
766 $prompt->chunk_metadata = array();
767 $prompt->display_content = $prompt->article_content;
768 }
769 }
770
771 if (!empty($source_url)) {
772 if (!isset($grouped_prompts[$source_url])) {
773 $grouped_prompts[$source_url] = array();
774 }
775 $grouped_prompts[$source_url][] = $prompt;
776 } else {
777 // Use a unique key for ungrouped prompts
778 $grouped_prompts['_ungrouped_' . $prompt->id] = array($prompt);
779 }
780 }
781
782 // Sort each group by chunk_index from metadata
783 foreach ($grouped_prompts as $source_url => &$group) {
784 usort($group, function($a, $b) {
785 $index_a = isset($a->chunk_metadata['chunk_index']) ? intval($a->chunk_metadata['chunk_index']) : 0;
786 $index_b = isset($b->chunk_metadata['chunk_index']) ? intval($b->chunk_metadata['chunk_index']) : 0;
787 return $index_a - $index_b;
788 });
789 }
790 unset($group); // Break reference
791 }
792 $display_index = 0;
793 ?>
794 <div id="knowledge-base" class="mxch-section">
795 <div class="mxch-content-header">
796 <h1 class="mxch-content-title"><?php esc_html_e('Knowledge Base', 'mxchat'); ?></h1>
797 <p class="mxch-content-subtitle"><?php esc_html_e('View and manage your imported knowledge entries.', 'mxchat'); ?></p>
798 </div>
799
800 <?php if (!empty($use_vectorstore)) : ?>
801 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
802 <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>
803 <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>
804 </div>
805 <?php endif; ?>
806
807 <div class="mxch-card">
808 <div class="mxch-card-header">
809 <h3 class="mxch-card-title">
810 <?php esc_html_e('Knowledge Entries', 'mxchat'); ?>
811 <span id="mxchat-entry-count" style="font-weight: normal; color: var(--mxch-text-secondary);">(<?php echo esc_html($total_records); ?>)</span>
812 <?php if (!empty($use_vectorstore)) : ?>
813 <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;">
814 <span class="dashicons dashicons-database" style="font-size: 14px;"></span>
815 <?php esc_html_e('Not in use', 'mxchat'); ?>
816 </span>
817 <?php elseif ($use_pinecone) : ?>
818 <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;">
819 <span class="dashicons dashicons-cloud" style="font-size: 14px;"></span>
820 <?php esc_html_e('Pinecone', 'mxchat'); ?>
821 </span>
822 <?php else : ?>
823 <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;">
824 <span class="dashicons dashicons-database-view" style="font-size: 14px;"></span>
825 <?php esc_html_e('WordPress DB', 'mxchat'); ?>
826 </span>
827 <?php endif; ?>
828 </h3>
829 <div class="mxch-kb-header-controls">
830 <form method="get" id="knowledge-search" class="mxch-kb-search-form">
831 <?php wp_nonce_field('mxchat_prompts_search_nonce'); ?>
832 <input type="hidden" name="page" value="mxchat-prompts" />
833 <?php if ($multibot_active && !empty($current_bot_id) && $current_bot_id !== 'default') : ?>
834 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>" />
835 <?php endif; ?>
836 <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); ?>" />
837 <select name="content_type" class="mxch-select mxch-kb-type-filter" onchange="this.form.submit()">
838 <option value=""><?php esc_html_e('All Types', 'mxchat'); ?></option>
839 <?php
840 $post_types = get_post_types(array('public' => true), 'objects');
841 foreach ($post_types as $post_type) {
842 echo '<option value="' . esc_attr($post_type->name) . '" ' . selected($content_type_filter, $post_type->name, false) . '>' . esc_html($post_type->label) . '</option>';
843 }
844 ?>
845 <option value="pdf" <?php selected($content_type_filter, 'pdf'); ?>><?php esc_html_e('PDFs', 'mxchat'); ?></option>
846 <option value="url" <?php selected($content_type_filter, 'url'); ?>><?php esc_html_e('URLs', 'mxchat'); ?></option>
847 </select>
848 </form>
849 <!-- Delete Button Container - transforms between Delete All and Delete Selected -->
850 <div class="mxchat-delete-container" id="mxchat-delete-container">
851 <!-- Delete All Form (shown when nothing selected) -->
852 <?php
853 // Build confirm message and button text based on active filter
854 $delete_all_label = __('Delete All', 'mxchat');
855 $delete_all_confirm = __('Are you sure you want to delete all knowledge?', 'mxchat');
856 if (!empty($content_type_filter)) {
857 $filter_display = $content_type_filter;
858 // Try to get a human-readable label
859 $pt_obj = get_post_type_object($content_type_filter);
860 if ($pt_obj) {
861 $filter_display = $pt_obj->label;
862 } elseif ($content_type_filter === 'pdf') {
863 $filter_display = 'PDFs';
864 } elseif ($content_type_filter === 'url') {
865 $filter_display = 'URLs';
866 }
867 /* translators: %s: content type label (e.g. "Pages", "PDFs") */
868 $delete_all_label = sprintf(__('Delete All %s', 'mxchat'), $filter_display);
869 /* translators: %s: content type label */
870 $delete_all_confirm = sprintf(__('Are you sure you want to delete all %s from the knowledge base?', 'mxchat'), $filter_display);
871 }
872 ?>
873 <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); ?>');">
874 <?php wp_nonce_field('mxchat_delete_all_prompts_action', 'mxchat_delete_all_prompts_nonce'); ?>
875 <input type="hidden" name="data_source" value="<?php echo esc_attr($data_source); ?>" />
876 <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>" />
877 <?php if (!empty($content_type_filter)) : ?>
878 <input type="hidden" name="content_type_filter" value="<?php echo esc_attr($content_type_filter); ?>" />
879 <?php endif; ?>
880 <button type="submit" class="mxch-btn mxch-btn-secondary mxch-btn-sm" style="color: var(--mxch-error);">
881 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
882 <?php echo esc_html($delete_all_label); ?>
883 </button>
884 </form>
885 <!-- Delete Selected Button (shown when items selected) -->
886 <button type="button"
887 class="mxch-btn mxch-btn-secondary mxch-btn-sm mxchat-bulk-delete"
888 id="mxchat-delete-selected-entries"
889 style="display: none; color: var(--mxch-error);"
890 data-nonce="<?php echo wp_create_nonce('mxchat_bulk_delete_knowledge_nonce'); ?>"
891 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
892 data-data-source="<?php echo esc_attr($data_source); ?>">
893 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
894 <span class="mxchat-bulk-delete-text"><?php esc_html_e('Delete Selected', 'mxchat'); ?></span>
895 <span class="mxchat-selected-count" id="mxchat-selected-entry-count"></span>
896 </button>
897 </div>
898 </div>
899 </div>
900 <div class="mxch-card-body" style="padding: 0;">
901 <?php if ($use_pinecone) : ?>
902 <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;">
903 <div style="display: flex; align-items: center; gap: 8px;">
904 <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>
905 <span><?php esc_html_e('Changes may take up to 60 seconds to process in Pinecone. Click "Refresh Entries" to see updates.', 'mxchat'); ?></span>
906 </div>
907 <button type="button" id="mxchat-refresh-pinecone-entries" class="mxch-btn mxch-btn-secondary mxch-btn-sm">
908 <span class="dashicons dashicons-update"></span>
909 <?php esc_html_e('Refresh Entries', 'mxchat'); ?>
910 </button>
911 </div>
912 <?php endif; ?>
913
914 <div class="mxchat-table-wrapper" style="overflow-x: auto;">
915 <table class="mxchat-records-table" id="mxchat-records-table" style="width: 100%; border-collapse: collapse;">
916 <thead>
917 <tr style="background: #f8fafc; border-bottom: 1px solid var(--mxch-card-border);">
918 <th style="padding: 12px 16px; width: 40px; text-align: center;">
919 <input type="checkbox" id="mxchat-select-all-entries" class="mxchat-entry-checkbox-all" title="<?php esc_attr_e('Select All', 'mxchat'); ?>">
920 </th>
921 <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>
922 <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>
923 <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>
924 <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>
925 </tr>
926 </thead>
927 <tbody id="mxchat-entries-tbody">
928 <?php if (empty($grouped_prompts)) : ?>
929 <tr>
930 <td colspan="5" style="padding: 40px; text-align: center; color: var(--mxch-text-muted);">
931 <?php esc_html_e('No knowledge entries found. Use the Import Options to add content.', 'mxchat'); ?>
932 </td>
933 </tr>
934 <?php else : ?>
935 <?php
936 // Display grouped prompts
937 foreach ($grouped_prompts as $source_url => $group) :
938 $chunk_count = count($group);
939 $first_prompt = $group[0];
940 $display_index++;
941
942 if ($chunk_count > 1) :
943 // Multiple chunks - show grouped row with expand button
944 $group_id = 'group-' . md5($source_url);
945 ?>
946 <tr id="prompt-<?php echo esc_attr($first_prompt->id); ?>"
947 class="mxchat-chunk-group-header"
948 data-source="<?php echo esc_attr($data_source); ?>"
949 data-group-id="<?php echo esc_attr($group_id); ?>"
950 style="border-bottom: 1px solid var(--mxch-card-border); <?php if ($data_source === 'pinecone') echo 'background: rgba(33, 150, 243, 0.02);'; ?>">
951 <td style="padding: 12px 16px; text-align: center;">
952 <input type="checkbox"
953 class="mxchat-entry-checkbox"
954 data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
955 data-source="<?php echo esc_attr($data_source); ?>"
956 data-source-url="<?php echo esc_attr($source_url); ?>"
957 data-is-group="true"
958 data-chunk-count="<?php echo esc_attr($chunk_count); ?>">
959 </td>
960 <td style="padding: 12px 16px; font-size: 13px;">
961 <?php if ($data_source === 'pinecone') : ?>
962 <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
963 <?php else : ?>
964 <?php echo esc_html($first_prompt->id); ?>
965 <?php endif; ?>
966 </td>
967 <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
968 <div class="mxchat-chunk-group-info">
969 <button type="button" class="mxchat-chunk-toggle" data-group-id="<?php echo esc_attr($group_id); ?>">
970 <span class="dashicons dashicons-arrow-right-alt2"></span>
971 </button>
972 <span class="mxchat-chunk-badge"><?php echo esc_html($chunk_count); ?> <?php esc_html_e('chunks', 'mxchat'); ?></span>
973 <span class="mxchat-chunk-preview">
974 <?php
975 $parent_content = isset($first_prompt->display_content) ? $first_prompt->display_content : $first_prompt->article_content;
976 $content_preview = mb_substr($parent_content, 0, 100);
977 echo esc_html($content_preview . '...');
978 ?>
979 </span>
980 </div>
981 </td>
982 <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
983 <?php if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0 && strpos($source_url, '_ungrouped_') !== 0) : ?>
984 <a href="<?php echo esc_url($source_url); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
985 <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
986 <?php esc_html_e('View Source', 'mxchat'); ?>
987 </a>
988 <?php else : ?>
989 <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
990 <?php endif; ?>
991 </td>
992 <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
993 <?php if ($data_source !== 'pinecone') : ?>
994 <button type="button"
995 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
996 data-source-url="<?php echo esc_attr($source_url); ?>"
997 data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
998 data-data-source="<?php echo esc_attr($data_source); ?>"
999 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1000 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1001 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1002 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1003 </button>
1004 <?php endif; ?>
1005 <button type="button"
1006 class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
1007 data-source-url="<?php echo esc_attr($source_url); ?>"
1008 data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
1009 data-data-source="<?php echo esc_attr($data_source); ?>"
1010 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1011 data-nonce="<?php echo wp_create_nonce('mxchat_delete_chunks_nonce'); ?>"
1012 style="color: var(--mxch-error);"
1013 title="<?php esc_attr_e('Delete all chunks', 'mxchat'); ?>">
1014 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1015 </button>
1016 </td>
1017 </tr>
1018 <?php
1019 // Render hidden chunk rows
1020 foreach ($group as $chunk_index => $prompt) :
1021 $meta_chunk_index = isset($prompt->chunk_metadata['chunk_index']) ? intval($prompt->chunk_metadata['chunk_index']) : $chunk_index;
1022 $meta_total_chunks = isset($prompt->chunk_metadata['total_chunks']) ? intval($prompt->chunk_metadata['total_chunks']) : $chunk_count;
1023 $content = isset($prompt->display_content) ? $prompt->display_content : $prompt->article_content;
1024 $preview_length = 150;
1025 $content_preview = mb_strlen($content) > $preview_length
1026 ? mb_substr($content, 0, $preview_length) . '...'
1027 : $content;
1028 ?>
1029 <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
1030 class="mxchat-chunk-row <?php echo esc_attr($group_id); ?>"
1031 data-source="<?php echo esc_attr($data_source); ?>"
1032 style="display: none; background: #f8f9fa; border-bottom: 1px solid var(--mxch-card-border);">
1033 <td style="padding: 12px 16px; text-align: center;">
1034 <!-- Checkbox placeholder for chunk rows -->
1035 </td>
1036 <td style="padding: 12px 16px 12px 30px; font-size: 13px;">
1037 <!-- Hidden ID column for chunks -->
1038 </td>
1039 <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
1040 <div class="mxchat-accordion-wrapper">
1041 <div class="mxchat-content-preview">
1042 <span class="mxchat-chunk-indicator" style="margin-right: 10px; color: var(--mxch-text-secondary); font-size: 12px;">
1043 <?php printf(esc_html__('Chunk %d of %d', 'mxchat'), $meta_chunk_index + 1, $meta_total_chunks); ?>
1044 </span>
1045 <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
1046 <?php if (mb_strlen($content) > $preview_length) : ?>
1047 <button class="mxchat-expand-toggle" type="button">
1048 <span class="dashicons dashicons-arrow-down-alt2"></span>
1049 </button>
1050 <?php endif; ?>
1051 </div>
1052 <div class="mxchat-content-full" style="display: none;">
1053 <div class="content-view">
1054 <?php
1055 if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
1056 echo '<div dir="rtl" lang="he" class="rtl-content">';
1057 echo wp_kses_post(wpautop($content));
1058 echo '</div>';
1059 } else {
1060 echo wp_kses_post(wpautop($content));
1061 }
1062 ?>
1063 </div>
1064 </div>
1065 </div>
1066 </td>
1067 <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
1068 <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Same as parent', 'mxchat'); ?></span>
1069 </td>
1070 <td class="mxchat-actions-cell" style="padding: 12px 16px;">
1071 <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Managed by group', 'mxchat'); ?></span>
1072 </td>
1073 </tr>
1074 <?php endforeach; ?>
1075 <?php else :
1076 // Single entry - display normally with accordion
1077 $prompt = $first_prompt;
1078 $content = isset($prompt->display_content) ? $prompt->display_content : $prompt->article_content;
1079 $preview_length = 150;
1080 $content_preview = mb_strlen($content) > $preview_length
1081 ? mb_substr($content, 0, $preview_length) . '...'
1082 : $content;
1083 ?>
1084 <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
1085 data-source="<?php echo esc_attr($data_source); ?>"
1086 style="border-bottom: 1px solid var(--mxch-card-border); <?php if ($data_source === 'pinecone') echo 'background: rgba(33, 150, 243, 0.02);'; ?>">
1087 <td style="padding: 12px 16px; text-align: center;">
1088 <input type="checkbox"
1089 class="mxchat-entry-checkbox"
1090 data-entry-id="<?php echo esc_attr($prompt->id); ?>"
1091 data-source="<?php echo esc_attr($data_source); ?>"
1092 data-source-url="<?php echo esc_attr($source_url); ?>"
1093 data-is-group="false">
1094 </td>
1095 <td style="padding: 12px 16px; font-size: 13px;">
1096 <?php if ($data_source === 'pinecone') : ?>
1097 <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
1098 <?php else : ?>
1099 <?php echo esc_html($prompt->id); ?>
1100 <?php endif; ?>
1101 </td>
1102 <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
1103 <div class="mxchat-accordion-wrapper">
1104 <div class="mxchat-content-preview">
1105 <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
1106 <?php if (mb_strlen($content) > $preview_length) : ?>
1107 <button class="mxchat-expand-toggle" type="button">
1108 <span class="dashicons dashicons-arrow-down-alt2"></span>
1109 </button>
1110 <?php endif; ?>
1111 </div>
1112 <div class="mxchat-content-full" style="display: none;">
1113 <div class="content-view">
1114 <?php
1115 if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
1116 echo '<div dir="rtl" lang="he" class="rtl-content">';
1117 echo wp_kses_post(wpautop($content));
1118 echo '</div>';
1119 } else {
1120 echo wp_kses_post(wpautop($content));
1121 }
1122 ?>
1123 </div>
1124 </div>
1125 </div>
1126 </td>
1127 <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
1128 <?php
1129 $actual_source = $source_url;
1130 if (strpos($source_url, '_ungrouped_') === 0) {
1131 $actual_source = $prompt->source_url ?? '';
1132 }
1133 if (!empty($actual_source) && strpos($actual_source, 'mxchat://') !== 0) : ?>
1134 <a href="<?php echo esc_url($actual_source); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
1135 <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
1136 <?php esc_html_e('View', 'mxchat'); ?>
1137 </a>
1138 <?php else : ?>
1139 <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
1140 <?php endif; ?>
1141 </td>
1142 <td style="padding: 12px 16px; white-space: nowrap;">
1143 <?php if ($data_source !== 'pinecone') : ?>
1144 <button type="button"
1145 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
1146 data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
1147 data-entry-id="<?php echo esc_attr($prompt->id); ?>"
1148 data-data-source="<?php echo esc_attr($data_source); ?>"
1149 data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1150 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1151 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1152 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1153 </button>
1154 <?php endif; ?>
1155 <?php if ($data_source === 'pinecone') : ?>
1156 <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);">
1157 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1158 </button>
1159 <?php else : ?>
1160 <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);">
1161 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1162 </button>
1163 <?php endif; ?>
1164 </td>
1165 </tr>
1166 <?php endif; ?>
1167 <?php endforeach; ?>
1168 <?php endif; ?>
1169 </tbody>
1170 </table>
1171 </div>
1172
1173 <!-- Pagination wrapper - always present so JS can populate it after processing -->
1174 <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); ?>">
1175 <?php if ($total_pages > 1) : ?>
1176 <div class="mxchat-ajax-pagination" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>">
1177 <?php if ($current_page > 1) : ?>
1178 <a href="#" class="mxchat-page-link" data-page="<?php echo ($current_page - 1); ?>"><?php esc_html_e('&laquo; Previous', 'mxchat'); ?></a>
1179 <?php endif; ?>
1180
1181 <?php
1182 $start_page = max(1, $current_page - 2);
1183 $end_page = min($total_pages, $current_page + 2);
1184
1185 if ($start_page > 1) {
1186 echo '<a href="#" class="mxchat-page-link" data-page="1">1</a> ';
1187 if ($start_page > 2) {
1188 echo '<span class="mxchat-page-dots">...</span> ';
1189 }
1190 }
1191
1192 for ($i = $start_page; $i <= $end_page; $i++) {
1193 if ($i == $current_page) {
1194 echo '<span class="mxchat-page-current">' . $i . '</span> ';
1195 } else {
1196 echo '<a href="#" class="mxchat-page-link" data-page="' . $i . '">' . $i . '</a> ';
1197 }
1198 }
1199
1200 if ($end_page < $total_pages) {
1201 if ($end_page < $total_pages - 1) {
1202 echo '<span class="mxchat-page-dots">...</span> ';
1203 }
1204 echo '<a href="#" class="mxchat-page-link" data-page="' . $total_pages . '">' . $total_pages . '</a> ';
1205 }
1206 ?>
1207
1208 <?php if ($current_page < $total_pages) : ?>
1209 <a href="#" class="mxchat-page-link" data-page="<?php echo ($current_page + 1); ?>"><?php esc_html_e('Next &raquo;', 'mxchat'); ?></a>
1210 <?php endif; ?>
1211 </div>
1212 <?php endif; ?>
1213 </div>
1214 </div>
1215 </div>
1216 </div>
1217 <?php
1218 }
1219
1220 /**
1221 * Render Auto-Sync Section
1222 */
1223 function mxchat_render_auto_sync_section($knowledge_manager) {
1224 ?>
1225 <div id="auto-sync" class="mxch-section">
1226 <div class="mxch-content-header">
1227 <h1 class="mxch-content-title"><?php esc_html_e('Auto-Sync Settings', 'mxchat'); ?></h1>
1228 <p class="mxch-content-subtitle"><?php esc_html_e('Automatically sync WordPress content to your knowledge base when published or updated.', 'mxchat'); ?></p>
1229 </div>
1230
1231 <div class="mxch-card">
1232 <div class="mxch-card-body mxchat-autosave-section">
1233 <div class="mxch-field">
1234 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 16px;">
1235 <label class="mxchat-toggle-switch">
1236 <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'); ?>>
1237 <span class="mxchat-toggle-slider"></span>
1238 </label>
1239 <span style="font-weight: 500;"><?php esc_html_e('Auto-sync Posts', 'mxchat'); ?></span>
1240 </div>
1241 <div style="display: flex; align-items: center; gap: 12px;">
1242 <label class="mxchat-toggle-switch">
1243 <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'); ?>>
1244 <span class="mxchat-toggle-slider"></span>
1245 </label>
1246 <span style="font-weight: 500;"><?php esc_html_e('Auto-sync Pages', 'mxchat'); ?></span>
1247 </div>
1248 </div>
1249
1250 <div class="mxch-field" style="margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--mxch-card-border);">
1251 <div style="display: flex; align-items: center; gap: 12px;">
1252 <label class="mxchat-toggle-switch">
1253 <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'); ?>>
1254 <span class="mxchat-toggle-slider"></span>
1255 </label>
1256 <span style="font-weight: 500;"><?php esc_html_e('Extract text from PDFs in ACF fields on save', 'mxchat'); ?></span>
1257 </div>
1258 <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>
1259 </div>
1260
1261 <div style="margin-top: 24px;">
1262 <button id="mxchat-custom-post-types-toggle" class="mxch-btn mxch-btn-secondary">
1263 <?php esc_html_e('Advanced Custom Post Sync Settings', 'mxchat'); ?>
1264 <span style="margin-left: 5px;"></span>
1265 </button>
1266 <div id="mxchat-custom-post-types-container" style="display: none; margin-top: 16px; padding: 16px; background: #f8fafc; border-radius: var(--mxch-radius-md);">
1267 <h4 style="margin: 0 0 12px 0;"><?php esc_html_e('Sync Custom Post Types', 'mxchat'); ?></h4>
1268 <?php
1269 $post_types = $knowledge_manager->mxchat_get_public_post_types();
1270 unset($post_types['post'], $post_types['page']);
1271
1272 if (!empty($post_types)) {
1273 foreach ($post_types as $post_type => $label) {
1274 $option_name = 'mxchat_auto_sync_' . $post_type;
1275 $is_enabled = get_option($option_name, '0');
1276 ?>
1277 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 8px;">
1278 <label class="mxchat-toggle-switch">
1279 <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'); ?>>
1280 <span class="mxchat-toggle-slider"></span>
1281 </label>
1282 <span><?php echo esc_html($label); ?> (<?php echo esc_html($post_type); ?>)</span>
1283 </div>
1284 <?php
1285 }
1286 } else {
1287 echo '<p style="color: var(--mxch-text-muted);">' . esc_html__('No custom post types found.', 'mxchat') . '</p>';
1288 }
1289 ?>
1290 </div>
1291 </div>
1292 </div>
1293 </div>
1294 </div>
1295 <?php
1296 }
1297
1298 /**
1299 * Render Chunking Section
1300 */
1301 function mxchat_render_chunking_section() {
1302 $chunking_settings = MxChat_Chunker::get_settings();
1303 $chunking_enabled = $chunking_settings['chunking_enabled'];
1304 $chunk_size = $chunking_settings['chunk_size'];
1305 ?>
1306 <div id="chunking" class="mxch-section">
1307 <div class="mxch-content-header">
1308 <h1 class="mxch-content-title"><?php esc_html_e('Content Chunking', 'mxchat'); ?></h1>
1309 <p class="mxch-content-subtitle"><?php esc_html_e('Split large content into smaller segments for more accurate semantic search.', 'mxchat'); ?></p>
1310 </div>
1311
1312 <div class="mxch-card">
1313 <div class="mxch-card-body mxchat-autosave-section">
1314 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1315 <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>
1316 <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>
1317 </div>
1318
1319 <div class="mxch-field">
1320 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
1321 <label class="mxchat-toggle-switch">
1322 <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); ?>>
1323 <span class="mxchat-toggle-slider"></span>
1324 </label>
1325 <span style="font-weight: 500;"><?php esc_html_e('Enable Content Chunking', 'mxchat'); ?></span>
1326 </div>
1327 </div>
1328
1329 <div class="mxch-field">
1330 <label class="mxch-field-label" for="mxchat_chunk_size"><?php esc_html_e('Chunk Size (characters)', 'mxchat'); ?></label>
1331 <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;">
1332 <p class="mxch-field-description"><?php esc_html_e('Recommended: 4000 characters (~1000 tokens). Range: 1000-10000. Larger chunks preserve more context.', 'mxchat'); ?></p>
1333 </div>
1334 </div>
1335 </div>
1336 </div>
1337 <?php
1338 }
1339
1340 /**
1341 * Render Role Restrictions Section
1342 */
1343 function mxchat_render_role_restrictions_section($knowledge_manager) {
1344 $role_options = array(
1345 'public' => __('Public (Everyone)', 'mxchat'),
1346 'logged_in' => __('Logged In Users', 'mxchat'),
1347 'subscriber' => __('Subscribers & Above', 'mxchat'),
1348 'contributor' => __('Contributors & Above', 'mxchat'),
1349 'author' => __('Authors & Above', 'mxchat'),
1350 'editor' => __('Editors & Above', 'mxchat'),
1351 'administrator' => __('Administrators Only', 'mxchat')
1352 );
1353 ?>
1354 <div id="role-restrictions" class="mxch-section">
1355 <div class="mxch-content-header">
1356 <h1 class="mxch-content-title"><?php esc_html_e('Role-Based Content Restrictions', 'mxchat'); ?></h1>
1357 <p class="mxch-content-subtitle"><?php esc_html_e('Automatically restrict content access based on WordPress tags.', 'mxchat'); ?></p>
1358 </div>
1359
1360 <div class="mxch-card">
1361 <div class="mxch-card-body">
1362 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1363 <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>
1364 <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>
1365 </div>
1366
1367 <h4 style="margin: 0 0 16px 0;"><?php esc_html_e('Add Tag-Role Mapping', 'mxchat'); ?></h4>
1368 <div style="display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 24px;">
1369 <div class="mxch-field" style="flex: 1; min-width: 200px; margin-bottom: 0;">
1370 <label class="mxch-field-label" for="mxchat-tag-input"><?php esc_html_e('Tag Name', 'mxchat'); ?></label>
1371 <input type="text" id="mxchat-tag-input" class="mxch-input" placeholder="<?php esc_attr_e('e.g., premium, members-only', 'mxchat'); ?>">
1372 </div>
1373 <div class="mxch-field" style="flex: 1; min-width: 200px; margin-bottom: 0;">
1374 <label class="mxch-field-label" for="mxchat-role-select"><?php esc_html_e('Required Role', 'mxchat'); ?></label>
1375 <select id="mxchat-role-select" class="mxch-select">
1376 <?php foreach ($role_options as $role_key => $role_label) : ?>
1377 <option value="<?php echo esc_attr($role_key); ?>"><?php echo esc_html($role_label); ?></option>
1378 <?php endforeach; ?>
1379 </select>
1380 </div>
1381 <button type="button" id="mxchat-add-tag-role" class="mxch-btn mxch-btn-primary">
1382 <span class="dashicons dashicons-plus-alt" style="font-size: 16px;"></span>
1383 <?php esc_html_e('Add Mapping', 'mxchat'); ?>
1384 </button>
1385 </div>
1386
1387 <h4 style="margin: 24px 0 16px 0;"><?php esc_html_e('Current Tag-Role Mappings', 'mxchat'); ?></h4>
1388 <div id="mxchat-mappings-container">
1389 <div class="mxchat-loading-mappings" style="text-align: center; padding: 20px; color: var(--mxch-text-muted);">
1390 <?php esc_html_e('Loading mappings...', 'mxchat'); ?>
1391 </div>
1392 </div>
1393 <div id="mxchat-no-mappings" style="display: none; text-align: center; padding: 40px; color: var(--mxch-text-muted);">
1394 <span class="dashicons dashicons-tag" style="font-size: 48px; opacity: 0.3;"></span>
1395 <p><?php esc_html_e('No tag-role mappings yet. Add your first mapping above.', 'mxchat'); ?></p>
1396 </div>
1397
1398 <div style="margin-top: 30px; padding-top: 24px; border-top: 1px solid var(--mxch-card-border);">
1399 <h4 style="margin: 0 0 12px 0;"><?php esc_html_e('Bulk Update Existing Content', 'mxchat'); ?></h4>
1400 <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>
1401 <button type="button" id="mxchat-bulk-update-roles" class="mxch-btn mxch-btn-secondary">
1402 <span class="dashicons dashicons-update" style="font-size: 16px;"></span>
1403 <?php esc_html_e('Update All Existing Content', 'mxchat'); ?>
1404 </button>
1405 <div id="mxchat-bulk-update-progress" style="display: none; margin-top: 15px;">
1406 <div style="height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden;">
1407 <div class="mxchat-progress-fill" style="height: 100%; background: var(--mxch-primary); width: 0%; transition: width 0.3s;"></div>
1408 </div>
1409 <p class="mxchat-progress-text" style="margin-top: 10px; color: var(--mxch-text-secondary);"><?php esc_html_e('Processing...', 'mxchat'); ?></p>
1410 </div>
1411 <div id="mxchat-bulk-update-result" style="display: none; margin-top: 15px;"></div>
1412 </div>
1413 </div>
1414 </div>
1415 </div>
1416 <?php
1417 }
1418
1419 /**
1420 * Render ACF Fields Section
1421 */
1422 function mxchat_render_acf_fields_section($knowledge_manager) {
1423 $excluded_fields = get_option('mxchat_acf_excluded_fields', array());
1424 if (!is_array($excluded_fields)) {
1425 $excluded_fields = array();
1426 }
1427 ?>
1428 <div id="acf-fields" class="mxch-section">
1429 <div class="mxch-content-header">
1430 <h1 class="mxch-content-title"><?php esc_html_e('ACF Field Settings', 'mxchat'); ?></h1>
1431 <p class="mxch-content-subtitle"><?php esc_html_e('Control which Advanced Custom Fields are included in knowledge base embeddings.', 'mxchat'); ?></p>
1432 </div>
1433
1434 <div class="mxch-card">
1435 <div class="mxch-card-body mxchat-autosave-section">
1436 <?php if (!function_exists('acf_get_field_groups')): ?>
1437 <div class="mxch-notice mxch-notice-info">
1438 <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>
1439 <span><?php esc_html_e('Advanced Custom Fields (ACF) plugin is not detected. Install and activate ACF to use this feature.', 'mxchat'); ?></span>
1440 </div>
1441 <?php else:
1442 $all_acf_fields = $knowledge_manager->mxchat_get_all_acf_fields();
1443
1444 if (empty($all_acf_fields)): ?>
1445 <div class="mxch-notice mxch-notice-info">
1446 <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>
1447 <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>
1448 </div>
1449 <?php else: ?>
1450 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1451 <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>
1452 <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>
1453 </div>
1454
1455 <?php foreach ($all_acf_fields as $group_title => $fields): ?>
1456 <div class="mxchat-acf-field-group" style="margin-bottom: 24px;">
1457 <h4 style="margin: 0 0 12px 0; color: var(--mxch-text-primary); font-weight: 600;">
1458 <?php echo esc_html($group_title); ?>
1459 </h4>
1460 <div style="background: #f8fafc; border-radius: var(--mxch-radius-md); padding: 16px;">
1461 <?php foreach ($fields as $field):
1462 $field_name = $field['name'];
1463 $is_enabled = !in_array($field_name, $excluded_fields);
1464 ?>
1465 <div style="display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #e2e8f0;">
1466 <div>
1467 <span style="font-weight: 500;"><?php echo esc_html($field['label']); ?></span>
1468 <span style="color: var(--mxch-text-muted); font-size: 12px; margin-left: 8px;">(<?php echo esc_html($field_name); ?>)</span>
1469 <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>
1470 </div>
1471 <label class="mxchat-toggle-switch">
1472 <input type="checkbox"
1473 name="mxchat_acf_field_<?php echo esc_attr($field_name); ?>"
1474 class="mxchat-autosave-field"
1475 value="on"
1476 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1477 <?php checked($is_enabled, true); ?>>
1478 <span class="mxchat-toggle-slider"></span>
1479 </label>
1480 </div>
1481 <?php endforeach; ?>
1482 </div>
1483 </div>
1484 <?php endforeach; ?>
1485 <?php endif; ?>
1486 <?php endif; ?>
1487 </div>
1488 </div>
1489 </div>
1490 <?php
1491 }
1492
1493 /**
1494 * Render Custom Meta Section
1495 */
1496 function mxchat_render_custom_meta_section() {
1497 $whitelist = get_option('mxchat_custom_meta_whitelist', '');
1498 ?>
1499 <div id="custom-meta" class="mxch-section">
1500 <div class="mxch-content-header">
1501 <h1 class="mxch-content-title"><?php esc_html_e('Custom Post Meta', 'mxchat'); ?></h1>
1502 <p class="mxch-content-subtitle"><?php esc_html_e('Include custom post meta fields (non-ACF) in knowledge base embeddings.', 'mxchat'); ?></p>
1503 </div>
1504
1505 <div class="mxch-card">
1506 <div class="mxch-card-body mxchat-autosave-section">
1507 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1508 <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>
1509 <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>
1510 </div>
1511
1512 <div class="mxch-field">
1513 <label class="mxch-field-label" for="mxchat_custom_meta_whitelist">
1514 <?php esc_html_e('Meta Key Whitelist', 'mxchat'); ?>
1515 </label>
1516 <textarea
1517 name="mxchat_custom_meta_whitelist"
1518 id="mxchat_custom_meta_whitelist"
1519 class="mxch-textarea mxchat-autosave-field"
1520 rows="6"
1521 placeholder="speaker_profession&#10;speaker_company&#10;event_location&#10;_custom_field_key"
1522 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1523 style="font-family: monospace;"
1524 ><?php echo esc_textarea($whitelist); ?></textarea>
1525 <p class="mxch-field-description">
1526 <?php esc_html_e('Enter one meta key per line. These fields will be appended to the content during embedding. Only string values are included.', 'mxchat'); ?>
1527 </p>
1528 </div>
1529
1530 <div style="margin-top: 20px; padding: 16px; background: #f8fafc; border-radius: var(--mxch-radius-md);">
1531 <h4 style="margin: 0 0 12px 0; font-size: 14px;"><?php esc_html_e('How to find meta keys:', 'mxchat'); ?></h4>
1532 <ul style="margin: 0; padding-left: 20px; color: var(--mxch-text-secondary); font-size: 13px;">
1533 <li><?php esc_html_e('Check your theme documentation for meta key names', 'mxchat'); ?></li>
1534 <li><?php esc_html_e('Look in the wp_postmeta database table', 'mxchat'); ?></li>
1535 <li><?php esc_html_e('Use a plugin like "Show Post Meta" to view meta keys on any post', 'mxchat'); ?></li>
1536 <li><?php esc_html_e('Meta keys starting with underscore (_) are usually hidden/internal', 'mxchat'); ?></li>
1537 </ul>
1538 </div>
1539 </div>
1540 </div>
1541 </div>
1542 <?php
1543 }
1544
1545 /**
1546 * Render Pinecone Section
1547 */
1548 function mxchat_render_pinecone_section() {
1549 $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
1550 $use_pinecone = $pinecone_options['mxchat_use_pinecone'] ?? '0';
1551 ?>
1552 <div id="pinecone" class="mxch-section">
1553 <div class="mxch-content-header">
1554 <h1 class="mxch-content-title"><?php esc_html_e('Pinecone Vector Database', 'mxchat'); ?></h1>
1555 <p class="mxch-content-subtitle"><?php esc_html_e('Configure Pinecone for enhanced search performance with larger knowledge bases.', 'mxchat'); ?></p>
1556 </div>
1557
1558 <div class="mxch-card">
1559 <div class="mxch-card-body">
1560 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1561 <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>
1562 <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>
1563 </div>
1564
1565 <form method="post" action="options.php">
1566 <?php settings_fields('mxchat_pinecone_addon_options'); ?>
1567
1568 <div class="mxch-field">
1569 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
1570 <label class="mxchat-toggle-switch">
1571 <input type="checkbox" name="mxchat_pinecone_addon_options[mxchat_use_pinecone]" value="1" <?php checked($use_pinecone, '1'); ?>>
1572 <span class="mxchat-toggle-slider"></span>
1573 </label>
1574 <span style="font-weight: 500;"><?php esc_html_e('Enable Pinecone Database', 'mxchat'); ?></span>
1575 </div>
1576 </div>
1577
1578 <div class="mxchat-pinecone-settings" <?php echo $use_pinecone ? '' : 'style="display: none;"'; ?>>
1579 <?php if ($use_pinecone) : ?>
1580 <div class="mxch-notice mxch-notice-success" style="margin-bottom: 20px;">
1581 <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>
1582 <span><?php esc_html_e('Pinecone is enabled. All new knowledge base content will be stored in Pinecone.', 'mxchat'); ?></span>
1583 </div>
1584 <?php endif; ?>
1585
1586 <div class="mxch-field">
1587 <label class="mxch-field-label" for="mxchat_pinecone_api_key">
1588 <?php esc_html_e('Pinecone API Key', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1589 </label>
1590 <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_...">
1591 <p class="mxch-field-description">
1592 <?php esc_html_e('Found in your Pinecone dashboard under API Keys.', 'mxchat'); ?>
1593 <a href="https://app.pinecone.io/" target="_blank"><?php esc_html_e('Open Pinecone Dashboard', 'mxchat'); ?></a>
1594 </p>
1595 </div>
1596
1597 <div class="mxch-field">
1598 <label class="mxch-field-label" for="mxchat_pinecone_environment"><?php esc_html_e('Region', 'mxchat'); ?></label>
1599 <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">
1600 <p class="mxch-field-description"><?php esc_html_e('Your Pinecone environment/region (e.g., gcp-starter, us-west1-gcp)', 'mxchat'); ?></p>
1601 </div>
1602
1603 <div class="mxch-field">
1604 <label class="mxch-field-label" for="mxchat_pinecone_index">
1605 <?php esc_html_e('Index Name', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1606 </label>
1607 <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">
1608 <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>
1609 </div>
1610
1611 <div class="mxch-field">
1612 <label class="mxch-field-label" for="mxchat_pinecone_host">
1613 <?php esc_html_e('Pinecone Host', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1614 </label>
1615 <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">
1616 <p class="mxch-field-description"><?php esc_html_e('The hostname from your Pinecone index URL (exclude https://). Found in index details.', 'mxchat'); ?></p>
1617 </div>
1618 </div>
1619
1620 <?php submit_button(__('Save Pinecone Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1621 </form>
1622 </div>
1623 </div>
1624 </div>
1625 <?php
1626 }
1627
1628 /**
1629 * Render OpenAI Vector Store Section
1630 */
1631 function mxchat_render_openai_vectorstore_section() {
1632 $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
1633 $use_vectorstore = $vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0';
1634 $vectorstore_ids = $vectorstore_options['mxchat_vectorstore_ids'] ?? '';
1635 $max_results = $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5;
1636
1637 // Get current chat model to check compatibility
1638 $mxchat_options = get_option('mxchat_options', array());
1639 $current_model = $mxchat_options['model'] ?? 'gpt-5.1-chat-latest';
1640 $is_openai_model = preg_match('/^(gpt-|o1-|o3-)/', $current_model);
1641 ?>
1642 <div id="openai-vectorstore" class="mxch-section">
1643 <div class="mxch-content-header">
1644 <h1 class="mxch-content-title"><?php esc_html_e('OpenAI Vector Store', 'mxchat'); ?></h1>
1645 <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>
1646 </div>
1647
1648 <div class="mxch-card">
1649 <div class="mxch-card-body">
1650 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1651 <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>
1652 <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.1-chat-latest, gpt-5-mini, etc.). Create Vector Stores in your OpenAI Dashboard.', 'mxchat'); ?></span>
1653 </div>
1654
1655 <?php if (!$is_openai_model && $use_vectorstore === '1'): ?>
1656 <div class="mxch-notice mxch-notice-warning" style="margin-bottom: 20px;">
1657 <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>
1658 <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>
1659 </div>
1660 <?php endif; ?>
1661
1662 <form method="post" action="options.php" id="mxchat-vectorstore-form">
1663 <?php settings_fields('mxchat_openai_vectorstore_options'); ?>
1664
1665 <div class="mxch-field">
1666 <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
1667 <label class="mxchat-toggle-switch">
1668 <input type="checkbox" name="mxchat_openai_vectorstore_options[mxchat_use_openai_vectorstore]" value="1" <?php checked($use_vectorstore, '1'); ?> id="mxchat_use_openai_vectorstore">
1669 <span class="mxchat-toggle-slider"></span>
1670 </label>
1671 <span style="font-weight: 500;"><?php esc_html_e('Enable OpenAI Vector Store', 'mxchat'); ?></span>
1672 </div>
1673 </div>
1674
1675 <div class="mxchat-vectorstore-settings" <?php echo $use_vectorstore === '1' ? '' : 'style="display: none;"'; ?>>
1676 <?php if ($use_vectorstore === '1'): ?>
1677 <div class="mxch-notice mxch-notice-success" style="margin-bottom: 20px;">
1678 <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>
1679 <span><?php esc_html_e('OpenAI Vector Store is enabled. Queries will search your Vector Store for relevant content.', 'mxchat'); ?></span>
1680 </div>
1681 <?php endif; ?>
1682
1683 <div class="mxch-field">
1684 <label class="mxch-field-label" for="mxchat_vectorstore_ids">
1685 <?php esc_html_e('Vector Store ID(s)', 'mxchat'); ?> <span class="mxch-field-label-required">*</span>
1686 </label>
1687 <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">
1688 <p class="mxch-field-description">
1689 <?php esc_html_e('Enter your Vector Store ID from the OpenAI Dashboard. For multiple stores, separate with commas.', 'mxchat'); ?>
1690 <a href="https://platform.openai.com/storage/vector_stores" target="_blank"><?php esc_html_e('Open OpenAI Vector Store Dashboard', 'mxchat'); ?></a>
1691 </p>
1692 </div>
1693
1694 <div class="mxch-field">
1695 <label class="mxch-field-label" for="mxchat_vectorstore_max_results">
1696 <?php esc_html_e('Max Results', 'mxchat'); ?>
1697 </label>
1698 <select id="mxchat_vectorstore_max_results" name="mxchat_openai_vectorstore_options[mxchat_vectorstore_max_results]" class="mxch-input" style="width: auto;">
1699 <?php for ($i = 1; $i <= 10; $i++): ?>
1700 <option value="<?php echo $i; ?>" <?php selected($max_results, $i); ?>><?php echo $i; ?></option>
1701 <?php endfor; ?>
1702 </select>
1703 <p class="mxch-field-description"><?php esc_html_e('Maximum number of content chunks to retrieve from the Vector Store.', 'mxchat'); ?></p>
1704 </div>
1705 </div>
1706
1707 <?php submit_button(__('Save Vector Store Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1708 </form>
1709 </div>
1710 </div>
1711 </div>
1712 <?php
1713 }
1714
1715 /**
1716 * Render Content Selector Modal
1717 */
1718 function mxchat_render_content_selector_modal() {
1719 ?>
1720 <div id="mxchat-kb-content-selector-modal" class="mxchat-kb-modal">
1721 <div class="mxchat-kb-modal-content">
1722 <div class="mxchat-kb-modal-header">
1723 <h3>
1724 <?php esc_html_e('Select WordPress Content', 'mxchat'); ?><br>
1725 <span class="mxchat-kb-header-note"><?php esc_html_e('(Content imported here will be tagged "In Knowledge Base")', 'mxchat'); ?></span>
1726 </h3>
1727 <span class="mxchat-kb-modal-close">&times;</span>
1728 </div>
1729 <div class="mxchat-kb-modal-filters">
1730 <div class="mxchat-kb-search-group">
1731 <input type="text" id="mxchat-kb-content-search" placeholder="<?php esc_attr_e('Search...', 'mxchat'); ?>">
1732 </div>
1733 <div class="mxchat-kb-filter-group">
1734 <select id="mxchat-kb-content-type-filter">
1735 <option value="all"><?php esc_html_e('All Content Types', 'mxchat'); ?></option>
1736 <option value="post"><?php esc_html_e('Posts', 'mxchat'); ?></option>
1737 <option value="page"><?php esc_html_e('Pages', 'mxchat'); ?></option>
1738 <?php
1739 $post_types = get_post_types(array('public' => true), 'objects');
1740 foreach ($post_types as $post_type) {
1741 if (!in_array($post_type->name, array('post', 'page'))) {
1742 echo '<option value="' . esc_attr($post_type->name) . '">' . esc_html($post_type->label) . '</option>';
1743 }
1744 }
1745 ?>
1746 </select>
1747 <select id="mxchat-kb-content-status-filter">
1748 <option value="publish"><?php esc_html_e('Published', 'mxchat'); ?></option>
1749 <option value="draft"><?php esc_html_e('Drafts', 'mxchat'); ?></option>
1750 <option value="all"><?php esc_html_e('All Statuses', 'mxchat'); ?></option>
1751 </select>
1752 <select id="mxchat-kb-processed-filter">
1753 <option value="all"><?php esc_html_e('All Content', 'mxchat'); ?></option>
1754 <option value="processed"><?php esc_html_e('In Knowledge Base', 'mxchat'); ?></option>
1755 <option value="unprocessed"><?php esc_html_e('Not In Knowledge Base', 'mxchat'); ?></option>
1756 </select>
1757 </div>
1758 </div>
1759 <div class="mxchat-kb-content-selection">
1760 <div class="mxchat-kb-selection-header">
1761 <label>
1762 <input type="checkbox" id="mxchat-kb-select-all">
1763 <?php esc_html_e('Select All on Page', 'mxchat'); ?>
1764 </label>
1765 <span class="mxchat-kb-selection-count">0 <?php esc_html_e('selected', 'mxchat'); ?></span>
1766 <span class="mxchat-kb-selection-hint" style="margin-left: auto; font-size: 12px; color: var(--mxch-text-muted);">
1767 <span class="dashicons dashicons-info-outline" style="font-size: 14px; vertical-align: middle;"></span>
1768 <?php esc_html_e('Selections persist across pages', 'mxchat'); ?>
1769 </span>
1770 </div>
1771 <div class="mxchat-kb-content-list">
1772 <div class="mxchat-kb-loading">
1773 <span class="mxchat-kb-spinner is-active"></span>
1774 <?php esc_html_e('Loading content...', 'mxchat'); ?>
1775 </div>
1776 </div>
1777 </div>
1778 <div class="mxchat-kb-modal-footer">
1779 <div class="mxchat-kb-pagination"></div>
1780 <div class="mxchat-kb-acf-pdf-option" style="display:flex; align-items:flex-start; gap:8px; padding:8px 0; font-size:12px; color: var(--mxch-text-secondary, #64748b);">
1781 <input type="checkbox" id="mxchat-kb-acf-pdf-extract" style="margin-top:2px;">
1782 <label for="mxchat-kb-acf-pdf-extract" style="cursor:pointer; line-height:1.4;">
1783 <strong style="color: var(--mxch-text-primary, #1a1a2e);"><?php esc_html_e('Also extract text from PDFs linked in ACF File/Image fields', 'mxchat'); ?></strong><br>
1784 <span><?php esc_html_e('Adds 200-500ms per post and ~30 KB to each KB entry. Recommended only if your ACF setup uses PDF attachments for primary content.', 'mxchat'); ?></span>
1785 </label>
1786 </div>
1787 <div class="mxchat-kb-footer-actions">
1788 <button type="button" id="mxchat-kb-process-selected" class="mxchat-kb-button-primary" disabled>
1789 <?php esc_html_e('Process Selected Content', 'mxchat'); ?>
1790 <span class="mxchat-kb-selected-count">(0)</span>
1791 </button>
1792 <button type="button" class="mxchat-kb-button-secondary mxchat-kb-modal-close">
1793 <?php esc_html_e('Cancel', 'mxchat'); ?>
1794 </button>
1795 </div>
1796 </div>
1797 </div>
1798 </div>
1799 <?php
1800 }
1801
1802 /**
1803 * Render Edit Entry Modal
1804 */
1805 function mxchat_render_edit_entry_modal() {
1806 ?>
1807 <div id="mxchat-kb-edit-modal" class="mxchat-kb-modal">
1808 <div class="mxchat-kb-modal-content mxchat-kb-edit-modal-content">
1809 <div class="mxchat-kb-modal-header">
1810 <h3 id="mxchat-kb-edit-title"><?php esc_html_e('Edit Knowledge Entry', 'mxchat'); ?></h3>
1811 <span class="mxchat-kb-modal-close" id="mxchat-kb-edit-close">&times;</span>
1812 </div>
1813 <div class="mxchat-kb-edit-body">
1814 <div class="mxchat-kb-edit-source" id="mxchat-kb-edit-source"></div>
1815 <textarea id="mxchat-kb-edit-textarea" class="mxchat-kb-edit-textarea" placeholder="<?php esc_attr_e('Loading content...', 'mxchat'); ?>"></textarea>
1816 <div class="mxchat-kb-edit-meta">
1817 <span id="mxchat-kb-edit-charcount"></span>
1818 <span id="mxchat-kb-edit-chunkinfo"></span>
1819 </div>
1820 </div>
1821 <div class="mxchat-kb-edit-footer">
1822 <div class="mxchat-kb-edit-notice" id="mxchat-kb-edit-notice"></div>
1823 <div class="mxchat-kb-edit-actions">
1824 <button type="button" id="mxchat-kb-edit-cancel" class="mxch-btn mxch-btn-ghost">
1825 <?php esc_html_e('Cancel', 'mxchat'); ?>
1826 </button>
1827 <button type="button" id="mxchat-kb-edit-save" class="mxch-btn mxch-btn-primary">
1828 <span class="mxchat-kb-edit-save-text"><?php esc_html_e('Save & Re-embed', 'mxchat'); ?></span>
1829 <span class="mxchat-kb-edit-save-spinner" style="display:none;">
1830 <span class="mxchat-kb-spinner is-active" style="margin: 0;"></span>
1831 <?php esc_html_e('Saving...', 'mxchat'); ?>
1832 </span>
1833 </button>
1834 </div>
1835 </div>
1836 </div>
1837 </div>
1838 <?php
1839 }
1840
1841 /**
1842 * Render Knowledge Page Navigation Scripts
1843 */
1844 function mxchat_render_knowledge_page_scripts() {
1845 ?>
1846 <script>
1847 (function() {
1848 'use strict';
1849
1850 // Wait for DOM
1851 document.addEventListener('DOMContentLoaded', function() {
1852 initKnowledgeNavigation();
1853 initMobileMenu();
1854 initImportOptions();
1855 initCustomPostTypesToggle();
1856 // Sitemap detection is now initialized when user clicks the Sitemap Import option
1857 });
1858
1859 function initKnowledgeNavigation() {
1860 const navLinks = document.querySelectorAll('.mxch-nav-link[data-target], .mxch-mobile-nav-link[data-target]');
1861 const sections = document.querySelectorAll('.mxch-section');
1862
1863 function showSection(targetId) {
1864 // Hide all sections
1865 sections.forEach(section => section.classList.remove('active'));
1866 // Show target section
1867 const target = document.getElementById(targetId);
1868 if (target) {
1869 target.classList.add('active');
1870 }
1871 // Update nav links
1872 navLinks.forEach(link => {
1873 link.classList.toggle('active', link.dataset.target === targetId);
1874 });
1875 // Update URL hash
1876 history.replaceState(null, null, '#' + targetId);
1877 }
1878
1879 navLinks.forEach(link => {
1880 link.addEventListener('click', function(e) {
1881 e.preventDefault();
1882 const targetId = this.dataset.target;
1883 showSection(targetId);
1884 // Close mobile menu if open
1885 const mobileMenu = document.querySelector('.mxch-mobile-menu');
1886 const overlay = document.querySelector('.mxch-mobile-overlay');
1887 if (mobileMenu) mobileMenu.classList.remove('open');
1888 if (overlay) overlay.classList.remove('open');
1889 });
1890 });
1891
1892 // Handle initial hash
1893 const hash = window.location.hash.substring(1);
1894 if (hash && document.getElementById(hash)) {
1895 showSection(hash);
1896 }
1897 }
1898
1899 function initMobileMenu() {
1900 const menuBtn = document.querySelector('.mxch-mobile-menu-btn');
1901 const closeBtn = document.querySelector('.mxch-mobile-menu-close');
1902 const overlay = document.querySelector('.mxch-mobile-overlay');
1903 const menu = document.querySelector('.mxch-mobile-menu');
1904
1905 if (menuBtn && menu) {
1906 menuBtn.addEventListener('click', function() {
1907 menu.classList.add('open');
1908 if (overlay) overlay.classList.add('open');
1909 });
1910 }
1911
1912 if (closeBtn && menu) {
1913 closeBtn.addEventListener('click', function() {
1914 menu.classList.remove('open');
1915 if (overlay) overlay.classList.remove('open');
1916 });
1917 }
1918
1919 if (overlay && menu) {
1920 overlay.addEventListener('click', function() {
1921 menu.classList.remove('open');
1922 overlay.classList.remove('open');
1923 });
1924 }
1925 }
1926
1927 function initImportOptions() {
1928 const importBoxes = document.querySelectorAll('.mxchat-import-box');
1929 const urlInputArea = document.getElementById('mxchat-url-input-area');
1930 const contentInputArea = document.getElementById('mxchat-content-input-area');
1931 const urlInput = document.getElementById('sitemap_url');
1932 const importTypeField = document.getElementById('import_type');
1933 const descriptionText = document.getElementById('url-description-text');
1934
1935 // Sitemap detection elements
1936 const sitemapsLoading = document.getElementById('mxchat-sitemaps-loading');
1937 const detectedSitemaps = document.getElementById('mxchat-detected-sitemaps');
1938 const noSitemaps = document.getElementById('mxchat-no-sitemaps');
1939
1940 // Helper to hide all sitemap detection UI
1941 function hideSitemapDetection() {
1942 if (sitemapsLoading) sitemapsLoading.style.display = 'none';
1943 if (detectedSitemaps) detectedSitemaps.style.display = 'none';
1944 if (noSitemaps) noSitemaps.style.display = 'none';
1945 }
1946
1947 importBoxes.forEach(box => {
1948 box.addEventListener('click', function() {
1949 const option = this.dataset.option;
1950 const type = this.dataset.type;
1951
1952 // Remove active from all boxes
1953 importBoxes.forEach(b => b.classList.remove('active'));
1954 this.classList.add('active');
1955
1956 // Handle different import types
1957 if (option === 'wordpress') {
1958 // WordPress content selector - handled by existing modal
1959 hideSitemapDetection();
1960 return;
1961 }
1962
1963 if (option === 'content') {
1964 if (urlInputArea) urlInputArea.style.display = 'none';
1965 if (contentInputArea) contentInputArea.style.display = 'block';
1966 hideSitemapDetection();
1967 } else if (type === 'sitemap' || type === 'url' || type === 'pdf') {
1968 if (contentInputArea) contentInputArea.style.display = 'none';
1969 if (urlInputArea) urlInputArea.style.display = 'block';
1970
1971 if (urlInput) {
1972 urlInput.placeholder = this.dataset.placeholder || 'Enter URL here';
1973 }
1974 if (importTypeField) {
1975 importTypeField.value = type;
1976 }
1977 if (descriptionText) {
1978 const descriptions = {
1979 'sitemap': '<?php echo esc_js(__('Use a content-specific sub-sitemap (e.g., post-sitemap.xml), not the main sitemap index.', 'mxchat')); ?>',
1980 'url': '<?php echo esc_js(__('Enter the URL of any webpage to import its content.', 'mxchat')); ?>',
1981 'pdf': '<?php echo esc_js(__('Enter the URL of a publicly accessible PDF file.', 'mxchat')); ?>'
1982 };
1983 descriptionText.textContent = descriptions[type] || '';
1984 }
1985
1986 // Only show sitemap detection when Sitemap Import is selected
1987 if (type === 'sitemap') {
1988 // Initialize sitemap detection (function is defined in knowledge-processing.js)
1989 if (typeof window.mxchatInitSitemapDetection === 'function') {
1990 window.mxchatInitSitemapDetection();
1991 }
1992 } else {
1993 hideSitemapDetection();
1994 }
1995 }
1996 });
1997 });
1998 }
1999
2000 function initCustomPostTypesToggle() {
2001 const toggleBtn = document.getElementById('mxchat-custom-post-types-toggle');
2002 const container = document.getElementById('mxchat-custom-post-types-container');
2003
2004 if (toggleBtn && container) {
2005 toggleBtn.addEventListener('click', function() {
2006 const isHidden = container.style.display === 'none';
2007 container.style.display = isHidden ? 'block' : 'none';
2008 const icon = this.querySelector('span:last-child');
2009 if (icon) {
2010 icon.textContent = isHidden ? '' : '';
2011 }
2012 });
2013 }
2014 }
2015
2016 // Sitemap detection is now handled by knowledge-processing.js
2017 // It will be initialized when user clicks "Sitemap Import" option
2018
2019 // ─── Edit Entry Modal ────────────────────────────────────
2020 initEditModal();
2021
2022 function initEditModal() {
2023 var modal = document.getElementById('mxchat-kb-edit-modal');
2024 var textarea = document.getElementById('mxchat-kb-edit-textarea');
2025 var saveBtn = document.getElementById('mxchat-kb-edit-save');
2026 var cancelBtn = document.getElementById('mxchat-kb-edit-cancel');
2027 var closeBtn = document.getElementById('mxchat-kb-edit-close');
2028 var notice = document.getElementById('mxchat-kb-edit-notice');
2029 var charCount = document.getElementById('mxchat-kb-edit-charcount');
2030 var chunkInfo = document.getElementById('mxchat-kb-edit-chunkinfo');
2031 var sourceEl = document.getElementById('mxchat-kb-edit-source');
2032 var titleEl = document.getElementById('mxchat-kb-edit-title');
2033 var saveText = saveBtn ? saveBtn.querySelector('.mxchat-kb-edit-save-text') : null;
2034 var saveSpin = saveBtn ? saveBtn.querySelector('.mxchat-kb-edit-save-spinner') : null;
2035
2036 if (!modal) return;
2037
2038 var currentEntry = {};
2039
2040 // Bind edit buttons (delegated)
2041 document.addEventListener('click', function(e) {
2042 var btn = e.target.closest('.mxchat-edit-entry-btn');
2043 if (btn) {
2044 e.preventDefault();
2045 openEditModal(btn);
2046 }
2047 });
2048
2049 // Close handlers
2050 if (closeBtn) closeBtn.addEventListener('click', closeModal);
2051 if (cancelBtn) cancelBtn.addEventListener('click', closeModal);
2052 modal.addEventListener('click', function(e) {
2053 if (e.target === modal) closeModal();
2054 });
2055
2056 // Character count
2057 if (textarea) {
2058 textarea.addEventListener('input', function() {
2059 updateCharCount();
2060 });
2061 }
2062
2063 // Save handler
2064 if (saveBtn) saveBtn.addEventListener('click', saveContent);
2065
2066 function openEditModal(btn) {
2067 currentEntry = {
2068 sourceUrl: btn.getAttribute('data-source-url') || '',
2069 entryId: btn.getAttribute('data-entry-id') || '',
2070 dataSource: btn.getAttribute('data-data-source') || 'wordpress',
2071 botId: btn.getAttribute('data-bot-id') || 'default',
2072 nonce: btn.getAttribute('data-nonce') || ''
2073 };
2074
2075 // Reset state
2076 textarea.value = '';
2077 textarea.placeholder = 'Loading content...';
2078 textarea.disabled = true;
2079 saveBtn.disabled = true;
2080 notice.textContent = '';
2081 notice.className = 'mxchat-kb-edit-notice';
2082 charCount.textContent = '';
2083 chunkInfo.textContent = '';
2084
2085 // Show source
2086 if (currentEntry.sourceUrl && currentEntry.sourceUrl.indexOf('mxchat://') !== 0) {
2087 sourceEl.innerHTML = 'Source: <a href="' + escapeHtml(currentEntry.sourceUrl) + '" target="_blank">' + escapeHtml(truncate(currentEntry.sourceUrl, 60)) + '</a>';
2088 } else {
2089 sourceEl.textContent = 'Manual Content';
2090 }
2091
2092 // Show modal
2093 modal.classList.add('active');
2094
2095 // Fetch content
2096 var formData = new FormData();
2097 formData.append('action', 'mxchat_get_entry_content');
2098 formData.append('nonce', currentEntry.nonce);
2099 formData.append('source_url', currentEntry.sourceUrl);
2100 formData.append('entry_id', currentEntry.entryId);
2101 formData.append('data_source', currentEntry.dataSource);
2102 formData.append('bot_id', currentEntry.botId);
2103
2104 fetch(ajaxurl, { method: 'POST', body: formData })
2105 .then(function(r) { return r.json(); })
2106 .then(function(resp) {
2107 if (resp.success) {
2108 textarea.value = resp.data.content || '';
2109 textarea.disabled = false;
2110 textarea.placeholder = 'Edit your content here...';
2111 saveBtn.disabled = false;
2112 currentEntry.contentType = resp.data.content_type || 'content';
2113
2114 updateCharCount();
2115
2116 if (resp.data.is_chunked) {
2117 chunkInfo.textContent = resp.data.chunk_count + ' chunks — will be re-chunked on save';
2118 titleEl.textContent = 'Edit Knowledge Entry (' + resp.data.chunk_count + ' chunks)';
2119 } else {
2120 chunkInfo.textContent = '';
2121 titleEl.textContent = 'Edit Knowledge Entry';
2122 }
2123
2124 textarea.focus();
2125 } else {
2126 textarea.placeholder = '';
2127 showNotice((resp.data && resp.data.message) || 'Failed to load content.', 'error');
2128 }
2129 })
2130 .catch(function(err) {
2131 textarea.placeholder = '';
2132 showNotice('Network error: ' + err.message, 'error');
2133 });
2134 }
2135
2136 function saveContent() {
2137 if (!textarea.value.trim()) {
2138 showNotice('Content cannot be empty.', 'error');
2139 return;
2140 }
2141
2142 saveBtn.disabled = true;
2143 if (saveText) saveText.style.display = 'none';
2144 if (saveSpin) saveSpin.style.display = '';
2145 showNotice('Saving and re-embedding...', '');
2146
2147 var formData = new FormData();
2148 formData.append('action', 'mxchat_save_entry_content');
2149 formData.append('nonce', currentEntry.nonce);
2150 formData.append('source_url', currentEntry.sourceUrl);
2151 formData.append('entry_id', currentEntry.entryId);
2152 formData.append('content', textarea.value);
2153 formData.append('data_source', currentEntry.dataSource);
2154 formData.append('bot_id', currentEntry.botId);
2155 formData.append('content_type', currentEntry.contentType || 'content');
2156
2157 fetch(ajaxurl, { method: 'POST', body: formData })
2158 .then(function(r) { return r.json(); })
2159 .then(function(resp) {
2160 if (saveText) saveText.style.display = '';
2161 if (saveSpin) saveSpin.style.display = 'none';
2162 saveBtn.disabled = false;
2163
2164 if (resp.success) {
2165 showNotice('Saved successfully!', 'success');
2166 setTimeout(function() {
2167 closeModal();
2168 window.location.reload();
2169 }, 1000);
2170 } else {
2171 showNotice((resp.data && resp.data.message) || 'Save failed.', 'error');
2172 }
2173 })
2174 .catch(function(err) {
2175 if (saveText) saveText.style.display = '';
2176 if (saveSpin) saveSpin.style.display = 'none';
2177 saveBtn.disabled = false;
2178 showNotice('Network error: ' + err.message, 'error');
2179 });
2180 }
2181
2182 function closeModal() {
2183 modal.classList.remove('active');
2184 }
2185
2186 function updateCharCount() {
2187 var len = textarea.value.length;
2188 charCount.textContent = len.toLocaleString() + ' characters';
2189 }
2190
2191 function showNotice(msg, type) {
2192 notice.textContent = msg;
2193 notice.className = 'mxchat-kb-edit-notice' + (type ? ' ' + type : '');
2194 }
2195
2196 function escapeHtml(str) {
2197 var div = document.createElement('div');
2198 div.textContent = str;
2199 return div.innerHTML;
2200 }
2201
2202 function truncate(str, max) {
2203 return str.length > max ? str.substring(0, max) + '...' : str;
2204 }
2205 }
2206
2207 })();
2208 </script>
2209 <?php
2210 }
2211