PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.5.5
MxChat – AI Chatbot & Content Generation for WordPress v2.5.5
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | admin/class-knowledge-manager.php +4850 -8406 3.2.122.5.5 View file →
@@ -1,8407 +1,4851 @@
1 -<?php
2 -/**
3 - * File: admin/class-knowledge-manager.php
4 - *
5 - * Handles all knowledge base content processing for MxChat
6 - * Including PDF, sitemap, content processing, and WordPress post management
7 - */
8 -if (!defined('ABSPATH')) {
9 - exit; // Exit if accessed directly
10 -}
11 -
12 -class MxChat_Knowledge_Manager {
13 -
14 - private $options;
15 -
16 - /**
17 - * Constructor - Register hooks for content processing
18 - */
19 -public function __construct() {
20 - $this->options = get_option('mxchat_options', array());
21 - $this->mxchat_init_hooks();
22 -
23 - $this->mxchat_init_role_hooks();
24 -}
25 -
26 -/**
27 - * Initialize WordPress hooks for content processing
28 - *
29 - */
30 -private function mxchat_init_hooks() {
31 - // Admin post handlers for form submissions
32 - add_action('admin_post_mxchat_submit_content', array($this, 'mxchat_handle_content_submission'));
33 - add_action('admin_post_mxchat_submit_sitemap', array($this, 'mxchat_handle_sitemap_submission'));
34 - add_action('admin_post_mxchat_submit_pdf_file', array($this, 'mxchat_handle_pdf_file_submission'));
35 - add_action('admin_post_mxchat_stop_processing', array($this, 'mxchat_stop_processing'));
36 -
37 - // AJAX handlers for real-time processing and status updates
38 - add_action('wp_ajax_mxchat_get_status_updates', array($this, 'mxchat_ajax_get_status_updates'));
39 - add_action('wp_ajax_mxchat_dismiss_completed_status', array($this, 'mxchat_ajax_dismiss_completed_status'));
40 - add_action('wp_ajax_mxchat_get_content_list', array($this, 'ajax_mxchat_get_content_list'));
41 - add_action('wp_ajax_mxchat_process_selected_content', array($this, 'ajax_mxchat_process_selected_content'));
42 - add_action('wp_ajax_mxchat_save_inline_prompt', array($this, 'mxchat_save_inline_prompt'));
43 - add_action('admin_post_mxchat_delete_pinecone_prompt', array($this, 'mxchat_handle_pinecone_prompt_delete'));
44 - add_action('wp_ajax_mxchat_delete_pinecone_prompt', array($this, 'ajax_mxchat_delete_pinecone_prompt'));
45 - add_action('wp_ajax_mxchat_delete_chunks_by_url', array($this, 'ajax_mxchat_delete_chunks_by_url'));
46 - add_action('wp_ajax_mxchat_delete_wordpress_prompt', array($this, 'ajax_mxchat_delete_wordpress_prompt'));
47 - add_action('wp_ajax_mxchat_bulk_delete_knowledge', array($this, 'ajax_mxchat_bulk_delete_knowledge'));
48 - add_action('wp_ajax_mxchat_update_role_restriction', array($this, 'ajax_mxchat_update_role_restriction'));
49 -
50 - // Queue-based processing AJAX handlers
51 - add_action('wp_ajax_mxchat_get_next_queue_item', array($this, 'ajax_mxchat_get_next_queue_item'));
52 - add_action('wp_ajax_mxchat_process_queue_item', array($this, 'ajax_mxchat_process_queue_item'));
53 - add_action('wp_ajax_mxchat_get_queue_status', array($this, 'ajax_mxchat_get_queue_status'));
54 - add_action('wp_ajax_mxchat_clear_queue', array($this, 'ajax_mxchat_clear_queue'));
55 - add_action('wp_ajax_mxchat_retry_failed', array($this, 'ajax_mxchat_retry_failed'));
56 - add_action('wp_ajax_mxchat_get_recent_entries', array($this, 'ajax_mxchat_get_recent_entries'));
57 - add_action('wp_ajax_mxchat_detect_sitemaps', array($this, 'ajax_mxchat_detect_sitemaps'));
58 - add_action('wp_ajax_mxchat_refresh_pinecone_entries', array($this, 'ajax_mxchat_refresh_pinecone_entries'));
59 - add_action('wp_ajax_mxchat_paginate_entries', array($this, 'ajax_mxchat_paginate_entries'));
60 - add_action('wp_ajax_mxchat_get_entry_content', array($this, 'ajax_mxchat_get_entry_content'));
61 - add_action('wp_ajax_mxchat_save_entry_content', array($this, 'ajax_mxchat_save_entry_content'));
62 - add_action('wp_ajax_mxchat_inspect_entry', array($this, 'ajax_mxchat_inspect_entry'));
63 -
64 - // WordPress post management hooks
65 - add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2);
66 - add_action('post_updated', array($this, 'mxchat_handle_post_update'), 10, 3);
67 - add_action('before_delete_post', array($this, 'mxchat_handle_post_delete'));
68 - add_action('wp_trash_post', array($this, 'mxchat_handle_post_delete'));
69 -
70 - // ACF hook - fires AFTER ACF fields are saved, ensuring ACF data is available
71 - // Priority 20 to run after ACF's own save (which runs at priority 10)
72 - add_action('acf/save_post', array($this, 'mxchat_handle_acf_save'), 20);
73 -
74 - add_action('wp_ajax_mxchat_mark_queue_complete', array($this, 'ajax_mxchat_mark_queue_complete'));
75 -
76 - // WooCommerce product hooks (if WooCommerce is active)
77 - if (class_exists('WooCommerce')) {
78 - add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2);
79 - add_action('save_post_product', array($this, 'mxchat_handle_product_change'), 10, 3);
80 - add_action('wp_trash_post', array($this, 'mxchat_handle_product_delete'));
81 - add_action('before_delete_post', array($this, 'mxchat_handle_product_delete'));
82 - }
83 -}
84 -
85 - /**
86 - * Get current options (refreshed)
87 - */
88 - private function mxchat_get_options() {
89 - if (empty($this->options)) {
90 - $this->options = get_option('mxchat_options', array());
91 - }
92 - return $this->options;
93 - }
94 -
95 -
96 - // ========================================
97 - // MAIN CONTENT SUBMISSION HANDLERS
98 - // ========================================
99 -
100 -public function mxchat_handle_content_submission() {
101 - // Check if the form was submitted and the user has permission.
102 - if (!isset($_POST['submit_content']) || !current_user_can('manage_options')) {
103 - return;
104 - }
105 -
106 - // Verify the nonce.
107 - $nonce = isset($_POST['mxchat_submit_content_nonce']) ? sanitize_text_field(wp_unslash($_POST['mxchat_submit_content_nonce'])) : '';
108 - if (!wp_verify_nonce($nonce, 'mxchat_submit_content_action')) {
109 - wp_die(esc_html__('Nonce verification failed.', 'mxchat'));
110 - }
111 -
112 - // Sanitize the inputs.
113 - // Use wp_kses_post to allow safe HTML and wp_unslash to remove WordPress added slashes
114 - $article_content = wp_kses_post(wp_unslash($_POST['article_content']));
115 - $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
116 -
117 - // Get bot_id from form submission
118 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
119 -
120 - // Get bot-specific options and API key
121 - $bot_options = $this->get_bot_options($bot_id);
122 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
123 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
124 -
125 - if (strpos($selected_model, 'voyage') === 0) {
126 - $api_key = $options['voyage_api_key'] ?? '';
127 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
128 - $api_key = $options['gemini_api_key'] ?? '';
129 - } else {
130 - $api_key = $options['api_key'] ?? '';
131 - }
132 -
133 - if (empty($api_key)) {
134 - set_transient('mxchat_admin_notice_error',
135 - esc_html__('API key is not configured. Please add your API key in the settings before submitting content.', 'mxchat'),
136 - 30
137 - );
138 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
139 - exit;
140 - }
141 -
142 - // Use centralized utility function with bot_id
143 - $result = MxChat_Utils::submit_content_to_db($article_content, $article_url, $api_key, null, $bot_id);
144 -
145 - if (is_wp_error($result)) {
146 - set_transient('mxchat_admin_notice_error',
147 - esc_html__('Error storing content: ', 'mxchat') . $result->get_error_message(),
148 - 30
149 - );
150 - } else {
151 - set_transient('mxchat_admin_notice_success',
152 - esc_html__('Content successfully submitted!', 'mxchat'),
153 - 30
154 - );
155 - }
156 -
157 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
158 - exit;
159 -}
160 -
161 -public function mxchat_is_pdf_url($url, $response) {
162 - $content_type = wp_remote_retrieve_header($response, 'content-type');
163 - $file_extension = strtolower(pathinfo($url, PATHINFO_EXTENSION));
164 -
165 - // Check Content-Disposition header for .pdf filename (Google Drive sends this)
166 - $disposition = wp_remote_retrieve_header($response, 'content-disposition');
167 - $has_pdf_disposition = ! empty($disposition) && stripos($disposition, '.pdf') !== false;
168 -
169 - return strpos($content_type, 'pdf') !== false || $file_extension === 'pdf' || $has_pdf_disposition;
170 -}
171 -
172 -
173 -public function mxchat_handle_pdf_for_knowledge_base($pdf_url, $response, $bot_id = 'default') {
174 - if (!current_user_can('manage_options')) {
175 - return false;
176 - }
177 -
178 - $pdf_url = esc_url_raw($pdf_url);
179 - $upload_dir = wp_upload_dir();
180 -
181 - if (isset($upload_dir['error']) && $upload_dir['error'] !== false) {
182 - return false;
183 - }
184 -
185 - $pdf_filename = sanitize_file_name('mxchat_kb_' . time() . '.pdf');
186 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
187 -
188 - $response_body = wp_remote_retrieve_body($response);
189 - if (empty($response_body)) {
190 - return false;
191 - }
192 -
193 - if (!wp_mkdir_p(dirname($pdf_path))) {
194 - return false;
195 - }
196 -
197 - try {
198 - file_put_contents($pdf_path, $response_body);
199 -
200 - if (!file_exists($pdf_path)) {
201 - throw new Exception(__('Failed to save PDF file', 'mxchat'));
202 - }
203 -
204 - $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
205 -
206 - if ($total_pages === false || $total_pages < 1) {
207 - throw new Exception(__('Invalid PDF: Unable to parse or no pages found', 'mxchat'));
208 - }
209 -
210 - // Create unique queue ID
211 - $queue_id = 'pdf_' . md5($pdf_url . time());
212 -
213 - // Create array of pages to process
214 - $pages = array();
215 - for ($i = 1; $i <= $total_pages; $i++) {
216 - $pages[] = array(
217 - 'pdf_path' => $pdf_path,
218 - 'pdf_url' => $pdf_url,
219 - 'page_number' => $i,
220 - 'total_pages' => $total_pages
221 - );
222 - }
223 -
224 - // Add pages to queue
225 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'pdf_page', $pages, $bot_id);
226 -
227 - if ($queued_count === 0) {
228 - wp_delete_file($pdf_path);
229 - throw new Exception(__('Failed to add PDF pages to processing queue', 'mxchat'));
230 - }
231 -
232 - // Store queue metadata
233 - $this->mxchat_set_queue_meta($queue_id, 'source_url', $pdf_url);
234 - $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'pdf');
235 - $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_pages);
236 - $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
237 - $this->mxchat_set_queue_meta($queue_id, 'pdf_path', $pdf_path);
238 - $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
239 -
240 - // Store queue ID in transient for status tracking
241 - set_transient('mxchat_active_queue_pdf', $queue_id, DAY_IN_SECONDS);
242 - set_transient('mxchat_last_pdf_url', $pdf_url, DAY_IN_SECONDS);
243 -
244 - return 'queued';
245 -
246 - } catch (Exception $e) {
247 - if (file_exists($pdf_path)) {
248 - wp_delete_file($pdf_path);
249 - }
250 - return $e->getMessage();
251 - }
252 -}
253 -
254 -/**
255 - * Handle direct PDF file upload from the knowledge base page
256 - */
257 -public function mxchat_handle_pdf_file_submission() {
258 - if (!isset($_POST['submit_pdf_file']) || !current_user_can('manage_options')) {
259 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
260 - }
261 -
262 - check_admin_referer('mxchat_submit_pdf_file_action', 'mxchat_submit_pdf_file_nonce');
263 -
264 - $redirect_url = admin_url('admin.php?page=mxchat-prompts');
265 -
266 - // Validate file upload
267 - if (empty($_FILES['pdf_file']) || $_FILES['pdf_file']['error'] !== UPLOAD_ERR_OK) {
268 - $error_code = isset($_FILES['pdf_file']['error']) ? $_FILES['pdf_file']['error'] : UPLOAD_ERR_NO_FILE;
269 - $error_messages = array(
270 - UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the server upload_max_filesize limit.', 'mxchat'),
271 - UPLOAD_ERR_FORM_SIZE => __('The uploaded file exceeds the form MAX_FILE_SIZE limit.', 'mxchat'),
272 - UPLOAD_ERR_PARTIAL => __('The file was only partially uploaded.', 'mxchat'),
273 - UPLOAD_ERR_NO_FILE => __('No file was uploaded. Please select a PDF file.', 'mxchat'),
274 - UPLOAD_ERR_NO_TMP_DIR => __('Server missing temporary folder.', 'mxchat'),
275 - UPLOAD_ERR_CANT_WRITE => __('Server failed to write file to disk.', 'mxchat'),
276 - );
277 - $error_msg = isset($error_messages[$error_code]) ? $error_messages[$error_code] : __('Unknown upload error.', 'mxchat');
278 - set_transient('mxchat_admin_notice_error', $error_msg, 30);
279 - wp_safe_redirect(esc_url($redirect_url));
280 - exit;
281 - }
282 -
283 - $file = $_FILES['pdf_file'];
284 -
285 - // Validate MIME type
286 - $finfo = finfo_open(FILEINFO_MIME_TYPE);
287 - $mime_type = finfo_file($finfo, $file['tmp_name']);
288 - finfo_close($finfo);
289 -
290 - if ($mime_type !== 'application/pdf') {
291 - set_transient('mxchat_admin_notice_error',
292 - esc_html__('Invalid file type. Only PDF files are accepted.', 'mxchat'),
293 - 30
294 - );
295 - wp_safe_redirect(esc_url($redirect_url));
296 - exit;
297 - }
298 -
299 - // Validate extension
300 - $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
301 - if ($ext !== 'pdf') {
302 - set_transient('mxchat_admin_notice_error',
303 - esc_html__('Invalid file extension. Only .pdf files are accepted.', 'mxchat'),
304 - 30
305 - );
306 - wp_safe_redirect(esc_url($redirect_url));
307 - exit;
308 - }
309 -
310 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
311 - $original_filename = sanitize_file_name($file['name']);
312 -
313 - $upload_dir = wp_upload_dir();
314 - if (isset($upload_dir['error']) && $upload_dir['error'] !== false) {
315 - set_transient('mxchat_admin_notice_error',
316 - esc_html__('WordPress upload directory is not writable.', 'mxchat'),
317 - 30
318 - );
319 - wp_safe_redirect(esc_url($redirect_url));
320 - exit;
321 - }
322 -
323 - $pdf_filename = sanitize_file_name('mxchat_kb_' . time() . '.pdf');
324 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
325 -
326 - if (!wp_mkdir_p(dirname($pdf_path))) {
327 - set_transient('mxchat_admin_notice_error',
328 - esc_html__('Failed to create upload directory.', 'mxchat'),
329 - 30
330 - );
331 - wp_safe_redirect(esc_url($redirect_url));
332 - exit;
333 - }
334 -
335 - // Move uploaded file
336 - if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
337 - set_transient('mxchat_admin_notice_error',
338 - esc_html__('Failed to save uploaded PDF file.', 'mxchat'),
339 - 30
340 - );
341 - wp_safe_redirect(esc_url($redirect_url));
342 - exit;
343 - }
344 -
345 - try {
346 - $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
347 -
348 - if ($total_pages === false || $total_pages < 1) {
349 - throw new Exception(__('Invalid PDF: Unable to parse or no pages found', 'mxchat'));
350 - }
351 -
352 - // Use original filename as the source identifier
353 - $source_label = 'upload://' . $original_filename;
354 -
355 - $queue_id = 'pdf_' . md5($source_label . time());
356 -
357 - $pages = array();
358 - for ($i = 1; $i <= $total_pages; $i++) {
359 - $pages[] = array(
360 - 'pdf_path' => $pdf_path,
361 - 'pdf_url' => $source_label,
362 - 'page_number' => $i,
363 - 'total_pages' => $total_pages,
364 - );
365 - }
366 -
367 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'pdf_page', $pages, $bot_id);
368 -
369 - if ($queued_count === 0) {
370 - wp_delete_file($pdf_path);
371 - throw new Exception(__('Failed to add PDF pages to processing queue', 'mxchat'));
372 - }
373 -
374 - $this->mxchat_set_queue_meta($queue_id, 'source_url', $source_label);
375 - $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'pdf');
376 - $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_pages);
377 - $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
378 - $this->mxchat_set_queue_meta($queue_id, 'pdf_path', $pdf_path);
379 - $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
380 -
381 - set_transient('mxchat_active_queue_pdf', $queue_id, DAY_IN_SECONDS);
382 - set_transient('mxchat_last_pdf_url', $source_label, DAY_IN_SECONDS);
383 -
384 - set_transient('mxchat_admin_notice_success',
385 - sprintf(
386 - esc_html__('PDF "%s" (%d pages) queued for processing. Processing will start automatically.', 'mxchat'),
387 - esc_html($original_filename),
388 - $total_pages
389 - ),
390 - 30
391 - );
392 -
393 - } catch (Exception $e) {
394 - if (file_exists($pdf_path)) {
395 - wp_delete_file($pdf_path);
396 - }
397 - set_transient('mxchat_admin_notice_error',
398 - esc_html__('Failed to process uploaded PDF: ', 'mxchat') . esc_html($e->getMessage()),
399 - 30
400 - );
401 - }
402 -
403 - wp_safe_redirect(esc_url($redirect_url));
404 - exit;
405 -}
406 -
407 -/**
408 - * Validate PDF and count pages with multiple parser attempts
409 - */
410 -private function mxchat_validate_and_count_pdf_pages($pdf_path) {
411 - // Method 1: Try with Smalot PDF Parser (your current method)
412 - try {
413 - mxchat_load_pdf_parser();
414 - $parser = new \Smalot\PdfParser\Parser();
415 - $pdf = $parser->parseFile($pdf_path);
416 - $pages = $pdf->getPages();
417 - $page_count = count($pages);
418 -
419 - if ($page_count > 0) {
420 - //error_log('PDF parsed successfully with Smalot parser: ' . $page_count . ' pages');
421 - return $page_count;
422 - }
423 - } catch (Exception $e) {
424 - //error_log('Smalot PDF parser failed: ' . $e->getMessage());
425 - }
426 -
427 - // Method 2: Try with pdfinfo command (if available)
428 - if (function_exists('shell_exec') && !$this->mxchat_is_shell_disabled()) {
429 - try {
430 - $command = 'pdfinfo ' . escapeshellarg($pdf_path) . ' 2>&1';
431 - $output = shell_exec($command);
432 -
433 - if ($output && preg_match('/Pages:\s*(\d+)/', $output, $matches)) {
434 - $page_count = intval($matches[1]);
435 - if ($page_count > 0) {
436 - //error_log('PDF parsed successfully with pdfinfo: ' . $page_count . ' pages');
437 - return $page_count;
438 - }
439 - }
440 - } catch (Exception $e) {
441 - //error_log('pdfinfo command failed: ' . $e->getMessage());
442 - }
443 - }
444 -
445 - // Method 3: Try to repair PDF and parse again
446 - try {
447 - $repaired_path = $this->mxchat_attempt_pdf_repair($pdf_path);
448 - if ($repaired_path && $repaired_path !== $pdf_path) {
449 - mxchat_load_pdf_parser();
450 - $parser = new \Smalot\PdfParser\Parser();
451 - $pdf = $parser->parseFile($repaired_path);
452 - $pages = $pdf->getPages();
453 - $page_count = count($pages);
454 -
455 - if ($page_count > 0) {
456 - // Replace original with repaired version
457 - copy($repaired_path, $pdf_path);
458 - unlink($repaired_path);
459 - //error_log('PDF repaired and parsed successfully: ' . $page_count . ' pages');
460 - return $page_count;
461 - }
462 -
463 - // Clean up repaired file if it didn't work
464 - unlink($repaired_path);
465 - }
466 - } catch (Exception $e) {
467 - //error_log('PDF repair attempt failed: ' . $e->getMessage());
468 - }
469 -
470 - // Method 4: Manual PDF structure analysis (basic page count)
471 - try {
472 - $page_count = $this->mxchat_manual_pdf_page_count($pdf_path);
473 - if ($page_count > 0) {
474 - //error_log('PDF page count determined manually: ' . $page_count . ' pages');
475 - return $page_count;
476 - }
477 - } catch (Exception $e) {
478 - //error_log('Manual PDF analysis failed: ' . $e->getMessage());
479 - }
480 -
481 - //error_log('All PDF parsing methods failed for: ' . $pdf_path);
482 - return false;
483 -}
484 -
485 -/**
486 - * Check if shell_exec is disabled
487 - */
488 -private function mxchat_is_shell_disabled() {
489 - $disabled = explode(',', ini_get('disable_functions'));
490 - return in_array('shell_exec', $disabled);
491 -}
492 -
493 -/**
494 - * Attempt to repair PDF using basic methods
495 - */
496 -private function mxchat_attempt_pdf_repair($pdf_path) {
497 - try {
498 - $content = file_get_contents($pdf_path);
499 - if (!$content) {
500 - return false;
501 - }
502 -
503 - // Check if PDF starts with proper header
504 - if (substr($content, 0, 4) !== '%PDF') {
505 - // Try to find PDF header in the content
506 - $header_pos = strpos($content, '%PDF');
507 - if ($header_pos !== false && $header_pos < 1024) {
508 - // Remove junk before PDF header
509 - $content = substr($content, $header_pos);
510 - $repaired_path = $pdf_path . '.repaired';
511 - file_put_contents($repaired_path, $content);
512 - return $repaired_path;
513 - }
514 - }
515 -
516 - // Check for EOF marker
517 - $content = rtrim($content);
518 - if (!preg_match('/%%EOF\s*$/', $content)) {
519 - // Add EOF marker if missing
520 - $content .= "\n%%EOF";
521 - $repaired_path = $pdf_path . '.repaired';
522 - file_put_contents($repaired_path, $content);
523 - return $repaired_path;
524 - }
525 -
526 - } catch (Exception $e) {
527 - //error_log('PDF repair error: ' . $e->getMessage());
528 - }
529 -
530 - return false;
531 -}
532 -
533 -/**
534 - * Manual PDF page counting by analyzing PDF structure
535 - */
536 -private function mxchat_manual_pdf_page_count($pdf_path) {
537 - try {
538 - $content = file_get_contents($pdf_path);
539 - if (!$content) {
540 - return 0;
541 - }
542 -
543 - // Method 1: Count /Type /Page objects
544 - $page_count = preg_match_all('/\/Type\s*\/Page[^s]/', $content);
545 - if ($page_count > 0) {
546 - return $page_count;
547 - }
548 -
549 - // Method 2: Look for /Count in pages object
550 - if (preg_match('/\/Type\s*\/Pages.*?\/Count\s+(\d+)/', $content, $matches)) {
551 - return intval($matches[1]);
552 - }
553 -
554 - // Method 3: Count page references
555 - $page_count = preg_match_all('/\d+\s+0\s+obj\s*<<[^>]*\/Type\s*\/Page/', $content);
556 - if ($page_count > 0) {
557 - return $page_count;
558 - }
559 -
560 - } catch (Exception $e) {
561 - //error_log('Manual PDF analysis error: ' . $e->getMessage());
562 - }
563 -
564 - return 0;
565 -}
566 -
567 -
568 -public function mxchat_save_inline_prompt() {
569 - // DEBUG: Log what we're receiving
570 - //error_log('=== MXCHAT DEBUG ===');
571 - //error_log('POST data: ' . print_r($_POST, true));
572 - //error_log('Nonce from POST: ' . ($_POST['_ajax_nonce'] ?? 'NOT FOUND'));
573 -
574 - // Check for nonce security
575 - check_ajax_referer('mxchat_save_inline_nonce', '_ajax_nonce');
576 -
577 - // If we get here, nonce passed
578 - //error_log('Nonce verification PASSED');
579 -
580 - // Verify permissions
581 - if (!current_user_can('manage_options')) {
582 - wp_send_json_error(esc_html__('Permission denied.', 'mxchat'));
583 - return;
584 - }
585 -
586 - global $wpdb;
587 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
588 -
589 - // Validate and sanitize input data
590 - $prompt_id = isset($_POST['id']) ? absint($_POST['id']) : 0;
591 - $article_content = isset($_POST['article_content']) ? wp_kses_post(wp_unslash($_POST['article_content'])) : '';
592 - $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
593 -
594 - if ($prompt_id > 0 && !empty($article_content)) {
595 - // Re-generate the embedding vector for the updated content
596 - $embedding_vector = $this->mxchat_generate_embedding($article_content);
597 - if (is_array($embedding_vector)) {
598 - // Serialize the embedding vector before storing it
599 - $embedding_vector_serialized = serialize($embedding_vector);
600 - // Update the prompt in the database
601 - $updated = $wpdb->update(
602 - $table_name,
603 - array(
604 - 'article_content' => $article_content,
605 - 'embedding_vector' => $embedding_vector_serialized,
606 - 'source_url' => $article_url,
607 - ),
608 - array('id' => $prompt_id),
609 - array('%s', '%s', '%s'),
610 - array('%d')
611 - );
612 - if ($updated !== false) {
613 - wp_send_json_success();
614 - } else {
615 - MxChat_Admin::mxchat_log_debug('knowledge_error', 'Database update failed when saving knowledge entry');
616 - wp_send_json_error(esc_html__('Database update failed.', 'mxchat'));
617 - }
618 - } else {
619 - MxChat_Admin::mxchat_log_debug('embedding_error', 'Embedding generation failed for knowledge entry');
620 - wp_send_json_error(esc_html__('Embedding generation failed.', 'mxchat'));
621 - }
622 - } else {
623 - wp_send_json_error(esc_html__('Invalid data.', 'mxchat'));
624 - }
625 -}
626 -
627 -
628 -/**
629 - * AJAX: Get full content for editing — reassembles chunks if needed.
630 - * Works for both WordPress DB and Pinecone entries.
631 - */
632 -public function ajax_mxchat_get_entry_content() {
633 - check_ajax_referer('mxchat_edit_entry_nonce', 'nonce');
634 -
635 - if ( ! current_user_can('manage_options') ) {
636 - wp_send_json_error( array( 'message' => 'Permission denied.' ) );
637 - }
638 -
639 - $source_url = isset($_POST['source_url']) ? sanitize_text_field( wp_unslash($_POST['source_url']) ) : '';
640 - $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0;
641 - $data_source = isset($_POST['data_source']) ? sanitize_key($_POST['data_source']) : 'wordpress';
642 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
643 -
644 - if ( $data_source === 'pinecone' ) {
645 - // Pinecone: fetch vectors by source_url, reassemble chunks
646 - $content = $this->get_pinecone_entry_content( $source_url, $entry_id, $bot_id );
647 - } else {
648 - // WordPress DB
649 - $content = $this->get_wordpress_entry_content( $source_url, $entry_id );
650 - }
651 -
652 - if ( is_wp_error( $content ) ) {
653 - wp_send_json_error( array( 'message' => $content->get_error_message() ) );
654 - }
655 -
656 - wp_send_json_success( $content );
657 -}
658 -
659 -/**
660 - * Get content from WordPress DB — reassembles chunks by source_url.
661 - */
662 -private function get_wordpress_entry_content( $source_url, $entry_id ) {
663 - global $wpdb;
664 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
665 -
666 - // If we have a source_url, check for chunks
667 - if ( ! empty( $source_url ) && strpos( $source_url, 'mxchat://' ) !== 0 ) {
668 - $rows = $wpdb->get_results( $wpdb->prepare(
669 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE source_url = %s ORDER BY id ASC",
670 - $source_url
671 - ) );
672 -
673 - if ( $rows && count( $rows ) > 1 ) {
674 - // Multiple rows = chunked. Reassemble.
675 - $chunks = array();
676 - foreach ( $rows as $row ) {
677 - $parsed = MxChat_Chunker::parse_stored_chunk( $row->article_content );
678 - $index = isset( $parsed['metadata']['chunk_index'] ) ? intval( $parsed['metadata']['chunk_index'] ) : count( $chunks );
679 - $chunks[ $index ] = $parsed['text'];
680 - }
681 - ksort( $chunks );
682 - return array(
683 - 'content' => implode( "\n\n", $chunks ),
684 - 'source_url' => $source_url,
685 - 'is_chunked' => true,
686 - 'chunk_count' => count( $chunks ),
687 - 'content_type' => $rows[0]->content_type,
688 - );
689 - } elseif ( $rows && count( $rows ) === 1 ) {
690 - $parsed = MxChat_Chunker::parse_stored_chunk( $rows[0]->article_content );
691 - return array(
692 - 'content' => $parsed['text'],
693 - 'source_url' => $source_url,
694 - 'entry_id' => $rows[0]->id,
695 - 'is_chunked' => false,
696 - 'content_type' => $rows[0]->content_type,
697 - );
698 - }
699 - }
700 -
701 - // Fallback: fetch by ID
702 - if ( $entry_id > 0 ) {
703 - $row = $wpdb->get_row( $wpdb->prepare(
704 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE id = %d",
705 - $entry_id
706 - ) );
707 - if ( $row ) {
708 - $parsed = MxChat_Chunker::parse_stored_chunk( $row->article_content );
709 - return array(
710 - 'content' => $parsed['text'],
711 - 'source_url' => $row->source_url,
712 - 'entry_id' => $row->id,
713 - 'is_chunked' => false,
714 - 'content_type' => $row->content_type,
715 - );
716 - }
717 - }
718 -
719 - return new WP_Error( 'not_found', 'Entry not found.' );
720 -}
721 -
722 -/**
723 - * Get content from Pinecone — fetches vectors by source_url, reassembles chunks.
724 - */
725 -private function get_pinecone_entry_content( $source_url, $entry_id, $bot_id ) {
726 - if ( ! class_exists('MxChat_Pinecone_Manager') ) {
727 - return new WP_Error( 'pinecone_unavailable', 'Pinecone manager not available.' );
728 - }
729 -
730 - // Get Pinecone config
731 - if ( $bot_id === 'default' || ! class_exists('MxChat_Multi_Bot_Manager') ) {
732 - $pinecone_options = get_option('mxchat_pinecone_addon_options');
733 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
734 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
735 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
736 - } else {
737 - $bot_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
738 - $api_key = $bot_config['api_key'] ?? '';
739 - $host = $bot_config['host'] ?? '';
740 - $namespace = $bot_config['namespace'] ?? '';
741 - }
742 -
743 - if ( empty($host) || empty($api_key) ) {
744 - return new WP_Error( 'pinecone_config', 'Pinecone not configured.' );
745 - }
746 -
747 - // List vectors with the source_url prefix
748 - $base_id = md5( $source_url );
749 - $vector_ids = array( $base_id );
750 -
751 - // Find chunk vectors
752 - $list_url = "https://{$host}/vectors/list";
753 - $list_body = array( 'prefix' => $base_id . '_chunk_', 'limit' => 100 );
754 - if ( ! empty($namespace) ) {
755 - $list_body['namespace'] = $namespace;
756 - }
757 -
758 - $list_resp = wp_remote_post( $list_url, array(
759 - 'headers' => array( 'Api-Key' => $api_key, 'Content-Type' => 'application/json' ),
760 - 'body' => wp_json_encode( $list_body ),
761 - 'timeout' => 15,
762 - ) );
763 -
764 - if ( ! is_wp_error($list_resp) ) {
765 - $list_data = json_decode( wp_remote_retrieve_body($list_resp), true );
766 - if ( ! empty($list_data['vectors']) ) {
767 - foreach ( $list_data['vectors'] as $v ) {
768 - $vector_ids[] = $v['id'];
769 - }
770 - }
771 - }
772 -
773 - // Fetch vectors with metadata
774 - $fetch_url = "https://{$host}/vectors/fetch";
775 - $fetch_body = array( 'ids' => $vector_ids );
776 - if ( ! empty($namespace) ) {
777 - $fetch_body['namespace'] = $namespace;
778 - }
779 -
780 - $fetch_resp = wp_remote_post( $fetch_url, array(
781 - 'headers' => array( 'Api-Key' => $api_key, 'Content-Type' => 'application/json' ),
782 - 'body' => wp_json_encode( $fetch_body ),
783 - 'timeout' => 15,
784 - ) );
785 -
786 - if ( is_wp_error($fetch_resp) ) {
787 - return new WP_Error( 'pinecone_fetch', 'Failed to fetch from Pinecone.' );
788 - }
789 -
790 - $fetch_data = json_decode( wp_remote_retrieve_body($fetch_resp), true );
791 - $vectors = $fetch_data['vectors'] ?? array();
792 -
793 - if ( empty($vectors) ) {
794 - return new WP_Error( 'not_found', 'Entry not found in Pinecone.' );
795 - }
796 -
797 - // Reassemble chunks
798 - $chunks = array();
799 - $content_type = 'content';
800 - foreach ( $vectors as $vid => $vector ) {
801 - $meta = $vector['metadata'] ?? array();
802 - $text = $meta['text'] ?? '';
803 - $index = $meta['chunk_index'] ?? 0;
804 - $content_type = $meta['type'] ?? 'content';
805 - $chunks[ intval($index) ] = $text;
806 - }
807 - ksort( $chunks );
808 -
809 - return array(
810 - 'content' => implode( "\n\n", $chunks ),
811 - 'source_url' => $source_url,
812 - 'is_chunked' => count($chunks) > 1,
813 - 'chunk_count' => count($chunks),
814 - 'content_type' => $content_type,
815 - );
816 -}
817 -
818 -/**
819 - * AJAX: Inspect a knowledge entry — returns the per-chunk stored text + metadata
820 - * WITHOUT collapsing it, so a site owner can see exactly what was indexed for an
821 - * entry (plan-mxchat-20260628-d8cb4b). READ-ONLY: never re-embeds or mutates.
822 - */
823 -public function ajax_mxchat_inspect_entry() {
824 - check_ajax_referer('mxchat_inspect_entry_nonce', 'nonce');
825 -
826 - if ( ! current_user_can('manage_options') ) {
827 - wp_send_json_error( array( 'message' => esc_html__('Permission denied.', 'mxchat') ) );
828 - }
829 -
830 - $source_url = isset($_POST['source_url']) ? sanitize_text_field( wp_unslash($_POST['source_url']) ) : '';
831 - $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0;
832 - $data_source = isset($_POST['data_source']) ? sanitize_key($_POST['data_source']) : 'wordpress';
833 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
834 -
835 - if ( $data_source === 'pinecone' ) {
836 - $result = $this->inspect_pinecone_entry( $source_url, $entry_id, $bot_id );
837 - } else {
838 - $result = $this->inspect_wordpress_entry( $source_url, $entry_id );
839 - }
840 -
841 - if ( is_wp_error( $result ) ) {
842 - wp_send_json_error( array( 'message' => $result->get_error_message() ) );
843 - }
844 -
845 - wp_send_json_success( $result );
846 -}
847 -
848 -/**
849 - * Read-only inspector for WordPress-DB entries. Mirrors get_wordpress_entry_content()
850 - * but returns each STORED chunk's exact text + length (no implode), plus the assembled
851 - * embedded text. This shows what is actually in the index, not a re-derivation from the post.
852 - */
853 -private function inspect_wordpress_entry( $source_url, $entry_id ) {
854 - global $wpdb;
855 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
856 -
857 - $rows = array();
858 -
859 - // Group by the real stored source_url — this INCLUDES "mxchat://" manual
860 - // Direct Content entries (the spec's manual-entry case), which share one
861 - // source_url across their chunk rows. Only the synthetic "_ungrouped_<id>"
862 - // display key (invented by the table view for rows with no source_url) is
863 - // excluded; those fall through to the entry_id lookup below.
864 - if ( ! empty( $source_url ) && strpos( $source_url, '_ungrouped_' ) !== 0 ) {
865 - $rows = $wpdb->get_results( $wpdb->prepare(
866 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE source_url = %s ORDER BY id ASC",
867 - $source_url
868 - ) );
869 - }
870 -
871 - // Fallback / manual "Direct Content" entries: fetch the single row by id.
872 - if ( empty( $rows ) && $entry_id > 0 ) {
873 - $row = $wpdb->get_row( $wpdb->prepare(
874 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE id = %d",
875 - $entry_id
876 - ) );
877 - if ( $row ) {
878 - $rows = array( $row );
879 - }
880 - }
881 -
882 - if ( empty( $rows ) ) {
883 - return new WP_Error( 'not_found', esc_html__('Entry not found in the local knowledge database.', 'mxchat') );
884 - }
885 -
886 - $chunks = array();
887 - $content_type = '';
888 - foreach ( $rows as $row ) {
889 - $parsed = MxChat_Chunker::parse_stored_chunk( $row->article_content );
890 - $text = isset( $parsed['text'] ) ? $parsed['text'] : '';
891 - $index = isset( $parsed['metadata']['chunk_index'] ) ? intval( $parsed['metadata']['chunk_index'] ) : count( $chunks );
892 - $content_type = $row->content_type;
893 - $chunks[] = array(
894 - 'index' => $index,
895 - 'text' => $text,
896 - 'length' => function_exists('mb_strlen') ? mb_strlen( $text ) : strlen( $text ),
897 - 'row_id' => intval( $row->id ),
898 - );
899 - }
900 -
901 - usort( $chunks, function( $a, $b ) { return $a['index'] - $b['index']; } );
902 -
903 - $assembled = implode( "\n\n", wp_list_pluck( $chunks, 'text' ) );
904 -
905 - return array(
906 - 'store' => 'wordpress',
907 - 'source_url' => $source_url,
908 - 'content_type' => $content_type,
909 - 'is_chunked' => count( $chunks ) > 1,
910 - 'chunk_count' => count( $chunks ),
911 - 'assembled' => $assembled,
912 - 'assembled_length' => function_exists('mb_strlen') ? mb_strlen( $assembled ) : strlen( $assembled ),
913 - 'chunks' => array_values( $chunks ),
914 - // WP-DB storage carries no separate vector metadata; surface that fact
915 - // rather than letting the owner guess (the spec's taxonomy question).
916 - 'metadata' => array(),
917 - 'metadata_note' => esc_html__('Stored in the local WordPress database. Only the assembled text shown here is embedded — there are no separate vector metadata fields (e.g. taxonomy terms are not stored unless they were injected into the text itself).', 'mxchat'),
918 - );
919 -}
920 -
921 -/**
922 - * Read-only inspector for Pinecone entries. Mirrors get_pinecone_entry_content()
923 - * but keeps each vector's text + metadata instead of imploding, so the owner can
924 - * confirm exactly which metadata fields (text/source_url/type/last_updated/created_at/bot_id)
925 - * are present per chunk. READ-ONLY.
926 - */
927 -private function inspect_pinecone_entry( $source_url, $entry_id, $bot_id ) {
928 - if ( ! class_exists('MxChat_Pinecone_Manager') ) {
929 - return new WP_Error( 'pinecone_unavailable', esc_html__('Pinecone manager not available.', 'mxchat') );
930 - }
931 -
932 - if ( $bot_id === 'default' || ! class_exists('MxChat_Multi_Bot_Manager') ) {
933 - $pinecone_options = get_option('mxchat_pinecone_addon_options');
934 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
935 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
936 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
937 - } else {
938 - $bot_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
939 - $api_key = $bot_config['api_key'] ?? '';
940 - $host = $bot_config['host'] ?? '';
941 - $namespace = $bot_config['namespace'] ?? '';
942 - }
943 -
944 - if ( empty($host) || empty($api_key) ) {
945 - return new WP_Error( 'pinecone_config', esc_html__('Pinecone not configured.', 'mxchat') );
946 - }
947 -
948 - $base_id = md5( $source_url );
949 - $vector_ids = array( $base_id );
950 -
951 - $list_url = "https://{$host}/vectors/list";
952 - $list_body = array( 'prefix' => $base_id . '_chunk_', 'limit' => 100 );
953 - if ( ! empty($namespace) ) {
954 - $list_body['namespace'] = $namespace;
955 - }
956 -
957 - $list_resp = wp_remote_post( $list_url, array(
958 - 'headers' => array( 'Api-Key' => $api_key, 'Content-Type' => 'application/json' ),
959 - 'body' => wp_json_encode( $list_body ),
960 - 'timeout' => 15,
961 - ) );
962 -
963 - if ( ! is_wp_error($list_resp) ) {
964 - $list_data = json_decode( wp_remote_retrieve_body($list_resp), true );
965 - if ( ! empty($list_data['vectors']) ) {
966 - foreach ( $list_data['vectors'] as $v ) {
967 - $vector_ids[] = $v['id'];
968 - }
969 - }
970 - }
971 -
972 - $fetch_url = "https://{$host}/vectors/fetch";
973 - $fetch_body = array( 'ids' => $vector_ids );
974 - if ( ! empty($namespace) ) {
975 - $fetch_body['namespace'] = $namespace;
976 - }
977 -
978 - $fetch_resp = wp_remote_post( $fetch_url, array(
979 - 'headers' => array( 'Api-Key' => $api_key, 'Content-Type' => 'application/json' ),
980 - 'body' => wp_json_encode( $fetch_body ),
981 - 'timeout' => 15,
982 - ) );
983 -
984 - if ( is_wp_error($fetch_resp) ) {
985 - return new WP_Error( 'pinecone_fetch', esc_html__('Failed to fetch from Pinecone.', 'mxchat') );
986 - }
987 -
988 - $fetch_data = json_decode( wp_remote_retrieve_body($fetch_resp), true );
989 - $vectors = $fetch_data['vectors'] ?? array();
990 -
991 - if ( empty($vectors) ) {
992 - return new WP_Error( 'not_found', esc_html__('Entry not found in Pinecone.', 'mxchat') );
993 - }
994 -
995 - // Whitelisted metadata fields the spec calls out — shown so devs can confirm
996 - // what is (and is NOT) stored per vector.
997 - $meta_fields = array( 'text', 'source_url', 'type', 'last_updated', 'created_at', 'bot_id', 'chunk_index', 'total_chunks' );
998 - $chunks = array();
999 - $content_type = '';
1000 - foreach ( $vectors as $vid => $vector ) {
1001 - $meta = isset($vector['metadata']) && is_array($vector['metadata']) ? $vector['metadata'] : array();
1002 - $text = $meta['text'] ?? '';
1003 - $index = isset($meta['chunk_index']) ? intval($meta['chunk_index']) : count($chunks);
1004 - $content_type = $meta['type'] ?? $content_type;
1005 -
1006 - $clean_meta = array();
1007 - foreach ( $meta_fields as $field ) {
1008 - if ( array_key_exists( $field, $meta ) && $field !== 'text' ) {
1009 - $clean_meta[ $field ] = is_scalar( $meta[ $field ] ) ? (string) $meta[ $field ] : wp_json_encode( $meta[ $field ] );
1010 - }
1011 - }
1012 -
1013 - $chunks[] = array(
1014 - 'index' => $index,
1015 - 'text' => $text,
1016 - 'length' => function_exists('mb_strlen') ? mb_strlen( $text ) : strlen( $text ),
1017 - 'vector_id' => (string) $vid,
1018 - 'metadata' => $clean_meta,
1019 - );
1020 - }
1021 -
1022 - usort( $chunks, function( $a, $b ) { return $a['index'] - $b['index']; } );
1023 -
1024 - $assembled = implode( "\n\n", wp_list_pluck( $chunks, 'text' ) );
1025 -
1026 - return array(
1027 - 'store' => 'pinecone',
1028 - 'source_url' => $source_url,
1029 - 'content_type' => $content_type,
1030 - 'is_chunked' => count( $chunks ) > 1,
1031 - 'chunk_count' => count( $chunks ),
1032 - 'assembled' => $assembled,
1033 - 'assembled_length' => function_exists('mb_strlen') ? mb_strlen( $assembled ) : strlen( $assembled ),
1034 - 'chunks' => array_values( $chunks ),
1035 - 'metadata' => array(),
1036 - 'metadata_note' => esc_html__('Stored in Pinecone. Each chunk above lists the vector metadata fields actually present — if a field you expect (such as taxonomy terms) is missing here, it was not stored as metadata and is only searchable if it appears in the embedded text.', 'mxchat'),
1037 - );
1038 -}
1039 -
1040 -/**
1041 - * AJAX: Save edited content — re-chunks and re-embeds as needed.
1042 - * Works for both WordPress DB and Pinecone entries.
1043 - */
1044 -public function ajax_mxchat_save_entry_content() {
1045 - check_ajax_referer('mxchat_edit_entry_nonce', 'nonce');
1046 -
1047 - if ( ! current_user_can('manage_options') ) {
1048 - wp_send_json_error( array( 'message' => 'Permission denied.' ) );
1049 - }
1050 -
1051 - $source_url = isset($_POST['source_url']) ? sanitize_text_field( wp_unslash($_POST['source_url']) ) : '';
1052 - $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0;
1053 - $content = isset($_POST['content']) ? wp_kses_post( wp_unslash($_POST['content']) ) : '';
1054 - $data_source = isset($_POST['data_source']) ? sanitize_key($_POST['data_source']) : 'wordpress';
1055 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1056 - $content_type = isset($_POST['content_type']) ? sanitize_key($_POST['content_type']) : 'content';
1057 -
1058 - if ( empty($content) ) {
1059 - wp_send_json_error( array( 'message' => 'Content cannot be empty.' ) );
1060 - }
1061 -
1062 - // Get the embedding API key
1063 - $options = get_option('mxchat_options', array());
1064 - $api_key = '';
1065 -
1066 - if ( $bot_id !== 'default' && class_exists('MxChat_Multi_Bot_Manager') ) {
1067 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
1068 - $api_key = $bot_options['api_key'] ?? '';
1069 - }
1070 - if ( empty($api_key) ) {
1071 - $api_key = $options['api_key'] ?? '';
1072 - }
1073 -
1074 - global $wpdb;
1075 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
1076 -
1077 - // If source_url is empty but we have an entry_id, look it up
1078 - if ( empty($source_url) && $entry_id > 0 && $data_source === 'wordpress' ) {
1079 - $row = $wpdb->get_row( $wpdb->prepare( "SELECT source_url FROM {$table} WHERE id = %d", $entry_id ) );
1080 - if ( $row && ! empty($row->source_url) ) {
1081 - $source_url = $row->source_url;
1082 - }
1083 - }
1084 -
1085 - // For manual entries (no source_url or mxchat:// prefix), delete the old entry by ID first
1086 - // so submit_content_to_db creates a replacement instead of a duplicate
1087 - // Also treat legacy mxchat.ai source URLs as manual — old bug assigned the site URL to manual entries
1088 - $is_legacy_manual = !empty($source_url) && strpos($source_url, 'mxchat.ai') !== false && strpos($source_url, 'mxchat://') !== 0;
1089 - if ( $entry_id > 0 && (empty($source_url) || strpos($source_url, 'mxchat://') === 0 || $is_legacy_manual) ) {
1090 - $wpdb->delete( $table, array( 'id' => $entry_id ), array( '%d' ) );
1091 - // Clear legacy URL so submit_content_to_db generates a unique mxchat:// identifier
1092 - // instead of reusing the shared URL (which would mass-delete other entries with the same URL)
1093 - if ( $is_legacy_manual ) {
1094 - $source_url = '';
1095 - }
1096 - }
1097 -
1098 - // Use the existing submit_content_to_db which handles chunking, Pinecone, and WP DB
1099 - $vector_id = ! empty($source_url) ? md5($source_url) : md5('mxchat_manual_' . $entry_id);
1100 -
1101 - // submit_content_to_db already handles: delete old chunks → re-chunk → re-embed → store
1102 - $result = MxChat_Utils::submit_content_to_db( $content, $source_url, $api_key, $vector_id, $bot_id, $content_type );
1103 -
1104 - if ( is_wp_error($result) ) {
1105 - wp_send_json_error( array( 'message' => $result->get_error_message() ) );
1106 - }
1107 -
1108 - wp_send_json_success( array( 'message' => 'Content saved and re-embedded successfully.' ) );
1109 -}
1110 -
1111 -public function mxchat_get_pdf_processing_status($pdf_url) {
1112 - $pdf_url = esc_url_raw($pdf_url);
1113 - $status = get_transient(sanitize_key('mxchat_pdf_status_' . md5($pdf_url)));
1114 -
1115 - if (!$status || !is_array($status)) {
1116 - return false;
1117 - }
1118 -
1119 - // Check for stalled processing (no updates for 5 minutes)
1120 - if ($status['status'] === 'processing' && (time() - absint($status['last_update'])) > 300) {
1121 - $status['status'] = 'error';
1122 - $status['error'] = __('PDF processing appears to be stalled. No updates for over 5 minutes.', 'mxchat');
1123 -
1124 - // Save the updated status
1125 - set_transient(
1126 - sanitize_key('mxchat_pdf_status_' . md5($pdf_url)),
1127 - array_map('sanitize_text_field', $status),
1128 - DAY_IN_SECONDS
1129 - );
1130 - }
1131 -
1132 - $result = array(
1133 - 'total_pages' => absint($status['total_pages']),
1134 - 'processed_pages' => absint($status['processed_pages']),
1135 - 'failed_pages' => absint($status['failed_pages'] ?? 0),
1136 - 'percentage' => ($status['total_pages'] > 0)
1137 - ? round((absint($status['processed_pages']) / absint($status['total_pages'])) * 100)
1138 - : 0,
1139 - 'status' => sanitize_text_field($status['status']),
1140 - 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
1141 - 'failed_pages_list' => isset($status['failed_pages_list']) ? $status['failed_pages_list'] : array(),
1142 - 'completion_summary' => isset($status['completion_summary']) ? $status['completion_summary'] : null
1143 - );
1144 -
1145 - // Add error message if present
1146 - if (isset($status['error']) && !empty($status['error'])) {
1147 - $result['error'] = sanitize_text_field($status['error']);
1148 - }
1149 -
1150 - return $result;
1151 -}
1152 -
1153 -
1154 -public function mxchat_handle_sitemap_submission() {
1155 - // Check if the form was submitted and verify permissions
1156 - if (!isset($_POST['submit_sitemap']) || !current_user_can('manage_options')) {
1157 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
1158 - }
1159 -
1160 - // Verify nonce
1161 - check_admin_referer('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce');
1162 -
1163 - // Validate URL
1164 - if (!isset($_POST['sitemap_url']) || empty($_POST['sitemap_url'])) {
1165 - set_transient('mxchat_admin_notice_error',
1166 - esc_html__('Please provide a valid URL.', 'mxchat'),
1167 - 30
1168 - );
1169 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1170 - exit;
1171 - }
1172 -
1173 - $submitted_url = esc_url_raw($_POST['sitemap_url']);
1174 -
1175 - // Convert Google Drive sharing URLs to direct download URLs
1176 - if ( strpos($submitted_url, 'drive.google.com') !== false ) {
1177 - $file_id = '';
1178 - if ( preg_match('/[?&]id=([a-zA-Z0-9_-]+)/', $submitted_url, $m) ) {
1179 - $file_id = $m[1];
1180 - } elseif ( preg_match('#/file/d/([a-zA-Z0-9_-]+)#', $submitted_url, $m) ) {
1181 - $file_id = $m[1];
1182 - }
1183 - if ( ! empty($file_id) ) {
1184 - $submitted_url = 'https://drive.google.com/uc?export=download&id=' . $file_id;
1185 - }
1186 - }
1187 -
1188 - // Get bot_id from form submission
1189 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1190 -
1191 - // Get bot-specific options and validate API key
1192 - $bot_options = $this->get_bot_options($bot_id);
1193 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1194 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
1195 -
1196 - if (strpos($selected_model, 'voyage') === 0) {
1197 - $api_key = $options['voyage_api_key'] ?? '';
1198 - $provider_name = 'Voyage AI';
1199 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
1200 - $api_key = $options['gemini_api_key'] ?? '';
1201 - $provider_name = 'Google Gemini';
1202 - } else {
1203 - $api_key = $options['api_key'] ?? '';
1204 - $provider_name = 'OpenAI';
1205 - }
1206 -
1207 - if (empty($api_key)) {
1208 - $error_message = sprintf(
1209 - esc_html__('%s API key is not configured. Please add your API key in the settings before submitting content.', 'mxchat'),
1210 - $provider_name
1211 - );
1212 - set_transient('mxchat_admin_notice_error', $error_message, 30);
1213 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1214 - exit;
1215 - }
1216 -
1217 - // Fetch URL — use browser-like headers so servers with bot protection don't block us
1218 - $response = wp_remote_get($submitted_url, array(
1219 - 'timeout' => 30,
1220 - 'sslverify' => false,
1221 - 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
1222 - 'headers' => array(
1223 - 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
1224 - 'Accept-Language' => 'en-US,en;q=0.9',
1225 - ),
1226 - ));
1227 -
1228 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1229 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP Status: ' . wp_remote_retrieve_response_code($response);
1230 - set_transient('mxchat_admin_notice_error',
1231 - sprintf(
1232 - esc_html__('Failed to fetch the URL: %s', 'mxchat'),
1233 - esc_html($error_message)
1234 - ),
1235 - 30
1236 - );
1237 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1238 - exit;
1239 - }
1240 -
1241 - $content_type = wp_remote_retrieve_header($response, 'content-type');
1242 - $body_content = wp_remote_retrieve_body($response);
1243 -
1244 - if (empty($body_content)) {
1245 - set_transient('mxchat_admin_notice_error',
1246 - esc_html__('Empty response received from URL.', 'mxchat'),
1247 - 30
1248 - );
1249 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1250 - exit;
1251 - }
1252 -
1253 - // Handle PDF URL
1254 - if ($this->mxchat_is_pdf_url($submitted_url, $response)) {
1255 - $result = $this->mxchat_handle_pdf_for_knowledge_base($submitted_url, $response, $bot_id);
1256 -
1257 - if ($result === 'queued') {
1258 - set_transient('mxchat_admin_notice_success',
1259 - esc_html__('PDF queued for processing. Processing will start automatically.', 'mxchat'),
1260 - 30
1261 - );
1262 - } else {
1263 - set_transient('mxchat_admin_notice_error',
1264 - esc_html__('Failed to queue PDF processing: ', 'mxchat') . esc_html($result),
1265 - 30
1266 - );
1267 - }
1268 -
1269 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1270 - exit;
1271 - }
1272 -
1273 - // Handle Sitemap XML
1274 - if (strpos($content_type, 'xml') !== false || strpos($body_content, '<urlset') !== false) {
1275 - libxml_use_internal_errors(true);
1276 - $xml = simplexml_load_string($body_content);
1277 - $xml_errors = libxml_get_errors();
1278 - libxml_clear_errors();
1279 -
1280 - if ($xml === false || !empty($xml_errors)) {
1281 - set_transient('mxchat_admin_notice_error',
1282 - esc_html__('Invalid sitemap XML. Please provide a valid sitemap.', 'mxchat'),
1283 - 30
1284 - );
1285 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1286 - exit;
1287 - }
1288 -
1289 - $result = $this->mxchat_handle_sitemap_for_knowledge_base($xml, $submitted_url, $bot_id);
1290 -
1291 - if ($result === 'queued') {
1292 - set_transient('mxchat_admin_notice_success',
1293 - esc_html__('Sitemap queued for processing. Processing will start automatically.', 'mxchat'),
1294 - 30
1295 - );
1296 - } else {
1297 - set_transient('mxchat_admin_notice_error',
1298 - esc_html__('Failed to queue sitemap processing. Please check the status below for details.', 'mxchat'),
1299 - 30
1300 - );
1301 - }
1302 -
1303 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1304 - exit;
1305 - }
1306 -
1307 - // Handle Regular URL (single page)
1308 - $page_content = $this->mxchat_extract_main_content($body_content);
1309 - $sanitized_content = $this->mxchat_sanitize_content_for_api($page_content);
1310 -
1311 - //error_log('[MXCHAT-URL-DEBUG] URL: ' . $submitted_url);
1312 - //error_log('[MXCHAT-URL-DEBUG] Extracted page_content length: ' . strlen($page_content));
1313 - //error_log('[MXCHAT-URL-DEBUG] Sanitized content length: ' . strlen($sanitized_content));
1314 - //error_log('[MXCHAT-URL-DEBUG] Content preview: ' . substr($sanitized_content, 0, 500));
1315 -
1316 - if (empty($sanitized_content)) {
1317 - set_transient('mxchat_admin_notice_error',
1318 - esc_html__('No valid content found on the provided URL.', 'mxchat'),
1319 - 30
1320 - );
1321 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1322 - exit;
1323 - }
1324 -
1325 - // For single URLs, process immediately using submit_content_to_db
1326 - // This handles chunking automatically for large content
1327 - $db_result = MxChat_Utils::submit_content_to_db(
1328 - $sanitized_content,
1329 - $submitted_url,
1330 - $api_key,
1331 - null,
1332 - $bot_id,
1333 - 'url' // content_type
1334 - );
1335 -
1336 - if (is_wp_error($db_result)) {
1337 - $error_message = esc_html__('Failed to store content in database: ', 'mxchat') . esc_html($db_result->get_error_message());
1338 - set_transient('mxchat_admin_notice_error', $error_message, 30);
1339 - } else {
1340 - $success_message = esc_html__('URL content successfully submitted!', 'mxchat');
1341 - set_transient('mxchat_admin_notice_success', $success_message, 30);
1342 - }
1343 -
1344 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1345 - exit;
1346 -}
1347 -
1348 -
1349 -public function mxchat_get_single_url_status() {
1350 - $status = get_transient('mxchat_single_url_status');
1351 - if (!$status) {
1352 - return null;
1353 - }
1354 -
1355 - // Add human-readable time
1356 - if (isset($status['timestamp'])) {
1357 - $status['human_time'] = human_time_diff(strtotime($status['timestamp']), current_time('timestamp')) . ' ' . __('ago', 'mxchat');
1358 - }
1359 -
1360 - return $status;
1361 -}
1362 -
1363 -public function mxchat_handle_sitemap_for_knowledge_base($xml, $sitemap_url, $bot_id = 'default') {
1364 - if (!current_user_can('manage_options')) {
1365 - return false;
1366 - }
1367 -
1368 - try {
1369 - $sitemap_url = esc_url_raw($sitemap_url);
1370 -
1371 - if (!$xml || !is_object($xml)) {
1372 - throw new Exception(__('Invalid XML object provided', 'mxchat'));
1373 - }
1374 -
1375 - // Get bot-specific embedding API for validation
1376 - $bot_options = $this->get_bot_options($bot_id);
1377 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1378 -
1379 - // Test the embedding API before processing
1380 - $test_phrase = "Test embedding generation for MxChat";
1381 - $test_result = $this->mxchat_generate_embedding($test_phrase, $bot_id);
1382 -
1383 - if (is_string($test_result)) {
1384 - throw new Exception(__('Embedding API validation failed: ', 'mxchat') . $test_result);
1385 - }
1386 -
1387 - if (!is_array($test_result)) {
1388 - throw new Exception(__('Embedding API returned unexpected result type. Please check your configuration.', 'mxchat'));
1389 - }
1390 -
1391 - // Extract URLs from sitemap
1392 - $urls = array();
1393 - foreach ($xml->url as $url_element) {
1394 - $url = esc_url_raw((string)$url_element->loc);
1395 - if ($url) {
1396 - $urls[] = array('url' => $url);
1397 - }
1398 - }
1399 -
1400 - $total_urls = count($urls);
1401 -
1402 - if ($total_urls < 1) {
1403 - throw new Exception(__('No valid URLs found in sitemap', 'mxchat'));
1404 - }
1405 -
1406 - // Create unique queue ID
1407 - $queue_id = 'sitemap_' . md5($sitemap_url . time());
1408 -
1409 - // Add URLs to queue
1410 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'url', $urls, $bot_id);
1411 -
1412 - if ($queued_count === 0) {
1413 - throw new Exception(__('Failed to add URLs to processing queue', 'mxchat'));
1414 - }
1415 -
1416 - // Store queue metadata
1417 - $this->mxchat_set_queue_meta($queue_id, 'source_url', $sitemap_url);
1418 - $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'sitemap');
1419 - $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_urls);
1420 - $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
1421 - $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
1422 -
1423 - // Store queue ID in transient for status tracking
1424 - set_transient('mxchat_active_queue_sitemap', $queue_id, DAY_IN_SECONDS);
1425 - set_transient('mxchat_last_sitemap_url', $sitemap_url, DAY_IN_SECONDS);
1426 -
1427 - return 'queued';
1428 -
1429 - } catch (Exception $e) {
1430 - $error_message = $e->getMessage();
1431 - //error_log(sprintf(esc_html__('Error preparing sitemap for processing: %s', 'mxchat'), esc_html($error_message)));
1432 -
1433 - return $error_message;
1434 - }
1435 -
1436 -}
1437 -
1438 -/**
1439 - * Remove shortcode tags but preserve the content inside them
1440 - * Example: [vc_column]Hello World[/vc_column] becomes "Hello World"
1441 - *
1442 - * @param string $content The content containing shortcodes
1443 - * @return string Content with shortcode tags removed but inner content preserved
1444 - */
1445 -private function strip_shortcode_tags_preserve_content($content) {
1446 - // Single-pass regex removes all shortcode brackets: [tag], [tag attr="val"], [/tag], [tag /]
1447 - // Content between tags is inherently preserved since only brackets are targeted
1448 - $result = preg_replace('/\[\/?\w[\w-]*[^\]]*\]/', '', $content);
1449 - return ($result !== null) ? $result : $content;
1450 -}
1451 -
1452 -public function mxchat_sanitize_content_for_api($content) {
1453 - //error_log('[MXCHAT-SANITIZE] Original content preview: ' . substr($content, 0, 500) . '...');
1454 -
1455 - // Remove shortcode tags but PRESERVE content inside them
1456 - $content = $this->strip_shortcode_tags_preserve_content($content);
1457 -
1458 - // Remove script, style tags, and HTML comments
1459 - $content = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $content);
1460 - $content = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $content);
1461 - $content = preg_replace('/<!--(.|\s)*?-->/', '', $content);
1462 -
1463 - // Remove all HTML tags and decode HTML entities
1464 - $content = wp_strip_all_tags($content);
1465 - $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5);
1466 -
1467 - // Normalize whitespace but preserve paragraph breaks
1468 - // First, normalize line endings to \n
1469 - $content = str_replace(["\r\n", "\r"], "\n", $content);
1470 - // Replace multiple spaces/tabs with single space, but preserve newlines
1471 - $content = preg_replace('/[ \t]+/', ' ', $content);
1472 - // Replace 3+ newlines with 2 newlines (max 2 blank lines)
1473 - $content = preg_replace('/\n{3,}/', "\n\n", $content);
1474 - // Trim each line
1475 - $lines = explode("\n", $content);
1476 - $lines = array_map('trim', $lines);
1477 - $content = implode("\n", $lines);
1478 - // Final trim
1479 - $content = trim($content);
1480 -
1481 - // Remove control characters (which can cause database issues) but preserve \n (0x0A) and \r (0x0D)
1482 - $content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $content);
1483 -
1484 - // Remove NULL bytes which can cause database errors
1485 - $content = str_replace("\0", "", $content);
1486 -
1487 - // Ensure valid UTF-8 encoding
1488 - $content = wp_check_invalid_utf8($content);
1489 -
1490 - // Remove any extremely long strings without spaces (often garbage)
1491 - $content = preg_replace('/\S{300,}/', ' ', $content);
1492 -
1493 - // Replace problematic characters that often cause database issues
1494 - $content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content); // Remove emoji and other high Unicode characters
1495 -
1496 - // Replace any remaining potentially problematic characters with spaces
1497 - // BUT preserve newlines by temporarily replacing them
1498 - $content = str_replace("\n", "NEWLINE_PLACEHOLDER", $content);
1499 - $content = preg_replace('/[^\p{L}\p{N}\p{P}\p{Z}\p{Sm}]/u', ' ', $content);
1500 - $content = str_replace("NEWLINE_PLACEHOLDER", "\n", $content);
1501 -
1502 - // Limit to reasonable length if needed
1503 - $max_length = 65000; // Just under MySQL TEXT field limit
1504 - if (strlen($content) > $max_length) {
1505 - $content = substr($content, 0, $max_length);
1506 - }
1507 -
1508 - //error_log('[MXCHAT-SANITIZE] Sanitized content preview: ' . substr($content, 0, 500) . '...');
1509 - return $content;
1510 -}
1511 -public function mxchat_extract_main_content($html) {
1512 - if (empty($html)) {
1513 - return '';
1514 - }
1515 - try {
1516 - $dom = new DOMDocument;
1517 - libxml_use_internal_errors(true); // Suppress HTML parsing errors
1518 - @$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
1519 - $xpath = new DOMXPath($dom);
1520 -
1521 - // For debugging purposes
1522 - $debugEnabled = true; // Set to true to enable debugging output
1523 - $debug = function($message) use ($debugEnabled) {
1524 - if ($debugEnabled) {
1525 - //error_log('[MXCHAT-EXTRACT-DEBUG] ' . $message);
1526 - }
1527 - };
1528 -
1529 - // Direct targeting for Gerow theme posts
1530 - $post_text = $xpath->query('//div[contains(@class, "post-text")]');
1531 - if ($post_text && $post_text->length > 0) {
1532 - $debug("Found post-text directly");
1533 - $content = '';
1534 - foreach ($post_text as $node) {
1535 - $content .= $dom->saveHTML($node);
1536 - }
1537 - if (!empty($content)) {
1538 - $debug("Returning post-text content");
1539 - return $content;
1540 - }
1541 - }
1542 -
1543 - // Try to get the blog details content which contains the post-text
1544 - $blog_details = $xpath->query('//div[contains(@class, "blog-details-content")]');
1545 - if ($blog_details && $blog_details->length > 0) {
1546 - $debug("Found blog-details-content");
1547 - $content = '';
1548 - foreach ($blog_details as $node) {
1549 - $content .= $dom->saveHTML($node);
1550 - }
1551 - if (!empty($content)) {
1552 - $debug("Returning blog-details-content");
1553 - return $content;
1554 - }
1555 - }
1556 -
1557 - // Try to get the article which contains the blog details
1558 - $article = $xpath->query('//article[contains(@class, "blog-details-wrap")]');
1559 - if ($article && $article->length > 0) {
1560 - $debug("Found article with blog-details-wrap");
1561 - $content = '';
1562 - foreach ($article as $node) {
1563 - $content .= $dom->saveHTML($node);
1564 - }
1565 - if (!empty($content)) {
1566 - $debug("Returning article content");
1567 - return $content;
1568 - }
1569 - }
1570 -
1571 - // Try even broader with the blog-item-wrap
1572 - $blog_item = $xpath->query('//div[contains(@class, "blog-item-wrap")]');
1573 - if ($blog_item && $blog_item->length > 0) {
1574 - $debug("Found blog-item-wrap");
1575 - $content = '';
1576 - foreach ($blog_item as $node) {
1577 - $content .= $dom->saveHTML($node);
1578 - }
1579 - if (!empty($content)) {
1580 - $debug("Returning blog-item-wrap content");
1581 - return $content;
1582 - }
1583 - }
1584 -
1585 - // Specific Gerow theme path
1586 - $gerow_path = $xpath->query('//section[contains(@class, "blog-area")]//div[contains(@class, "post-text")]');
1587 - if ($gerow_path && $gerow_path->length > 0) {
1588 - $debug("Found Gerow theme path to post-text");
1589 - $content = '';
1590 - foreach ($gerow_path as $node) {
1591 - $content .= $dom->saveHTML($node);
1592 - }
1593 - if (!empty($content)) {
1594 - $debug("Returning Gerow post-text content");
1595 - return $content;
1596 - }
1597 - }
1598 -
1599 - // Generic blog post selectors
1600 - $selectors = [
1601 - // Blog post specific selectors
1602 - '//div[contains(@class, "post-text")]',
1603 - '//article[contains(@class, "blog-post-item")]//div[contains(@class, "post-text")]',
1604 - '//div[contains(@class, "blog-details-content")]',
1605 - '//article[contains(@class, "blog-details-wrap")]',
1606 - '//div[contains(@class, "entry-content")]',
1607 - '//div[contains(@class, "blog-content")]',
1608 - '//div[contains(@class, "blog-item-wrap")]',
1609 -
1610 - // More general content selectors
1611 - '//div[contains(@class, "page__content")]',
1612 - '//div[contains(@class, "elementor-widget-container")]',
1613 - '//div[contains(@class, "elementor-text-editor")]',
1614 - '//div[contains(@class, "elementor-widget-text-editor")]',
1615 - '//*[contains(@class, "entry-content")]',
1616 - '//*[contains(@class, "post-content")]',
1617 - '//*[contains(@class, "article-content")]',
1618 - '//*[@id="content"]',
1619 - '//*[@id="main-content"]',
1620 - '//section[contains(@class, "blog-area")]',
1621 - '//article',
1622 - '//main',
1623 - '//div[contains(@class, "content")]'
1624 - ];
1625 -
1626 - // First handle Elementor content - get only leaf widget containers to avoid duplicates
1627 - $debug("Checking for Elementor content");
1628 - // Get widget containers that are direct children of widgets (not nested inside other widget containers)
1629 - $elementor_widgets = $xpath->query('//div[contains(@class, "elementor-widget")]//div[contains(@class, "elementor-widget-container")]');
1630 - if ($elementor_widgets && $elementor_widgets->length > 0) {
1631 - $debug("Found Elementor widgets");
1632 - $seen_content = array(); // Track seen content to avoid duplicates
1633 - $combined_content = '';
1634 - foreach ($elementor_widgets as $widget) {
1635 - $widget_content = $dom->saveHTML($widget);
1636 - if (!empty($widget_content)) {
1637 - // Create a hash of the content to detect duplicates
1638 - $content_hash = md5($widget_content);
1639 - if (!isset($seen_content[$content_hash])) {
1640 - $seen_content[$content_hash] = true;
1641 - $combined_content .= $widget_content;
1642 - }
1643 - }
1644 - }
1645 - if (!empty($combined_content)) {
1646 - $debug("Returning Elementor content");
1647 - return $combined_content;
1648 - }
1649 - }
1650 -
1651 - // Try standard selectors one by one
1652 - foreach ($selectors as $selector) {
1653 - $debug("Trying selector: " . $selector);
1654 - $nodes = $xpath->query($selector);
1655 - if ($nodes && $nodes->length > 0) {
1656 - $debug("Found " . $nodes->length . " matches for selector: " . $selector);
1657 - // Only take the FIRST matching node to avoid duplicate content
1658 - // (pages often have nested or multiple containers with same class)
1659 - $content = $dom->saveHTML($nodes->item(0));
1660 - if (!empty($content)) {
1661 - $debug("Returning content from selector: " . $selector . " (first match only)");
1662 - return $content;
1663 - }
1664 - }
1665 - }
1666 -
1667 - // Manual regex fallback for post-text if DOM methods fail
1668 - $debug("Trying regex fallback");
1669 - if (preg_match('/<div class="post-text">(.*?)<\/div>\s*<\/div>\s*<\/div>/s', $html, $matches)) {
1670 - $debug("Found post-text via regex");
1671 - return '<div class="post-text">' . $matches[1] . '</div>';
1672 - }
1673 -
1674 - // Try to extract the blog section as a whole
1675 - $blog_section = $xpath->query('//section[contains(@class, "blog-area")]');
1676 - if ($blog_section && $blog_section->length > 0) {
1677 - $debug("Found blog-area section");
1678 - $content = '';
1679 - foreach ($blog_section as $node) {
1680 - $content .= $dom->saveHTML($node);
1681 - }
1682 - if (!empty($content)) {
1683 - $debug("Returning blog-area section content");
1684 - return $content;
1685 - }
1686 - }
1687 -
1688 - // Generic container selectors for non-CMS sites (like .asp pages)
1689 - $debug("Trying generic container selectors");
1690 - $generic_selectors = [
1691 - '//div[@id="main"]',
1692 - '//div[@id="wrapper"]',
1693 - '//div[@id="page"]',
1694 - '//div[@id="site-content"]',
1695 - '//div[contains(@class, "main-content")]',
1696 - '//div[contains(@class, "page-content")]',
1697 - '//div[contains(@class, "site-content")]',
1698 - ];
1699 -
1700 - foreach ($generic_selectors as $selector) {
1701 - $debug("Trying generic selector: " . $selector);
1702 - $nodes = $xpath->query($selector);
1703 - if ($nodes && $nodes->length > 0) {
1704 - $content = $dom->saveHTML($nodes->item(0));
1705 - if (!empty($content)) {
1706 - $debug("Returning content from generic selector: " . $selector);
1707 - return $content;
1708 - }
1709 - }
1710 - }
1711 -
1712 - // Paragraph-based content detection - find regions with substantial text
1713 - $debug("Trying paragraph-based content detection");
1714 - $paragraphs = $xpath->query('//p[string-length(normalize-space()) > 50]');
1715 - if ($paragraphs && $paragraphs->length >= 3) {
1716 - $debug("Found " . $paragraphs->length . " substantial paragraphs");
1717 - // Collect all substantial paragraphs and their content
1718 - $paragraph_content = '';
1719 - foreach ($paragraphs as $p) {
1720 - $paragraph_content .= $dom->saveHTML($p) . "\n";
1721 - }
1722 - if (!empty($paragraph_content)) {
1723 - $debug("Returning paragraph-based content");
1724 - return $paragraph_content;
1725 - }
1726 - }
1727 -
1728 - // Improved body fallback - strip nav/header/footer elements first
1729 - $debug("Using improved body fallback");
1730 - $body = $dom->getElementsByTagName('body');
1731 - if ($body->length > 0) {
1732 - // Clone the body to avoid modifying the original DOM
1733 - $body_clone = $body->item(0)->cloneNode(true);
1734 -
1735 - // Remove common non-content elements by tag name
1736 - $remove_tags = ['nav', 'header', 'footer', 'aside', 'script', 'style', 'noscript'];
1737 - foreach ($remove_tags as $tag) {
1738 - $elements = $body_clone->getElementsByTagName($tag);
1739 - // Iterate backwards to safely remove elements
1740 - for ($i = $elements->length - 1; $i >= 0; $i--) {
1741 - $el = $elements->item($i);
1742 - if ($el && $el->parentNode) {
1743 - $el->parentNode->removeChild($el);
1744 - }
1745 - }
1746 - }
1747 -
1748 - // Remove elements with common non-content class names using XPath on the cloned body
1749 - $temp_dom = new DOMDocument();
1750 - @$temp_dom->appendChild($temp_dom->importNode($body_clone, true));
1751 - $temp_xpath = new DOMXPath($temp_dom);
1752 -
1753 - $remove_class_patterns = [
1754 - '//*[contains(@class, "nav")]',
1755 - '//*[contains(@class, "menu")]',
1756 - '//*[contains(@class, "sidebar")]',
1757 - '//*[contains(@class, "footer")]',
1758 - '//*[contains(@class, "header")]',
1759 - '//*[contains(@id, "nav")]',
1760 - '//*[contains(@id, "menu")]',
1761 - '//*[contains(@id, "sidebar")]',
1762 - '//*[contains(@id, "footer")]',
1763 - '//*[contains(@id, "header")]',
1764 - ];
1765 -
1766 - foreach ($remove_class_patterns as $pattern) {
1767 - $elements = $temp_xpath->query($pattern);
1768 - if ($elements) {
1769 - for ($i = $elements->length - 1; $i >= 0; $i--) {
1770 - $el = $elements->item($i);
1771 - if ($el && $el->parentNode) {
1772 - $el->parentNode->removeChild($el);
1773 - }
1774 - }
1775 - }
1776 - }
1777 -
1778 - $cleaned_content = $temp_dom->saveHTML();
1779 - if (!empty($cleaned_content)) {
1780 - $debug("Returning cleaned body content");
1781 - return $cleaned_content;
1782 - }
1783 - }
1784 -
1785 - // Last resort: return the original HTML
1786 - $debug("Returning original HTML");
1787 - return $html;
1788 - } catch (Exception $e) {
1789 - //error_log('[MXCHAT-ERROR] Content extraction failed: ' . $e->getMessage());
1790 - return $html; // Return original HTML if parsing fails
1791 - } finally {
1792 - libxml_clear_errors();
1793 - }
1794 -}
1795 -public function mxchat_get_sitemap_processing_status($sitemap_url) {
1796 - $sitemap_url = esc_url_raw($sitemap_url);
1797 - $status_key = sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url));
1798 - $status = get_transient($status_key);
1799 -
1800 - if (!$status || !is_array($status)) {
1801 - return false;
1802 - }
1803 -
1804 - // Auto-complete check: if all URLs are processed but status isn't complete
1805 - if (isset($status['processed_urls']) && isset($status['total_urls']) &&
1806 - $status['processed_urls'] >= $status['total_urls'] &&
1807 - isset($status['status']) && $status['status'] !== 'complete' &&
1808 - $status['status'] !== 'error') {
1809 -
1810 - // Mark as complete
1811 - $status['status'] = 'complete';
1812 - $status['processed_urls'] = $status['total_urls']; // Ensure exact match
1813 -
1814 - // Update the transient with the corrected status
1815 - set_transient($status_key, $status, DAY_IN_SECONDS);
1816 - }
1817 -
1818 - return array(
1819 - 'total_urls' => absint($status['total_urls']),
1820 - 'processed_urls' => absint($status['processed_urls']),
1821 - 'failed_urls' => absint($status['failed_urls'] ?? 0),
1822 - 'percentage' => ($status['total_urls'] > 0)
1823 - ? round((absint($status['processed_urls']) / absint($status['total_urls'])) * 100)
1824 - : 0,
1825 - 'status' => sanitize_text_field($status['status']),
1826 - 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
1827 - 'error' => isset($status['error']) ? sanitize_text_field($status['error']) : '',
1828 - 'last_error' => isset($status['last_error']) ? sanitize_text_field($status['last_error']) : '',
1829 - 'failed_urls_list' => isset($status['failed_urls_list']) ? $status['failed_urls_list'] : array()
1830 - );
1831 -}
1832 -
1833 -public function mxchat_ajax_get_status_updates() {
1834 - try {
1835 - // Verify the request
1836 - check_ajax_referer('mxchat_status_nonce', 'nonce');
1837 -
1838 - // Get active queue IDs
1839 - $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
1840 - $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
1841 -
1842 - $sitemap_status = false;
1843 - $pdf_status = false;
1844 -
1845 - // Get sitemap queue status
1846 - if ($sitemap_queue_id) {
1847 - $sitemap_status = $this->mxchat_get_queue_status_data($sitemap_queue_id, 'sitemap');
1848 - }
1849 -
1850 - // Get PDF queue status
1851 - if ($pdf_queue_id) {
1852 - $pdf_status = $this->mxchat_get_queue_status_data($pdf_queue_id, 'pdf');
1853 - }
1854 -
1855 - $is_active_processing =
1856 - ($sitemap_status && $sitemap_status['status'] === 'processing') ||
1857 - ($pdf_status && $pdf_status['status'] === 'processing');
1858 -
1859 - // Return JSON response with the status data
1860 - wp_send_json(array(
1861 - 'pdf_status' => $pdf_status,
1862 - 'sitemap_status' => $sitemap_status,
1863 - 'is_processing' => $is_active_processing,
1864 - 'sitemap_queue_id' => $sitemap_queue_id,
1865 - 'pdf_queue_id' => $pdf_queue_id
1866 - ));
1867 -
1868 - } catch (Exception $e) {
1869 - //error_log('MxChat Status Update Error: ' . $e->getMessage());
1870 -
1871 - wp_send_json_error(array(
1872 - 'message' => 'Error getting status updates: ' . $e->getMessage(),
1873 - 'status' => 'error'
1874 - ));
1875 - }
1876 -}
1877 -
1878 -/**
1879 - * Helper function to get queue status data
1880 - */
1881 -private function mxchat_get_queue_status_data($queue_id, $type = 'sitemap') {
1882 - global $wpdb;
1883 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
1884 -
1885 - // Get counts by status
1886 - $counts = $wpdb->get_results($wpdb->prepare(
1887 - "SELECT status, COUNT(*) as count
1888 - FROM $table_name
1889 - WHERE queue_id = %s
1890 - GROUP BY status",
1891 - $queue_id
1892 - ), OBJECT_K);
1893 -
1894 - $total = 0;
1895 - $completed = 0;
1896 - $failed = 0;
1897 - $processing = 0;
1898 - $pending = 0;
1899 -
1900 - foreach ($counts as $status => $data) {
1901 - $count = absint($data->count);
1902 - $total += $count;
1903 -
1904 - switch ($status) {
1905 - case 'completed':
1906 - $completed = $count;
1907 - break;
1908 - case 'failed':
1909 - $failed = $count;
1910 - break;
1911 - case 'processing':
1912 - $processing = $count;
1913 - break;
1914 - case 'pending':
1915 - $pending = $count;
1916 - break;
1917 - }
1918 - }
1919 -
1920 - if ($total === 0) {
1921 - return false;
1922 - }
1923 -
1924 - // Calculate percentage
1925 - $percentage = round((($completed + $failed) / $total) * 100);
1926 -
1927 - // Get failed items details (limit to 50)
1928 - $failed_items = array();
1929 - if ($failed > 0) {
1930 - $failed_results = $wpdb->get_results($wpdb->prepare(
1931 - "SELECT item_type, item_data, error_message, attempts, completed_at
1932 - FROM $table_name
1933 - WHERE queue_id = %s
1934 - AND status = 'failed'
1935 - AND attempts >= max_attempts
1936 - ORDER BY id DESC
1937 - LIMIT 50",
1938 - $queue_id
1939 - ));
1940 -
1941 - foreach ($failed_results as $item) {
1942 - $data = json_decode($item->item_data, true);
1943 - $url = $type === 'pdf' ? ($data['pdf_url'] ?? '') : ($data['url'] ?? '');
1944 -
1945 - $failed_items[] = array(
1946 - 'url' => $url,
1947 - 'page' => $type === 'pdf' ? ($data['page_number'] ?? 0) : 0,
1948 - 'error' => $item->error_message,
1949 - 'retries' => $item->attempts,
1950 - 'time' => strtotime($item->completed_at)
1951 - );
1952 - }
1953 - }
1954 -
1955 - // Get queue metadata
1956 - $source_url = $this->mxchat_get_queue_meta($queue_id, 'source_url');
1957 -
1958 - // Determine if queue is complete
1959 - $is_complete = ($pending === 0 && $processing === 0);
1960 -
1961 - // Get last update time
1962 - $last_update = $wpdb->get_var($wpdb->prepare(
1963 - "SELECT MAX(UNIX_TIMESTAMP(COALESCE(completed_at, started_at, created_at)))
1964 - FROM $table_name
1965 - WHERE queue_id = %s",
1966 - $queue_id
1967 - ));
1968 -
1969 - $last_update_text = $last_update ? human_time_diff($last_update, time()) . ' ' . __('ago', 'mxchat') : __('Just now', 'mxchat');
1970 -
1971 - // Format based on type
1972 - if ($type === 'pdf') {
1973 - return array(
1974 - 'total_pages' => $total,
1975 - 'processed_pages' => $completed + $failed,
1976 - 'failed_pages' => $failed,
1977 - 'percentage' => $percentage,
1978 - 'status' => $is_complete ? 'complete' : 'processing',
1979 - 'last_update' => $last_update_text,
1980 - 'failed_pages_list' => $failed_items,
1981 - 'pdf_url' => $source_url,
1982 - 'queue_id' => $queue_id
1983 - );
1984 - } else {
1985 - return array(
1986 - 'total_urls' => $total,
1987 - 'processed_urls' => $completed + $failed,
1988 - 'failed_urls' => $failed,
1989 - 'percentage' => $percentage,
1990 - 'status' => $is_complete ? 'complete' : 'processing',
1991 - 'last_update' => $last_update_text,
1992 - 'failed_urls_list' => $failed_items,
1993 - 'sitemap_url' => $source_url,
1994 - 'queue_id' => $queue_id
1995 - );
1996 - }
1997 -}
1998 -
1999 -/**
2000 - * Public method to get processing status for both sitemap and PDF queues
2001 - * Used by admin pages to display processing status
2002 - *
2003 - * @return array Array with 'sitemap_status', 'pdf_status', and 'is_processing' keys
2004 - */
2005 -public function mxchat_get_processing_statuses() {
2006 - $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
2007 - $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
2008 -
2009 - $sitemap_status = false;
2010 - $pdf_status = false;
2011 -
2012 - if ($sitemap_queue_id) {
2013 - $sitemap_status = $this->mxchat_get_queue_status_data($sitemap_queue_id, 'sitemap');
2014 - }
2015 -
2016 - if ($pdf_queue_id) {
2017 - $pdf_status = $this->mxchat_get_queue_status_data($pdf_queue_id, 'pdf');
2018 - }
2019 -
2020 - $is_processing =
2021 - ($sitemap_status && $sitemap_status['status'] === 'processing') ||
2022 - ($pdf_status && $pdf_status['status'] === 'processing');
2023 -
2024 - return array(
2025 - 'sitemap_status' => $sitemap_status,
2026 - 'pdf_status' => $pdf_status,
2027 - 'is_processing' => $is_processing
2028 - );
2029 -}
2030 -
2031 -/**
2032 - * AJAX handler to get recent knowledge entries for real-time table updates
2033 - * UPDATED: Now supports both WordPress DB and Pinecone data sources
2034 - */
2035 -public function ajax_mxchat_get_recent_entries() {
2036 - check_ajax_referer('mxchat_entries_nonce', 'nonce');
2037 -
2038 - if (!current_user_can('manage_options')) {
2039 - wp_send_json_error(array('message' => 'Unauthorized'));
2040 - return;
2041 - }
2042 -
2043 - global $wpdb;
2044 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2045 -
2046 - // Get parameters
2047 - $last_id = isset($_POST['last_id']) ? absint($_POST['last_id']) : 0;
2048 - $limit = isset($_POST['limit']) ? min(absint($_POST['limit']), 50) : 10;
2049 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
2050 -
2051 - // Check if Pinecone is enabled for this bot
2052 - $pinecone_manager = $this->mxchat_get_pinecone_manager();
2053 - $pinecone_options = $pinecone_manager ? $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id) : array();
2054 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
2055 - $has_pinecone_api = !empty($pinecone_options['mxchat_pinecone_api_key']);
2056 -
2057 - if ($use_pinecone && $has_pinecone_api) {
2058 - // PINECONE DATA SOURCE - Get grouped entry count (not raw vector count)
2059 - // Use mxchat_fetch_pinecone_records which returns total_unique_entries
2060 - $records = $pinecone_manager->mxchat_fetch_pinecone_records($pinecone_options, '', 1, 10, $bot_id, '');
2061 - $total_count = $records['total'] ?? 0;
2062 -
2063 - // For Pinecone, we don't return individual entries during polling
2064 - // (entries are already displayed on page load via mxchat_fetch_pinecone_records)
2065 - // We just return the updated count
2066 - wp_send_json_success(array(
2067 - 'entries' => array(),
2068 - 'total_count' => absint($total_count),
2069 - 'max_id' => $last_id,
2070 - 'data_source' => 'pinecone'
2071 - ));
2072 - return;
2073 - }
2074 -
2075 - // WORDPRESS DB DATA SOURCE
2076 - // Build query to get entries newer than last_id
2077 - $where_clauses = array('1=1');
2078 - $where_values = array();
2079 -
2080 - if ($last_id > 0) {
2081 - $where_clauses[] = 'id > %d';
2082 - $where_values[] = $last_id;
2083 - }
2084 -
2085 - // Note: WordPress DB table doesn't have bot_id column
2086 - // Multi-bot filtering is handled via Pinecone namespaces
2087 -
2088 - $where_sql = implode(' AND ', $where_clauses);
2089 -
2090 - // Get recent entries
2091 - $query = "SELECT id, article_content, source_url, timestamp
2092 - FROM $table_name
2093 - WHERE $where_sql
2094 - ORDER BY id DESC
2095 - LIMIT %d";
2096 -
2097 - $where_values[] = $limit;
2098 -
2099 - $entries = $wpdb->get_results($wpdb->prepare($query, $where_values));
2100 -
2101 - // Get total count of GROUPED entries (by source_url) - matches pagination display
2102 - // Count unique source_urls (excluding mxchat:// internal refs) + count of ungrouped rows
2103 - $total_count = $wpdb->get_var(
2104 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} WHERE source_url != '' AND source_url NOT LIKE 'mxchat://%') +
2105 - (SELECT COUNT(*) FROM {$table_name} WHERE source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%')"
2106 - );
2107 -
2108 - // Format entries for response
2109 - $formatted_entries = array();
2110 - $preview_length = 150;
2111 - foreach ($entries as $entry) {
2112 - // Parse chunk metadata using the proper chunker method (same as initial page load)
2113 - if (class_exists('MxChat_Chunker')) {
2114 - $chunk_meta = MxChat_Chunker::parse_stored_chunk($entry->article_content);
2115 - $display_content = $chunk_meta['text'];
2116 - $chunk_metadata = $chunk_meta['metadata'];
2117 - } else {
2118 - $display_content = $entry->article_content;
2119 - $chunk_metadata = array();
2120 - }
2121 -
2122 - $content_preview = mb_strlen($display_content) > $preview_length
2123 - ? mb_substr($display_content, 0, $preview_length) . '...'
2124 - : $display_content;
2125 -
2126 - $formatted_entries[] = array(
2127 - 'id' => $entry->id,
2128 - 'preview' => esc_html($content_preview),
2129 - 'full_content' => wp_kses_post(wpautop($display_content)),
2130 - 'content_length' => mb_strlen($display_content),
2131 - 'preview_length' => $preview_length,
2132 - 'source_url' => $entry->source_url,
2133 - 'has_link' => !empty($entry->source_url) && strpos($entry->source_url, 'mxchat://') !== 0,
2134 - 'chunk_metadata' => $chunk_metadata,
2135 - 'bot_id' => $entry->bot_id ?? 'default',
2136 - 'edit_nonce' => wp_create_nonce('mxchat_edit_entry_nonce'),
2137 - 'delete_nonce' => wp_create_nonce('mxchat_delete_prompt_nonce')
2138 - );
2139 - }
2140 -
2141 - wp_send_json_success(array(
2142 - 'entries' => $formatted_entries,
2143 - 'total_count' => absint($total_count),
2144 - 'max_id' => !empty($entries) ? $entries[0]->id : $last_id,
2145 - 'data_source' => 'wordpress'
2146 - ));
2147 -}
2148 -
2149 -/**
2150 - * Get Pinecone total count from stats API
2151 - * Helper function for ajax_mxchat_get_recent_entries
2152 - */
2153 -private function mxchat_get_pinecone_count_from_stats($pinecone_options) {
2154 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2155 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
2156 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
2157 -
2158 - if (empty($api_key) || empty($host)) {
2159 - return 0;
2160 - }
2161 -
2162 - try {
2163 - $stats_url = "https://{$host}/describe_index_stats";
2164 -
2165 - $response = wp_remote_post($stats_url, array(
2166 - 'headers' => array(
2167 - 'Api-Key' => $api_key,
2168 - 'Content-Type' => 'application/json'
2169 - ),
2170 - 'body' => '{}',
2171 - 'timeout' => 10
2172 - ));
2173 -
2174 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
2175 - $body = wp_remote_retrieve_body($response);
2176 - $stats_data = json_decode($body, true);
2177 -
2178 - // If namespace is specified, get count from that specific namespace
2179 - if (!empty($namespace) && isset($stats_data['namespaces'][$namespace]['vectorCount'])) {
2180 - return intval($stats_data['namespaces'][$namespace]['vectorCount']);
2181 - }
2182 -
2183 - // If no namespace specified or namespace not found in response, use total
2184 - return intval($stats_data['totalVectorCount'] ?? 0);
2185 - }
2186 -
2187 - return 0;
2188 -
2189 - } catch (Exception $e) {
2190 - return 0;
2191 - }
2192 -}
2193 -
2194 -/**
2195 - * AJAX handler to refresh Pinecone entries table via AJAX
2196 - * Returns the table HTML for updating the UI without a full page reload
2197 - */
2198 -public function ajax_mxchat_refresh_pinecone_entries() {
2199 - check_ajax_referer('mxchat_entries_nonce', 'nonce');
2200 -
2201 - if (!current_user_can('manage_options')) {
2202 - wp_send_json_error(array('message' => 'Unauthorized'));
2203 - return;
2204 - }
2205 -
2206 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
2207 - $page = isset($_POST['page']) ? absint($_POST['page']) : 1;
2208 - $per_page = 25;
2209 - $search_query = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
2210 - $content_type_filter = isset($_POST['content_type']) ? sanitize_key($_POST['content_type']) : '';
2211 -
2212 - // Get Pinecone manager and options
2213 - $pinecone_manager = $this->mxchat_get_pinecone_manager();
2214 - if (!$pinecone_manager) {
2215 - wp_send_json_error(array('message' => 'Pinecone manager not available'));
2216 - return;
2217 - }
2218 -
2219 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
2220 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
2221 - $pinecone_api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2222 -
2223 - if (!$use_pinecone || empty($pinecone_api_key)) {
2224 - wp_send_json_error(array('message' => 'Pinecone not configured'));
2225 - return;
2226 - }
2227 -
2228 - // Fetch records from Pinecone
2229 - $records = $pinecone_manager->mxchat_fetch_pinecone_records($pinecone_options, $search_query, $page, $per_page, $bot_id, $content_type_filter);
2230 - $prompts = $records['data'] ?? array();
2231 - $total_records = $records['total'] ?? 0;
2232 -
2233 - // Preprocess Pinecone records — set chunk_metadata and display_content
2234 - // (matches admin-knowledge-page.php preprocessing)
2235 - foreach ($prompts as $prompt) {
2236 - if (isset($prompt->chunk_index) && $prompt->chunk_index !== null) {
2237 - $prompt->chunk_metadata = array(
2238 - 'chunk_index' => intval($prompt->chunk_index),
2239 - 'total_chunks' => isset($prompt->total_chunks) ? intval($prompt->total_chunks) : null,
2240 - 'is_chunked' => isset($prompt->is_chunked) ? (bool) $prompt->is_chunked : true,
2241 - 'source_url' => $prompt->source_url ?? ''
2242 - );
2243 - $prompt->display_content = $prompt->article_content;
2244 - } else {
2245 - $prompt->chunk_metadata = array();
2246 - $prompt->display_content = $prompt->article_content ?? '';
2247 - }
2248 - }
2249 -
2250 - // Group prompts by source_url
2251 - $grouped_prompts = array();
2252 - foreach ($prompts as $prompt) {
2253 - $source_url = '';
2254 - if (!empty($prompt->chunk_metadata['source_url'])) {
2255 - $source_url = $prompt->chunk_metadata['source_url'];
2256 - } elseif (!empty($prompt->source_url)) {
2257 - $source_url = $prompt->source_url;
2258 - }
2259 -
2260 - if (!empty($source_url)) {
2261 - if (!isset($grouped_prompts[$source_url])) {
2262 - $grouped_prompts[$source_url] = array();
2263 - }
2264 - $grouped_prompts[$source_url][] = $prompt;
2265 - } else {
2266 - $grouped_prompts['_ungrouped_' . $prompt->id] = array($prompt);
2267 - }
2268 - }
2269 -
2270 - // Sort each group by chunk_index
2271 - foreach ($grouped_prompts as $source_url => &$group) {
2272 - usort($group, function($a, $b) {
2273 - $index_a = isset($a->chunk_metadata['chunk_index']) ? intval($a->chunk_metadata['chunk_index']) : 0;
2274 - $index_b = isset($b->chunk_metadata['chunk_index']) ? intval($b->chunk_metadata['chunk_index']) : 0;
2275 - return $index_a - $index_b;
2276 - });
2277 - }
2278 - unset($group);
2279 -
2280 - // Build HTML for the table rows - must match admin-knowledge-page.php structure exactly
2281 - ob_start();
2282 - $display_index = 0;
2283 - $current_page = $page;
2284 - $data_source = 'pinecone';
2285 - $current_bot_id = $bot_id;
2286 - $preview_length = 150;
2287 -
2288 - if (empty($grouped_prompts)) {
2289 - echo '<tr><td colspan="4" style="padding: 40px; text-align: center; color: var(--mxch-text-muted);">';
2290 - esc_html_e('No knowledge entries found in Pinecone.', 'mxchat');
2291 - echo '</td></tr>';
2292 - } else {
2293 - foreach ($grouped_prompts as $source_url => $group) {
2294 - $chunk_count = count($group);
2295 - $first_prompt = $group[0];
2296 - $display_index++;
2297 -
2298 - if ($chunk_count > 1) {
2299 - // Multiple chunks - show grouped row with expand button
2300 - $group_id = 'group-' . md5($source_url);
2301 - ?>
2302 - <tr id="prompt-<?php echo esc_attr($first_prompt->id); ?>"
2303 - class="mxchat-chunk-group-header"
2304 - data-source="<?php echo esc_attr($data_source); ?>"
2305 - data-group-id="<?php echo esc_attr($group_id); ?>"
2306 - style="border-bottom: 1px solid var(--mxch-card-border); background: rgba(33, 150, 243, 0.02);">
2307 - <td style="padding: 12px 16px; text-align: center;">
2308 - <input type="checkbox"
2309 - class="mxchat-entry-checkbox"
2310 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
2311 - data-source="<?php echo esc_attr($data_source); ?>"
2312 - data-source-url="<?php echo esc_attr($source_url); ?>"
2313 - data-is-group="true"
2314 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>">
2315 - </td>
2316 - <td style="padding: 12px 16px; font-size: 13px;">
2317 - <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
2318 - </td>
2319 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
2320 - <div class="mxchat-chunk-group-info">
2321 - <button type="button" class="mxchat-chunk-toggle" data-group-id="<?php echo esc_attr($group_id); ?>">
2322 - <span class="dashicons dashicons-arrow-right-alt2"></span>
2323 - </button>
2324 - <span class="mxchat-chunk-badge"><?php echo esc_html($chunk_count); ?> <?php esc_html_e('chunks', 'mxchat'); ?></span>
2325 - <span class="mxchat-chunk-preview">
2326 - <?php
2327 - $parent_content = isset($first_prompt->display_content) ? $first_prompt->display_content : (isset($first_prompt->article_content) ? $first_prompt->article_content : '');
2328 - $content_preview = mb_substr($parent_content, 0, 100);
2329 - echo esc_html($content_preview . '...');
2330 - ?>
2331 - </span>
2332 - </div>
2333 - </td>
2334 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
2335 - <?php if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0 && strpos($source_url, '_ungrouped_') !== 0) : ?>
2336 - <a href="<?php echo esc_url($source_url); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
2337 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
2338 - <?php esc_html_e('View Source', 'mxchat'); ?>
2339 - </a>
2340 - <?php else : ?>
2341 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
2342 - <?php endif; ?>
2343 - </td>
2344 - <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
2345 - <?php if ($data_source !== 'pinecone') : ?>
2346 - <button type="button"
2347 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
2348 - data-source-url="<?php echo esc_attr($source_url); ?>"
2349 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
2350 - data-data-source="<?php echo esc_attr($data_source); ?>"
2351 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2352 - data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
2353 - title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
2354 - <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
2355 - </button>
2356 - <?php endif; ?>
2357 - <button type="button"
2358 - class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
2359 - data-source-url="<?php echo esc_attr($source_url); ?>"
2360 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
2361 - data-data-source="<?php echo esc_attr($data_source); ?>"
2362 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2363 - data-nonce="<?php echo wp_create_nonce('mxchat_delete_chunks_nonce'); ?>"
2364 - style="color: var(--mxch-error);"
2365 - title="<?php esc_attr_e('Delete all chunks', 'mxchat'); ?>">
2366 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
2367 - </button>
2368 - </td>
2369 - </tr>
2370 - <?php
2371 - // Render hidden chunk rows
2372 - foreach ($group as $chunk_index => $chunk) {
2373 - $meta_chunk_index = isset($chunk->chunk_metadata['chunk_index']) ? intval($chunk->chunk_metadata['chunk_index']) : $chunk_index;
2374 - $meta_total_chunks = isset($chunk->chunk_metadata['total_chunks']) ? intval($chunk->chunk_metadata['total_chunks']) : $chunk_count;
2375 - $content = isset($chunk->display_content) ? $chunk->display_content : (isset($chunk->article_content) ? $chunk->article_content : '');
2376 - $content_preview = mb_strlen($content) > $preview_length
2377 - ? mb_substr($content, 0, $preview_length) . '...'
2378 - : $content;
2379 - ?>
2380 - <tr id="prompt-<?php echo esc_attr($chunk->id); ?>"
2381 - class="mxchat-chunk-row <?php echo esc_attr($group_id); ?>"
2382 - data-source="<?php echo esc_attr($data_source); ?>"
2383 - style="display: none; background: #f8f9fa; border-bottom: 1px solid var(--mxch-card-border);">
2384 - <td style="padding: 12px 16px; text-align: center;">
2385 - <!-- Checkbox column placeholder for chunks (managed by group) -->
2386 - </td>
2387 - <td style="padding: 12px 16px 12px 30px; font-size: 13px;">
2388 - <!-- Hidden ID column for chunks -->
2389 - </td>
2390 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
2391 - <div class="mxchat-accordion-wrapper">
2392 - <div class="mxchat-content-preview">
2393 - <span class="mxchat-chunk-indicator" style="margin-right: 10px; color: var(--mxch-text-secondary); font-size: 12px;">
2394 - <?php printf(esc_html__('Chunk %d of %d', 'mxchat'), $meta_chunk_index + 1, $meta_total_chunks); ?>
2395 - </span>
2396 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
2397 - <?php if (mb_strlen($content) > $preview_length) : ?>
2398 - <button class="mxchat-expand-toggle" type="button">
2399 - <span class="dashicons dashicons-arrow-down-alt2"></span>
2400 - </button>
2401 - <?php endif; ?>
2402 - </div>
2403 - <div class="mxchat-content-full" style="display: none;">
2404 - <div class="content-view">
2405 - <?php
2406 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
2407 - echo '<div dir="rtl" lang="he" class="rtl-content">';
2408 - echo wp_kses_post(wpautop($content));
2409 - echo '</div>';
2410 - } else {
2411 - echo wp_kses_post(wpautop($content));
2412 - }
2413 - ?>
2414 - </div>
2415 - </div>
2416 - </div>
2417 - </td>
2418 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
2419 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Same as parent', 'mxchat'); ?></span>
2420 - </td>
2421 - <td class="mxchat-actions-cell" style="padding: 12px 16px;">
2422 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Managed by group', 'mxchat'); ?></span>
2423 - </td>
2424 - </tr>
2425 - <?php
2426 - }
2427 - } else {
2428 - // Single entry - display normally with accordion
2429 - $prompt = $first_prompt;
2430 - $content = isset($prompt->display_content) ? $prompt->display_content : (isset($prompt->article_content) ? $prompt->article_content : '');
2431 - $content_preview = mb_strlen($content) > $preview_length
2432 - ? mb_substr($content, 0, $preview_length) . '...'
2433 - : $content;
2434 - ?>
2435 - <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
2436 - data-source="<?php echo esc_attr($data_source); ?>"
2437 - style="border-bottom: 1px solid var(--mxch-card-border); background: rgba(33, 150, 243, 0.02);">
2438 - <td style="padding: 12px 16px; text-align: center;">
2439 - <input type="checkbox"
2440 - class="mxchat-entry-checkbox"
2441 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
2442 - data-source="<?php echo esc_attr($data_source); ?>"
2443 - data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
2444 - data-is-group="false"
2445 - data-chunk-count="1">
2446 - </td>
2447 - <td style="padding: 12px 16px; font-size: 13px;">
2448 - <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
2449 - </td>
2450 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
2451 - <div class="mxchat-accordion-wrapper">
2452 - <div class="mxchat-content-preview">
2453 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
2454 - <?php if (mb_strlen($content) > $preview_length) : ?>
2455 - <button class="mxchat-expand-toggle" type="button">
2456 - <span class="dashicons dashicons-arrow-down-alt2"></span>
2457 - </button>
2458 - <?php endif; ?>
2459 - </div>
2460 - <div class="mxchat-content-full" style="display: none;">
2461 - <div class="content-view">
2462 - <?php
2463 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
2464 - echo '<div dir="rtl" lang="he" class="rtl-content">';
2465 - echo wp_kses_post(wpautop($content));
2466 - echo '</div>';
2467 - } else {
2468 - echo wp_kses_post(wpautop($content));
2469 - }
2470 - ?>
2471 - </div>
2472 - </div>
2473 - </div>
2474 - </td>
2475 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
2476 - <?php
2477 - $actual_source = $source_url;
2478 - if (strpos($source_url, '_ungrouped_') === 0) {
2479 - $actual_source = $prompt->source_url ?? '';
2480 - }
2481 - if (!empty($actual_source) && strpos($actual_source, 'mxchat://') !== 0) : ?>
2482 - <a href="<?php echo esc_url($actual_source); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
2483 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
2484 - <?php esc_html_e('View', 'mxchat'); ?>
2485 - </a>
2486 - <?php else : ?>
2487 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
2488 - <?php endif; ?>
2489 - </td>
2490 - <td style="padding: 12px 16px;">
2491 - <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);">
2492 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
2493 - </button>
2494 - </td>
2495 - </tr>
2496 - <?php
2497 - }
2498 - }
2499 - }
2500 - $html = ob_get_clean();
2501 -
2502 - // Generate pagination HTML for Pinecone
2503 - $total_pages = ceil($total_records / $per_page);
2504 - $pagination_html = '';
2505 - if ($total_pages > 1) {
2506 - $pagination_html = '<div class="mxchat-ajax-pagination" data-current-page="' . esc_attr($page) . '" data-total-pages="' . esc_attr($total_pages) . '" data-search="' . esc_attr($search_query) . '" data-content-type="' . esc_attr($content_type_filter) . '">';
2507 -
2508 - // Previous button
2509 - if ($page > 1) {
2510 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page - 1) . '">' . esc_html__('&laquo; Previous', 'mxchat') . '</a> ';
2511 - }
2512 -
2513 - // Page numbers
2514 - $start_page = max(1, $page - 2);
2515 - $end_page = min($total_pages, $page + 2);
2516 -
2517 - if ($start_page > 1) {
2518 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="1">1</a> ';
2519 - if ($start_page > 2) {
2520 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
2521 - }
2522 - }
2523 -
2524 - for ($i = $start_page; $i <= $end_page; $i++) {
2525 - if ($i == $page) {
2526 - $pagination_html .= '<span class="mxchat-page-current">' . $i . '</span> ';
2527 - } else {
2528 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $i . '">' . $i . '</a> ';
2529 - }
2530 - }
2531 -
2532 - if ($end_page < $total_pages) {
2533 - if ($end_page < $total_pages - 1) {
2534 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
2535 - }
2536 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $total_pages . '">' . $total_pages . '</a> ';
2537 - }
2538 -
2539 - // Next button
2540 - if ($page < $total_pages) {
2541 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page + 1) . '">' . esc_html__('Next &raquo;', 'mxchat') . '</a>';
2542 - }
2543 -
2544 - $pagination_html .= '</div>';
2545 - }
2546 -
2547 - wp_send_json_success(array(
2548 - 'html' => $html,
2549 - 'pagination_html' => $pagination_html,
2550 - 'total_count' => $total_records,
2551 - 'total_pages' => $total_pages,
2552 - 'page' => $page,
2553 - 'per_page' => $per_page,
2554 - 'data_source' => 'pinecone'
2555 - ));
2556 -}
2557 -
2558 -/**
2559 - * AJAX handler for pagination - handles both WordPress DB and Pinecone data sources
2560 - * Returns paginated entries without requiring a full page reload
2561 - */
2562 -public function ajax_mxchat_paginate_entries() {
2563 - check_ajax_referer('mxchat_entries_nonce', 'nonce');
2564 -
2565 - if (!current_user_can('manage_options')) {
2566 - wp_send_json_error(array('message' => 'Unauthorized'));
2567 - return;
2568 - }
2569 -
2570 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
2571 - $page = isset($_POST['page']) ? absint($_POST['page']) : 1;
2572 - $search_query = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
2573 - $content_type_filter = isset($_POST['content_type']) ? sanitize_key($_POST['content_type']) : '';
2574 - $per_page = 25;
2575 -
2576 - // Check if Pinecone is enabled for this bot
2577 - $pinecone_manager = $this->mxchat_get_pinecone_manager();
2578 - $pinecone_options = $pinecone_manager ? $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id) : array();
2579 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
2580 - $has_pinecone_api = !empty($pinecone_options['mxchat_pinecone_api_key']);
2581 -
2582 - if ($use_pinecone && $has_pinecone_api) {
2583 - // Delegate to Pinecone pagination handler (pass search params)
2584 - $_POST['page'] = $page;
2585 - $_POST['search'] = $search_query;
2586 - $_POST['content_type'] = $content_type_filter;
2587 - $this->ajax_mxchat_refresh_pinecone_entries();
2588 - return;
2589 - }
2590 -
2591 - // WordPress DB pagination - MUST match initial page load logic exactly
2592 - global $wpdb;
2593 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2594 - $offset = ($page - 1) * $per_page;
2595 -
2596 - // Build WHERE clause for search and content type filtering
2597 - $where_clauses = array();
2598 - $where_values = array();
2599 -
2600 - if ($search_query) {
2601 - $where_clauses[] = "article_content LIKE %s";
2602 - $where_values[] = '%' . $wpdb->esc_like($search_query) . '%';
2603 - }
2604 -
2605 - if ($content_type_filter) {
2606 - switch ($content_type_filter) {
2607 - case 'manual':
2608 - $where_clauses[] = "(source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%')";
2609 - break;
2610 - case 'pdf':
2611 - $where_clauses[] = "source_url LIKE '%.pdf'";
2612 - break;
2613 - case 'url':
2614 - $where_clauses[] = "source_url != '' AND source_url IS NOT NULL AND source_url NOT LIKE 'mxchat://%' AND source_url NOT LIKE '%.pdf'";
2615 - break;
2616 - }
2617 - }
2618 -
2619 - $where_sql = !empty($where_clauses) ? 'WHERE ' . implode(' AND ', $where_clauses) : '';
2620 -
2621 - // Count grouped entries with filters applied
2622 - if (!empty($where_values)) {
2623 - $count_args = array_merge($where_values, $where_values);
2624 - $count_query = $wpdb->prepare(
2625 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} {$where_sql} AND source_url != '' AND source_url NOT LIKE 'mxchat://%') +
2626 - (SELECT COUNT(*) FROM {$table_name} {$where_sql} AND (source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%'))",
2627 - ...$count_args
2628 - );
2629 - $total_records = $wpdb->get_var($count_query);
2630 - } else if (!empty($where_sql)) {
2631 - // Content type filter only (no search), no prepared values needed
2632 - $total_records = $wpdb->get_var(
2633 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} {$where_sql} AND source_url != '' AND source_url NOT LIKE 'mxchat://%') +
2634 - (SELECT COUNT(*) FROM {$table_name} {$where_sql} AND (source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%'))"
2635 - );
2636 - } else {
2637 - // No filters
2638 - $total_records = $wpdb->get_var(
2639 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} WHERE source_url != '' AND source_url NOT LIKE 'mxchat://%') +
2640 - (SELECT COUNT(*) FROM {$table_name} WHERE source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%')"
2641 - );
2642 - }
2643 - $total_pages = ceil($total_records / $per_page);
2644 -
2645 - // Step 1: Get unique source_urls for this page (ordered by latest timestamp) with filters
2646 - if (!empty($where_values)) {
2647 - $query_args = array_merge($where_values, array($per_page, $offset));
2648 - $urls_query = $wpdb->prepare(
2649 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name}
2650 - {$where_sql}
2651 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
2652 - ...$query_args
2653 - );
2654 - } else if (!empty($where_sql)) {
2655 - $urls_query = $wpdb->prepare(
2656 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name}
2657 - {$where_sql}
2658 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
2659 - $per_page, $offset
2660 - );
2661 - } else {
2662 - $urls_query = $wpdb->prepare(
2663 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name}
2664 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
2665 - $per_page, $offset
2666 - );
2667 - }
2668 - $page_urls = $wpdb->get_results($urls_query);
2669 -
2670 - // Step 2: Build list of source_urls to fetch
2671 - $url_list = array();
2672 - $url_order_map = array();
2673 - $order_index = 0;
2674 - foreach ($page_urls as $url_row) {
2675 - $url = $url_row->source_url;
2676 - $url_list[] = $url;
2677 - $url_order_map[$url] = $order_index++;
2678 - }
2679 -
2680 - // Step 3: Fetch all rows for these source_urls (with search filter if applicable)
2681 - $prompts = array();
2682 - if (!empty($url_list)) {
2683 - $placeholders = implode(',', array_fill(0, count($url_list), '%s'));
2684 - if ($search_query) {
2685 - // Include search filter in the final fetch
2686 - $prompts_query = $wpdb->prepare(
2687 - "SELECT id, article_content, source_url, timestamp, role_restriction
2688 - FROM {$table_name}
2689 - WHERE source_url IN ($placeholders) AND article_content LIKE %s
2690 - ORDER BY timestamp DESC",
2691 - ...array_merge($url_list, ['%' . $wpdb->esc_like($search_query) . '%'])
2692 - );
2693 - } else {
2694 - $prompts_query = $wpdb->prepare(
2695 - "SELECT id, article_content, source_url, timestamp, role_restriction
2696 - FROM {$table_name}
2697 - WHERE source_url IN ($placeholders)
2698 - ORDER BY timestamp DESC",
2699 - $url_list
2700 - );
2701 - }
2702 - $prompts = $wpdb->get_results($prompts_query);
2703 - }
2704 -
2705 - // Group prompts by source_url for chunk display
2706 - $grouped_prompts = array();
2707 - foreach ($prompts as $prompt) {
2708 - $source_url = $prompt->source_url ?? '';
2709 -
2710 - // Parse chunk metadata using the proper chunker method (same as initial page load)
2711 - if (class_exists('MxChat_Chunker')) {
2712 - $chunk_meta = MxChat_Chunker::parse_stored_chunk($prompt->article_content);
2713 - $prompt->chunk_metadata = $chunk_meta['metadata'];
2714 - $prompt->display_content = $chunk_meta['text'];
2715 - } else {
2716 - $prompt->chunk_metadata = array();
2717 - $prompt->display_content = $prompt->article_content;
2718 - }
2719 -
2720 - if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0) {
2721 - if (!isset($grouped_prompts[$source_url])) {
2722 - $grouped_prompts[$source_url] = array();
2723 - }
2724 - $grouped_prompts[$source_url][] = $prompt;
2725 - } else {
2726 - // Ungrouped entries
2727 - $grouped_prompts['_ungrouped_' . $prompt->id] = array($prompt);
2728 - }
2729 - }
2730 -
2731 - // Sort groups by the original URL order (newest first)
2732 - uksort($grouped_prompts, function($a, $b) use ($url_order_map) {
2733 - $order_a = isset($url_order_map[$a]) ? $url_order_map[$a] : PHP_INT_MAX;
2734 - $order_b = isset($url_order_map[$b]) ? $url_order_map[$b] : PHP_INT_MAX;
2735 - return $order_a - $order_b;
2736 - });
2737 -
2738 - // Sort each group internally by chunk_index
2739 - foreach ($grouped_prompts as $source_url => &$group) {
2740 - usort($group, function($a, $b) {
2741 - $index_a = isset($a->chunk_metadata['chunk_index']) ? intval($a->chunk_metadata['chunk_index']) : 0;
2742 - $index_b = isset($b->chunk_metadata['chunk_index']) ? intval($b->chunk_metadata['chunk_index']) : 0;
2743 - return $index_a - $index_b;
2744 - });
2745 - }
2746 - unset($group);
2747 -
2748 - // Build HTML for the table rows
2749 - ob_start();
2750 - $display_index = 0;
2751 - $current_page = $page;
2752 - $data_source = 'wordpress';
2753 - $current_bot_id = $bot_id;
2754 - $preview_length = 150;
2755 -
2756 - if (empty($grouped_prompts)) {
2757 - echo '<tr><td colspan="5" style="padding: 40px; text-align: center; color: var(--mxch-text-muted);">';
2758 - esc_html_e('No knowledge entries found. Use the Import Options to add content.', 'mxchat');
2759 - echo '</td></tr>';
2760 - } else {
2761 - foreach ($grouped_prompts as $source_url => $group) {
2762 - $chunk_count = count($group);
2763 - $first_prompt = $group[0];
2764 - $display_index++;
2765 -
2766 - if ($chunk_count > 1) {
2767 - // Multiple chunks - show grouped row with expand button
2768 - $group_id = 'group-' . md5($source_url);
2769 - ?>
2770 - <tr id="prompt-<?php echo esc_attr($first_prompt->id); ?>"
2771 - class="mxchat-chunk-group-header"
2772 - data-source="<?php echo esc_attr($data_source); ?>"
2773 - data-group-id="<?php echo esc_attr($group_id); ?>"
2774 - style="border-bottom: 1px solid var(--mxch-card-border);">
2775 - <td style="padding: 12px 16px; text-align: center;">
2776 - <input type="checkbox"
2777 - class="mxchat-entry-checkbox"
2778 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
2779 - data-source="<?php echo esc_attr($data_source); ?>"
2780 - data-source-url="<?php echo esc_attr($source_url); ?>"
2781 - data-is-group="true"
2782 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>">
2783 - </td>
2784 - <td style="padding: 12px 16px; font-size: 13px;">
2785 - <?php echo esc_html($first_prompt->id); ?>
2786 - </td>
2787 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
2788 - <div class="mxchat-chunk-group-info">
2789 - <button type="button" class="mxchat-chunk-toggle" data-group-id="<?php echo esc_attr($group_id); ?>">
2790 - <span class="dashicons dashicons-arrow-right-alt2"></span>
2791 - </button>
2792 - <span class="mxchat-chunk-badge"><?php echo esc_html($chunk_count); ?> <?php esc_html_e('chunks', 'mxchat'); ?></span>
2793 - <span class="mxchat-chunk-preview">
2794 - <?php
2795 - $parent_content = isset($first_prompt->display_content) ? $first_prompt->display_content : $first_prompt->article_content;
2796 - $content_preview = mb_substr($parent_content, 0, 100);
2797 - echo esc_html($content_preview . '...');
2798 - ?>
2799 - </span>
2800 - </div>
2801 - </td>
2802 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
2803 - <?php if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0 && strpos($source_url, '_ungrouped_') !== 0) : ?>
2804 - <a href="<?php echo esc_url($source_url); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
2805 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
2806 - <?php esc_html_e('View Source', 'mxchat'); ?>
2807 - </a>
2808 - <?php else : ?>
2809 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
2810 - <?php endif; ?>
2811 - </td>
2812 - <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
2813 - <?php if ($data_source !== 'pinecone') : ?>
2814 - <button type="button"
2815 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
2816 - data-source-url="<?php echo esc_attr($source_url); ?>"
2817 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
2818 - data-data-source="<?php echo esc_attr($data_source); ?>"
2819 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2820 - data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
2821 - title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
2822 - <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
2823 - </button>
2824 - <?php endif; ?>
2825 - <button type="button"
2826 - class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
2827 - data-source-url="<?php echo esc_attr($source_url); ?>"
2828 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
2829 - data-data-source="<?php echo esc_attr($data_source); ?>"
2830 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2831 - data-nonce="<?php echo wp_create_nonce('mxchat_delete_chunks_nonce'); ?>"
2832 - style="color: var(--mxch-error);"
2833 - title="<?php esc_attr_e('Delete all chunks', 'mxchat'); ?>">
2834 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
2835 - </button>
2836 - </td>
2837 - </tr>
2838 - <?php
2839 - // Render hidden chunk rows
2840 - foreach ($group as $chunk_index => $chunk) {
2841 - $meta_chunk_index = isset($chunk->chunk_metadata['chunk_index']) ? intval($chunk->chunk_metadata['chunk_index']) : $chunk_index;
2842 - $meta_total_chunks = isset($chunk->chunk_metadata['total_chunks']) ? intval($chunk->chunk_metadata['total_chunks']) : $chunk_count;
2843 - $content = isset($chunk->display_content) ? $chunk->display_content : $chunk->article_content;
2844 - $content_preview = mb_strlen($content) > $preview_length
2845 - ? mb_substr($content, 0, $preview_length) . '...'
2846 - : $content;
2847 - ?>
2848 - <tr id="prompt-<?php echo esc_attr($chunk->id); ?>"
2849 - class="mxchat-chunk-row <?php echo esc_attr($group_id); ?>"
2850 - data-source="<?php echo esc_attr($data_source); ?>"
2851 - style="display: none; background: #f8f9fa; border-bottom: 1px solid var(--mxch-card-border);">
2852 - <td style="padding: 12px 16px; text-align: center;">
2853 - <!-- Checkbox column placeholder for chunks (managed by group) -->
2854 - </td>
2855 - <td style="padding: 12px 16px 12px 30px; font-size: 13px;">
2856 - <!-- Hidden ID column for chunks -->
2857 - </td>
2858 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
2859 - <div class="mxchat-accordion-wrapper">
2860 - <div class="mxchat-content-preview">
2861 - <span class="mxchat-chunk-indicator" style="margin-right: 10px; color: var(--mxch-text-secondary); font-size: 12px;">
2862 - <?php printf(esc_html__('Chunk %d of %d', 'mxchat'), $meta_chunk_index + 1, $meta_total_chunks); ?>
2863 - </span>
2864 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
2865 - <?php if (mb_strlen($content) > $preview_length) : ?>
2866 - <button class="mxchat-expand-toggle" type="button">
2867 - <span class="dashicons dashicons-arrow-down-alt2"></span>
2868 - </button>
2869 - <?php endif; ?>
2870 - </div>
2871 - <div class="mxchat-content-full" style="display: none;">
2872 - <div class="content-view">
2873 - <?php
2874 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
2875 - echo '<div dir="rtl" lang="he" class="rtl-content">';
2876 - echo wp_kses_post(wpautop($content));
2877 - echo '</div>';
2878 - } else {
2879 - echo wp_kses_post(wpautop($content));
2880 - }
2881 - ?>
2882 - </div>
2883 - </div>
2884 - </div>
2885 - </td>
2886 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
2887 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Same as parent', 'mxchat'); ?></span>
2888 - </td>
2889 - <td class="mxchat-actions-cell" style="padding: 12px 16px;">
2890 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Managed by group', 'mxchat'); ?></span>
2891 - </td>
2892 - </tr>
2893 - <?php
2894 - }
2895 - } else {
2896 - // Single entry - display normally with accordion
2897 - $prompt = $first_prompt;
2898 - $content = isset($prompt->display_content) ? $prompt->display_content : $prompt->article_content;
2899 - $content_preview = mb_strlen($content) > $preview_length
2900 - ? mb_substr($content, 0, $preview_length) . '...'
2901 - : $content;
2902 - ?>
2903 - <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
2904 - data-source="<?php echo esc_attr($data_source); ?>"
2905 - style="border-bottom: 1px solid var(--mxch-card-border);">
2906 - <td style="padding: 12px 16px; text-align: center;">
2907 - <input type="checkbox"
2908 - class="mxchat-entry-checkbox"
2909 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
2910 - data-source="<?php echo esc_attr($data_source); ?>"
2911 - data-source-url="<?php echo esc_attr($source_url); ?>"
2912 - data-is-group="false">
2913 - </td>
2914 - <td style="padding: 12px 16px; font-size: 13px;">
2915 - <?php echo esc_html($prompt->id); ?>
2916 - </td>
2917 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
2918 - <div class="mxchat-accordion-wrapper">
2919 - <div class="mxchat-content-preview">
2920 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
2921 - <?php if (mb_strlen($content) > $preview_length) : ?>
2922 - <button class="mxchat-expand-toggle" type="button">
2923 - <span class="dashicons dashicons-arrow-down-alt2"></span>
2924 - </button>
2925 - <?php endif; ?>
2926 - </div>
2927 - <div class="mxchat-content-full" style="display: none;">
2928 - <div class="content-view">
2929 - <?php
2930 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
2931 - echo '<div dir="rtl" lang="he" class="rtl-content">';
2932 - echo wp_kses_post(wpautop($content));
2933 - echo '</div>';
2934 - } else {
2935 - echo wp_kses_post(wpautop($content));
2936 - }
2937 - ?>
2938 - </div>
2939 - </div>
2940 - </div>
2941 - </td>
2942 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
2943 - <?php
2944 - $actual_source = $source_url;
2945 - if (strpos($source_url, '_ungrouped_') === 0) {
2946 - $actual_source = $prompt->source_url ?? '';
2947 - }
2948 - if (!empty($actual_source) && strpos($actual_source, 'mxchat://') !== 0) : ?>
2949 - <a href="<?php echo esc_url($actual_source); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
2950 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
2951 - <?php esc_html_e('View', 'mxchat'); ?>
2952 - </a>
2953 - <?php else : ?>
2954 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
2955 - <?php endif; ?>
2956 - </td>
2957 - <td style="padding: 12px 16px; white-space: nowrap;">
2958 - <button type="button"
2959 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
2960 - data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
2961 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
2962 - data-data-source="<?php echo esc_attr($data_source); ?>"
2963 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2964 - data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
2965 - title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
2966 - <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
2967 - </button>
2968 - <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);">
2969 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
2970 - </button>
2971 - </td>
2972 - </tr>
2973 - <?php
2974 - }
2975 - }
2976 - }
2977 - $html = ob_get_clean();
2978 -
2979 - // Generate pagination HTML (include search/filter data for subsequent pages)
2980 - $pagination_html = '';
2981 - if ($total_pages > 1) {
2982 - $pagination_html = '<div class="mxchat-ajax-pagination" data-current-page="' . esc_attr($page) . '" data-total-pages="' . esc_attr($total_pages) . '" data-search="' . esc_attr($search_query) . '" data-content-type="' . esc_attr($content_type_filter) . '">';
2983 -
2984 - // Previous button
2985 - if ($page > 1) {
2986 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page - 1) . '">' . esc_html__('&laquo; Previous', 'mxchat') . '</a> ';
2987 - }
2988 -
2989 - // Page numbers
2990 - $start_page = max(1, $page - 2);
2991 - $end_page = min($total_pages, $page + 2);
2992 -
2993 - if ($start_page > 1) {
2994 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="1">1</a> ';
2995 - if ($start_page > 2) {
2996 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
2997 - }
2998 - }
2999 -
3000 - for ($i = $start_page; $i <= $end_page; $i++) {
3001 - if ($i == $page) {
3002 - $pagination_html .= '<span class="mxchat-page-current">' . $i . '</span> ';
3003 - } else {
3004 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $i . '">' . $i . '</a> ';
3005 - }
3006 - }
3007 -
3008 - if ($end_page < $total_pages) {
3009 - if ($end_page < $total_pages - 1) {
3010 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
3011 - }
3012 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $total_pages . '">' . $total_pages . '</a> ';
3013 - }
3014 -
3015 - // Next button
3016 - if ($page < $total_pages) {
3017 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page + 1) . '">' . esc_html__('Next &raquo;', 'mxchat') . '</a>';
3018 - }
3019 -
3020 - $pagination_html .= '</div>';
3021 - }
3022 -
3023 - wp_send_json_success(array(
3024 - 'html' => $html,
3025 - 'pagination_html' => $pagination_html,
3026 - 'total_count' => $total_records,
3027 - 'total_pages' => $total_pages,
3028 - 'page' => $page,
3029 - 'per_page' => $per_page,
3030 - 'data_source' => 'wordpress'
3031 - ));
3032 -}
3033 -
3034 -/**
3035 - * AJAX handler to detect available sitemaps on the site
3036 - * Optimized for speed - only checks primary sitemap indexes first
3037 - */
3038 -public function ajax_mxchat_detect_sitemaps() {
3039 - check_ajax_referer('mxchat_detect_sitemaps_nonce', 'nonce');
3040 -
3041 - if (!current_user_can('manage_options')) {
3042 - wp_send_json_error(array('message' => 'Unauthorized'));
3043 - return;
3044 - }
3045 -
3046 - $site_url = get_site_url();
3047 - $sitemaps = array();
3048 - $found_index = false;
3049 -
3050 - // Only check the main sitemap index files first (much faster)
3051 - // These are the primary entry points that contain sub-sitemaps
3052 - $primary_indexes = array(
3053 - 'sitemap_index.xml' => 'Yoast SEO / Rank Math', // Yoast & Rank Math
3054 - 'wp-sitemap.xml' => 'WordPress Core', // WordPress Core
3055 - 'sitemap.xml' => 'Standard', // Generic/AIOSEO
3056 - );
3057 -
3058 - foreach ($primary_indexes as $path => $source) {
3059 - $url = trailingslashit($site_url) . $path;
3060 -
3061 - $response = wp_remote_head($url, array(
3062 - 'timeout' => 10,
3063 - 'sslverify' => false,
3064 - 'redirection' => 1,
3065 - 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
3066 - ));
3067 -
3068 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
3069 - // Found a sitemap index - parse it to get sub-sitemaps
3070 - $sub_sitemaps = $this->parse_sitemap_index($url);
3071 - if (!empty($sub_sitemaps)) {
3072 - $sitemaps[] = array(
3073 - 'url' => $url,
3074 - 'type' => 'index',
3075 - 'source' => $source,
3076 - 'sub_sitemaps' => $sub_sitemaps
3077 - );
3078 - $found_index = true;
3079 - // Found a valid index, no need to check others
3080 - break;
3081 - }
3082 - }
3083 - }
3084 -
3085 - // If no sitemap index found, check for standalone sitemaps
3086 - if (!$found_index) {
3087 - $standalone_sitemaps = array(
3088 - 'post-sitemap.xml' => array('type' => 'content', 'source' => 'Yoast SEO'),
3089 - 'page-sitemap.xml' => array('type' => 'content', 'source' => 'Yoast SEO'),
3090 - );
3091 -
3092 - foreach ($standalone_sitemaps as $path => $info) {
3093 - $url = trailingslashit($site_url) . $path;
3094 -
3095 - $response = wp_remote_head($url, array(
3096 - 'timeout' => 2,
3097 - 'sslverify' => false
3098 - ));
3099 -
3100 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
3101 - $sitemaps[] = array(
3102 - 'url' => $url,
3103 - 'type' => $info['type'],
3104 - 'source' => $info['source'],
3105 - 'url_count' => 0 // Skip URL count for speed
3106 - );
3107 - }
3108 - }
3109 - }
3110 -
3111 - wp_send_json_success(array(
3112 - 'sitemaps' => $sitemaps,
3113 - 'site_url' => $site_url
3114 - ));
3115 -}
3116 -
3117 -/**
3118 - * Parse a sitemap index to get sub-sitemaps
3119 - * Optimized: doesn't fetch URL count for each sub-sitemap (too slow)
3120 - */
3121 -private function parse_sitemap_index($url) {
3122 - $sub_sitemaps = array();
3123 -
3124 - $response = wp_remote_get($url, array(
3125 - 'timeout' => 30,
3126 - 'sslverify' => false,
3127 - 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
3128 - 'headers' => array(
3129 - 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
3130 - 'Accept-Language' => 'en-US,en;q=0.9',
3131 - ),
3132 - ));
3133 -
3134 - if (is_wp_error($response)) {
3135 - return $sub_sitemaps;
3136 - }
3137 -
3138 - $body = wp_remote_retrieve_body($response);
3139 - if (empty($body)) {
3140 - return $sub_sitemaps;
3141 - }
3142 -
3143 - // Suppress XML errors
3144 - libxml_use_internal_errors(true);
3145 - $xml = simplexml_load_string($body);
3146 - libxml_clear_errors();
3147 -
3148 - if ($xml === false) {
3149 - return $sub_sitemaps;
3150 - }
3151 -
3152 - // Check if it's a sitemap index (contains <sitemap> elements)
3153 - if (isset($xml->sitemap)) {
3154 - foreach ($xml->sitemap as $sitemap) {
3155 - $loc = (string) $sitemap->loc;
3156 - if (!empty($loc)) {
3157 - // Try to determine the type from the URL
3158 - $type = 'content';
3159 - if (strpos($loc, 'category') !== false || strpos($loc, 'tag') !== false || strpos($loc, 'taxonomy') !== false) {
3160 - $type = 'taxonomy';
3161 - } elseif (strpos($loc, 'author') !== false || strpos($loc, 'user') !== false) {
3162 - $type = 'author';
3163 - }
3164 -
3165 - // Skip URL count - too slow to fetch for each sitemap
3166 - $sub_sitemaps[] = array(
3167 - 'url' => $loc,
3168 - 'type' => $type,
3169 - 'url_count' => 0, // Don't fetch - takes too long
3170 - 'name' => basename(parse_url($loc, PHP_URL_PATH))
3171 - );
3172 - }
3173 - }
3174 - }
3175 -
3176 - return $sub_sitemaps;
3177 -}
3178 -
3179 -/**
3180 - * Get URL count from a sitemap
3181 - */
3182 -private function get_sitemap_url_count($url) {
3183 - $response = wp_remote_get($url, array(
3184 - 'timeout' => 30,
3185 - 'sslverify' => false,
3186 - 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
3187 - 'headers' => array(
3188 - 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
3189 - 'Accept-Language' => 'en-US,en;q=0.9',
3190 - ),
3191 - ));
3192 -
3193 - if (is_wp_error($response)) {
3194 - return 0;
3195 - }
3196 -
3197 - $body = wp_remote_retrieve_body($response);
3198 - if (empty($body)) {
3199 - return 0;
3200 - }
3201 -
3202 - // Count <url> or <loc> elements
3203 - $count = preg_match_all('/<url>/i', $body, $matches);
3204 - return $count ?: 0;
3205 -}
3206 -
3207 -/**
3208 - * Get sitemaps declared in robots.txt
3209 - */
3210 -private function get_sitemaps_from_robots($site_url) {
3211 - $sitemaps = array();
3212 - $robots_url = trailingslashit($site_url) . 'robots.txt';
3213 -
3214 - $response = wp_remote_get($robots_url, array(
3215 - 'timeout' => 15,
3216 - 'sslverify' => false,
3217 - 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
3218 - ));
3219 -
3220 - if (is_wp_error($response)) {
3221 - return $sitemaps;
3222 - }
3223 -
3224 - $body = wp_remote_retrieve_body($response);
3225 - if (empty($body)) {
3226 - return $sitemaps;
3227 - }
3228 -
3229 - // Find Sitemap: declarations
3230 - if (preg_match_all('/^Sitemap:\s*(.+)$/mi', $body, $matches)) {
3231 - foreach ($matches[1] as $sitemap_url) {
3232 - $sitemap_url = trim($sitemap_url);
3233 - if (filter_var($sitemap_url, FILTER_VALIDATE_URL)) {
3234 - $sitemaps[] = $sitemap_url;
3235 - }
3236 - }
3237 - }
3238 -
3239 - return $sitemaps;
3240 -}
3241 -
3242 -public function mxchat_stop_processing() {
3243 - // Verify permissions
3244 - if (!current_user_can('manage_options')) {
3245 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
3246 - }
3247 -
3248 - // Verify nonce
3249 - check_admin_referer('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce');
3250 -
3251 - global $wpdb;
3252 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
3253 -
3254 - // Get active queue IDs
3255 - $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
3256 - $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
3257 -
3258 - // Delete all pending items from active queues
3259 - if ($sitemap_queue_id) {
3260 - $wpdb->delete(
3261 - $table_name,
3262 - array(
3263 - 'queue_id' => $sitemap_queue_id,
3264 - 'status' => 'pending'
3265 - ),
3266 - array('%s', '%s')
3267 - );
3268 -
3269 - delete_transient('mxchat_active_queue_sitemap');
3270 - delete_transient('mxchat_last_sitemap_url');
3271 - }
3272 -
3273 - if ($pdf_queue_id) {
3274 - // Get PDF path before deleting
3275 - $pdf_path = $this->mxchat_get_queue_meta($pdf_queue_id, 'pdf_path');
3276 -
3277 - $wpdb->delete(
3278 - $table_name,
3279 - array(
3280 - 'queue_id' => $pdf_queue_id,
3281 - 'status' => 'pending'
3282 - ),
3283 - array('%s', '%s')
3284 - );
3285 -
3286 - // Delete PDF file
3287 - if ($pdf_path && file_exists($pdf_path)) {
3288 - wp_delete_file($pdf_path);
3289 - }
3290 -
3291 - delete_transient('mxchat_active_queue_pdf');
3292 - delete_transient('mxchat_last_pdf_url');
3293 - }
3294 -
3295 - // Redirect back with a success message
3296 - set_transient('mxchat_admin_notice_success',
3297 - esc_html__('Processing has been stopped successfully.', 'mxchat'),
3298 - 30
3299 - );
3300 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
3301 - exit;
3302 -}
3303 -
3304 -/**
3305 - * Get content list for processing
3306 - */
3307 -public function ajax_mxchat_get_content_list() {
3308 - // Verify the nonce
3309 - check_ajax_referer('mxchat_content_selector_nonce', 'nonce');
3310 -
3311 - if (!current_user_can('manage_options')) {
3312 - wp_send_json_error(__('Unauthorized access', 'mxchat'));
3313 - }
3314 -
3315 - $page = isset($_GET['page']) ? absint($_GET['page']) : 1;
3316 - $per_page = isset($_GET['per_page']) ? absint($_GET['per_page']) : 100;
3317 - $search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
3318 - $post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : 'all';
3319 - $post_status = isset($_GET['post_status']) ? sanitize_text_field($_GET['post_status']) : 'publish';
3320 - $processed_filter = isset($_GET['processed_filter']) ? sanitize_text_field($_GET['processed_filter']) : 'all';
3321 -
3322 - // Build query args
3323 - $args = array(
3324 - 'posts_per_page' => $per_page,
3325 - 'paged' => $page,
3326 - 'post_status' => $post_status !== 'all' ? $post_status : array('publish', 'draft', 'pending'),
3327 - 'orderby' => 'date',
3328 - 'order' => 'DESC',
3329 - );
3330 -
3331 - // Handle post types - IMPROVED VERSION
3332 - if ($post_type !== 'all') {
3333 - $args['post_type'] = $post_type;
3334 - } else {
3335 - // Get all available post types that might contain content
3336 - $all_post_types = array();
3337 -
3338 - // First get all public post types
3339 - $public_types = get_post_types(array('public' => true), 'names');
3340 - $all_post_types = array_merge($all_post_types, $public_types);
3341 -
3342 - // Add common forum/community post types
3343 - $forum_types = array('topic', 'reply', 'forum', 'wpforo_topic', 'wpforo_post');
3344 - foreach ($forum_types as $forum_type) {
3345 - if (post_type_exists($forum_type)) {
3346 - $all_post_types[] = $forum_type;
3347 - }
3348 - }
3349 -
3350 - // Add other commonly used post types
3351 - $common_types = array('product', 'job_listing', 'event', 'portfolio');
3352 - foreach ($common_types as $common_type) {
3353 - if (post_type_exists($common_type)) {
3354 - $all_post_types[] = $common_type;
3355 - }
3356 - }
3357 -
3358 - // Remove duplicates and ensure we have at least some post types
3359 - $all_post_types = array_unique($all_post_types);
3360 -
3361 - if (empty($all_post_types)) {
3362 - // Fallback to basic post types
3363 - $all_post_types = array('post', 'page');
3364 - }
3365 -
3366 - $args['post_type'] = $all_post_types;
3367 -
3368 - // Debug logging to see what post types are being queried
3369 - //error_log('MxChat Debug: Querying post types: ' . implode(', ', $all_post_types));
3370 - }
3371 -
3372 - if (!empty($search)) {
3373 - $args['s'] = $search;
3374 - }
3375 -
3376 - // Get processed data from storage
3377 - $processed_data = array();
3378 -
3379 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3380 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3381 -
3382 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
3383 - // Get fresh data from Pinecone - no caching
3384 - $processed_data = $this->mxchat_get_pinecone_processed_content($pinecone_options);
3385 - } else {
3386 - // WordPress DB checking with better URL matching for all post types
3387 - global $wpdb;
3388 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3389 - $processed_items = $wpdb->get_results("SELECT id, source_url, article_content, timestamp FROM {$table_name}");
3390 -
3391 - // Group items by source_url to count chunks
3392 - $url_chunk_counts = array();
3393 - $url_latest_timestamp = array();
3394 - $url_first_id = array();
3395 -
3396 - if (!empty($processed_items)) {
3397 - foreach ($processed_items as $item) {
3398 - $url = $item->source_url;
3399 - if (empty($url)) continue;
3400 -
3401 - // Count chunks per URL
3402 - if (!isset($url_chunk_counts[$url])) {
3403 - $url_chunk_counts[$url] = 0;
3404 - $url_latest_timestamp[$url] = $item->timestamp;
3405 - $url_first_id[$url] = $item->id;
3406 - }
3407 - $url_chunk_counts[$url]++;
3408 -
3409 - // Track latest timestamp
3410 - if (strtotime($item->timestamp) > strtotime($url_latest_timestamp[$url])) {
3411 - $url_latest_timestamp[$url] = $item->timestamp;
3412 - }
3413 - }
3414 -
3415 - // Now build processed_data with chunk counts
3416 - foreach ($url_chunk_counts as $url => $chunk_count) {
3417 - $post_id = $this->mxchat_url_to_post_id_improved($url);
3418 -
3419 - if ($post_id) {
3420 - $processed_data[$post_id] = array(
3421 - 'db_id' => $url_first_id[$url],
3422 - 'timestamp' => $url_latest_timestamp[$url],
3423 - 'url' => $url,
3424 - 'source' => 'wordpress',
3425 - 'chunk_count' => $chunk_count
3426 - );
3427 - }
3428 - }
3429 - }
3430 - }
3431 -
3432 - // Get processed IDs as a simple array for in_array checks
3433 - $processed_ids = array_keys($processed_data);
3434 -
3435 - // Handle processed/unprocessed filter
3436 - if ($processed_filter === 'processed' && !empty($processed_ids)) {
3437 - $args['post__in'] = $processed_ids;
3438 - } elseif ($processed_filter === 'unprocessed' && !empty($processed_ids)) {
3439 - $args['post__not_in'] = $processed_ids;
3440 - }
3441 -
3442 - // Run the query
3443 - $query = new WP_Query($args);
3444 - $content_items = array();
3445 -
3446 - if ($query->have_posts()) {
3447 - while ($query->have_posts()) {
3448 - $query->the_post();
3449 - $id = get_the_ID();
3450 - $post_date = get_the_date();
3451 - $excerpt = wp_trim_words(get_the_excerpt(), 20, '...');
3452 - $word_count = str_word_count(strip_tags(get_the_content()));
3453 -
3454 - $is_processed = in_array($id, $processed_ids);
3455 - $processed_date = '';
3456 - $db_record_id = 0;
3457 - $data_source = 'none';
3458 -
3459 - if ($is_processed && isset($processed_data[$id])) {
3460 - $item_data = $processed_data[$id];
3461 - $data_source = $item_data['source'];
3462 -
3463 - if ($data_source === 'wordpress' && isset($item_data['timestamp'])) {
3464 - // WordPress DB format
3465 - $timestamp = strtotime($item_data['timestamp']);
3466 - $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
3467 - $db_record_id = $item_data['db_id'];
3468 - } elseif ($data_source === 'pinecone') {
3469 - // Pinecone format
3470 - $processed_date = $item_data['processed_date'];
3471 - $db_record_id = $item_data['db_id'];
3472 - }
3473 - }
3474 -
3475 - // Get chunk count for this item
3476 - $chunk_count = 0;
3477 - if ($is_processed && isset($processed_data[$id]['chunk_count'])) {
3478 - $chunk_count = intval($processed_data[$id]['chunk_count']);
3479 - }
3480 -
3481 - $content_items[] = array(
3482 - 'id' => $id,
3483 - 'title' => get_the_title(),
3484 - 'permalink' => get_permalink(),
3485 - 'date' => $post_date,
3486 - 'type' => get_post_type(),
3487 - 'status' => get_post_status(),
3488 - 'excerpt' => $excerpt,
3489 - 'word_count' => $word_count,
3490 - 'already_processed' => $is_processed,
3491 - 'processed_date' => $processed_date,
3492 - 'db_record_id' => $db_record_id,
3493 - 'data_source' => $data_source,
3494 - 'chunk_count' => $chunk_count
3495 - );
3496 - }
3497 - wp_reset_postdata();
3498 - }
3499 -
3500 - $response = array(
3501 - 'items' => $content_items,
3502 - 'total' => $query->found_posts,
3503 - 'total_pages' => $query->max_num_pages,
3504 - 'current_page' => $page,
3505 - 'processed_count' => count($processed_ids)
3506 - );
3507 -
3508 - wp_send_json_success($response);
3509 - exit;
3510 -}
3511 -
3512 -
3513 -/**
3514 - * This function handles various WooCommerce URL formats and permalink structures
3515 - */
3516 -private function mxchat_url_to_post_id_improved($url) {
3517 - // First try the standard WordPress function
3518 - $post_id = url_to_postid($url);
3519 -
3520 - if ($post_id > 0) {
3521 - return $post_id;
3522 - }
3523 -
3524 - // If that fails, try more aggressive URL matching
3525 - // Remove trailing slashes and query parameters for better matching
3526 - $clean_url = rtrim($url, '/');
3527 - $clean_url = strtok($clean_url, '?'); // Remove query parameters
3528 -
3529 - // Try again with cleaned URL
3530 - $post_id = url_to_postid($clean_url);
3531 - if ($post_id > 0) {
3532 - return $post_id;
3533 - }
3534 -
3535 - // For bbPress forum topics, try extracting slug from URL
3536 - if (strpos($url, '/topic/') !== false || strpos($url, '/forums/') !== false) {
3537 - // Handle bbPress URLs: /forums/topic/topic-name/
3538 - if (preg_match('/\/forums\/topic\/([^\/\?]+)/', $url, $matches)) {
3539 - $topic_slug = $matches[1];
3540 -
3541 - // Look up topic by slug
3542 - $topic = get_page_by_path($topic_slug, OBJECT, 'topic');
3543 - if ($topic) {
3544 - return $topic->ID;
3545 - }
3546 -
3547 - // Alternative method: query by post_name
3548 - global $wpdb;
3549 - $post_id = $wpdb->get_var($wpdb->prepare(
3550 - "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
3551 - $topic_slug
3552 - ));
3553 -
3554 - if ($post_id) {
3555 - return intval($post_id);
3556 - }
3557 - }
3558 -
3559 - // Handle simpler topic URLs: /topic/topic-name/
3560 - if (preg_match('/\/topic\/([^\/\?]+)/', $url, $matches)) {
3561 - $topic_slug = $matches[1];
3562 -
3563 - global $wpdb;
3564 - $post_id = $wpdb->get_var($wpdb->prepare(
3565 - "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
3566 - $topic_slug
3567 - ));
3568 -
3569 - if ($post_id) {
3570 - return intval($post_id);
3571 - }
3572 - }
3573 - }
3574 -
3575 - // For WooCommerce products
3576 - if (strpos($url, '/product/') !== false || strpos($url, 'product=') !== false) {
3577 - // Extract product slug from various URL formats
3578 - $product_slug = '';
3579 -
3580 - // Handle pretty permalinks: /product/product-name/
3581 - if (preg_match('/\/product\/([^\/\?]+)/', $url, $matches)) {
3582 - $product_slug = $matches[1];
3583 - }
3584 - // Handle query parameters: ?product=product-name
3585 - elseif (preg_match('/[\?&]product=([^&]+)/', $url, $matches)) {
3586 - $product_slug = $matches[1];
3587 - }
3588 -
3589 - if (!empty($product_slug)) {
3590 - // Look up product by slug
3591 - $product = get_page_by_path($product_slug, OBJECT, 'product');
3592 - if ($product) {
3593 - return $product->ID;
3594 - }
3595 -
3596 - // Alternative method: query by post_name
3597 - global $wpdb;
3598 - $post_id = $wpdb->get_var($wpdb->prepare(
3599 - "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'product' AND post_status = 'publish'",
3600 - $product_slug
3601 - ));
3602 -
3603 - if ($post_id) {
3604 - return intval($post_id);
3605 - }
3606 - }
3607 - }
3608 -
3609 - // Generic approach: try to extract slug and match against all post types
3610 - $parsed_url = wp_parse_url($clean_url);
3611 - $path = $parsed_url['path'] ?? '';
3612 -
3613 - if (!empty($path)) {
3614 - // Get the last part of the path as potential slug
3615 - $path_parts = array_filter(explode('/', trim($path, '/')));
3616 - $potential_slug = end($path_parts);
3617 -
3618 - if (!empty($potential_slug)) {
3619 - global $wpdb;
3620 -
3621 - // Try to find any post with this slug
3622 - $post_id = $wpdb->get_var($wpdb->prepare(
3623 - "SELECT ID FROM {$wpdb->posts}
3624 - WHERE post_name = %s
3625 - AND post_status IN ('publish', 'closed', 'private')
3626 - AND post_type NOT IN ('revision', 'attachment', 'nav_menu_item')
3627 - ORDER BY CASE
3628 - WHEN post_type = 'post' THEN 1
3629 - WHEN post_type = 'page' THEN 2
3630 - WHEN post_type = 'topic' THEN 3
3631 - WHEN post_type = 'product' THEN 4
3632 - ELSE 5
3633 - END
3634 - LIMIT 1",
3635 - $potential_slug
3636 - ));
3637 -
3638 - if ($post_id) {
3639 - return intval($post_id);
3640 - }
3641 - }
3642 - }
3643 -
3644 - // ADDITIONAL: Try direct database lookup by URL variations
3645 - global $wpdb;
3646 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3647 -
3648 - // Try variations of the URL (with/without trailing slash, http/https)
3649 - $url_variations = array(
3650 - $url,
3651 - rtrim($url, '/'),
3652 - $url . '/',
3653 - str_replace('http://', 'https://', $url),
3654 - str_replace('https://', 'http://', $url),
3655 - str_replace('http://', 'https://', rtrim($url, '/')),
3656 - str_replace('https://', 'http://', rtrim($url, '/'))
3657 - );
3658 -
3659 - // Remove duplicates
3660 - $url_variations = array_unique($url_variations);
3661 -
3662 - foreach ($url_variations as $variation) {
3663 - $existing_record = $wpdb->get_row($wpdb->prepare(
3664 - "SELECT id, source_url FROM $table_name WHERE source_url = %s",
3665 - $variation
3666 - ));
3667 -
3668 - if ($existing_record) {
3669 - // Try to get post ID from this stored URL
3670 - $stored_post_id = url_to_postid($existing_record->source_url);
3671 - if ($stored_post_id > 0) {
3672 - return $stored_post_id;
3673 - }
3674 - }
3675 - }
3676 -
3677 - return 0; // No match found
3678 -}
3679 -/**
3680 - * Process selected content via AJAX
3681 - */
3682 -public function ajax_mxchat_process_selected_content() {
3683 - // Basic request validation
3684 - if (!check_ajax_referer('mxchat_content_selector_nonce', 'nonce', false)) {
3685 - wp_send_json_error('Invalid nonce');
3686 - exit;
3687 - }
3688 -
3689 - if (!current_user_can('manage_options')) {
3690 - wp_send_json_error('Unauthorized access');
3691 - exit;
3692 - }
3693 -
3694 - // Get post IDs - safely parse the array
3695 - $post_ids = array();
3696 - if (isset($_POST['post_ids']) && is_array($_POST['post_ids'])) {
3697 - foreach ($_POST['post_ids'] as $id) {
3698 - $post_ids[] = absint($id);
3699 - }
3700 - }
3701 -
3702 - if (empty($post_ids)) {
3703 - wp_send_json_error('No content selected');
3704 - exit;
3705 - }
3706 -
3707 - // Get bot_id from request
3708 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
3709 -
3710 - // ACF→PDF extraction is opt-in per import batch. Persist the last-used value so users
3711 - // don't re-check on every batch; the default is OFF for installs that haven't set it.
3712 - $extract_acf_pdfs = !empty($_POST['extract_acf_pdfs']) && $_POST['extract_acf_pdfs'] !== 'false';
3713 - $mxchat_options = get_option('mxchat_options', array());
3714 - if (!is_array($mxchat_options)) {
3715 - $mxchat_options = array();
3716 - }
3717 - $prior_default = !empty($mxchat_options['acf_pdf_extract_default']);
3718 - if ($prior_default !== $extract_acf_pdfs) {
3719 - $mxchat_options['acf_pdf_extract_default'] = $extract_acf_pdfs ? 1 : 0;
3720 - update_option('mxchat_options', $mxchat_options);
3721 - }
3722 -
3723 - // Process only ONE post at a time to avoid request size issues
3724 - $post_id = reset($post_ids);
3725 - $post = get_post($post_id);
3726 -
3727 - if (!$post) {
3728 - wp_send_json_error('Post not found');
3729 - exit;
3730 - }
3731 -
3732 - // Allow developers to modify post data before processing into knowledge base
3733 - $post = apply_filters('mxchat_before_process_post', $post, $bot_id);
3734 -
3735 - // Get content including title, short description (for WooCommerce), and main content
3736 - $content = $post->post_title . "\n\n";
3737 -
3738 - // Add short description if it exists (WooCommerce products use post_excerpt for short description)
3739 - if (!empty($post->post_excerpt)) {
3740 - // Remove shortcode tags but preserve content inside them
3741 - $clean_excerpt = $this->strip_shortcode_tags_preserve_content($post->post_excerpt);
3742 - $content .= "Short Description: " . wp_strip_all_tags($clean_excerpt) . "\n\n";
3743 - }
3744 -
3745 - // Add main content - remove shortcode tags but preserve content inside them
3746 - $clean_content = $this->strip_shortcode_tags_preserve_content($post->post_content);
3747 - $content .= wp_strip_all_tags($clean_content);
3748 -
3749 - // ADD WOOCOMMERCE PRODUCT DATA (pricing, stock, categories, custom tabs)
3750 - if (get_post_type($post_id) === 'product' && class_exists('WooCommerce')) {
3751 - $product = wc_get_product($post_id);
3752 -
3753 - if ($product) {
3754 - // Get pricing information
3755 - $regular_price = $product->get_regular_price();
3756 - $sale_price = $product->get_sale_price();
3757 - $price = $product->get_price();
3758 - $sku = $product->get_sku();
3759 -
3760 - // Get currency symbol
3761 - $currency_symbol = get_woocommerce_currency_symbol();
3762 -
3763 - // Add pricing information
3764 - $content .= "\n";
3765 - if (!empty($regular_price)) {
3766 - $content .= "Price: " . $currency_symbol . $regular_price . "\n";
3767 - } elseif (!empty($price)) {
3768 - $content .= "Price: " . $currency_symbol . $price . "\n";
3769 - }
3770 -
3771 - if (!empty($sale_price) && $sale_price !== $regular_price) {
3772 - $content .= "Sale Price: " . $currency_symbol . $sale_price . "\n";
3773 - }
3774 -
3775 - // Handle variable products - show price range
3776 - if ($product->is_type('variable')) {
3777 - $min_price = $product->get_variation_price('min');
3778 - $max_price = $product->get_variation_price('max');
3779 - if ($min_price !== $max_price) {
3780 - $content .= "Price Range: " . $currency_symbol . $min_price . " - " . $currency_symbol . $max_price . "\n";
3781 - }
3782 - }
3783 -
3784 - if (!empty($sku)) {
3785 - $content .= "SKU: " . $sku . "\n";
3786 - }
3787 -
3788 - // Get product categories
3789 - $categories = wp_get_post_terms($post_id, 'product_cat', array('fields' => 'names'));
3790 - if (!empty($categories) && !is_wp_error($categories)) {
3791 - $content .= "Categories: " . implode(', ', $categories) . "\n";
3792 - }
3793 - }
3794 -
3795 - // Get Custom Product Tabs (supports "Custom Product Tabs for WooCommerce" by Code Parrots)
3796 - $custom_tabs = get_post_meta($post_id, 'yikes_woo_products_tabs', true);
3797 - if (!empty($custom_tabs) && is_array($custom_tabs)) {
3798 - foreach ($custom_tabs as $tab) {
3799 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
3800 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
3801 -
3802 - if (!empty($tab_title) && !empty($tab_content)) {
3803 - $content .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
3804 - }
3805 - }
3806 - }
3807 -
3808 - // Also check for reusable/saved tabs applied to this product
3809 - $applied_saved_tabs = get_post_meta($post_id, 'yikes_woo_reusable_products_tabs_applied', true);
3810 - if (!empty($applied_saved_tabs) && is_array($applied_saved_tabs)) {
3811 - $saved_tabs = get_option('yikes_woo_reusable_products_tabs', array());
3812 - if (!empty($saved_tabs) && is_array($saved_tabs)) {
3813 - foreach ($applied_saved_tabs as $saved_tab_id) {
3814 - if (isset($saved_tabs[$saved_tab_id])) {
3815 - $tab = $saved_tabs[$saved_tab_id];
3816 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
3817 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
3818 -
3819 - if (!empty($tab_title) && !empty($tab_content)) {
3820 - $content .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
3821 - }
3822 - }
3823 - }
3824 - }
3825 - }
3826 - }
3827 -
3828 - // ADD ACF FIELDS SUPPORT
3829 - $acf_fields = $this->mxchat_get_acf_fields_for_post($post_id);
3830 - $pdf_extracted_count = 0;
3831 - if (!empty($acf_fields)) {
3832 - $acf_content_parts = array();
3833 - $pdf_attachment_ids = array();
3834 -
3835 - foreach ($acf_fields as $field_name => $field_value) {
3836 - $formatted_value = $this->mxchat_format_acf_field_value($field_value, $field_name, $post_id);
3837 -
3838 - if (!empty($formatted_value)) {
3839 - $field_label = ucwords(str_replace('_', ' ', $field_name));
3840 - $acf_content_parts[] = $field_label . ": " . $formatted_value;
3841 - }
3842 -
3843 - // Walk this field's value tree for any PDF attachment references and queue them for extraction.
3844 - // Only when the user opted into ACF→PDF extraction for this batch; otherwise the ACF text
3845 - // still lands in the KB but the heavier PDF parsing is skipped.
3846 - if ($extract_acf_pdfs) {
3847 - $this->mxchat_collect_pdf_attachment_ids_from_acf_value($field_value, $pdf_attachment_ids);
3848 - }
3849 - }
3850 -
3851 - if (!empty($acf_content_parts)) {
3852 - $content .= "\n\n" . implode("\n", $acf_content_parts);
3853 - }
3854 -
3855 - // Extract text from each unique PDF found in ACF fields and append as a labeled section
3856 - if ($extract_acf_pdfs && !empty($pdf_attachment_ids)) {
3857 - $pdf_attachment_ids = array_unique(array_filter(array_map('intval', $pdf_attachment_ids)));
3858 - $pdf_sections = array();
3859 - foreach ($pdf_attachment_ids as $att_id) {
3860 - $pdf_text = $this->mxchat_extract_pdf_text_by_attachment_id($att_id);
3861 - if (!empty($pdf_text)) {
3862 - $pdf_title = get_the_title($att_id);
3863 - $pdf_url = wp_get_attachment_url($att_id);
3864 - $header = 'PDF Attachment';
3865 - if (!empty($pdf_title)) {
3866 - $header .= ': ' . $pdf_title;
3867 - }
3868 - if (!empty($pdf_url)) {
3869 - $header .= ' (' . $pdf_url . ')';
3870 - }
3871 - $pdf_sections[] = $header . "\n" . $pdf_text;
3872 - $pdf_extracted_count++;
3873 - }
3874 - }
3875 - if (!empty($pdf_sections)) {
3876 - $content .= "\n\nAttached PDFs:\n" . implode("\n\n", $pdf_sections);
3877 - }
3878 - }
3879 - }
3880 -
3881 - // ADD CUSTOM POST META SUPPORT (whitelisted non-ACF meta fields)
3882 - $custom_meta = $this->mxchat_get_whitelisted_post_meta($post_id);
3883 - if (!empty($custom_meta)) {
3884 - $meta_content_parts = array();
3885 -
3886 - foreach ($custom_meta as $meta_key => $meta_value) {
3887 - // Convert meta key to readable label
3888 - $meta_label = ucwords(str_replace(array('_', '-'), ' ', $meta_key));
3889 - $meta_content_parts[] = $meta_label . ": " . $meta_value;
3890 - }
3891 -
3892 - if (!empty($meta_content_parts)) {
3893 - $content .= "\n\n" . implode("\n", $meta_content_parts);
3894 - }
3895 - }
3896 -
3897 - // Debug logging for WordPress Import content
3898 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Post ID: ' . $post_id . ' Title: ' . $post->post_title);
3899 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Raw post_content length: ' . strlen($post->post_content));
3900 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Final content length: ' . strlen($content));
3901 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Content preview: ' . substr($content, 0, 300));
3902 -
3903 - // Note: Removed 10,000 char limit - chunking now handles large content properly
3904 -
3905 - // Get bot-specific API key
3906 - $bot_options = $this->get_bot_options($bot_id);
3907 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
3908 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
3909 -
3910 - if (strpos($selected_model, 'voyage') === 0) {
3911 - $api_key = $options['voyage_api_key'] ?? '';
3912 - $provider_name = 'Voyage AI';
3913 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
3914 - $api_key = $options['gemini_api_key'] ?? '';
3915 - $provider_name = 'Google Gemini';
3916 - } else {
3917 - $api_key = $options['api_key'] ?? '';
3918 - $provider_name = 'OpenAI';
3919 - }
3920 -
3921 - if (empty($api_key)) {
3922 - MxChat_Admin::mxchat_log_debug('api_error', $provider_name . ' API key not configured for knowledge processing');
3923 - wp_send_json_error($provider_name . ' API key not configured');
3924 - exit;
3925 - }
3926 -
3927 - $source_url = get_permalink($post_id);
3928 - $vector_id = md5($source_url); // Vector ID for Pinecone
3929 -
3930 - // Check for existing content in bot-specific storage
3931 - $is_update = false;
3932 -
3933 - // Get bot-specific Pinecone configuration
3934 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
3935 - $use_pinecone = !empty($bot_pinecone_config) && ($bot_pinecone_config['use_pinecone'] ?? false);
3936 -
3937 - if ($use_pinecone && !empty($bot_pinecone_config['api_key'])) {
3938 - // Check Pinecone for this bot
3939 - $pinecone_data = $this->mxchat_get_pinecone_processed_content($bot_pinecone_config);
3940 - if (isset($pinecone_data[$post_id])) {
3941 - $is_update = true;
3942 - }
3943 - } else {
3944 - // Check WordPress DB (same as before since it's shared)
3945 - global $wpdb;
3946 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3947 - $existing_record = $wpdb->get_row($wpdb->prepare(
3948 - "SELECT id FROM $table_name WHERE source_url = %s",
3949 - $source_url
3950 - ));
3951 -
3952 - if ($existing_record) {
3953 - $is_update = true;
3954 - }
3955 - }
3956 -
3957 - // UPDATED 2.5.6: Determine content type based on post_type
3958 - $post_type = $post->post_type;
3959 - $content_type = 'content'; // Default fallback
3960 -
3961 - // Map WordPress post types to content types
3962 - switch ($post_type) {
3963 - case 'post':
3964 - $content_type = 'post';
3965 - break;
3966 - case 'page':
3967 - $content_type = 'page';
3968 - break;
3969 - case 'product':
3970 - $content_type = 'product';
3971 - break;
3972 - default:
3973 - // For custom post types, use the post type name
3974 - $content_type = sanitize_key($post_type);
3975 - break;
3976 - }
3977 -
3978 - // Use the centralized utility function with bot_id and content_type
3979 - $result = MxChat_Utils::submit_content_to_db(
3980 - $content,
3981 - $source_url,
3982 - $api_key,
3983 - $vector_id,
3984 - $bot_id,
3985 - $content_type
3986 - );
3987 -
3988 - if (is_wp_error($result)) {
3989 - MxChat_Admin::mxchat_log_debug('storage_error', 'Knowledge storage failed: ' . $result->get_error_message(), array('source_url' => $source_url));
3990 - wp_send_json_error('Storage failed: ' . $result->get_error_message());
3991 - exit;
3992 - }
3993 -
3994 - // Automatically apply role restriction based on tags
3995 - $this->apply_role_restriction_to_post($post_id, $source_url);
3996 -
3997 - $operation_type = $is_update ? 'update' : 'new';
3998 -
3999 - // Count ACF fields for debugging
4000 - $acf_field_count = count($acf_fields);
4001 -
4002 - // Success response with minimal data
4003 - wp_send_json_success(array(
4004 - 'message' => $operation_type === 'update' ? 'Content updated successfully' : 'Content processed successfully',
4005 - 'post_id' => $post_id,
4006 - 'title' => $post->post_title,
4007 - 'operation_type' => $operation_type,
4008 - 'vector_id' => $vector_id,
4009 - 'acf_fields_found' => $acf_field_count,
4010 - 'pdf_extracted_count' => (int) $pdf_extracted_count,
4011 - 'content_preview' => substr($content, 0, 100) . '...',
4012 - 'bot_id' => $bot_id
4013 - ));
4014 - exit;
4015 -}
4016 -
4017 -private function apply_role_restriction_to_post($post_id, $source_url) {
4018 - // Get tag-role mappings
4019 - $mappings = get_option('mxchat_tag_role_mappings', array());
4020 -
4021 - if (empty($mappings)) {
4022 - return; // No mappings, leave as public
4023 - }
4024 -
4025 - // Get all tags for the post
4026 - $post_tags = wp_get_post_tags($post_id, array('fields' => 'slugs'));
4027 -
4028 - if (empty($post_tags)) {
4029 - return; // No tags, leave as public
4030 - }
4031 -
4032 - // Determine the highest role restriction based on tags
4033 - $highest_role = 'public';
4034 - $role_hierarchy = array(
4035 - 'public' => 0,
4036 - 'logged_in' => 1,
4037 - 'subscriber' => 2,
4038 - 'contributor' => 3,
4039 - 'author' => 4,
4040 - 'editor' => 5,
4041 - 'administrator' => 6
4042 - );
4043 -
4044 - foreach ($post_tags as $tag_slug) {
4045 - if (isset($mappings[$tag_slug])) {
4046 - $role = $mappings[$tag_slug];
4047 - if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
4048 - $highest_role = $role;
4049 - }
4050 - }
4051 - }
4052 -
4053 - // If no restricted tags found, return (leave as public)
4054 - if ($highest_role === 'public') {
4055 - return;
4056 - }
4057 -
4058 - // Update the role restriction in the database
4059 - global $wpdb;
4060 -
4061 - // Check if using Pinecone
4062 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
4063 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4064 -
4065 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
4066 - // Update Pinecone role restriction
4067 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
4068 - $vector_id = md5($source_url);
4069 -
4070 - $wpdb->replace(
4071 - $roles_table,
4072 - array(
4073 - 'vector_id' => $vector_id,
4074 - 'role_restriction' => $highest_role,
4075 - 'updated_at' => current_time('mysql')
4076 - ),
4077 - array('%s', '%s', '%s')
4078 - );
4079 - } else {
4080 - // Update WordPress DB
4081 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4082 -
4083 - $wpdb->update(
4084 - $table_name,
4085 - array('role_restriction' => $highest_role),
4086 - array('source_url' => $source_url),
4087 - array('%s'),
4088 - array('%s')
4089 - );
4090 - }
4091 -}
4092 -
4093 -public function mxchat_get_public_post_types() {
4094 - // Get all public post types
4095 - $post_types = get_post_types(array('public' => true), 'objects');
4096 - $post_type_options = array();
4097 -
4098 - foreach ($post_types as $post_type) {
4099 - $post_type_options[$post_type->name] = $post_type->label;
4100 - }
4101 -
4102 - // Also include common forum/community post types that might not be marked as public
4103 - $additional_types = array(
4104 - 'topic' => 'Forum Topics (bbPress)',
4105 - 'reply' => 'Forum Replies (bbPress)',
4106 - 'forum' => 'Forums (bbPress)',
4107 - 'wpforo_topic' => 'wpForo Topics',
4108 - 'wpforo_post' => 'wpForo Posts'
4109 - );
4110 -
4111 - foreach ($additional_types as $type_name => $type_label) {
4112 - if (post_type_exists($type_name) && !isset($post_type_options[$type_name])) {
4113 - $post_type_options[$type_name] = $type_label;
4114 - }
4115 - }
4116 -
4117 - return $post_type_options;
4118 -}
4119 -
4120 -/**
4121 - * Retrieves processed content from Pinecone API
4122 - */
4123 -public function mxchat_get_pinecone_processed_content($pinecone_options) {
4124 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
4125 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
4126 -
4127 - if (empty($api_key) || empty($host)) {
4128 - return array();
4129 - }
4130 -
4131 - $pinecone_data = array();
4132 -
4133 - try {
4134 - // Always get fresh data from Pinecone
4135 - $pinecone_data = $this->mxchat_scan_pinecone_for_processed_content($pinecone_options);
4136 -
4137 - // Method 2: Final fallback - try stats endpoint (if available)
4138 - if (empty($pinecone_data)) {
4139 - $stats_url = "https://{$host}/describe_index_stats";
4140 -
4141 - $response = wp_remote_post($stats_url, array(
4142 - 'headers' => array(
4143 - 'Api-Key' => $api_key,
4144 - 'Content-Type' => 'application/json'
4145 - ),
4146 - 'body' => json_encode(array()),
4147 - 'timeout' => 30
4148 - ));
4149 -
4150 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
4151 - $body = wp_remote_retrieve_body($response);
4152 - $stats_data = json_decode($body, true);
4153 - }
4154 - }
4155 -
4156 - } catch (Exception $e) {
4157 - // Log error but return fresh data only
4158 - }
4159 -
4160 - return $pinecone_data;
4161 -}
4162 -public function mxchat_fetch_pinecone_vectors_by_ids($pinecone_options, $vector_ids) {
4163 - //error_log('=== DEBUG: Starting mxchat_fetch_pinecone_vectors_by_ids ===');
4164 -
4165 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
4166 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
4167 -
4168 - if (empty($api_key) || empty($host) || empty($vector_ids)) {
4169 - //error_log('DEBUG: Missing parameters for fetch by IDs');
4170 - return array();
4171 - }
4172 -
4173 - try {
4174 - $fetch_url = "https://{$host}/vectors/fetch";
4175 - //error_log('DEBUG: Fetch URL: ' . $fetch_url);
4176 - //error_log('DEBUG: Fetching ' . count($vector_ids) . ' vector IDs');
4177 -
4178 - // Pinecone fetch API allows fetching specific vectors by ID
4179 - $fetch_data = array(
4180 - 'ids' => array_values($vector_ids)
4181 - );
4182 -
4183 - $response = wp_remote_post($fetch_url, array(
4184 - 'headers' => array(
4185 - 'Api-Key' => $api_key,
4186 - 'Content-Type' => 'application/json'
4187 - ),
4188 - 'body' => json_encode($fetch_data),
4189 - 'timeout' => 30
4190 - ));
4191 -
4192 - if (is_wp_error($response)) {
4193 - //error_log('DEBUG: Fetch by IDs WP error: ' . $response->get_error_message());
4194 - return array();
4195 - }
4196 -
4197 - $response_code = wp_remote_retrieve_response_code($response);
4198 - //error_log('DEBUG: Fetch response code: ' . $response_code);
4199 -
4200 - if ($response_code !== 200) {
4201 - $error_body = wp_remote_retrieve_body($response);
4202 - //error_log('DEBUG: Fetch failed with body: ' . $error_body);
4203 - return array();
4204 - }
4205 -
4206 - $body = wp_remote_retrieve_body($response);
4207 - $data = json_decode($body, true);
4208 -
4209 - //error_log('DEBUG: Fetch response structure: ' . print_r(array_keys($data), true));
4210 -
4211 - if (!isset($data['vectors'])) {
4212 - //error_log('DEBUG: No vectors key in response');
4213 - return array();
4214 - }
4215 -
4216 - $processed_data = array();
4217 -
4218 - foreach ($data['vectors'] as $vector_id => $vector_data) {
4219 - $metadata = $vector_data['metadata'] ?? array();
4220 - $source_url = $metadata['source_url'] ?? '';
4221 -
4222 - if (!empty($source_url)) {
4223 - $post_id = url_to_postid($source_url);
4224 - if ($post_id) {
4225 - $created_at = $metadata['created_at'] ?? '';
4226 - $processed_date = 'Recently';
4227 -
4228 - if (!empty($created_at)) {
4229 - $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
4230 - if ($timestamp) {
4231 - $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
4232 - }
4233 - }
4234 -
4235 - $processed_data[$post_id] = array(
4236 - 'db_id' => $vector_id,
4237 - 'processed_date' => $processed_date,
4238 - 'url' => $source_url,
4239 - 'source' => 'pinecone',
4240 - 'timestamp' => $timestamp ?? current_time('timestamp')
4241 - );
4242 - }
4243 - }
4244 - }
4245 -
4246 - //error_log('DEBUG: Processed ' . count($processed_data) . ' vectors from fetch');
4247 - return $processed_data;
4248 -
4249 - } catch (Exception $e) {
4250 - //error_log('DEBUG: Exception in fetch_pinecone_vectors_by_ids: ' . $e->getMessage());
4251 - return array();
4252 - }
4253 -}
4254 -
4255 -/**
4256 - * Get embedding dimensions based on the selected model.
4257 - */
4258 -private function mxchat_get_embedding_dimensions() {
4259 - $options = get_option('mxchat_options', array());
4260 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4261 -
4262 - $model_dimensions = array(
4263 - 'text-embedding-ada-002' => 1536,
4264 - 'text-embedding-3-small' => 1536,
4265 - 'text-embedding-3-large' => 3072,
4266 - 'voyage-2' => 1024,
4267 - 'voyage-large-2' => 1536,
4268 - 'voyage-3-large' => 2048,
4269 - 'gemini-embedding-001' => 1536,
4270 - );
4271 -
4272 - if (strpos($selected_model, 'voyage-3-large') === 0) {
4273 - $custom_dimensions = $options['voyage_output_dimension'] ?? 2048;
4274 - return intval($custom_dimensions);
4275 - }
4276 -
4277 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4278 - $custom_dimensions = $options['gemini_output_dimension'] ?? 1536;
4279 - return intval($custom_dimensions);
4280 - }
4281 -
4282 - return $model_dimensions[$selected_model] ?? 1536;
4283 -}
4284 -
4285 -/**
4286 - * Scan Pinecone for processed content
4287 - */
4288 -public function mxchat_scan_pinecone_for_processed_content($pinecone_options) {
4289 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
4290 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
4291 -
4292 - if (empty($api_key) || empty($host)) {
4293 - return array();
4294 - }
4295 -
4296 - try {
4297 - // Use multiple random vectors to get better coverage
4298 - $all_matches = array();
4299 - $seen_ids = array();
4300 -
4301 - // Get correct dimensions for the configured embedding model
4302 - $dimensions = $this->mxchat_get_embedding_dimensions();
4303 -
4304 - // Try 3 different random vectors to get better coverage
4305 - for ($i = 0; $i < 3; $i++) {
4306 - $query_url = "https://{$host}/query";
4307 -
4308 - // Generate a random unit vector instead of zeros
4309 - $random_vector = array();
4310 - for ($j = 0; $j < $dimensions; $j++) {
4311 - $random_vector[] = (rand(-1000, 1000) / 1000.0);
4312 - }
4313 -
4314 - // Normalize the vector to unit length
4315 - $magnitude = sqrt(array_sum(array_map(function($x) { return $x * $x; }, $random_vector)));
4316 - if ($magnitude > 0) {
4317 - $random_vector = array_map(function($x) use ($magnitude) { return $x / $magnitude; }, $random_vector);
4318 - }
4319 -
4320 - $query_data = array(
4321 - 'includeMetadata' => true,
4322 - 'includeValues' => false,
4323 - 'topK' => 10000,
4324 - 'vector' => $random_vector
4325 - );
4326 -
4327 - $response = wp_remote_post($query_url, array(
4328 - 'headers' => array(
4329 - 'Api-Key' => $api_key,
4330 - 'Content-Type' => 'application/json'
4331 - ),
4332 - 'body' => json_encode($query_data),
4333 - 'timeout' => 30
4334 - ));
4335 -
4336 - if (is_wp_error($response)) {
4337 - continue;
4338 - }
4339 -
4340 - $response_code = wp_remote_retrieve_response_code($response);
4341 -
4342 - if ($response_code !== 200) {
4343 - continue;
4344 - }
4345 -
4346 - $body = wp_remote_retrieve_body($response);
4347 - $data = json_decode($body, true);
4348 -
4349 - if (isset($data['matches'])) {
4350 - foreach ($data['matches'] as $match) {
4351 - $match_id = $match['id'] ?? '';
4352 - if (!empty($match_id) && !isset($seen_ids[$match_id])) {
4353 - $all_matches[] = $match;
4354 - $seen_ids[$match_id] = true;
4355 - }
4356 - }
4357 - }
4358 - }
4359 -
4360 - // Convert matches to processed data format, grouping by URL to count chunks
4361 - $processed_data = array();
4362 - $url_chunk_counts = array();
4363 -
4364 - foreach ($all_matches as $match) {
4365 - $metadata = $match['metadata'] ?? array();
4366 - $source_url = $metadata['source_url'] ?? '';
4367 - $match_id = $match['id'] ?? '';
4368 -
4369 - if (!empty($source_url) && !empty($match_id)) {
4370 - $post_id = url_to_postid($source_url);
4371 - if ($post_id) {
4372 - // Count chunks per post_id
4373 - if (!isset($url_chunk_counts[$post_id])) {
4374 - $url_chunk_counts[$post_id] = 0;
4375 - }
4376 - $url_chunk_counts[$post_id]++;
4377 -
4378 - $created_at = $metadata['created_at'] ?? '';
4379 - $processed_date = 'Recently';
4380 -
4381 - if (!empty($created_at)) {
4382 - $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
4383 - if ($timestamp) {
4384 - $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
4385 - }
4386 - }
4387 -
4388 - // Only store if not already set, or update with newer timestamp
4389 - if (!isset($processed_data[$post_id]) ||
4390 - ($timestamp ?? 0) > ($processed_data[$post_id]['timestamp'] ?? 0)) {
4391 - $processed_data[$post_id] = array(
4392 - 'db_id' => $match_id,
4393 - 'processed_date' => $processed_date,
4394 - 'url' => $source_url,
4395 - 'source' => 'pinecone',
4396 - 'timestamp' => $timestamp ?? current_time('timestamp')
4397 - );
4398 - }
4399 - }
4400 - }
4401 - }
4402 -
4403 - // Add chunk counts to processed data
4404 - foreach ($url_chunk_counts as $post_id => $chunk_count) {
4405 - if (isset($processed_data[$post_id])) {
4406 - $processed_data[$post_id]['chunk_count'] = $chunk_count;
4407 - }
4408 - }
4409 -
4410 - return $processed_data;
4411 -
4412 - } catch (Exception $e) {
4413 - return array();
4414 - }
4415 -}
4416 -/**
4417 - * Generate embeddings from input text for MXChat with bot support
4418 - */
4419 -private function mxchat_generate_embedding($text, $bot_id = 'default') {
4420 - // Enable detailed logging for debugging
4421 - //error_log('[MXCHAT-EMBED] Starting embedding generation for bot: ' . $bot_id . '. Text length: ' . strlen($text) . ' bytes');
4422 - //error_log('[MXCHAT-EMBED] Text preview: ' . substr($text, 0, 100) . '...');
4423 -
4424 - // Get bot-specific options
4425 - $bot_options = $this->get_bot_options($bot_id);
4426 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
4427 -
4428 - // Opt-in: when the custom provider is selected for embeddings, index through
4429 - // the same custom endpoint the query path uses so stored vectors and query
4430 - // vectors share a model. Returns the vector array on success, or an error
4431 - // string on failure (this function's existing failure contract).
4432 - if (isset($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
4433 - if (!class_exists('MxChat_Utils')) {
4434 - require_once dirname(__FILE__) . '/../includes/class-mxchat-utils.php';
4435 - }
4436 - return MxChat_Utils::generate_embedding_custom($text, $options);
4437 - }
4438 -
4439 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4440 - //error_log('[MXCHAT-EMBED] Selected embedding model for bot ' . $bot_id . ': ' . $selected_model);
4441 -
4442 - // Determine provider and endpoint
4443 - if (strpos($selected_model, 'voyage') === 0) {
4444 - $api_key = $options['voyage_api_key'] ?? '';
4445 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
4446 - $provider_name = 'Voyage AI';
4447 - //error_log('[MXCHAT-EMBED] Using Voyage AI API for bot ' . $bot_id);
4448 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
4449 - $api_key = $options['gemini_api_key'] ?? '';
4450 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
4451 - $provider_name = 'Google Gemini';
4452 - //error_log('[MXCHAT-EMBED] Using Google Gemini API for bot ' . $bot_id);
4453 - } else {
4454 - $api_key = $options['api_key'] ?? '';
4455 - $endpoint = 'https://api.openai.com/v1/embeddings';
4456 - $provider_name = 'OpenAI';
4457 - //error_log('[MXCHAT-EMBED] Using OpenAI API for bot ' . $bot_id);
4458 - }
4459 -
4460 - //error_log('[MXCHAT-EMBED] Using endpoint: ' . $endpoint);
4461 -
4462 - if (empty($api_key)) {
4463 - $error_message = sprintf('Missing %s API key for bot %s. Please configure your API key in the bot settings.', $provider_name, $bot_id);
4464 - //error_log('[MXCHAT-EMBED] Error: ' . $error_message);
4465 - return $error_message;
4466 - }
4467 -
4468 - // Check if text is too long (for OpenAI, roughly estimate tokens as words/0.75)
4469 - $estimated_tokens = ceil(str_word_count($text) / 0.75);
4470 - //error_log('[MXCHAT-EMBED] Estimated token count: ~' . $estimated_tokens);
4471 -
4472 - if ($estimated_tokens > 8000 && strpos($selected_model, 'voyage') === false && strpos($selected_model, 'gemini-embedding') === false) {
4473 - //error_log('[MXCHAT-EMBED] Warning: Text may exceed OpenAI token limits (8K for most models)');
4474 - // Consider truncating text here
4475 - }
4476 -
4477 - // Prepare request body based on provider
4478 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4479 - // Gemini API format
4480 - $request_body = array(
4481 - 'model' => 'models/' . $selected_model,
4482 - 'content' => array(
4483 - 'parts' => array(
4484 - array('text' => $text)
4485 - )
4486 - )
4487 - );
4488 -
4489 - // Set output dimensionality to 1536 for consistency with other models
4490 - $request_body['outputDimensionality'] = 1536;
4491 - } else {
4492 - // OpenAI/Voyage API format
4493 - $request_body = array(
4494 - 'model' => $selected_model,
4495 - 'input' => $text
4496 - );
4497 -
4498 - // Add output_dimension for voyage-3-large model
4499 - if ($selected_model === 'voyage-3-large') {
4500 - $request_body['output_dimension'] = 2048;
4501 - }
4502 - }
4503 -
4504 - //error_log('[MXCHAT-EMBED] Request prepared with model: ' . $selected_model);
4505 -
4506 - // Prepare headers based on provider
4507 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4508 - // Gemini uses API key as query parameter
4509 - $endpoint .= '?key=' . $api_key;
4510 - $headers = array(
4511 - 'Content-Type' => 'application/json'
4512 - );
4513 - } else {
4514 - // OpenAI/Voyage use Bearer token
4515 - $headers = array(
4516 - 'Authorization' => 'Bearer ' . $api_key,
4517 - 'Content-Type' => 'application/json'
4518 - );
4519 - }
4520 -
4521 - // Make API request
4522 - //error_log('[MXCHAT-EMBED] Sending API request to: ' . $endpoint);
4523 - $response = wp_remote_post($endpoint, array(
4524 - 'body' => wp_json_encode($request_body),
4525 - 'headers' => $headers,
4526 - 'timeout' => 60 // Increased timeout for large inputs
4527 - ));
4528 -
4529 - // Handle wp_remote_post errors
4530 - if (is_wp_error($response)) {
4531 - $error_message = $response->get_error_message();
4532 - //error_log('[MXCHAT-EMBED] WP Remote Post Error: ' . $error_message);
4533 - return 'Connection error: ' . $error_message;
4534 - }
4535 -
4536 - // Get and check HTTP response code
4537 - $http_code = wp_remote_retrieve_response_code($response);
4538 - //error_log('[MXCHAT-EMBED] API Response Code: ' . $http_code);
4539 -
4540 - if ($http_code !== 200) {
4541 - $error_body = wp_remote_retrieve_body($response);
4542 - //error_log('[MXCHAT-EMBED] API Error Response Body: ' . $error_body);
4543 -
4544 - // Try to parse error for more details
4545 - $error_json = json_decode($error_body, true);
4546 - if (json_last_error() === JSON_ERROR_NONE && isset($error_json['error'])) {
4547 - $error_type = $error_json['error']['type'] ?? 'unknown';
4548 - $error_message = $error_json['error']['message'] ?? 'No message';
4549 - //error_log('[MXCHAT-EMBED] API Error Type: ' . $error_type);
4550 - //error_log('[MXCHAT-EMBED] API Error Message: ' . $error_message);
4551 -
4552 - // Customize error message for common API errors
4553 - if ($error_type === 'invalid_request_error' && strpos($error_message, 'API key') !== false) {
4554 - $error_message = sprintf('Invalid %s API key for bot %s. Please check your API key in the bot settings.', $provider_name, $bot_id);
4555 - } elseif ($error_type === 'authentication_error') {
4556 - $error_message = sprintf('%s authentication failed for bot %s. Please verify your API key in the bot settings.', $provider_name, $bot_id);
4557 - }
4558 -
4559 - //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
4560 - return $error_message;
4561 - }
4562 -
4563 - $error_message = sprintf("API Error (HTTP %d): Unable to generate embedding for bot %s", $http_code, $bot_id);
4564 - //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
4565 - return $error_message;
4566 - }
4567 -
4568 - // Parse response body
4569 - $response_body = wp_remote_retrieve_body($response);
4570 - //error_log('[MXCHAT-EMBED] Received response length: ' . strlen($response_body) . ' bytes');
4571 -
4572 - $response_data = json_decode($response_body, true);
4573 -
4574 - if (json_last_error() !== JSON_ERROR_NONE) {
4575 - $error = json_last_error_msg();
4576 - //error_log('[MXCHAT-EMBED] JSON Parse Error: ' . $error);
4577 - //error_log('[MXCHAT-EMBED] Response preview: ' . substr($response_body, 0, 200));
4578 - return "Failed to parse API response: $error";
4579 - }
4580 -
4581 - // Handle different response formats based on provider
4582 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4583 - // Gemini API response format
4584 - if (isset($response_data['embedding']['values'])) {
4585 - $embedding_dimensions = count($response_data['embedding']['values']);
4586 - //error_log('[MXCHAT-EMBED] Successfully extracted Gemini embedding with ' . $embedding_dimensions . ' dimensions');
4587 -
4588 - // Check if embedding dimensions are as expected (should be 1536)
4589 - if ($embedding_dimensions !== 1536) {
4590 - //error_log('[MXCHAT-EMBED] Warning: Unexpected Gemini embedding dimensions: ' . $embedding_dimensions);
4591 - }
4592 -
4593 - return $response_data['embedding']['values'];
4594 - } else {
4595 - //error_log('[MXCHAT-EMBED] Error: No embedding found in Gemini response');
4596 - //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
4597 -
4598 - if (isset($response_data['error'])) {
4599 - $error_message = "Gemini API Error in response: " . wp_json_encode($response_data['error']);
4600 - //error_log('[MXCHAT-EMBED] ' . $error_message);
4601 - return $error_message;
4602 - }
4603 -
4604 - $error_message = "Invalid Gemini API response format: No embedding found";
4605 - //error_log('[MXCHAT-EMBED] ' . $error_message);
4606 - return $error_message;
4607 - }
4608 - } else {
4609 - // OpenAI/Voyage API response format
4610 - if (isset($response_data['data'][0]['embedding'])) {
4611 - $embedding_dimensions = count($response_data['data'][0]['embedding']);
4612 - //error_log('[MXCHAT-EMBED] Successfully extracted embedding with ' . $embedding_dimensions . ' dimensions');
4613 -
4614 - // Check if embedding dimensions are as expected
4615 - if (($selected_model === 'text-embedding-ada-002' && $embedding_dimensions !== 1536) ||
4616 - ($selected_model === 'voyage-3-large' && $embedding_dimensions !== 2048)) {
4617 - //error_log('[MXCHAT-EMBED] Warning: Unexpected embedding dimensions');
4618 - }
4619 -
4620 - return $response_data['data'][0]['embedding'];
4621 - } else {
4622 - //error_log('[MXCHAT-EMBED] Error: No embedding found in response');
4623 - //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
4624 -
4625 - if (isset($response_data['error'])) {
4626 - $error_message = "API Error in response: " . wp_json_encode($response_data['error']);
4627 - //error_log('[MXCHAT-EMBED] ' . $error_message);
4628 - return $error_message;
4629 - }
4630 -
4631 - $error_message = "Invalid API response format: No embedding found";
4632 - //error_log('[MXCHAT-EMBED] ' . $error_message);
4633 - return $error_message;
4634 - }
4635 - }
4636 -}
4637 -
4638 -/**
4639 - * Get bot-specific options for multi-bot functionality
4640 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
4641 - */
4642 -private function get_bot_options($bot_id = 'default') {
4643 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
4644 -
4645 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
4646 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
4647 - return array();
4648 - }
4649 -
4650 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
4651 -
4652 - if (!empty($bot_options)) {
4653 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
4654 - if (isset($bot_options['similarity_threshold'])) {
4655 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
4656 - }
4657 - }
4658 -
4659 - return is_array($bot_options) ? $bot_options : array();
4660 -}
4661 -
4662 -/**
4663 - * Get bot-specific Pinecone configuration
4664 - * Used in the knowledge retrieval functions
4665 - */
4666 -// Also add debugging to your get_bot_pinecone_config function
4667 -private function get_bot_pinecone_config($bot_id = 'default') {
4668 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
4669 -
4670 - // If default bot or multi-bot add-on not active, use default Pinecone config
4671 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
4672 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
4673 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
4674 - $config = array(
4675 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
4676 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
4677 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
4678 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
4679 - );
4680 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
4681 - return $config;
4682 - }
4683 -
4684 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
4685 -
4686 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
4687 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
4688 -
4689 - if (!empty($bot_pinecone_config)) {
4690 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
4691 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
4692 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
4693 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
4694 - } else {
4695 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
4696 - }
4697 -
4698 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
4699 -}
4700 -
4701 -
4702 -public function mxchat_ajax_dismiss_completed_status() {
4703 - try {
4704 - // Verify the request
4705 - check_ajax_referer('mxchat_status_nonce', 'nonce');
4706 -
4707 - if (!current_user_can('manage_options')) {
4708 - wp_send_json_error('Unauthorized access');
4709 - exit;
4710 - }
4711 -
4712 - $card_type = isset($_POST['card_type']) ? sanitize_text_field($_POST['card_type']) : '';
4713 -
4714 - if ($card_type === 'pdf') {
4715 - // Clear PDF status
4716 - $pdf_url = get_transient('mxchat_last_pdf_url');
4717 - if ($pdf_url) {
4718 - delete_transient('mxchat_pdf_status_' . md5($pdf_url));
4719 - delete_transient('mxchat_last_pdf_url');
4720 - }
4721 - } elseif ($card_type === 'sitemap') {
4722 - // Clear sitemap status
4723 - $sitemap_url = get_transient('mxchat_last_sitemap_url');
4724 - if ($sitemap_url) {
4725 - delete_transient('mxchat_sitemap_status_' . md5($sitemap_url));
4726 - delete_transient('mxchat_last_sitemap_url');
4727 - }
4728 - }
4729 -
4730 - wp_send_json_success(array('message' => 'Status dismissed successfully'));
4731 -
4732 - } catch (Exception $e) {
4733 - wp_send_json_error(array('message' => 'Error dismissing status: ' . $e->getMessage()));
4734 - }
4735 -}
4736 -
4737 -/**
4738 - * Render completed status cards on page load
4739 - * This ensures completed processing status persists through page refreshes
4740 - */
4741 -public function mxchat_render_completed_status_cards() {
4742 - $output = '';
4743 -
4744 - // Check for completed PDF status
4745 - $pdf_url = get_transient('mxchat_last_pdf_url');
4746 - if ($pdf_url) {
4747 - $pdf_status = $this->mxchat_get_pdf_processing_status($pdf_url);
4748 - if ($pdf_status && ($pdf_status['status'] === 'complete' || $pdf_status['status'] === 'error')) {
4749 - $output .= $this->mxchat_render_pdf_status_card($pdf_status, $pdf_url);
4750 - }
4751 - }
4752 -
4753 - // Check for completed sitemap status
4754 - $sitemap_url = get_transient('mxchat_last_sitemap_url');
4755 - if ($sitemap_url) {
4756 - $sitemap_status = $this->mxchat_get_sitemap_processing_status($sitemap_url);
4757 - if ($sitemap_status && ($sitemap_status['status'] === 'complete' || $sitemap_status['status'] === 'error')) {
4758 - $output .= $this->mxchat_render_sitemap_status_card($sitemap_status, $sitemap_url);
4759 - }
4760 - }
4761 -
4762 - return $output;
4763 -}
4764 -
4765 -/**
4766 - * Render PDF status card HTML
4767 - */
4768 -private function mxchat_render_pdf_status_card($status, $pdf_url) {
4769 - $html = '<div class="mxchat-status-card" data-card-type="pdf">';
4770 - $html .= '<div class="mxchat-status-header">';
4771 - $html .= '<h4>' . esc_html__('PDF Processing Status', 'mxchat') . '</h4>';
4772 -
4773 - // Add dismiss button for completed status
4774 - if ($status['status'] === 'complete' || $status['status'] === 'error') {
4775 - $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
4776 - }
4777 -
4778 - // Process Batch button for processing status
4779 - if ($status['status'] === 'processing') {
4780 - $html .= '<button type="button" class="mxchat-manual-batch-btn"
4781 - data-process-type="pdf"
4782 - data-url="' . esc_attr($pdf_url) . '">
4783 - ' . esc_html__('Process Batch', 'mxchat') . '</button>';
4784 - }
4785 -
4786 - // Add status badges
4787 - if ($status['status'] === 'error') {
4788 - $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
4789 - } elseif ($status['status'] === 'complete') {
4790 - if ($status['failed_pages'] > 0) {
4791 - $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
4792 - sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_pages']) . '</span>';
4793 - } else {
4794 - $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
4795 - }
4796 - }
4797 -
4798 - $html .= '</div>'; // End header
4799 -
4800 - // Progress bar
4801 - $html .= '<div class="mxchat-progress-bar">';
4802 - $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
4803 - $html .= '</div>';
4804 -
4805 - // Status details
4806 - $html .= '<div class="mxchat-status-details">';
4807 - $html .= '<p>' . sprintf(
4808 - esc_html__('Progress: %d of %d pages (%d%%)', 'mxchat'),
4809 - $status['processed_pages'],
4810 - $status['total_pages'],
4811 - $status['percentage']
4812 - ) . '</p>';
4813 -
4814 - // Show failed pages count if any
4815 - if ($status['failed_pages'] > 0) {
4816 - $html .= '<p><strong>' . esc_html__('Failed pages:', 'mxchat') . '</strong> ' . esc_html($status['failed_pages']) . '</p>';
4817 - }
4818 -
4819 - $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
4820 - $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
4821 -
4822 - // Add completion summary if available AND it's an array
4823 - if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
4824 - $summary = $status['completion_summary'];
4825 - $html .= '<div class="mxchat-completion-summary">';
4826 - $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
4827 - $html .= '<p><strong>' . esc_html__('Total Pages:', 'mxchat') . '</strong> ' . esc_html($summary['total_pages']) . '</p>';
4828 - $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_pages']) . '</p>';
4829 - $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_pages']) . '</p>';
4830 - $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
4831 - $html .= '</div>';
4832 - }
4833 -
4834 - // Add failed pages list if any AND it's an array
4835 - if (isset($status['failed_pages_list']) && is_array($status['failed_pages_list']) && !empty($status['failed_pages_list'])) {
4836 - $html .= $this->mxchat_render_failed_pages_list($status['failed_pages_list']);
4837 - }
4838 -
4839 - // Add error message if any
4840 - if (isset($status['error']) && !empty($status['error'])) {
4841 - $html .= '<div class="mxchat-error-notice">';
4842 - $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
4843 - $html .= '</div>';
4844 - }
4845 -
4846 - $html .= '</div>'; // End details
4847 - $html .= '</div>'; // End card
4848 -
4849 - return $html;
4850 -}
4851 -/**
4852 - * Render sitemap status card HTML
4853 - */
4854 -private function mxchat_render_sitemap_status_card($status, $sitemap_url) {
4855 - $html = '<div class="mxchat-status-card" data-card-type="sitemap">';
4856 - $html .= '<div class="mxchat-status-header">';
4857 - $html .= '<h4>' . esc_html__('Sitemap Processing Status', 'mxchat') . '</h4>';
4858 -
4859 - // Add dismiss button for completed status
4860 - if ($status['status'] === 'complete' || $status['status'] === 'error') {
4861 - $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
4862 - }
4863 -
4864 - // Process Batch button for processing status
4865 - if ($status['status'] === 'processing') {
4866 - $html .= '<button type="button" class="mxchat-manual-batch-btn"
4867 - data-process-type="sitemap"
4868 - data-url="' . esc_attr($sitemap_url) . '">
4869 - ' . esc_html__('Process Batch', 'mxchat') . '</button>';
4870 - }
4871 -
4872 - // Add status badges
4873 - if ($status['status'] === 'error') {
4874 - $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
4875 - } elseif ($status['status'] === 'complete') {
4876 - if ($status['failed_urls'] > 0) {
4877 - $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
4878 - sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_urls']) . '</span>';
4879 - } else {
4880 - $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
4881 - }
4882 - }
4883 -
4884 - $html .= '</div>'; // End header
4885 -
4886 - // Progress bar
4887 - $html .= '<div class="mxchat-progress-bar">';
4888 - $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
4889 - $html .= '</div>';
4890 -
4891 - // Status details
4892 - $html .= '<div class="mxchat-status-details">';
4893 - $html .= '<p>' . sprintf(
4894 - esc_html__('Progress: %d of %d URLs (%d%%)', 'mxchat'),
4895 - $status['processed_urls'],
4896 - $status['total_urls'],
4897 - $status['percentage']
4898 - ) . '</p>';
4899 -
4900 - // Show failed URLs count if any
4901 - if ($status['failed_urls'] > 0) {
4902 - $html .= '<p><strong>' . esc_html__('Failed URLs:', 'mxchat') . '</strong> ' . esc_html($status['failed_urls']) . '</p>';
4903 - }
4904 -
4905 - $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
4906 - $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
4907 -
4908 - // Add completion summary if available AND it's an array
4909 - if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
4910 - $summary = $status['completion_summary'];
4911 - $html .= '<div class="mxchat-completion-summary">';
4912 - $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
4913 - $html .= '<p><strong>' . esc_html__('Total URLs:', 'mxchat') . '</strong> ' . esc_html($summary['total_urls']) . '</p>';
4914 - $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_urls']) . '</p>';
4915 - $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_urls']) . '</p>';
4916 - $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
4917 - $html .= '</div>';
4918 - }
4919 -
4920 - // Add error messages if any (but not the failed URLs list)
4921 - if (!empty($status['error']) || !empty($status['last_error'])) {
4922 - $html .= '<div class="mxchat-error-notice">';
4923 -
4924 - if (!empty($status['error'])) {
4925 - $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
4926 - }
4927 -
4928 - if (!empty($status['last_error'])) {
4929 - $html .= '<p class="last-error">' . esc_html__('Last error:', 'mxchat') . ' ' . esc_html($status['last_error']) . '</p>';
4930 - }
4931 -
4932 - $html .= '</div>';
4933 - }
4934 -
4935 - $html .= '</div>'; // End details
4936 - $html .= '</div>'; // End card
4937 -
4938 - return $html;
4939 -}
4940 -
4941 -
4942 -/**
4943 - * Render failed pages list
4944 - */
4945 -private function mxchat_render_failed_pages_list($failed_pages_list) {
4946 - // Validate that $failed_pages_list is an array and not empty
4947 - if (!is_array($failed_pages_list) || empty($failed_pages_list)) {
4948 - return '';
4949 - }
4950 -
4951 - $html = '<div class="mxchat-error-notice">';
4952 - $html .= '<div class="mxchat-failed-pages-container">';
4953 - $html .= '<h5>' . sprintf(esc_html__('Failed Pages (%d)', 'mxchat'), count($failed_pages_list)) . '</h5>';
4954 - $html .= '<details>';
4955 - $html .= '<summary>' . esc_html__('Show Failed Pages', 'mxchat') . '</summary>';
4956 - $html .= '<div class="mxchat-failed-pages-list">';
4957 -
4958 - // Create table for failed pages
4959 - $html .= '<table class="widefat striped">';
4960 - $html .= '<thead><tr>';
4961 - $html .= '<th>' . esc_html__('Page', 'mxchat') . '</th>';
4962 - $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
4963 - $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
4964 - $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
4965 - $html .= '</tr></thead><tbody>';
4966 -
4967 - // Sort failed pages by most recent
4968 - $sorted_failed_pages = $failed_pages_list;
4969 - usort($sorted_failed_pages, function($a, $b) {
4970 - return ($b['time'] ?? 0) - ($a['time'] ?? 0);
4971 - });
4972 -
4973 - foreach ($sorted_failed_pages as $item) {
4974 - // Ensure $item is an array before accessing its elements
4975 - if (!is_array($item)) {
4976 - continue;
4977 - }
4978 -
4979 - $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
4980 - $html .= '<tr>';
4981 - $html .= '<td>' . esc_html__('Page', 'mxchat') . ' ' . esc_html($item['page'] ?? 'Unknown') . '</td>';
4982 - $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
4983 - $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
4984 - $html .= '<td>' . esc_html($time_ago) . '</td>';
4985 - $html .= '</tr>';
4986 - }
4987 -
4988 - $html .= '</tbody></table>';
4989 - $html .= '</div></details></div></div>';
4990 -
4991 - return $html;
4992 -}
4993 -
4994 -/**
4995 - * Render failed URLs list
4996 - */
4997 -private function mxchat_render_failed_urls_list($failed_urls_list) {
4998 - // Validate that $failed_urls_list is an array and not empty
4999 - if (!is_array($failed_urls_list) || empty($failed_urls_list)) {
5000 - return '';
5001 - }
5002 -
5003 - $html = '<div class="mxchat-failed-urls-container">';
5004 - $html .= '<h5>' . sprintf(esc_html__('Failed URLs (%d)', 'mxchat'), count($failed_urls_list)) . '</h5>';
5005 - $html .= '<details>';
5006 - $html .= '<summary>' . esc_html__('Show Failed URLs', 'mxchat') . '</summary>';
5007 - $html .= '<div class="mxchat-failed-urls-list">';
5008 -
5009 - // Create table for failed URLs
5010 - $html .= '<table class="widefat striped">';
5011 - $html .= '<thead><tr>';
5012 - $html .= '<th>' . esc_html__('URL', 'mxchat') . '</th>';
5013 - $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
5014 - $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
5015 - $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
5016 - $html .= '</tr></thead><tbody>';
5017 -
5018 - // Sort failed URLs by most recent
5019 - $sorted_failed_urls = $failed_urls_list;
5020 - usort($sorted_failed_urls, function($a, $b) {
5021 - return ($b['time'] ?? 0) - ($a['time'] ?? 0);
5022 - });
5023 -
5024 - // Show up to 50 failed URLs
5025 - $display_urls = array_slice($sorted_failed_urls, 0, 50);
5026 -
5027 - foreach ($display_urls as $item) {
5028 - // Ensure $item is an array before accessing its elements
5029 - if (!is_array($item)) {
5030 - continue;
5031 - }
5032 -
5033 - $url = $item['url'] ?? '';
5034 - $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
5035 -
5036 - // Truncate URL for display
5037 - $display_url = strlen($url) > 50 ? substr($url, 0, 47) . '...' : $url;
5038 -
5039 - $html .= '<tr>';
5040 - $html .= '<td style="word-break: break-all;">';
5041 - if (!empty($url)) {
5042 - $html .= '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($display_url) . '</a>';
5043 - } else {
5044 - $html .= esc_html__('Unknown URL', 'mxchat');
5045 - }
5046 - $html .= '</td>';
5047 - $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
5048 - $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
5049 - $html .= '<td>' . esc_html($time_ago) . '</td>';
5050 - $html .= '</tr>';
5051 - }
5052 -
5053 - $html .= '</tbody></table>';
5054 -
5055 - if (count($failed_urls_list) > 50) {
5056 - $html .= '<div class="mxchat-failed-urls-more">+ ' .
5057 - (count($failed_urls_list) - 50) .
5058 - ' ' . esc_html__('more failed URLs not shown', 'mxchat') . '</div>';
5059 - }
5060 -
5061 - $html .= '</div></details></div>';
5062 -
5063 - return $html;
5064 -}
5065 -
5066 -/**
5067 - * Get all ACF fields for a specific post, excluding any fields the user has disabled
5068 - */
5069 -public function mxchat_get_acf_fields_for_post($post_id) {
5070 - if (!function_exists('get_fields')) {
5071 - return array();
5072 - }
5073 -
5074 - $fields = get_fields($post_id);
5075 - if (!$fields || !is_array($fields)) {
5076 - return array();
5077 - }
5078 -
5079 - // Get excluded fields from settings
5080 - $excluded_fields = get_option('mxchat_acf_excluded_fields', array());
5081 - if (!empty($excluded_fields) && is_array($excluded_fields)) {
5082 - foreach ($excluded_fields as $excluded_field) {
5083 - if (isset($fields[$excluded_field])) {
5084 - unset($fields[$excluded_field]);
5085 - }
5086 - }
5087 - }
5088 -
5089 - return $fields;
5090 -}
5091 -
5092 -/**
5093 - * Get all registered ACF field groups and their fields for the settings UI
5094 - */
5095 -public function mxchat_get_all_acf_fields() {
5096 - if (!function_exists('acf_get_field_groups') || !function_exists('acf_get_fields')) {
5097 - return array();
5098 - }
5099 -
5100 - $all_fields = array();
5101 - $field_groups = acf_get_field_groups();
5102 -
5103 - if (!empty($field_groups)) {
5104 - foreach ($field_groups as $group) {
5105 - $group_fields = acf_get_fields($group['key']);
5106 - if (!empty($group_fields)) {
5107 - $all_fields[$group['title']] = array();
5108 - foreach ($group_fields as $field) {
5109 - $all_fields[$group['title']][] = array(
5110 - 'name' => $field['name'],
5111 - 'label' => $field['label'],
5112 - 'type' => $field['type']
5113 - );
5114 - }
5115 - }
5116 - }
5117 - }
5118 -
5119 - return $all_fields;
5120 -}
5121 -
5122 -/**
5123 - * Get whitelisted custom post meta for a given post
5124 - * This allows non-ACF meta fields (like OptionTree, theme meta boxes) to be included in embeddings
5125 - */
5126 -public function mxchat_get_whitelisted_post_meta($post_id) {
5127 - $whitelist = get_option('mxchat_custom_meta_whitelist', '');
5128 -
5129 - if (empty($whitelist)) {
5130 - return array();
5131 - }
5132 -
5133 - // Parse the whitelist - one meta key per line
5134 - $meta_keys = array_filter(array_map('trim', explode("\n", $whitelist)));
5135 -
5136 - if (empty($meta_keys)) {
5137 - return array();
5138 - }
5139 -
5140 - $result = array();
5141 -
5142 - foreach ($meta_keys as $key) {
5143 - // Skip empty keys
5144 - if (empty($key)) {
5145 - continue;
5146 - }
5147 -
5148 - $value = get_post_meta($post_id, $key, true);
5149 -
5150 - // Only include non-empty string values
5151 - if (!empty($value) && is_string($value)) {
5152 - $result[$key] = $value;
5153 - } elseif (!empty($value) && is_array($value)) {
5154 - // Handle array values by joining them
5155 - $flat_value = $this->mxchat_flatten_meta_array($value);
5156 - if (!empty($flat_value)) {
5157 - $result[$key] = $flat_value;
5158 - }
5159 - }
5160 - }
5161 -
5162 - return $result;
5163 -}
5164 -
5165 -/**
5166 - * Flatten array meta values into a readable string
5167 - */
5168 -private function mxchat_flatten_meta_array($array, $depth = 0) {
5169 - if ($depth > 3) {
5170 - return ''; // Prevent infinite recursion
5171 - }
5172 -
5173 - $parts = array();
5174 -
5175 - foreach ($array as $key => $value) {
5176 - if (is_string($value) && !empty($value)) {
5177 - $parts[] = $value;
5178 - } elseif (is_array($value)) {
5179 - $nested = $this->mxchat_flatten_meta_array($value, $depth + 1);
5180 - if (!empty($nested)) {
5181 - $parts[] = $nested;
5182 - }
5183 - }
5184 - }
5185 -
5186 - return implode(', ', $parts);
5187 -}
5188 -
5189 -/**
5190 - * Format ACF field values for content extraction
5191 - */
5192 -public function mxchat_format_acf_field_value($value, $field_name = '', $post_id = 0) {
5193 - if (empty($value)) {
5194 - return '';
5195 - }
5196 -
5197 - // Handle WP_Post objects first (THIS IS THE KEY FIX)
5198 - if ($value instanceof WP_Post) {
5199 - return $value->post_title ?: '';
5200 - }
5201 -
5202 - // Handle other WP objects
5203 - if (is_object($value)) {
5204 - if (isset($value->post_title)) {
5205 - return $value->post_title;
5206 - } elseif (isset($value->display_name)) {
5207 - return $value->display_name;
5208 - } elseif (isset($value->name)) {
5209 - return $value->name;
5210 - } elseif (method_exists($value, '__toString')) {
5211 - try {
5212 - return (string) $value;
5213 - } catch (Exception $e) {
5214 - return '';
5215 - }
5216 - }
5217 - // For any other objects, return empty string
5218 - return '';
5219 - }
5220 -
5221 - // Handle different ACF field types
5222 - if (is_array($value)) {
5223 - // Check if it's an image/file field
5224 - if (isset($value['url'])) {
5225 - // Image field - return alt text, title, or caption
5226 - if (!empty($value['alt'])) {
5227 - return $value['alt'];
5228 - } elseif (!empty($value['title'])) {
5229 - return $value['title'];
5230 - } elseif (!empty($value['caption'])) {
5231 - return $value['caption'];
5232 - } else {
5233 - return ''; // Don't include just the URL
5234 - }
5235 - }
5236 -
5237 - // Check if it's a post object or relationship field
5238 - if (isset($value['post_title'])) {
5239 - return $value['post_title'];
5240 - }
5241 -
5242 - // Check if it's a user field
5243 - if (isset($value['display_name'])) {
5244 - return $value['display_name'];
5245 - }
5246 -
5247 - // Check if it's a taxonomy term
5248 - if (isset($value['name']) && isset($value['taxonomy'])) {
5249 - return $value['name'];
5250 - }
5251 -
5252 - // Check if it's a select field with label
5253 - if (isset($value['label'])) {
5254 - return $value['label'];
5255 - }
5256 -
5257 - // Check for repeater field or flexible content
5258 - if (is_numeric(key($value))) {
5259 - $sub_values = array();
5260 - foreach ($value as $sub_item) {
5261 - if (is_array($sub_item)) {
5262 - // For repeater/flexible content, extract text values
5263 - $sub_text = $this->mxchat_extract_text_from_acf_array($sub_item);
5264 - if (!empty($sub_text)) {
5265 - $sub_values[] = $sub_text;
5266 - }
5267 - } elseif ($sub_item instanceof WP_Post) {
5268 - // Handle WP_Post objects in arrays
5269 - $sub_values[] = $sub_item->post_title ?: '';
5270 - } else {
5271 - $sub_values[] = (string) $sub_item;
5272 - }
5273 - }
5274 - return implode(', ', array_filter($sub_values));
5275 - }
5276 -
5277 - // For other arrays, try to extract meaningful text
5278 - $text_values = array();
5279 - foreach ($value as $key => $val) {
5280 - if (is_string($val) && !empty(trim($val))) {
5281 - $text_values[] = trim($val);
5282 - } elseif ($val instanceof WP_Post) {
5283 - // Handle WP_Post objects in associative arrays
5284 - $text_values[] = $val->post_title ?: '';
5285 - } elseif (is_array($val) && isset($val['post_title'])) {
5286 - $text_values[] = $val['post_title'];
5287 - } elseif (is_array($val) && isset($val['name'])) {
5288 - $text_values[] = $val['name'];
5289 - }
5290 - }
5291 -
5292 - return implode(', ', array_filter($text_values));
5293 - }
5294 -
5295 - // Handle boolean values
5296 - if (is_bool($value)) {
5297 - return $value ? 'Yes' : 'No';
5298 - }
5299 -
5300 - // Handle numeric values
5301 - if (is_numeric($value)) {
5302 - return (string) $value;
5303 - }
5304 -
5305 - // Handle string values
5306 - if (is_string($value)) {
5307 - return trim($value);
5308 - }
5309 -
5310 - // For anything else that we can't handle, return empty string
5311 - // This prevents the "Object could not be converted to string" error
5312 - return '';
5313 -}
5314 -
5315 -/**
5316 - * Extract text from complex ACF array structures
5317 - */
5318 -private function mxchat_extract_text_from_acf_array($array) {
5319 - if (!is_array($array)) {
5320 - return '';
5321 - }
5322 -
5323 - $text_parts = array();
5324 -
5325 - foreach ($array as $key => $value) {
5326 - if (is_string($value) && !empty(trim($value))) {
5327 - // Skip keys that are likely to be IDs or technical values
5328 - if (!is_numeric($value) || strlen($value) > 10) {
5329 - $text_parts[] = trim($value);
5330 - }
5331 - } elseif ($value instanceof WP_Post) {
5332 - // Handle WP_Post objects
5333 - $text_parts[] = $value->post_title ?: '';
5334 - } elseif (is_array($value)) {
5335 - if (isset($value['post_title'])) {
5336 - $text_parts[] = $value['post_title'];
5337 - } elseif (isset($value['name'])) {
5338 - $text_parts[] = $value['name'];
5339 - } elseif (isset($value['label'])) {
5340 - $text_parts[] = $value['label'];
5341 - }
5342 - } elseif (is_object($value)) {
5343 - // Handle other objects safely
5344 - if (isset($value->post_title)) {
5345 - $text_parts[] = $value->post_title;
5346 - } elseif (isset($value->name)) {
5347 - $text_parts[] = $value->name;
5348 - } elseif (isset($value->display_name)) {
5349 - $text_parts[] = $value->display_name;
5350 - }
5351 - }
5352 - }
5353 -
5354 - return implode(', ', array_filter($text_parts));
5355 -}
5356 -
5357 -/**
5358 - * Walk an ACF field value tree and collect attachment IDs for any value that
5359 - * resolves to a PDF in the WordPress media library. Handles the three shapes
5360 - * ACF returns for File/Image/URL fields (array with ID+url, integer attachment ID,
5361 - * plain URL string), and recurses through repeater/group/flexible content.
5362 - *
5363 - * @param mixed $value The ACF field value (any depth)
5364 - * @param array $out Accumulator (passed by reference) for attachment IDs
5365 - * @param int $depth Recursion guard
5366 - */
5367 -private function mxchat_collect_pdf_attachment_ids_from_acf_value($value, &$out, $depth = 0) {
5368 - if ($depth > 6) {
5369 - return; // prevent runaway recursion on circular/very-deep structures
5370 - }
5371 -
5372 - if (empty($value)) {
5373 - return;
5374 - }
5375 -
5376 - // Array shapes: ACF File/Image return value=array; repeaters/groups are arrays of arrays
5377 - if (is_array($value)) {
5378 - // Direct File/Image-style array (has 'url' and usually 'ID' + 'mime_type')
5379 - $looks_like_attachment = isset($value['url']) || isset($value['ID']) || isset($value['id']);
5380 - if ($looks_like_attachment) {
5381 - $att_id = 0;
5382 - if (!empty($value['ID']) && is_numeric($value['ID'])) {
5383 - $att_id = (int) $value['ID'];
5384 - } elseif (!empty($value['id']) && is_numeric($value['id'])) {
5385 - $att_id = (int) $value['id'];
5386 - } elseif (!empty($value['url']) && is_string($value['url'])) {
5387 - $att_id = (int) attachment_url_to_postid($value['url']);
5388 - }
5389 -
5390 - $is_pdf = false;
5391 - if (!empty($value['mime_type']) && $value['mime_type'] === 'application/pdf') {
5392 - $is_pdf = true;
5393 - } elseif (!empty($value['subtype']) && strtolower((string) $value['subtype']) === 'pdf') {
5394 - $is_pdf = true;
5395 - } elseif (!empty($value['url']) && is_string($value['url']) && $this->mxchat_url_looks_like_pdf($value['url'])) {
5396 - $is_pdf = true;
5397 - } elseif ($att_id && get_post_mime_type($att_id) === 'application/pdf') {
5398 - $is_pdf = true;
5399 - }
5400 -
5401 - if ($is_pdf && $att_id && get_post_mime_type($att_id) === 'application/pdf') {
5402 - $out[] = $att_id;
5403 - }
5404 - // An array node that represents one attachment doesn't contain other
5405 - // attachments inside it — done with this branch.
5406 - return;
5407 - }
5408 -
5409 - // Recurse: repeater rows, flexible-content layouts, groups, etc.
5410 - foreach ($value as $sub) {
5411 - $this->mxchat_collect_pdf_attachment_ids_from_acf_value($sub, $out, $depth + 1);
5412 - }
5413 - return;
5414 - }
5415 -
5416 - // Plain numeric attachment ID (ACF File field set to "Return: ID")
5417 - if (is_numeric($value)) {
5418 - $att_id = (int) $value;
5419 - if ($att_id > 0 && get_post_mime_type($att_id) === 'application/pdf') {
5420 - $out[] = $att_id;
5421 - }
5422 - return;
5423 - }
5424 -
5425 - // Plain string — URL pointing at a PDF (ACF File field set to "Return: URL", or a custom URL/text field)
5426 - if (is_string($value)) {
5427 - $trimmed = trim($value);
5428 - if ($trimmed !== '' && $this->mxchat_url_looks_like_pdf($trimmed)) {
5429 - $att_id = (int) attachment_url_to_postid($trimmed);
5430 - if ($att_id > 0 && get_post_mime_type($att_id) === 'application/pdf') {
5431 - $out[] = $att_id;
5432 - }
5433 - }
5434 - return;
5435 - }
5436 -}
5437 -
5438 -/**
5439 - * Heuristic: does this URL/string look like a PDF reference?
5440 - * Tolerates query strings and fragments (#page=2).
5441 - */
5442 -private function mxchat_url_looks_like_pdf($url) {
5443 - if (!is_string($url) || $url === '') {
5444 - return false;
5445 - }
5446 - // Strip query + fragment before checking extension
5447 - $path = preg_replace('/[?#].*$/', '', $url);
5448 - return (bool) preg_match('/\.pdf$/i', $path);
5449 -}
5450 -
5451 -/**
5452 - * Extract text from a PDF attachment by ID using the bundled Smalot parser.
5453 - * Reads the file directly from disk via get_attached_file (no HTTP fetch).
5454 - * Result is cached on the attachment as post_meta keyed by file mtime so we
5455 - * only parse the same PDF once unless the file changes on disk.
5456 - *
5457 - * @param int $attachment_id
5458 - * @return string Extracted plain text, or '' on failure.
5459 - */
5460 -private function mxchat_extract_pdf_text_by_attachment_id($attachment_id) {
5461 - $attachment_id = (int) $attachment_id;
5462 - if ($attachment_id <= 0) {
5463 - return '';
5464 - }
5465 - if (get_post_mime_type($attachment_id) !== 'application/pdf') {
5466 - return '';
5467 - }
5468 -
5469 - $pdf_path = get_attached_file($attachment_id);
5470 - if (empty($pdf_path) || !file_exists($pdf_path) || !is_readable($pdf_path)) {
5471 - return '';
5472 - }
5473 -
5474 - // Raw-file size cap. Parsing very large PDFs can OOM the request; skip with a log entry
5475 - // and let the rest of the ACF content land in the KB. Filterable for users who need it bigger.
5476 - $default_max_bytes = 25 * 1024 * 1024;
5477 - $max_bytes = (int) apply_filters('mxchat_acf_pdf_max_bytes', $default_max_bytes, $attachment_id, $pdf_path);
5478 - if ($max_bytes > 0) {
5479 - $file_size = @filesize($pdf_path);
5480 - if ($file_size !== false && $file_size > $max_bytes) {
5481 - error_log(sprintf(
5482 - '[mxchat] ACF PDF skipped (over size cap): attachment %d "%s" %d bytes > cap %d',
5483 - $attachment_id,
5484 - basename($pdf_path),
5485 - $file_size,
5486 - $max_bytes
5487 - ));
5488 - return '';
5489 - }
5490 - }
5491 -
5492 - $mtime = @filemtime($pdf_path);
5493 - $cache_meta_key = '_mxchat_acf_pdf_text_v1';
5494 - $cached = get_post_meta($attachment_id, $cache_meta_key, true);
5495 - if (is_array($cached) && isset($cached['mtime'], $cached['text']) && (int) $cached['mtime'] === (int) $mtime) {
5496 - return (string) $cached['text'];
5497 - }
5498 -
5499 - $text = '';
5500 - try {
5501 - if (function_exists('mxchat_load_pdf_parser')) {
5502 - mxchat_load_pdf_parser();
5503 - }
5504 - if (!class_exists('\\Smalot\\PdfParser\\Parser')) {
5505 - return '';
5506 - }
5507 - $parser = new \Smalot\PdfParser\Parser();
5508 - $pdf = $parser->parseFile($pdf_path);
5509 - $pages = $pdf->getPages();
5510 - $page_texts = array();
5511 - foreach ($pages as $page) {
5512 - $page_text = '';
5513 - try {
5514 - $page_text = $page->getText();
5515 - } catch (\Exception $e) {
5516 - $page_text = '';
5517 - }
5518 - if (!empty($page_text)) {
5519 - $page_texts[] = $page_text;
5520 - }
5521 - }
5522 - $text = trim(implode("\n\n", $page_texts));
5523 - } catch (\Exception $e) {
5524 - error_log('[mxchat] ACF PDF extraction failed for attachment ' . $attachment_id . ': ' . $e->getMessage());
5525 - return '';
5526 - } catch (\Throwable $e) {
5527 - error_log('[mxchat] ACF PDF extraction error for attachment ' . $attachment_id . ': ' . $e->getMessage());
5528 - return '';
5529 - }
5530 -
5531 - // Cap per-PDF text to avoid blowing up the embedding payload on enormous PDFs.
5532 - // The chunker downstream will still split this into multiple vectors.
5533 - $max_len = (int) apply_filters('mxchat_acf_pdf_text_max_length', 50000);
5534 - if ($max_len > 0 && strlen($text) > $max_len) {
5535 - $text = substr($text, 0, $max_len);
5536 - }
5537 -
5538 - update_post_meta($attachment_id, $cache_meta_key, array(
5539 - 'mtime' => (int) $mtime,
5540 - 'text' => $text,
5541 - ));
5542 -
5543 - return $text;
5544 -}
5545 -
5546 -/**
5547 - * Handle ACF save - fires after ACF fields are saved
5548 - * This ensures ACF field data is available when syncing to knowledge base
5549 - */
5550 -public function mxchat_handle_acf_save($post_id) {
5551 - // Skip if not a valid post
5552 - if (!$post_id || $post_id === 'options') {
5553 - return;
5554 - }
5555 -
5556 - // Skip autosaves and revisions
5557 - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) {
5558 - return;
5559 - }
5560 -
5561 - $post = get_post($post_id);
5562 - if (!$post) {
5563 - return;
5564 - }
5565 -
5566 - $post_type = $post->post_type;
5567 -
5568 - // Check if sync is enabled for this post type
5569 - $should_sync = false;
5570 -
5571 - if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
5572 - $should_sync = true;
5573 - } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
5574 - $should_sync = true;
5575 - } else if ($post_type === 'product' && class_exists('WooCommerce')) {
5576 - // WooCommerce products - check if WooCommerce integration is enabled
5577 - $options = get_option('mxchat_options', array());
5578 - if (isset($options['enable_woocommerce_integration']) &&
5579 - ($options['enable_woocommerce_integration'] === '1' || $options['enable_woocommerce_integration'] === 'on')) {
5580 - $should_sync = true;
5581 - }
5582 - } else {
5583 - // Check custom post types
5584 - $option_name = 'mxchat_auto_sync_' . $post_type;
5585 - if (get_option($option_name) === '1') {
5586 - $should_sync = true;
5587 - }
5588 - }
5589 -
5590 - if (!$should_sync) {
5591 - return;
5592 - }
5593 -
5594 - // Only process published posts
5595 - if ($post->post_status !== 'publish') {
5596 - return;
5597 - }
5598 -
5599 - // Check if this post has any ACF fields - if not, no need to re-sync
5600 - $acf_fields = $this->mxchat_get_acf_fields_for_post($post_id);
5601 - if (empty($acf_fields)) {
5602 - return;
5603 - }
5604 -
5605 - // Use a transient to prevent duplicate processing (post_updated may have already run)
5606 - $transient_key = 'mxchat_acf_synced_' . $post_id;
5607 - if (get_transient($transient_key)) {
5608 - return;
5609 - }
5610 - set_transient($transient_key, true, 60); // Prevent re-processing for 60 seconds
5611 -
5612 - // Re-run the sync with ACF data now available
5613 - // We pass $update=true since this is effectively an update with ACF data
5614 - $this->mxchat_handle_post_update($post_id, $post, true);
5615 -}
5616 -
5617 -public function mxchat_handle_post_update($post_id, $post, $update) {
5618 - // Basic validation checks
5619 - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) {
5620 - return;
5621 - }
5622 -
5623 - $post_type = $post->post_type;
5624 -
5625 - // Check if sync is enabled for this post type
5626 - $should_sync = false;
5627 -
5628 - // Check built-in post types first
5629 - if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
5630 - $should_sync = true;
5631 - } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
5632 - $should_sync = true;
5633 - } else {
5634 - // Check custom post types
5635 - $option_name = 'mxchat_auto_sync_' . $post_type;
5636 - if (get_option($option_name) === '1') {
5637 - $should_sync = true;
5638 - }
5639 - }
5640 -
5641 - if (!$should_sync) {
5642 - return;
5643 - }
5644 -
5645 - // Check if we have stored the previous status and URL in our transients
5646 - $previous_status_key = 'mxchat_prev_status_' . $post_id;
5647 - $previous_status = get_transient($previous_status_key);
5648 -
5649 - $previous_url_key = 'mxchat_prev_url_' . $post_id;
5650 - $previous_url = get_transient($previous_url_key);
5651 -
5652 - // If the post was previously published but is now not published, remove from knowledge base
5653 - if ($previous_status === 'publish' && $post->post_status !== 'publish') {
5654 - // Use the stored URL from when it was published, or fall back to current permalink
5655 - $source_url = $previous_url ?: get_permalink($post_id);
5656 -
5657 - if ($source_url) {
5658 - // Chunk-aware deletion (routes to Pinecone or WP DB and removes base + all chunks)
5659 - MxChat_Utils::delete_chunks_for_url($source_url, 'default');
5660 - }
5661 -
5662 - // Clean up the transients and exit early
5663 - delete_transient($previous_status_key);
5664 - delete_transient($previous_url_key);
5665 - return;
5666 - }
5667 -
5668 - // Slug/permalink rename while still published: delete the old vectors before upserting new ones.
5669 - // Without this, md5(old_url) vectors (base + chunks) would be orphaned under the stale URL.
5670 - if ($post->post_status === 'publish' && !empty($previous_url)) {
5671 - $current_url = get_permalink($post_id);
5672 - if ($current_url && $current_url !== $previous_url) {
5673 - MxChat_Utils::delete_chunks_for_url($previous_url, 'default');
5674 - }
5675 - }
5676 -
5677 - // Store the current status for next time (if this is an update)
5678 - if ($update) {
5679 - set_transient($previous_status_key, $post->post_status, DAY_IN_SECONDS);
5680 -
5681 - // If the post is currently published, also store its URL
5682 - if ($post->post_status === 'publish') {
5683 - $current_url = get_permalink($post_id);
5684 - set_transient($previous_url_key, $current_url, DAY_IN_SECONDS);
5685 - }
5686 - }
5687 -
5688 - // Only process currently published content for adding/updating
5689 - if ($post->post_status === 'publish') {
5690 - // Get the source URL
5691 - $source_url = get_permalink($post_id);
5692 -
5693 - // Get content with proper formatting (matching ajax_mxchat_process_selected_content)
5694 - $title = get_the_title($post_id);
5695 - $content = get_post_field('post_content', $post_id);
5696 - $excerpt = get_post_field('post_excerpt', $post_id);
5697 -
5698 - // Remove shortcode tags but preserve content inside them
5699 - $content = $this->strip_shortcode_tags_preserve_content($content);
5700 - $excerpt = $this->strip_shortcode_tags_preserve_content($excerpt);
5701 -
5702 - // Strip tags but preserve structure (don't use 'the_content' filter as it may re-add shortcodes)
5703 - $content = wp_strip_all_tags($content);
5704 -
5705 - // Combine title, short description (if exists), and content
5706 - $final_content = $title . "\n\n";
5707 -
5708 - // Add short description if it exists (WooCommerce products use post_excerpt for short description)
5709 - if (!empty($excerpt)) {
5710 - $final_content .= "Short Description: " . wp_strip_all_tags($excerpt) . "\n\n";
5711 - }
5712 -
5713 - $final_content .= $content;
5714 -
5715 - // For WooCommerce products, include pricing and product details
5716 - if ($post_type === 'product' && class_exists('WooCommerce')) {
5717 - $product = wc_get_product($post_id);
5718 -
5719 - if ($product) {
5720 - // Get pricing information
5721 - $regular_price = $product->get_regular_price();
5722 - $sale_price = $product->get_sale_price();
5723 - $price = $product->get_price();
5724 - $sku = $product->get_sku();
5725 -
5726 - // Get currency symbol
5727 - $currency_symbol = get_woocommerce_currency_symbol();
5728 -
5729 - // Add pricing information
5730 - $final_content .= "\n";
5731 - if (!empty($regular_price)) {
5732 - $final_content .= "Price: " . $currency_symbol . $regular_price . "\n";
5733 - } elseif (!empty($price)) {
5734 - $final_content .= "Price: " . $currency_symbol . $price . "\n";
5735 - }
5736 -
5737 - if (!empty($sale_price) && $sale_price !== $regular_price) {
5738 - $final_content .= "Sale Price: " . $currency_symbol . $sale_price . "\n";
5739 - }
5740 -
5741 - // Handle variable products - show price range
5742 - if ($product->is_type('variable')) {
5743 - $min_price = $product->get_variation_price('min');
5744 - $max_price = $product->get_variation_price('max');
5745 - if ($min_price !== $max_price) {
5746 - $final_content .= "Price Range: " . $currency_symbol . $min_price . " - " . $currency_symbol . $max_price . "\n";
5747 - }
5748 - }
5749 -
5750 - if (!empty($sku)) {
5751 - $final_content .= "SKU: " . $sku . "\n";
5752 - }
5753 -
5754 - // Get product categories
5755 - $categories = wp_get_post_terms($post_id, 'product_cat', array('fields' => 'names'));
5756 - if (!empty($categories) && !is_wp_error($categories)) {
5757 - $final_content .= "Categories: " . implode(', ', $categories) . "\n";
5758 - }
5759 - }
5760 - }
5761 -
5762 - // For custom post types like job_listing, include additional fields
5763 - if ($post_type === 'job_listing') {
5764 - // Add job-specific meta if available
5765 - $job_location = get_post_meta($post_id, '_job_location', true);
5766 - if (!empty($job_location)) {
5767 - $final_content .= "\n\nLocation: " . $job_location;
5768 - }
5769 -
5770 - // Get job type terms
5771 - $job_types = get_the_terms($post_id, 'job_listing_type');
5772 - if (!empty($job_types) && !is_wp_error($job_types)) {
5773 - $types = array();
5774 - foreach ($job_types as $type) {
5775 - $types[] = $type->name;
5776 - }
5777 - $final_content .= "\n\nJob Type: " . implode(', ', $types);
5778 - }
5779 -
5780 - // Get company name if available
5781 - $company_name = get_post_meta($post_id, '_company_name', true);
5782 - if (!empty($company_name)) {
5783 - $final_content .= "\n\nCompany: " . $company_name;
5784 - }
5785 - }
5786 -
5787 - // ADD ACF FIELDS SUPPORT (matches ajax_mxchat_process_selected_content behavior)
5788 - $acf_fields = $this->mxchat_get_acf_fields_for_post($post_id);
5789 - if (!empty($acf_fields)) {
5790 - $acf_content_parts = array();
5791 - $pdf_attachment_ids = array();
5792 -
5793 - foreach ($acf_fields as $field_name => $field_value) {
5794 - $formatted_value = $this->mxchat_format_acf_field_value($field_value, $field_name, $post_id);
5795 - if (!empty($formatted_value)) {
5796 - // Convert field name to readable label
5797 - $field_label = ucwords(str_replace(['_', '-'], ' ', $field_name));
5798 - $acf_content_parts[] = $field_label . ": " . $formatted_value;
5799 - }
5800 -
5801 - $this->mxchat_collect_pdf_attachment_ids_from_acf_value($field_value, $pdf_attachment_ids);
5802 - }
5803 -
5804 - if (!empty($acf_content_parts)) {
5805 - $final_content .= "\n\n" . implode("\n", $acf_content_parts);
5806 - }
5807 -
5808 - // Gate the auto-sync PDF-extraction loop behind an opt-in option.
5809 - // Mirrors the per-batch checkbox the manual content selector has; the
5810 - // 25 MB size cap lives in the shared extractor so it applies in both
5811 - // paths regardless. Default OFF — re-parsing every ACF PDF on every
5812 - // editor save is expensive and most sites don't want it.
5813 - $autosync_extract_acf_pdfs = get_option('mxchat_auto_sync_acf_pdfs', '0') === '1';
5814 - if ($autosync_extract_acf_pdfs && !empty($pdf_attachment_ids)) {
5815 - $pdf_attachment_ids = array_unique(array_filter(array_map('intval', $pdf_attachment_ids)));
5816 - $pdf_sections = array();
5817 - foreach ($pdf_attachment_ids as $att_id) {
5818 - $pdf_text = $this->mxchat_extract_pdf_text_by_attachment_id($att_id);
5819 - if (!empty($pdf_text)) {
5820 - $pdf_title = get_the_title($att_id);
5821 - $pdf_url = wp_get_attachment_url($att_id);
5822 - $header = 'PDF Attachment';
5823 - if (!empty($pdf_title)) {
5824 - $header .= ': ' . $pdf_title;
5825 - }
5826 - if (!empty($pdf_url)) {
5827 - $header .= ' (' . $pdf_url . ')';
5828 - }
5829 - $pdf_sections[] = $header . "\n" . $pdf_text;
5830 - }
5831 - }
5832 - if (!empty($pdf_sections)) {
5833 - $final_content .= "\n\nAttached PDFs:\n" . implode("\n\n", $pdf_sections);
5834 - }
5835 - }
5836 - }
5837 -
5838 - // ADD CUSTOM POST META SUPPORT (whitelisted non-ACF meta fields)
5839 - $custom_meta = $this->mxchat_get_whitelisted_post_meta($post_id);
5840 - if (!empty($custom_meta)) {
5841 - $meta_content_parts = array();
5842 -
5843 - foreach ($custom_meta as $meta_key => $meta_value) {
5844 - // Convert meta key to readable label
5845 - $meta_label = ucwords(str_replace(array('_', '-'), ' ', $meta_key));
5846 - $meta_content_parts[] = $meta_label . ": " . $meta_value;
5847 - }
5848 -
5849 - if (!empty($meta_content_parts)) {
5850 - $final_content .= "\n\n" . implode("\n", $meta_content_parts);
5851 - }
5852 - }
5853 -
5854 - // Get API key with proper model detection
5855 - $options = get_option('mxchat_options');
5856 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
5857 -
5858 - if (strpos($selected_model, 'voyage') === 0) {
5859 - $api_key = $options['voyage_api_key'] ?? '';
5860 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
5861 - $api_key = $options['gemini_api_key'] ?? '';
5862 - } else {
5863 - $api_key = $options['api_key'] ?? '';
5864 - }
5865 -
5866 - if (empty($api_key)) {
5867 - return;
5868 - }
5869 -
5870 - // Use the centralized utility function for storage
5871 - $result = MxChat_Utils::submit_content_to_db(
5872 - $final_content,
5873 - $source_url,
5874 - $api_key,
5875 - md5($source_url) // Vector ID for Pinecone
5876 - );
5877 -
5878 - // After successful storage, apply role restriction based on tags
5879 - if (!is_wp_error($result)) {
5880 - $this->apply_role_restriction_to_post($post_id, $source_url);
5881 - }
5882 - }
5883 -
5884 - // Clean up the stored previous status if not used above
5885 - if ($previous_status !== 'publish' || $post->post_status === 'publish') {
5886 - delete_transient($previous_status_key);
5887 - delete_transient($previous_url_key);
5888 - }
5889 -}
5890 -
5891 -/**
5892 - * Store the post status and URL before update to detect status transitions
5893 - * This runs before the post is actually updated in the database
5894 - */
5895 -public function mxchat_store_pre_update_status($post_id, $data) {
5896 - // Get the current post from database (before update)
5897 - $current_post = get_post($post_id);
5898 -
5899 - if ($current_post) {
5900 - // Store the current status temporarily
5901 - $status_key = 'mxchat_prev_status_' . $post_id;
5902 - set_transient($status_key, $current_post->post_status, HOUR_IN_SECONDS);
5903 -
5904 - // If the post is currently published, also store its URL
5905 - if ($current_post->post_status === 'publish') {
5906 - $url_key = 'mxchat_prev_url_' . $post_id;
5907 - $current_url = get_permalink($post_id);
5908 - set_transient($url_key, $current_url, HOUR_IN_SECONDS);
5909 - }
5910 - }
5911 -}
5912 -
5913 -public function mxchat_handle_post_delete($post_id) {
5914 - // Get post data before it's deleted
5915 - $post = get_post($post_id);
5916 -
5917 - // Basic validation
5918 - if (!$post || wp_is_post_revision($post_id)) {
5919 - return;
5920 - }
5921 -
5922 - $post_type = $post->post_type;
5923 -
5924 - // Check if sync is enabled for this post type
5925 - $should_sync = false;
5926 -
5927 - // Check built-in post types first
5928 - if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
5929 - $should_sync = true;
5930 - } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
5931 - $should_sync = true;
5932 - } else {
5933 - // Check custom post types
5934 - $option_name = 'mxchat_auto_sync_' . $post_type;
5935 - if (get_option($option_name) === '1') {
5936 - $should_sync = true;
5937 - }
5938 - }
5939 -
5940 - if (!$should_sync) {
5941 - return;
5942 - }
5943 -
5944 - // Resolve the pre-trash URL. wp_trash_post renames the slug with "__trashed" before firing
5945 - // this hook, so get_permalink() here would return the trashed URL and md5() would miss the
5946 - // real vector IDs stored under the original URL.
5947 - $source_url = $this->mxchat_resolve_pre_trash_url($post_id);
5948 - if (!$source_url) {
5949 - //error_log('MXChat: Failed to resolve source URL for post ' . $post_id);
5950 - return;
5951 - }
5952 -
5953 - // Use chunk-aware deletion (handles both chunked and non-chunked content)
5954 - $delete_result = MxChat_Utils::delete_chunks_for_url($source_url, 'default');
5955 -
5956 - if (is_wp_error($delete_result)) {
5957 - //error_log('MXChat: Chunk-aware deletion failed for URL: ' . $source_url . ' - ' . $delete_result->get_error_message());
5958 - }
5959 -
5960 - delete_transient('mxchat_prev_url_' . $post_id);
5961 - delete_transient('mxchat_prev_status_' . $post_id);
5962 -}
5963 -
5964 -/**
5965 - * Resolve the source URL for a post being trashed/deleted.
5966 - *
5967 - * Why: wp_trash_post appends "__trashed" to the slug before the wp_trash_post action fires, so
5968 - * get_permalink() returns a URL whose md5() won't match the vector IDs stored in Pinecone or
5969 - * the source_url rows in the WP DB. Prefer the URL captured by mxchat_store_pre_update_status
5970 - * (runs on pre_post_update, before the rename); fall back to stripping the __trashed suffix.
5971 - */
5972 -private function mxchat_resolve_pre_trash_url($post_id) {
5973 - $previous_url = get_transient('mxchat_prev_url_' . $post_id);
5974 - if (!empty($previous_url)) {
5975 - return $previous_url;
5976 - }
5977 -
5978 - $current = get_permalink($post_id);
5979 - if (!$current) {
5980 - return '';
5981 - }
5982 - return preg_replace('#__trashed(/?)$#', '$1', $current);
5983 -}
5984 -
5985 -
5986 -
5987 -public function mxchat_handle_product_change($post_id, $post, $update) {
5988 - if ($post->post_type !== 'product') {
5989 - return;
5990 - }
5991 -
5992 - if ($post->post_status === 'publish') {
5993 - add_action('shutdown', function() use ($post_id) {
5994 - $product = wc_get_product($post_id);
5995 - if ($product) {
5996 - $this->mxchat_store_product_embedding($product);
5997 - }
5998 - });
5999 - }
6000 -}
6001 -
6002 -/**
6003 - * Store WooCommerce product embeddings
6004 - */
6005 -private function mxchat_store_product_embedding($product) {
6006 - if (!isset($this->options['enable_woocommerce_integration']) ||
6007 - !in_array($this->options['enable_woocommerce_integration'], ['1', 'on'])) {
6008 - return;
6009 - }
6010 -
6011 - $source_url = get_permalink($product->get_id());
6012 - $product_id = $product->get_id();
6013 -
6014 - // Build product content
6015 - $title = $product->get_name();
6016 - $description = $product->get_description();
6017 - $short_description = $product->get_short_description();
6018 - $regular_price = $product->get_regular_price();
6019 - $sale_price = $product->get_sale_price();
6020 - $price = $product->get_price();
6021 - $sku = $product->get_sku();
6022 -
6023 - // Get currency symbol
6024 - $currency_symbol = get_woocommerce_currency_symbol();
6025 -
6026 - // Format content consistently
6027 - $content = $title . "\n\n";
6028 -
6029 - if (!empty($short_description)) {
6030 - $content .= "Short Description: " . wp_strip_all_tags($short_description) . "\n\n";
6031 - }
6032 -
6033 - if (!empty($description)) {
6034 - $content .= wp_strip_all_tags($description) . "\n\n";
6035 - }
6036 -
6037 - // Add pricing information
6038 - if (!empty($regular_price)) {
6039 - $content .= "Price: " . $currency_symbol . $regular_price . "\n";
6040 - } elseif (!empty($price)) {
6041 - $content .= "Price: " . $currency_symbol . $price . "\n";
6042 - }
6043 -
6044 - if (!empty($sale_price) && $sale_price !== $regular_price) {
6045 - $content .= "Sale Price: " . $currency_symbol . $sale_price . "\n";
6046 - }
6047 -
6048 - // Handle variable products - show price range
6049 - if ($product->is_type('variable')) {
6050 - $min_price = $product->get_variation_price('min');
6051 - $max_price = $product->get_variation_price('max');
6052 - if ($min_price !== $max_price) {
6053 - $content .= "Price Range: " . $currency_symbol . $min_price . " - " . $currency_symbol . $max_price . "\n";
6054 - }
6055 - }
6056 -
6057 - if (!empty($sku)) {
6058 - $content .= "SKU: " . $sku . "\n";
6059 - }
6060 -
6061 - // Get product categories
6062 - $categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'names'));
6063 - if (!empty($categories) && !is_wp_error($categories)) {
6064 - $content .= "Categories: " . implode(', ', $categories) . "\n";
6065 - }
6066 -
6067 - // Get Custom Product Tabs (supports "Custom Product Tabs for WooCommerce" by Code Parrots)
6068 - $custom_tabs = get_post_meta($product_id, 'yikes_woo_products_tabs', true);
6069 - if (!empty($custom_tabs) && is_array($custom_tabs)) {
6070 - foreach ($custom_tabs as $tab) {
6071 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
6072 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
6073 -
6074 - if (!empty($tab_title) && !empty($tab_content)) {
6075 - $content .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
6076 - }
6077 - }
6078 - }
6079 -
6080 - // Also check for reusable/saved tabs applied to this product
6081 - $applied_saved_tabs = get_post_meta($product_id, 'yikes_woo_reusable_products_tabs_applied', true);
6082 - if (!empty($applied_saved_tabs) && is_array($applied_saved_tabs)) {
6083 - // Get the saved tabs option
6084 - $saved_tabs = get_option('yikes_woo_reusable_products_tabs', array());
6085 - if (!empty($saved_tabs) && is_array($saved_tabs)) {
6086 - foreach ($applied_saved_tabs as $saved_tab_id) {
6087 - if (isset($saved_tabs[$saved_tab_id])) {
6088 - $tab = $saved_tabs[$saved_tab_id];
6089 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
6090 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
6091 -
6092 - if (!empty($tab_title) && !empty($tab_content)) {
6093 - $content .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
6094 - }
6095 - }
6096 - }
6097 - }
6098 - }
6099 -
6100 - // Get API key with proper model detection
6101 - $options = get_option('mxchat_options');
6102 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
6103 -
6104 - if (strpos($selected_model, 'voyage') === 0) {
6105 - $api_key = $options['voyage_api_key'] ?? '';
6106 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
6107 - $api_key = $options['gemini_api_key'] ?? '';
6108 - } else {
6109 - $api_key = $options['api_key'] ?? '';
6110 - }
6111 -
6112 - if (empty($api_key)) {
6113 - //error_log('MxChat Auto-sync: No API key configured for embedding model');
6114 - return;
6115 - }
6116 -
6117 - // Use the centralized utility function for storage
6118 - $result = MxChat_Utils::submit_content_to_db(
6119 - $content,
6120 - $source_url,
6121 - $api_key,
6122 - md5($source_url) // Vector ID for Pinecone
6123 - );
6124 -
6125 - // After successful storage, apply role restriction based on tags
6126 - if (!is_wp_error($result)) {
6127 - $this->apply_role_restriction_to_post($product_id, $source_url);
6128 - }
6129 -
6130 - if (is_wp_error($result)) {
6131 - //error_log('MxChat WooCommerce sync failed for product ' . $product_id . ': ' . $result->get_error_message());
6132 - }
6133 -}
6134 -
6135 -public function mxchat_handle_product_delete($post_id) {
6136 - if (get_post_type($post_id) !== 'product') {
6137 - return;
6138 - }
6139 -
6140 - $source_url = $this->mxchat_resolve_pre_trash_url($post_id);
6141 - if (!$source_url) {
6142 - return;
6143 - }
6144 -
6145 - // Chunk-aware deletion (routes to Pinecone or WP DB and removes base + all chunks)
6146 - MxChat_Utils::delete_chunks_for_url($source_url, 'default');
6147 -
6148 - delete_transient('mxchat_prev_url_' . $post_id);
6149 - delete_transient('mxchat_prev_status_' . $post_id);
6150 -}
6151 -
6152 -/**
6153 - * Handle individual Pinecone content deletion
6154 - */
6155 -public function mxchat_handle_pinecone_prompt_delete() {
6156 - // Check permissions
6157 - if (!current_user_can('manage_options')) {
6158 - wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
6159 - }
6160 -
6161 - // Verify nonce
6162 - if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'mxchat_delete_pinecone_prompt_nonce')) {
6163 - wp_die(esc_html__('Security check failed.', 'mxchat'));
6164 - }
6165 -
6166 - $vector_id = isset($_GET['vector_id']) ? sanitize_text_field($_GET['vector_id']) : '';
6167 -
6168 - if (empty($vector_id)) {
6169 - set_transient('mxchat_admin_notice_error',
6170 - esc_html__('Invalid vector ID.', 'mxchat'),
6171 - 30
6172 - );
6173 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
6174 - exit;
6175 - }
6176 -
6177 - // Get Pinecone settings
6178 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
6179 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
6180 -
6181 - if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
6182 - set_transient('mxchat_admin_notice_error',
6183 - esc_html__('Pinecone is not properly configured.', 'mxchat'),
6184 - 30
6185 - );
6186 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
6187 - exit;
6188 - }
6189 -
6190 - // Delete from Pinecone
6191 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
6192 - $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
6193 - $vector_id,
6194 - $pinecone_options['mxchat_pinecone_api_key'],
6195 - $pinecone_options['mxchat_pinecone_host']
6196 - );
6197 -
6198 - if ($result['success']) {
6199 - // No cache clearing needed since we removed caching
6200 - set_transient('mxchat_admin_notice_success',
6201 - esc_html__('Entry deleted successfully from Pinecone.', 'mxchat'),
6202 - 30
6203 - );
6204 - } else {
6205 - set_transient('mxchat_admin_notice_error',
6206 - esc_html__('Failed to delete entry: ', 'mxchat') . $result['message'],
6207 - 30
6208 - );
6209 - }
6210 -
6211 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
6212 - exit;
6213 -}
6214 -/**
6215 - * Handle individual Pinecone content deletion via AJAX
6216 - */
6217 -public function ajax_mxchat_delete_pinecone_prompt() {
6218 - // Verify nonce and permissions
6219 - if (!check_ajax_referer('mxchat_delete_pinecone_prompt_nonce', 'nonce', false)) {
6220 - wp_send_json_error('Invalid nonce');
6221 - exit;
6222 - }
6223 -
6224 - if (!current_user_can('manage_options')) {
6225 - wp_send_json_error('Unauthorized access');
6226 - exit;
6227 - }
6228 -
6229 - $vector_id = isset($_POST['vector_id']) ? sanitize_text_field($_POST['vector_id']) : '';
6230 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
6231 -
6232 - if (empty($vector_id)) {
6233 - wp_send_json_error('Missing vector ID');
6234 - exit;
6235 - }
6236 -
6237 - // Get bot-specific Pinecone settings
6238 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
6239 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
6240 -
6241 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
6242 -
6243 - if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
6244 - wp_send_json_error('Pinecone is not properly configured for bot: ' . $bot_id);
6245 - exit;
6246 - }
6247 -
6248 - // Delete from the correct Pinecone index
6249 - $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
6250 - $vector_id,
6251 - $pinecone_options['mxchat_pinecone_api_key'],
6252 - $pinecone_options['mxchat_pinecone_host']
6253 - );
6254 -
6255 - if ($result['success']) {
6256 - // No cache clearing needed since we removed caching
6257 - wp_send_json_success(array(
6258 - 'message' => 'Entry deleted successfully from Pinecone',
6259 - 'vector_id' => $vector_id,
6260 - 'bot_id' => $bot_id
6261 - ));
6262 - } else {
6263 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Failed to delete from Pinecone: ' . $result['message'], array('vector_id' => $vector_id, 'bot_id' => $bot_id));
6264 - wp_send_json_error('Failed to delete from Pinecone: ' . $result['message']);
6265 - }
6266 -
6267 - exit;
6268 -}
6269 -
6270 -/**
6271 - * Handle deletion of all chunks for a given source URL via AJAX
6272 - * Follows the same pattern as ajax_mxchat_delete_pinecone_prompt
6273 - */
6274 -public function ajax_mxchat_delete_chunks_by_url() {
6275 - // Verify nonce and permissions
6276 - if (!check_ajax_referer('mxchat_delete_chunks_nonce', 'nonce', false)) {
6277 - wp_send_json_error('Invalid nonce');
6278 - exit;
6279 - }
6280 -
6281 - if (!current_user_can('manage_options')) {
6282 - wp_send_json_error('Unauthorized access');
6283 - exit;
6284 - }
6285 -
6286 - $source_url = isset($_POST['source_url']) ? esc_url_raw($_POST['source_url']) : '';
6287 - $data_source = isset($_POST['data_source']) ? sanitize_text_field($_POST['data_source']) : 'wordpress';
6288 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
6289 -
6290 - if (empty($source_url)) {
6291 - wp_send_json_error('Missing source URL');
6292 - exit;
6293 - }
6294 -
6295 - // Generate the base vector ID from the source URL (same as how chunks are created)
6296 - $base_vector_id = md5($source_url);
6297 -
6298 - if ($data_source === 'pinecone') {
6299 - // Get bot-specific Pinecone settings (same as working delete function)
6300 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
6301 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
6302 -
6303 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
6304 -
6305 - if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
6306 - wp_send_json_error('Pinecone is not properly configured for bot: ' . $bot_id);
6307 - exit;
6308 - }
6309 -
6310 - $api_key = $pinecone_options['mxchat_pinecone_api_key'];
6311 - $host = $pinecone_options['mxchat_pinecone_host'];
6312 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
6313 -
6314 - // Collect all vector IDs to delete
6315 - $vectors_to_delete = array();
6316 -
6317 - // Add the original single-vector ID (for non-chunked content)
6318 - $vectors_to_delete[] = $base_vector_id;
6319 -
6320 - // Use Pinecone list API to find all chunk vectors with this prefix
6321 - // NOTE: Pinecone List API is a GET request with query parameters, not POST
6322 - $prefix = $base_vector_id . '_chunk_';
6323 -
6324 - $query_params = array(
6325 - 'prefix' => $prefix,
6326 - 'limit' => 100
6327 - );
6328 -
6329 - if (!empty($namespace)) {
6330 - $query_params['namespace'] = $namespace;
6331 - }
6332 -
6333 - $list_url = "https://{$host}/vectors/list?" . http_build_query($query_params);
6334 -
6335 - $list_response = wp_remote_get($list_url, array(
6336 - 'headers' => array(
6337 - 'Api-Key' => $api_key,
6338 - 'accept' => 'application/json'
6339 - ),
6340 - 'timeout' => 30
6341 - ));
6342 -
6343 - if (!is_wp_error($list_response)) {
6344 - $list_body_response = wp_remote_retrieve_body($list_response);
6345 - $list_data = json_decode($list_body_response, true);
6346 - if (!empty($list_data['vectors'])) {
6347 - foreach ($list_data['vectors'] as $vector) {
6348 - if (isset($vector['id'])) {
6349 - $vectors_to_delete[] = $vector['id'];
6350 - }
6351 - }
6352 - }
6353 - }
6354 -
6355 - if (empty($vectors_to_delete)) {
6356 - wp_send_json_success(array(
6357 - 'message' => 'No vectors found to delete',
6358 - 'source_url' => $source_url
6359 - ));
6360 - exit;
6361 - }
6362 -
6363 - // Delete all vectors using the same endpoint as the working function
6364 - $delete_url = "https://{$host}/vectors/delete";
6365 -
6366 - $delete_body = array(
6367 - 'ids' => $vectors_to_delete
6368 - );
6369 -
6370 - if (!empty($namespace)) {
6371 - $delete_body['namespace'] = $namespace;
6372 - }
6373 -
6374 - $delete_response = wp_remote_post($delete_url, array(
6375 - 'headers' => array(
6376 - 'Api-Key' => $api_key,
6377 - 'accept' => 'application/json',
6378 - 'content-type' => 'application/json'
6379 - ),
6380 - 'body' => wp_json_encode($delete_body),
6381 - 'timeout' => 30
6382 - ));
6383 -
6384 - if (is_wp_error($delete_response)) {
6385 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Failed to delete chunks from Pinecone: ' . $delete_response->get_error_message(), array('source_url' => $source_url));
6386 - wp_send_json_error('Failed to delete from Pinecone: ' . $delete_response->get_error_message());
6387 - exit;
6388 - }
6389 -
6390 - $response_code = wp_remote_retrieve_response_code($delete_response);
6391 -
6392 - if ($response_code !== 200) {
6393 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Pinecone API error (HTTP ' . $response_code . ')', array('source_url' => $source_url));
6394 - wp_send_json_error('Pinecone API error (HTTP ' . $response_code . ')');
6395 - exit;
6396 - }
6397 -
6398 - wp_send_json_success(array(
6399 - 'message' => 'All chunks deleted successfully from Pinecone',
6400 - 'source_url' => $source_url,
6401 - 'deleted_count' => count($vectors_to_delete)
6402 - ));
6403 -
6404 - } else {
6405 - // WordPress database deletion
6406 - global $wpdb;
6407 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
6408 -
6409 - $result = $wpdb->delete(
6410 - $table_name,
6411 - array('source_url' => $source_url),
6412 - array('%s')
6413 - );
6414 -
6415 - if ($result === false) {
6416 - MxChat_Admin::mxchat_log_debug('database_error', 'Failed to delete from database: ' . $wpdb->last_error, array('source_url' => $source_url));
6417 - wp_send_json_error('Failed to delete from database: ' . $wpdb->last_error);
6418 - exit;
6419 - }
6420 -
6421 - wp_send_json_success(array(
6422 - 'message' => 'All chunks deleted successfully from database',
6423 - 'source_url' => $source_url,
6424 - 'deleted_count' => $result
6425 - ));
6426 - }
6427 -
6428 - exit;
6429 -}
6430 -
6431 -/**
6432 - * Handle individual WordPress database content deletion via AJAX
6433 - * Mirrors the Pinecone delete handler but for WordPress database entries
6434 - */
6435 -public function ajax_mxchat_delete_wordpress_prompt() {
6436 - // Verify nonce and permissions
6437 - if (!check_ajax_referer('mxchat_delete_wordpress_prompt_nonce', 'nonce', false)) {
6438 - wp_send_json_error('Invalid nonce');
6439 - exit;
6440 - }
6441 -
6442 - if (!current_user_can('manage_options')) {
6443 - wp_send_json_error('Unauthorized access');
6444 - exit;
6445 - }
6446 -
6447 - $entry_id = isset($_POST['entry_id']) ? intval($_POST['entry_id']) : 0;
6448 -
6449 - if (empty($entry_id)) {
6450 - wp_send_json_error('Missing entry ID');
6451 - exit;
6452 - }
6453 -
6454 - global $wpdb;
6455 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
6456 -
6457 - // Clear cache for this entry
6458 - wp_cache_delete('prompt_' . $entry_id, 'mxchat_prompts');
6459 -
6460 - // Delete from database
6461 - $result = $wpdb->delete(
6462 - $table_name,
6463 - array('id' => $entry_id),
6464 - array('%d')
6465 - );
6466 -
6467 - if ($result !== false) {
6468 - wp_send_json_success(array(
6469 - 'message' => 'Entry deleted successfully',
6470 - 'entry_id' => $entry_id
6471 - ));
6472 - } else {
6473 - wp_send_json_error('Failed to delete entry from database');
6474 - }
6475 -
6476 - exit;
6477 -}
6478 -
6479 -/**
6480 - * Handle bulk deletion of knowledge entries via AJAX
6481 - * Supports both Pinecone and WordPress database entries
6482 - */
6483 -public function ajax_mxchat_bulk_delete_knowledge() {
6484 - // Verify nonce and permissions
6485 - if (!check_ajax_referer('mxchat_bulk_delete_knowledge_nonce', 'nonce', false)) {
6486 - wp_send_json_error('Invalid nonce');
6487 - exit;
6488 - }
6489 -
6490 - if (!current_user_can('manage_options')) {
6491 - wp_send_json_error('Unauthorized access');
6492 - exit;
6493 - }
6494 -
6495 - $entries = isset($_POST['entries']) ? $_POST['entries'] : array();
6496 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
6497 -
6498 - if (empty($entries) || !is_array($entries)) {
6499 - wp_send_json_error('No entries provided');
6500 - exit;
6501 - }
6502 -
6503 - // Extend execution time — bulk Pinecone operations can take a while
6504 - if (function_exists('set_time_limit')) {
6505 - set_time_limit(120);
6506 - }
6507 -
6508 - $success_ids = array();
6509 - $failed_ids = array();
6510 - $errors = array();
6511 -
6512 - global $wpdb;
6513 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
6514 -
6515 - // Get Pinecone manager for Pinecone deletions
6516 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
6517 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
6518 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
6519 -
6520 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
6521 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
6522 -
6523 - // =============================================
6524 - // PHASE 1: Collect all Pinecone vector IDs
6525 - // and separate WordPress entries
6526 - // =============================================
6527 - $pinecone_entry_ids = array(); // entry IDs that are pinecone-sourced
6528 - $wordpress_entries = array(); // entries for WordPress DB deletion
6529 - $all_vector_ids = array(); // all pinecone vector IDs to delete in one batch
6530 -
6531 - foreach ($entries as $entry) {
6532 - $entry_id = sanitize_text_field($entry['id'] ?? '');
6533 - $source = sanitize_text_field($entry['source'] ?? 'wordpress');
6534 - $source_url = isset($entry['sourceUrl']) ? esc_url_raw($entry['sourceUrl']) : '';
6535 - $is_group = isset($entry['isGroup']) && ($entry['isGroup'] === true || $entry['isGroup'] === 'true');
6536 -
6537 - if (empty($entry_id)) {
6538 - continue;
6539 - }
6540 -
6541 - if ($source === 'pinecone') {
6542 - if (!$use_pinecone || empty($api_key)) {
6543 - $failed_ids[] = $entry_id;
6544 - $errors[] = "Pinecone not configured for entry: $entry_id";
6545 - continue;
6546 - }
6547 -
6548 - $pinecone_entry_ids[] = $entry_id;
6549 -
6550 - if ($is_group && !empty($source_url)) {
6551 - // Grouped/chunked entry: collect base ID + chunk IDs via List API
6552 - $base_vector_id = md5($source_url);
6553 - $all_vector_ids[] = $base_vector_id;
6554 -
6555 - $list_url = 'https://' . $host . '/vectors/list?prefix=' . urlencode($base_vector_id . '_chunk_') . '&limit=100';
6556 - $list_response = wp_remote_get($list_url, array(
6557 - 'headers' => array(
6558 - 'Api-Key' => $api_key,
6559 - 'accept' => 'application/json'
6560 - ),
6561 - 'timeout' => 30
6562 - ));
6563 -
6564 - if (!is_wp_error($list_response)) {
6565 - $list_body = json_decode(wp_remote_retrieve_body($list_response), true);
6566 - if (!empty($list_body['vectors']) && is_array($list_body['vectors'])) {
6567 - foreach ($list_body['vectors'] as $vector) {
6568 - if (isset($vector['id'])) {
6569 - $all_vector_ids[] = $vector['id'];
6570 - }
6571 - }
6572 - }
6573 - }
6574 - } else {
6575 - // Single entry: the entry_id IS the vector ID
6576 - $all_vector_ids[] = $entry_id;
6577 - }
6578 - } else {
6579 - $wordpress_entries[] = $entry;
6580 - }
6581 - }
6582 -
6583 - // =============================================
6584 - // PHASE 2: Single batch delete to Pinecone
6585 - // =============================================
6586 - if (!empty($all_vector_ids)) {
6587 - $all_vector_ids = array_values(array_unique($all_vector_ids));
6588 - $pinecone_success = true;
6589 - $batches = array_chunk($all_vector_ids, 100);
6590 -
6591 - foreach ($batches as $batch) {
6592 - $delete_response = wp_remote_post("https://{$host}/vectors/delete", array(
6593 - 'headers' => array(
6594 - 'Api-Key' => $api_key,
6595 - 'accept' => 'application/json',
6596 - 'content-type' => 'application/json'
6597 - ),
6598 - 'body' => wp_json_encode(array('ids' => $batch)),
6599 - 'timeout' => 60
6600 - ));
6601 -
6602 - if (is_wp_error($delete_response)) {
6603 - $pinecone_success = false;
6604 - $errors[] = 'Pinecone batch deletion failed: ' . $delete_response->get_error_message();
6605 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Bulk delete batch failed: ' . $delete_response->get_error_message());
6606 - } else {
6607 - $response_code = wp_remote_retrieve_response_code($delete_response);
6608 - if ($response_code !== 200) {
6609 - $pinecone_success = false;
6610 - $response_body = wp_remote_retrieve_body($delete_response);
6611 - $errors[] = "Pinecone API error (HTTP $response_code)";
6612 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Bulk delete batch failed (HTTP ' . $response_code . ')', array('response' => substr($response_body, 0, 200)));
6613 - }
6614 - }
6615 - }
6616 -
6617 - // Mark all pinecone entries based on batch result
6618 - foreach ($pinecone_entry_ids as $eid) {
6619 - if ($pinecone_success) {
6620 - $success_ids[] = $eid;
6621 - } else {
6622 - $failed_ids[] = $eid;
6623 - }
6624 - }
6625 - }
6626 -
6627 - // =============================================
6628 - // PHASE 3: WordPress database deletions
6629 - // =============================================
6630 - foreach ($wordpress_entries as $entry) {
6631 - $entry_id = sanitize_text_field($entry['id'] ?? '');
6632 - $source_url = isset($entry['sourceUrl']) ? esc_url_raw($entry['sourceUrl']) : '';
6633 - $is_group = isset($entry['isGroup']) && ($entry['isGroup'] === true || $entry['isGroup'] === 'true');
6634 -
6635 - if (empty($entry_id)) {
6636 - continue;
6637 - }
6638 -
6639 - try {
6640 - if ($is_group && !empty($source_url)) {
6641 - $result = $wpdb->delete(
6642 - $table_name,
6643 - array('source_url' => $source_url),
6644 - array('%s')
6645 - );
6646 - } else {
6647 - wp_cache_delete('prompt_' . $entry_id, 'mxchat_prompts');
6648 - $result = $wpdb->delete(
6649 - $table_name,
6650 - array('id' => intval($entry_id)),
6651 - array('%d')
6652 - );
6653 - }
6654 -
6655 - if ($result !== false) {
6656 - $success_ids[] = $entry_id;
6657 - } else {
6658 - $failed_ids[] = $entry_id;
6659 - $errors[] = "Database error for entry: $entry_id";
6660 - }
6661 - } catch (Exception $e) {
6662 - $failed_ids[] = $entry_id;
6663 - $errors[] = $e->getMessage();
6664 - }
6665 - }
6666 -
6667 - wp_send_json_success(array(
6668 - 'success_ids' => $success_ids,
6669 - 'failed_ids' => $failed_ids,
6670 - 'errors' => $errors,
6671 - 'total_processed' => count($success_ids) + count($failed_ids)
6672 - ));
6673 -
6674 - exit;
6675 -}
6676 -
6677 -/**
6678 - * Get hierarchical roles for dropdown
6679 - */
6680 -public function mxchat_get_role_options() {
6681 - return array(
6682 - 'public' => __('Public (Everyone)', 'mxchat'),
6683 - 'logged_in' => __('Logged In Users', 'mxchat'),
6684 - 'subscriber' => __('Subscribers & Above', 'mxchat'),
6685 - 'contributor' => __('Contributors & Above', 'mxchat'),
6686 - 'author' => __('Authors & Above', 'mxchat'),
6687 - 'editor' => __('Editors & Above', 'mxchat'),
6688 - 'administrator' => __('Administrators Only', 'mxchat')
6689 - );
6690 -}
6691 -
6692 -/**
6693 - * Check if user has access to content based on role restriction
6694 - */
6695 -public function mxchat_user_has_content_access($role_restriction) {
6696 - // Public content is always accessible
6697 - if ($role_restriction === 'public' || empty($role_restriction)) {
6698 - return true;
6699 - }
6700 -
6701 - // Check if user is logged in for logged_in restriction
6702 - if ($role_restriction === 'logged_in') {
6703 - return is_user_logged_in();
6704 - }
6705 -
6706 - // If not logged in, no access to role-restricted content
6707 - if (!is_user_logged_in()) {
6708 - return false;
6709 - }
6710 -
6711 - $user = wp_get_current_user();
6712 - $user_roles = $user->roles;
6713 -
6714 - if (empty($user_roles)) {
6715 - return false;
6716 - }
6717 -
6718 - // Define role hierarchy (higher number = higher access)
6719 - $hierarchy = array(
6720 - 'subscriber' => 1,
6721 - 'contributor' => 2,
6722 - 'author' => 3,
6723 - 'editor' => 4,
6724 - 'administrator' => 5
6725 - );
6726 -
6727 - // Get required level
6728 - $required_level = isset($hierarchy[$role_restriction]) ? $hierarchy[$role_restriction] : 0;
6729 -
6730 - // Check if user has required level or higher
6731 - foreach ($user_roles as $user_role) {
6732 - $user_level = isset($hierarchy[$user_role]) ? $hierarchy[$user_role] : 0;
6733 - if ($user_level >= $required_level) {
6734 - return true;
6735 - }
6736 - }
6737 -
6738 - return false;
6739 -}
6740 -
6741 -/**
6742 - * Handle role restriction updates via AJAX
6743 - * Removed cache clearing call since we removed caching
6744 - */
6745 -public function ajax_mxchat_update_role_restriction() {
6746 - // Verify nonce and permissions
6747 - if (!check_ajax_referer('mxchat_update_role_nonce', 'nonce', false)) {
6748 - wp_send_json_error('Invalid nonce');
6749 - exit;
6750 - }
6751 -
6752 - if (!current_user_can('manage_options')) {
6753 - wp_send_json_error('Unauthorized access');
6754 - exit;
6755 - }
6756 -
6757 - $entry_id = isset($_POST['entry_id']) ? sanitize_text_field($_POST['entry_id']) : '';
6758 - $role_restriction = isset($_POST['role_restriction']) ? sanitize_text_field($_POST['role_restriction']) : 'public';
6759 - $data_source = isset($_POST['data_source']) ? sanitize_text_field($_POST['data_source']) : 'wordpress';
6760 -
6761 - if (empty($entry_id)) {
6762 - wp_send_json_error('Invalid entry ID');
6763 - exit;
6764 - }
6765 -
6766 - // Get knowledge manager instance to validate role restriction
6767 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
6768 - $valid_roles = array_keys($knowledge_manager->mxchat_get_role_options());
6769 - if (!in_array($role_restriction, $valid_roles)) {
6770 - wp_send_json_error('Invalid role restriction');
6771 - exit;
6772 - }
6773 -
6774 - global $wpdb;
6775 -
6776 - if ($data_source === 'pinecone') {
6777 - // Handle Pinecone role restriction (stored separately in WordPress table)
6778 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6779 -
6780 - // Use REPLACE to insert or update the role restriction
6781 - $result = $wpdb->replace(
6782 - $roles_table,
6783 - array(
6784 - 'vector_id' => $entry_id,
6785 - 'role_restriction' => $role_restriction,
6786 - 'updated_at' => current_time('mysql')
6787 - ),
6788 - array('%s', '%s', '%s')
6789 - );
6790 -
6791 - // No cache clearing needed since we removed caching
6792 -
6793 - } else {
6794 - // Handle WordPress database role restriction (existing functionality)
6795 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
6796 -
6797 - $result = $wpdb->update(
6798 - $table_name,
6799 - array('role_restriction' => $role_restriction),
6800 - array('id' => absint($entry_id)),
6801 - array('%s'),
6802 - array('%d')
6803 - );
6804 - }
6805 -
6806 - if ($result === false) {
6807 - wp_send_json_error('Database update failed: ' . $wpdb->last_error);
6808 - exit;
6809 - }
6810 -
6811 - wp_send_json_success(array(
6812 - 'message' => 'Role restriction updated successfully',
6813 - 'role_restriction' => $role_restriction,
6814 - 'data_source' => $data_source,
6815 - 'entry_id' => $entry_id
6816 - ));
6817 - exit;
6818 -}
6819 -
6820 -// ========================================
6821 -// ROLE-BASED CONTENT RESTRICTIONS - NEW FUNCTIONS
6822 -// Add these to your MxChat_Knowledge_Manager class
6823 -// ========================================
6824 -
6825 -/**
6826 - * Initialize role-based content hooks
6827 - * Add this call to your __construct() or mxchat_init_hooks() method
6828 - */
6829 -private function mxchat_init_role_hooks() {
6830 - // AJAX handlers for tag-role mappings
6831 - add_action('wp_ajax_mxchat_add_tag_role_mapping', array($this, 'ajax_add_tag_role_mapping'));
6832 - add_action('wp_ajax_mxchat_delete_tag_role_mapping', array($this, 'ajax_delete_tag_role_mapping'));
6833 - add_action('wp_ajax_mxchat_get_tag_role_mappings', array($this, 'ajax_get_tag_role_mappings'));
6834 - add_action('wp_ajax_mxchat_bulk_update_tag_roles', array($this, 'ajax_bulk_update_tag_roles'));
6835 -
6836 - // Hook to automatically update role restrictions when tags are added/removed
6837 - add_action('set_object_terms', array($this, 'handle_tag_change'), 10, 6);
6838 -
6839 - // Hook to apply role restrictions on auto-sync
6840 - add_action('mxchat_content_stored', array($this, 'apply_role_restriction_after_storage'), 10, 2);
6841 -}
6842 -
6843 -/**
6844 - * Add tag-role mapping via AJAX
6845 - */
6846 -public function ajax_add_tag_role_mapping() {
6847 - // Verify nonce and permissions
6848 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
6849 -
6850 - if (!current_user_can('manage_options')) {
6851 - wp_send_json_error('Unauthorized access');
6852 - exit;
6853 - }
6854 -
6855 - $tag_input = isset($_POST['tag_slug']) ? sanitize_text_field($_POST['tag_slug']) : '';
6856 - $role_restriction = isset($_POST['role_restriction']) ? sanitize_text_field($_POST['role_restriction']) : 'public';
6857 -
6858 - if (empty($tag_input)) {
6859 - wp_send_json_error('Please enter a tag name or slug');
6860 - exit;
6861 - }
6862 -
6863 - // Validate role restriction
6864 - $valid_roles = array_keys($this->mxchat_get_role_options());
6865 - if (!in_array($role_restriction, $valid_roles)) {
6866 - wp_send_json_error('Invalid role restriction');
6867 - exit;
6868 - }
6869 -
6870 - // Resolve the tag by slug first, then fall back to its display name, so users can
6871 - // enter either "premium-content" or "Premium Content". (plan b8bcf5 — the field is
6872 - // labeled by name but previously validated by slug only, producing the confusing
6873 - // "Tag does not exist in WordPress" error when a real tag's name was typed.)
6874 - $term = get_term_by('slug', $tag_input, 'post_tag');
6875 - if (!$term) {
6876 - $term = get_term_by('name', $tag_input, 'post_tag');
6877 - }
6878 - if (!$term) {
6879 - wp_send_json_error('No tag with that name or slug exists yet. Create it under Posts → Tags first, then enter its name or slug.');
6880 - exit;
6881 - }
6882 -
6883 - // Always key the mapping by the RESOLVED slug — apply_role_restriction_to_post()
6884 - // compares against each post's tag slugs, so the stored key must be a slug,
6885 - // never the raw (possibly display-name) input.
6886 - $tag_slug = $term->slug;
6887 -
6888 - // Get existing mappings
6889 - $mappings = get_option('mxchat_tag_role_mappings', array());
6890 -
6891 - // Check if mapping already exists
6892 - if (isset($mappings[$tag_slug])) {
6893 - wp_send_json_error('Mapping for this tag already exists');
6894 - exit;
6895 - }
6896 -
6897 - // Add new mapping
6898 - $mappings[$tag_slug] = $role_restriction;
6899 - update_option('mxchat_tag_role_mappings', $mappings);
6900 -
6901 - wp_send_json_success(array(
6902 - 'message' => 'Tag-role mapping added successfully',
6903 - 'tag_slug' => $tag_slug,
6904 - 'role_restriction' => $role_restriction
6905 - ));
6906 - exit;
6907 -}
6908 -
6909 -/**
6910 - * Delete tag-role mapping via AJAX
6911 - */
6912 -public function ajax_delete_tag_role_mapping() {
6913 - // Verify nonce and permissions
6914 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
6915 -
6916 - if (!current_user_can('manage_options')) {
6917 - wp_send_json_error('Unauthorized access');
6918 - exit;
6919 - }
6920 -
6921 - $tag_slug = isset($_POST['tag_slug']) ? sanitize_text_field($_POST['tag_slug']) : '';
6922 -
6923 - if (empty($tag_slug)) {
6924 - wp_send_json_error('Tag slug is required');
6925 - exit;
6926 - }
6927 -
6928 - // Get existing mappings
6929 - $mappings = get_option('mxchat_tag_role_mappings', array());
6930 -
6931 - // Check if mapping exists
6932 - if (!isset($mappings[$tag_slug])) {
6933 - wp_send_json_error('Mapping does not exist');
6934 - exit;
6935 - }
6936 -
6937 - // Remove mapping
6938 - unset($mappings[$tag_slug]);
6939 - update_option('mxchat_tag_role_mappings', $mappings);
6940 -
6941 - wp_send_json_success(array(
6942 - 'message' => 'Tag-role mapping deleted successfully',
6943 - 'tag_slug' => $tag_slug
6944 - ));
6945 - exit;
6946 -}
6947 -
6948 -/**
6949 - * Get all tag-role mappings via AJAX
6950 - */
6951 -public function ajax_get_tag_role_mappings() {
6952 - // Verify nonce and permissions
6953 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
6954 -
6955 - if (!current_user_can('manage_options')) {
6956 - wp_send_json_error('Unauthorized access');
6957 - exit;
6958 - }
6959 -
6960 - // Get mappings
6961 - $mappings = get_option('mxchat_tag_role_mappings', array());
6962 - $role_options = $this->mxchat_get_role_options();
6963 -
6964 - $formatted_mappings = array();
6965 -
6966 - foreach ($mappings as $tag_slug => $role_restriction) {
6967 - // Get tag object
6968 - $term = get_term_by('slug', $tag_slug, 'post_tag');
6969 -
6970 - // Count posts with this tag
6971 - $post_count = 0;
6972 - if ($term) {
6973 - $post_count = $term->count;
6974 - }
6975 -
6976 - $formatted_mappings[] = array(
6977 - 'tag_slug' => $tag_slug,
6978 - 'role_restriction' => $role_restriction,
6979 - 'role_label' => $role_options[$role_restriction] ?? $role_restriction,
6980 - 'post_count' => $post_count
6981 - );
6982 - }
6983 -
6984 - wp_send_json_success(array(
6985 - 'mappings' => $formatted_mappings
6986 - ));
6987 - exit;
6988 -}
6989 -
6990 -/**
6991 - * Bulk update role restrictions for all existing content with mapped tags
6992 - */
6993 -public function ajax_bulk_update_tag_roles() {
6994 - // Verify nonce and permissions
6995 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
6996 -
6997 - if (!current_user_can('manage_options')) {
6998 - wp_send_json_error('Unauthorized access');
6999 - exit;
7000 - }
7001 -
7002 - // Get mappings
7003 - $mappings = get_option('mxchat_tag_role_mappings', array());
7004 -
7005 - if (empty($mappings)) {
7006 - wp_send_json_error('No tag-role mappings found');
7007 - exit;
7008 - }
7009 -
7010 - global $wpdb;
7011 -
7012 - // Check if using Pinecone
7013 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
7014 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
7015 -
7016 - $updated_count = 0;
7017 - $details = array();
7018 -
7019 - foreach ($mappings as $tag_slug => $role_restriction) {
7020 - // Get all posts with this tag
7021 - $posts = get_posts(array(
7022 - 'tag' => $tag_slug,
7023 - 'post_type' => 'any',
7024 - 'posts_per_page' => -1,
7025 - 'fields' => 'ids',
7026 - 'post_status' => 'publish'
7027 - ));
7028 -
7029 - if (empty($posts)) {
7030 - continue;
7031 - }
7032 -
7033 - $tag_updated = 0;
7034 -
7035 - foreach ($posts as $post_id) {
7036 - $source_url = get_permalink($post_id);
7037 - if (!$source_url) {
7038 - continue;
7039 - }
7040 -
7041 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
7042 - // Update Pinecone role restriction
7043 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
7044 - $vector_id = md5($source_url);
7045 -
7046 - $result = $wpdb->replace(
7047 - $roles_table,
7048 - array(
7049 - 'vector_id' => $vector_id,
7050 - 'role_restriction' => $role_restriction,
7051 - 'updated_at' => current_time('mysql')
7052 - ),
7053 - array('%s', '%s', '%s')
7054 - );
7055 - } else {
7056 - // Update WordPress DB
7057 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
7058 -
7059 - $result = $wpdb->update(
7060 - $table_name,
7061 - array('role_restriction' => $role_restriction),
7062 - array('source_url' => $source_url),
7063 - array('%s'),
7064 - array('%s')
7065 - );
7066 - }
7067 -
7068 - if ($result !== false) {
7069 - $tag_updated++;
7070 - $updated_count++;
7071 - }
7072 - }
7073 -
7074 - if ($tag_updated > 0) {
7075 - $details[] = sprintf(
7076 - 'Tag "%s" (%s): %d posts updated',
7077 - $tag_slug,
7078 - $role_restriction,
7079 - $tag_updated
7080 - );
7081 - }
7082 - }
7083 -
7084 - wp_send_json_success(array(
7085 - 'message' => 'Bulk update completed',
7086 - 'updated_count' => $updated_count,
7087 - 'tags_processed' => count($mappings),
7088 - 'details' => $details
7089 - ));
7090 - exit;
7091 -}
7092 -
7093 -/**
7094 - * Handle tag changes on posts (when tags are added or removed)
7095 - */
7096 -public function handle_tag_change($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {
7097 - // Only process post tags
7098 - if ($taxonomy !== 'post_tag') {
7099 - return;
7100 - }
7101 -
7102 - // Get tag-role mappings
7103 - $mappings = get_option('mxchat_tag_role_mappings', array());
7104 -
7105 - if (empty($mappings)) {
7106 - return;
7107 - }
7108 -
7109 - // Get the post's URL
7110 - $source_url = get_permalink($object_id);
7111 - if (!$source_url) {
7112 - return;
7113 - }
7114 -
7115 - // Determine the highest role restriction based on tags
7116 - $highest_role = 'public';
7117 - $role_hierarchy = array(
7118 - 'public' => 0,
7119 - 'logged_in' => 1,
7120 - 'subscriber' => 2,
7121 - 'contributor' => 3,
7122 - 'author' => 4,
7123 - 'editor' => 5,
7124 - 'administrator' => 6
7125 - );
7126 -
7127 - // Get all current tags for the post
7128 - $current_tags = wp_get_post_tags($object_id, array('fields' => 'slugs'));
7129 -
7130 - // Find the highest role restriction among the tags
7131 - foreach ($current_tags as $tag_slug) {
7132 - if (isset($mappings[$tag_slug])) {
7133 - $role = $mappings[$tag_slug];
7134 - if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
7135 - $highest_role = $role;
7136 - }
7137 - }
7138 - }
7139 -
7140 - // Update the role restriction in the database
7141 - global $wpdb;
7142 -
7143 - // Check if using Pinecone
7144 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
7145 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
7146 -
7147 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
7148 - // Update Pinecone role restriction
7149 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
7150 - $vector_id = md5($source_url);
7151 -
7152 - $wpdb->replace(
7153 - $roles_table,
7154 - array(
7155 - 'vector_id' => $vector_id,
7156 - 'role_restriction' => $highest_role,
7157 - 'updated_at' => current_time('mysql')
7158 - ),
7159 - array('%s', '%s', '%s')
7160 - );
7161 - } else {
7162 - // Update WordPress DB
7163 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
7164 -
7165 - $wpdb->update(
7166 - $table_name,
7167 - array('role_restriction' => $highest_role),
7168 - array('source_url' => $source_url),
7169 - array('%s'),
7170 - array('%s')
7171 - );
7172 - }
7173 -}
7174 -
7175 -/**
7176 - * Apply role restriction after content is stored (for auto-sync)
7177 - */
7178 -public function apply_role_restriction_after_storage($post_id, $source_url) {
7179 - // Get tag-role mappings
7180 - $mappings = get_option('mxchat_tag_role_mappings', array());
7181 -
7182 - if (empty($mappings)) {
7183 - return;
7184 - }
7185 -
7186 - // Get all tags for the post
7187 - $post_tags = wp_get_post_tags($post_id, array('fields' => 'slugs'));
7188 -
7189 - if (empty($post_tags)) {
7190 - return;
7191 - }
7192 -
7193 - // Determine the highest role restriction based on tags
7194 - $highest_role = 'public';
7195 - $role_hierarchy = array(
7196 - 'public' => 0,
7197 - 'logged_in' => 1,
7198 - 'subscriber' => 2,
7199 - 'contributor' => 3,
7200 - 'author' => 4,
7201 - 'editor' => 5,
7202 - 'administrator' => 6
7203 - );
7204 -
7205 - foreach ($post_tags as $tag_slug) {
7206 - if (isset($mappings[$tag_slug])) {
7207 - $role = $mappings[$tag_slug];
7208 - if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
7209 - $highest_role = $role;
7210 - }
7211 - }
7212 - }
7213 -
7214 - // If no restricted tags found, return (leave as public)
7215 - if ($highest_role === 'public') {
7216 - return;
7217 - }
7218 -
7219 - // Update the role restriction
7220 - global $wpdb;
7221 -
7222 - // Check if using Pinecone
7223 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
7224 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
7225 -
7226 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
7227 - // Update Pinecone role restriction
7228 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
7229 - $vector_id = md5($source_url);
7230 -
7231 - $wpdb->replace(
7232 - $roles_table,
7233 - array(
7234 - 'vector_id' => $vector_id,
7235 - 'role_restriction' => $highest_role,
7236 - 'updated_at' => current_time('mysql')
7237 - ),
7238 - array('%s', '%s', '%s')
7239 - );
7240 - } else {
7241 - // Update WordPress DB
7242 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
7243 -
7244 - $wpdb->update(
7245 - $table_name,
7246 - array('role_restriction' => $highest_role),
7247 - array('source_url' => $source_url),
7248 - array('%s'),
7249 - array('%s')
7250 - );
7251 - }
7252 -}
7253 -
7254 -
7255 - // ========================================
7256 - // HELPER METHODS
7257 - // ========================================
7258 -
7259 - /**
7260 - * Check if user has required permissions for content processing
7261 - */
7262 - private function mxchat_check_user_permissions() {
7263 - if (!current_user_can('manage_options')) {
7264 - wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
7265 - }
7266 - }
7267 -
7268 - /**
7269 - * Validate nonce for security
7270 - */
7271 - private function mxchat_validate_nonce($nonce_name, $nonce_action) {
7272 - if (!isset($_POST[$nonce_name]) || !wp_verify_nonce($_POST[$nonce_name], $nonce_action)) {
7273 - wp_die(esc_html__('Security check failed.', 'mxchat'));
7274 - }
7275 - }
7276 -
7277 - /**
7278 - * Get embedding API credentials
7279 - */
7280 - private function mxchat_get_embedding_credentials() {
7281 - $embedding_model = $this->options['embedding_model'] ?? 'text-embedding-ada-002';
7282 -
7283 - if (strpos($embedding_model, 'text-embedding-') !== false) {
7284 - return array(
7285 - 'type' => 'openai',
7286 - 'api_key' => $this->options['api_key'] ?? ''
7287 - );
7288 - } elseif (strpos($embedding_model, 'voyage-') !== false) {
7289 - return array(
7290 - 'type' => 'voyage',
7291 - 'api_key' => $this->options['voyage_api_key'] ?? ''
7292 - );
7293 - } elseif (strpos($embedding_model, 'gemini-embedding-') !== false) {
7294 - return array(
7295 - 'type' => 'gemini',
7296 - 'api_key' => $this->options['gemini_api_key'] ?? ''
7297 - );
7298 - }
7299 -
7300 - return array('type' => 'unknown', 'api_key' => '');
7301 - }
7302 -
7303 - /**
7304 - * Log processing errors
7305 - */
7306 - private function mxchat_log_processing_error($operation, $error_message) {
7307 - //error_log("MxChat Knowledge Processing {$operation} Error: " . $error_message);
7308 - }
7309 -
7310 - /**
7311 - * Set admin notice transient
7312 - */
7313 - private function mxchat_set_admin_notice($type, $message) {
7314 - set_transient("mxchat_admin_notice_{$type}", $message, 30);
7315 - }
7316 -
7317 - /**
7318 - * Get Pinecone manager instance for vector operations
7319 - */
7320 - private function mxchat_get_pinecone_manager() {
7321 - return MxChat_Pinecone_Manager::get_instance();
7322 - }
7323 -
7324 -
7325 - // ========================================
7326 -// DATABASE QUEUE TABLE MANAGEMENT
7327 -// ========================================
7328 -
7329 -/**
7330 - * Create queue table on plugin activation
7331 - * Call this from your plugin activation hook
7332 - */
7333 -public function mxchat_create_queue_table() {
7334 - global $wpdb;
7335 -
7336 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
7337 - $charset_collate = $wpdb->get_charset_collate();
7338 -
7339 - $sql = "CREATE TABLE IF NOT EXISTS $table_name (
7340 - id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
7341 - queue_id varchar(64) NOT NULL,
7342 - item_type varchar(20) NOT NULL,
7343 - item_data longtext NOT NULL,
7344 - status varchar(20) NOT NULL DEFAULT 'pending',
7345 - bot_id varchar(50) NOT NULL DEFAULT 'default',
7346 - priority int(11) NOT NULL DEFAULT 0,
7347 - attempts int(11) NOT NULL DEFAULT 0,
7348 - max_attempts int(11) NOT NULL DEFAULT 3,
7349 - error_message text DEFAULT NULL,
7350 - created_at datetime NOT NULL,
7351 - started_at datetime DEFAULT NULL,
7352 - completed_at datetime DEFAULT NULL,
7353 - PRIMARY KEY (id),
7354 - KEY queue_id (queue_id),
7355 - KEY status (status),
7356 - KEY item_type (item_type),
7357 - KEY priority (priority)
7358 - ) $charset_collate;";
7359 -
7360 - require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
7361 - dbDelta($sql);
7362 -
7363 - // Also create a meta table for queue metadata
7364 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
7365 -
7366 - $meta_sql = "CREATE TABLE IF NOT EXISTS $meta_table (
7367 - id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
7368 - queue_id varchar(64) NOT NULL,
7369 - meta_key varchar(255) NOT NULL,
7370 - meta_value longtext,
7371 - PRIMARY KEY (id),
7372 - KEY queue_id (queue_id),
7373 - KEY meta_key (meta_key)
7374 - ) $charset_collate;";
7375 -
7376 - dbDelta($meta_sql);
7377 -}
7378 -
7379 -/**
7380 - * Add items to the processing queue
7381 - *
7382 - * @param string $queue_id Unique identifier for this queue batch
7383 - * @param string $item_type Type of item (url, pdf_page)
7384 - * @param array $items Array of items to queue
7385 - * @param string $bot_id Bot ID for processing
7386 - * @return int Number of items queued
7387 - */
7388 -private function mxchat_add_to_queue($queue_id, $item_type, $items, $bot_id = 'default') {
7389 - global $wpdb;
7390 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
7391 -
7392 - $queued_count = 0;
7393 - $priority = 0;
7394 -
7395 - foreach ($items as $item) {
7396 - $result = $wpdb->insert(
7397 - $table_name,
7398 - array(
7399 - 'queue_id' => $queue_id,
7400 - 'item_type' => $item_type,
7401 - 'item_data' => wp_json_encode($item),
7402 - 'status' => 'pending',
7403 - 'bot_id' => $bot_id,
7404 - 'priority' => $priority,
7405 - 'attempts' => 0,
7406 - 'max_attempts' => 3,
7407 - 'created_at' => current_time('mysql')
7408 - ),
7409 - array('%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%s')
7410 - );
7411 -
7412 - if ($result) {
7413 - $queued_count++;
7414 - }
7415 -
7416 - $priority++; // Process in order
7417 - }
7418 -
7419 - return $queued_count;
7420 -}
7421 -
7422 -/**
7423 - * Store queue metadata (total counts, source URL, etc.)
7424 - */
7425 -private function mxchat_set_queue_meta($queue_id, $meta_key, $meta_value) {
7426 - global $wpdb;
7427 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
7428 -
7429 - // Check if meta exists
7430 - $existing = $wpdb->get_var($wpdb->prepare(
7431 - "SELECT id FROM $meta_table WHERE queue_id = %s AND meta_key = %s",
7432 - $queue_id,
7433 - $meta_key
7434 - ));
7435 -
7436 - if ($existing) {
7437 - // Update
7438 - $wpdb->update(
7439 - $meta_table,
7440 - array('meta_value' => maybe_serialize($meta_value)),
7441 - array('queue_id' => $queue_id, 'meta_key' => $meta_key),
7442 - array('%s'),
7443 - array('%s', '%s')
7444 - );
7445 - } else {
7446 - // Insert
7447 - $wpdb->insert(
7448 - $meta_table,
7449 - array(
7450 - 'queue_id' => $queue_id,
7451 - 'meta_key' => $meta_key,
7452 - 'meta_value' => maybe_serialize($meta_value)
7453 - ),
7454 - array('%s', '%s', '%s')
7455 - );
7456 - }
7457 -}
7458 -
7459 -/**
7460 - * Get queue metadata
7461 - */
7462 -private function mxchat_get_queue_meta($queue_id, $meta_key) {
7463 - global $wpdb;
7464 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
7465 -
7466 - $value = $wpdb->get_var($wpdb->prepare(
7467 - "SELECT meta_value FROM $meta_table WHERE queue_id = %s AND meta_key = %s",
7468 - $queue_id,
7469 - $meta_key
7470 - ));
7471 -
7472 - return maybe_unserialize($value);
7473 -}
7474 -
7475 -// ========================================
7476 -// AJAX QUEUE PROCESSING HANDLERS
7477 -// ========================================
7478 -
7479 -/**
7480 - * AJAX: Get next item from queue to process
7481 - */
7482 -public function ajax_mxchat_get_next_queue_item() {
7483 - // Verify nonce and permissions
7484 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
7485 -
7486 - if (!current_user_can('manage_options')) {
7487 - wp_send_json_error('Unauthorized access');
7488 - }
7489 -
7490 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
7491 -
7492 - if (empty($queue_id)) {
7493 - wp_send_json_error('Missing queue ID');
7494 - }
7495 -
7496 - global $wpdb;
7497 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
7498 -
7499 - // Get next pending item with retry logic for failed items
7500 - $next_item = $wpdb->get_row($wpdb->prepare(
7501 - "SELECT * FROM $table_name
7502 - WHERE queue_id = %s
7503 - AND status IN ('pending', 'failed')
7504 - AND attempts < max_attempts
7505 - ORDER BY priority ASC, id ASC
7506 - LIMIT 1",
7507 - $queue_id
7508 - ));
7509 -
7510 - if (!$next_item) {
7511 - // No more items - queue complete
7512 - wp_send_json_success(array(
7513 - 'complete' => true,
7514 - 'message' => 'Queue processing complete'
7515 - ));
7516 - }
7517 -
7518 - // Mark item as processing
7519 - $wpdb->update(
7520 - $table_name,
7521 - array(
7522 - 'status' => 'processing',
7523 - 'started_at' => current_time('mysql'),
7524 - 'attempts' => $next_item->attempts + 1
7525 - ),
7526 - array('id' => $next_item->id),
7527 - array('%s', '%s', '%d'),
7528 - array('%d')
7529 - );
7530 -
7531 - wp_send_json_success(array(
7532 - 'complete' => false,
7533 - 'item' => array(
7534 - 'id' => $next_item->id,
7535 - 'type' => $next_item->item_type,
7536 - 'data' => json_decode($next_item->item_data, true),
7537 - 'bot_id' => $next_item->bot_id,
7538 - 'attempt' => $next_item->attempts + 1
7539 - )
7540 - ));
7541 -}
7542 -
7543 -/**
7544 - * AJAX: Process a single queue item
7545 - */
7546 -public function ajax_mxchat_process_queue_item() {
7547 - // Verify nonce and permissions
7548 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
7549 -
7550 - if (!current_user_can('manage_options')) {
7551 - wp_send_json_error('Unauthorized access');
7552 - }
7553 -
7554 - $item_id = isset($_POST['item_id']) ? absint($_POST['item_id']) : 0;
7555 - $item_type = isset($_POST['item_type']) ? sanitize_text_field($_POST['item_type']) : '';
7556 - $item_data = isset($_POST['item_data']) ? $_POST['item_data'] : array();
7557 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
7558 -
7559 - if (empty($item_id) || empty($item_type)) {
7560 - wp_send_json_error('Missing item data');
7561 - }
7562 -
7563 - global $wpdb;
7564 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
7565 -
7566 - // Process based on item type
7567 - try {
7568 - set_time_limit(60); // Give processing 60 seconds
7569 -
7570 - $result = false;
7571 - $error_message = '';
7572 -
7573 - // Read item directly from DB to get queue_id and preserve special chars in item_data
7574 - // (POST round-trip through JS mangles characters like apostrophes in URLs)
7575 - $db_item = $wpdb->get_row($wpdb->prepare(
7576 - "SELECT queue_id, item_data FROM $table_name WHERE id = %d",
7577 - $item_id
7578 - ));
7579 - $item_queue_id = $db_item ? $db_item->queue_id : '';
7580 - if ($db_item && !empty($db_item->item_data)) {
7581 - $db_data = json_decode($db_item->item_data, true);
7582 - if (is_array($db_data)) {
7583 - $item_data = $db_data;
7584 - }
7585 - }
7586 -
7587 - switch ($item_type) {
7588 - case 'url':
7589 - $result = $this->mxchat_process_queue_url($item_data, $bot_id, $item_queue_id);
7590 - break;
7591 -
7592 - case 'pdf_page':
7593 - $result = $this->mxchat_process_queue_pdf_page($item_data, $bot_id);
7594 - break;
7595 -
7596 - default:
7597 - throw new Exception('Unknown item type: ' . $item_type);
7598 - }
7599 -
7600 - if (is_wp_error($result)) {
7601 - $error_code = $result->get_error_code();
7602 - // Content errors (empty page, sanitization) are permanent — retrying won't help
7603 - $permanent_codes = array('empty_page', 'empty_after_sanitization', 'no_api_key', 'page_not_found');
7604 - if (in_array($error_code, $permanent_codes)) {
7605 - // Mark as permanently failed — set attempts = max_attempts so it won't be retried
7606 - $current_item = $wpdb->get_row($wpdb->prepare(
7607 - "SELECT max_attempts FROM $table_name WHERE id = %d", $item_id
7608 - ));
7609 - $wpdb->update(
7610 - $table_name,
7611 - array(
7612 - 'status' => 'failed',
7613 - 'error_message' => $result->get_error_message(),
7614 - 'attempts' => $current_item ? $current_item->max_attempts : 3
7615 - ),
7616 - array('id' => $item_id),
7617 - array('%s', '%s', '%d'),
7618 - array('%d')
7619 - );
7620 - wp_send_json_error(array(
7621 - 'message' => $result->get_error_message(),
7622 - 'permanent_failure' => true,
7623 - 'item_id' => $item_id
7624 - ));
7625 - return;
7626 - }
7627 - throw new Exception($result->get_error_message());
7628 - }
7629 -
7630 - if ($result === false) {
7631 - throw new Exception('Processing returned false - item may be empty or invalid');
7632 - }
7633 -
7634 - // Mark as completed
7635 - $wpdb->update(
7636 - $table_name,
7637 - array(
7638 - 'status' => 'completed',
7639 - 'completed_at' => current_time('mysql'),
7640 - 'error_message' => null
7641 - ),
7642 - array('id' => $item_id),
7643 - array('%s', '%s', '%s'),
7644 - array('%d')
7645 - );
7646 -
7647 - wp_send_json_success(array(
7648 - 'processed' => true,
7649 - 'item_id' => $item_id,
7650 - 'message' => 'Item processed successfully'
7651 - ));
7652 -
7653 - } catch (Exception $e) {
7654 - $error_message = $e->getMessage();
7655 -
7656 - // Get current attempt count
7657 - $item = $wpdb->get_row($wpdb->prepare(
7658 - "SELECT attempts, max_attempts FROM $table_name WHERE id = %d",
7659 - $item_id
7660 - ));
7661 -
7662 - // Check if we've exhausted retries
7663 - if ($item && $item->attempts >= $item->max_attempts) {
7664 - // Permanently failed
7665 - $wpdb->update(
7666 - $table_name,
7667 - array(
7668 - 'status' => 'failed',
7669 - 'error_message' => $error_message
7670 - ),
7671 - array('id' => $item_id),
7672 - array('%s', '%s'),
7673 - array('%d')
7674 - );
7675 -
7676 - wp_send_json_error(array(
7677 - 'message' => 'Item failed after maximum attempts: ' . $error_message,
7678 - 'permanent_failure' => true,
7679 - 'item_id' => $item_id
7680 - ));
7681 - } else {
7682 - // Mark for retry
7683 - $wpdb->update(
7684 - $table_name,
7685 - array(
7686 - 'status' => 'failed',
7687 - 'error_message' => $error_message
7688 - ),
7689 - array('id' => $item_id),
7690 - array('%s', '%s'),
7691 - array('%d')
7692 - );
7693 -
7694 - wp_send_json_error(array(
7695 - 'message' => 'Item processing failed, will retry: ' . $error_message,
7696 - 'can_retry' => true,
7697 - 'item_id' => $item_id,
7698 - 'attempts' => $item ? $item->attempts : 0
7699 - ));
7700 - }
7701 - }
7702 -}
7703 -
7704 -/**
7705 - * Process a URL from the queue
7706 - */
7707 -private function mxchat_process_queue_url($item_data, $bot_id = 'default', $queue_id = '') {
7708 - $url = isset($item_data['url']) ? $item_data['url'] : '';
7709 -
7710 - if (empty($url)) {
7711 - return new WP_Error('invalid_url', 'URL is empty');
7712 - }
7713 -
7714 - // Get bot-specific API key early (needed for both paths)
7715 - $bot_options = $this->get_bot_options($bot_id);
7716 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
7717 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
7718 -
7719 - if (strpos($selected_model, 'voyage') === 0) {
7720 - $api_key = $options['voyage_api_key'] ?? '';
7721 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
7722 - $api_key = $options['gemini_api_key'] ?? '';
7723 - } else {
7724 - $api_key = $options['api_key'] ?? '';
7725 - }
7726 -
7727 - if (empty($api_key)) {
7728 - return new WP_Error('no_api_key', 'API key not configured for bot: ' . $bot_id);
7729 - }
7730 -
7731 - // Check if this is a WooCommerce product URL and WooCommerce is active
7732 - $is_product_url = (strpos($url, '/product/') !== false || strpos($url, '/shop/') !== false);
7733 - $content_type = $is_product_url ? 'product' : 'url';
7734 -
7735 - // Try to get WooCommerce product data if it's a product URL
7736 - if ($is_product_url && class_exists('WooCommerce')) {
7737 - $product_content = $this->mxchat_extract_woocommerce_product_content($url);
7738 -
7739 - if (!empty($product_content)) {
7740 - // Successfully extracted WooCommerce product data with pricing
7741 - $result = MxChat_Utils::submit_content_to_db(
7742 - $product_content,
7743 - $url,
7744 - $api_key,
7745 - null,
7746 - $bot_id,
7747 - 'product'
7748 - );
7749 - return $result;
7750 - }
7751 - // If WooCommerce extraction failed, fall through to HTML extraction
7752 - }
7753 -
7754 - // Fetch URL content (fallback for non-products or when WooCommerce extraction fails)
7755 - $is_likely_pdf = (strtolower(pathinfo(parse_url($url, PHP_URL_PATH) ?: '', PATHINFO_EXTENSION)) === 'pdf');
7756 - $response = wp_remote_get($url, array(
7757 - 'timeout' => $is_likely_pdf ? 120 : 30,
7758 - 'redirection' => 5,
7759 - 'user-agent' => 'MxChat/1.0'
7760 - ));
7761 -
7762 - if (is_wp_error($response)) {
7763 - return $response;
7764 - }
7765 -
7766 - $response_code = wp_remote_retrieve_response_code($response);
7767 - if ($response_code !== 200) {
7768 - return new WP_Error('http_error', 'HTTP ' . $response_code . ' error for: ' . $url);
7769 - }
7770 -
7771 - // Check if URL is a PDF — expand into per-page queue items using the standard PDF pipeline
7772 - if ($this->mxchat_is_pdf_url($url, $response)) {
7773 - return $this->mxchat_expand_pdf_to_queue($url, $response, $bot_id, $queue_id);
7774 - }
7775 -
7776 - $html = wp_remote_retrieve_body($response);
7777 -
7778 - if (empty($html)) {
7779 - return new WP_Error('empty_response', 'Empty response body');
7780 - }
7781 -
7782 - // Extract and sanitize content
7783 - $content = $this->mxchat_extract_main_content($html);
7784 - $sanitized = $this->mxchat_sanitize_content_for_api($content);
7785 -
7786 - if (empty($sanitized)) {
7787 - // Not an error - just no content found (maybe a redirect or empty page)
7788 - return false;
7789 - }
7790 -
7791 - // Submit to database with content_type
7792 - $result = MxChat_Utils::submit_content_to_db(
7793 - $sanitized,
7794 - $url,
7795 - $api_key,
7796 - null,
7797 - $bot_id,
7798 - $content_type
7799 - );
7800 -
7801 - return $result;
7802 -}
7803 -
7804 -/**
7805 - * Expand a PDF URL into per-page queue items using the standard PDF pipeline.
7806 - * Called when a sitemap URL turns out to be a PDF — downloads, parses page count,
7807 - * and adds pdf_page items to the same queue so they process with full progress tracking.
7808 - */
7809 -private function mxchat_expand_pdf_to_queue($pdf_url, $response, $bot_id = 'default', $queue_id = '') {
7810 - set_time_limit(120); // PDFs need extra time for download + parsing
7811 -
7812 - $upload_dir = wp_upload_dir();
7813 - $pdf_filename = sanitize_file_name('mxchat_kb_' . md5($pdf_url) . '.pdf');
7814 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
7815 -
7816 - $response_body = wp_remote_retrieve_body($response);
7817 - if (empty($response_body)) {
7818 - return new WP_Error('empty_pdf', 'Empty PDF response for: ' . $pdf_url);
7819 - }
7820 -
7821 - if (!wp_mkdir_p(dirname($pdf_path))) {
7822 - return new WP_Error('dir_error', 'Failed to create upload directory');
7823 - }
7824 -
7825 - file_put_contents($pdf_path, $response_body);
7826 -
7827 - if (!file_exists($pdf_path)) {
7828 - return new WP_Error('save_error', 'Failed to save PDF file');
7829 - }
7830 -
7831 - try {
7832 - $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
7833 -
7834 - if ($total_pages === false || $total_pages < 1) {
7835 - wp_delete_file($pdf_path);
7836 - return new WP_Error('no_pages', 'PDF has no pages: ' . $pdf_url);
7837 - }
7838 -
7839 - // Build per-page items identical to mxchat_handle_pdf_for_knowledge_base
7840 - $pages = array();
7841 - for ($i = 1; $i <= $total_pages; $i++) {
7842 - $pages[] = array(
7843 - 'pdf_path' => $pdf_path,
7844 - 'pdf_url' => $pdf_url,
7845 - 'page_number' => $i,
7846 - 'total_pages' => $total_pages
7847 - );
7848 - }
7849 -
7850 - // Add pdf_page items to the SAME queue so the JS picks them up automatically
7851 - if (!empty($queue_id)) {
7852 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'pdf_page', $pages, $bot_id);
7853 - } else {
7854 - // Fallback: create a new PDF queue (shouldn't happen in sitemap flow)
7855 - $new_queue_id = 'pdf_' . md5($pdf_url . time());
7856 - $queued_count = $this->mxchat_add_to_queue($new_queue_id, 'pdf_page', $pages, $bot_id);
7857 - $this->mxchat_set_queue_meta($new_queue_id, 'source_url', $pdf_url);
7858 - $this->mxchat_set_queue_meta($new_queue_id, 'queue_type', 'pdf');
7859 - $this->mxchat_set_queue_meta($new_queue_id, 'total_items', $total_pages);
7860 - $this->mxchat_set_queue_meta($new_queue_id, 'bot_id', $bot_id);
7861 - $this->mxchat_set_queue_meta($new_queue_id, 'pdf_path', $pdf_path);
7862 - $this->mxchat_set_queue_meta($new_queue_id, 'created_at', current_time('mysql'));
7863 - }
7864 -
7865 - if ($queued_count === 0) {
7866 - wp_delete_file($pdf_path);
7867 - return new WP_Error('queue_error', 'Failed to add PDF pages to queue');
7868 - }
7869 -
7870 - // Return true so the original URL item is marked complete
7871 - // The new pdf_page items will be processed in subsequent batches
7872 - return true;
7873 -
7874 - } catch (Exception $e) {
7875 - if (file_exists($pdf_path)) {
7876 - wp_delete_file($pdf_path);
7877 - }
7878 - return new WP_Error('pdf_parse_error', 'Error parsing PDF: ' . $e->getMessage());
7879 - }
7880 -}
7881 -
7882 -/**
7883 - * Legacy: Process a PDF URL inline during sitemap queue processing.
7884 - * @deprecated Use mxchat_expand_pdf_to_queue instead — kept for reference only.
7885 - */
7886 -private function mxchat_process_pdf_url_inline($pdf_url, $response, $api_key, $bot_id = 'default') {
7887 - set_time_limit(120); // PDFs need more time — downloading + parsing all pages
7888 -
7889 - $upload_dir = wp_upload_dir();
7890 - $pdf_filename = sanitize_file_name('mxchat_kb_' . md5($pdf_url) . '.pdf');
7891 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
7892 -
7893 - $response_body = wp_remote_retrieve_body($response);
7894 - if (empty($response_body)) {
7895 - return new WP_Error('empty_pdf', 'Empty PDF response');
7896 - }
7897 -
7898 - if (!wp_mkdir_p(dirname($pdf_path))) {
7899 - return new WP_Error('dir_error', 'Failed to create upload directory');
7900 - }
7901 -
7902 - file_put_contents($pdf_path, $response_body);
7903 -
7904 - if (!file_exists($pdf_path)) {
7905 - return new WP_Error('save_error', 'Failed to save PDF file');
7906 - }
7907 -
7908 - try {
7909 - mxchat_load_pdf_parser();
7910 - $parser = new \Smalot\PdfParser\Parser();
7911 - $pdf = $parser->parseFile($pdf_path);
7912 - $pages = $pdf->getPages();
7913 - $total_pages = count($pages);
7914 -
7915 - if ($total_pages < 1) {
7916 - wp_delete_file($pdf_path);
7917 - return new WP_Error('no_pages', 'PDF has no pages');
7918 - }
7919 -
7920 - $processed = 0;
7921 - $skipped_pages = array();
7922 -
7923 - for ($i = 0; $i < $total_pages; $i++) {
7924 - $page_num = $i + 1;
7925 - $text = $pages[$i]->getText();
7926 - if (empty($text)) {
7927 - $skipped_pages[] = 'Page ' . $page_num . ': No text could be extracted — page may contain only images, links, or non-standard encoding';
7928 - continue;
7929 - }
7930 -
7931 - $sanitized = $this->mxchat_sanitize_content_for_api($text);
7932 - if (empty($sanitized)) {
7933 - $skipped_pages[] = 'Page ' . $page_num . ': Text was extracted but contained only special characters, control codes, or unsupported content';
7934 - continue;
7935 - }
7936 -
7937 - $metadata = array(
7938 - 'document_type' => 'pdf',
7939 - 'total_pages' => $total_pages,
7940 - 'current_page' => $page_num,
7941 - 'source_url' => $pdf_url,
7942 - );
7943 -
7944 - $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized;
7945 - $page_url = esc_url($pdf_url . '#page=' . $page_num);
7946 -
7947 - MxChat_Utils::submit_content_to_db(
7948 - $content_with_metadata,
7949 - $page_url,
7950 - $api_key,
7951 - null,
7952 - $bot_id,
7953 - 'pdf'
7954 - );
7955 -
7956 - $processed++;
7957 - }
7958 -
7959 - // Clean up the temp PDF file
7960 - wp_delete_file($pdf_path);
7961 -
7962 - if (!empty($skipped_pages)) {
7963 - error_log('MxChat PDF: Skipped ' . count($skipped_pages) . ' of ' . $total_pages . ' pages: ' . implode('; ', $skipped_pages));
7964 - }
7965 -
7966 - return $processed > 0 ? true : false;
7967 -
7968 - } catch (Exception $e) {
7969 - if (file_exists($pdf_path)) {
7970 - wp_delete_file($pdf_path);
7971 - }
7972 - return new WP_Error('pdf_parse_error', 'Error parsing PDF: ' . $e->getMessage());
7973 - }
7974 -}
7975 -
7976 -/**
7977 - * Extract WooCommerce product content including pricing
7978 - *
7979 - * @param string $url The product URL
7980 - * @return string|false Product content with pricing, or false if not found
7981 - */
7982 -private function mxchat_extract_woocommerce_product_content($url) {
7983 - // Try to get product ID from URL
7984 - $product_id = url_to_postid($url);
7985 -
7986 - // If url_to_postid fails, try to extract from URL pattern
7987 - if (!$product_id) {
7988 - $product_slug = '';
7989 -
7990 - // Handle pretty permalinks: /product/product-name/
7991 - if (preg_match('/\/product\/([^\/\?]+)/', $url, $matches)) {
7992 - $product_slug = $matches[1];
7993 - }
7994 -
7995 - if (!empty($product_slug)) {
7996 - $product_post = get_page_by_path($product_slug, OBJECT, 'product');
7997 - if ($product_post) {
7998 - $product_id = $product_post->ID;
7999 - }
8000 - }
8001 - }
8002 -
8003 - if (!$product_id) {
8004 - return false;
8005 - }
8006 -
8007 - // Get WooCommerce product object
8008 - $product = wc_get_product($product_id);
8009 -
8010 - if (!$product) {
8011 - return false;
8012 - }
8013 -
8014 - // Build product content with pricing (similar to mxchat_store_product_embedding)
8015 - $title = $product->get_name();
8016 - $description = $product->get_description();
8017 - $short_description = $product->get_short_description();
8018 - $sku = $product->get_sku();
8019 -
8020 - // Get pricing information
8021 - $regular_price = $product->get_regular_price();
8022 - $sale_price = $product->get_sale_price();
8023 - $price = $product->get_price(); // Current active price
8024 -
8025 - // Get currency symbol
8026 - $currency_symbol = get_woocommerce_currency_symbol();
8027 -
8028 - // Format content
8029 - $content = $title . "\n\n";
8030 -
8031 - if (!empty($short_description)) {
8032 - $content .= "Short Description: " . wp_strip_all_tags($short_description) . "\n\n";
8033 - }
8034 -
8035 - if (!empty($description)) {
8036 - $content .= wp_strip_all_tags($description) . "\n\n";
8037 - }
8038 -
8039 - // Add pricing information
8040 - if (!empty($regular_price)) {
8041 - $content .= "Price: " . $currency_symbol . $regular_price . "\n";
8042 - } elseif (!empty($price)) {
8043 - $content .= "Price: " . $currency_symbol . $price . "\n";
8044 - }
8045 -
8046 - if (!empty($sale_price) && $sale_price !== $regular_price) {
8047 - $content .= "Sale Price: " . $currency_symbol . $sale_price . "\n";
8048 - }
8049 -
8050 - // Handle variable products - show price range
8051 - if ($product->is_type('variable')) {
8052 - $min_price = $product->get_variation_price('min');
8053 - $max_price = $product->get_variation_price('max');
8054 - if ($min_price !== $max_price) {
8055 - $content .= "Price Range: " . $currency_symbol . $min_price . " - " . $currency_symbol . $max_price . "\n";
8056 - }
8057 - }
8058 -
8059 - if (!empty($sku)) {
8060 - $content .= "SKU: " . $sku . "\n";
8061 - }
8062 -
8063 - // Get product categories
8064 - $categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'names'));
8065 - if (!empty($categories) && !is_wp_error($categories)) {
8066 - $content .= "Categories: " . implode(', ', $categories) . "\n";
8067 - }
8068 -
8069 - // Get Custom Product Tabs (supports "Custom Product Tabs for WooCommerce" by Code Parrots)
8070 - $custom_tabs = get_post_meta($product_id, 'yikes_woo_products_tabs', true);
8071 - if (!empty($custom_tabs) && is_array($custom_tabs)) {
8072 - foreach ($custom_tabs as $tab) {
8073 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
8074 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
8075 -
8076 - if (!empty($tab_title) && !empty($tab_content)) {
8077 - $content .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
8078 - }
8079 - }
8080 - }
8081 -
8082 - // Also check for reusable/saved tabs applied to this product
8083 - $applied_saved_tabs = get_post_meta($product_id, 'yikes_woo_reusable_products_tabs_applied', true);
8084 - if (!empty($applied_saved_tabs) && is_array($applied_saved_tabs)) {
8085 - $saved_tabs = get_option('yikes_woo_reusable_products_tabs', array());
8086 - if (!empty($saved_tabs) && is_array($saved_tabs)) {
8087 - foreach ($applied_saved_tabs as $saved_tab_id) {
8088 - if (isset($saved_tabs[$saved_tab_id])) {
8089 - $tab = $saved_tabs[$saved_tab_id];
8090 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
8091 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
8092 -
8093 - if (!empty($tab_title) && !empty($tab_content)) {
8094 - $content .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
8095 - }
8096 - }
8097 - }
8098 - }
8099 - }
8100 -
8101 - return $this->mxchat_sanitize_content_for_api($content);
8102 -}
8103 -
8104 -/**
8105 - * Process a PDF page from the queue
8106 - */
8107 -private function mxchat_process_queue_pdf_page($item_data, $bot_id = 'default') {
8108 - $pdf_path = isset($item_data['pdf_path']) ? $item_data['pdf_path'] : '';
8109 - $pdf_url = isset($item_data['pdf_url']) ? $item_data['pdf_url'] : '';
8110 - $page_number = isset($item_data['page_number']) ? absint($item_data['page_number']) : 0;
8111 - $total_pages = isset($item_data['total_pages']) ? absint($item_data['total_pages']) : 0;
8112 -
8113 - if (empty($pdf_path) || !file_exists($pdf_path)) {
8114 - return new WP_Error('pdf_not_found', 'PDF file not found: ' . $pdf_path);
8115 - }
8116 -
8117 - if ($page_number < 1) {
8118 - return new WP_Error('invalid_page', 'Invalid page number');
8119 - }
8120 -
8121 - try {
8122 - mxchat_load_pdf_parser();
8123 - $parser = new \Smalot\PdfParser\Parser();
8124 - $pdf = $parser->parseFile($pdf_path);
8125 - $pages = $pdf->getPages();
8126 -
8127 - if (!isset($pages[$page_number - 1])) {
8128 - return new WP_Error('page_not_found', 'Page ' . $page_number . ' not found in PDF');
8129 - }
8130 -
8131 - $text = $pages[$page_number - 1]->getText();
8132 -
8133 - if (empty($text)) {
8134 - return new WP_Error('empty_page', 'Page ' . $page_number . ': No text could be extracted — page may contain only images, links, or non-standard encoding');
8135 - }
8136 -
8137 - $sanitized = $this->mxchat_sanitize_content_for_api($text);
8138 -
8139 - if (empty($sanitized)) {
8140 - return new WP_Error('empty_after_sanitization', 'Page ' . $page_number . ': Text was extracted but contained only special characters, control codes, or unsupported content that was removed during cleanup');
8141 - }
8142 -
8143 - // Create metadata
8144 - $metadata = array(
8145 - 'document_type' => 'pdf',
8146 - 'total_pages' => $total_pages,
8147 - 'current_page' => $page_number,
8148 - 'source_url' => $pdf_url
8149 - );
8150 -
8151 - $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized;
8152 - $page_url = esc_url($pdf_url . "#page=" . $page_number);
8153 -
8154 - // Get bot-specific API key
8155 - $bot_options = $this->get_bot_options($bot_id);
8156 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
8157 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
8158 -
8159 - if (strpos($selected_model, 'voyage') === 0) {
8160 - $api_key = $options['voyage_api_key'] ?? '';
8161 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
8162 - $api_key = $options['gemini_api_key'] ?? '';
8163 - } else {
8164 - $api_key = $options['api_key'] ?? '';
8165 - }
8166 -
8167 - if (empty($api_key)) {
8168 - return new WP_Error('no_api_key', 'API key not configured for bot: ' . $bot_id);
8169 - }
8170 -
8171 - // Submit to database - UPDATED 2.5.6: Added content_type 'pdf'
8172 - $result = MxChat_Utils::submit_content_to_db(
8173 - $content_with_metadata,
8174 - $page_url,
8175 - $api_key,
8176 - null,
8177 - $bot_id,
8178 - 'pdf'
8179 - );
8180 -
8181 - return $result;
8182 -
8183 - } catch (Exception $e) {
8184 - return new WP_Error('pdf_parse_error', 'Error parsing PDF: ' . $e->getMessage());
8185 - }
8186 -}
8187 -
8188 -/**
8189 - * AJAX: Get queue processing status
8190 - */
8191 -public function ajax_mxchat_get_queue_status() {
8192 - // Verify nonce and permissions
8193 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
8194 -
8195 - if (!current_user_can('manage_options')) {
8196 - wp_send_json_error('Unauthorized access');
8197 - }
8198 -
8199 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
8200 -
8201 - if (empty($queue_id)) {
8202 - wp_send_json_error('Missing queue ID');
8203 - }
8204 -
8205 - global $wpdb;
8206 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
8207 -
8208 - // Get counts by status
8209 - $counts = $wpdb->get_results($wpdb->prepare(
8210 - "SELECT status, COUNT(*) as count
8211 - FROM $table_name
8212 - WHERE queue_id = %s
8213 - GROUP BY status",
8214 - $queue_id
8215 - ), OBJECT_K);
8216 -
8217 - $total = 0;
8218 - $completed = 0;
8219 - $failed = 0;
8220 - $processing = 0;
8221 - $pending = 0;
8222 -
8223 - foreach ($counts as $status => $data) {
8224 - $count = absint($data->count);
8225 - $total += $count;
8226 -
8227 - switch ($status) {
8228 - case 'completed':
8229 - $completed = $count;
8230 - break;
8231 - case 'failed':
8232 - $failed = $count;
8233 - break;
8234 - case 'processing':
8235 - $processing = $count;
8236 - break;
8237 - case 'pending':
8238 - $pending = $count;
8239 - break;
8240 - }
8241 - }
8242 -
8243 - // Calculate percentage
8244 - $percentage = $total > 0 ? round((($completed + $failed) / $total) * 100) : 0;
8245 -
8246 - // Get failed items details (include all failed items, not just those that exhausted retries)
8247 - $failed_items = array();
8248 - if ($failed > 0) {
8249 - $failed_items = $wpdb->get_results($wpdb->prepare(
8250 - "SELECT item_type, item_data, error_message, attempts
8251 - FROM $table_name
8252 - WHERE queue_id = %s
8253 - AND status = 'failed'
8254 - ORDER BY id DESC
8255 - LIMIT 50",
8256 - $queue_id
8257 - ));
8258 - }
8259 -
8260 - // Get queue metadata
8261 - $source_url = $this->mxchat_get_queue_meta($queue_id, 'source_url');
8262 - $queue_type = $this->mxchat_get_queue_meta($queue_id, 'queue_type');
8263 -
8264 - // Determine if queue is complete
8265 - $is_complete = ($pending === 0 && $processing === 0);
8266 -
8267 - wp_send_json_success(array(
8268 - 'queue_id' => $queue_id,
8269 - 'queue_type' => $queue_type,
8270 - 'source_url' => $source_url,
8271 - 'total' => $total,
8272 - 'completed' => $completed,
8273 - 'failed' => $failed,
8274 - 'processing' => $processing,
8275 - 'pending' => $pending,
8276 - 'percentage' => $percentage,
8277 - 'is_complete' => $is_complete,
8278 - 'failed_items' => $failed_items,
8279 - 'status' => $is_complete ? 'complete' : 'processing'
8280 - ));
8281 -}
8282 -
8283 -/**
8284 - * AJAX: Clear completed queue
8285 - */
8286 -public function ajax_mxchat_clear_queue() {
8287 - // Verify nonce and permissions
8288 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
8289 -
8290 - if (!current_user_can('manage_options')) {
8291 - wp_send_json_error('Unauthorized access');
8292 - }
8293 -
8294 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
8295 -
8296 - if (empty($queue_id)) {
8297 - wp_send_json_error('Missing queue ID');
8298 - }
8299 -
8300 - global $wpdb;
8301 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
8302 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
8303 -
8304 - // Delete queue items
8305 - $wpdb->delete(
8306 - $table_name,
8307 - array('queue_id' => $queue_id),
8308 - array('%s')
8309 - );
8310 -
8311 - // Delete queue metadata
8312 - $wpdb->delete(
8313 - $meta_table,
8314 - array('queue_id' => $queue_id),
8315 - array('%s')
8316 - );
8317 -
8318 - wp_send_json_success(array(
8319 - 'message' => 'Queue cleared successfully'
8320 - ));
8321 -}
8322 -
8323 -/**
8324 - * AJAX: Retry failed items in queue
8325 - */
8326 -public function ajax_mxchat_retry_failed() {
8327 - // Verify nonce and permissions
8328 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
8329 -
8330 - if (!current_user_can('manage_options')) {
8331 - wp_send_json_error('Unauthorized access');
8332 - }
8333 -
8334 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
8335 -
8336 - if (empty($queue_id)) {
8337 - wp_send_json_error('Missing queue ID');
8338 - }
8339 -
8340 - global $wpdb;
8341 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
8342 -
8343 - // Reset failed items to pending and reset attempt count
8344 - $updated = $wpdb->update(
8345 - $table_name,
8346 - array(
8347 - 'status' => 'pending',
8348 - 'attempts' => 0,
8349 - 'error_message' => null
8350 - ),
8351 - array(
8352 - 'queue_id' => $queue_id,
8353 - 'status' => 'failed'
8354 - ),
8355 - array('%s', '%d', '%s'),
8356 - array('%s', '%s')
8357 - );
8358 -
8359 - wp_send_json_success(array(
8360 - 'message' => 'Reset ' . $updated . ' failed items for retry',
8361 - 'reset_count' => $updated
8362 - ));
8363 -}
8364 -
8365 -
8366 -public function ajax_mxchat_mark_queue_complete() {
8367 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
8368 -
8369 - if (!current_user_can('manage_options')) {
8370 - wp_send_json_error('Unauthorized access');
8371 - }
8372 -
8373 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
8374 -
8375 - if (empty($queue_id)) {
8376 - wp_send_json_error('Missing queue ID');
8377 - }
8378 -
8379 - // Clear active queue transients
8380 - if (strpos($queue_id, 'sitemap_') === 0) {
8381 - delete_transient('mxchat_active_queue_sitemap');
8382 - } else if (strpos($queue_id, 'pdf_') === 0) {
8383 - delete_transient('mxchat_active_queue_pdf');
8384 - }
8385 -
8386 - wp_send_json_success(array('message' => 'Queue marked as complete'));
8387 -}
8388 -
8389 -
8390 - // ========================================
8391 - // STATIC ACCESS METHODS
8392 - // ========================================
8393 -
8394 - /**
8395 - * Get singleton instance
8396 - */
8397 - public static function get_instance() {
8398 - static $instance = null;
8399 - if ($instance === null) {
8400 - $instance = new self();
8401 - }
8402 - return $instance;
8403 - }
8404 -}
8405 -
8406 -// Initialize the Knowledge manager
1 +<?php
2 +/**
3 + * File: admin/class-knowledge-manager.php
4 + *
5 + * Handles all knowledge base content processing for MxChat
6 + * Including PDF, sitemap, content processing, and WordPress post management
7 + */
8 +if (!defined('ABSPATH')) {
9 + exit; // Exit if accessed directly
10 +}
11 +
12 +class MxChat_Knowledge_Manager {
13 +
14 + private $options;
15 +
16 + /**
17 + * Constructor - Register hooks for content processing
18 + */
19 +public function __construct() {
20 + $this->options = get_option('mxchat_options', array());
21 + $this->mxchat_init_hooks();
22 +
23 + $this->mxchat_init_role_hooks();
24 +}
25 +
26 +/**
27 + * Initialize WordPress hooks for content processing
28 + *
29 + */
30 +private function mxchat_init_hooks() {
31 + // Admin post handlers for form submissions
32 + add_action('admin_post_mxchat_submit_content', array($this, 'mxchat_handle_content_submission'));
33 + add_action('admin_post_mxchat_submit_sitemap', array($this, 'mxchat_handle_sitemap_submission'));
34 + add_action('admin_post_mxchat_stop_processing', array($this, 'mxchat_stop_processing'));
35 +
36 + // AJAX handlers for real-time processing and status updates
37 + add_action('wp_ajax_mxchat_get_status_updates', array($this, 'mxchat_ajax_get_status_updates'));
38 + add_action('wp_ajax_mxchat_dismiss_completed_status', array($this, 'mxchat_ajax_dismiss_completed_status'));
39 + add_action('wp_ajax_mxchat_get_content_list', array($this, 'ajax_mxchat_get_content_list'));
40 + add_action('wp_ajax_mxchat_process_selected_content', array($this, 'ajax_mxchat_process_selected_content'));
41 + add_action('wp_ajax_mxchat_save_inline_prompt', array($this, 'mxchat_save_inline_prompt'));
42 + add_action('admin_post_mxchat_delete_pinecone_prompt', array($this, 'mxchat_handle_pinecone_prompt_delete'));
43 + add_action('wp_ajax_mxchat_delete_pinecone_prompt', array($this, 'ajax_mxchat_delete_pinecone_prompt'));
44 + add_action('wp_ajax_mxchat_update_role_restriction', array($this, 'ajax_mxchat_update_role_restriction'));
45 +
46 + // Queue-based processing AJAX handlers
47 + add_action('wp_ajax_mxchat_get_next_queue_item', array($this, 'ajax_mxchat_get_next_queue_item'));
48 + add_action('wp_ajax_mxchat_process_queue_item', array($this, 'ajax_mxchat_process_queue_item'));
49 + add_action('wp_ajax_mxchat_get_queue_status', array($this, 'ajax_mxchat_get_queue_status'));
50 + add_action('wp_ajax_mxchat_clear_queue', array($this, 'ajax_mxchat_clear_queue'));
51 + add_action('wp_ajax_mxchat_retry_failed', array($this, 'ajax_mxchat_retry_failed'));
52 +
53 + // Hook for content deletion
54 + add_action('mxchat_delete_content', array($this, 'mxchat_delete_from_pinecone_by_url'), 10, 1);
55 +
56 + // WordPress post management hooks
57 + add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2);
58 + add_action('post_updated', array($this, 'mxchat_handle_post_update'), 10, 3);
59 + add_action('before_delete_post', array($this, 'mxchat_handle_post_delete'));
60 + add_action('wp_trash_post', array($this, 'mxchat_handle_post_delete'));
61 +
62 + add_action('wp_ajax_mxchat_mark_queue_complete', array($this, 'ajax_mxchat_mark_queue_complete'));
63 +
64 + // WooCommerce product hooks (if WooCommerce is active)
65 + if (class_exists('WooCommerce')) {
66 + add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2);
67 + add_action('save_post_product', array($this, 'mxchat_handle_product_change'), 10, 3);
68 + add_action('wp_trash_post', array($this, 'mxchat_handle_product_delete'));
69 + add_action('before_delete_post', array($this, 'mxchat_handle_product_delete'));
70 + }
71 +}
72 +
73 + /**
74 + * Get current options (refreshed)
75 + */
76 + private function mxchat_get_options() {
77 + if (empty($this->options)) {
78 + $this->options = get_option('mxchat_options', array());
79 + }
80 + return $this->options;
81 + }
82 +
83 +
84 + // ========================================
85 + // MAIN CONTENT SUBMISSION HANDLERS
86 + // ========================================
87 +
88 +public function mxchat_handle_content_submission() {
89 + // Check if the form was submitted and the user has permission.
90 + if (!isset($_POST['submit_content']) || !current_user_can('manage_options')) {
91 + return;
92 + }
93 +
94 + // Verify the nonce.
95 + $nonce = isset($_POST['mxchat_submit_content_nonce']) ? sanitize_text_field(wp_unslash($_POST['mxchat_submit_content_nonce'])) : '';
96 + if (!wp_verify_nonce($nonce, 'mxchat_submit_content_action')) {
97 + wp_die(esc_html__('Nonce verification failed.', 'mxchat'));
98 + }
99 +
100 + // Sanitize the inputs.
101 + // Use wp_kses_post to allow safe HTML and wp_unslash to remove WordPress added slashes
102 + $article_content = wp_kses_post(wp_unslash($_POST['article_content']));
103 + $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
104 +
105 + // Get bot_id from form submission
106 + $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
107 +
108 + // Get bot-specific options and API key
109 + $bot_options = $this->get_bot_options($bot_id);
110 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
111 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
112 +
113 + if (strpos($selected_model, 'voyage') === 0) {
114 + $api_key = $options['voyage_api_key'] ?? '';
115 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
116 + $api_key = $options['gemini_api_key'] ?? '';
117 + } else {
118 + $api_key = $options['api_key'] ?? '';
119 + }
120 +
121 + if (empty($api_key)) {
122 + set_transient('mxchat_admin_notice_error',
123 + esc_html__('API key is not configured. Please add your API key in the settings before submitting content.', 'mxchat'),
124 + 30
125 + );
126 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
127 + exit;
128 + }
129 +
130 + // Use centralized utility function with bot_id
131 + $result = MxChat_Utils::submit_content_to_db($article_content, $article_url, $api_key, null, $bot_id);
132 +
133 + if (is_wp_error($result)) {
134 + set_transient('mxchat_admin_notice_error',
135 + esc_html__('Error storing content: ', 'mxchat') . $result->get_error_message(),
136 + 30
137 + );
138 + } else {
139 + set_transient('mxchat_admin_notice_success',
140 + esc_html__('Content successfully submitted!', 'mxchat'),
141 + 30
142 + );
143 + }
144 +
145 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
146 + exit;
147 +}
148 +
149 +public function mxchat_is_pdf_url($url, $response) {
150 + $content_type = wp_remote_retrieve_header($response, 'content-type');
151 + $file_extension = strtolower(pathinfo($url, PATHINFO_EXTENSION));
152 +
153 + return strpos($content_type, 'pdf') !== false || $file_extension === 'pdf';
154 +}
155 +
156 +
157 +public function mxchat_handle_pdf_for_knowledge_base($pdf_url, $response, $bot_id = 'default') {
158 + if (!current_user_can('manage_options')) {
159 + return false;
160 + }
161 +
162 + $pdf_url = esc_url_raw($pdf_url);
163 + $upload_dir = wp_upload_dir();
164 +
165 + if (isset($upload_dir['error']) && $upload_dir['error'] !== false) {
166 + return false;
167 + }
168 +
169 + $pdf_filename = sanitize_file_name('mxchat_kb_' . time() . '.pdf');
170 + $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
171 +
172 + $response_body = wp_remote_retrieve_body($response);
173 + if (empty($response_body)) {
174 + return false;
175 + }
176 +
177 + if (!wp_mkdir_p(dirname($pdf_path))) {
178 + return false;
179 + }
180 +
181 + try {
182 + file_put_contents($pdf_path, $response_body);
183 +
184 + if (!file_exists($pdf_path)) {
185 + throw new Exception(__('Failed to save PDF file', 'mxchat'));
186 + }
187 +
188 + $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
189 +
190 + if ($total_pages === false || $total_pages < 1) {
191 + throw new Exception(__('Invalid PDF: Unable to parse or no pages found', 'mxchat'));
192 + }
193 +
194 + // Create unique queue ID
195 + $queue_id = 'pdf_' . md5($pdf_url . time());
196 +
197 + // Create array of pages to process
198 + $pages = array();
199 + for ($i = 1; $i <= $total_pages; $i++) {
200 + $pages[] = array(
201 + 'pdf_path' => $pdf_path,
202 + 'pdf_url' => $pdf_url,
203 + 'page_number' => $i,
204 + 'total_pages' => $total_pages
205 + );
206 + }
207 +
208 + // Add pages to queue
209 + $queued_count = $this->mxchat_add_to_queue($queue_id, 'pdf_page', $pages, $bot_id);
210 +
211 + if ($queued_count === 0) {
212 + wp_delete_file($pdf_path);
213 + throw new Exception(__('Failed to add PDF pages to processing queue', 'mxchat'));
214 + }
215 +
216 + // Store queue metadata
217 + $this->mxchat_set_queue_meta($queue_id, 'source_url', $pdf_url);
218 + $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'pdf');
219 + $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_pages);
220 + $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
221 + $this->mxchat_set_queue_meta($queue_id, 'pdf_path', $pdf_path);
222 + $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
223 +
224 + // Store queue ID in transient for status tracking
225 + set_transient('mxchat_active_queue_pdf', $queue_id, DAY_IN_SECONDS);
226 + set_transient('mxchat_last_pdf_url', $pdf_url, DAY_IN_SECONDS);
227 +
228 + return 'queued';
229 +
230 + } catch (Exception $e) {
231 + if (file_exists($pdf_path)) {
232 + wp_delete_file($pdf_path);
233 + }
234 + return $e->getMessage();
235 + }
236 +}
237 +
238 +/**
239 + * Validate PDF and count pages with multiple parser attempts
240 + */
241 +private function mxchat_validate_and_count_pdf_pages($pdf_path) {
242 + // Method 1: Try with Smalot PDF Parser (your current method)
243 + try {
244 + $parser = new \Smalot\PdfParser\Parser();
245 + $pdf = $parser->parseFile($pdf_path);
246 + $pages = $pdf->getPages();
247 + $page_count = count($pages);
248 +
249 + if ($page_count > 0) {
250 + //error_log('PDF parsed successfully with Smalot parser: ' . $page_count . ' pages');
251 + return $page_count;
252 + }
253 + } catch (Exception $e) {
254 + //error_log('Smalot PDF parser failed: ' . $e->getMessage());
255 + }
256 +
257 + // Method 2: Try with pdfinfo command (if available)
258 + if (function_exists('shell_exec') && !$this->mxchat_is_shell_disabled()) {
259 + try {
260 + $command = 'pdfinfo ' . escapeshellarg($pdf_path) . ' 2>&1';
261 + $output = shell_exec($command);
262 +
263 + if ($output && preg_match('/Pages:\s*(\d+)/', $output, $matches)) {
264 + $page_count = intval($matches[1]);
265 + if ($page_count > 0) {
266 + //error_log('PDF parsed successfully with pdfinfo: ' . $page_count . ' pages');
267 + return $page_count;
268 + }
269 + }
270 + } catch (Exception $e) {
271 + //error_log('pdfinfo command failed: ' . $e->getMessage());
272 + }
273 + }
274 +
275 + // Method 3: Try to repair PDF and parse again
276 + try {
277 + $repaired_path = $this->mxchat_attempt_pdf_repair($pdf_path);
278 + if ($repaired_path && $repaired_path !== $pdf_path) {
279 + $parser = new \Smalot\PdfParser\Parser();
280 + $pdf = $parser->parseFile($repaired_path);
281 + $pages = $pdf->getPages();
282 + $page_count = count($pages);
283 +
284 + if ($page_count > 0) {
285 + // Replace original with repaired version
286 + copy($repaired_path, $pdf_path);
287 + unlink($repaired_path);
288 + //error_log('PDF repaired and parsed successfully: ' . $page_count . ' pages');
289 + return $page_count;
290 + }
291 +
292 + // Clean up repaired file if it didn't work
293 + unlink($repaired_path);
294 + }
295 + } catch (Exception $e) {
296 + //error_log('PDF repair attempt failed: ' . $e->getMessage());
297 + }
298 +
299 + // Method 4: Manual PDF structure analysis (basic page count)
300 + try {
301 + $page_count = $this->mxchat_manual_pdf_page_count($pdf_path);
302 + if ($page_count > 0) {
303 + //error_log('PDF page count determined manually: ' . $page_count . ' pages');
304 + return $page_count;
305 + }
306 + } catch (Exception $e) {
307 + //error_log('Manual PDF analysis failed: ' . $e->getMessage());
308 + }
309 +
310 + //error_log('All PDF parsing methods failed for: ' . $pdf_path);
311 + return false;
312 +}
313 +
314 +/**
315 + * Check if shell_exec is disabled
316 + */
317 +private function mxchat_is_shell_disabled() {
318 + $disabled = explode(',', ini_get('disable_functions'));
319 + return in_array('shell_exec', $disabled);
320 +}
321 +
322 +/**
323 + * Attempt to repair PDF using basic methods
324 + */
325 +private function mxchat_attempt_pdf_repair($pdf_path) {
326 + try {
327 + $content = file_get_contents($pdf_path);
328 + if (!$content) {
329 + return false;
330 + }
331 +
332 + // Check if PDF starts with proper header
333 + if (substr($content, 0, 4) !== '%PDF') {
334 + // Try to find PDF header in the content
335 + $header_pos = strpos($content, '%PDF');
336 + if ($header_pos !== false && $header_pos < 1024) {
337 + // Remove junk before PDF header
338 + $content = substr($content, $header_pos);
339 + $repaired_path = $pdf_path . '.repaired';
340 + file_put_contents($repaired_path, $content);
341 + return $repaired_path;
342 + }
343 + }
344 +
345 + // Check for EOF marker
346 + $content = rtrim($content);
347 + if (!preg_match('/%%EOF\s*$/', $content)) {
348 + // Add EOF marker if missing
349 + $content .= "\n%%EOF";
350 + $repaired_path = $pdf_path . '.repaired';
351 + file_put_contents($repaired_path, $content);
352 + return $repaired_path;
353 + }
354 +
355 + } catch (Exception $e) {
356 + //error_log('PDF repair error: ' . $e->getMessage());
357 + }
358 +
359 + return false;
360 +}
361 +
362 +/**
363 + * Manual PDF page counting by analyzing PDF structure
364 + */
365 +private function mxchat_manual_pdf_page_count($pdf_path) {
366 + try {
367 + $content = file_get_contents($pdf_path);
368 + if (!$content) {
369 + return 0;
370 + }
371 +
372 + // Method 1: Count /Type /Page objects
373 + $page_count = preg_match_all('/\/Type\s*\/Page[^s]/', $content);
374 + if ($page_count > 0) {
375 + return $page_count;
376 + }
377 +
378 + // Method 2: Look for /Count in pages object
379 + if (preg_match('/\/Type\s*\/Pages.*?\/Count\s+(\d+)/', $content, $matches)) {
380 + return intval($matches[1]);
381 + }
382 +
383 + // Method 3: Count page references
384 + $page_count = preg_match_all('/\d+\s+0\s+obj\s*<<[^>]*\/Type\s*\/Page/', $content);
385 + if ($page_count > 0) {
386 + return $page_count;
387 + }
388 +
389 + } catch (Exception $e) {
390 + //error_log('Manual PDF analysis error: ' . $e->getMessage());
391 + }
392 +
393 + return 0;
394 +}
395 +
396 +
397 +public function mxchat_save_inline_prompt() {
398 + // DEBUG: Log what we're receiving
399 + //error_log('=== MXCHAT DEBUG ===');
400 + //error_log('POST data: ' . print_r($_POST, true));
401 + //error_log('Nonce from POST: ' . ($_POST['_ajax_nonce'] ?? 'NOT FOUND'));
402 +
403 + // Check for nonce security
404 + check_ajax_referer('mxchat_save_inline_nonce', '_ajax_nonce');
405 +
406 + // If we get here, nonce passed
407 + //error_log('Nonce verification PASSED');
408 +
409 + // Verify permissions
410 + if (!current_user_can('manage_options')) {
411 + wp_send_json_error(esc_html__('Permission denied.', 'mxchat'));
412 + return;
413 + }
414 +
415 + global $wpdb;
416 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
417 +
418 + // Validate and sanitize input data
419 + $prompt_id = isset($_POST['id']) ? absint($_POST['id']) : 0;
420 + $article_content = isset($_POST['article_content']) ? wp_kses_post(wp_unslash($_POST['article_content'])) : '';
421 + $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
422 +
423 + if ($prompt_id > 0 && !empty($article_content)) {
424 + // Re-generate the embedding vector for the updated content
425 + $embedding_vector = $this->mxchat_generate_embedding($article_content);
426 + if (is_array($embedding_vector)) {
427 + // Serialize the embedding vector before storing it
428 + $embedding_vector_serialized = serialize($embedding_vector);
429 + // Update the prompt in the database
430 + $updated = $wpdb->update(
431 + $table_name,
432 + array(
433 + 'article_content' => $article_content,
434 + 'embedding_vector' => $embedding_vector_serialized,
435 + 'source_url' => $article_url,
436 + ),
437 + array('id' => $prompt_id),
438 + array('%s', '%s', '%s'),
439 + array('%d')
440 + );
441 + if ($updated !== false) {
442 + wp_send_json_success();
443 + } else {
444 + wp_send_json_error(esc_html__('Database update failed.', 'mxchat'));
445 + }
446 + } else {
447 + wp_send_json_error(esc_html__('Embedding generation failed.', 'mxchat'));
448 + }
449 + } else {
450 + wp_send_json_error(esc_html__('Invalid data.', 'mxchat'));
451 + }
452 +}
453 +
454 +
455 +public function mxchat_get_pdf_processing_status($pdf_url) {
456 + $pdf_url = esc_url_raw($pdf_url);
457 + $status = get_transient(sanitize_key('mxchat_pdf_status_' . md5($pdf_url)));
458 +
459 + if (!$status || !is_array($status)) {
460 + return false;
461 + }
462 +
463 + // Check for stalled processing (no updates for 5 minutes)
464 + if ($status['status'] === 'processing' && (time() - absint($status['last_update'])) > 300) {
465 + $status['status'] = 'error';
466 + $status['error'] = __('PDF processing appears to be stalled. No updates for over 5 minutes.', 'mxchat');
467 +
468 + // Save the updated status
469 + set_transient(
470 + sanitize_key('mxchat_pdf_status_' . md5($pdf_url)),
471 + array_map('sanitize_text_field', $status),
472 + DAY_IN_SECONDS
473 + );
474 + }
475 +
476 + $result = array(
477 + 'total_pages' => absint($status['total_pages']),
478 + 'processed_pages' => absint($status['processed_pages']),
479 + 'failed_pages' => absint($status['failed_pages'] ?? 0),
480 + 'percentage' => ($status['total_pages'] > 0)
481 + ? round((absint($status['processed_pages']) / absint($status['total_pages'])) * 100)
482 + : 0,
483 + 'status' => sanitize_text_field($status['status']),
484 + 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
485 + 'failed_pages_list' => isset($status['failed_pages_list']) ? $status['failed_pages_list'] : array(),
486 + 'completion_summary' => isset($status['completion_summary']) ? $status['completion_summary'] : null
487 + );
488 +
489 + // Add error message if present
490 + if (isset($status['error']) && !empty($status['error'])) {
491 + $result['error'] = sanitize_text_field($status['error']);
492 + }
493 +
494 + return $result;
495 +}
496 +
497 +
498 +public function mxchat_handle_sitemap_submission() {
499 + // Check if the form was submitted and verify permissions
500 + if (!isset($_POST['submit_sitemap']) || !current_user_can('manage_options')) {
501 + wp_die(esc_html__('Unauthorized access', 'mxchat'));
502 + }
503 +
504 + // Verify nonce
505 + check_admin_referer('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce');
506 +
507 + // Validate URL
508 + if (!isset($_POST['sitemap_url']) || empty($_POST['sitemap_url'])) {
509 + set_transient('mxchat_admin_notice_error',
510 + esc_html__('Please provide a valid URL.', 'mxchat'),
511 + 30
512 + );
513 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
514 + exit;
515 + }
516 +
517 + $submitted_url = esc_url_raw($_POST['sitemap_url']);
518 +
519 + // Get bot_id from form submission
520 + $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
521 +
522 + // Get bot-specific options and validate API key
523 + $bot_options = $this->get_bot_options($bot_id);
524 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
525 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
526 +
527 + if (strpos($selected_model, 'voyage') === 0) {
528 + $api_key = $options['voyage_api_key'] ?? '';
529 + $provider_name = 'Voyage AI';
530 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
531 + $api_key = $options['gemini_api_key'] ?? '';
532 + $provider_name = 'Google Gemini';
533 + } else {
534 + $api_key = $options['api_key'] ?? '';
535 + $provider_name = 'OpenAI';
536 + }
537 +
538 + if (empty($api_key)) {
539 + $error_message = sprintf(
540 + esc_html__('%s API key is not configured. Please add your API key in the settings before submitting content.', 'mxchat'),
541 + $provider_name
542 + );
543 + set_transient('mxchat_admin_notice_error', $error_message, 30);
544 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
545 + exit;
546 + }
547 +
548 + // Fetch URL
549 + $response = wp_remote_get($submitted_url, array('timeout' => 30));
550 +
551 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
552 + $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP Status: ' . wp_remote_retrieve_response_code($response);
553 + set_transient('mxchat_admin_notice_error',
554 + sprintf(
555 + esc_html__('Failed to fetch the URL: %s', 'mxchat'),
556 + esc_html($error_message)
557 + ),
558 + 30
559 + );
560 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
561 + exit;
562 + }
563 +
564 + $content_type = wp_remote_retrieve_header($response, 'content-type');
565 + $body_content = wp_remote_retrieve_body($response);
566 +
567 + if (empty($body_content)) {
568 + set_transient('mxchat_admin_notice_error',
569 + esc_html__('Empty response received from URL.', 'mxchat'),
570 + 30
571 + );
572 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
573 + exit;
574 + }
575 +
576 + // Handle PDF URL
577 + if ($this->mxchat_is_pdf_url($submitted_url, $response)) {
578 + $result = $this->mxchat_handle_pdf_for_knowledge_base($submitted_url, $response, $bot_id);
579 +
580 + if ($result === 'queued') {
581 + set_transient('mxchat_admin_notice_success',
582 + esc_html__('PDF queued for processing. Processing will start automatically.', 'mxchat'),
583 + 30
584 + );
585 + } else {
586 + set_transient('mxchat_admin_notice_error',
587 + esc_html__('Failed to queue PDF processing: ', 'mxchat') . esc_html($result),
588 + 30
589 + );
590 + }
591 +
592 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
593 + exit;
594 + }
595 +
596 + // Handle Sitemap XML
597 + if (strpos($content_type, 'xml') !== false || strpos($body_content, '<urlset') !== false) {
598 + libxml_use_internal_errors(true);
599 + $xml = simplexml_load_string($body_content);
600 + $xml_errors = libxml_get_errors();
601 + libxml_clear_errors();
602 +
603 + if ($xml === false || !empty($xml_errors)) {
604 + set_transient('mxchat_admin_notice_error',
605 + esc_html__('Invalid sitemap XML. Please provide a valid sitemap.', 'mxchat'),
606 + 30
607 + );
608 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
609 + exit;
610 + }
611 +
612 + $result = $this->mxchat_handle_sitemap_for_knowledge_base($xml, $submitted_url, $bot_id);
613 +
614 + if ($result === 'queued') {
615 + set_transient('mxchat_admin_notice_success',
616 + esc_html__('Sitemap queued for processing. Processing will start automatically.', 'mxchat'),
617 + 30
618 + );
619 + } else {
620 + set_transient('mxchat_admin_notice_error',
621 + esc_html__('Failed to queue sitemap processing. Please check the status below for details.', 'mxchat'),
622 + 30
623 + );
624 + }
625 +
626 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
627 + exit;
628 + }
629 +
630 + // Handle Regular URL (single page)
631 + $page_content = $this->mxchat_extract_main_content($body_content);
632 + $sanitized_content = $this->mxchat_sanitize_content_for_api($page_content);
633 +
634 + if (empty($sanitized_content)) {
635 + set_transient('mxchat_admin_notice_error',
636 + esc_html__('No valid content found on the provided URL.', 'mxchat'),
637 + 30
638 + );
639 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
640 + exit;
641 + }
642 +
643 + // For single URLs, process immediately (not queued)
644 + $embedding_vector = $this->mxchat_generate_embedding($sanitized_content, $bot_id);
645 +
646 + if (is_string($embedding_vector)) {
647 + $error_message = esc_html__('Failed to generate embedding: ', 'mxchat') . esc_html($embedding_vector);
648 + set_transient('mxchat_admin_notice_error', $error_message, 30);
649 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
650 + exit;
651 + }
652 +
653 + if (is_array($embedding_vector)) {
654 + $db_result = MxChat_Utils::submit_content_to_db(
655 + $sanitized_content,
656 + $submitted_url,
657 + $api_key,
658 + null,
659 + $bot_id
660 + );
661 +
662 + if (is_wp_error($db_result)) {
663 + $error_message = esc_html__('Failed to store content in database: ', 'mxchat') . esc_html($db_result->get_error_message());
664 + set_transient('mxchat_admin_notice_error', $error_message, 30);
665 + } else {
666 + $success_message = esc_html__('URL content successfully submitted!', 'mxchat');
667 + set_transient('mxchat_admin_notice_success', $success_message, 30);
668 + }
669 + } else {
670 + $error_message = esc_html__('Failed to generate embedding: Unexpected result type. Please check your API key and try again.', 'mxchat');
671 + set_transient('mxchat_admin_notice_error', $error_message, 30);
672 + }
673 +
674 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
675 + exit;
676 +}
677 +
678 +
679 +public function mxchat_get_single_url_status() {
680 + $status = get_transient('mxchat_single_url_status');
681 + if (!$status) {
682 + return null;
683 + }
684 +
685 + // Add human-readable time
686 + if (isset($status['timestamp'])) {
687 + $status['human_time'] = human_time_diff(strtotime($status['timestamp']), current_time('timestamp')) . ' ' . __('ago', 'mxchat');
688 + }
689 +
690 + return $status;
691 +}
692 +
693 +public function mxchat_handle_sitemap_for_knowledge_base($xml, $sitemap_url, $bot_id = 'default') {
694 + if (!current_user_can('manage_options')) {
695 + return false;
696 + }
697 +
698 + try {
699 + $sitemap_url = esc_url_raw($sitemap_url);
700 +
701 + if (!$xml || !is_object($xml)) {
702 + throw new Exception(__('Invalid XML object provided', 'mxchat'));
703 + }
704 +
705 + // Get bot-specific embedding API for validation
706 + $bot_options = $this->get_bot_options($bot_id);
707 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
708 +
709 + // Test the embedding API before processing
710 + $test_phrase = "Test embedding generation for MxChat";
711 + $test_result = $this->mxchat_generate_embedding($test_phrase, $bot_id);
712 +
713 + if (is_string($test_result)) {
714 + throw new Exception(__('Embedding API validation failed: ', 'mxchat') . $test_result);
715 + }
716 +
717 + if (!is_array($test_result)) {
718 + throw new Exception(__('Embedding API returned unexpected result type. Please check your configuration.', 'mxchat'));
719 + }
720 +
721 + // Extract URLs from sitemap
722 + $urls = array();
723 + foreach ($xml->url as $url_element) {
724 + $url = esc_url_raw((string)$url_element->loc);
725 + if ($url) {
726 + $urls[] = array('url' => $url);
727 + }
728 + }
729 +
730 + $total_urls = count($urls);
731 +
732 + if ($total_urls < 1) {
733 + throw new Exception(__('No valid URLs found in sitemap', 'mxchat'));
734 + }
735 +
736 + // Create unique queue ID
737 + $queue_id = 'sitemap_' . md5($sitemap_url . time());
738 +
739 + // Add URLs to queue
740 + $queued_count = $this->mxchat_add_to_queue($queue_id, 'url', $urls, $bot_id);
741 +
742 + if ($queued_count === 0) {
743 + throw new Exception(__('Failed to add URLs to processing queue', 'mxchat'));
744 + }
745 +
746 + // Store queue metadata
747 + $this->mxchat_set_queue_meta($queue_id, 'source_url', $sitemap_url);
748 + $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'sitemap');
749 + $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_urls);
750 + $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
751 + $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
752 +
753 + // Store queue ID in transient for status tracking
754 + set_transient('mxchat_active_queue_sitemap', $queue_id, DAY_IN_SECONDS);
755 + set_transient('mxchat_last_sitemap_url', $sitemap_url, DAY_IN_SECONDS);
756 +
757 + return 'queued';
758 +
759 + } catch (Exception $e) {
760 + $error_message = $e->getMessage();
761 + error_log(sprintf(esc_html__('Error preparing sitemap for processing: %s', 'mxchat'), esc_html($error_message)));
762 +
763 + return $error_message;
764 + }
765 +
766 +}
767 +
768 +public function mxchat_sanitize_content_for_api($content) {
769 + //error_log('[MXCHAT-SANITIZE] Original content preview: ' . substr($content, 0, 500) . '...');
770 +
771 + // Strip WordPress shortcodes FIRST (WPBakery, Elementor, Woodmart, etc.)
772 + // This must be done before stripping HTML tags, otherwise the brackets are removed
773 + $content = strip_shortcodes($content);
774 +
775 + // Additional regex-based shortcode removal as a safety net
776 + // This catches any remaining shortcodes that strip_shortcodes() might have missed
777 + $content = preg_replace('/\[(\[?)([a-zA-Z0-9_-]+)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*(?:\[(?!\/\2\])[^\[]*)*)\[\/\2\])?)(\]?)/s', '', $content);
778 +
779 + // Remove script, style tags, and HTML comments
780 + $content = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $content);
781 + $content = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $content);
782 + $content = preg_replace('/<!--(.|\s)*?-->/', '', $content);
783 +
784 + // Remove all HTML tags and decode HTML entities
785 + $content = wp_strip_all_tags($content);
786 + $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5);
787 +
788 + // Normalize whitespace but preserve paragraph breaks
789 + // First, normalize line endings to \n
790 + $content = str_replace(["\r\n", "\r"], "\n", $content);
791 + // Replace multiple spaces/tabs with single space, but preserve newlines
792 + $content = preg_replace('/[ \t]+/', ' ', $content);
793 + // Replace 3+ newlines with 2 newlines (max 2 blank lines)
794 + $content = preg_replace('/\n{3,}/', "\n\n", $content);
795 + // Trim each line
796 + $lines = explode("\n", $content);
797 + $lines = array_map('trim', $lines);
798 + $content = implode("\n", $lines);
799 + // Final trim
800 + $content = trim($content);
801 +
802 + // Remove control characters (which can cause database issues) but preserve \n (0x0A) and \r (0x0D)
803 + $content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $content);
804 +
805 + // Remove NULL bytes which can cause database errors
806 + $content = str_replace("\0", "", $content);
807 +
808 + // Ensure valid UTF-8 encoding
809 + $content = wp_check_invalid_utf8($content);
810 +
811 + // Remove any extremely long strings without spaces (often garbage)
812 + $content = preg_replace('/\S{300,}/', ' ', $content);
813 +
814 + // Replace problematic characters that often cause database issues
815 + $content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content); // Remove emoji and other high Unicode characters
816 +
817 + // Replace any remaining potentially problematic characters with spaces
818 + // BUT preserve newlines by temporarily replacing them
819 + $content = str_replace("\n", "NEWLINE_PLACEHOLDER", $content);
820 + $content = preg_replace('/[^\p{L}\p{N}\p{P}\p{Z}\p{Sm}]/u', ' ', $content);
821 + $content = str_replace("NEWLINE_PLACEHOLDER", "\n", $content);
822 +
823 + // Limit to reasonable length if needed
824 + $max_length = 65000; // Just under MySQL TEXT field limit
825 + if (strlen($content) > $max_length) {
826 + $content = substr($content, 0, $max_length);
827 + }
828 +
829 + //error_log('[MXCHAT-SANITIZE] Sanitized content preview: ' . substr($content, 0, 500) . '...');
830 + return $content;
831 +}
832 +public function mxchat_extract_main_content($html) {
833 + if (empty($html)) {
834 + return '';
835 + }
836 + try {
837 + $dom = new DOMDocument;
838 + libxml_use_internal_errors(true); // Suppress HTML parsing errors
839 + @$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
840 + $xpath = new DOMXPath($dom);
841 +
842 + // For debugging purposes
843 + $debugEnabled = false; // Set to true to enable debugging output
844 + $debug = function($message) use ($debugEnabled) {
845 + if ($debugEnabled) {
846 + //error_log('[MXCHAT-DEBUG] ' . $message);
847 + }
848 + };
849 +
850 + // Direct targeting for Gerow theme posts
851 + $post_text = $xpath->query('//div[contains(@class, "post-text")]');
852 + if ($post_text && $post_text->length > 0) {
853 + $debug("Found post-text directly");
854 + $content = '';
855 + foreach ($post_text as $node) {
856 + $content .= $dom->saveHTML($node);
857 + }
858 + if (!empty($content)) {
859 + $debug("Returning post-text content");
860 + return $content;
861 + }
862 + }
863 +
864 + // Try to get the blog details content which contains the post-text
865 + $blog_details = $xpath->query('//div[contains(@class, "blog-details-content")]');
866 + if ($blog_details && $blog_details->length > 0) {
867 + $debug("Found blog-details-content");
868 + $content = '';
869 + foreach ($blog_details as $node) {
870 + $content .= $dom->saveHTML($node);
871 + }
872 + if (!empty($content)) {
873 + $debug("Returning blog-details-content");
874 + return $content;
875 + }
876 + }
877 +
878 + // Try to get the article which contains the blog details
879 + $article = $xpath->query('//article[contains(@class, "blog-details-wrap")]');
880 + if ($article && $article->length > 0) {
881 + $debug("Found article with blog-details-wrap");
882 + $content = '';
883 + foreach ($article as $node) {
884 + $content .= $dom->saveHTML($node);
885 + }
886 + if (!empty($content)) {
887 + $debug("Returning article content");
888 + return $content;
889 + }
890 + }
891 +
892 + // Try even broader with the blog-item-wrap
893 + $blog_item = $xpath->query('//div[contains(@class, "blog-item-wrap")]');
894 + if ($blog_item && $blog_item->length > 0) {
895 + $debug("Found blog-item-wrap");
896 + $content = '';
897 + foreach ($blog_item as $node) {
898 + $content .= $dom->saveHTML($node);
899 + }
900 + if (!empty($content)) {
901 + $debug("Returning blog-item-wrap content");
902 + return $content;
903 + }
904 + }
905 +
906 + // Specific Gerow theme path
907 + $gerow_path = $xpath->query('//section[contains(@class, "blog-area")]//div[contains(@class, "post-text")]');
908 + if ($gerow_path && $gerow_path->length > 0) {
909 + $debug("Found Gerow theme path to post-text");
910 + $content = '';
911 + foreach ($gerow_path as $node) {
912 + $content .= $dom->saveHTML($node);
913 + }
914 + if (!empty($content)) {
915 + $debug("Returning Gerow post-text content");
916 + return $content;
917 + }
918 + }
919 +
920 + // Generic blog post selectors
921 + $selectors = [
922 + // Blog post specific selectors
923 + '//div[contains(@class, "post-text")]',
924 + '//article[contains(@class, "blog-post-item")]//div[contains(@class, "post-text")]',
925 + '//div[contains(@class, "blog-details-content")]',
926 + '//article[contains(@class, "blog-details-wrap")]',
927 + '//div[contains(@class, "entry-content")]',
928 + '//div[contains(@class, "blog-content")]',
929 + '//div[contains(@class, "blog-item-wrap")]',
930 +
931 + // More general content selectors
932 + '//div[contains(@class, "page__content")]',
933 + '//div[contains(@class, "elementor-widget-container")]',
934 + '//div[contains(@class, "elementor-text-editor")]',
935 + '//div[contains(@class, "elementor-widget-text-editor")]',
936 + '//*[contains(@class, "entry-content")]',
937 + '//*[contains(@class, "post-content")]',
938 + '//*[contains(@class, "article-content")]',
939 + '//*[@id="content"]',
940 + '//*[@id="main-content"]',
941 + '//section[contains(@class, "blog-area")]',
942 + '//article',
943 + '//main',
944 + '//div[contains(@class, "content")]'
945 + ];
946 +
947 + // First handle Elementor content
948 + $debug("Checking for Elementor content");
949 + $elementor_widgets = $xpath->query('//div[contains(@class, "elementor-element")]//div[contains(@class, "elementor-widget-container")]');
950 + if ($elementor_widgets && $elementor_widgets->length > 0) {
951 + $debug("Found Elementor widgets");
952 + $combined_content = '';
953 + foreach ($elementor_widgets as $widget) {
954 + $widget_content = $dom->saveHTML($widget);
955 + if (!empty($widget_content)) {
956 + $combined_content .= $widget_content;
957 + }
958 + }
959 + if (!empty($combined_content)) {
960 + $debug("Returning Elementor content");
961 + return $combined_content;
962 + }
963 + }
964 +
965 + // Try standard selectors one by one
966 + foreach ($selectors as $selector) {
967 + $debug("Trying selector: " . $selector);
968 + $nodes = $xpath->query($selector);
969 + if ($nodes && $nodes->length > 0) {
970 + $debug("Found matches for selector: " . $selector);
971 + $content = '';
972 + foreach ($nodes as $node) {
973 + $content .= $dom->saveHTML($node);
974 + }
975 + if (!empty($content)) {
976 + $debug("Returning content from selector: " . $selector);
977 + return $content;
978 + }
979 + }
980 + }
981 +
982 + // Manual regex fallback for post-text if DOM methods fail
983 + $debug("Trying regex fallback");
984 + if (preg_match('/<div class="post-text">(.*?)<\/div>\s*<\/div>\s*<\/div>/s', $html, $matches)) {
985 + $debug("Found post-text via regex");
986 + return '<div class="post-text">' . $matches[1] . '</div>';
987 + }
988 +
989 + // Try to extract the blog section as a whole
990 + $blog_section = $xpath->query('//section[contains(@class, "blog-area")]');
991 + if ($blog_section && $blog_section->length > 0) {
992 + $debug("Found blog-area section");
993 + $content = '';
994 + foreach ($blog_section as $node) {
995 + $content .= $dom->saveHTML($node);
996 + }
997 + if (!empty($content)) {
998 + $debug("Returning blog-area section content");
999 + return $content;
1000 + }
1001 + }
1002 +
1003 + // Fallback: Return the body content if no specific selector matches
1004 + $debug("Using body fallback");
1005 + $body = $dom->getElementsByTagName('body');
1006 + if ($body->length > 0) {
1007 + return $dom->saveHTML($body->item(0));
1008 + }
1009 +
1010 + // Last resort: return the original HTML
1011 + $debug("Returning original HTML");
1012 + return $html;
1013 + } catch (Exception $e) {
1014 + //error_log('[MXCHAT-ERROR] Content extraction failed: ' . $e->getMessage());
1015 + return $html; // Return original HTML if parsing fails
1016 + } finally {
1017 + libxml_clear_errors();
1018 + }
1019 +}
1020 +public function mxchat_get_sitemap_processing_status($sitemap_url) {
1021 + $sitemap_url = esc_url_raw($sitemap_url);
1022 + $status_key = sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url));
1023 + $status = get_transient($status_key);
1024 +
1025 + if (!$status || !is_array($status)) {
1026 + return false;
1027 + }
1028 +
1029 + // Auto-complete check: if all URLs are processed but status isn't complete
1030 + if (isset($status['processed_urls']) && isset($status['total_urls']) &&
1031 + $status['processed_urls'] >= $status['total_urls'] &&
1032 + isset($status['status']) && $status['status'] !== 'complete' &&
1033 + $status['status'] !== 'error') {
1034 +
1035 + // Mark as complete
1036 + $status['status'] = 'complete';
1037 + $status['processed_urls'] = $status['total_urls']; // Ensure exact match
1038 +
1039 + // Update the transient with the corrected status
1040 + set_transient($status_key, $status, DAY_IN_SECONDS);
1041 + }
1042 +
1043 + return array(
1044 + 'total_urls' => absint($status['total_urls']),
1045 + 'processed_urls' => absint($status['processed_urls']),
1046 + 'failed_urls' => absint($status['failed_urls'] ?? 0),
1047 + 'percentage' => ($status['total_urls'] > 0)
1048 + ? round((absint($status['processed_urls']) / absint($status['total_urls'])) * 100)
1049 + : 0,
1050 + 'status' => sanitize_text_field($status['status']),
1051 + 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
1052 + 'error' => isset($status['error']) ? sanitize_text_field($status['error']) : '',
1053 + 'last_error' => isset($status['last_error']) ? sanitize_text_field($status['last_error']) : '',
1054 + 'failed_urls_list' => isset($status['failed_urls_list']) ? $status['failed_urls_list'] : array()
1055 + );
1056 +}
1057 +
1058 +public function mxchat_ajax_get_status_updates() {
1059 + try {
1060 + // Verify the request
1061 + check_ajax_referer('mxchat_status_nonce', 'nonce');
1062 +
1063 + // Get active queue IDs
1064 + $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
1065 + $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
1066 +
1067 + $sitemap_status = false;
1068 + $pdf_status = false;
1069 +
1070 + // Get sitemap queue status
1071 + if ($sitemap_queue_id) {
1072 + $sitemap_status = $this->mxchat_get_queue_status_data($sitemap_queue_id, 'sitemap');
1073 + }
1074 +
1075 + // Get PDF queue status
1076 + if ($pdf_queue_id) {
1077 + $pdf_status = $this->mxchat_get_queue_status_data($pdf_queue_id, 'pdf');
1078 + }
1079 +
1080 + $is_active_processing =
1081 + ($sitemap_status && $sitemap_status['status'] === 'processing') ||
1082 + ($pdf_status && $pdf_status['status'] === 'processing');
1083 +
1084 + // Return JSON response with the status data
1085 + wp_send_json(array(
1086 + 'pdf_status' => $pdf_status,
1087 + 'sitemap_status' => $sitemap_status,
1088 + 'is_processing' => $is_active_processing,
1089 + 'sitemap_queue_id' => $sitemap_queue_id,
1090 + 'pdf_queue_id' => $pdf_queue_id
1091 + ));
1092 +
1093 + } catch (Exception $e) {
1094 + error_log('MxChat Status Update Error: ' . $e->getMessage());
1095 +
1096 + wp_send_json_error(array(
1097 + 'message' => 'Error getting status updates: ' . $e->getMessage(),
1098 + 'status' => 'error'
1099 + ));
1100 + }
1101 +}
1102 +
1103 +/**
1104 + * Helper function to get queue status data
1105 + */
1106 +private function mxchat_get_queue_status_data($queue_id, $type = 'sitemap') {
1107 + global $wpdb;
1108 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
1109 +
1110 + // Get counts by status
1111 + $counts = $wpdb->get_results($wpdb->prepare(
1112 + "SELECT status, COUNT(*) as count
1113 + FROM $table_name
1114 + WHERE queue_id = %s
1115 + GROUP BY status",
1116 + $queue_id
1117 + ), OBJECT_K);
1118 +
1119 + $total = 0;
1120 + $completed = 0;
1121 + $failed = 0;
1122 + $processing = 0;
1123 + $pending = 0;
1124 +
1125 + foreach ($counts as $status => $data) {
1126 + $count = absint($data->count);
1127 + $total += $count;
1128 +
1129 + switch ($status) {
1130 + case 'completed':
1131 + $completed = $count;
1132 + break;
1133 + case 'failed':
1134 + $failed = $count;
1135 + break;
1136 + case 'processing':
1137 + $processing = $count;
1138 + break;
1139 + case 'pending':
1140 + $pending = $count;
1141 + break;
1142 + }
1143 + }
1144 +
1145 + if ($total === 0) {
1146 + return false;
1147 + }
1148 +
1149 + // Calculate percentage
1150 + $percentage = round((($completed + $failed) / $total) * 100);
1151 +
1152 + // Get failed items details (limit to 50)
1153 + $failed_items = array();
1154 + if ($failed > 0) {
1155 + $failed_results = $wpdb->get_results($wpdb->prepare(
1156 + "SELECT item_type, item_data, error_message, attempts, completed_at
1157 + FROM $table_name
1158 + WHERE queue_id = %s
1159 + AND status = 'failed'
1160 + AND attempts >= max_attempts
1161 + ORDER BY id DESC
1162 + LIMIT 50",
1163 + $queue_id
1164 + ));
1165 +
1166 + foreach ($failed_results as $item) {
1167 + $data = json_decode($item->item_data, true);
1168 + $url = $type === 'pdf' ? ($data['pdf_url'] ?? '') : ($data['url'] ?? '');
1169 +
1170 + $failed_items[] = array(
1171 + 'url' => $url,
1172 + 'page' => $type === 'pdf' ? ($data['page_number'] ?? 0) : 0,
1173 + 'error' => $item->error_message,
1174 + 'retries' => $item->attempts,
1175 + 'time' => strtotime($item->completed_at)
1176 + );
1177 + }
1178 + }
1179 +
1180 + // Get queue metadata
1181 + $source_url = $this->mxchat_get_queue_meta($queue_id, 'source_url');
1182 +
1183 + // Determine if queue is complete
1184 + $is_complete = ($pending === 0 && $processing === 0);
1185 +
1186 + // Get last update time
1187 + $last_update = $wpdb->get_var($wpdb->prepare(
1188 + "SELECT MAX(UNIX_TIMESTAMP(COALESCE(completed_at, started_at, created_at)))
1189 + FROM $table_name
1190 + WHERE queue_id = %s",
1191 + $queue_id
1192 + ));
1193 +
1194 + $last_update_text = $last_update ? human_time_diff($last_update, time()) . ' ' . __('ago', 'mxchat') : __('Just now', 'mxchat');
1195 +
1196 + // Format based on type
1197 + if ($type === 'pdf') {
1198 + return array(
1199 + 'total_pages' => $total,
1200 + 'processed_pages' => $completed + $failed,
1201 + 'failed_pages' => $failed,
1202 + 'percentage' => $percentage,
1203 + 'status' => $is_complete ? 'complete' : 'processing',
1204 + 'last_update' => $last_update_text,
1205 + 'failed_pages_list' => $failed_items,
1206 + 'pdf_url' => $source_url,
1207 + 'queue_id' => $queue_id
1208 + );
1209 + } else {
1210 + return array(
1211 + 'total_urls' => $total,
1212 + 'processed_urls' => $completed + $failed,
1213 + 'failed_urls' => $failed,
1214 + 'percentage' => $percentage,
1215 + 'status' => $is_complete ? 'complete' : 'processing',
1216 + 'last_update' => $last_update_text,
1217 + 'failed_urls_list' => $failed_items,
1218 + 'sitemap_url' => $source_url,
1219 + 'queue_id' => $queue_id
1220 + );
1221 + }
1222 +}
1223 +
1224 +public function mxchat_stop_processing() {
1225 + // Verify permissions
1226 + if (!current_user_can('manage_options')) {
1227 + wp_die(esc_html__('Unauthorized access', 'mxchat'));
1228 + }
1229 +
1230 + // Verify nonce
1231 + check_admin_referer('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce');
1232 +
1233 + global $wpdb;
1234 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
1235 +
1236 + // Get active queue IDs
1237 + $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
1238 + $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
1239 +
1240 + // Delete all pending items from active queues
1241 + if ($sitemap_queue_id) {
1242 + $wpdb->delete(
1243 + $table_name,
1244 + array(
1245 + 'queue_id' => $sitemap_queue_id,
1246 + 'status' => 'pending'
1247 + ),
1248 + array('%s', '%s')
1249 + );
1250 +
1251 + delete_transient('mxchat_active_queue_sitemap');
1252 + delete_transient('mxchat_last_sitemap_url');
1253 + }
1254 +
1255 + if ($pdf_queue_id) {
1256 + // Get PDF path before deleting
1257 + $pdf_path = $this->mxchat_get_queue_meta($pdf_queue_id, 'pdf_path');
1258 +
1259 + $wpdb->delete(
1260 + $table_name,
1261 + array(
1262 + 'queue_id' => $pdf_queue_id,
1263 + 'status' => 'pending'
1264 + ),
1265 + array('%s', '%s')
1266 + );
1267 +
1268 + // Delete PDF file
1269 + if ($pdf_path && file_exists($pdf_path)) {
1270 + wp_delete_file($pdf_path);
1271 + }
1272 +
1273 + delete_transient('mxchat_active_queue_pdf');
1274 + delete_transient('mxchat_last_pdf_url');
1275 + }
1276 +
1277 + // Redirect back with a success message
1278 + set_transient('mxchat_admin_notice_success',
1279 + esc_html__('Processing has been stopped successfully.', 'mxchat'),
1280 + 30
1281 + );
1282 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
1283 + exit;
1284 +}
1285 +
1286 +/**
1287 + * Get content list for processing
1288 + */
1289 +public function ajax_mxchat_get_content_list() {
1290 + // Verify the nonce
1291 + check_ajax_referer('mxchat_content_selector_nonce', 'nonce');
1292 +
1293 + if (!current_user_can('manage_options')) {
1294 + wp_send_json_error(__('Unauthorized access', 'mxchat'));
1295 + }
1296 +
1297 + $page = isset($_GET['page']) ? absint($_GET['page']) : 1;
1298 + $per_page = isset($_GET['per_page']) ? absint($_GET['per_page']) : 50;
1299 + $search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
1300 + $post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : 'all';
1301 + $post_status = isset($_GET['post_status']) ? sanitize_text_field($_GET['post_status']) : 'publish';
1302 + $processed_filter = isset($_GET['processed_filter']) ? sanitize_text_field($_GET['processed_filter']) : 'all';
1303 +
1304 + // Build query args
1305 + $args = array(
1306 + 'posts_per_page' => $per_page,
1307 + 'paged' => $page,
1308 + 'post_status' => $post_status !== 'all' ? $post_status : array('publish', 'draft', 'pending'),
1309 + 'orderby' => 'date',
1310 + 'order' => 'DESC',
1311 + );
1312 +
1313 + // Handle post types - IMPROVED VERSION
1314 + if ($post_type !== 'all') {
1315 + $args['post_type'] = $post_type;
1316 + } else {
1317 + // Get all available post types that might contain content
1318 + $all_post_types = array();
1319 +
1320 + // First get all public post types
1321 + $public_types = get_post_types(array('public' => true), 'names');
1322 + $all_post_types = array_merge($all_post_types, $public_types);
1323 +
1324 + // Add common forum/community post types
1325 + $forum_types = array('topic', 'reply', 'forum', 'wpforo_topic', 'wpforo_post');
1326 + foreach ($forum_types as $forum_type) {
1327 + if (post_type_exists($forum_type)) {
1328 + $all_post_types[] = $forum_type;
1329 + }
1330 + }
1331 +
1332 + // Add other commonly used post types
1333 + $common_types = array('product', 'job_listing', 'event', 'portfolio');
1334 + foreach ($common_types as $common_type) {
1335 + if (post_type_exists($common_type)) {
1336 + $all_post_types[] = $common_type;
1337 + }
1338 + }
1339 +
1340 + // Remove duplicates and ensure we have at least some post types
1341 + $all_post_types = array_unique($all_post_types);
1342 +
1343 + if (empty($all_post_types)) {
1344 + // Fallback to basic post types
1345 + $all_post_types = array('post', 'page');
1346 + }
1347 +
1348 + $args['post_type'] = $all_post_types;
1349 +
1350 + // Debug logging to see what post types are being queried
1351 + //error_log('MxChat Debug: Querying post types: ' . implode(', ', $all_post_types));
1352 + }
1353 +
1354 + if (!empty($search)) {
1355 + $args['s'] = $search;
1356 + }
1357 +
1358 + // Get processed data from storage
1359 + $processed_data = array();
1360 +
1361 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
1362 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
1363 +
1364 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
1365 + // Get fresh data from Pinecone - no caching
1366 + $processed_data = $this->mxchat_get_pinecone_processed_content($pinecone_options);
1367 + } else {
1368 + // WordPress DB checking with better URL matching for all post types
1369 + global $wpdb;
1370 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
1371 + $processed_items = $wpdb->get_results("SELECT id, source_url, timestamp FROM {$table_name}");
1372 +
1373 + if (!empty($processed_items)) {
1374 + foreach ($processed_items as $item) {
1375 + // Use improved URL matching that works for all post types
1376 + $post_id = $this->mxchat_url_to_post_id_improved($item->source_url);
1377 +
1378 + if ($post_id) {
1379 + $processed_data[$post_id] = array(
1380 + 'db_id' => $item->id,
1381 + 'timestamp' => $item->timestamp,
1382 + 'url' => $item->source_url,
1383 + 'source' => 'wordpress'
1384 + );
1385 + }
1386 + }
1387 + }
1388 + }
1389 +
1390 + // Get processed IDs as a simple array for in_array checks
1391 + $processed_ids = array_keys($processed_data);
1392 +
1393 + // Handle processed/unprocessed filter
1394 + if ($processed_filter === 'processed' && !empty($processed_ids)) {
1395 + $args['post__in'] = $processed_ids;
1396 + } elseif ($processed_filter === 'unprocessed' && !empty($processed_ids)) {
1397 + $args['post__not_in'] = $processed_ids;
1398 + }
1399 +
1400 + // Run the query
1401 + $query = new WP_Query($args);
1402 + $content_items = array();
1403 +
1404 + if ($query->have_posts()) {
1405 + while ($query->have_posts()) {
1406 + $query->the_post();
1407 + $id = get_the_ID();
1408 + $post_date = get_the_date();
1409 + $excerpt = wp_trim_words(get_the_excerpt(), 20, '...');
1410 + $word_count = str_word_count(strip_tags(get_the_content()));
1411 +
1412 + $is_processed = in_array($id, $processed_ids);
1413 + $processed_date = '';
1414 + $db_record_id = 0;
1415 + $data_source = 'none';
1416 +
1417 + if ($is_processed && isset($processed_data[$id])) {
1418 + $item_data = $processed_data[$id];
1419 + $data_source = $item_data['source'];
1420 +
1421 + if ($data_source === 'wordpress' && isset($item_data['timestamp'])) {
1422 + // WordPress DB format
1423 + $timestamp = strtotime($item_data['timestamp']);
1424 + $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
1425 + $db_record_id = $item_data['db_id'];
1426 + } elseif ($data_source === 'pinecone') {
1427 + // Pinecone format
1428 + $processed_date = $item_data['processed_date'];
1429 + $db_record_id = $item_data['db_id'];
1430 + }
1431 + }
1432 +
1433 + $content_items[] = array(
1434 + 'id' => $id,
1435 + 'title' => get_the_title(),
1436 + 'permalink' => get_permalink(),
1437 + 'date' => $post_date,
1438 + 'type' => get_post_type(),
1439 + 'status' => get_post_status(),
1440 + 'excerpt' => $excerpt,
1441 + 'word_count' => $word_count,
1442 + 'already_processed' => $is_processed,
1443 + 'processed_date' => $processed_date,
1444 + 'db_record_id' => $db_record_id,
1445 + 'data_source' => $data_source
1446 + );
1447 + }
1448 + wp_reset_postdata();
1449 + }
1450 +
1451 + $response = array(
1452 + 'items' => $content_items,
1453 + 'total' => $query->found_posts,
1454 + 'total_pages' => $query->max_num_pages,
1455 + 'current_page' => $page,
1456 + 'processed_count' => count($processed_ids)
1457 + );
1458 +
1459 + wp_send_json_success($response);
1460 + exit;
1461 +}
1462 +
1463 +
1464 +/**
1465 + * This function handles various WooCommerce URL formats and permalink structures
1466 + */
1467 +private function mxchat_url_to_post_id_improved($url) {
1468 + // First try the standard WordPress function
1469 + $post_id = url_to_postid($url);
1470 +
1471 + if ($post_id > 0) {
1472 + return $post_id;
1473 + }
1474 +
1475 + // If that fails, try more aggressive URL matching
1476 + // Remove trailing slashes and query parameters for better matching
1477 + $clean_url = rtrim($url, '/');
1478 + $clean_url = strtok($clean_url, '?'); // Remove query parameters
1479 +
1480 + // Try again with cleaned URL
1481 + $post_id = url_to_postid($clean_url);
1482 + if ($post_id > 0) {
1483 + return $post_id;
1484 + }
1485 +
1486 + // For bbPress forum topics, try extracting slug from URL
1487 + if (strpos($url, '/topic/') !== false || strpos($url, '/forums/') !== false) {
1488 + // Handle bbPress URLs: /forums/topic/topic-name/
1489 + if (preg_match('/\/forums\/topic\/([^\/\?]+)/', $url, $matches)) {
1490 + $topic_slug = $matches[1];
1491 +
1492 + // Look up topic by slug
1493 + $topic = get_page_by_path($topic_slug, OBJECT, 'topic');
1494 + if ($topic) {
1495 + return $topic->ID;
1496 + }
1497 +
1498 + // Alternative method: query by post_name
1499 + global $wpdb;
1500 + $post_id = $wpdb->get_var($wpdb->prepare(
1501 + "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
1502 + $topic_slug
1503 + ));
1504 +
1505 + if ($post_id) {
1506 + return intval($post_id);
1507 + }
1508 + }
1509 +
1510 + // Handle simpler topic URLs: /topic/topic-name/
1511 + if (preg_match('/\/topic\/([^\/\?]+)/', $url, $matches)) {
1512 + $topic_slug = $matches[1];
1513 +
1514 + global $wpdb;
1515 + $post_id = $wpdb->get_var($wpdb->prepare(
1516 + "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
1517 + $topic_slug
1518 + ));
1519 +
1520 + if ($post_id) {
1521 + return intval($post_id);
1522 + }
1523 + }
1524 + }
1525 +
1526 + // For WooCommerce products
1527 + if (strpos($url, '/product/') !== false || strpos($url, 'product=') !== false) {
1528 + // Extract product slug from various URL formats
1529 + $product_slug = '';
1530 +
1531 + // Handle pretty permalinks: /product/product-name/
1532 + if (preg_match('/\/product\/([^\/\?]+)/', $url, $matches)) {
1533 + $product_slug = $matches[1];
1534 + }
1535 + // Handle query parameters: ?product=product-name
1536 + elseif (preg_match('/[\?&]product=([^&]+)/', $url, $matches)) {
1537 + $product_slug = $matches[1];
1538 + }
1539 +
1540 + if (!empty($product_slug)) {
1541 + // Look up product by slug
1542 + $product = get_page_by_path($product_slug, OBJECT, 'product');
1543 + if ($product) {
1544 + return $product->ID;
1545 + }
1546 +
1547 + // Alternative method: query by post_name
1548 + global $wpdb;
1549 + $post_id = $wpdb->get_var($wpdb->prepare(
1550 + "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'product' AND post_status = 'publish'",
1551 + $product_slug
1552 + ));
1553 +
1554 + if ($post_id) {
1555 + return intval($post_id);
1556 + }
1557 + }
1558 + }
1559 +
1560 + // Generic approach: try to extract slug and match against all post types
1561 + $parsed_url = wp_parse_url($clean_url);
1562 + $path = $parsed_url['path'] ?? '';
1563 +
1564 + if (!empty($path)) {
1565 + // Get the last part of the path as potential slug
1566 + $path_parts = array_filter(explode('/', trim($path, '/')));
1567 + $potential_slug = end($path_parts);
1568 +
1569 + if (!empty($potential_slug)) {
1570 + global $wpdb;
1571 +
1572 + // Try to find any post with this slug
1573 + $post_id = $wpdb->get_var($wpdb->prepare(
1574 + "SELECT ID FROM {$wpdb->posts}
1575 + WHERE post_name = %s
1576 + AND post_status IN ('publish', 'closed', 'private')
1577 + AND post_type NOT IN ('revision', 'attachment', 'nav_menu_item')
1578 + ORDER BY CASE
1579 + WHEN post_type = 'post' THEN 1
1580 + WHEN post_type = 'page' THEN 2
1581 + WHEN post_type = 'topic' THEN 3
1582 + WHEN post_type = 'product' THEN 4
1583 + ELSE 5
1584 + END
1585 + LIMIT 1",
1586 + $potential_slug
1587 + ));
1588 +
1589 + if ($post_id) {
1590 + return intval($post_id);
1591 + }
1592 + }
1593 + }
1594 +
1595 + // ADDITIONAL: Try direct database lookup by URL variations
1596 + global $wpdb;
1597 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
1598 +
1599 + // Try variations of the URL (with/without trailing slash, http/https)
1600 + $url_variations = array(
1601 + $url,
1602 + rtrim($url, '/'),
1603 + $url . '/',
1604 + str_replace('http://', 'https://', $url),
1605 + str_replace('https://', 'http://', $url),
1606 + str_replace('http://', 'https://', rtrim($url, '/')),
1607 + str_replace('https://', 'http://', rtrim($url, '/'))
1608 + );
1609 +
1610 + // Remove duplicates
1611 + $url_variations = array_unique($url_variations);
1612 +
1613 + foreach ($url_variations as $variation) {
1614 + $existing_record = $wpdb->get_row($wpdb->prepare(
1615 + "SELECT id, source_url FROM $table_name WHERE source_url = %s",
1616 + $variation
1617 + ));
1618 +
1619 + if ($existing_record) {
1620 + // Try to get post ID from this stored URL
1621 + $stored_post_id = url_to_postid($existing_record->source_url);
1622 + if ($stored_post_id > 0) {
1623 + return $stored_post_id;
1624 + }
1625 + }
1626 + }
1627 +
1628 + return 0; // No match found
1629 +}
1630 +/**
1631 + * Process selected content via AJAX
1632 + */
1633 +public function ajax_mxchat_process_selected_content() {
1634 + // Basic request validation
1635 + if (!check_ajax_referer('mxchat_content_selector_nonce', 'nonce', false)) {
1636 + wp_send_json_error('Invalid nonce');
1637 + exit;
1638 + }
1639 +
1640 + if (!current_user_can('manage_options')) {
1641 + wp_send_json_error('Unauthorized access');
1642 + exit;
1643 + }
1644 +
1645 + // Get post IDs - safely parse the array
1646 + $post_ids = array();
1647 + if (isset($_POST['post_ids']) && is_array($_POST['post_ids'])) {
1648 + foreach ($_POST['post_ids'] as $id) {
1649 + $post_ids[] = absint($id);
1650 + }
1651 + }
1652 +
1653 + if (empty($post_ids)) {
1654 + wp_send_json_error('No content selected');
1655 + exit;
1656 + }
1657 +
1658 + // Get bot_id from request
1659 + $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1660 +
1661 + // Process only ONE post at a time to avoid request size issues
1662 + $post_id = reset($post_ids);
1663 + $post = get_post($post_id);
1664 +
1665 + if (!$post) {
1666 + wp_send_json_error('Post not found');
1667 + exit;
1668 + }
1669 +
1670 + // Get content including title, short description (for WooCommerce), and main content
1671 + $content = $post->post_title . "\n\n";
1672 +
1673 + // Add short description if it exists (WooCommerce products use post_excerpt for short description)
1674 + if (!empty($post->post_excerpt)) {
1675 + // Strip shortcodes first (WPBakery, Elementor, etc.), then strip HTML tags
1676 + $clean_excerpt = strip_shortcodes($post->post_excerpt);
1677 + // Additional regex-based shortcode removal as a safety net
1678 + $clean_excerpt = preg_replace('/\[(\[?)([a-zA-Z0-9_-]+)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*(?:\[(?!\/\2\])[^\[]*)*)\[\/\2\])?)(\]?)/s', '', $clean_excerpt);
1679 + $content .= "Short Description: " . wp_strip_all_tags($clean_excerpt) . "\n\n";
1680 + }
1681 +
1682 + // Add main content - strip shortcodes first, then strip HTML tags
1683 + $clean_content = strip_shortcodes($post->post_content);
1684 + // Additional regex-based shortcode removal as a safety net
1685 + $clean_content = preg_replace('/\[(\[?)([a-zA-Z0-9_-]+)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*(?:\[(?!\/\2\])[^\[]*)*)\[\/\2\])?)(\]?)/s', '', $clean_content);
1686 + $content .= wp_strip_all_tags($clean_content);
1687 +
1688 + // ADD ACF FIELDS SUPPORT
1689 + $acf_fields = $this->mxchat_get_acf_fields_for_post($post_id);
1690 + if (!empty($acf_fields)) {
1691 + $acf_content_parts = array();
1692 +
1693 + foreach ($acf_fields as $field_name => $field_value) {
1694 + $formatted_value = $this->mxchat_format_acf_field_value($field_value, $field_name, $post_id);
1695 +
1696 + if (!empty($formatted_value)) {
1697 + $field_label = ucwords(str_replace('_', ' ', $field_name));
1698 + $acf_content_parts[] = $field_label . ": " . $formatted_value;
1699 + }
1700 + }
1701 +
1702 + if (!empty($acf_content_parts)) {
1703 + $content .= "\n\n" . implode("\n", $acf_content_parts);
1704 + }
1705 + }
1706 +
1707 + $content = substr($content, 0, 10000); // Limit content size
1708 +
1709 + // Get bot-specific API key
1710 + $bot_options = $this->get_bot_options($bot_id);
1711 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1712 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
1713 +
1714 + if (strpos($selected_model, 'voyage') === 0) {
1715 + $api_key = $options['voyage_api_key'] ?? '';
1716 + $provider_name = 'Voyage AI';
1717 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
1718 + $api_key = $options['gemini_api_key'] ?? '';
1719 + $provider_name = 'Google Gemini';
1720 + } else {
1721 + $api_key = $options['api_key'] ?? '';
1722 + $provider_name = 'OpenAI';
1723 + }
1724 +
1725 + if (empty($api_key)) {
1726 + wp_send_json_error($provider_name . ' API key not configured');
1727 + exit;
1728 + }
1729 +
1730 + $source_url = get_permalink($post_id);
1731 + $vector_id = md5($source_url); // Vector ID for Pinecone
1732 +
1733 + // Check for existing content in bot-specific storage
1734 + $is_update = false;
1735 +
1736 + // Get bot-specific Pinecone configuration
1737 + $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1738 + $use_pinecone = !empty($bot_pinecone_config) && ($bot_pinecone_config['use_pinecone'] ?? false);
1739 +
1740 + if ($use_pinecone && !empty($bot_pinecone_config['api_key'])) {
1741 + // Check Pinecone for this bot
1742 + $pinecone_data = $this->mxchat_get_pinecone_processed_content($bot_pinecone_config);
1743 + if (isset($pinecone_data[$post_id])) {
1744 + $is_update = true;
1745 + }
1746 + } else {
1747 + // Check WordPress DB (same as before since it's shared)
1748 + global $wpdb;
1749 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
1750 + $existing_record = $wpdb->get_row($wpdb->prepare(
1751 + "SELECT id FROM $table_name WHERE source_url = %s",
1752 + $source_url
1753 + ));
1754 +
1755 + if ($existing_record) {
1756 + $is_update = true;
1757 + }
1758 + }
1759 +
1760 + // Use the centralized utility function with bot_id
1761 + $result = MxChat_Utils::submit_content_to_db(
1762 + $content,
1763 + $source_url,
1764 + $api_key,
1765 + $vector_id,
1766 + $bot_id
1767 + );
1768 +
1769 + if (is_wp_error($result)) {
1770 + wp_send_json_error('Storage failed: ' . $result->get_error_message());
1771 + exit;
1772 + }
1773 +
1774 + // Automatically apply role restriction based on tags
1775 + $this->apply_role_restriction_to_post($post_id, $source_url);
1776 +
1777 + $operation_type = $is_update ? 'update' : 'new';
1778 +
1779 + // Count ACF fields for debugging
1780 + $acf_field_count = count($acf_fields);
1781 +
1782 + // Success response with minimal data
1783 + wp_send_json_success(array(
1784 + 'message' => $operation_type === 'update' ? 'Content updated successfully' : 'Content processed successfully',
1785 + 'post_id' => $post_id,
1786 + 'title' => $post->post_title,
1787 + 'operation_type' => $operation_type,
1788 + 'vector_id' => $vector_id,
1789 + 'acf_fields_found' => $acf_field_count,
1790 + 'content_preview' => substr($content, 0, 100) . '...',
1791 + 'bot_id' => $bot_id
1792 + ));
1793 + exit;
1794 +}
1795 +
1796 +private function apply_role_restriction_to_post($post_id, $source_url) {
1797 + // Get tag-role mappings
1798 + $mappings = get_option('mxchat_tag_role_mappings', array());
1799 +
1800 + if (empty($mappings)) {
1801 + return; // No mappings, leave as public
1802 + }
1803 +
1804 + // Get all tags for the post
1805 + $post_tags = wp_get_post_tags($post_id, array('fields' => 'slugs'));
1806 +
1807 + if (empty($post_tags)) {
1808 + return; // No tags, leave as public
1809 + }
1810 +
1811 + // Determine the highest role restriction based on tags
1812 + $highest_role = 'public';
1813 + $role_hierarchy = array(
1814 + 'public' => 0,
1815 + 'logged_in' => 1,
1816 + 'subscriber' => 2,
1817 + 'contributor' => 3,
1818 + 'author' => 4,
1819 + 'editor' => 5,
1820 + 'administrator' => 6
1821 + );
1822 +
1823 + foreach ($post_tags as $tag_slug) {
1824 + if (isset($mappings[$tag_slug])) {
1825 + $role = $mappings[$tag_slug];
1826 + if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
1827 + $highest_role = $role;
1828 + }
1829 + }
1830 + }
1831 +
1832 + // If no restricted tags found, return (leave as public)
1833 + if ($highest_role === 'public') {
1834 + return;
1835 + }
1836 +
1837 + // Update the role restriction in the database
1838 + global $wpdb;
1839 +
1840 + // Check if using Pinecone
1841 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
1842 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
1843 +
1844 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
1845 + // Update Pinecone role restriction
1846 + $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
1847 + $vector_id = md5($source_url);
1848 +
1849 + $wpdb->replace(
1850 + $roles_table,
1851 + array(
1852 + 'vector_id' => $vector_id,
1853 + 'role_restriction' => $highest_role,
1854 + 'updated_at' => current_time('mysql')
1855 + ),
1856 + array('%s', '%s', '%s')
1857 + );
1858 + } else {
1859 + // Update WordPress DB
1860 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
1861 +
1862 + $wpdb->update(
1863 + $table_name,
1864 + array('role_restriction' => $highest_role),
1865 + array('source_url' => $source_url),
1866 + array('%s'),
1867 + array('%s')
1868 + );
1869 + }
1870 +}
1871 +
1872 +public function mxchat_get_public_post_types() {
1873 + // Get all public post types
1874 + $post_types = get_post_types(array('public' => true), 'objects');
1875 + $post_type_options = array();
1876 +
1877 + foreach ($post_types as $post_type) {
1878 + $post_type_options[$post_type->name] = $post_type->label;
1879 + }
1880 +
1881 + // Also include common forum/community post types that might not be marked as public
1882 + $additional_types = array(
1883 + 'topic' => 'Forum Topics (bbPress)',
1884 + 'reply' => 'Forum Replies (bbPress)',
1885 + 'forum' => 'Forums (bbPress)',
1886 + 'wpforo_topic' => 'wpForo Topics',
1887 + 'wpforo_post' => 'wpForo Posts'
1888 + );
1889 +
1890 + foreach ($additional_types as $type_name => $type_label) {
1891 + if (post_type_exists($type_name) && !isset($post_type_options[$type_name])) {
1892 + $post_type_options[$type_name] = $type_label;
1893 + }
1894 + }
1895 +
1896 + return $post_type_options;
1897 +}
1898 +
1899 +/**
1900 + * Retrieves processed content from Pinecone API
1901 + */
1902 +public function mxchat_get_pinecone_processed_content($pinecone_options) {
1903 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
1904 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
1905 +
1906 + if (empty($api_key) || empty($host)) {
1907 + return array();
1908 + }
1909 +
1910 + $pinecone_data = array();
1911 +
1912 + try {
1913 + // Always get fresh data from Pinecone
1914 + $pinecone_data = $this->mxchat_scan_pinecone_for_processed_content($pinecone_options);
1915 +
1916 + // Method 2: Final fallback - try stats endpoint (if available)
1917 + if (empty($pinecone_data)) {
1918 + $stats_url = "https://{$host}/describe_index_stats";
1919 +
1920 + $response = wp_remote_post($stats_url, array(
1921 + 'headers' => array(
1922 + 'Api-Key' => $api_key,
1923 + 'Content-Type' => 'application/json'
1924 + ),
1925 + 'body' => json_encode(array()),
1926 + 'timeout' => 30
1927 + ));
1928 +
1929 + if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
1930 + $body = wp_remote_retrieve_body($response);
1931 + $stats_data = json_decode($body, true);
1932 + }
1933 + }
1934 +
1935 + } catch (Exception $e) {
1936 + // Log error but return fresh data only
1937 + }
1938 +
1939 + return $pinecone_data;
1940 +}
1941 +public function mxchat_fetch_pinecone_vectors_by_ids($pinecone_options, $vector_ids) {
1942 + //error_log('=== DEBUG: Starting mxchat_fetch_pinecone_vectors_by_ids ===');
1943 +
1944 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
1945 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
1946 +
1947 + if (empty($api_key) || empty($host) || empty($vector_ids)) {
1948 + //error_log('DEBUG: Missing parameters for fetch by IDs');
1949 + return array();
1950 + }
1951 +
1952 + try {
1953 + $fetch_url = "https://{$host}/vectors/fetch";
1954 + //error_log('DEBUG: Fetch URL: ' . $fetch_url);
1955 + //error_log('DEBUG: Fetching ' . count($vector_ids) . ' vector IDs');
1956 +
1957 + // Pinecone fetch API allows fetching specific vectors by ID
1958 + $fetch_data = array(
1959 + 'ids' => array_values($vector_ids)
1960 + );
1961 +
1962 + $response = wp_remote_post($fetch_url, array(
1963 + 'headers' => array(
1964 + 'Api-Key' => $api_key,
1965 + 'Content-Type' => 'application/json'
1966 + ),
1967 + 'body' => json_encode($fetch_data),
1968 + 'timeout' => 30
1969 + ));
1970 +
1971 + if (is_wp_error($response)) {
1972 + //error_log('DEBUG: Fetch by IDs WP error: ' . $response->get_error_message());
1973 + return array();
1974 + }
1975 +
1976 + $response_code = wp_remote_retrieve_response_code($response);
1977 + //error_log('DEBUG: Fetch response code: ' . $response_code);
1978 +
1979 + if ($response_code !== 200) {
1980 + $error_body = wp_remote_retrieve_body($response);
1981 + //error_log('DEBUG: Fetch failed with body: ' . $error_body);
1982 + return array();
1983 + }
1984 +
1985 + $body = wp_remote_retrieve_body($response);
1986 + $data = json_decode($body, true);
1987 +
1988 + //error_log('DEBUG: Fetch response structure: ' . print_r(array_keys($data), true));
1989 +
1990 + if (!isset($data['vectors'])) {
1991 + //error_log('DEBUG: No vectors key in response');
1992 + return array();
1993 + }
1994 +
1995 + $processed_data = array();
1996 +
1997 + foreach ($data['vectors'] as $vector_id => $vector_data) {
1998 + $metadata = $vector_data['metadata'] ?? array();
1999 + $source_url = $metadata['source_url'] ?? '';
2000 +
2001 + if (!empty($source_url)) {
2002 + $post_id = url_to_postid($source_url);
2003 + if ($post_id) {
2004 + $created_at = $metadata['created_at'] ?? '';
2005 + $processed_date = 'Recently';
2006 +
2007 + if (!empty($created_at)) {
2008 + $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
2009 + if ($timestamp) {
2010 + $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
2011 + }
2012 + }
2013 +
2014 + $processed_data[$post_id] = array(
2015 + 'db_id' => $vector_id,
2016 + 'processed_date' => $processed_date,
2017 + 'url' => $source_url,
2018 + 'source' => 'pinecone',
2019 + 'timestamp' => $timestamp ?? current_time('timestamp')
2020 + );
2021 + }
2022 + }
2023 + }
2024 +
2025 + //error_log('DEBUG: Processed ' . count($processed_data) . ' vectors from fetch');
2026 + return $processed_data;
2027 +
2028 + } catch (Exception $e) {
2029 + //error_log('DEBUG: Exception in fetch_pinecone_vectors_by_ids: ' . $e->getMessage());
2030 + return array();
2031 + }
2032 +}
2033 +
2034 +/**
2035 + * Scan Pinecone for processed content
2036 + */
2037 +public function mxchat_scan_pinecone_for_processed_content($pinecone_options) {
2038 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2039 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
2040 +
2041 + if (empty($api_key) || empty($host)) {
2042 + return array();
2043 + }
2044 +
2045 + try {
2046 + // Use multiple random vectors to get better coverage
2047 + $all_matches = array();
2048 + $seen_ids = array();
2049 +
2050 + // Try 3 different random vectors to get better coverage
2051 + for ($i = 0; $i < 3; $i++) {
2052 + $query_url = "https://{$host}/query";
2053 +
2054 + // Generate a random unit vector instead of zeros
2055 + $random_vector = array();
2056 + for ($j = 0; $j < 1536; $j++) {
2057 + $random_vector[] = (rand(-1000, 1000) / 1000.0);
2058 + }
2059 +
2060 + // Normalize the vector to unit length
2061 + $magnitude = sqrt(array_sum(array_map(function($x) { return $x * $x; }, $random_vector)));
2062 + if ($magnitude > 0) {
2063 + $random_vector = array_map(function($x) use ($magnitude) { return $x / $magnitude; }, $random_vector);
2064 + }
2065 +
2066 + $query_data = array(
2067 + 'includeMetadata' => true,
2068 + 'includeValues' => false,
2069 + 'topK' => 10000,
2070 + 'vector' => $random_vector
2071 + );
2072 +
2073 + $response = wp_remote_post($query_url, array(
2074 + 'headers' => array(
2075 + 'Api-Key' => $api_key,
2076 + 'Content-Type' => 'application/json'
2077 + ),
2078 + 'body' => json_encode($query_data),
2079 + 'timeout' => 30
2080 + ));
2081 +
2082 + if (is_wp_error($response)) {
2083 + continue;
2084 + }
2085 +
2086 + $response_code = wp_remote_retrieve_response_code($response);
2087 +
2088 + if ($response_code !== 200) {
2089 + continue;
2090 + }
2091 +
2092 + $body = wp_remote_retrieve_body($response);
2093 + $data = json_decode($body, true);
2094 +
2095 + if (isset($data['matches'])) {
2096 + foreach ($data['matches'] as $match) {
2097 + $match_id = $match['id'] ?? '';
2098 + if (!empty($match_id) && !isset($seen_ids[$match_id])) {
2099 + $all_matches[] = $match;
2100 + $seen_ids[$match_id] = true;
2101 + }
2102 + }
2103 + }
2104 + }
2105 +
2106 + // Convert matches to processed data format
2107 + $processed_data = array();
2108 +
2109 + foreach ($all_matches as $match) {
2110 + $metadata = $match['metadata'] ?? array();
2111 + $source_url = $metadata['source_url'] ?? '';
2112 + $match_id = $match['id'] ?? '';
2113 +
2114 + if (!empty($source_url) && !empty($match_id)) {
2115 + $post_id = url_to_postid($source_url);
2116 + if ($post_id) {
2117 + $created_at = $metadata['created_at'] ?? '';
2118 + $processed_date = 'Recently';
2119 +
2120 + if (!empty($created_at)) {
2121 + $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
2122 + if ($timestamp) {
2123 + $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
2124 + }
2125 + }
2126 +
2127 + $processed_data[$post_id] = array(
2128 + 'db_id' => $match_id,
2129 + 'processed_date' => $processed_date,
2130 + 'url' => $source_url,
2131 + 'source' => 'pinecone',
2132 + 'timestamp' => $timestamp ?? current_time('timestamp')
2133 + );
2134 + }
2135 + }
2136 + }
2137 +
2138 + return $processed_data;
2139 +
2140 + } catch (Exception $e) {
2141 + return array();
2142 + }
2143 +}
2144 +/**
2145 + * Generate embeddings from input text for MXChat with bot support
2146 + */
2147 +private function mxchat_generate_embedding($text, $bot_id = 'default') {
2148 + // Enable detailed logging for debugging
2149 + //error_log('[MXCHAT-EMBED] Starting embedding generation for bot: ' . $bot_id . '. Text length: ' . strlen($text) . ' bytes');
2150 + //error_log('[MXCHAT-EMBED] Text preview: ' . substr($text, 0, 100) . '...');
2151 +
2152 + // Get bot-specific options
2153 + $bot_options = $this->get_bot_options($bot_id);
2154 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
2155 +
2156 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
2157 + //error_log('[MXCHAT-EMBED] Selected embedding model for bot ' . $bot_id . ': ' . $selected_model);
2158 +
2159 + // Determine provider and endpoint
2160 + if (strpos($selected_model, 'voyage') === 0) {
2161 + $api_key = $options['voyage_api_key'] ?? '';
2162 + $endpoint = 'https://api.voyageai.com/v1/embeddings';
2163 + $provider_name = 'Voyage AI';
2164 + //error_log('[MXCHAT-EMBED] Using Voyage AI API for bot ' . $bot_id);
2165 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
2166 + $api_key = $options['gemini_api_key'] ?? '';
2167 + $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
2168 + $provider_name = 'Google Gemini';
2169 + //error_log('[MXCHAT-EMBED] Using Google Gemini API for bot ' . $bot_id);
2170 + } else {
2171 + $api_key = $options['api_key'] ?? '';
2172 + $endpoint = 'https://api.openai.com/v1/embeddings';
2173 + $provider_name = 'OpenAI';
2174 + //error_log('[MXCHAT-EMBED] Using OpenAI API for bot ' . $bot_id);
2175 + }
2176 +
2177 + //error_log('[MXCHAT-EMBED] Using endpoint: ' . $endpoint);
2178 +
2179 + if (empty($api_key)) {
2180 + $error_message = sprintf('Missing %s API key for bot %s. Please configure your API key in the bot settings.', $provider_name, $bot_id);
2181 + //error_log('[MXCHAT-EMBED] Error: ' . $error_message);
2182 + return $error_message;
2183 + }
2184 +
2185 + // Check if text is too long (for OpenAI, roughly estimate tokens as words/0.75)
2186 + $estimated_tokens = ceil(str_word_count($text) / 0.75);
2187 + //error_log('[MXCHAT-EMBED] Estimated token count: ~' . $estimated_tokens);
2188 +
2189 + if ($estimated_tokens > 8000 && strpos($selected_model, 'voyage') === false && strpos($selected_model, 'gemini-embedding') === false) {
2190 + //error_log('[MXCHAT-EMBED] Warning: Text may exceed OpenAI token limits (8K for most models)');
2191 + // Consider truncating text here
2192 + }
2193 +
2194 + // Prepare request body based on provider
2195 + if (strpos($selected_model, 'gemini-embedding') === 0) {
2196 + // Gemini API format
2197 + $request_body = array(
2198 + 'model' => 'models/' . $selected_model,
2199 + 'content' => array(
2200 + 'parts' => array(
2201 + array('text' => $text)
2202 + )
2203 + )
2204 + );
2205 +
2206 + // Set output dimensionality to 1536 for consistency with other models
2207 + $request_body['outputDimensionality'] = 1536;
2208 + } else {
2209 + // OpenAI/Voyage API format
2210 + $request_body = array(
2211 + 'model' => $selected_model,
2212 + 'input' => $text
2213 + );
2214 +
2215 + // Add output_dimension for voyage-3-large model
2216 + if ($selected_model === 'voyage-3-large') {
2217 + $request_body['output_dimension'] = 2048;
2218 + }
2219 + }
2220 +
2221 + //error_log('[MXCHAT-EMBED] Request prepared with model: ' . $selected_model);
2222 +
2223 + // Prepare headers based on provider
2224 + if (strpos($selected_model, 'gemini-embedding') === 0) {
2225 + // Gemini uses API key as query parameter
2226 + $endpoint .= '?key=' . $api_key;
2227 + $headers = array(
2228 + 'Content-Type' => 'application/json'
2229 + );
2230 + } else {
2231 + // OpenAI/Voyage use Bearer token
2232 + $headers = array(
2233 + 'Authorization' => 'Bearer ' . $api_key,
2234 + 'Content-Type' => 'application/json'
2235 + );
2236 + }
2237 +
2238 + // Make API request
2239 + //error_log('[MXCHAT-EMBED] Sending API request to: ' . $endpoint);
2240 + $response = wp_remote_post($endpoint, array(
2241 + 'body' => wp_json_encode($request_body),
2242 + 'headers' => $headers,
2243 + 'timeout' => 60 // Increased timeout for large inputs
2244 + ));
2245 +
2246 + // Handle wp_remote_post errors
2247 + if (is_wp_error($response)) {
2248 + $error_message = $response->get_error_message();
2249 + //error_log('[MXCHAT-EMBED] WP Remote Post Error: ' . $error_message);
2250 + return 'Connection error: ' . $error_message;
2251 + }
2252 +
2253 + // Get and check HTTP response code
2254 + $http_code = wp_remote_retrieve_response_code($response);
2255 + //error_log('[MXCHAT-EMBED] API Response Code: ' . $http_code);
2256 +
2257 + if ($http_code !== 200) {
2258 + $error_body = wp_remote_retrieve_body($response);
2259 + //error_log('[MXCHAT-EMBED] API Error Response Body: ' . $error_body);
2260 +
2261 + // Try to parse error for more details
2262 + $error_json = json_decode($error_body, true);
2263 + if (json_last_error() === JSON_ERROR_NONE && isset($error_json['error'])) {
2264 + $error_type = $error_json['error']['type'] ?? 'unknown';
2265 + $error_message = $error_json['error']['message'] ?? 'No message';
2266 + //error_log('[MXCHAT-EMBED] API Error Type: ' . $error_type);
2267 + //error_log('[MXCHAT-EMBED] API Error Message: ' . $error_message);
2268 +
2269 + // Customize error message for common API errors
2270 + if ($error_type === 'invalid_request_error' && strpos($error_message, 'API key') !== false) {
2271 + $error_message = sprintf('Invalid %s API key for bot %s. Please check your API key in the bot settings.', $provider_name, $bot_id);
2272 + } elseif ($error_type === 'authentication_error') {
2273 + $error_message = sprintf('%s authentication failed for bot %s. Please verify your API key in the bot settings.', $provider_name, $bot_id);
2274 + }
2275 +
2276 + //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
2277 + return $error_message;
2278 + }
2279 +
2280 + $error_message = sprintf("API Error (HTTP %d): Unable to generate embedding for bot %s", $http_code, $bot_id);
2281 + //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
2282 + return $error_message;
2283 + }
2284 +
2285 + // Parse response body
2286 + $response_body = wp_remote_retrieve_body($response);
2287 + //error_log('[MXCHAT-EMBED] Received response length: ' . strlen($response_body) . ' bytes');
2288 +
2289 + $response_data = json_decode($response_body, true);
2290 +
2291 + if (json_last_error() !== JSON_ERROR_NONE) {
2292 + $error = json_last_error_msg();
2293 + //error_log('[MXCHAT-EMBED] JSON Parse Error: ' . $error);
2294 + //error_log('[MXCHAT-EMBED] Response preview: ' . substr($response_body, 0, 200));
2295 + return "Failed to parse API response: $error";
2296 + }
2297 +
2298 + // Handle different response formats based on provider
2299 + if (strpos($selected_model, 'gemini-embedding') === 0) {
2300 + // Gemini API response format
2301 + if (isset($response_data['embedding']['values'])) {
2302 + $embedding_dimensions = count($response_data['embedding']['values']);
2303 + //error_log('[MXCHAT-EMBED] Successfully extracted Gemini embedding with ' . $embedding_dimensions . ' dimensions');
2304 +
2305 + // Check if embedding dimensions are as expected (should be 1536)
2306 + if ($embedding_dimensions !== 1536) {
2307 + //error_log('[MXCHAT-EMBED] Warning: Unexpected Gemini embedding dimensions: ' . $embedding_dimensions);
2308 + }
2309 +
2310 + return $response_data['embedding']['values'];
2311 + } else {
2312 + //error_log('[MXCHAT-EMBED] Error: No embedding found in Gemini response');
2313 + //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
2314 +
2315 + if (isset($response_data['error'])) {
2316 + $error_message = "Gemini API Error in response: " . wp_json_encode($response_data['error']);
2317 + //error_log('[MXCHAT-EMBED] ' . $error_message);
2318 + return $error_message;
2319 + }
2320 +
2321 + $error_message = "Invalid Gemini API response format: No embedding found";
2322 + //error_log('[MXCHAT-EMBED] ' . $error_message);
2323 + return $error_message;
2324 + }
2325 + } else {
2326 + // OpenAI/Voyage API response format
2327 + if (isset($response_data['data'][0]['embedding'])) {
2328 + $embedding_dimensions = count($response_data['data'][0]['embedding']);
2329 + //error_log('[MXCHAT-EMBED] Successfully extracted embedding with ' . $embedding_dimensions . ' dimensions');
2330 +
2331 + // Check if embedding dimensions are as expected
2332 + if (($selected_model === 'text-embedding-ada-002' && $embedding_dimensions !== 1536) ||
2333 + ($selected_model === 'voyage-3-large' && $embedding_dimensions !== 2048)) {
2334 + //error_log('[MXCHAT-EMBED] Warning: Unexpected embedding dimensions');
2335 + }
2336 +
2337 + return $response_data['data'][0]['embedding'];
2338 + } else {
2339 + //error_log('[MXCHAT-EMBED] Error: No embedding found in response');
2340 + //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
2341 +
2342 + if (isset($response_data['error'])) {
2343 + $error_message = "API Error in response: " . wp_json_encode($response_data['error']);
2344 + //error_log('[MXCHAT-EMBED] ' . $error_message);
2345 + return $error_message;
2346 + }
2347 +
2348 + $error_message = "Invalid API response format: No embedding found";
2349 + //error_log('[MXCHAT-EMBED] ' . $error_message);
2350 + return $error_message;
2351 + }
2352 + }
2353 +}
2354 +
2355 +/**
2356 + * Get bot-specific options for multi-bot functionality
2357 + * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2358 + */
2359 +private function get_bot_options($bot_id = 'default') {
2360 + //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2361 +
2362 + if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2363 + //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2364 + return array();
2365 + }
2366 +
2367 + $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2368 +
2369 + if (!empty($bot_options)) {
2370 + //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2371 + if (isset($bot_options['similarity_threshold'])) {
2372 + //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2373 + }
2374 + }
2375 +
2376 + return is_array($bot_options) ? $bot_options : array();
2377 +}
2378 +
2379 +/**
2380 + * Get bot-specific Pinecone configuration
2381 + * Used in the knowledge retrieval functions
2382 + */
2383 +// Also add debugging to your get_bot_pinecone_config function
2384 +private function get_bot_pinecone_config($bot_id = 'default') {
2385 + //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2386 +
2387 + // If default bot or multi-bot add-on not active, use default Pinecone config
2388 + if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2389 + //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2390 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
2391 + $config = array(
2392 + 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2393 + 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2394 + 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2395 + 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2396 + );
2397 + //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2398 + return $config;
2399 + }
2400 +
2401 + //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2402 +
2403 + // Hook for multi-bot add-on to provide bot-specific Pinecone config
2404 + $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2405 +
2406 + if (!empty($bot_pinecone_config)) {
2407 + //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2408 + //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2409 + //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2410 + //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2411 + } else {
2412 + //error_log("MXCHAT DEBUG: Filter returned empty config!");
2413 + }
2414 +
2415 + return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2416 +}
2417 +
2418 +
2419 +public function mxchat_ajax_dismiss_completed_status() {
2420 + try {
2421 + // Verify the request
2422 + check_ajax_referer('mxchat_status_nonce', 'nonce');
2423 +
2424 + if (!current_user_can('manage_options')) {
2425 + wp_send_json_error('Unauthorized access');
2426 + exit;
2427 + }
2428 +
2429 + $card_type = isset($_POST['card_type']) ? sanitize_text_field($_POST['card_type']) : '';
2430 +
2431 + if ($card_type === 'pdf') {
2432 + // Clear PDF status
2433 + $pdf_url = get_transient('mxchat_last_pdf_url');
2434 + if ($pdf_url) {
2435 + delete_transient('mxchat_pdf_status_' . md5($pdf_url));
2436 + delete_transient('mxchat_last_pdf_url');
2437 + }
2438 + } elseif ($card_type === 'sitemap') {
2439 + // Clear sitemap status
2440 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
2441 + if ($sitemap_url) {
2442 + delete_transient('mxchat_sitemap_status_' . md5($sitemap_url));
2443 + delete_transient('mxchat_last_sitemap_url');
2444 + }
2445 + }
2446 +
2447 + wp_send_json_success(array('message' => 'Status dismissed successfully'));
2448 +
2449 + } catch (Exception $e) {
2450 + wp_send_json_error(array('message' => 'Error dismissing status: ' . $e->getMessage()));
2451 + }
2452 +}
2453 +
2454 +/**
2455 + * Render completed status cards on page load
2456 + * This ensures completed processing status persists through page refreshes
2457 + */
2458 +public function mxchat_render_completed_status_cards() {
2459 + $output = '';
2460 +
2461 + // Check for completed PDF status
2462 + $pdf_url = get_transient('mxchat_last_pdf_url');
2463 + if ($pdf_url) {
2464 + $pdf_status = $this->mxchat_get_pdf_processing_status($pdf_url);
2465 + if ($pdf_status && ($pdf_status['status'] === 'complete' || $pdf_status['status'] === 'error')) {
2466 + $output .= $this->mxchat_render_pdf_status_card($pdf_status, $pdf_url);
2467 + }
2468 + }
2469 +
2470 + // Check for completed sitemap status
2471 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
2472 + if ($sitemap_url) {
2473 + $sitemap_status = $this->mxchat_get_sitemap_processing_status($sitemap_url);
2474 + if ($sitemap_status && ($sitemap_status['status'] === 'complete' || $sitemap_status['status'] === 'error')) {
2475 + $output .= $this->mxchat_render_sitemap_status_card($sitemap_status, $sitemap_url);
2476 + }
2477 + }
2478 +
2479 + return $output;
2480 +}
2481 +
2482 +/**
2483 + * Render PDF status card HTML
2484 + */
2485 +private function mxchat_render_pdf_status_card($status, $pdf_url) {
2486 + $html = '<div class="mxchat-status-card" data-card-type="pdf">';
2487 + $html .= '<div class="mxchat-status-header">';
2488 + $html .= '<h4>' . esc_html__('PDF Processing Status', 'mxchat') . '</h4>';
2489 +
2490 + // Add dismiss button for completed status
2491 + if ($status['status'] === 'complete' || $status['status'] === 'error') {
2492 + $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
2493 + }
2494 +
2495 + // Process Batch button for processing status
2496 + if ($status['status'] === 'processing') {
2497 + $html .= '<button type="button" class="mxchat-manual-batch-btn"
2498 + data-process-type="pdf"
2499 + data-url="' . esc_attr($pdf_url) . '">
2500 + ' . esc_html__('Process Batch', 'mxchat') . '</button>';
2501 + }
2502 +
2503 + // Add status badges
2504 + if ($status['status'] === 'error') {
2505 + $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
2506 + } elseif ($status['status'] === 'complete') {
2507 + if ($status['failed_pages'] > 0) {
2508 + $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
2509 + sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_pages']) . '</span>';
2510 + } else {
2511 + $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
2512 + }
2513 + }
2514 +
2515 + $html .= '</div>'; // End header
2516 +
2517 + // Progress bar
2518 + $html .= '<div class="mxchat-progress-bar">';
2519 + $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
2520 + $html .= '</div>';
2521 +
2522 + // Status details
2523 + $html .= '<div class="mxchat-status-details">';
2524 + $html .= '<p>' . sprintf(
2525 + esc_html__('Progress: %d of %d pages (%d%%)', 'mxchat'),
2526 + $status['processed_pages'],
2527 + $status['total_pages'],
2528 + $status['percentage']
2529 + ) . '</p>';
2530 +
2531 + // Show failed pages count if any
2532 + if ($status['failed_pages'] > 0) {
2533 + $html .= '<p><strong>' . esc_html__('Failed pages:', 'mxchat') . '</strong> ' . esc_html($status['failed_pages']) . '</p>';
2534 + }
2535 +
2536 + $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
2537 + $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
2538 +
2539 + // Add completion summary if available AND it's an array
2540 + if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
2541 + $summary = $status['completion_summary'];
2542 + $html .= '<div class="mxchat-completion-summary">';
2543 + $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
2544 + $html .= '<p><strong>' . esc_html__('Total Pages:', 'mxchat') . '</strong> ' . esc_html($summary['total_pages']) . '</p>';
2545 + $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_pages']) . '</p>';
2546 + $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_pages']) . '</p>';
2547 + $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
2548 + $html .= '</div>';
2549 + }
2550 +
2551 + // Add failed pages list if any AND it's an array
2552 + if (isset($status['failed_pages_list']) && is_array($status['failed_pages_list']) && !empty($status['failed_pages_list'])) {
2553 + $html .= $this->mxchat_render_failed_pages_list($status['failed_pages_list']);
2554 + }
2555 +
2556 + // Add error message if any
2557 + if (isset($status['error']) && !empty($status['error'])) {
2558 + $html .= '<div class="mxchat-error-notice">';
2559 + $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
2560 + $html .= '</div>';
2561 + }
2562 +
2563 + $html .= '</div>'; // End details
2564 + $html .= '</div>'; // End card
2565 +
2566 + return $html;
2567 +}
2568 +/**
2569 + * Render sitemap status card HTML
2570 + */
2571 +private function mxchat_render_sitemap_status_card($status, $sitemap_url) {
2572 + $html = '<div class="mxchat-status-card" data-card-type="sitemap">';
2573 + $html .= '<div class="mxchat-status-header">';
2574 + $html .= '<h4>' . esc_html__('Sitemap Processing Status', 'mxchat') . '</h4>';
2575 +
2576 + // Add dismiss button for completed status
2577 + if ($status['status'] === 'complete' || $status['status'] === 'error') {
2578 + $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
2579 + }
2580 +
2581 + // Process Batch button for processing status
2582 + if ($status['status'] === 'processing') {
2583 + $html .= '<button type="button" class="mxchat-manual-batch-btn"
2584 + data-process-type="sitemap"
2585 + data-url="' . esc_attr($sitemap_url) . '">
2586 + ' . esc_html__('Process Batch', 'mxchat') . '</button>';
2587 + }
2588 +
2589 + // Add status badges
2590 + if ($status['status'] === 'error') {
2591 + $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
2592 + } elseif ($status['status'] === 'complete') {
2593 + if ($status['failed_urls'] > 0) {
2594 + $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
2595 + sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_urls']) . '</span>';
2596 + } else {
2597 + $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
2598 + }
2599 + }
2600 +
2601 + $html .= '</div>'; // End header
2602 +
2603 + // Progress bar
2604 + $html .= '<div class="mxchat-progress-bar">';
2605 + $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
2606 + $html .= '</div>';
2607 +
2608 + // Status details
2609 + $html .= '<div class="mxchat-status-details">';
2610 + $html .= '<p>' . sprintf(
2611 + esc_html__('Progress: %d of %d URLs (%d%%)', 'mxchat'),
2612 + $status['processed_urls'],
2613 + $status['total_urls'],
2614 + $status['percentage']
2615 + ) . '</p>';
2616 +
2617 + // Show failed URLs count if any
2618 + if ($status['failed_urls'] > 0) {
2619 + $html .= '<p><strong>' . esc_html__('Failed URLs:', 'mxchat') . '</strong> ' . esc_html($status['failed_urls']) . '</p>';
2620 + }
2621 +
2622 + $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
2623 + $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
2624 +
2625 + // Add completion summary if available AND it's an array
2626 + if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
2627 + $summary = $status['completion_summary'];
2628 + $html .= '<div class="mxchat-completion-summary">';
2629 + $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
2630 + $html .= '<p><strong>' . esc_html__('Total URLs:', 'mxchat') . '</strong> ' . esc_html($summary['total_urls']) . '</p>';
2631 + $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_urls']) . '</p>';
2632 + $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_urls']) . '</p>';
2633 + $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
2634 + $html .= '</div>';
2635 + }
2636 +
2637 + // Add error messages if any (but not the failed URLs list)
2638 + if (!empty($status['error']) || !empty($status['last_error'])) {
2639 + $html .= '<div class="mxchat-error-notice">';
2640 +
2641 + if (!empty($status['error'])) {
2642 + $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
2643 + }
2644 +
2645 + if (!empty($status['last_error'])) {
2646 + $html .= '<p class="last-error">' . esc_html__('Last error:', 'mxchat') . ' ' . esc_html($status['last_error']) . '</p>';
2647 + }
2648 +
2649 + $html .= '</div>';
2650 + }
2651 +
2652 + $html .= '</div>'; // End details
2653 + $html .= '</div>'; // End card
2654 +
2655 + return $html;
2656 +}
2657 +
2658 +
2659 +/**
2660 + * Render failed pages list
2661 + */
2662 +private function mxchat_render_failed_pages_list($failed_pages_list) {
2663 + // Validate that $failed_pages_list is an array and not empty
2664 + if (!is_array($failed_pages_list) || empty($failed_pages_list)) {
2665 + return '';
2666 + }
2667 +
2668 + $html = '<div class="mxchat-error-notice">';
2669 + $html .= '<div class="mxchat-failed-pages-container">';
2670 + $html .= '<h5>' . sprintf(esc_html__('Failed Pages (%d)', 'mxchat'), count($failed_pages_list)) . '</h5>';
2671 + $html .= '<details>';
2672 + $html .= '<summary>' . esc_html__('Show Failed Pages', 'mxchat') . '</summary>';
2673 + $html .= '<div class="mxchat-failed-pages-list">';
2674 +
2675 + // Create table for failed pages
2676 + $html .= '<table class="widefat striped">';
2677 + $html .= '<thead><tr>';
2678 + $html .= '<th>' . esc_html__('Page', 'mxchat') . '</th>';
2679 + $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
2680 + $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
2681 + $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
2682 + $html .= '</tr></thead><tbody>';
2683 +
2684 + // Sort failed pages by most recent
2685 + $sorted_failed_pages = $failed_pages_list;
2686 + usort($sorted_failed_pages, function($a, $b) {
2687 + return ($b['time'] ?? 0) - ($a['time'] ?? 0);
2688 + });
2689 +
2690 + foreach ($sorted_failed_pages as $item) {
2691 + // Ensure $item is an array before accessing its elements
2692 + if (!is_array($item)) {
2693 + continue;
2694 + }
2695 +
2696 + $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
2697 + $html .= '<tr>';
2698 + $html .= '<td>' . esc_html__('Page', 'mxchat') . ' ' . esc_html($item['page'] ?? 'Unknown') . '</td>';
2699 + $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
2700 + $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
2701 + $html .= '<td>' . esc_html($time_ago) . '</td>';
2702 + $html .= '</tr>';
2703 + }
2704 +
2705 + $html .= '</tbody></table>';
2706 + $html .= '</div></details></div></div>';
2707 +
2708 + return $html;
2709 +}
2710 +
2711 +/**
2712 + * Render failed URLs list
2713 + */
2714 +private function mxchat_render_failed_urls_list($failed_urls_list) {
2715 + // Validate that $failed_urls_list is an array and not empty
2716 + if (!is_array($failed_urls_list) || empty($failed_urls_list)) {
2717 + return '';
2718 + }
2719 +
2720 + $html = '<div class="mxchat-failed-urls-container">';
2721 + $html .= '<h5>' . sprintf(esc_html__('Failed URLs (%d)', 'mxchat'), count($failed_urls_list)) . '</h5>';
2722 + $html .= '<details>';
2723 + $html .= '<summary>' . esc_html__('Show Failed URLs', 'mxchat') . '</summary>';
2724 + $html .= '<div class="mxchat-failed-urls-list">';
2725 +
2726 + // Create table for failed URLs
2727 + $html .= '<table class="widefat striped">';
2728 + $html .= '<thead><tr>';
2729 + $html .= '<th>' . esc_html__('URL', 'mxchat') . '</th>';
2730 + $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
2731 + $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
2732 + $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
2733 + $html .= '</tr></thead><tbody>';
2734 +
2735 + // Sort failed URLs by most recent
2736 + $sorted_failed_urls = $failed_urls_list;
2737 + usort($sorted_failed_urls, function($a, $b) {
2738 + return ($b['time'] ?? 0) - ($a['time'] ?? 0);
2739 + });
2740 +
2741 + // Show up to 50 failed URLs
2742 + $display_urls = array_slice($sorted_failed_urls, 0, 50);
2743 +
2744 + foreach ($display_urls as $item) {
2745 + // Ensure $item is an array before accessing its elements
2746 + if (!is_array($item)) {
2747 + continue;
2748 + }
2749 +
2750 + $url = $item['url'] ?? '';
2751 + $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
2752 +
2753 + // Truncate URL for display
2754 + $display_url = strlen($url) > 50 ? substr($url, 0, 47) . '...' : $url;
2755 +
2756 + $html .= '<tr>';
2757 + $html .= '<td style="word-break: break-all;">';
2758 + if (!empty($url)) {
2759 + $html .= '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($display_url) . '</a>';
2760 + } else {
2761 + $html .= esc_html__('Unknown URL', 'mxchat');
2762 + }
2763 + $html .= '</td>';
2764 + $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
2765 + $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
2766 + $html .= '<td>' . esc_html($time_ago) . '</td>';
2767 + $html .= '</tr>';
2768 + }
2769 +
2770 + $html .= '</tbody></table>';
2771 +
2772 + if (count($failed_urls_list) > 50) {
2773 + $html .= '<div class="mxchat-failed-urls-more">+ ' .
2774 + (count($failed_urls_list) - 50) .
2775 + ' ' . esc_html__('more failed URLs not shown', 'mxchat') . '</div>';
2776 + }
2777 +
2778 + $html .= '</div></details></div>';
2779 +
2780 + return $html;
2781 +}
2782 +
2783 +/**
2784 + * Get all ACF fields for a specific post
2785 + */
2786 +public function mxchat_get_acf_fields_for_post($post_id) {
2787 + if (!function_exists('get_fields')) {
2788 + return array();
2789 + }
2790 +
2791 + $fields = get_fields($post_id);
2792 + if (!$fields || !is_array($fields)) {
2793 + return array();
2794 + }
2795 +
2796 + return $fields;
2797 +}
2798 +
2799 +/**
2800 + * Format ACF field values for content extraction
2801 + */
2802 +public function mxchat_format_acf_field_value($value, $field_name = '', $post_id = 0) {
2803 + if (empty($value)) {
2804 + return '';
2805 + }
2806 +
2807 + // Handle WP_Post objects first (THIS IS THE KEY FIX)
2808 + if ($value instanceof WP_Post) {
2809 + return $value->post_title ?: '';
2810 + }
2811 +
2812 + // Handle other WP objects
2813 + if (is_object($value)) {
2814 + if (isset($value->post_title)) {
2815 + return $value->post_title;
2816 + } elseif (isset($value->display_name)) {
2817 + return $value->display_name;
2818 + } elseif (isset($value->name)) {
2819 + return $value->name;
2820 + } elseif (method_exists($value, '__toString')) {
2821 + try {
2822 + return (string) $value;
2823 + } catch (Exception $e) {
2824 + return '';
2825 + }
2826 + }
2827 + // For any other objects, return empty string
2828 + return '';
2829 + }
2830 +
2831 + // Handle different ACF field types
2832 + if (is_array($value)) {
2833 + // Check if it's an image/file field
2834 + if (isset($value['url'])) {
2835 + // Image field - return alt text, title, or caption
2836 + if (!empty($value['alt'])) {
2837 + return $value['alt'];
2838 + } elseif (!empty($value['title'])) {
2839 + return $value['title'];
2840 + } elseif (!empty($value['caption'])) {
2841 + return $value['caption'];
2842 + } else {
2843 + return ''; // Don't include just the URL
2844 + }
2845 + }
2846 +
2847 + // Check if it's a post object or relationship field
2848 + if (isset($value['post_title'])) {
2849 + return $value['post_title'];
2850 + }
2851 +
2852 + // Check if it's a user field
2853 + if (isset($value['display_name'])) {
2854 + return $value['display_name'];
2855 + }
2856 +
2857 + // Check if it's a taxonomy term
2858 + if (isset($value['name']) && isset($value['taxonomy'])) {
2859 + return $value['name'];
2860 + }
2861 +
2862 + // Check if it's a select field with label
2863 + if (isset($value['label'])) {
2864 + return $value['label'];
2865 + }
2866 +
2867 + // Check for repeater field or flexible content
2868 + if (is_numeric(key($value))) {
2869 + $sub_values = array();
2870 + foreach ($value as $sub_item) {
2871 + if (is_array($sub_item)) {
2872 + // For repeater/flexible content, extract text values
2873 + $sub_text = $this->mxchat_extract_text_from_acf_array($sub_item);
2874 + if (!empty($sub_text)) {
2875 + $sub_values[] = $sub_text;
2876 + }
2877 + } elseif ($sub_item instanceof WP_Post) {
2878 + // Handle WP_Post objects in arrays
2879 + $sub_values[] = $sub_item->post_title ?: '';
2880 + } else {
2881 + $sub_values[] = (string) $sub_item;
2882 + }
2883 + }
2884 + return implode(', ', array_filter($sub_values));
2885 + }
2886 +
2887 + // For other arrays, try to extract meaningful text
2888 + $text_values = array();
2889 + foreach ($value as $key => $val) {
2890 + if (is_string($val) && !empty(trim($val))) {
2891 + $text_values[] = trim($val);
2892 + } elseif ($val instanceof WP_Post) {
2893 + // Handle WP_Post objects in associative arrays
2894 + $text_values[] = $val->post_title ?: '';
2895 + } elseif (is_array($val) && isset($val['post_title'])) {
2896 + $text_values[] = $val['post_title'];
2897 + } elseif (is_array($val) && isset($val['name'])) {
2898 + $text_values[] = $val['name'];
2899 + }
2900 + }
2901 +
2902 + return implode(', ', array_filter($text_values));
2903 + }
2904 +
2905 + // Handle boolean values
2906 + if (is_bool($value)) {
2907 + return $value ? 'Yes' : 'No';
2908 + }
2909 +
2910 + // Handle numeric values
2911 + if (is_numeric($value)) {
2912 + return (string) $value;
2913 + }
2914 +
2915 + // Handle string values
2916 + if (is_string($value)) {
2917 + return trim($value);
2918 + }
2919 +
2920 + // For anything else that we can't handle, return empty string
2921 + // This prevents the "Object could not be converted to string" error
2922 + return '';
2923 +}
2924 +
2925 +/**
2926 + * Extract text from complex ACF array structures
2927 + */
2928 +private function mxchat_extract_text_from_acf_array($array) {
2929 + if (!is_array($array)) {
2930 + return '';
2931 + }
2932 +
2933 + $text_parts = array();
2934 +
2935 + foreach ($array as $key => $value) {
2936 + if (is_string($value) && !empty(trim($value))) {
2937 + // Skip keys that are likely to be IDs or technical values
2938 + if (!is_numeric($value) || strlen($value) > 10) {
2939 + $text_parts[] = trim($value);
2940 + }
2941 + } elseif ($value instanceof WP_Post) {
2942 + // Handle WP_Post objects
2943 + $text_parts[] = $value->post_title ?: '';
2944 + } elseif (is_array($value)) {
2945 + if (isset($value['post_title'])) {
2946 + $text_parts[] = $value['post_title'];
2947 + } elseif (isset($value['name'])) {
2948 + $text_parts[] = $value['name'];
2949 + } elseif (isset($value['label'])) {
2950 + $text_parts[] = $value['label'];
2951 + }
2952 + } elseif (is_object($value)) {
2953 + // Handle other objects safely
2954 + if (isset($value->post_title)) {
2955 + $text_parts[] = $value->post_title;
2956 + } elseif (isset($value->name)) {
2957 + $text_parts[] = $value->name;
2958 + } elseif (isset($value->display_name)) {
2959 + $text_parts[] = $value->display_name;
2960 + }
2961 + }
2962 + }
2963 +
2964 + return implode(', ', array_filter($text_parts));
2965 +}
2966 +
2967 +public function mxchat_handle_post_update($post_id, $post, $update) {
2968 + // Basic validation checks
2969 + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) {
2970 + return;
2971 + }
2972 +
2973 + $post_type = $post->post_type;
2974 +
2975 + // Check if sync is enabled for this post type
2976 + $should_sync = false;
2977 +
2978 + // Check built-in post types first
2979 + if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
2980 + $should_sync = true;
2981 + } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
2982 + $should_sync = true;
2983 + } else {
2984 + // Check custom post types
2985 + $option_name = 'mxchat_auto_sync_' . $post_type;
2986 + if (get_option($option_name) === '1') {
2987 + $should_sync = true;
2988 + }
2989 + }
2990 +
2991 + if (!$should_sync) {
2992 + return;
2993 + }
2994 +
2995 + // Check if we have stored the previous status and URL in our transients
2996 + $previous_status_key = 'mxchat_prev_status_' . $post_id;
2997 + $previous_status = get_transient($previous_status_key);
2998 +
2999 + $previous_url_key = 'mxchat_prev_url_' . $post_id;
3000 + $previous_url = get_transient($previous_url_key);
3001 +
3002 + // If the post was previously published but is now not published, remove from knowledge base
3003 + if ($previous_status === 'publish' && $post->post_status !== 'publish') {
3004 + // Use the stored URL from when it was published, or fall back to current permalink
3005 + $source_url = $previous_url ?: get_permalink($post_id);
3006 +
3007 + if ($source_url) {
3008 + // Check if Pinecone is enabled
3009 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3010 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3011 +
3012 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
3013 + // Delete from Pinecone
3014 + $this->mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options);
3015 + } else {
3016 + // Delete from WordPress DB
3017 + global $wpdb;
3018 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3019 +
3020 + $result = $wpdb->delete(
3021 + $table_name,
3022 + array('source_url' => $source_url),
3023 + array('%s')
3024 + );
3025 + }
3026 + }
3027 +
3028 + // Clean up the transients and exit early
3029 + delete_transient($previous_status_key);
3030 + delete_transient($previous_url_key);
3031 + return;
3032 + }
3033 +
3034 + // Store the current status for next time (if this is an update)
3035 + if ($update) {
3036 + set_transient($previous_status_key, $post->post_status, DAY_IN_SECONDS);
3037 +
3038 + // If the post is currently published, also store its URL
3039 + if ($post->post_status === 'publish') {
3040 + $current_url = get_permalink($post_id);
3041 + set_transient($previous_url_key, $current_url, DAY_IN_SECONDS);
3042 + }
3043 + }
3044 +
3045 + // Only process currently published content for adding/updating
3046 + if ($post->post_status === 'publish') {
3047 + // Get the source URL
3048 + $source_url = get_permalink($post_id);
3049 +
3050 + // Get content with proper formatting (matching ajax_mxchat_process_selected_content)
3051 + $title = get_the_title($post_id);
3052 + $content = get_post_field('post_content', $post_id);
3053 + $excerpt = get_post_field('post_excerpt', $post_id);
3054 +
3055 + // Strip shortcodes first (removes WPBakery, Elementor, etc.)
3056 + $content = strip_shortcodes($content);
3057 + $excerpt = strip_shortcodes($excerpt);
3058 +
3059 + // Additional regex-based shortcode removal as a safety net
3060 + $content = preg_replace('/\[(\[?)([a-zA-Z0-9_-]+)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*(?:\[(?!\/\2\])[^\[]*)*)\[\/\2\])?)(\]?)/s', '', $content);
3061 + $excerpt = preg_replace('/\[(\[?)([a-zA-Z0-9_-]+)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*(?:\[(?!\/\2\])[^\[]*)*)\[\/\2\])?)(\]?)/s', '', $excerpt);
3062 +
3063 + // Strip tags but preserve structure (don't use 'the_content' filter as it may re-add shortcodes)
3064 + $content = wp_strip_all_tags($content);
3065 +
3066 + // Combine title, short description (if exists), and content
3067 + $final_content = $title . "\n\n";
3068 +
3069 + // Add short description if it exists (WooCommerce products use post_excerpt for short description)
3070 + if (!empty($excerpt)) {
3071 + $final_content .= "Short Description: " . wp_strip_all_tags($excerpt) . "\n\n";
3072 + }
3073 +
3074 + $final_content .= $content;
3075 +
3076 + // For custom post types like job_listing, include additional fields
3077 + if ($post_type === 'job_listing') {
3078 + // Add job-specific meta if available
3079 + $job_location = get_post_meta($post_id, '_job_location', true);
3080 + if (!empty($job_location)) {
3081 + $final_content .= "\n\nLocation: " . $job_location;
3082 + }
3083 +
3084 + // Get job type terms
3085 + $job_types = get_the_terms($post_id, 'job_listing_type');
3086 + if (!empty($job_types) && !is_wp_error($job_types)) {
3087 + $types = array();
3088 + foreach ($job_types as $type) {
3089 + $types[] = $type->name;
3090 + }
3091 + $final_content .= "\n\nJob Type: " . implode(', ', $types);
3092 + }
3093 +
3094 + // Get company name if available
3095 + $company_name = get_post_meta($post_id, '_company_name', true);
3096 + if (!empty($company_name)) {
3097 + $final_content .= "\n\nCompany: " . $company_name;
3098 + }
3099 + }
3100 +
3101 + // Get API key with proper model detection
3102 + $options = get_option('mxchat_options');
3103 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
3104 +
3105 + if (strpos($selected_model, 'voyage') === 0) {
3106 + $api_key = $options['voyage_api_key'] ?? '';
3107 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
3108 + $api_key = $options['gemini_api_key'] ?? '';
3109 + } else {
3110 + $api_key = $options['api_key'] ?? '';
3111 + }
3112 +
3113 + if (empty($api_key)) {
3114 + return;
3115 + }
3116 +
3117 + // Use the centralized utility function for storage
3118 + $result = MxChat_Utils::submit_content_to_db(
3119 + $final_content,
3120 + $source_url,
3121 + $api_key,
3122 + md5($source_url) // Vector ID for Pinecone
3123 + );
3124 +
3125 + // After successful storage, apply role restriction based on tags
3126 + if (!is_wp_error($result)) {
3127 + $this->apply_role_restriction_to_post($post_id, $source_url);
3128 + }
3129 + }
3130 +
3131 + // Clean up the stored previous status if not used above
3132 + if ($previous_status !== 'publish' || $post->post_status === 'publish') {
3133 + delete_transient($previous_status_key);
3134 + delete_transient($previous_url_key);
3135 + }
3136 +}
3137 +
3138 +/**
3139 + * Store the post status and URL before update to detect status transitions
3140 + * This runs before the post is actually updated in the database
3141 + */
3142 +public function mxchat_store_pre_update_status($post_id, $data) {
3143 + // Get the current post from database (before update)
3144 + $current_post = get_post($post_id);
3145 +
3146 + if ($current_post) {
3147 + // Store the current status temporarily
3148 + $status_key = 'mxchat_prev_status_' . $post_id;
3149 + set_transient($status_key, $current_post->post_status, HOUR_IN_SECONDS);
3150 +
3151 + // If the post is currently published, also store its URL
3152 + if ($current_post->post_status === 'publish') {
3153 + $url_key = 'mxchat_prev_url_' . $post_id;
3154 + $current_url = get_permalink($post_id);
3155 + set_transient($url_key, $current_url, HOUR_IN_SECONDS);
3156 + }
3157 + }
3158 +}
3159 +
3160 +public function mxchat_handle_post_delete($post_id) {
3161 + // Get post data before it's deleted
3162 + $post = get_post($post_id);
3163 +
3164 + // Basic validation
3165 + if (!$post || wp_is_post_revision($post_id)) {
3166 + return;
3167 + }
3168 +
3169 + $post_type = $post->post_type;
3170 +
3171 + // Check if sync is enabled for this post type
3172 + $should_sync = false;
3173 +
3174 + // Check built-in post types first
3175 + if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
3176 + $should_sync = true;
3177 + } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
3178 + $should_sync = true;
3179 + } else {
3180 + // Check custom post types
3181 + $option_name = 'mxchat_auto_sync_' . $post_type;
3182 + if (get_option($option_name) === '1') {
3183 + $should_sync = true;
3184 + }
3185 + }
3186 +
3187 + if (!$should_sync) {
3188 + return;
3189 + }
3190 +
3191 + // Get the URL before post is deleted
3192 + $source_url = get_permalink($post_id);
3193 + if (!$source_url) {
3194 + //error_log('MXChat: Failed to get permalink for post ' . $post_id);
3195 + return;
3196 + }
3197 +
3198 + // Check if Pinecone is enabled
3199 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3200 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3201 +
3202 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
3203 + // Delete from Pinecone
3204 + $this->mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options);
3205 + } else {
3206 + // Delete from WordPress DB
3207 + global $wpdb;
3208 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3209 +
3210 + $result = $wpdb->delete(
3211 + $table_name,
3212 + array('source_url' => $source_url),
3213 + array('%s')
3214 + );
3215 +
3216 + if ($result === false) {
3217 + //error_log('MXChat: WordPress DB deletion failed for URL: ' . $source_url);
3218 + }
3219 + }
3220 +}
3221 +
3222 +
3223 + /**
3224 + * Deletes data from Pinecone using a source URL
3225 + */
3226 + public function mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options) {
3227 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
3228 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
3229 +
3230 + if (empty($host) || empty($api_key)) {
3231 + //error_log('MXChat: Pinecone deletion failed - missing configuration');
3232 + return false;
3233 + }
3234 +
3235 + $api_endpoint = "https://{$host}/vectors/delete";
3236 + $vector_id = md5($source_url);
3237 +
3238 + $request_body = array(
3239 + 'ids' => array($vector_id)
3240 + );
3241 +
3242 + $response = wp_remote_post($api_endpoint, array(
3243 + 'headers' => array(
3244 + 'Api-Key' => $api_key,
3245 + 'accept' => 'application/json',
3246 + 'content-type' => 'application/json'
3247 + ),
3248 + 'body' => wp_json_encode($request_body),
3249 + 'timeout' => 30
3250 + ));
3251 +
3252 + if (is_wp_error($response)) {
3253 + //error_log('MXChat: Pinecone deletion error - ' . $response->get_error_message());
3254 + return false;
3255 + }
3256 +
3257 + $response_code = wp_remote_retrieve_response_code($response);
3258 + if ($response_code !== 200) {
3259 + //error_log('MXChat: Pinecone deletion failed with status ' . $response_code);
3260 + return false;
3261 + }
3262 +
3263 + return true;
3264 + }
3265 +
3266 +
3267 +
3268 +public function mxchat_handle_product_change($post_id, $post, $update) {
3269 + if ($post->post_type !== 'product') {
3270 + return;
3271 + }
3272 +
3273 + if ($post->post_status === 'publish') {
3274 + add_action('shutdown', function() use ($post_id) {
3275 + $product = wc_get_product($post_id);
3276 + if ($product) {
3277 + $this->mxchat_store_product_embedding($product);
3278 + }
3279 + });
3280 + }
3281 +}
3282 +
3283 +/**
3284 + * Store WooCommerce product embeddings
3285 + */
3286 +private function mxchat_store_product_embedding($product) {
3287 + if (!isset($this->options['enable_woocommerce_integration']) ||
3288 + !in_array($this->options['enable_woocommerce_integration'], ['1', 'on'])) {
3289 + return;
3290 + }
3291 +
3292 + $source_url = get_permalink($product->get_id());
3293 +
3294 + // Build product content
3295 + $title = $product->get_name();
3296 + $description = $product->get_description();
3297 + $short_description = $product->get_short_description();
3298 + $regular_price = $product->get_regular_price();
3299 + $sale_price = $product->get_sale_price();
3300 + $sku = $product->get_sku();
3301 +
3302 + // Format content consistently
3303 + $content = $title . "\n\n";
3304 +
3305 + if (!empty($description)) {
3306 + $content .= wp_strip_all_tags($description) . "\n\n";
3307 + }
3308 +
3309 + if (!empty($short_description)) {
3310 + $content .= "Short Description: " . wp_strip_all_tags($short_description) . "\n\n";
3311 + }
3312 +
3313 + $content .= "Price: $" . $regular_price . "\n";
3314 +
3315 + if (!empty($sale_price)) {
3316 + $content .= "Sale Price: $" . $sale_price . "\n";
3317 + }
3318 +
3319 + if (!empty($sku)) {
3320 + $content .= "SKU: " . $sku . "\n";
3321 + }
3322 +
3323 + // Get API key with proper model detection
3324 + $options = get_option('mxchat_options');
3325 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
3326 +
3327 + if (strpos($selected_model, 'voyage') === 0) {
3328 + $api_key = $options['voyage_api_key'] ?? '';
3329 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
3330 + $api_key = $options['gemini_api_key'] ?? '';
3331 + } else {
3332 + $api_key = $options['api_key'] ?? '';
3333 + }
3334 +
3335 + if (empty($api_key)) {
3336 + //error_log('MxChat Auto-sync: No API key configured for embedding model');
3337 + return;
3338 + }
3339 +
3340 + // Use the centralized utility function for storage
3341 + $result = MxChat_Utils::submit_content_to_db(
3342 + $content,
3343 + $source_url,
3344 + $api_key,
3345 + md5($source_url) // Vector ID for Pinecone
3346 + );
3347 +
3348 + // After successful storage, apply role restriction based on tags
3349 + if (!is_wp_error($result)) {
3350 + $this->apply_role_restriction_to_post($product->get_id(), $source_url);
3351 + }
3352 +
3353 + if (is_wp_error($result)) {
3354 + //error_log('MxChat WooCommerce sync failed for product ' . $product->get_id() . ': ' . $result->get_error_message());
3355 + }
3356 +}
3357 +
3358 +public function mxchat_handle_product_delete($post_id) {
3359 + if (get_post_type($post_id) !== 'product') {
3360 + return;
3361 + }
3362 +
3363 + $source_url = get_permalink($post_id);
3364 +
3365 + // Check if Pinecone is enabled
3366 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3367 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3368 +
3369 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
3370 + // Delete from Pinecone
3371 + $this->mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options);
3372 + } else {
3373 + // Delete from WordPress DB
3374 + global $wpdb;
3375 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3376 +
3377 + $wpdb->delete(
3378 + $table_name,
3379 + array('source_url' => $source_url),
3380 + array('%s')
3381 + );
3382 + }
3383 +}
3384 +
3385 +/**
3386 + * Handle individual Pinecone content deletion
3387 + */
3388 +public function mxchat_handle_pinecone_prompt_delete() {
3389 + // Check permissions
3390 + if (!current_user_can('manage_options')) {
3391 + wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
3392 + }
3393 +
3394 + // Verify nonce
3395 + if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'mxchat_delete_pinecone_prompt_nonce')) {
3396 + wp_die(esc_html__('Security check failed.', 'mxchat'));
3397 + }
3398 +
3399 + $vector_id = isset($_GET['vector_id']) ? sanitize_text_field($_GET['vector_id']) : '';
3400 +
3401 + if (empty($vector_id)) {
3402 + set_transient('mxchat_admin_notice_error',
3403 + esc_html__('Invalid vector ID.', 'mxchat'),
3404 + 30
3405 + );
3406 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
3407 + exit;
3408 + }
3409 +
3410 + // Get Pinecone settings
3411 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3412 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3413 +
3414 + if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
3415 + set_transient('mxchat_admin_notice_error',
3416 + esc_html__('Pinecone is not properly configured.', 'mxchat'),
3417 + 30
3418 + );
3419 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
3420 + exit;
3421 + }
3422 +
3423 + // Delete from Pinecone
3424 + $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
3425 + $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
3426 + $vector_id,
3427 + $pinecone_options['mxchat_pinecone_api_key'],
3428 + $pinecone_options['mxchat_pinecone_host']
3429 + );
3430 +
3431 + if ($result['success']) {
3432 + // No cache clearing needed since we removed caching
3433 + set_transient('mxchat_admin_notice_success',
3434 + esc_html__('Entry deleted successfully from Pinecone.', 'mxchat'),
3435 + 30
3436 + );
3437 + } else {
3438 + set_transient('mxchat_admin_notice_error',
3439 + esc_html__('Failed to delete entry: ', 'mxchat') . $result['message'],
3440 + 30
3441 + );
3442 + }
3443 +
3444 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
3445 + exit;
3446 +}
3447 +/**
3448 + * Handle individual Pinecone content deletion via AJAX
3449 + */
3450 +public function ajax_mxchat_delete_pinecone_prompt() {
3451 + // Verify nonce and permissions
3452 + if (!check_ajax_referer('mxchat_delete_pinecone_prompt_nonce', 'nonce', false)) {
3453 + wp_send_json_error('Invalid nonce');
3454 + exit;
3455 + }
3456 +
3457 + if (!current_user_can('manage_options')) {
3458 + wp_send_json_error('Unauthorized access');
3459 + exit;
3460 + }
3461 +
3462 + $vector_id = isset($_POST['vector_id']) ? sanitize_text_field($_POST['vector_id']) : '';
3463 + $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
3464 +
3465 + if (empty($vector_id)) {
3466 + wp_send_json_error('Missing vector ID');
3467 + exit;
3468 + }
3469 +
3470 + // Get bot-specific Pinecone settings
3471 + $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
3472 + $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
3473 +
3474 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3475 +
3476 + if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
3477 + wp_send_json_error('Pinecone is not properly configured for bot: ' . $bot_id);
3478 + exit;
3479 + }
3480 +
3481 + // Delete from the correct Pinecone index
3482 + $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
3483 + $vector_id,
3484 + $pinecone_options['mxchat_pinecone_api_key'],
3485 + $pinecone_options['mxchat_pinecone_host']
3486 + );
3487 +
3488 + if ($result['success']) {
3489 + // No cache clearing needed since we removed caching
3490 + wp_send_json_success(array(
3491 + 'message' => 'Entry deleted successfully from Pinecone',
3492 + 'vector_id' => $vector_id,
3493 + 'bot_id' => $bot_id
3494 + ));
3495 + } else {
3496 + wp_send_json_error('Failed to delete from Pinecone: ' . $result['message']);
3497 + }
3498 +
3499 + exit;
3500 +}
3501 +
3502 +/**
3503 + * Get hierarchical roles for dropdown
3504 + */
3505 +public function mxchat_get_role_options() {
3506 + return array(
3507 + 'public' => __('Public (Everyone)', 'mxchat'),
3508 + 'logged_in' => __('Logged In Users', 'mxchat'),
3509 + 'subscriber' => __('Subscribers & Above', 'mxchat'),
3510 + 'contributor' => __('Contributors & Above', 'mxchat'),
3511 + 'author' => __('Authors & Above', 'mxchat'),
3512 + 'editor' => __('Editors & Above', 'mxchat'),
3513 + 'administrator' => __('Administrators Only', 'mxchat')
3514 + );
3515 +}
3516 +
3517 +/**
3518 + * Check if user has access to content based on role restriction
3519 + */
3520 +public function mxchat_user_has_content_access($role_restriction) {
3521 + // Public content is always accessible
3522 + if ($role_restriction === 'public' || empty($role_restriction)) {
3523 + return true;
3524 + }
3525 +
3526 + // Check if user is logged in for logged_in restriction
3527 + if ($role_restriction === 'logged_in') {
3528 + return is_user_logged_in();
3529 + }
3530 +
3531 + // If not logged in, no access to role-restricted content
3532 + if (!is_user_logged_in()) {
3533 + return false;
3534 + }
3535 +
3536 + $user = wp_get_current_user();
3537 + $user_roles = $user->roles;
3538 +
3539 + if (empty($user_roles)) {
3540 + return false;
3541 + }
3542 +
3543 + // Define role hierarchy (higher number = higher access)
3544 + $hierarchy = array(
3545 + 'subscriber' => 1,
3546 + 'contributor' => 2,
3547 + 'author' => 3,
3548 + 'editor' => 4,
3549 + 'administrator' => 5
3550 + );
3551 +
3552 + // Get required level
3553 + $required_level = isset($hierarchy[$role_restriction]) ? $hierarchy[$role_restriction] : 0;
3554 +
3555 + // Check if user has required level or higher
3556 + foreach ($user_roles as $user_role) {
3557 + $user_level = isset($hierarchy[$user_role]) ? $hierarchy[$user_role] : 0;
3558 + if ($user_level >= $required_level) {
3559 + return true;
3560 + }
3561 + }
3562 +
3563 + return false;
3564 +}
3565 +
3566 +/**
3567 + * Handle role restriction updates via AJAX
3568 + * Removed cache clearing call since we removed caching
3569 + */
3570 +public function ajax_mxchat_update_role_restriction() {
3571 + // Verify nonce and permissions
3572 + if (!check_ajax_referer('mxchat_update_role_nonce', 'nonce', false)) {
3573 + wp_send_json_error('Invalid nonce');
3574 + exit;
3575 + }
3576 +
3577 + if (!current_user_can('manage_options')) {
3578 + wp_send_json_error('Unauthorized access');
3579 + exit;
3580 + }
3581 +
3582 + $entry_id = isset($_POST['entry_id']) ? sanitize_text_field($_POST['entry_id']) : '';
3583 + $role_restriction = isset($_POST['role_restriction']) ? sanitize_text_field($_POST['role_restriction']) : 'public';
3584 + $data_source = isset($_POST['data_source']) ? sanitize_text_field($_POST['data_source']) : 'wordpress';
3585 +
3586 + if (empty($entry_id)) {
3587 + wp_send_json_error('Invalid entry ID');
3588 + exit;
3589 + }
3590 +
3591 + // Get knowledge manager instance to validate role restriction
3592 + $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
3593 + $valid_roles = array_keys($knowledge_manager->mxchat_get_role_options());
3594 + if (!in_array($role_restriction, $valid_roles)) {
3595 + wp_send_json_error('Invalid role restriction');
3596 + exit;
3597 + }
3598 +
3599 + global $wpdb;
3600 +
3601 + if ($data_source === 'pinecone') {
3602 + // Handle Pinecone role restriction (stored separately in WordPress table)
3603 + $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
3604 +
3605 + // Use REPLACE to insert or update the role restriction
3606 + $result = $wpdb->replace(
3607 + $roles_table,
3608 + array(
3609 + 'vector_id' => $entry_id,
3610 + 'role_restriction' => $role_restriction,
3611 + 'updated_at' => current_time('mysql')
3612 + ),
3613 + array('%s', '%s', '%s')
3614 + );
3615 +
3616 + // No cache clearing needed since we removed caching
3617 +
3618 + } else {
3619 + // Handle WordPress database role restriction (existing functionality)
3620 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3621 +
3622 + $result = $wpdb->update(
3623 + $table_name,
3624 + array('role_restriction' => $role_restriction),
3625 + array('id' => absint($entry_id)),
3626 + array('%s'),
3627 + array('%d')
3628 + );
3629 + }
3630 +
3631 + if ($result === false) {
3632 + wp_send_json_error('Database update failed: ' . $wpdb->last_error);
3633 + exit;
3634 + }
3635 +
3636 + wp_send_json_success(array(
3637 + 'message' => 'Role restriction updated successfully',
3638 + 'role_restriction' => $role_restriction,
3639 + 'data_source' => $data_source,
3640 + 'entry_id' => $entry_id
3641 + ));
3642 + exit;
3643 +}
3644 +
3645 +// ========================================
3646 +// ROLE-BASED CONTENT RESTRICTIONS - NEW FUNCTIONS
3647 +// Add these to your MxChat_Knowledge_Manager class
3648 +// ========================================
3649 +
3650 +/**
3651 + * Initialize role-based content hooks
3652 + * Add this call to your __construct() or mxchat_init_hooks() method
3653 + */
3654 +private function mxchat_init_role_hooks() {
3655 + // AJAX handlers for tag-role mappings
3656 + add_action('wp_ajax_mxchat_add_tag_role_mapping', array($this, 'ajax_add_tag_role_mapping'));
3657 + add_action('wp_ajax_mxchat_delete_tag_role_mapping', array($this, 'ajax_delete_tag_role_mapping'));
3658 + add_action('wp_ajax_mxchat_get_tag_role_mappings', array($this, 'ajax_get_tag_role_mappings'));
3659 + add_action('wp_ajax_mxchat_bulk_update_tag_roles', array($this, 'ajax_bulk_update_tag_roles'));
3660 +
3661 + // Hook to automatically update role restrictions when tags are added/removed
3662 + add_action('set_object_terms', array($this, 'handle_tag_change'), 10, 6);
3663 +
3664 + // Hook to apply role restrictions on auto-sync
3665 + add_action('mxchat_content_stored', array($this, 'apply_role_restriction_after_storage'), 10, 2);
3666 +}
3667 +
3668 +/**
3669 + * Add tag-role mapping via AJAX
3670 + */
3671 +public function ajax_add_tag_role_mapping() {
3672 + // Verify nonce and permissions
3673 + check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
3674 +
3675 + if (!current_user_can('manage_options')) {
3676 + wp_send_json_error('Unauthorized access');
3677 + exit;
3678 + }
3679 +
3680 + $tag_slug = isset($_POST['tag_slug']) ? sanitize_text_field($_POST['tag_slug']) : '';
3681 + $role_restriction = isset($_POST['role_restriction']) ? sanitize_text_field($_POST['role_restriction']) : 'public';
3682 +
3683 + if (empty($tag_slug)) {
3684 + wp_send_json_error('Tag slug is required');
3685 + exit;
3686 + }
3687 +
3688 + // Validate role restriction
3689 + $valid_roles = array_keys($this->mxchat_get_role_options());
3690 + if (!in_array($role_restriction, $valid_roles)) {
3691 + wp_send_json_error('Invalid role restriction');
3692 + exit;
3693 + }
3694 +
3695 + // Check if tag exists in WordPress
3696 + $term = get_term_by('slug', $tag_slug, 'post_tag');
3697 + if (!$term) {
3698 + wp_send_json_error('Tag does not exist in WordPress');
3699 + exit;
3700 + }
3701 +
3702 + // Get existing mappings
3703 + $mappings = get_option('mxchat_tag_role_mappings', array());
3704 +
3705 + // Check if mapping already exists
3706 + if (isset($mappings[$tag_slug])) {
3707 + wp_send_json_error('Mapping for this tag already exists');
3708 + exit;
3709 + }
3710 +
3711 + // Add new mapping
3712 + $mappings[$tag_slug] = $role_restriction;
3713 + update_option('mxchat_tag_role_mappings', $mappings);
3714 +
3715 + wp_send_json_success(array(
3716 + 'message' => 'Tag-role mapping added successfully',
3717 + 'tag_slug' => $tag_slug,
3718 + 'role_restriction' => $role_restriction
3719 + ));
3720 + exit;
3721 +}
3722 +
3723 +/**
3724 + * Delete tag-role mapping via AJAX
3725 + */
3726 +public function ajax_delete_tag_role_mapping() {
3727 + // Verify nonce and permissions
3728 + check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
3729 +
3730 + if (!current_user_can('manage_options')) {
3731 + wp_send_json_error('Unauthorized access');
3732 + exit;
3733 + }
3734 +
3735 + $tag_slug = isset($_POST['tag_slug']) ? sanitize_text_field($_POST['tag_slug']) : '';
3736 +
3737 + if (empty($tag_slug)) {
3738 + wp_send_json_error('Tag slug is required');
3739 + exit;
3740 + }
3741 +
3742 + // Get existing mappings
3743 + $mappings = get_option('mxchat_tag_role_mappings', array());
3744 +
3745 + // Check if mapping exists
3746 + if (!isset($mappings[$tag_slug])) {
3747 + wp_send_json_error('Mapping does not exist');
3748 + exit;
3749 + }
3750 +
3751 + // Remove mapping
3752 + unset($mappings[$tag_slug]);
3753 + update_option('mxchat_tag_role_mappings', $mappings);
3754 +
3755 + wp_send_json_success(array(
3756 + 'message' => 'Tag-role mapping deleted successfully',
3757 + 'tag_slug' => $tag_slug
3758 + ));
3759 + exit;
3760 +}
3761 +
3762 +/**
3763 + * Get all tag-role mappings via AJAX
3764 + */
3765 +public function ajax_get_tag_role_mappings() {
3766 + // Verify nonce and permissions
3767 + check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
3768 +
3769 + if (!current_user_can('manage_options')) {
3770 + wp_send_json_error('Unauthorized access');
3771 + exit;
3772 + }
3773 +
3774 + // Get mappings
3775 + $mappings = get_option('mxchat_tag_role_mappings', array());
3776 + $role_options = $this->mxchat_get_role_options();
3777 +
3778 + $formatted_mappings = array();
3779 +
3780 + foreach ($mappings as $tag_slug => $role_restriction) {
3781 + // Get tag object
3782 + $term = get_term_by('slug', $tag_slug, 'post_tag');
3783 +
3784 + // Count posts with this tag
3785 + $post_count = 0;
3786 + if ($term) {
3787 + $post_count = $term->count;
3788 + }
3789 +
3790 + $formatted_mappings[] = array(
3791 + 'tag_slug' => $tag_slug,
3792 + 'role_restriction' => $role_restriction,
3793 + 'role_label' => $role_options[$role_restriction] ?? $role_restriction,
3794 + 'post_count' => $post_count
3795 + );
3796 + }
3797 +
3798 + wp_send_json_success(array(
3799 + 'mappings' => $formatted_mappings
3800 + ));
3801 + exit;
3802 +}
3803 +
3804 +/**
3805 + * Bulk update role restrictions for all existing content with mapped tags
3806 + */
3807 +public function ajax_bulk_update_tag_roles() {
3808 + // Verify nonce and permissions
3809 + check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
3810 +
3811 + if (!current_user_can('manage_options')) {
3812 + wp_send_json_error('Unauthorized access');
3813 + exit;
3814 + }
3815 +
3816 + // Get mappings
3817 + $mappings = get_option('mxchat_tag_role_mappings', array());
3818 +
3819 + if (empty($mappings)) {
3820 + wp_send_json_error('No tag-role mappings found');
3821 + exit;
3822 + }
3823 +
3824 + global $wpdb;
3825 +
3826 + // Check if using Pinecone
3827 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3828 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3829 +
3830 + $updated_count = 0;
3831 + $details = array();
3832 +
3833 + foreach ($mappings as $tag_slug => $role_restriction) {
3834 + // Get all posts with this tag
3835 + $posts = get_posts(array(
3836 + 'tag' => $tag_slug,
3837 + 'post_type' => 'any',
3838 + 'posts_per_page' => -1,
3839 + 'fields' => 'ids',
3840 + 'post_status' => 'publish'
3841 + ));
3842 +
3843 + if (empty($posts)) {
3844 + continue;
3845 + }
3846 +
3847 + $tag_updated = 0;
3848 +
3849 + foreach ($posts as $post_id) {
3850 + $source_url = get_permalink($post_id);
3851 + if (!$source_url) {
3852 + continue;
3853 + }
3854 +
3855 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
3856 + // Update Pinecone role restriction
3857 + $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
3858 + $vector_id = md5($source_url);
3859 +
3860 + $result = $wpdb->replace(
3861 + $roles_table,
3862 + array(
3863 + 'vector_id' => $vector_id,
3864 + 'role_restriction' => $role_restriction,
3865 + 'updated_at' => current_time('mysql')
3866 + ),
3867 + array('%s', '%s', '%s')
3868 + );
3869 + } else {
3870 + // Update WordPress DB
3871 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3872 +
3873 + $result = $wpdb->update(
3874 + $table_name,
3875 + array('role_restriction' => $role_restriction),
3876 + array('source_url' => $source_url),
3877 + array('%s'),
3878 + array('%s')
3879 + );
3880 + }
3881 +
3882 + if ($result !== false) {
3883 + $tag_updated++;
3884 + $updated_count++;
3885 + }
3886 + }
3887 +
3888 + if ($tag_updated > 0) {
3889 + $details[] = sprintf(
3890 + 'Tag "%s" (%s): %d posts updated',
3891 + $tag_slug,
3892 + $role_restriction,
3893 + $tag_updated
3894 + );
3895 + }
3896 + }
3897 +
3898 + wp_send_json_success(array(
3899 + 'message' => 'Bulk update completed',
3900 + 'updated_count' => $updated_count,
3901 + 'tags_processed' => count($mappings),
3902 + 'details' => $details
3903 + ));
3904 + exit;
3905 +}
3906 +
3907 +/**
3908 + * Handle tag changes on posts (when tags are added or removed)
3909 + */
3910 +public function handle_tag_change($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {
3911 + // Only process post tags
3912 + if ($taxonomy !== 'post_tag') {
3913 + return;
3914 + }
3915 +
3916 + // Get tag-role mappings
3917 + $mappings = get_option('mxchat_tag_role_mappings', array());
3918 +
3919 + if (empty($mappings)) {
3920 + return;
3921 + }
3922 +
3923 + // Get the post's URL
3924 + $source_url = get_permalink($object_id);
3925 + if (!$source_url) {
3926 + return;
3927 + }
3928 +
3929 + // Determine the highest role restriction based on tags
3930 + $highest_role = 'public';
3931 + $role_hierarchy = array(
3932 + 'public' => 0,
3933 + 'logged_in' => 1,
3934 + 'subscriber' => 2,
3935 + 'contributor' => 3,
3936 + 'author' => 4,
3937 + 'editor' => 5,
3938 + 'administrator' => 6
3939 + );
3940 +
3941 + // Get all current tags for the post
3942 + $current_tags = wp_get_post_tags($object_id, array('fields' => 'slugs'));
3943 +
3944 + // Find the highest role restriction among the tags
3945 + foreach ($current_tags as $tag_slug) {
3946 + if (isset($mappings[$tag_slug])) {
3947 + $role = $mappings[$tag_slug];
3948 + if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
3949 + $highest_role = $role;
3950 + }
3951 + }
3952 + }
3953 +
3954 + // Update the role restriction in the database
3955 + global $wpdb;
3956 +
3957 + // Check if using Pinecone
3958 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3959 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3960 +
3961 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
3962 + // Update Pinecone role restriction
3963 + $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
3964 + $vector_id = md5($source_url);
3965 +
3966 + $wpdb->replace(
3967 + $roles_table,
3968 + array(
3969 + 'vector_id' => $vector_id,
3970 + 'role_restriction' => $highest_role,
3971 + 'updated_at' => current_time('mysql')
3972 + ),
3973 + array('%s', '%s', '%s')
3974 + );
3975 + } else {
3976 + // Update WordPress DB
3977 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3978 +
3979 + $wpdb->update(
3980 + $table_name,
3981 + array('role_restriction' => $highest_role),
3982 + array('source_url' => $source_url),
3983 + array('%s'),
3984 + array('%s')
3985 + );
3986 + }
3987 +}
3988 +
3989 +/**
3990 + * Apply role restriction after content is stored (for auto-sync)
3991 + */
3992 +public function apply_role_restriction_after_storage($post_id, $source_url) {
3993 + // Get tag-role mappings
3994 + $mappings = get_option('mxchat_tag_role_mappings', array());
3995 +
3996 + if (empty($mappings)) {
3997 + return;
3998 + }
3999 +
4000 + // Get all tags for the post
4001 + $post_tags = wp_get_post_tags($post_id, array('fields' => 'slugs'));
4002 +
4003 + if (empty($post_tags)) {
4004 + return;
4005 + }
4006 +
4007 + // Determine the highest role restriction based on tags
4008 + $highest_role = 'public';
4009 + $role_hierarchy = array(
4010 + 'public' => 0,
4011 + 'logged_in' => 1,
4012 + 'subscriber' => 2,
4013 + 'contributor' => 3,
4014 + 'author' => 4,
4015 + 'editor' => 5,
4016 + 'administrator' => 6
4017 + );
4018 +
4019 + foreach ($post_tags as $tag_slug) {
4020 + if (isset($mappings[$tag_slug])) {
4021 + $role = $mappings[$tag_slug];
4022 + if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
4023 + $highest_role = $role;
4024 + }
4025 + }
4026 + }
4027 +
4028 + // If no restricted tags found, return (leave as public)
4029 + if ($highest_role === 'public') {
4030 + return;
4031 + }
4032 +
4033 + // Update the role restriction
4034 + global $wpdb;
4035 +
4036 + // Check if using Pinecone
4037 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
4038 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4039 +
4040 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
4041 + // Update Pinecone role restriction
4042 + $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
4043 + $vector_id = md5($source_url);
4044 +
4045 + $wpdb->replace(
4046 + $roles_table,
4047 + array(
4048 + 'vector_id' => $vector_id,
4049 + 'role_restriction' => $highest_role,
4050 + 'updated_at' => current_time('mysql')
4051 + ),
4052 + array('%s', '%s', '%s')
4053 + );
4054 + } else {
4055 + // Update WordPress DB
4056 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4057 +
4058 + $wpdb->update(
4059 + $table_name,
4060 + array('role_restriction' => $highest_role),
4061 + array('source_url' => $source_url),
4062 + array('%s'),
4063 + array('%s')
4064 + );
4065 + }
4066 +}
4067 +
4068 +
4069 + // ========================================
4070 + // HELPER METHODS
4071 + // ========================================
4072 +
4073 + /**
4074 + * Check if user has required permissions for content processing
4075 + */
4076 + private function mxchat_check_user_permissions() {
4077 + if (!current_user_can('manage_options')) {
4078 + wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
4079 + }
4080 + }
4081 +
4082 + /**
4083 + * Validate nonce for security
4084 + */
4085 + private function mxchat_validate_nonce($nonce_name, $nonce_action) {
4086 + if (!isset($_POST[$nonce_name]) || !wp_verify_nonce($_POST[$nonce_name], $nonce_action)) {
4087 + wp_die(esc_html__('Security check failed.', 'mxchat'));
4088 + }
4089 + }
4090 +
4091 + /**
4092 + * Get embedding API credentials
4093 + */
4094 + private function mxchat_get_embedding_credentials() {
4095 + $embedding_model = $this->options['embedding_model'] ?? 'text-embedding-ada-002';
4096 +
4097 + if (strpos($embedding_model, 'text-embedding-') !== false) {
4098 + return array(
4099 + 'type' => 'openai',
4100 + 'api_key' => $this->options['api_key'] ?? ''
4101 + );
4102 + } elseif (strpos($embedding_model, 'voyage-') !== false) {
4103 + return array(
4104 + 'type' => 'voyage',
4105 + 'api_key' => $this->options['voyage_api_key'] ?? ''
4106 + );
4107 + } elseif (strpos($embedding_model, 'gemini-embedding-') !== false) {
4108 + return array(
4109 + 'type' => 'gemini',
4110 + 'api_key' => $this->options['gemini_api_key'] ?? ''
4111 + );
4112 + }
4113 +
4114 + return array('type' => 'unknown', 'api_key' => '');
4115 + }
4116 +
4117 + /**
4118 + * Log processing errors
4119 + */
4120 + private function mxchat_log_processing_error($operation, $error_message) {
4121 + //error_log("MxChat Knowledge Processing {$operation} Error: " . $error_message);
4122 + }
4123 +
4124 + /**
4125 + * Set admin notice transient
4126 + */
4127 + private function mxchat_set_admin_notice($type, $message) {
4128 + set_transient("mxchat_admin_notice_{$type}", $message, 30);
4129 + }
4130 +
4131 + /**
4132 + * Get Pinecone manager instance for vector operations
4133 + */
4134 + private function mxchat_get_pinecone_manager() {
4135 + return MxChat_Pinecone_Manager::get_instance();
4136 + }
4137 +
4138 +
4139 + // ========================================
4140 +// DATABASE QUEUE TABLE MANAGEMENT
4141 +// ========================================
4142 +
4143 +/**
4144 + * Create queue table on plugin activation
4145 + * Call this from your plugin activation hook
4146 + */
4147 +public function mxchat_create_queue_table() {
4148 + global $wpdb;
4149 +
4150 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
4151 + $charset_collate = $wpdb->get_charset_collate();
4152 +
4153 + $sql = "CREATE TABLE IF NOT EXISTS $table_name (
4154 + id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
4155 + queue_id varchar(64) NOT NULL,
4156 + item_type varchar(20) NOT NULL,
4157 + item_data longtext NOT NULL,
4158 + status varchar(20) NOT NULL DEFAULT 'pending',
4159 + bot_id varchar(50) NOT NULL DEFAULT 'default',
4160 + priority int(11) NOT NULL DEFAULT 0,
4161 + attempts int(11) NOT NULL DEFAULT 0,
4162 + max_attempts int(11) NOT NULL DEFAULT 3,
4163 + error_message text DEFAULT NULL,
4164 + created_at datetime NOT NULL,
4165 + started_at datetime DEFAULT NULL,
4166 + completed_at datetime DEFAULT NULL,
4167 + PRIMARY KEY (id),
4168 + KEY queue_id (queue_id),
4169 + KEY status (status),
4170 + KEY item_type (item_type),
4171 + KEY priority (priority)
4172 + ) $charset_collate;";
4173 +
4174 + require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
4175 + dbDelta($sql);
4176 +
4177 + // Also create a meta table for queue metadata
4178 + $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
4179 +
4180 + $meta_sql = "CREATE TABLE IF NOT EXISTS $meta_table (
4181 + id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
4182 + queue_id varchar(64) NOT NULL,
4183 + meta_key varchar(255) NOT NULL,
4184 + meta_value longtext,
4185 + PRIMARY KEY (id),
4186 + KEY queue_id (queue_id),
4187 + KEY meta_key (meta_key)
4188 + ) $charset_collate;";
4189 +
4190 + dbDelta($meta_sql);
4191 +}
4192 +
4193 +/**
4194 + * Add items to the processing queue
4195 + *
4196 + * @param string $queue_id Unique identifier for this queue batch
4197 + * @param string $item_type Type of item (url, pdf_page)
4198 + * @param array $items Array of items to queue
4199 + * @param string $bot_id Bot ID for processing
4200 + * @return int Number of items queued
4201 + */
4202 +private function mxchat_add_to_queue($queue_id, $item_type, $items, $bot_id = 'default') {
4203 + global $wpdb;
4204 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
4205 +
4206 + $queued_count = 0;
4207 + $priority = 0;
4208 +
4209 + foreach ($items as $item) {
4210 + $result = $wpdb->insert(
4211 + $table_name,
4212 + array(
4213 + 'queue_id' => $queue_id,
4214 + 'item_type' => $item_type,
4215 + 'item_data' => wp_json_encode($item),
4216 + 'status' => 'pending',
4217 + 'bot_id' => $bot_id,
4218 + 'priority' => $priority,
4219 + 'attempts' => 0,
4220 + 'max_attempts' => 3,
4221 + 'created_at' => current_time('mysql')
4222 + ),
4223 + array('%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%s')
4224 + );
4225 +
4226 + if ($result) {
4227 + $queued_count++;
4228 + }
4229 +
4230 + $priority++; // Process in order
4231 + }
4232 +
4233 + return $queued_count;
4234 +}
4235 +
4236 +/**
4237 + * Store queue metadata (total counts, source URL, etc.)
4238 + */
4239 +private function mxchat_set_queue_meta($queue_id, $meta_key, $meta_value) {
4240 + global $wpdb;
4241 + $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
4242 +
4243 + // Check if meta exists
4244 + $existing = $wpdb->get_var($wpdb->prepare(
4245 + "SELECT id FROM $meta_table WHERE queue_id = %s AND meta_key = %s",
4246 + $queue_id,
4247 + $meta_key
4248 + ));
4249 +
4250 + if ($existing) {
4251 + // Update
4252 + $wpdb->update(
4253 + $meta_table,
4254 + array('meta_value' => maybe_serialize($meta_value)),
4255 + array('queue_id' => $queue_id, 'meta_key' => $meta_key),
4256 + array('%s'),
4257 + array('%s', '%s')
4258 + );
4259 + } else {
4260 + // Insert
4261 + $wpdb->insert(
4262 + $meta_table,
4263 + array(
4264 + 'queue_id' => $queue_id,
4265 + 'meta_key' => $meta_key,
4266 + 'meta_value' => maybe_serialize($meta_value)
4267 + ),
4268 + array('%s', '%s', '%s')
4269 + );
4270 + }
4271 +}
4272 +
4273 +/**
4274 + * Get queue metadata
4275 + */
4276 +private function mxchat_get_queue_meta($queue_id, $meta_key) {
4277 + global $wpdb;
4278 + $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
4279 +
4280 + $value = $wpdb->get_var($wpdb->prepare(
4281 + "SELECT meta_value FROM $meta_table WHERE queue_id = %s AND meta_key = %s",
4282 + $queue_id,
4283 + $meta_key
4284 + ));
4285 +
4286 + return maybe_unserialize($value);
4287 +}
4288 +
4289 +// ========================================
4290 +// AJAX QUEUE PROCESSING HANDLERS
4291 +// ========================================
4292 +
4293 +/**
4294 + * AJAX: Get next item from queue to process
4295 + */
4296 +public function ajax_mxchat_get_next_queue_item() {
4297 + // Verify nonce and permissions
4298 + check_ajax_referer('mxchat_queue_nonce', 'nonce');
4299 +
4300 + if (!current_user_can('manage_options')) {
4301 + wp_send_json_error('Unauthorized access');
4302 + }
4303 +
4304 + $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
4305 +
4306 + if (empty($queue_id)) {
4307 + wp_send_json_error('Missing queue ID');
4308 + }
4309 +
4310 + global $wpdb;
4311 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
4312 +
4313 + // Get next pending item with retry logic for failed items
4314 + $next_item = $wpdb->get_row($wpdb->prepare(
4315 + "SELECT * FROM $table_name
4316 + WHERE queue_id = %s
4317 + AND status IN ('pending', 'failed')
4318 + AND attempts < max_attempts
4319 + ORDER BY priority ASC, id ASC
4320 + LIMIT 1",
4321 + $queue_id
4322 + ));
4323 +
4324 + if (!$next_item) {
4325 + // No more items - queue complete
4326 + wp_send_json_success(array(
4327 + 'complete' => true,
4328 + 'message' => 'Queue processing complete'
4329 + ));
4330 + }
4331 +
4332 + // Mark item as processing
4333 + $wpdb->update(
4334 + $table_name,
4335 + array(
4336 + 'status' => 'processing',
4337 + 'started_at' => current_time('mysql'),
4338 + 'attempts' => $next_item->attempts + 1
4339 + ),
4340 + array('id' => $next_item->id),
4341 + array('%s', '%s', '%d'),
4342 + array('%d')
4343 + );
4344 +
4345 + wp_send_json_success(array(
4346 + 'complete' => false,
4347 + 'item' => array(
4348 + 'id' => $next_item->id,
4349 + 'type' => $next_item->item_type,
4350 + 'data' => json_decode($next_item->item_data, true),
4351 + 'bot_id' => $next_item->bot_id,
4352 + 'attempt' => $next_item->attempts + 1
4353 + )
4354 + ));
4355 +}
4356 +
4357 +/**
4358 + * AJAX: Process a single queue item
4359 + */
4360 +public function ajax_mxchat_process_queue_item() {
4361 + // Verify nonce and permissions
4362 + check_ajax_referer('mxchat_queue_nonce', 'nonce');
4363 +
4364 + if (!current_user_can('manage_options')) {
4365 + wp_send_json_error('Unauthorized access');
4366 + }
4367 +
4368 + $item_id = isset($_POST['item_id']) ? absint($_POST['item_id']) : 0;
4369 + $item_type = isset($_POST['item_type']) ? sanitize_text_field($_POST['item_type']) : '';
4370 + $item_data = isset($_POST['item_data']) ? $_POST['item_data'] : array();
4371 + $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
4372 +
4373 + if (empty($item_id) || empty($item_type)) {
4374 + wp_send_json_error('Missing item data');
4375 + }
4376 +
4377 + global $wpdb;
4378 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
4379 +
4380 + // Process based on item type
4381 + try {
4382 + set_time_limit(60); // Give processing 60 seconds
4383 +
4384 + $result = false;
4385 + $error_message = '';
4386 +
4387 + switch ($item_type) {
4388 + case 'url':
4389 + $result = $this->mxchat_process_queue_url($item_data, $bot_id);
4390 + break;
4391 +
4392 + case 'pdf_page':
4393 + $result = $this->mxchat_process_queue_pdf_page($item_data, $bot_id);
4394 + break;
4395 +
4396 + default:
4397 + throw new Exception('Unknown item type: ' . $item_type);
4398 + }
4399 +
4400 + if (is_wp_error($result)) {
4401 + throw new Exception($result->get_error_message());
4402 + }
4403 +
4404 + if ($result === false) {
4405 + throw new Exception('Processing returned false - item may be empty or invalid');
4406 + }
4407 +
4408 + // Mark as completed
4409 + $wpdb->update(
4410 + $table_name,
4411 + array(
4412 + 'status' => 'completed',
4413 + 'completed_at' => current_time('mysql'),
4414 + 'error_message' => null
4415 + ),
4416 + array('id' => $item_id),
4417 + array('%s', '%s', '%s'),
4418 + array('%d')
4419 + );
4420 +
4421 + wp_send_json_success(array(
4422 + 'processed' => true,
4423 + 'item_id' => $item_id,
4424 + 'message' => 'Item processed successfully'
4425 + ));
4426 +
4427 + } catch (Exception $e) {
4428 + $error_message = $e->getMessage();
4429 +
4430 + // Get current attempt count
4431 + $item = $wpdb->get_row($wpdb->prepare(
4432 + "SELECT attempts, max_attempts FROM $table_name WHERE id = %d",
4433 + $item_id
4434 + ));
4435 +
4436 + // Check if we've exhausted retries
4437 + if ($item && $item->attempts >= $item->max_attempts) {
4438 + // Permanently failed
4439 + $wpdb->update(
4440 + $table_name,
4441 + array(
4442 + 'status' => 'failed',
4443 + 'error_message' => $error_message
4444 + ),
4445 + array('id' => $item_id),
4446 + array('%s', '%s'),
4447 + array('%d')
4448 + );
4449 +
4450 + wp_send_json_error(array(
4451 + 'message' => 'Item failed after maximum attempts: ' . $error_message,
4452 + 'permanent_failure' => true,
4453 + 'item_id' => $item_id
4454 + ));
4455 + } else {
4456 + // Mark for retry
4457 + $wpdb->update(
4458 + $table_name,
4459 + array(
4460 + 'status' => 'failed',
4461 + 'error_message' => $error_message
4462 + ),
4463 + array('id' => $item_id),
4464 + array('%s', '%s'),
4465 + array('%d')
4466 + );
4467 +
4468 + wp_send_json_error(array(
4469 + 'message' => 'Item processing failed, will retry: ' . $error_message,
4470 + 'can_retry' => true,
4471 + 'item_id' => $item_id,
4472 + 'attempts' => $item ? $item->attempts : 0
4473 + ));
4474 + }
4475 + }
4476 +}
4477 +
4478 +/**
4479 + * Process a URL from the queue
4480 + */
4481 +private function mxchat_process_queue_url($item_data, $bot_id = 'default') {
4482 + $url = isset($item_data['url']) ? $item_data['url'] : '';
4483 +
4484 + if (empty($url)) {
4485 + return new WP_Error('invalid_url', 'URL is empty');
4486 + }
4487 +
4488 + // Fetch URL content
4489 + $response = wp_remote_get($url, array(
4490 + 'timeout' => 30,
4491 + 'redirection' => 5,
4492 + 'user-agent' => 'MxChat/1.0'
4493 + ));
4494 +
4495 + if (is_wp_error($response)) {
4496 + return $response;
4497 + }
4498 +
4499 + $response_code = wp_remote_retrieve_response_code($response);
4500 + if ($response_code !== 200) {
4501 + return new WP_Error('http_error', 'HTTP ' . $response_code . ' error');
4502 + }
4503 +
4504 + $html = wp_remote_retrieve_body($response);
4505 +
4506 + if (empty($html)) {
4507 + return new WP_Error('empty_response', 'Empty response body');
4508 + }
4509 +
4510 + // Extract and sanitize content
4511 + $content = $this->mxchat_extract_main_content($html);
4512 + $sanitized = $this->mxchat_sanitize_content_for_api($content);
4513 +
4514 + if (empty($sanitized)) {
4515 + // Not an error - just no content found (maybe a redirect or empty page)
4516 + return false;
4517 + }
4518 +
4519 + // Get bot-specific API key
4520 + $bot_options = $this->get_bot_options($bot_id);
4521 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
4522 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4523 +
4524 + if (strpos($selected_model, 'voyage') === 0) {
4525 + $api_key = $options['voyage_api_key'] ?? '';
4526 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
4527 + $api_key = $options['gemini_api_key'] ?? '';
4528 + } else {
4529 + $api_key = $options['api_key'] ?? '';
4530 + }
4531 +
4532 + if (empty($api_key)) {
4533 + return new WP_Error('no_api_key', 'API key not configured for bot: ' . $bot_id);
4534 + }
4535 +
4536 + // Submit to database
4537 + $result = MxChat_Utils::submit_content_to_db(
4538 + $sanitized,
4539 + $url,
4540 + $api_key,
4541 + null,
4542 + $bot_id
4543 + );
4544 +
4545 + return $result;
4546 +}
4547 +
4548 +/**
4549 + * Process a PDF page from the queue
4550 + */
4551 +private function mxchat_process_queue_pdf_page($item_data, $bot_id = 'default') {
4552 + $pdf_path = isset($item_data['pdf_path']) ? $item_data['pdf_path'] : '';
4553 + $pdf_url = isset($item_data['pdf_url']) ? $item_data['pdf_url'] : '';
4554 + $page_number = isset($item_data['page_number']) ? absint($item_data['page_number']) : 0;
4555 + $total_pages = isset($item_data['total_pages']) ? absint($item_data['total_pages']) : 0;
4556 +
4557 + if (empty($pdf_path) || !file_exists($pdf_path)) {
4558 + return new WP_Error('pdf_not_found', 'PDF file not found: ' . $pdf_path);
4559 + }
4560 +
4561 + if ($page_number < 1) {
4562 + return new WP_Error('invalid_page', 'Invalid page number');
4563 + }
4564 +
4565 + try {
4566 + $parser = new \Smalot\PdfParser\Parser();
4567 + $pdf = $parser->parseFile($pdf_path);
4568 + $pages = $pdf->getPages();
4569 +
4570 + if (!isset($pages[$page_number - 1])) {
4571 + return new WP_Error('page_not_found', 'Page ' . $page_number . ' not found in PDF');
4572 + }
4573 +
4574 + $text = $pages[$page_number - 1]->getText();
4575 +
4576 + if (empty($text)) {
4577 + // Not an error - just an empty page
4578 + return false;
4579 + }
4580 +
4581 + $sanitized = $this->mxchat_sanitize_content_for_api($text);
4582 +
4583 + if (empty($sanitized)) {
4584 + return false;
4585 + }
4586 +
4587 + // Create metadata
4588 + $metadata = array(
4589 + 'document_type' => 'pdf',
4590 + 'total_pages' => $total_pages,
4591 + 'current_page' => $page_number,
4592 + 'source_url' => $pdf_url
4593 + );
4594 +
4595 + $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized;
4596 + $page_url = esc_url($pdf_url . "#page=" . $page_number);
4597 +
4598 + // Get bot-specific API key
4599 + $bot_options = $this->get_bot_options($bot_id);
4600 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
4601 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4602 +
4603 + if (strpos($selected_model, 'voyage') === 0) {
4604 + $api_key = $options['voyage_api_key'] ?? '';
4605 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
4606 + $api_key = $options['gemini_api_key'] ?? '';
4607 + } else {
4608 + $api_key = $options['api_key'] ?? '';
4609 + }
4610 +
4611 + if (empty($api_key)) {
4612 + return new WP_Error('no_api_key', 'API key not configured for bot: ' . $bot_id);
4613 + }
4614 +
4615 + // Submit to database
4616 + $result = MxChat_Utils::submit_content_to_db(
4617 + $content_with_metadata,
4618 + $page_url,
4619 + $api_key,
4620 + null,
4621 + $bot_id
4622 + );
4623 +
4624 + return $result;
4625 +
4626 + } catch (Exception $e) {
4627 + return new WP_Error('pdf_parse_error', 'Error parsing PDF: ' . $e->getMessage());
4628 + }
4629 +}
4630 +
4631 +/**
4632 + * AJAX: Get queue processing status
4633 + */
4634 +public function ajax_mxchat_get_queue_status() {
4635 + // Verify nonce and permissions
4636 + check_ajax_referer('mxchat_queue_nonce', 'nonce');
4637 +
4638 + if (!current_user_can('manage_options')) {
4639 + wp_send_json_error('Unauthorized access');
4640 + }
4641 +
4642 + $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
4643 +
4644 + if (empty($queue_id)) {
4645 + wp_send_json_error('Missing queue ID');
4646 + }
4647 +
4648 + global $wpdb;
4649 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
4650 +
4651 + // Get counts by status
4652 + $counts = $wpdb->get_results($wpdb->prepare(
4653 + "SELECT status, COUNT(*) as count
4654 + FROM $table_name
4655 + WHERE queue_id = %s
4656 + GROUP BY status",
4657 + $queue_id
4658 + ), OBJECT_K);
4659 +
4660 + $total = 0;
4661 + $completed = 0;
4662 + $failed = 0;
4663 + $processing = 0;
4664 + $pending = 0;
4665 +
4666 + foreach ($counts as $status => $data) {
4667 + $count = absint($data->count);
4668 + $total += $count;
4669 +
4670 + switch ($status) {
4671 + case 'completed':
4672 + $completed = $count;
4673 + break;
4674 + case 'failed':
4675 + $failed = $count;
4676 + break;
4677 + case 'processing':
4678 + $processing = $count;
4679 + break;
4680 + case 'pending':
4681 + $pending = $count;
4682 + break;
4683 + }
4684 + }
4685 +
4686 + // Calculate percentage
4687 + $percentage = $total > 0 ? round((($completed + $failed) / $total) * 100) : 0;
4688 +
4689 + // Get failed items details
4690 + $failed_items = array();
4691 + if ($failed > 0) {
4692 + $failed_items = $wpdb->get_results($wpdb->prepare(
4693 + "SELECT item_type, item_data, error_message, attempts
4694 + FROM $table_name
4695 + WHERE queue_id = %s
4696 + AND status = 'failed'
4697 + AND attempts >= max_attempts
4698 + ORDER BY id DESC
4699 + LIMIT 50",
4700 + $queue_id
4701 + ));
4702 + }
4703 +
4704 + // Get queue metadata
4705 + $source_url = $this->mxchat_get_queue_meta($queue_id, 'source_url');
4706 + $queue_type = $this->mxchat_get_queue_meta($queue_id, 'queue_type');
4707 +
4708 + // Determine if queue is complete
4709 + $is_complete = ($pending === 0 && $processing === 0);
4710 +
4711 + wp_send_json_success(array(
4712 + 'queue_id' => $queue_id,
4713 + 'queue_type' => $queue_type,
4714 + 'source_url' => $source_url,
4715 + 'total' => $total,
4716 + 'completed' => $completed,
4717 + 'failed' => $failed,
4718 + 'processing' => $processing,
4719 + 'pending' => $pending,
4720 + 'percentage' => $percentage,
4721 + 'is_complete' => $is_complete,
4722 + 'failed_items' => $failed_items,
4723 + 'status' => $is_complete ? 'complete' : 'processing'
4724 + ));
4725 +}
4726 +
4727 +/**
4728 + * AJAX: Clear completed queue
4729 + */
4730 +public function ajax_mxchat_clear_queue() {
4731 + // Verify nonce and permissions
4732 + check_ajax_referer('mxchat_queue_nonce', 'nonce');
4733 +
4734 + if (!current_user_can('manage_options')) {
4735 + wp_send_json_error('Unauthorized access');
4736 + }
4737 +
4738 + $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
4739 +
4740 + if (empty($queue_id)) {
4741 + wp_send_json_error('Missing queue ID');
4742 + }
4743 +
4744 + global $wpdb;
4745 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
4746 + $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
4747 +
4748 + // Delete queue items
4749 + $wpdb->delete(
4750 + $table_name,
4751 + array('queue_id' => $queue_id),
4752 + array('%s')
4753 + );
4754 +
4755 + // Delete queue metadata
4756 + $wpdb->delete(
4757 + $meta_table,
4758 + array('queue_id' => $queue_id),
4759 + array('%s')
4760 + );
4761 +
4762 + wp_send_json_success(array(
4763 + 'message' => 'Queue cleared successfully'
4764 + ));
4765 +}
4766 +
4767 +/**
4768 + * AJAX: Retry failed items in queue
4769 + */
4770 +public function ajax_mxchat_retry_failed() {
4771 + // Verify nonce and permissions
4772 + check_ajax_referer('mxchat_queue_nonce', 'nonce');
4773 +
4774 + if (!current_user_can('manage_options')) {
4775 + wp_send_json_error('Unauthorized access');
4776 + }
4777 +
4778 + $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
4779 +
4780 + if (empty($queue_id)) {
4781 + wp_send_json_error('Missing queue ID');
4782 + }
4783 +
4784 + global $wpdb;
4785 + $table_name = $wpdb->prefix . 'mxchat_processing_queue';
4786 +
4787 + // Reset failed items to pending and reset attempt count
4788 + $updated = $wpdb->update(
4789 + $table_name,
4790 + array(
4791 + 'status' => 'pending',
4792 + 'attempts' => 0,
4793 + 'error_message' => null
4794 + ),
4795 + array(
4796 + 'queue_id' => $queue_id,
4797 + 'status' => 'failed'
4798 + ),
4799 + array('%s', '%d', '%s'),
4800 + array('%s', '%s')
4801 + );
4802 +
4803 + wp_send_json_success(array(
4804 + 'message' => 'Reset ' . $updated . ' failed items for retry',
4805 + 'reset_count' => $updated
4806 + ));
4807 +}
4808 +
4809 +
4810 +public function ajax_mxchat_mark_queue_complete() {
4811 + check_ajax_referer('mxchat_queue_nonce', 'nonce');
4812 +
4813 + if (!current_user_can('manage_options')) {
4814 + wp_send_json_error('Unauthorized access');
4815 + }
4816 +
4817 + $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
4818 +
4819 + if (empty($queue_id)) {
4820 + wp_send_json_error('Missing queue ID');
4821 + }
4822 +
4823 + // Clear active queue transients
4824 + if (strpos($queue_id, 'sitemap_') === 0) {
4825 + delete_transient('mxchat_active_queue_sitemap');
4826 + } else if (strpos($queue_id, 'pdf_') === 0) {
4827 + delete_transient('mxchat_active_queue_pdf');
4828 + }
4829 +
4830 + wp_send_json_success(array('message' => 'Queue marked as complete'));
4831 +}
4832 +
4833 +
4834 + // ========================================
4835 + // STATIC ACCESS METHODS
4836 + // ========================================
4837 +
4838 + /**
4839 + * Get singleton instance
4840 + */
4841 + public static function get_instance() {
4842 + static $instance = null;
4843 + if ($instance === null) {
4844 + $instance = new self();
4845 + }
4846 + return $instance;
4847 + }
4848 +}
4849 +
4850 +// Initialize the Knowledge manager
8407 4851 $mxchat_knowledge_manager = MxChat_Knowledge_Manager::get_instance();